blob: 9e22dead259ab6900b69f4948a2520ba87f20ef8 [file] [log] [blame]
Eli Cohene126ba92013-07-07 17:25:49 +03001/*
Saeed Mahameed6cf0a152015-04-02 17:07:30 +03002 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
Eli Cohene126ba92013-07-07 17:25:49 +03003 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33#include <linux/module.h>
34#include <rdma/ib_umem.h>
Achiad Shochat2811ba52015-12-23 18:47:24 +020035#include <rdma/ib_cache.h>
Haggai Abramovskycfb5e082016-01-14 19:12:57 +020036#include <rdma/ib_user_verbs.h>
Yishai Hadasc2e53b22017-06-08 16:15:08 +030037#include <linux/mlx5/fs.h>
Eli Cohene126ba92013-07-07 17:25:49 +030038#include "mlx5_ib.h"
Eli Cohene126ba92013-07-07 17:25:49 +030039
40/* not supported currently */
41static int wq_signature;
42
43enum {
44 MLX5_IB_ACK_REQ_FREQ = 8,
45};
46
47enum {
48 MLX5_IB_DEFAULT_SCHED_QUEUE = 0x83,
49 MLX5_IB_DEFAULT_QP0_SCHED_QUEUE = 0x3f,
50 MLX5_IB_LINK_TYPE_IB = 0,
51 MLX5_IB_LINK_TYPE_ETH = 1
52};
53
54enum {
55 MLX5_IB_SQ_STRIDE = 6,
Eli Cohene126ba92013-07-07 17:25:49 +030056};
57
58static const u32 mlx5_ib_opcode[] = {
59 [IB_WR_SEND] = MLX5_OPCODE_SEND,
Erez Shitritf0313962016-02-21 16:27:17 +020060 [IB_WR_LSO] = MLX5_OPCODE_LSO,
Eli Cohene126ba92013-07-07 17:25:49 +030061 [IB_WR_SEND_WITH_IMM] = MLX5_OPCODE_SEND_IMM,
62 [IB_WR_RDMA_WRITE] = MLX5_OPCODE_RDMA_WRITE,
63 [IB_WR_RDMA_WRITE_WITH_IMM] = MLX5_OPCODE_RDMA_WRITE_IMM,
64 [IB_WR_RDMA_READ] = MLX5_OPCODE_RDMA_READ,
65 [IB_WR_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_CS,
66 [IB_WR_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_FA,
67 [IB_WR_SEND_WITH_INV] = MLX5_OPCODE_SEND_INVAL,
68 [IB_WR_LOCAL_INV] = MLX5_OPCODE_UMR,
Sagi Grimberg8a187ee2015-10-13 19:11:26 +030069 [IB_WR_REG_MR] = MLX5_OPCODE_UMR,
Eli Cohene126ba92013-07-07 17:25:49 +030070 [IB_WR_MASKED_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_MASKED_CS,
71 [IB_WR_MASKED_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_MASKED_FA,
72 [MLX5_IB_WR_UMR] = MLX5_OPCODE_UMR,
73};
74
Erez Shitritf0313962016-02-21 16:27:17 +020075struct mlx5_wqe_eth_pad {
76 u8 rsvd0[16];
77};
Eli Cohene126ba92013-07-07 17:25:49 +030078
Alex Veskereb49ab02016-08-28 12:25:53 +030079enum raw_qp_set_mask_map {
80 MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID = 1UL << 0,
Bodong Wang7d29f342016-12-01 13:43:16 +020081 MLX5_RAW_QP_RATE_LIMIT = 1UL << 1,
Alex Veskereb49ab02016-08-28 12:25:53 +030082};
83
Alex Vesker0680efa2016-08-28 12:25:52 +030084struct mlx5_modify_raw_qp_param {
85 u16 operation;
Alex Veskereb49ab02016-08-28 12:25:53 +030086
87 u32 set_mask; /* raw_qp_set_mask_map */
Bodong Wang7d29f342016-12-01 13:43:16 +020088 u32 rate_limit;
Alex Veskereb49ab02016-08-28 12:25:53 +030089 u8 rq_q_ctr_id;
Alex Vesker0680efa2016-08-28 12:25:52 +030090};
91
Maor Gottlieb89ea94a72016-06-17 15:01:38 +030092static void get_cqs(enum ib_qp_type qp_type,
93 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
94 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq);
95
Eli Cohene126ba92013-07-07 17:25:49 +030096static int is_qp0(enum ib_qp_type qp_type)
97{
98 return qp_type == IB_QPT_SMI;
99}
100
Eli Cohene126ba92013-07-07 17:25:49 +0300101static int is_sqp(enum ib_qp_type qp_type)
102{
103 return is_qp0(qp_type) || is_qp1(qp_type);
104}
105
106static void *get_wqe(struct mlx5_ib_qp *qp, int offset)
107{
108 return mlx5_buf_offset(&qp->buf, offset);
109}
110
111static void *get_recv_wqe(struct mlx5_ib_qp *qp, int n)
112{
113 return get_wqe(qp, qp->rq.offset + (n << qp->rq.wqe_shift));
114}
115
116void *mlx5_get_send_wqe(struct mlx5_ib_qp *qp, int n)
117{
118 return get_wqe(qp, qp->sq.offset + (n << MLX5_IB_SQ_STRIDE));
119}
120
Haggai Eranc1395a22014-12-11 17:04:14 +0200121/**
122 * mlx5_ib_read_user_wqe() - Copy a user-space WQE to kernel space.
123 *
124 * @qp: QP to copy from.
125 * @send: copy from the send queue when non-zero, use the receive queue
126 * otherwise.
127 * @wqe_index: index to start copying from. For send work queues, the
128 * wqe_index is in units of MLX5_SEND_WQE_BB.
129 * For receive work queue, it is the number of work queue
130 * element in the queue.
131 * @buffer: destination buffer.
132 * @length: maximum number of bytes to copy.
133 *
134 * Copies at least a single WQE, but may copy more data.
135 *
136 * Return: the number of bytes copied, or an error code.
137 */
138int mlx5_ib_read_user_wqe(struct mlx5_ib_qp *qp, int send, int wqe_index,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200139 void *buffer, u32 length,
140 struct mlx5_ib_qp_base *base)
Haggai Eranc1395a22014-12-11 17:04:14 +0200141{
142 struct ib_device *ibdev = qp->ibqp.device;
143 struct mlx5_ib_dev *dev = to_mdev(ibdev);
144 struct mlx5_ib_wq *wq = send ? &qp->sq : &qp->rq;
145 size_t offset;
146 size_t wq_end;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200147 struct ib_umem *umem = base->ubuffer.umem;
Haggai Eranc1395a22014-12-11 17:04:14 +0200148 u32 first_copy_length;
149 int wqe_length;
150 int ret;
151
152 if (wq->wqe_cnt == 0) {
153 mlx5_ib_dbg(dev, "mlx5_ib_read_user_wqe for a QP with wqe_cnt == 0. qp_type: 0x%x\n",
154 qp->ibqp.qp_type);
155 return -EINVAL;
156 }
157
158 offset = wq->offset + ((wqe_index % wq->wqe_cnt) << wq->wqe_shift);
159 wq_end = wq->offset + (wq->wqe_cnt << wq->wqe_shift);
160
161 if (send && length < sizeof(struct mlx5_wqe_ctrl_seg))
162 return -EINVAL;
163
164 if (offset > umem->length ||
165 (send && offset + sizeof(struct mlx5_wqe_ctrl_seg) > umem->length))
166 return -EINVAL;
167
168 first_copy_length = min_t(u32, offset + length, wq_end) - offset;
169 ret = ib_umem_copy_from(buffer, umem, offset, first_copy_length);
170 if (ret)
171 return ret;
172
173 if (send) {
174 struct mlx5_wqe_ctrl_seg *ctrl = buffer;
175 int ds = be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_DS_MASK;
176
177 wqe_length = ds * MLX5_WQE_DS_UNITS;
178 } else {
179 wqe_length = 1 << wq->wqe_shift;
180 }
181
182 if (wqe_length <= first_copy_length)
183 return first_copy_length;
184
185 ret = ib_umem_copy_from(buffer + first_copy_length, umem, wq->offset,
186 wqe_length - first_copy_length);
187 if (ret)
188 return ret;
189
190 return wqe_length;
191}
192
Eli Cohene126ba92013-07-07 17:25:49 +0300193static void mlx5_ib_qp_event(struct mlx5_core_qp *qp, int type)
194{
195 struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
196 struct ib_event event;
197
majd@mellanox.com19098df2016-01-14 19:13:03 +0200198 if (type == MLX5_EVENT_TYPE_PATH_MIG) {
199 /* This event is only valid for trans_qps */
200 to_mibqp(qp)->port = to_mibqp(qp)->trans_qp.alt_port;
201 }
Eli Cohene126ba92013-07-07 17:25:49 +0300202
203 if (ibqp->event_handler) {
204 event.device = ibqp->device;
205 event.element.qp = ibqp;
206 switch (type) {
207 case MLX5_EVENT_TYPE_PATH_MIG:
208 event.event = IB_EVENT_PATH_MIG;
209 break;
210 case MLX5_EVENT_TYPE_COMM_EST:
211 event.event = IB_EVENT_COMM_EST;
212 break;
213 case MLX5_EVENT_TYPE_SQ_DRAINED:
214 event.event = IB_EVENT_SQ_DRAINED;
215 break;
216 case MLX5_EVENT_TYPE_SRQ_LAST_WQE:
217 event.event = IB_EVENT_QP_LAST_WQE_REACHED;
218 break;
219 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
220 event.event = IB_EVENT_QP_FATAL;
221 break;
222 case MLX5_EVENT_TYPE_PATH_MIG_FAILED:
223 event.event = IB_EVENT_PATH_MIG_ERR;
224 break;
225 case MLX5_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
226 event.event = IB_EVENT_QP_REQ_ERR;
227 break;
228 case MLX5_EVENT_TYPE_WQ_ACCESS_ERROR:
229 event.event = IB_EVENT_QP_ACCESS_ERR;
230 break;
231 default:
232 pr_warn("mlx5_ib: Unexpected event type %d on QP %06x\n", type, qp->qpn);
233 return;
234 }
235
236 ibqp->event_handler(&event, ibqp->qp_context);
237 }
238}
239
240static int set_rq_size(struct mlx5_ib_dev *dev, struct ib_qp_cap *cap,
241 int has_rq, struct mlx5_ib_qp *qp, struct mlx5_ib_create_qp *ucmd)
242{
243 int wqe_size;
244 int wq_size;
245
246 /* Sanity check RQ size before proceeding */
Saeed Mahameed938fe832015-05-28 22:28:41 +0300247 if (cap->max_recv_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz)))
Eli Cohene126ba92013-07-07 17:25:49 +0300248 return -EINVAL;
249
250 if (!has_rq) {
251 qp->rq.max_gs = 0;
252 qp->rq.wqe_cnt = 0;
253 qp->rq.wqe_shift = 0;
Noa Osherovich0540d812016-06-04 15:15:32 +0300254 cap->max_recv_wr = 0;
255 cap->max_recv_sge = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300256 } else {
257 if (ucmd) {
258 qp->rq.wqe_cnt = ucmd->rq_wqe_count;
259 qp->rq.wqe_shift = ucmd->rq_wqe_shift;
260 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
261 qp->rq.max_post = qp->rq.wqe_cnt;
262 } else {
263 wqe_size = qp->wq_sig ? sizeof(struct mlx5_wqe_signature_seg) : 0;
264 wqe_size += cap->max_recv_sge * sizeof(struct mlx5_wqe_data_seg);
265 wqe_size = roundup_pow_of_two(wqe_size);
266 wq_size = roundup_pow_of_two(cap->max_recv_wr) * wqe_size;
267 wq_size = max_t(int, wq_size, MLX5_SEND_WQE_BB);
268 qp->rq.wqe_cnt = wq_size / wqe_size;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300269 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300270 mlx5_ib_dbg(dev, "wqe_size %d, max %d\n",
271 wqe_size,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300272 MLX5_CAP_GEN(dev->mdev,
273 max_wqe_sz_rq));
Eli Cohene126ba92013-07-07 17:25:49 +0300274 return -EINVAL;
275 }
276 qp->rq.wqe_shift = ilog2(wqe_size);
277 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
278 qp->rq.max_post = qp->rq.wqe_cnt;
279 }
280 }
281
282 return 0;
283}
284
Erez Shitritf0313962016-02-21 16:27:17 +0200285static int sq_overhead(struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300286{
Andi Shyti618af382013-07-16 15:35:01 +0200287 int size = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300288
Erez Shitritf0313962016-02-21 16:27:17 +0200289 switch (attr->qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +0300290 case IB_QPT_XRC_INI:
Eli Cohenb125a542013-09-11 16:35:22 +0300291 size += sizeof(struct mlx5_wqe_xrc_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300292 /* fall through */
293 case IB_QPT_RC:
294 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200295 max(sizeof(struct mlx5_wqe_atomic_seg) +
296 sizeof(struct mlx5_wqe_raddr_seg),
297 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
298 sizeof(struct mlx5_mkey_seg));
Eli Cohene126ba92013-07-07 17:25:49 +0300299 break;
300
Eli Cohenb125a542013-09-11 16:35:22 +0300301 case IB_QPT_XRC_TGT:
302 return 0;
303
Eli Cohene126ba92013-07-07 17:25:49 +0300304 case IB_QPT_UC:
Eli Cohenb125a542013-09-11 16:35:22 +0300305 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200306 max(sizeof(struct mlx5_wqe_raddr_seg),
307 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
308 sizeof(struct mlx5_mkey_seg));
Eli Cohene126ba92013-07-07 17:25:49 +0300309 break;
310
311 case IB_QPT_UD:
Erez Shitritf0313962016-02-21 16:27:17 +0200312 if (attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
313 size += sizeof(struct mlx5_wqe_eth_pad) +
314 sizeof(struct mlx5_wqe_eth_seg);
315 /* fall through */
Eli Cohene126ba92013-07-07 17:25:49 +0300316 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +0200317 case MLX5_IB_QPT_HW_GSI:
Eli Cohenb125a542013-09-11 16:35:22 +0300318 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300319 sizeof(struct mlx5_wqe_datagram_seg);
320 break;
321
322 case MLX5_IB_QPT_REG_UMR:
Eli Cohenb125a542013-09-11 16:35:22 +0300323 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300324 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
325 sizeof(struct mlx5_mkey_seg);
326 break;
327
328 default:
329 return -EINVAL;
330 }
331
332 return size;
333}
334
335static int calc_send_wqe(struct ib_qp_init_attr *attr)
336{
337 int inl_size = 0;
338 int size;
339
Erez Shitritf0313962016-02-21 16:27:17 +0200340 size = sq_overhead(attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300341 if (size < 0)
342 return size;
343
344 if (attr->cap.max_inline_data) {
345 inl_size = size + sizeof(struct mlx5_wqe_inline_seg) +
346 attr->cap.max_inline_data;
347 }
348
349 size += attr->cap.max_send_sge * sizeof(struct mlx5_wqe_data_seg);
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200350 if (attr->create_flags & IB_QP_CREATE_SIGNATURE_EN &&
351 ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB) < MLX5_SIG_WQE_SIZE)
352 return MLX5_SIG_WQE_SIZE;
353 else
354 return ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB);
Eli Cohene126ba92013-07-07 17:25:49 +0300355}
356
Eli Cohen288c01b2016-10-27 16:36:45 +0300357static int get_send_sge(struct ib_qp_init_attr *attr, int wqe_size)
358{
359 int max_sge;
360
361 if (attr->qp_type == IB_QPT_RC)
362 max_sge = (min_t(int, wqe_size, 512) -
363 sizeof(struct mlx5_wqe_ctrl_seg) -
364 sizeof(struct mlx5_wqe_raddr_seg)) /
365 sizeof(struct mlx5_wqe_data_seg);
366 else if (attr->qp_type == IB_QPT_XRC_INI)
367 max_sge = (min_t(int, wqe_size, 512) -
368 sizeof(struct mlx5_wqe_ctrl_seg) -
369 sizeof(struct mlx5_wqe_xrc_seg) -
370 sizeof(struct mlx5_wqe_raddr_seg)) /
371 sizeof(struct mlx5_wqe_data_seg);
372 else
373 max_sge = (wqe_size - sq_overhead(attr)) /
374 sizeof(struct mlx5_wqe_data_seg);
375
376 return min_t(int, max_sge, wqe_size - sq_overhead(attr) /
377 sizeof(struct mlx5_wqe_data_seg));
378}
379
Eli Cohene126ba92013-07-07 17:25:49 +0300380static int calc_sq_size(struct mlx5_ib_dev *dev, struct ib_qp_init_attr *attr,
381 struct mlx5_ib_qp *qp)
382{
383 int wqe_size;
384 int wq_size;
385
386 if (!attr->cap.max_send_wr)
387 return 0;
388
389 wqe_size = calc_send_wqe(attr);
390 mlx5_ib_dbg(dev, "wqe_size %d\n", wqe_size);
391 if (wqe_size < 0)
392 return wqe_size;
393
Saeed Mahameed938fe832015-05-28 22:28:41 +0300394 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohenb125a542013-09-11 16:35:22 +0300395 mlx5_ib_dbg(dev, "wqe_size(%d) > max_sq_desc_sz(%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300396 wqe_size, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300397 return -EINVAL;
398 }
399
Erez Shitritf0313962016-02-21 16:27:17 +0200400 qp->max_inline_data = wqe_size - sq_overhead(attr) -
401 sizeof(struct mlx5_wqe_inline_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300402 attr->cap.max_inline_data = qp->max_inline_data;
403
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200404 if (attr->create_flags & IB_QP_CREATE_SIGNATURE_EN)
405 qp->signature_en = true;
406
Eli Cohene126ba92013-07-07 17:25:49 +0300407 wq_size = roundup_pow_of_two(attr->cap.max_send_wr * wqe_size);
408 qp->sq.wqe_cnt = wq_size / MLX5_SEND_WQE_BB;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300409 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Bart Van Assche1974ab92016-12-05 17:19:52 -0800410 mlx5_ib_dbg(dev, "send queue size (%d * %d / %d -> %d) exceeds limits(%d)\n",
411 attr->cap.max_send_wr, wqe_size, MLX5_SEND_WQE_BB,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300412 qp->sq.wqe_cnt,
413 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohenb125a542013-09-11 16:35:22 +0300414 return -ENOMEM;
415 }
Eli Cohene126ba92013-07-07 17:25:49 +0300416 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
Eli Cohen288c01b2016-10-27 16:36:45 +0300417 qp->sq.max_gs = get_send_sge(attr, wqe_size);
418 if (qp->sq.max_gs < attr->cap.max_send_sge)
419 return -ENOMEM;
420
421 attr->cap.max_send_sge = qp->sq.max_gs;
Eli Cohenb125a542013-09-11 16:35:22 +0300422 qp->sq.max_post = wq_size / wqe_size;
423 attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +0300424
425 return wq_size;
426}
427
428static int set_user_buf_size(struct mlx5_ib_dev *dev,
429 struct mlx5_ib_qp *qp,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200430 struct mlx5_ib_create_qp *ucmd,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200431 struct mlx5_ib_qp_base *base,
432 struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300433{
434 int desc_sz = 1 << qp->sq.wqe_shift;
435
Saeed Mahameed938fe832015-05-28 22:28:41 +0300436 if (desc_sz > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300437 mlx5_ib_warn(dev, "desc_sz %d, max_sq_desc_sz %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300438 desc_sz, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300439 return -EINVAL;
440 }
441
442 if (ucmd->sq_wqe_count && ((1 << ilog2(ucmd->sq_wqe_count)) != ucmd->sq_wqe_count)) {
443 mlx5_ib_warn(dev, "sq_wqe_count %d, sq_wqe_count %d\n",
444 ucmd->sq_wqe_count, ucmd->sq_wqe_count);
445 return -EINVAL;
446 }
447
448 qp->sq.wqe_cnt = ucmd->sq_wqe_count;
449
Saeed Mahameed938fe832015-05-28 22:28:41 +0300450 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Eli Cohene126ba92013-07-07 17:25:49 +0300451 mlx5_ib_warn(dev, "wqe_cnt %d, max_wqes %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300452 qp->sq.wqe_cnt,
453 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohene126ba92013-07-07 17:25:49 +0300454 return -EINVAL;
455 }
456
Yishai Hadasc2e53b22017-06-08 16:15:08 +0300457 if (attr->qp_type == IB_QPT_RAW_PACKET ||
458 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200459 base->ubuffer.buf_size = qp->rq.wqe_cnt << qp->rq.wqe_shift;
460 qp->raw_packet_qp.sq.ubuffer.buf_size = qp->sq.wqe_cnt << 6;
461 } else {
462 base->ubuffer.buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
463 (qp->sq.wqe_cnt << 6);
464 }
Eli Cohene126ba92013-07-07 17:25:49 +0300465
466 return 0;
467}
468
469static int qp_has_rq(struct ib_qp_init_attr *attr)
470{
471 if (attr->qp_type == IB_QPT_XRC_INI ||
472 attr->qp_type == IB_QPT_XRC_TGT || attr->srq ||
473 attr->qp_type == MLX5_IB_QPT_REG_UMR ||
474 !attr->cap.max_recv_wr)
475 return 0;
476
477 return 1;
478}
479
Eli Cohen2f5ff262017-01-03 23:55:21 +0200480static int first_med_bfreg(void)
Eli Cohenc1be5232014-01-14 17:45:12 +0200481{
482 return 1;
483}
484
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200485enum {
486 /* this is the first blue flame register in the array of bfregs assigned
487 * to a processes. Since we do not use it for blue flame but rather
488 * regular 64 bit doorbells, we do not need a lock for maintaiing
489 * "odd/even" order
490 */
491 NUM_NON_BLUE_FLAME_BFREGS = 1,
492};
493
Eli Cohenb037c292017-01-03 23:55:26 +0200494static int max_bfregs(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi)
495{
496 return get_num_uars(dev, bfregi) * MLX5_NON_FP_BFREGS_PER_UAR;
497}
498
499static int num_med_bfreg(struct mlx5_ib_dev *dev,
500 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200501{
502 int n;
503
Eli Cohenb037c292017-01-03 23:55:26 +0200504 n = max_bfregs(dev, bfregi) - bfregi->num_low_latency_bfregs -
505 NUM_NON_BLUE_FLAME_BFREGS;
Eli Cohenc1be5232014-01-14 17:45:12 +0200506
507 return n >= 0 ? n : 0;
508}
509
Eli Cohenb037c292017-01-03 23:55:26 +0200510static int first_hi_bfreg(struct mlx5_ib_dev *dev,
511 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200512{
513 int med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200514
Eli Cohenb037c292017-01-03 23:55:26 +0200515 med = num_med_bfreg(dev, bfregi);
516 return ++med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200517}
518
Eli Cohenb037c292017-01-03 23:55:26 +0200519static int alloc_high_class_bfreg(struct mlx5_ib_dev *dev,
520 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300521{
Eli Cohene126ba92013-07-07 17:25:49 +0300522 int i;
523
Eli Cohenb037c292017-01-03 23:55:26 +0200524 for (i = first_hi_bfreg(dev, bfregi); i < max_bfregs(dev, bfregi); i++) {
525 if (!bfregi->count[i]) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200526 bfregi->count[i]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300527 return i;
528 }
529 }
530
531 return -ENOMEM;
532}
533
Eli Cohenb037c292017-01-03 23:55:26 +0200534static int alloc_med_class_bfreg(struct mlx5_ib_dev *dev,
535 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300536{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200537 int minidx = first_med_bfreg();
Eli Cohene126ba92013-07-07 17:25:49 +0300538 int i;
539
Eli Cohenb037c292017-01-03 23:55:26 +0200540 for (i = first_med_bfreg(); i < first_hi_bfreg(dev, bfregi); i++) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200541 if (bfregi->count[i] < bfregi->count[minidx])
Eli Cohene126ba92013-07-07 17:25:49 +0300542 minidx = i;
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200543 if (!bfregi->count[minidx])
544 break;
Eli Cohene126ba92013-07-07 17:25:49 +0300545 }
546
Eli Cohen2f5ff262017-01-03 23:55:21 +0200547 bfregi->count[minidx]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300548 return minidx;
549}
550
Eli Cohenb037c292017-01-03 23:55:26 +0200551static int alloc_bfreg(struct mlx5_ib_dev *dev,
552 struct mlx5_bfreg_info *bfregi,
Eli Cohen2f5ff262017-01-03 23:55:21 +0200553 enum mlx5_ib_latency_class lat)
Eli Cohene126ba92013-07-07 17:25:49 +0300554{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200555 int bfregn = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300556
Eli Cohen2f5ff262017-01-03 23:55:21 +0200557 mutex_lock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300558 switch (lat) {
559 case MLX5_IB_LATENCY_CLASS_LOW:
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200560 BUILD_BUG_ON(NUM_NON_BLUE_FLAME_BFREGS != 1);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200561 bfregn = 0;
562 bfregi->count[bfregn]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300563 break;
564
565 case MLX5_IB_LATENCY_CLASS_MEDIUM:
Eli Cohen2f5ff262017-01-03 23:55:21 +0200566 if (bfregi->ver < 2)
567 bfregn = -ENOMEM;
Eli Cohen78c0f982014-01-30 13:49:48 +0200568 else
Eli Cohenb037c292017-01-03 23:55:26 +0200569 bfregn = alloc_med_class_bfreg(dev, bfregi);
Eli Cohene126ba92013-07-07 17:25:49 +0300570 break;
571
572 case MLX5_IB_LATENCY_CLASS_HIGH:
Eli Cohen2f5ff262017-01-03 23:55:21 +0200573 if (bfregi->ver < 2)
574 bfregn = -ENOMEM;
Eli Cohen78c0f982014-01-30 13:49:48 +0200575 else
Eli Cohenb037c292017-01-03 23:55:26 +0200576 bfregn = alloc_high_class_bfreg(dev, bfregi);
Eli Cohene126ba92013-07-07 17:25:49 +0300577 break;
578 }
Eli Cohen2f5ff262017-01-03 23:55:21 +0200579 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300580
Eli Cohen2f5ff262017-01-03 23:55:21 +0200581 return bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300582}
583
Eli Cohenb037c292017-01-03 23:55:26 +0200584static void free_bfreg(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi, int bfregn)
Eli Cohene126ba92013-07-07 17:25:49 +0300585{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200586 mutex_lock(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +0200587 bfregi->count[bfregn]--;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200588 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300589}
590
591static enum mlx5_qp_state to_mlx5_state(enum ib_qp_state state)
592{
593 switch (state) {
594 case IB_QPS_RESET: return MLX5_QP_STATE_RST;
595 case IB_QPS_INIT: return MLX5_QP_STATE_INIT;
596 case IB_QPS_RTR: return MLX5_QP_STATE_RTR;
597 case IB_QPS_RTS: return MLX5_QP_STATE_RTS;
598 case IB_QPS_SQD: return MLX5_QP_STATE_SQD;
599 case IB_QPS_SQE: return MLX5_QP_STATE_SQER;
600 case IB_QPS_ERR: return MLX5_QP_STATE_ERR;
601 default: return -1;
602 }
603}
604
605static int to_mlx5_st(enum ib_qp_type type)
606{
607 switch (type) {
608 case IB_QPT_RC: return MLX5_QP_ST_RC;
609 case IB_QPT_UC: return MLX5_QP_ST_UC;
610 case IB_QPT_UD: return MLX5_QP_ST_UD;
611 case MLX5_IB_QPT_REG_UMR: return MLX5_QP_ST_REG_UMR;
612 case IB_QPT_XRC_INI:
613 case IB_QPT_XRC_TGT: return MLX5_QP_ST_XRC;
614 case IB_QPT_SMI: return MLX5_QP_ST_QP0;
Haggai Erand16e91d2016-02-29 15:45:05 +0200615 case MLX5_IB_QPT_HW_GSI: return MLX5_QP_ST_QP1;
Eli Cohene126ba92013-07-07 17:25:49 +0300616 case IB_QPT_RAW_IPV6: return MLX5_QP_ST_RAW_IPV6;
Eli Cohene126ba92013-07-07 17:25:49 +0300617 case IB_QPT_RAW_PACKET:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200618 case IB_QPT_RAW_ETHERTYPE: return MLX5_QP_ST_RAW_ETHERTYPE;
Eli Cohene126ba92013-07-07 17:25:49 +0300619 case IB_QPT_MAX:
620 default: return -EINVAL;
621 }
622}
623
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300624static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq,
625 struct mlx5_ib_cq *recv_cq);
626static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq,
627 struct mlx5_ib_cq *recv_cq);
628
Eli Cohenb037c292017-01-03 23:55:26 +0200629static int bfregn_to_uar_index(struct mlx5_ib_dev *dev,
630 struct mlx5_bfreg_info *bfregi, int bfregn)
Eli Cohene126ba92013-07-07 17:25:49 +0300631{
Eli Cohenb037c292017-01-03 23:55:26 +0200632 int bfregs_per_sys_page;
633 int index_of_sys_page;
634 int offset;
635
636 bfregs_per_sys_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k) *
637 MLX5_NON_FP_BFREGS_PER_UAR;
638 index_of_sys_page = bfregn / bfregs_per_sys_page;
639
640 offset = bfregn % bfregs_per_sys_page / MLX5_NON_FP_BFREGS_PER_UAR;
641
642 return bfregi->sys_pages[index_of_sys_page] + offset;
Eli Cohene126ba92013-07-07 17:25:49 +0300643}
644
majd@mellanox.com19098df2016-01-14 19:13:03 +0200645static int mlx5_ib_umem_get(struct mlx5_ib_dev *dev,
646 struct ib_pd *pd,
647 unsigned long addr, size_t size,
648 struct ib_umem **umem,
649 int *npages, int *page_shift, int *ncont,
650 u32 *offset)
651{
652 int err;
653
654 *umem = ib_umem_get(pd->uobject->context, addr, size, 0, 0);
655 if (IS_ERR(*umem)) {
656 mlx5_ib_dbg(dev, "umem_get failed\n");
657 return PTR_ERR(*umem);
658 }
659
Majd Dibbiny762f8992016-10-27 16:36:47 +0300660 mlx5_ib_cont_pages(*umem, addr, 0, npages, page_shift, ncont, NULL);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200661
662 err = mlx5_ib_get_buf_offset(addr, *page_shift, offset);
663 if (err) {
664 mlx5_ib_warn(dev, "bad offset\n");
665 goto err_umem;
666 }
667
668 mlx5_ib_dbg(dev, "addr 0x%lx, size %zu, npages %d, page_shift %d, ncont %d, offset %d\n",
669 addr, size, *npages, *page_shift, *ncont, *offset);
670
671 return 0;
672
673err_umem:
674 ib_umem_release(*umem);
675 *umem = NULL;
676
677 return err;
678}
679
Maor Gottliebfe248c32017-05-30 10:29:14 +0300680static void destroy_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
681 struct mlx5_ib_rwq *rwq)
Yishai Hadas79b20a62016-05-23 15:20:50 +0300682{
683 struct mlx5_ib_ucontext *context;
684
Maor Gottliebfe248c32017-05-30 10:29:14 +0300685 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_DELAY_DROP)
686 atomic_dec(&dev->delay_drop.rqs_cnt);
687
Yishai Hadas79b20a62016-05-23 15:20:50 +0300688 context = to_mucontext(pd->uobject->context);
689 mlx5_ib_db_unmap_user(context, &rwq->db);
690 if (rwq->umem)
691 ib_umem_release(rwq->umem);
692}
693
694static int create_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
695 struct mlx5_ib_rwq *rwq,
696 struct mlx5_ib_create_wq *ucmd)
697{
698 struct mlx5_ib_ucontext *context;
699 int page_shift = 0;
700 int npages;
701 u32 offset = 0;
702 int ncont = 0;
703 int err;
704
705 if (!ucmd->buf_addr)
706 return -EINVAL;
707
708 context = to_mucontext(pd->uobject->context);
709 rwq->umem = ib_umem_get(pd->uobject->context, ucmd->buf_addr,
710 rwq->buf_size, 0, 0);
711 if (IS_ERR(rwq->umem)) {
712 mlx5_ib_dbg(dev, "umem_get failed\n");
713 err = PTR_ERR(rwq->umem);
714 return err;
715 }
716
Majd Dibbiny762f8992016-10-27 16:36:47 +0300717 mlx5_ib_cont_pages(rwq->umem, ucmd->buf_addr, 0, &npages, &page_shift,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300718 &ncont, NULL);
719 err = mlx5_ib_get_buf_offset(ucmd->buf_addr, page_shift,
720 &rwq->rq_page_offset);
721 if (err) {
722 mlx5_ib_warn(dev, "bad offset\n");
723 goto err_umem;
724 }
725
726 rwq->rq_num_pas = ncont;
727 rwq->page_shift = page_shift;
728 rwq->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
729 rwq->wq_sig = !!(ucmd->flags & MLX5_WQ_FLAG_SIGNATURE);
730
731 mlx5_ib_dbg(dev, "addr 0x%llx, size %zd, npages %d, page_shift %d, ncont %d, offset %d\n",
732 (unsigned long long)ucmd->buf_addr, rwq->buf_size,
733 npages, page_shift, ncont, offset);
734
735 err = mlx5_ib_db_map_user(context, ucmd->db_addr, &rwq->db);
736 if (err) {
737 mlx5_ib_dbg(dev, "map failed\n");
738 goto err_umem;
739 }
740
741 rwq->create_type = MLX5_WQ_USER;
742 return 0;
743
744err_umem:
745 ib_umem_release(rwq->umem);
746 return err;
747}
748
Eli Cohenb037c292017-01-03 23:55:26 +0200749static int adjust_bfregn(struct mlx5_ib_dev *dev,
750 struct mlx5_bfreg_info *bfregi, int bfregn)
751{
752 return bfregn / MLX5_NON_FP_BFREGS_PER_UAR * MLX5_BFREGS_PER_UAR +
753 bfregn % MLX5_NON_FP_BFREGS_PER_UAR;
754}
755
Eli Cohene126ba92013-07-07 17:25:49 +0300756static int create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
757 struct mlx5_ib_qp *qp, struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200758 struct ib_qp_init_attr *attr,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300759 u32 **in,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200760 struct mlx5_ib_create_qp_resp *resp, int *inlen,
761 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300762{
763 struct mlx5_ib_ucontext *context;
764 struct mlx5_ib_create_qp ucmd;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200765 struct mlx5_ib_ubuffer *ubuffer = &base->ubuffer;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200766 int page_shift = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300767 int uar_index;
768 int npages;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200769 u32 offset = 0;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200770 int bfregn;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200771 int ncont = 0;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300772 __be64 *pas;
773 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300774 int err;
775
776 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
777 if (err) {
778 mlx5_ib_dbg(dev, "copy failed\n");
779 return err;
780 }
781
782 context = to_mucontext(pd->uobject->context);
783 /*
784 * TBD: should come from the verbs when we have the API
785 */
Leon Romanovsky051f2632015-12-20 12:16:11 +0200786 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
787 /* In CROSS_CHANNEL CQ and QP must use the same UAR */
Eli Cohen2f5ff262017-01-03 23:55:21 +0200788 bfregn = MLX5_CROSS_CHANNEL_BFREG;
Leon Romanovsky051f2632015-12-20 12:16:11 +0200789 else {
Eli Cohenb037c292017-01-03 23:55:26 +0200790 bfregn = alloc_bfreg(dev, &context->bfregi, MLX5_IB_LATENCY_CLASS_HIGH);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200791 if (bfregn < 0) {
792 mlx5_ib_dbg(dev, "failed to allocate low latency BFREG\n");
Leon Romanovsky051f2632015-12-20 12:16:11 +0200793 mlx5_ib_dbg(dev, "reverting to medium latency\n");
Eli Cohenb037c292017-01-03 23:55:26 +0200794 bfregn = alloc_bfreg(dev, &context->bfregi, MLX5_IB_LATENCY_CLASS_MEDIUM);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200795 if (bfregn < 0) {
796 mlx5_ib_dbg(dev, "failed to allocate medium latency BFREG\n");
Leon Romanovsky051f2632015-12-20 12:16:11 +0200797 mlx5_ib_dbg(dev, "reverting to high latency\n");
Eli Cohenb037c292017-01-03 23:55:26 +0200798 bfregn = alloc_bfreg(dev, &context->bfregi, MLX5_IB_LATENCY_CLASS_LOW);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200799 if (bfregn < 0) {
800 mlx5_ib_warn(dev, "bfreg allocation failed\n");
801 return bfregn;
Leon Romanovsky051f2632015-12-20 12:16:11 +0200802 }
Eli Cohenc1be5232014-01-14 17:45:12 +0200803 }
Eli Cohene126ba92013-07-07 17:25:49 +0300804 }
805 }
806
Eli Cohenb037c292017-01-03 23:55:26 +0200807 uar_index = bfregn_to_uar_index(dev, &context->bfregi, bfregn);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200808 mlx5_ib_dbg(dev, "bfregn 0x%x, uar_index 0x%x\n", bfregn, uar_index);
Eli Cohene126ba92013-07-07 17:25:49 +0300809
Haggai Eran48fea832014-05-22 14:50:11 +0300810 qp->rq.offset = 0;
811 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
812 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
813
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200814 err = set_user_buf_size(dev, qp, &ucmd, base, attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300815 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200816 goto err_bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300817
majd@mellanox.com19098df2016-01-14 19:13:03 +0200818 if (ucmd.buf_addr && ubuffer->buf_size) {
819 ubuffer->buf_addr = ucmd.buf_addr;
820 err = mlx5_ib_umem_get(dev, pd, ubuffer->buf_addr,
821 ubuffer->buf_size,
822 &ubuffer->umem, &npages, &page_shift,
823 &ncont, &offset);
824 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200825 goto err_bfreg;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200826 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +0200827 ubuffer->umem = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300828 }
Eli Cohene126ba92013-07-07 17:25:49 +0300829
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300830 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
831 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300832 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300833 if (!*in) {
834 err = -ENOMEM;
835 goto err_umem;
836 }
Eli Cohene126ba92013-07-07 17:25:49 +0300837
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300838 pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas);
839 if (ubuffer->umem)
840 mlx5_ib_populate_pas(dev, ubuffer->umem, page_shift, pas, 0);
841
842 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
843
844 MLX5_SET(qpc, qpc, log_page_size, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
845 MLX5_SET(qpc, qpc, page_offset, offset);
846
847 MLX5_SET(qpc, qpc, uar_page, uar_index);
Eli Cohenb037c292017-01-03 23:55:26 +0200848 resp->bfreg_index = adjust_bfregn(dev, &context->bfregi, bfregn);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200849 qp->bfregn = bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300850
851 err = mlx5_ib_db_map_user(context, ucmd.db_addr, &qp->db);
852 if (err) {
853 mlx5_ib_dbg(dev, "map failed\n");
854 goto err_free;
855 }
856
857 err = ib_copy_to_udata(udata, resp, sizeof(*resp));
858 if (err) {
859 mlx5_ib_dbg(dev, "copy failed\n");
860 goto err_unmap;
861 }
862 qp->create_type = MLX5_QP_USER;
863
864 return 0;
865
866err_unmap:
867 mlx5_ib_db_unmap_user(context, &qp->db);
868
869err_free:
Al Viro479163f2014-11-20 08:13:57 +0000870 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +0300871
872err_umem:
majd@mellanox.com19098df2016-01-14 19:13:03 +0200873 if (ubuffer->umem)
874 ib_umem_release(ubuffer->umem);
Eli Cohene126ba92013-07-07 17:25:49 +0300875
Eli Cohen2f5ff262017-01-03 23:55:21 +0200876err_bfreg:
Eli Cohenb037c292017-01-03 23:55:26 +0200877 free_bfreg(dev, &context->bfregi, bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300878 return err;
879}
880
Eli Cohenb037c292017-01-03 23:55:26 +0200881static void destroy_qp_user(struct mlx5_ib_dev *dev, struct ib_pd *pd,
882 struct mlx5_ib_qp *qp, struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300883{
884 struct mlx5_ib_ucontext *context;
885
886 context = to_mucontext(pd->uobject->context);
887 mlx5_ib_db_unmap_user(context, &qp->db);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200888 if (base->ubuffer.umem)
889 ib_umem_release(base->ubuffer.umem);
Eli Cohenb037c292017-01-03 23:55:26 +0200890 free_bfreg(dev, &context->bfregi, qp->bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300891}
892
893static int create_kernel_qp(struct mlx5_ib_dev *dev,
894 struct ib_qp_init_attr *init_attr,
895 struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300896 u32 **in, int *inlen,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200897 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300898{
Eli Cohene126ba92013-07-07 17:25:49 +0300899 int uar_index;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300900 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300901 int err;
902
Erez Shitritf0313962016-02-21 16:27:17 +0200903 if (init_attr->create_flags & ~(IB_QP_CREATE_SIGNATURE_EN |
904 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
Haggai Eranb11a4f92016-02-29 15:45:03 +0200905 IB_QP_CREATE_IPOIB_UD_LSO |
Erez Shitrit93d576a2017-04-13 06:37:06 +0300906 IB_QP_CREATE_NETIF_QP |
Haggai Eranb11a4f92016-02-29 15:45:03 +0200907 mlx5_ib_create_qp_sqpn_qp1()))
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200908 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300909
910 if (init_attr->qp_type == MLX5_IB_QPT_REG_UMR)
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200911 qp->bf.bfreg = &dev->fp_bfreg;
912 else
913 qp->bf.bfreg = &dev->bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300914
Eli Cohend8030b02017-02-09 19:31:47 +0200915 /* We need to divide by two since each register is comprised of
916 * two buffers of identical size, namely odd and even
917 */
918 qp->bf.buf_size = (1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size)) / 2;
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200919 uar_index = qp->bf.bfreg->index;
Eli Cohene126ba92013-07-07 17:25:49 +0300920
921 err = calc_sq_size(dev, init_attr, qp);
922 if (err < 0) {
923 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200924 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300925 }
926
927 qp->rq.offset = 0;
928 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200929 base->ubuffer.buf_size = err + (qp->rq.wqe_cnt << qp->rq.wqe_shift);
Eli Cohene126ba92013-07-07 17:25:49 +0300930
majd@mellanox.com19098df2016-01-14 19:13:03 +0200931 err = mlx5_buf_alloc(dev->mdev, base->ubuffer.buf_size, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +0300932 if (err) {
933 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200934 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300935 }
936
937 qp->sq.qend = mlx5_get_send_wqe(qp, qp->sq.wqe_cnt);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300938 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
939 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * qp->buf.npages;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300940 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300941 if (!*in) {
942 err = -ENOMEM;
943 goto err_buf;
944 }
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300945
946 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
947 MLX5_SET(qpc, qpc, uar_page, uar_index);
948 MLX5_SET(qpc, qpc, log_page_size, qp->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
949
Eli Cohene126ba92013-07-07 17:25:49 +0300950 /* Set "fast registration enabled" for all kernel QPs */
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300951 MLX5_SET(qpc, qpc, fre, 1);
952 MLX5_SET(qpc, qpc, rlky, 1);
Eli Cohene126ba92013-07-07 17:25:49 +0300953
Haggai Eranb11a4f92016-02-29 15:45:03 +0200954 if (init_attr->create_flags & mlx5_ib_create_qp_sqpn_qp1()) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300955 MLX5_SET(qpc, qpc, deth_sqpn, 1);
Haggai Eranb11a4f92016-02-29 15:45:03 +0200956 qp->flags |= MLX5_IB_QP_SQPN_QP1;
957 }
958
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300959 mlx5_fill_page_array(&qp->buf,
960 (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas));
Eli Cohene126ba92013-07-07 17:25:49 +0300961
Jack Morgenstein9603b612014-07-28 23:30:22 +0300962 err = mlx5_db_alloc(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300963 if (err) {
964 mlx5_ib_dbg(dev, "err %d\n", err);
965 goto err_free;
966 }
967
Li Dongyangb5883002017-08-16 23:31:22 +1000968 qp->sq.wrid = kvmalloc_array(qp->sq.wqe_cnt,
969 sizeof(*qp->sq.wrid), GFP_KERNEL);
970 qp->sq.wr_data = kvmalloc_array(qp->sq.wqe_cnt,
971 sizeof(*qp->sq.wr_data), GFP_KERNEL);
972 qp->rq.wrid = kvmalloc_array(qp->rq.wqe_cnt,
973 sizeof(*qp->rq.wrid), GFP_KERNEL);
974 qp->sq.w_list = kvmalloc_array(qp->sq.wqe_cnt,
975 sizeof(*qp->sq.w_list), GFP_KERNEL);
976 qp->sq.wqe_head = kvmalloc_array(qp->sq.wqe_cnt,
977 sizeof(*qp->sq.wqe_head), GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300978
979 if (!qp->sq.wrid || !qp->sq.wr_data || !qp->rq.wrid ||
980 !qp->sq.w_list || !qp->sq.wqe_head) {
981 err = -ENOMEM;
982 goto err_wrid;
983 }
984 qp->create_type = MLX5_QP_KERNEL;
985
986 return 0;
987
988err_wrid:
Li Dongyangb5883002017-08-16 23:31:22 +1000989 kvfree(qp->sq.wqe_head);
990 kvfree(qp->sq.w_list);
991 kvfree(qp->sq.wrid);
992 kvfree(qp->sq.wr_data);
993 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +0200994 mlx5_db_free(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300995
996err_free:
Al Viro479163f2014-11-20 08:13:57 +0000997 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +0300998
999err_buf:
Jack Morgenstein9603b612014-07-28 23:30:22 +03001000 mlx5_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001001 return err;
1002}
1003
1004static void destroy_qp_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1005{
Li Dongyangb5883002017-08-16 23:31:22 +10001006 kvfree(qp->sq.wqe_head);
1007 kvfree(qp->sq.w_list);
1008 kvfree(qp->sq.wrid);
1009 kvfree(qp->sq.wr_data);
1010 kvfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001011 mlx5_db_free(dev->mdev, &qp->db);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001012 mlx5_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001013}
1014
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001015static u32 get_rx_type(struct mlx5_ib_qp *qp, struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +03001016{
1017 if (attr->srq || (attr->qp_type == IB_QPT_XRC_TGT) ||
1018 (attr->qp_type == IB_QPT_XRC_INI))
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001019 return MLX5_SRQ_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001020 else if (!qp->has_rq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001021 return MLX5_ZERO_LEN_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001022 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001023 return MLX5_NON_ZERO_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001024}
1025
1026static int is_connected(enum ib_qp_type qp_type)
1027{
1028 if (qp_type == IB_QPT_RC || qp_type == IB_QPT_UC)
1029 return 1;
1030
1031 return 0;
1032}
1033
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001034static int create_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001035 struct mlx5_ib_qp *qp,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001036 struct mlx5_ib_sq *sq, u32 tdn)
1037{
Saeed Mahameedc4f287c2016-07-19 20:17:12 +03001038 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001039 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1040
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001041 MLX5_SET(tisc, tisc, transport_domain, tdn);
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001042 if (qp->flags & MLX5_IB_QP_UNDERLAY)
1043 MLX5_SET(tisc, tisc, underlay_qpn, qp->underlay_qpn);
1044
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001045 return mlx5_core_create_tis(dev->mdev, in, sizeof(in), &sq->tisn);
1046}
1047
1048static void destroy_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
1049 struct mlx5_ib_sq *sq)
1050{
1051 mlx5_core_destroy_tis(dev->mdev, sq->tisn);
1052}
1053
1054static int create_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1055 struct mlx5_ib_sq *sq, void *qpin,
1056 struct ib_pd *pd)
1057{
1058 struct mlx5_ib_ubuffer *ubuffer = &sq->ubuffer;
1059 __be64 *pas;
1060 void *in;
1061 void *sqc;
1062 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1063 void *wq;
1064 int inlen;
1065 int err;
1066 int page_shift = 0;
1067 int npages;
1068 int ncont = 0;
1069 u32 offset = 0;
1070
1071 err = mlx5_ib_umem_get(dev, pd, ubuffer->buf_addr, ubuffer->buf_size,
1072 &sq->ubuffer.umem, &npages, &page_shift,
1073 &ncont, &offset);
1074 if (err)
1075 return err;
1076
1077 inlen = MLX5_ST_SZ_BYTES(create_sq_in) + sizeof(u64) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001078 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001079 if (!in) {
1080 err = -ENOMEM;
1081 goto err_umem;
1082 }
1083
1084 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1085 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
Bodong Wang795b6092017-08-17 15:52:34 +03001086 if (MLX5_CAP_ETH(dev->mdev, multi_pkt_send_wqe))
1087 MLX5_SET(sqc, sqc, allow_multi_pkt_send_wqe, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001088 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1089 MLX5_SET(sqc, sqc, user_index, MLX5_GET(qpc, qpc, user_index));
1090 MLX5_SET(sqc, sqc, cqn, MLX5_GET(qpc, qpc, cqn_snd));
1091 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1092 MLX5_SET(sqc, sqc, tis_num_0, sq->tisn);
Noa Osherovich96dc3fc2017-08-17 15:52:28 +03001093 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
1094 MLX5_CAP_ETH(dev->mdev, swp))
1095 MLX5_SET(sqc, sqc, allow_swp, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001096
1097 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1098 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1099 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1100 MLX5_SET(wq, wq, uar_page, MLX5_GET(qpc, qpc, uar_page));
1101 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1102 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1103 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_sq_size));
1104 MLX5_SET(wq, wq, log_wq_pg_sz, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1105 MLX5_SET(wq, wq, page_offset, offset);
1106
1107 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1108 mlx5_ib_populate_pas(dev, sq->ubuffer.umem, page_shift, pas, 0);
1109
1110 err = mlx5_core_create_sq_tracked(dev->mdev, in, inlen, &sq->base.mqp);
1111
1112 kvfree(in);
1113
1114 if (err)
1115 goto err_umem;
1116
1117 return 0;
1118
1119err_umem:
1120 ib_umem_release(sq->ubuffer.umem);
1121 sq->ubuffer.umem = NULL;
1122
1123 return err;
1124}
1125
1126static void destroy_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1127 struct mlx5_ib_sq *sq)
1128{
1129 mlx5_core_destroy_sq_tracked(dev->mdev, &sq->base.mqp);
1130 ib_umem_release(sq->ubuffer.umem);
1131}
1132
1133static int get_rq_pas_size(void *qpc)
1134{
1135 u32 log_page_size = MLX5_GET(qpc, qpc, log_page_size) + 12;
1136 u32 log_rq_stride = MLX5_GET(qpc, qpc, log_rq_stride);
1137 u32 log_rq_size = MLX5_GET(qpc, qpc, log_rq_size);
1138 u32 page_offset = MLX5_GET(qpc, qpc, page_offset);
1139 u32 po_quanta = 1 << (log_page_size - 6);
1140 u32 rq_sz = 1 << (log_rq_size + 4 + log_rq_stride);
1141 u32 page_size = 1 << log_page_size;
1142 u32 rq_sz_po = rq_sz + (page_offset * po_quanta);
1143 u32 rq_num_pas = (rq_sz_po + page_size - 1) / page_size;
1144
1145 return rq_num_pas * sizeof(u64);
1146}
1147
1148static int create_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1149 struct mlx5_ib_rq *rq, void *qpin)
1150{
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001151 struct mlx5_ib_qp *mqp = rq->base.container_mibqp;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001152 __be64 *pas;
1153 __be64 *qp_pas;
1154 void *in;
1155 void *rqc;
1156 void *wq;
1157 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1158 int inlen;
1159 int err;
1160 u32 rq_pas_size = get_rq_pas_size(qpc);
1161
1162 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + rq_pas_size;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001163 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001164 if (!in)
1165 return -ENOMEM;
1166
1167 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001168 if (!(rq->flags & MLX5_IB_RQ_CVLAN_STRIPPING))
1169 MLX5_SET(rqc, rqc, vsd, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001170 MLX5_SET(rqc, rqc, mem_rq_type, MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
1171 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
1172 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
1173 MLX5_SET(rqc, rqc, user_index, MLX5_GET(qpc, qpc, user_index));
1174 MLX5_SET(rqc, rqc, cqn, MLX5_GET(qpc, qpc, cqn_rcv));
1175
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001176 if (mqp->flags & MLX5_IB_QP_CAP_SCATTER_FCS)
1177 MLX5_SET(rqc, rqc, scatter_fcs, 1);
1178
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001179 wq = MLX5_ADDR_OF(rqc, rqc, wq);
1180 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1181 MLX5_SET(wq, wq, end_padding_mode,
Maor Gottlieb01581fb2016-01-28 17:51:49 +02001182 MLX5_GET(qpc, qpc, end_padding_mode));
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001183 MLX5_SET(wq, wq, page_offset, MLX5_GET(qpc, qpc, page_offset));
1184 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1185 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1186 MLX5_SET(wq, wq, log_wq_stride, MLX5_GET(qpc, qpc, log_rq_stride) + 4);
1187 MLX5_SET(wq, wq, log_wq_pg_sz, MLX5_GET(qpc, qpc, log_page_size));
1188 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_rq_size));
1189
1190 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1191 qp_pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, qpin, pas);
1192 memcpy(pas, qp_pas, rq_pas_size);
1193
1194 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rq->base.mqp);
1195
1196 kvfree(in);
1197
1198 return err;
1199}
1200
1201static void destroy_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1202 struct mlx5_ib_rq *rq)
1203{
1204 mlx5_core_destroy_rq_tracked(dev->mdev, &rq->base.mqp);
1205}
1206
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001207static bool tunnel_offload_supported(struct mlx5_core_dev *dev)
1208{
1209 return (MLX5_CAP_ETH(dev, tunnel_stateless_vxlan) ||
1210 MLX5_CAP_ETH(dev, tunnel_stateless_gre) ||
1211 MLX5_CAP_ETH(dev, tunnel_stateless_geneve_rx));
1212}
1213
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001214static int create_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001215 struct mlx5_ib_rq *rq, u32 tdn,
1216 bool tunnel_offload_en)
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001217{
1218 u32 *in;
1219 void *tirc;
1220 int inlen;
1221 int err;
1222
1223 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001224 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001225 if (!in)
1226 return -ENOMEM;
1227
1228 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1229 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
1230 MLX5_SET(tirc, tirc, inline_rqn, rq->base.mqp.qpn);
1231 MLX5_SET(tirc, tirc, transport_domain, tdn);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001232 if (tunnel_offload_en)
1233 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001234
1235 err = mlx5_core_create_tir(dev->mdev, in, inlen, &rq->tirn);
1236
1237 kvfree(in);
1238
1239 return err;
1240}
1241
1242static void destroy_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1243 struct mlx5_ib_rq *rq)
1244{
1245 mlx5_core_destroy_tir(dev->mdev, rq->tirn);
1246}
1247
1248static int create_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001249 u32 *in,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001250 struct ib_pd *pd)
1251{
1252 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1253 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1254 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1255 struct ib_uobject *uobj = pd->uobject;
1256 struct ib_ucontext *ucontext = uobj->context;
1257 struct mlx5_ib_ucontext *mucontext = to_mucontext(ucontext);
1258 int err;
1259 u32 tdn = mucontext->tdn;
1260
1261 if (qp->sq.wqe_cnt) {
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001262 err = create_raw_packet_qp_tis(dev, qp, sq, tdn);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001263 if (err)
1264 return err;
1265
1266 err = create_raw_packet_qp_sq(dev, sq, in, pd);
1267 if (err)
1268 goto err_destroy_tis;
1269
1270 sq->base.container_mibqp = qp;
Majd Dibbiny1d31e9c2017-08-23 08:35:41 +03001271 sq->base.mqp.event = mlx5_ib_qp_event;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001272 }
1273
1274 if (qp->rq.wqe_cnt) {
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001275 rq->base.container_mibqp = qp;
1276
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001277 if (qp->flags & MLX5_IB_QP_CVLAN_STRIPPING)
1278 rq->flags |= MLX5_IB_RQ_CVLAN_STRIPPING;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001279 err = create_raw_packet_qp_rq(dev, rq, in);
1280 if (err)
1281 goto err_destroy_sq;
1282
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001283
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001284 err = create_raw_packet_qp_tir(dev, rq, tdn,
1285 qp->tunnel_offload_en);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001286 if (err)
1287 goto err_destroy_rq;
1288 }
1289
1290 qp->trans_qp.base.mqp.qpn = qp->sq.wqe_cnt ? sq->base.mqp.qpn :
1291 rq->base.mqp.qpn;
1292
1293 return 0;
1294
1295err_destroy_rq:
1296 destroy_raw_packet_qp_rq(dev, rq);
1297err_destroy_sq:
1298 if (!qp->sq.wqe_cnt)
1299 return err;
1300 destroy_raw_packet_qp_sq(dev, sq);
1301err_destroy_tis:
1302 destroy_raw_packet_qp_tis(dev, sq);
1303
1304 return err;
1305}
1306
1307static void destroy_raw_packet_qp(struct mlx5_ib_dev *dev,
1308 struct mlx5_ib_qp *qp)
1309{
1310 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1311 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1312 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1313
1314 if (qp->rq.wqe_cnt) {
1315 destroy_raw_packet_qp_tir(dev, rq);
1316 destroy_raw_packet_qp_rq(dev, rq);
1317 }
1318
1319 if (qp->sq.wqe_cnt) {
1320 destroy_raw_packet_qp_sq(dev, sq);
1321 destroy_raw_packet_qp_tis(dev, sq);
1322 }
1323}
1324
1325static void raw_packet_qp_copy_info(struct mlx5_ib_qp *qp,
1326 struct mlx5_ib_raw_packet_qp *raw_packet_qp)
1327{
1328 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1329 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1330
1331 sq->sq = &qp->sq;
1332 rq->rq = &qp->rq;
1333 sq->doorbell = &qp->db;
1334 rq->doorbell = &qp->db;
1335}
1336
Yishai Hadas28d61372016-05-23 15:20:56 +03001337static void destroy_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1338{
1339 mlx5_core_destroy_tir(dev->mdev, qp->rss_qp.tirn);
1340}
1341
1342static int create_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
1343 struct ib_pd *pd,
1344 struct ib_qp_init_attr *init_attr,
1345 struct ib_udata *udata)
1346{
1347 struct ib_uobject *uobj = pd->uobject;
1348 struct ib_ucontext *ucontext = uobj->context;
1349 struct mlx5_ib_ucontext *mucontext = to_mucontext(ucontext);
1350 struct mlx5_ib_create_qp_resp resp = {};
1351 int inlen;
1352 int err;
1353 u32 *in;
1354 void *tirc;
1355 void *hfso;
1356 u32 selected_fields = 0;
1357 size_t min_resp_len;
1358 u32 tdn = mucontext->tdn;
1359 struct mlx5_ib_create_qp_rss ucmd = {};
1360 size_t required_cmd_sz;
1361
1362 if (init_attr->qp_type != IB_QPT_RAW_PACKET)
1363 return -EOPNOTSUPP;
1364
1365 if (init_attr->create_flags || init_attr->send_cq)
1366 return -EINVAL;
1367
Eli Cohen2f5ff262017-01-03 23:55:21 +02001368 min_resp_len = offsetof(typeof(resp), bfreg_index) + sizeof(resp.bfreg_index);
Yishai Hadas28d61372016-05-23 15:20:56 +03001369 if (udata->outlen < min_resp_len)
1370 return -EINVAL;
1371
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001372 required_cmd_sz = offsetof(typeof(ucmd), flags) + sizeof(ucmd.flags);
Yishai Hadas28d61372016-05-23 15:20:56 +03001373 if (udata->inlen < required_cmd_sz) {
1374 mlx5_ib_dbg(dev, "invalid inlen\n");
1375 return -EINVAL;
1376 }
1377
1378 if (udata->inlen > sizeof(ucmd) &&
1379 !ib_is_udata_cleared(udata, sizeof(ucmd),
1380 udata->inlen - sizeof(ucmd))) {
1381 mlx5_ib_dbg(dev, "inlen is not supported\n");
1382 return -EOPNOTSUPP;
1383 }
1384
1385 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
1386 mlx5_ib_dbg(dev, "copy failed\n");
1387 return -EFAULT;
1388 }
1389
1390 if (ucmd.comp_mask) {
1391 mlx5_ib_dbg(dev, "invalid comp mask\n");
1392 return -EOPNOTSUPP;
1393 }
1394
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001395 if (ucmd.flags & ~MLX5_QP_FLAG_TUNNEL_OFFLOADS) {
1396 mlx5_ib_dbg(dev, "invalid flags\n");
1397 return -EOPNOTSUPP;
1398 }
1399
1400 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS &&
1401 !tunnel_offload_supported(dev->mdev)) {
1402 mlx5_ib_dbg(dev, "tunnel offloads isn't supported\n");
Yishai Hadas28d61372016-05-23 15:20:56 +03001403 return -EOPNOTSUPP;
1404 }
1405
Maor Gottlieb309fa342017-10-19 08:25:56 +03001406 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER &&
1407 !(ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)) {
1408 mlx5_ib_dbg(dev, "Tunnel offloads must be set for inner RSS\n");
1409 return -EOPNOTSUPP;
1410 }
1411
Yishai Hadas28d61372016-05-23 15:20:56 +03001412 err = ib_copy_to_udata(udata, &resp, min_resp_len);
1413 if (err) {
1414 mlx5_ib_dbg(dev, "copy failed\n");
1415 return -EINVAL;
1416 }
1417
1418 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001419 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas28d61372016-05-23 15:20:56 +03001420 if (!in)
1421 return -ENOMEM;
1422
1423 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1424 MLX5_SET(tirc, tirc, disp_type,
1425 MLX5_TIRC_DISP_TYPE_INDIRECT);
1426 MLX5_SET(tirc, tirc, indirect_table,
1427 init_attr->rwq_ind_tbl->ind_tbl_num);
1428 MLX5_SET(tirc, tirc, transport_domain, tdn);
1429
1430 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001431
1432 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
1433 MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
1434
Maor Gottlieb309fa342017-10-19 08:25:56 +03001435 if (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_INNER)
1436 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner);
1437 else
1438 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1439
Yishai Hadas28d61372016-05-23 15:20:56 +03001440 switch (ucmd.rx_hash_function) {
1441 case MLX5_RX_HASH_FUNC_TOEPLITZ:
1442 {
1443 void *rss_key = MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
1444 size_t len = MLX5_FLD_SZ_BYTES(tirc, rx_hash_toeplitz_key);
1445
1446 if (len != ucmd.rx_key_len) {
1447 err = -EINVAL;
1448 goto err;
1449 }
1450
1451 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_TOEPLITZ);
1452 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1453 memcpy(rss_key, ucmd.rx_hash_key, len);
1454 break;
1455 }
1456 default:
1457 err = -EOPNOTSUPP;
1458 goto err;
1459 }
1460
1461 if (!ucmd.rx_hash_fields_mask) {
1462 /* special case when this TIR serves as steering entry without hashing */
1463 if (!init_attr->rwq_ind_tbl->log_ind_tbl_size)
1464 goto create_tir;
1465 err = -EINVAL;
1466 goto err;
1467 }
1468
1469 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1470 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4)) &&
1471 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1472 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))) {
1473 err = -EINVAL;
1474 goto err;
1475 }
1476
1477 /* If none of IPV4 & IPV6 SRC/DST was set - this bit field is ignored */
1478 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1479 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4))
1480 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1481 MLX5_L3_PROT_TYPE_IPV4);
1482 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1483 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1484 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1485 MLX5_L3_PROT_TYPE_IPV6);
1486
1487 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1488 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP)) &&
1489 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1490 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))) {
1491 err = -EINVAL;
1492 goto err;
1493 }
1494
1495 /* If none of TCP & UDP SRC/DST was set - this bit field is ignored */
1496 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1497 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP))
1498 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1499 MLX5_L4_PROT_TYPE_TCP);
1500 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1501 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1502 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1503 MLX5_L4_PROT_TYPE_UDP);
1504
1505 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1506 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6))
1507 selected_fields |= MLX5_HASH_FIELD_SEL_SRC_IP;
1508
1509 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4) ||
1510 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1511 selected_fields |= MLX5_HASH_FIELD_SEL_DST_IP;
1512
1513 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1514 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP))
1515 selected_fields |= MLX5_HASH_FIELD_SEL_L4_SPORT;
1516
1517 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP) ||
1518 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1519 selected_fields |= MLX5_HASH_FIELD_SEL_L4_DPORT;
1520
1521 MLX5_SET(rx_hash_field_select, hfso, selected_fields, selected_fields);
1522
1523create_tir:
1524 err = mlx5_core_create_tir(dev->mdev, in, inlen, &qp->rss_qp.tirn);
1525
1526 if (err)
1527 goto err;
1528
1529 kvfree(in);
1530 /* qpn is reserved for that QP */
1531 qp->trans_qp.base.mqp.qpn = 0;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03001532 qp->flags |= MLX5_IB_QP_RSS;
Yishai Hadas28d61372016-05-23 15:20:56 +03001533 return 0;
1534
1535err:
1536 kvfree(in);
1537 return err;
1538}
1539
Eli Cohene126ba92013-07-07 17:25:49 +03001540static int create_qp_common(struct mlx5_ib_dev *dev, struct ib_pd *pd,
1541 struct ib_qp_init_attr *init_attr,
1542 struct ib_udata *udata, struct mlx5_ib_qp *qp)
1543{
1544 struct mlx5_ib_resources *devr = &dev->devr;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001545 int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
Saeed Mahameed938fe832015-05-28 22:28:41 +03001546 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001547 struct mlx5_ib_create_qp_resp resp;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001548 struct mlx5_ib_cq *send_cq;
1549 struct mlx5_ib_cq *recv_cq;
1550 unsigned long flags;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001551 u32 uidx = MLX5_IB_DEFAULT_UIDX;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001552 struct mlx5_ib_create_qp ucmd;
1553 struct mlx5_ib_qp_base *base;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001554 void *qpc;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001555 u32 *in;
1556 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03001557
1558 mutex_init(&qp->mutex);
1559 spin_lock_init(&qp->sq.lock);
1560 spin_lock_init(&qp->rq.lock);
1561
Yishai Hadas28d61372016-05-23 15:20:56 +03001562 if (init_attr->rwq_ind_tbl) {
1563 if (!udata)
1564 return -ENOSYS;
1565
1566 err = create_rss_raw_qp_tir(dev, qp, pd, init_attr, udata);
1567 return err;
1568 }
1569
Eli Cohenf360d882014-04-02 00:10:16 +03001570 if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001571 if (!MLX5_CAP_GEN(mdev, block_lb_mc)) {
Eli Cohenf360d882014-04-02 00:10:16 +03001572 mlx5_ib_dbg(dev, "block multicast loopback isn't supported\n");
1573 return -EINVAL;
1574 } else {
1575 qp->flags |= MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK;
1576 }
1577 }
1578
Leon Romanovsky051f2632015-12-20 12:16:11 +02001579 if (init_attr->create_flags &
1580 (IB_QP_CREATE_CROSS_CHANNEL |
1581 IB_QP_CREATE_MANAGED_SEND |
1582 IB_QP_CREATE_MANAGED_RECV)) {
1583 if (!MLX5_CAP_GEN(mdev, cd)) {
1584 mlx5_ib_dbg(dev, "cross-channel isn't supported\n");
1585 return -EINVAL;
1586 }
1587 if (init_attr->create_flags & IB_QP_CREATE_CROSS_CHANNEL)
1588 qp->flags |= MLX5_IB_QP_CROSS_CHANNEL;
1589 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_SEND)
1590 qp->flags |= MLX5_IB_QP_MANAGED_SEND;
1591 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_RECV)
1592 qp->flags |= MLX5_IB_QP_MANAGED_RECV;
1593 }
Erez Shitritf0313962016-02-21 16:27:17 +02001594
1595 if (init_attr->qp_type == IB_QPT_UD &&
1596 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO))
1597 if (!MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
1598 mlx5_ib_dbg(dev, "ipoib UD lso qp isn't supported\n");
1599 return -EOPNOTSUPP;
1600 }
1601
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001602 if (init_attr->create_flags & IB_QP_CREATE_SCATTER_FCS) {
1603 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
1604 mlx5_ib_dbg(dev, "Scatter FCS is supported only for Raw Packet QPs");
1605 return -EOPNOTSUPP;
1606 }
1607 if (!MLX5_CAP_GEN(dev->mdev, eth_net_offloads) ||
1608 !MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
1609 mlx5_ib_dbg(dev, "Scatter FCS isn't supported\n");
1610 return -EOPNOTSUPP;
1611 }
1612 qp->flags |= MLX5_IB_QP_CAP_SCATTER_FCS;
1613 }
1614
Eli Cohene126ba92013-07-07 17:25:49 +03001615 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
1616 qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;
1617
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001618 if (init_attr->create_flags & IB_QP_CREATE_CVLAN_STRIPPING) {
1619 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
1620 MLX5_CAP_ETH(dev->mdev, vlan_cap)) ||
1621 (init_attr->qp_type != IB_QPT_RAW_PACKET))
1622 return -EOPNOTSUPP;
1623 qp->flags |= MLX5_IB_QP_CVLAN_STRIPPING;
1624 }
1625
Eli Cohene126ba92013-07-07 17:25:49 +03001626 if (pd && pd->uobject) {
1627 if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
1628 mlx5_ib_dbg(dev, "copy failed\n");
1629 return -EFAULT;
1630 }
1631
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001632 err = get_qp_user_index(to_mucontext(pd->uobject->context),
1633 &ucmd, udata->inlen, &uidx);
1634 if (err)
1635 return err;
1636
Eli Cohene126ba92013-07-07 17:25:49 +03001637 qp->wq_sig = !!(ucmd.flags & MLX5_QP_FLAG_SIGNATURE);
1638 qp->scat_cqe = !!(ucmd.flags & MLX5_QP_FLAG_SCATTER_CQE);
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001639 if (ucmd.flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS) {
1640 if (init_attr->qp_type != IB_QPT_RAW_PACKET ||
1641 !tunnel_offload_supported(mdev)) {
1642 mlx5_ib_dbg(dev, "Tunnel offload isn't supported\n");
1643 return -EOPNOTSUPP;
1644 }
1645 qp->tunnel_offload_en = true;
1646 }
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001647
1648 if (init_attr->create_flags & IB_QP_CREATE_SOURCE_QPN) {
1649 if (init_attr->qp_type != IB_QPT_UD ||
1650 (MLX5_CAP_GEN(dev->mdev, port_type) !=
1651 MLX5_CAP_PORT_TYPE_IB) ||
1652 !mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS)) {
1653 mlx5_ib_dbg(dev, "Source QP option isn't supported\n");
1654 return -EOPNOTSUPP;
1655 }
1656
1657 qp->flags |= MLX5_IB_QP_UNDERLAY;
1658 qp->underlay_qpn = init_attr->source_qpn;
1659 }
Eli Cohene126ba92013-07-07 17:25:49 +03001660 } else {
1661 qp->wq_sig = !!wq_signature;
1662 }
1663
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001664 base = (init_attr->qp_type == IB_QPT_RAW_PACKET ||
1665 qp->flags & MLX5_IB_QP_UNDERLAY) ?
1666 &qp->raw_packet_qp.rq.base :
1667 &qp->trans_qp.base;
1668
Eli Cohene126ba92013-07-07 17:25:49 +03001669 qp->has_rq = qp_has_rq(init_attr);
1670 err = set_rq_size(dev, &init_attr->cap, qp->has_rq,
1671 qp, (pd && pd->uobject) ? &ucmd : NULL);
1672 if (err) {
1673 mlx5_ib_dbg(dev, "err %d\n", err);
1674 return err;
1675 }
1676
1677 if (pd) {
1678 if (pd->uobject) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001679 __u32 max_wqes =
1680 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Eli Cohene126ba92013-07-07 17:25:49 +03001681 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d)\n", ucmd.sq_wqe_count);
1682 if (ucmd.rq_wqe_shift != qp->rq.wqe_shift ||
1683 ucmd.rq_wqe_count != qp->rq.wqe_cnt) {
1684 mlx5_ib_dbg(dev, "invalid rq params\n");
1685 return -EINVAL;
1686 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03001687 if (ucmd.sq_wqe_count > max_wqes) {
Eli Cohene126ba92013-07-07 17:25:49 +03001688 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d) > max allowed (%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +03001689 ucmd.sq_wqe_count, max_wqes);
Eli Cohene126ba92013-07-07 17:25:49 +03001690 return -EINVAL;
1691 }
Haggai Eranb11a4f92016-02-29 15:45:03 +02001692 if (init_attr->create_flags &
1693 mlx5_ib_create_qp_sqpn_qp1()) {
1694 mlx5_ib_dbg(dev, "user-space is not allowed to create UD QPs spoofing as QP1\n");
1695 return -EINVAL;
1696 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001697 err = create_user_qp(dev, pd, qp, udata, init_attr, &in,
1698 &resp, &inlen, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001699 if (err)
1700 mlx5_ib_dbg(dev, "err %d\n", err);
1701 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +02001702 err = create_kernel_qp(dev, init_attr, qp, &in, &inlen,
1703 base);
Eli Cohene126ba92013-07-07 17:25:49 +03001704 if (err)
1705 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohene126ba92013-07-07 17:25:49 +03001706 }
1707
1708 if (err)
1709 return err;
1710 } else {
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001711 in = kvzalloc(inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001712 if (!in)
1713 return -ENOMEM;
1714
1715 qp->create_type = MLX5_QP_EMPTY;
1716 }
1717
1718 if (is_sqp(init_attr->qp_type))
1719 qp->port = init_attr->port_num;
1720
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001721 qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
1722
1723 MLX5_SET(qpc, qpc, st, to_mlx5_st(init_attr->qp_type));
1724 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
Eli Cohene126ba92013-07-07 17:25:49 +03001725
1726 if (init_attr->qp_type != MLX5_IB_QPT_REG_UMR)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001727 MLX5_SET(qpc, qpc, pd, to_mpd(pd ? pd : devr->p0)->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001728 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001729 MLX5_SET(qpc, qpc, latency_sensitive, 1);
1730
Eli Cohene126ba92013-07-07 17:25:49 +03001731
1732 if (qp->wq_sig)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001733 MLX5_SET(qpc, qpc, wq_signature, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001734
Eli Cohenf360d882014-04-02 00:10:16 +03001735 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001736 MLX5_SET(qpc, qpc, block_lb_mc, 1);
Eli Cohenf360d882014-04-02 00:10:16 +03001737
Leon Romanovsky051f2632015-12-20 12:16:11 +02001738 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001739 MLX5_SET(qpc, qpc, cd_master, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001740 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001741 MLX5_SET(qpc, qpc, cd_slave_send, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001742 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001743 MLX5_SET(qpc, qpc, cd_slave_receive, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001744
Eli Cohene126ba92013-07-07 17:25:49 +03001745 if (qp->scat_cqe && is_connected(init_attr->qp_type)) {
1746 int rcqe_sz;
1747 int scqe_sz;
1748
1749 rcqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->recv_cq);
1750 scqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->send_cq);
1751
1752 if (rcqe_sz == 128)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001753 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA64_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001754 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001755 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA32_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001756
1757 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR) {
1758 if (scqe_sz == 128)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001759 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA64_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001760 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001761 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA32_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001762 }
1763 }
1764
1765 if (qp->rq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001766 MLX5_SET(qpc, qpc, log_rq_stride, qp->rq.wqe_shift - 4);
1767 MLX5_SET(qpc, qpc, log_rq_size, ilog2(qp->rq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03001768 }
1769
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001770 MLX5_SET(qpc, qpc, rq_type, get_rx_type(qp, init_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03001771
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03001772 if (qp->sq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001773 MLX5_SET(qpc, qpc, log_sq_size, ilog2(qp->sq.wqe_cnt));
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03001774 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001775 MLX5_SET(qpc, qpc, no_sq, 1);
Artemy Kovalyov3fd33072017-08-17 15:52:11 +03001776 if (init_attr->srq &&
1777 init_attr->srq->srq_type == IB_SRQT_TM)
1778 MLX5_SET(qpc, qpc, offload_type,
1779 MLX5_QPC_OFFLOAD_TYPE_RNDV);
1780 }
Eli Cohene126ba92013-07-07 17:25:49 +03001781
1782 /* Set default resources */
1783 switch (init_attr->qp_type) {
1784 case IB_QPT_XRC_TGT:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001785 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
1786 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(devr->c0)->mcq.cqn);
1787 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
1788 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(init_attr->xrcd)->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001789 break;
1790 case IB_QPT_XRC_INI:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001791 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
1792 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
1793 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001794 break;
1795 default:
1796 if (init_attr->srq) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001797 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x0)->xrcdn);
1798 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(init_attr->srq)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001799 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001800 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
1801 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s1)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001802 }
1803 }
1804
1805 if (init_attr->send_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001806 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(init_attr->send_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001807
1808 if (init_attr->recv_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001809 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(init_attr->recv_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001810
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001811 MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
Eli Cohene126ba92013-07-07 17:25:49 +03001812
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001813 /* 0xffffff means we ask to work with cqe version 0 */
1814 if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1)
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001815 MLX5_SET(qpc, qpc, user_index, uidx);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001816
Erez Shitritf0313962016-02-21 16:27:17 +02001817 /* we use IB_QP_CREATE_IPOIB_UD_LSO to indicates ipoib qp */
1818 if (init_attr->qp_type == IB_QPT_UD &&
1819 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)) {
Erez Shitritf0313962016-02-21 16:27:17 +02001820 MLX5_SET(qpc, qpc, ulp_stateless_offload_mode, 1);
1821 qp->flags |= MLX5_IB_QP_LSO;
1822 }
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001823
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001824 if (init_attr->qp_type == IB_QPT_RAW_PACKET ||
1825 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001826 qp->raw_packet_qp.sq.ubuffer.buf_addr = ucmd.sq_buf_addr;
1827 raw_packet_qp_copy_info(qp, &qp->raw_packet_qp);
1828 err = create_raw_packet_qp(dev, qp, in, pd);
1829 } else {
1830 err = mlx5_core_create_qp(dev->mdev, &base->mqp, in, inlen);
1831 }
1832
Eli Cohene126ba92013-07-07 17:25:49 +03001833 if (err) {
1834 mlx5_ib_dbg(dev, "create qp failed\n");
1835 goto err_create;
1836 }
1837
Al Viro479163f2014-11-20 08:13:57 +00001838 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001839
majd@mellanox.com19098df2016-01-14 19:13:03 +02001840 base->container_mibqp = qp;
1841 base->mqp.event = mlx5_ib_qp_event;
Eli Cohene126ba92013-07-07 17:25:49 +03001842
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001843 get_cqs(init_attr->qp_type, init_attr->send_cq, init_attr->recv_cq,
1844 &send_cq, &recv_cq);
1845 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
1846 mlx5_ib_lock_cqs(send_cq, recv_cq);
1847 /* Maintain device to QPs access, needed for further handling via reset
1848 * flow
1849 */
1850 list_add_tail(&qp->qps_list, &dev->qp_list);
1851 /* Maintain CQ to QPs access, needed for further handling via reset flow
1852 */
1853 if (send_cq)
1854 list_add_tail(&qp->cq_send_list, &send_cq->list_send_qp);
1855 if (recv_cq)
1856 list_add_tail(&qp->cq_recv_list, &recv_cq->list_recv_qp);
1857 mlx5_ib_unlock_cqs(send_cq, recv_cq);
1858 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
1859
Eli Cohene126ba92013-07-07 17:25:49 +03001860 return 0;
1861
1862err_create:
1863 if (qp->create_type == MLX5_QP_USER)
Eli Cohenb037c292017-01-03 23:55:26 +02001864 destroy_qp_user(dev, pd, qp, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001865 else if (qp->create_type == MLX5_QP_KERNEL)
1866 destroy_qp_kernel(dev, qp);
1867
Al Viro479163f2014-11-20 08:13:57 +00001868 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001869 return err;
1870}
1871
1872static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
1873 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
1874{
1875 if (send_cq) {
1876 if (recv_cq) {
1877 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001878 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001879 spin_lock_nested(&recv_cq->lock,
1880 SINGLE_DEPTH_NESTING);
1881 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001882 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001883 __acquire(&recv_cq->lock);
1884 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001885 spin_lock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001886 spin_lock_nested(&send_cq->lock,
1887 SINGLE_DEPTH_NESTING);
1888 }
1889 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001890 spin_lock(&send_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001891 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001892 }
1893 } else if (recv_cq) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001894 spin_lock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001895 __acquire(&send_cq->lock);
1896 } else {
1897 __acquire(&send_cq->lock);
1898 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001899 }
1900}
1901
1902static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
1903 __releases(&send_cq->lock) __releases(&recv_cq->lock)
1904{
1905 if (send_cq) {
1906 if (recv_cq) {
1907 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
1908 spin_unlock(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001909 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001910 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
1911 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001912 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001913 } else {
1914 spin_unlock(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001915 spin_unlock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001916 }
1917 } else {
Eli Cohen6a4f1392014-12-02 12:26:18 +02001918 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001919 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001920 }
1921 } else if (recv_cq) {
Eli Cohen6a4f1392014-12-02 12:26:18 +02001922 __release(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001923 spin_unlock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001924 } else {
1925 __release(&recv_cq->lock);
1926 __release(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001927 }
1928}
1929
1930static struct mlx5_ib_pd *get_pd(struct mlx5_ib_qp *qp)
1931{
1932 return to_mpd(qp->ibqp.pd);
1933}
1934
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001935static void get_cqs(enum ib_qp_type qp_type,
1936 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
Eli Cohene126ba92013-07-07 17:25:49 +03001937 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq)
1938{
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001939 switch (qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +03001940 case IB_QPT_XRC_TGT:
1941 *send_cq = NULL;
1942 *recv_cq = NULL;
1943 break;
1944 case MLX5_IB_QPT_REG_UMR:
1945 case IB_QPT_XRC_INI:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001946 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03001947 *recv_cq = NULL;
1948 break;
1949
1950 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02001951 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03001952 case IB_QPT_RC:
1953 case IB_QPT_UC:
1954 case IB_QPT_UD:
1955 case IB_QPT_RAW_IPV6:
1956 case IB_QPT_RAW_ETHERTYPE:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001957 case IB_QPT_RAW_PACKET:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001958 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
1959 *recv_cq = ib_recv_cq ? to_mcq(ib_recv_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03001960 break;
1961
Eli Cohene126ba92013-07-07 17:25:49 +03001962 case IB_QPT_MAX:
1963 default:
1964 *send_cq = NULL;
1965 *recv_cq = NULL;
1966 break;
1967 }
1968}
1969
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001970static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03001971 const struct mlx5_modify_raw_qp_param *raw_qp_param,
1972 u8 lag_tx_affinity);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001973
Eli Cohene126ba92013-07-07 17:25:49 +03001974static void destroy_qp_common(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1975{
1976 struct mlx5_ib_cq *send_cq, *recv_cq;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001977 struct mlx5_ib_qp_base *base;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001978 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03001979 int err;
1980
Yishai Hadas28d61372016-05-23 15:20:56 +03001981 if (qp->ibqp.rwq_ind_tbl) {
1982 destroy_rss_raw_qp_tir(dev, qp);
1983 return;
1984 }
1985
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001986 base = (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
1987 qp->flags & MLX5_IB_QP_UNDERLAY) ?
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001988 &qp->raw_packet_qp.rq.base :
1989 &qp->trans_qp.base;
1990
Haggai Eran6aec21f2014-12-11 17:04:23 +02001991 if (qp->state != IB_QPS_RESET) {
Yishai Hadasc2e53b22017-06-08 16:15:08 +03001992 if (qp->ibqp.qp_type != IB_QPT_RAW_PACKET &&
1993 !(qp->flags & MLX5_IB_QP_UNDERLAY)) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001994 err = mlx5_core_qp_modify(dev->mdev,
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03001995 MLX5_CMD_OP_2RST_QP, 0,
1996 NULL, &base->mqp);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001997 } else {
Alex Vesker0680efa2016-08-28 12:25:52 +03001998 struct mlx5_modify_raw_qp_param raw_qp_param = {
1999 .operation = MLX5_CMD_OP_2RST_QP
2000 };
2001
Aviv Heller13eab212016-09-18 20:48:04 +03002002 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, 0);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002003 }
2004 if (err)
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002005 mlx5_ib_warn(dev, "mlx5_ib: modify QP 0x%06x to RESET failed\n",
majd@mellanox.com19098df2016-01-14 19:13:03 +02002006 base->mqp.qpn);
Haggai Eran6aec21f2014-12-11 17:04:23 +02002007 }
Eli Cohene126ba92013-07-07 17:25:49 +03002008
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002009 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
2010 &send_cq, &recv_cq);
2011
2012 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
2013 mlx5_ib_lock_cqs(send_cq, recv_cq);
2014 /* del from lists under both locks above to protect reset flow paths */
2015 list_del(&qp->qps_list);
2016 if (send_cq)
2017 list_del(&qp->cq_send_list);
2018
2019 if (recv_cq)
2020 list_del(&qp->cq_recv_list);
Eli Cohene126ba92013-07-07 17:25:49 +03002021
2022 if (qp->create_type == MLX5_QP_KERNEL) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002023 __mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03002024 qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
2025 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002026 __mlx5_ib_cq_clean(send_cq, base->mqp.qpn,
2027 NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002028 }
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002029 mlx5_ib_unlock_cqs(send_cq, recv_cq);
2030 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
Eli Cohene126ba92013-07-07 17:25:49 +03002031
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002032 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2033 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002034 destroy_raw_packet_qp(dev, qp);
2035 } else {
2036 err = mlx5_core_destroy_qp(dev->mdev, &base->mqp);
2037 if (err)
2038 mlx5_ib_warn(dev, "failed to destroy QP 0x%x\n",
2039 base->mqp.qpn);
2040 }
Eli Cohene126ba92013-07-07 17:25:49 +03002041
Eli Cohene126ba92013-07-07 17:25:49 +03002042 if (qp->create_type == MLX5_QP_KERNEL)
2043 destroy_qp_kernel(dev, qp);
2044 else if (qp->create_type == MLX5_QP_USER)
Eli Cohenb037c292017-01-03 23:55:26 +02002045 destroy_qp_user(dev, &get_pd(qp)->ibpd, qp, base);
Eli Cohene126ba92013-07-07 17:25:49 +03002046}
2047
2048static const char *ib_qp_type_str(enum ib_qp_type type)
2049{
2050 switch (type) {
2051 case IB_QPT_SMI:
2052 return "IB_QPT_SMI";
2053 case IB_QPT_GSI:
2054 return "IB_QPT_GSI";
2055 case IB_QPT_RC:
2056 return "IB_QPT_RC";
2057 case IB_QPT_UC:
2058 return "IB_QPT_UC";
2059 case IB_QPT_UD:
2060 return "IB_QPT_UD";
2061 case IB_QPT_RAW_IPV6:
2062 return "IB_QPT_RAW_IPV6";
2063 case IB_QPT_RAW_ETHERTYPE:
2064 return "IB_QPT_RAW_ETHERTYPE";
2065 case IB_QPT_XRC_INI:
2066 return "IB_QPT_XRC_INI";
2067 case IB_QPT_XRC_TGT:
2068 return "IB_QPT_XRC_TGT";
2069 case IB_QPT_RAW_PACKET:
2070 return "IB_QPT_RAW_PACKET";
2071 case MLX5_IB_QPT_REG_UMR:
2072 return "MLX5_IB_QPT_REG_UMR";
2073 case IB_QPT_MAX:
2074 default:
2075 return "Invalid QP type";
2076 }
2077}
2078
2079struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd,
2080 struct ib_qp_init_attr *init_attr,
2081 struct ib_udata *udata)
2082{
2083 struct mlx5_ib_dev *dev;
2084 struct mlx5_ib_qp *qp;
2085 u16 xrcdn = 0;
2086 int err;
2087
2088 if (pd) {
2089 dev = to_mdev(pd->device);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002090
2091 if (init_attr->qp_type == IB_QPT_RAW_PACKET) {
2092 if (!pd->uobject) {
2093 mlx5_ib_dbg(dev, "Raw Packet QP is not supported for kernel consumers\n");
2094 return ERR_PTR(-EINVAL);
2095 } else if (!to_mucontext(pd->uobject->context)->cqe_version) {
2096 mlx5_ib_dbg(dev, "Raw Packet QP is only supported for CQE version > 0\n");
2097 return ERR_PTR(-EINVAL);
2098 }
2099 }
Majd Dibbiny09f16cf2016-01-28 17:51:48 +02002100 } else {
2101 /* being cautious here */
2102 if (init_attr->qp_type != IB_QPT_XRC_TGT &&
2103 init_attr->qp_type != MLX5_IB_QPT_REG_UMR) {
2104 pr_warn("%s: no PD for transport %s\n", __func__,
2105 ib_qp_type_str(init_attr->qp_type));
2106 return ERR_PTR(-EINVAL);
2107 }
2108 dev = to_mdev(to_mxrcd(init_attr->xrcd)->ibxrcd.device);
Eli Cohene126ba92013-07-07 17:25:49 +03002109 }
2110
2111 switch (init_attr->qp_type) {
2112 case IB_QPT_XRC_TGT:
2113 case IB_QPT_XRC_INI:
Saeed Mahameed938fe832015-05-28 22:28:41 +03002114 if (!MLX5_CAP_GEN(dev->mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002115 mlx5_ib_dbg(dev, "XRC not supported\n");
2116 return ERR_PTR(-ENOSYS);
2117 }
2118 init_attr->recv_cq = NULL;
2119 if (init_attr->qp_type == IB_QPT_XRC_TGT) {
2120 xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
2121 init_attr->send_cq = NULL;
2122 }
2123
2124 /* fall through */
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002125 case IB_QPT_RAW_PACKET:
Eli Cohene126ba92013-07-07 17:25:49 +03002126 case IB_QPT_RC:
2127 case IB_QPT_UC:
2128 case IB_QPT_UD:
2129 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002130 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002131 case MLX5_IB_QPT_REG_UMR:
2132 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2133 if (!qp)
2134 return ERR_PTR(-ENOMEM);
2135
2136 err = create_qp_common(dev, pd, init_attr, udata, qp);
2137 if (err) {
2138 mlx5_ib_dbg(dev, "create_qp_common failed\n");
2139 kfree(qp);
2140 return ERR_PTR(err);
2141 }
2142
2143 if (is_qp0(init_attr->qp_type))
2144 qp->ibqp.qp_num = 0;
2145 else if (is_qp1(init_attr->qp_type))
2146 qp->ibqp.qp_num = 1;
2147 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002148 qp->ibqp.qp_num = qp->trans_qp.base.mqp.qpn;
Eli Cohene126ba92013-07-07 17:25:49 +03002149
2150 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 +02002151 qp->ibqp.qp_num, qp->trans_qp.base.mqp.qpn,
Eli Cohena1ab8402016-10-27 16:36:46 +03002152 init_attr->recv_cq ? to_mcq(init_attr->recv_cq)->mcq.cqn : -1,
2153 init_attr->send_cq ? to_mcq(init_attr->send_cq)->mcq.cqn : -1);
Eli Cohene126ba92013-07-07 17:25:49 +03002154
majd@mellanox.com19098df2016-01-14 19:13:03 +02002155 qp->trans_qp.xrcdn = xrcdn;
Eli Cohene126ba92013-07-07 17:25:49 +03002156
2157 break;
2158
Haggai Erand16e91d2016-02-29 15:45:05 +02002159 case IB_QPT_GSI:
2160 return mlx5_ib_gsi_create_qp(pd, init_attr);
2161
Eli Cohene126ba92013-07-07 17:25:49 +03002162 case IB_QPT_RAW_IPV6:
2163 case IB_QPT_RAW_ETHERTYPE:
Eli Cohene126ba92013-07-07 17:25:49 +03002164 case IB_QPT_MAX:
2165 default:
2166 mlx5_ib_dbg(dev, "unsupported qp type %d\n",
2167 init_attr->qp_type);
2168 /* Don't support raw QPs */
2169 return ERR_PTR(-EINVAL);
2170 }
2171
2172 return &qp->ibqp;
2173}
2174
2175int mlx5_ib_destroy_qp(struct ib_qp *qp)
2176{
2177 struct mlx5_ib_dev *dev = to_mdev(qp->device);
2178 struct mlx5_ib_qp *mqp = to_mqp(qp);
2179
Haggai Erand16e91d2016-02-29 15:45:05 +02002180 if (unlikely(qp->qp_type == IB_QPT_GSI))
2181 return mlx5_ib_gsi_destroy_qp(qp);
2182
Eli Cohene126ba92013-07-07 17:25:49 +03002183 destroy_qp_common(dev, mqp);
2184
2185 kfree(mqp);
2186
2187 return 0;
2188}
2189
2190static __be32 to_mlx5_access_flags(struct mlx5_ib_qp *qp, const struct ib_qp_attr *attr,
2191 int attr_mask)
2192{
2193 u32 hw_access_flags = 0;
2194 u8 dest_rd_atomic;
2195 u32 access_flags;
2196
2197 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
2198 dest_rd_atomic = attr->max_dest_rd_atomic;
2199 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002200 dest_rd_atomic = qp->trans_qp.resp_depth;
Eli Cohene126ba92013-07-07 17:25:49 +03002201
2202 if (attr_mask & IB_QP_ACCESS_FLAGS)
2203 access_flags = attr->qp_access_flags;
2204 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002205 access_flags = qp->trans_qp.atomic_rd_en;
Eli Cohene126ba92013-07-07 17:25:49 +03002206
2207 if (!dest_rd_atomic)
2208 access_flags &= IB_ACCESS_REMOTE_WRITE;
2209
2210 if (access_flags & IB_ACCESS_REMOTE_READ)
2211 hw_access_flags |= MLX5_QP_BIT_RRE;
2212 if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
2213 hw_access_flags |= (MLX5_QP_BIT_RAE | MLX5_ATOMIC_MODE_CX);
2214 if (access_flags & IB_ACCESS_REMOTE_WRITE)
2215 hw_access_flags |= MLX5_QP_BIT_RWE;
2216
2217 return cpu_to_be32(hw_access_flags);
2218}
2219
2220enum {
2221 MLX5_PATH_FLAG_FL = 1 << 0,
2222 MLX5_PATH_FLAG_FREE_AR = 1 << 1,
2223 MLX5_PATH_FLAG_COUNTER = 1 << 2,
2224};
2225
2226static int ib_rate_to_mlx5(struct mlx5_ib_dev *dev, u8 rate)
2227{
2228 if (rate == IB_RATE_PORT_CURRENT) {
2229 return 0;
2230 } else if (rate < IB_RATE_2_5_GBPS || rate > IB_RATE_300_GBPS) {
2231 return -EINVAL;
2232 } else {
2233 while (rate != IB_RATE_2_5_GBPS &&
2234 !(1 << (rate + MLX5_STAT_RATE_OFFSET) &
Saeed Mahameed938fe832015-05-28 22:28:41 +03002235 MLX5_CAP_GEN(dev->mdev, stat_rate_support)))
Eli Cohene126ba92013-07-07 17:25:49 +03002236 --rate;
2237 }
2238
2239 return rate + MLX5_STAT_RATE_OFFSET;
2240}
2241
majd@mellanox.com75850d02016-01-14 19:13:06 +02002242static int modify_raw_packet_eth_prio(struct mlx5_core_dev *dev,
2243 struct mlx5_ib_sq *sq, u8 sl)
2244{
2245 void *in;
2246 void *tisc;
2247 int inlen;
2248 int err;
2249
2250 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002251 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002252 if (!in)
2253 return -ENOMEM;
2254
2255 MLX5_SET(modify_tis_in, in, bitmask.prio, 1);
2256
2257 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2258 MLX5_SET(tisc, tisc, prio, ((sl & 0x7) << 1));
2259
2260 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2261
2262 kvfree(in);
2263
2264 return err;
2265}
2266
Aviv Heller13eab212016-09-18 20:48:04 +03002267static int modify_raw_packet_tx_affinity(struct mlx5_core_dev *dev,
2268 struct mlx5_ib_sq *sq, u8 tx_affinity)
2269{
2270 void *in;
2271 void *tisc;
2272 int inlen;
2273 int err;
2274
2275 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002276 in = kvzalloc(inlen, GFP_KERNEL);
Aviv Heller13eab212016-09-18 20:48:04 +03002277 if (!in)
2278 return -ENOMEM;
2279
2280 MLX5_SET(modify_tis_in, in, bitmask.lag_tx_port_affinity, 1);
2281
2282 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2283 MLX5_SET(tisc, tisc, lag_tx_port_affinity, tx_affinity);
2284
2285 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2286
2287 kvfree(in);
2288
2289 return err;
2290}
2291
majd@mellanox.com75850d02016-01-14 19:13:06 +02002292static int mlx5_set_path(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -04002293 const struct rdma_ah_attr *ah,
Eli Cohene126ba92013-07-07 17:25:49 +03002294 struct mlx5_qp_path *path, u8 port, int attr_mask,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002295 u32 path_flags, const struct ib_qp_attr *attr,
2296 bool alt)
Eli Cohene126ba92013-07-07 17:25:49 +03002297{
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002298 const struct ib_global_route *grh = rdma_ah_read_grh(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002299 int err;
Majd Dibbinyed884512017-01-18 14:10:35 +02002300 enum ib_gid_type gid_type;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002301 u8 ah_flags = rdma_ah_get_ah_flags(ah);
2302 u8 sl = rdma_ah_get_sl(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002303
Eli Cohene126ba92013-07-07 17:25:49 +03002304 if (attr_mask & IB_QP_PKEY_INDEX)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002305 path->pkey_index = cpu_to_be16(alt ? attr->alt_pkey_index :
2306 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002307
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002308 if (ah_flags & IB_AH_GRH) {
2309 if (grh->sgid_index >=
Saeed Mahameed938fe832015-05-28 22:28:41 +03002310 dev->mdev->port_caps[port - 1].gid_table_len) {
Joe Perchesf4f01b52015-05-08 15:58:07 -07002311 pr_err("sgid_index (%u) too large. max is %d\n",
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002312 grh->sgid_index,
Saeed Mahameed938fe832015-05-28 22:28:41 +03002313 dev->mdev->port_caps[port - 1].gid_table_len);
Eli Cohenf83b4262014-09-14 16:47:54 +03002314 return -EINVAL;
2315 }
Achiad Shochat2811ba52015-12-23 18:47:24 +02002316 }
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002317
2318 if (ah->type == RDMA_AH_ATTR_TYPE_ROCE) {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002319 if (!(ah_flags & IB_AH_GRH))
Achiad Shochat2811ba52015-12-23 18:47:24 +02002320 return -EINVAL;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002321 err = mlx5_get_roce_gid_type(dev, port, grh->sgid_index,
Majd Dibbinyed884512017-01-18 14:10:35 +02002322 &gid_type);
2323 if (err)
2324 return err;
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002325 memcpy(path->rmac, ah->roce.dmac, sizeof(ah->roce.dmac));
Achiad Shochat2811ba52015-12-23 18:47:24 +02002326 path->udp_sport = mlx5_get_roce_udp_sport(dev, port,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002327 grh->sgid_index);
2328 path->dci_cfi_prio_sl = (sl & 0x7) << 4;
Majd Dibbinyed884512017-01-18 14:10:35 +02002329 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002330 path->ecn_dscp = (grh->traffic_class >> 2) & 0x3f;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002331 } else {
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03002332 path->fl_free_ar = (path_flags & MLX5_PATH_FLAG_FL) ? 0x80 : 0;
2333 path->fl_free_ar |=
2334 (path_flags & MLX5_PATH_FLAG_FREE_AR) ? 0x40 : 0;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002335 path->rlid = cpu_to_be16(rdma_ah_get_dlid(ah));
2336 path->grh_mlid = rdma_ah_get_path_bits(ah) & 0x7f;
2337 if (ah_flags & IB_AH_GRH)
Achiad Shochat2811ba52015-12-23 18:47:24 +02002338 path->grh_mlid |= 1 << 7;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002339 path->dci_cfi_prio_sl = sl & 0xf;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002340 }
2341
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002342 if (ah_flags & IB_AH_GRH) {
2343 path->mgid_index = grh->sgid_index;
2344 path->hop_limit = grh->hop_limit;
Eli Cohene126ba92013-07-07 17:25:49 +03002345 path->tclass_flowlabel =
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002346 cpu_to_be32((grh->traffic_class << 20) |
2347 (grh->flow_label));
2348 memcpy(path->rgid, grh->dgid.raw, 16);
Eli Cohene126ba92013-07-07 17:25:49 +03002349 }
2350
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002351 err = ib_rate_to_mlx5(dev, rdma_ah_get_static_rate(ah));
Eli Cohene126ba92013-07-07 17:25:49 +03002352 if (err < 0)
2353 return err;
2354 path->static_rate = err;
2355 path->port = port;
2356
Eli Cohene126ba92013-07-07 17:25:49 +03002357 if (attr_mask & IB_QP_TIMEOUT)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002358 path->ackto_lt = (alt ? attr->alt_timeout : attr->timeout) << 3;
Eli Cohene126ba92013-07-07 17:25:49 +03002359
majd@mellanox.com75850d02016-01-14 19:13:06 +02002360 if ((qp->ibqp.qp_type == IB_QPT_RAW_PACKET) && qp->sq.wqe_cnt)
2361 return modify_raw_packet_eth_prio(dev->mdev,
2362 &qp->raw_packet_qp.sq,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002363 sl & 0xf);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002364
Eli Cohene126ba92013-07-07 17:25:49 +03002365 return 0;
2366}
2367
2368static enum mlx5_qp_optpar opt_mask[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE][MLX5_QP_ST_MAX] = {
2369 [MLX5_QP_STATE_INIT] = {
2370 [MLX5_QP_STATE_INIT] = {
2371 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2372 MLX5_QP_OPTPAR_RAE |
2373 MLX5_QP_OPTPAR_RWE |
2374 MLX5_QP_OPTPAR_PKEY_INDEX |
2375 MLX5_QP_OPTPAR_PRI_PORT,
2376 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
2377 MLX5_QP_OPTPAR_PKEY_INDEX |
2378 MLX5_QP_OPTPAR_PRI_PORT,
2379 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2380 MLX5_QP_OPTPAR_Q_KEY |
2381 MLX5_QP_OPTPAR_PRI_PORT,
2382 },
2383 [MLX5_QP_STATE_RTR] = {
2384 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2385 MLX5_QP_OPTPAR_RRE |
2386 MLX5_QP_OPTPAR_RAE |
2387 MLX5_QP_OPTPAR_RWE |
2388 MLX5_QP_OPTPAR_PKEY_INDEX,
2389 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2390 MLX5_QP_OPTPAR_RWE |
2391 MLX5_QP_OPTPAR_PKEY_INDEX,
2392 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2393 MLX5_QP_OPTPAR_Q_KEY,
2394 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_PKEY_INDEX |
2395 MLX5_QP_OPTPAR_Q_KEY,
Eli Cohena4774e92013-09-11 16:35:32 +03002396 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2397 MLX5_QP_OPTPAR_RRE |
2398 MLX5_QP_OPTPAR_RAE |
2399 MLX5_QP_OPTPAR_RWE |
2400 MLX5_QP_OPTPAR_PKEY_INDEX,
Eli Cohene126ba92013-07-07 17:25:49 +03002401 },
2402 },
2403 [MLX5_QP_STATE_RTR] = {
2404 [MLX5_QP_STATE_RTS] = {
2405 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2406 MLX5_QP_OPTPAR_RRE |
2407 MLX5_QP_OPTPAR_RAE |
2408 MLX5_QP_OPTPAR_RWE |
2409 MLX5_QP_OPTPAR_PM_STATE |
2410 MLX5_QP_OPTPAR_RNR_TIMEOUT,
2411 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2412 MLX5_QP_OPTPAR_RWE |
2413 MLX5_QP_OPTPAR_PM_STATE,
2414 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
2415 },
2416 },
2417 [MLX5_QP_STATE_RTS] = {
2418 [MLX5_QP_STATE_RTS] = {
2419 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2420 MLX5_QP_OPTPAR_RAE |
2421 MLX5_QP_OPTPAR_RWE |
2422 MLX5_QP_OPTPAR_RNR_TIMEOUT |
Eli Cohenc2a34312013-10-24 12:01:02 +03002423 MLX5_QP_OPTPAR_PM_STATE |
2424 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03002425 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
Eli Cohenc2a34312013-10-24 12:01:02 +03002426 MLX5_QP_OPTPAR_PM_STATE |
2427 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03002428 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY |
2429 MLX5_QP_OPTPAR_SRQN |
2430 MLX5_QP_OPTPAR_CQN_RCV,
2431 },
2432 },
2433 [MLX5_QP_STATE_SQER] = {
2434 [MLX5_QP_STATE_RTS] = {
2435 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
2436 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_Q_KEY,
Eli Cohen75959f52013-09-11 16:35:31 +03002437 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE,
Eli Cohena4774e92013-09-11 16:35:32 +03002438 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
2439 MLX5_QP_OPTPAR_RWE |
2440 MLX5_QP_OPTPAR_RAE |
2441 MLX5_QP_OPTPAR_RRE,
Eli Cohene126ba92013-07-07 17:25:49 +03002442 },
2443 },
2444};
2445
2446static int ib_nr_to_mlx5_nr(int ib_mask)
2447{
2448 switch (ib_mask) {
2449 case IB_QP_STATE:
2450 return 0;
2451 case IB_QP_CUR_STATE:
2452 return 0;
2453 case IB_QP_EN_SQD_ASYNC_NOTIFY:
2454 return 0;
2455 case IB_QP_ACCESS_FLAGS:
2456 return MLX5_QP_OPTPAR_RWE | MLX5_QP_OPTPAR_RRE |
2457 MLX5_QP_OPTPAR_RAE;
2458 case IB_QP_PKEY_INDEX:
2459 return MLX5_QP_OPTPAR_PKEY_INDEX;
2460 case IB_QP_PORT:
2461 return MLX5_QP_OPTPAR_PRI_PORT;
2462 case IB_QP_QKEY:
2463 return MLX5_QP_OPTPAR_Q_KEY;
2464 case IB_QP_AV:
2465 return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH |
2466 MLX5_QP_OPTPAR_PRI_PORT;
2467 case IB_QP_PATH_MTU:
2468 return 0;
2469 case IB_QP_TIMEOUT:
2470 return MLX5_QP_OPTPAR_ACK_TIMEOUT;
2471 case IB_QP_RETRY_CNT:
2472 return MLX5_QP_OPTPAR_RETRY_COUNT;
2473 case IB_QP_RNR_RETRY:
2474 return MLX5_QP_OPTPAR_RNR_RETRY;
2475 case IB_QP_RQ_PSN:
2476 return 0;
2477 case IB_QP_MAX_QP_RD_ATOMIC:
2478 return MLX5_QP_OPTPAR_SRA_MAX;
2479 case IB_QP_ALT_PATH:
2480 return MLX5_QP_OPTPAR_ALT_ADDR_PATH;
2481 case IB_QP_MIN_RNR_TIMER:
2482 return MLX5_QP_OPTPAR_RNR_TIMEOUT;
2483 case IB_QP_SQ_PSN:
2484 return 0;
2485 case IB_QP_MAX_DEST_RD_ATOMIC:
2486 return MLX5_QP_OPTPAR_RRA_MAX | MLX5_QP_OPTPAR_RWE |
2487 MLX5_QP_OPTPAR_RRE | MLX5_QP_OPTPAR_RAE;
2488 case IB_QP_PATH_MIG_STATE:
2489 return MLX5_QP_OPTPAR_PM_STATE;
2490 case IB_QP_CAP:
2491 return 0;
2492 case IB_QP_DEST_QPN:
2493 return 0;
2494 }
2495 return 0;
2496}
2497
2498static int ib_mask_to_mlx5_opt(int ib_mask)
2499{
2500 int result = 0;
2501 int i;
2502
2503 for (i = 0; i < 8 * sizeof(int); i++) {
2504 if ((1 << i) & ib_mask)
2505 result |= ib_nr_to_mlx5_nr(1 << i);
2506 }
2507
2508 return result;
2509}
2510
Alex Veskereb49ab02016-08-28 12:25:53 +03002511static int modify_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
2512 struct mlx5_ib_rq *rq, int new_state,
2513 const struct mlx5_modify_raw_qp_param *raw_qp_param)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002514{
2515 void *in;
2516 void *rqc;
2517 int inlen;
2518 int err;
2519
2520 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002521 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002522 if (!in)
2523 return -ENOMEM;
2524
2525 MLX5_SET(modify_rq_in, in, rq_state, rq->state);
2526
2527 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
2528 MLX5_SET(rqc, rqc, state, new_state);
2529
Alex Veskereb49ab02016-08-28 12:25:53 +03002530 if (raw_qp_param->set_mask & MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID) {
2531 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
2532 MLX5_SET64(modify_rq_in, in, modify_bitmask,
Majd Dibbiny23a69642017-01-18 15:25:10 +02002533 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Alex Veskereb49ab02016-08-28 12:25:53 +03002534 MLX5_SET(rqc, rqc, counter_set_id, raw_qp_param->rq_q_ctr_id);
2535 } else
2536 pr_info_once("%s: RAW PACKET QP counters are not supported on current FW\n",
2537 dev->ib_dev.name);
2538 }
2539
2540 err = mlx5_core_modify_rq(dev->mdev, rq->base.mqp.qpn, in, inlen);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002541 if (err)
2542 goto out;
2543
2544 rq->state = new_state;
2545
2546out:
2547 kvfree(in);
2548 return err;
2549}
2550
2551static int modify_raw_packet_qp_sq(struct mlx5_core_dev *dev,
Bodong Wang7d29f342016-12-01 13:43:16 +02002552 struct mlx5_ib_sq *sq,
2553 int new_state,
2554 const struct mlx5_modify_raw_qp_param *raw_qp_param)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002555{
Bodong Wang7d29f342016-12-01 13:43:16 +02002556 struct mlx5_ib_qp *ibqp = sq->base.container_mibqp;
2557 u32 old_rate = ibqp->rate_limit;
2558 u32 new_rate = old_rate;
2559 u16 rl_index = 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002560 void *in;
2561 void *sqc;
2562 int inlen;
2563 int err;
2564
2565 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002566 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002567 if (!in)
2568 return -ENOMEM;
2569
2570 MLX5_SET(modify_sq_in, in, sq_state, sq->state);
2571
2572 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
2573 MLX5_SET(sqc, sqc, state, new_state);
2574
Bodong Wang7d29f342016-12-01 13:43:16 +02002575 if (raw_qp_param->set_mask & MLX5_RAW_QP_RATE_LIMIT) {
2576 if (new_state != MLX5_SQC_STATE_RDY)
2577 pr_warn("%s: Rate limit can only be changed when SQ is moving to RDY\n",
2578 __func__);
2579 else
2580 new_rate = raw_qp_param->rate_limit;
2581 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002582
Bodong Wang7d29f342016-12-01 13:43:16 +02002583 if (old_rate != new_rate) {
2584 if (new_rate) {
2585 err = mlx5_rl_add_rate(dev, new_rate, &rl_index);
2586 if (err) {
2587 pr_err("Failed configuring rate %u: %d\n",
2588 new_rate, err);
2589 goto out;
2590 }
2591 }
2592
2593 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
2594 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
2595 }
2596
2597 err = mlx5_core_modify_sq(dev, sq->base.mqp.qpn, in, inlen);
2598 if (err) {
2599 /* Remove new rate from table if failed */
2600 if (new_rate &&
2601 old_rate != new_rate)
2602 mlx5_rl_remove_rate(dev, new_rate);
2603 goto out;
2604 }
2605
2606 /* Only remove the old rate after new rate was set */
2607 if ((old_rate &&
2608 (old_rate != new_rate)) ||
2609 (new_state != MLX5_SQC_STATE_RDY))
2610 mlx5_rl_remove_rate(dev, old_rate);
2611
2612 ibqp->rate_limit = new_rate;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002613 sq->state = new_state;
2614
2615out:
2616 kvfree(in);
2617 return err;
2618}
2619
2620static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03002621 const struct mlx5_modify_raw_qp_param *raw_qp_param,
2622 u8 tx_affinity)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002623{
2624 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
2625 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
2626 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
Bodong Wang7d29f342016-12-01 13:43:16 +02002627 int modify_rq = !!qp->rq.wqe_cnt;
2628 int modify_sq = !!qp->sq.wqe_cnt;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002629 int rq_state;
2630 int sq_state;
2631 int err;
2632
Alex Vesker0680efa2016-08-28 12:25:52 +03002633 switch (raw_qp_param->operation) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002634 case MLX5_CMD_OP_RST2INIT_QP:
2635 rq_state = MLX5_RQC_STATE_RDY;
2636 sq_state = MLX5_SQC_STATE_RDY;
2637 break;
2638 case MLX5_CMD_OP_2ERR_QP:
2639 rq_state = MLX5_RQC_STATE_ERR;
2640 sq_state = MLX5_SQC_STATE_ERR;
2641 break;
2642 case MLX5_CMD_OP_2RST_QP:
2643 rq_state = MLX5_RQC_STATE_RST;
2644 sq_state = MLX5_SQC_STATE_RST;
2645 break;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002646 case MLX5_CMD_OP_RTR2RTS_QP:
2647 case MLX5_CMD_OP_RTS2RTS_QP:
Bodong Wang7d29f342016-12-01 13:43:16 +02002648 if (raw_qp_param->set_mask ==
2649 MLX5_RAW_QP_RATE_LIMIT) {
2650 modify_rq = 0;
2651 sq_state = sq->state;
2652 } else {
2653 return raw_qp_param->set_mask ? -EINVAL : 0;
2654 }
2655 break;
2656 case MLX5_CMD_OP_INIT2INIT_QP:
2657 case MLX5_CMD_OP_INIT2RTR_QP:
Alex Veskereb49ab02016-08-28 12:25:53 +03002658 if (raw_qp_param->set_mask)
2659 return -EINVAL;
2660 else
2661 return 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002662 default:
2663 WARN_ON(1);
2664 return -EINVAL;
2665 }
2666
Bodong Wang7d29f342016-12-01 13:43:16 +02002667 if (modify_rq) {
2668 err = modify_raw_packet_qp_rq(dev, rq, rq_state, raw_qp_param);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002669 if (err)
2670 return err;
2671 }
2672
Bodong Wang7d29f342016-12-01 13:43:16 +02002673 if (modify_sq) {
Aviv Heller13eab212016-09-18 20:48:04 +03002674 if (tx_affinity) {
2675 err = modify_raw_packet_tx_affinity(dev->mdev, sq,
2676 tx_affinity);
2677 if (err)
2678 return err;
2679 }
2680
Bodong Wang7d29f342016-12-01 13:43:16 +02002681 return modify_raw_packet_qp_sq(dev->mdev, sq, sq_state, raw_qp_param);
Aviv Heller13eab212016-09-18 20:48:04 +03002682 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002683
2684 return 0;
2685}
2686
Eli Cohene126ba92013-07-07 17:25:49 +03002687static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
2688 const struct ib_qp_attr *attr, int attr_mask,
2689 enum ib_qp_state cur_state, enum ib_qp_state new_state)
2690{
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002691 static const u16 optab[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE] = {
2692 [MLX5_QP_STATE_RST] = {
2693 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2694 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2695 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_RST2INIT_QP,
2696 },
2697 [MLX5_QP_STATE_INIT] = {
2698 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2699 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2700 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_INIT2INIT_QP,
2701 [MLX5_QP_STATE_RTR] = MLX5_CMD_OP_INIT2RTR_QP,
2702 },
2703 [MLX5_QP_STATE_RTR] = {
2704 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2705 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2706 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTR2RTS_QP,
2707 },
2708 [MLX5_QP_STATE_RTS] = {
2709 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2710 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2711 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTS2RTS_QP,
2712 },
2713 [MLX5_QP_STATE_SQD] = {
2714 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2715 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2716 },
2717 [MLX5_QP_STATE_SQER] = {
2718 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2719 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2720 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_SQERR2RTS_QP,
2721 },
2722 [MLX5_QP_STATE_ERR] = {
2723 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2724 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2725 }
2726 };
2727
Eli Cohene126ba92013-07-07 17:25:49 +03002728 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2729 struct mlx5_ib_qp *qp = to_mqp(ibqp);
majd@mellanox.com19098df2016-01-14 19:13:03 +02002730 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Eli Cohene126ba92013-07-07 17:25:49 +03002731 struct mlx5_ib_cq *send_cq, *recv_cq;
2732 struct mlx5_qp_context *context;
Eli Cohene126ba92013-07-07 17:25:49 +03002733 struct mlx5_ib_pd *pd;
Alex Veskereb49ab02016-08-28 12:25:53 +03002734 struct mlx5_ib_port *mibport = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002735 enum mlx5_qp_state mlx5_cur, mlx5_new;
2736 enum mlx5_qp_optpar optpar;
Eli Cohene126ba92013-07-07 17:25:49 +03002737 int mlx5_st;
2738 int err;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002739 u16 op;
Aviv Heller13eab212016-09-18 20:48:04 +03002740 u8 tx_affinity = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002741
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002742 context = kzalloc(sizeof(*context), GFP_KERNEL);
2743 if (!context)
Eli Cohene126ba92013-07-07 17:25:49 +03002744 return -ENOMEM;
2745
Eli Cohene126ba92013-07-07 17:25:49 +03002746 err = to_mlx5_st(ibqp->qp_type);
Haggai Eran158abf82016-02-29 15:45:04 +02002747 if (err < 0) {
2748 mlx5_ib_dbg(dev, "unsupported qp type %d\n", ibqp->qp_type);
Eli Cohene126ba92013-07-07 17:25:49 +03002749 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002750 }
Eli Cohene126ba92013-07-07 17:25:49 +03002751
2752 context->flags = cpu_to_be32(err << 16);
2753
2754 if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
2755 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
2756 } else {
2757 switch (attr->path_mig_state) {
2758 case IB_MIG_MIGRATED:
2759 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
2760 break;
2761 case IB_MIG_REARM:
2762 context->flags |= cpu_to_be32(MLX5_QP_PM_REARM << 11);
2763 break;
2764 case IB_MIG_ARMED:
2765 context->flags |= cpu_to_be32(MLX5_QP_PM_ARMED << 11);
2766 break;
2767 }
2768 }
2769
Aviv Heller13eab212016-09-18 20:48:04 +03002770 if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
2771 if ((ibqp->qp_type == IB_QPT_RC) ||
2772 (ibqp->qp_type == IB_QPT_UD &&
2773 !(qp->flags & MLX5_IB_QP_SQPN_QP1)) ||
2774 (ibqp->qp_type == IB_QPT_UC) ||
2775 (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
2776 (ibqp->qp_type == IB_QPT_XRC_INI) ||
2777 (ibqp->qp_type == IB_QPT_XRC_TGT)) {
2778 if (mlx5_lag_is_active(dev->mdev)) {
2779 tx_affinity = (unsigned int)atomic_add_return(1,
2780 &dev->roce.next_port) %
2781 MLX5_MAX_PORTS + 1;
2782 context->flags |= cpu_to_be32(tx_affinity << 24);
2783 }
2784 }
2785 }
2786
Haggai Erand16e91d2016-02-29 15:45:05 +02002787 if (is_sqp(ibqp->qp_type)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002788 context->mtu_msgmax = (IB_MTU_256 << 5) | 8;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002789 } else if ((ibqp->qp_type == IB_QPT_UD &&
2790 !(qp->flags & MLX5_IB_QP_UNDERLAY)) ||
Eli Cohene126ba92013-07-07 17:25:49 +03002791 ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
2792 context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
2793 } else if (attr_mask & IB_QP_PATH_MTU) {
2794 if (attr->path_mtu < IB_MTU_256 ||
2795 attr->path_mtu > IB_MTU_4096) {
2796 mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
2797 err = -EINVAL;
2798 goto out;
2799 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03002800 context->mtu_msgmax = (attr->path_mtu << 5) |
2801 (u8)MLX5_CAP_GEN(dev->mdev, log_max_msg);
Eli Cohene126ba92013-07-07 17:25:49 +03002802 }
2803
2804 if (attr_mask & IB_QP_DEST_QPN)
2805 context->log_pg_sz_remote_qpn = cpu_to_be32(attr->dest_qp_num);
2806
2807 if (attr_mask & IB_QP_PKEY_INDEX)
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03002808 context->pri_path.pkey_index = cpu_to_be16(attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002809
2810 /* todo implement counter_index functionality */
2811
2812 if (is_sqp(ibqp->qp_type))
2813 context->pri_path.port = qp->port;
2814
2815 if (attr_mask & IB_QP_PORT)
2816 context->pri_path.port = attr->port_num;
2817
2818 if (attr_mask & IB_QP_AV) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02002819 err = mlx5_set_path(dev, qp, &attr->ah_attr, &context->pri_path,
Eli Cohene126ba92013-07-07 17:25:49 +03002820 attr_mask & IB_QP_PORT ? attr->port_num : qp->port,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002821 attr_mask, 0, attr, false);
Eli Cohene126ba92013-07-07 17:25:49 +03002822 if (err)
2823 goto out;
2824 }
2825
2826 if (attr_mask & IB_QP_TIMEOUT)
2827 context->pri_path.ackto_lt |= attr->timeout << 3;
2828
2829 if (attr_mask & IB_QP_ALT_PATH) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02002830 err = mlx5_set_path(dev, qp, &attr->alt_ah_attr,
2831 &context->alt_path,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002832 attr->alt_port_num,
2833 attr_mask | IB_QP_PKEY_INDEX | IB_QP_TIMEOUT,
2834 0, attr, true);
Eli Cohene126ba92013-07-07 17:25:49 +03002835 if (err)
2836 goto out;
2837 }
2838
2839 pd = get_pd(qp);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002840 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
2841 &send_cq, &recv_cq);
Eli Cohene126ba92013-07-07 17:25:49 +03002842
2843 context->flags_pd = cpu_to_be32(pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
2844 context->cqn_send = send_cq ? cpu_to_be32(send_cq->mcq.cqn) : 0;
2845 context->cqn_recv = recv_cq ? cpu_to_be32(recv_cq->mcq.cqn) : 0;
2846 context->params1 = cpu_to_be32(MLX5_IB_ACK_REQ_FREQ << 28);
2847
2848 if (attr_mask & IB_QP_RNR_RETRY)
2849 context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
2850
2851 if (attr_mask & IB_QP_RETRY_CNT)
2852 context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
2853
2854 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
2855 if (attr->max_rd_atomic)
2856 context->params1 |=
2857 cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
2858 }
2859
2860 if (attr_mask & IB_QP_SQ_PSN)
2861 context->next_send_psn = cpu_to_be32(attr->sq_psn);
2862
2863 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
2864 if (attr->max_dest_rd_atomic)
2865 context->params2 |=
2866 cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
2867 }
2868
2869 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC))
2870 context->params2 |= to_mlx5_access_flags(qp, attr, attr_mask);
2871
2872 if (attr_mask & IB_QP_MIN_RNR_TIMER)
2873 context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
2874
2875 if (attr_mask & IB_QP_RQ_PSN)
2876 context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
2877
2878 if (attr_mask & IB_QP_QKEY)
2879 context->qkey = cpu_to_be32(attr->qkey);
2880
2881 if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
2882 context->db_rec_addr = cpu_to_be64(qp->db.dma);
2883
Mark Bloch0837e862016-06-17 15:10:55 +03002884 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
2885 u8 port_num = (attr_mask & IB_QP_PORT ? attr->port_num :
2886 qp->port) - 1;
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002887
2888 /* Underlay port should be used - index 0 function per port */
2889 if (qp->flags & MLX5_IB_QP_UNDERLAY)
2890 port_num = 0;
2891
Alex Veskereb49ab02016-08-28 12:25:53 +03002892 mibport = &dev->port[port_num];
Mark Bloch0837e862016-06-17 15:10:55 +03002893 context->qp_counter_set_usr_page |=
Parav Pandite1f24a72017-04-16 07:29:29 +03002894 cpu_to_be32((u32)(mibport->cnts.set_id) << 24);
Mark Bloch0837e862016-06-17 15:10:55 +03002895 }
2896
Eli Cohene126ba92013-07-07 17:25:49 +03002897 if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
2898 context->sq_crq_size |= cpu_to_be16(1 << 4);
2899
Haggai Eranb11a4f92016-02-29 15:45:03 +02002900 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
2901 context->deth_sqpn = cpu_to_be32(1);
Eli Cohene126ba92013-07-07 17:25:49 +03002902
2903 mlx5_cur = to_mlx5_state(cur_state);
2904 mlx5_new = to_mlx5_state(new_state);
2905 mlx5_st = to_mlx5_st(ibqp->qp_type);
Eli Cohen07c91132013-10-24 12:01:01 +03002906 if (mlx5_st < 0)
Eli Cohene126ba92013-07-07 17:25:49 +03002907 goto out;
2908
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002909 if (mlx5_cur >= MLX5_QP_NUM_STATE || mlx5_new >= MLX5_QP_NUM_STATE ||
2910 !optab[mlx5_cur][mlx5_new])
2911 goto out;
2912
2913 op = optab[mlx5_cur][mlx5_new];
Eli Cohene126ba92013-07-07 17:25:49 +03002914 optpar = ib_mask_to_mlx5_opt(attr_mask);
2915 optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002916
Yishai Hadasc2e53b22017-06-08 16:15:08 +03002917 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2918 qp->flags & MLX5_IB_QP_UNDERLAY) {
Alex Vesker0680efa2016-08-28 12:25:52 +03002919 struct mlx5_modify_raw_qp_param raw_qp_param = {};
2920
2921 raw_qp_param.operation = op;
Alex Veskereb49ab02016-08-28 12:25:53 +03002922 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Parav Pandite1f24a72017-04-16 07:29:29 +03002923 raw_qp_param.rq_q_ctr_id = mibport->cnts.set_id;
Alex Veskereb49ab02016-08-28 12:25:53 +03002924 raw_qp_param.set_mask |= MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID;
2925 }
Bodong Wang7d29f342016-12-01 13:43:16 +02002926
2927 if (attr_mask & IB_QP_RATE_LIMIT) {
2928 raw_qp_param.rate_limit = attr->rate_limit;
2929 raw_qp_param.set_mask |= MLX5_RAW_QP_RATE_LIMIT;
2930 }
2931
Aviv Heller13eab212016-09-18 20:48:04 +03002932 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, tx_affinity);
Alex Vesker0680efa2016-08-28 12:25:52 +03002933 } else {
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002934 err = mlx5_core_qp_modify(dev->mdev, op, optpar, context,
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002935 &base->mqp);
Alex Vesker0680efa2016-08-28 12:25:52 +03002936 }
2937
Eli Cohene126ba92013-07-07 17:25:49 +03002938 if (err)
2939 goto out;
2940
2941 qp->state = new_state;
2942
2943 if (attr_mask & IB_QP_ACCESS_FLAGS)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002944 qp->trans_qp.atomic_rd_en = attr->qp_access_flags;
Eli Cohene126ba92013-07-07 17:25:49 +03002945 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002946 qp->trans_qp.resp_depth = attr->max_dest_rd_atomic;
Eli Cohene126ba92013-07-07 17:25:49 +03002947 if (attr_mask & IB_QP_PORT)
2948 qp->port = attr->port_num;
2949 if (attr_mask & IB_QP_ALT_PATH)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002950 qp->trans_qp.alt_port = attr->alt_port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03002951
2952 /*
2953 * If we moved a kernel QP to RESET, clean up all old CQ
2954 * entries and reinitialize the QP.
2955 */
2956 if (new_state == IB_QPS_RESET && !ibqp->uobject) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002957 mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03002958 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
2959 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002960 mlx5_ib_cq_clean(send_cq, base->mqp.qpn, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002961
2962 qp->rq.head = 0;
2963 qp->rq.tail = 0;
2964 qp->sq.head = 0;
2965 qp->sq.tail = 0;
2966 qp->sq.cur_post = 0;
2967 qp->sq.last_poll = 0;
2968 qp->db.db[MLX5_RCV_DBR] = 0;
2969 qp->db.db[MLX5_SND_DBR] = 0;
2970 }
2971
2972out:
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002973 kfree(context);
Eli Cohene126ba92013-07-07 17:25:49 +03002974 return err;
2975}
2976
2977int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2978 int attr_mask, struct ib_udata *udata)
2979{
2980 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2981 struct mlx5_ib_qp *qp = to_mqp(ibqp);
Haggai Erand16e91d2016-02-29 15:45:05 +02002982 enum ib_qp_type qp_type;
Eli Cohene126ba92013-07-07 17:25:49 +03002983 enum ib_qp_state cur_state, new_state;
2984 int err = -EINVAL;
2985 int port;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002986 enum rdma_link_layer ll = IB_LINK_LAYER_UNSPECIFIED;
Eli Cohene126ba92013-07-07 17:25:49 +03002987
Yishai Hadas28d61372016-05-23 15:20:56 +03002988 if (ibqp->rwq_ind_tbl)
2989 return -ENOSYS;
2990
Haggai Erand16e91d2016-02-29 15:45:05 +02002991 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
2992 return mlx5_ib_gsi_modify_qp(ibqp, attr, attr_mask);
2993
2994 qp_type = (unlikely(ibqp->qp_type == MLX5_IB_QPT_HW_GSI)) ?
2995 IB_QPT_GSI : ibqp->qp_type;
2996
Eli Cohene126ba92013-07-07 17:25:49 +03002997 mutex_lock(&qp->mutex);
2998
2999 cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
3000 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
3001
Achiad Shochat2811ba52015-12-23 18:47:24 +02003002 if (!(cur_state == new_state && cur_state == IB_QPS_RESET)) {
3003 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
3004 ll = dev->ib_dev.get_link_layer(&dev->ib_dev, port);
3005 }
3006
Yishai Hadasc2e53b22017-06-08 16:15:08 +03003007 if (qp->flags & MLX5_IB_QP_UNDERLAY) {
3008 if (attr_mask & ~(IB_QP_STATE | IB_QP_CUR_STATE)) {
3009 mlx5_ib_dbg(dev, "invalid attr_mask 0x%x when underlay QP is used\n",
3010 attr_mask);
3011 goto out;
3012 }
3013 } else if (qp_type != MLX5_IB_QPT_REG_UMR &&
Haggai Erand16e91d2016-02-29 15:45:05 +02003014 !ib_modify_qp_is_ok(cur_state, new_state, qp_type, attr_mask, ll)) {
Haggai Eran158abf82016-02-29 15:45:04 +02003015 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
3016 cur_state, new_state, ibqp->qp_type, attr_mask);
Eli Cohene126ba92013-07-07 17:25:49 +03003017 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003018 }
Eli Cohene126ba92013-07-07 17:25:49 +03003019
3020 if ((attr_mask & IB_QP_PORT) &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003021 (attr->port_num == 0 ||
Haggai Eran158abf82016-02-29 15:45:04 +02003022 attr->port_num > MLX5_CAP_GEN(dev->mdev, num_ports))) {
3023 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
3024 attr->port_num, dev->num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03003025 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003026 }
Eli Cohene126ba92013-07-07 17:25:49 +03003027
3028 if (attr_mask & IB_QP_PKEY_INDEX) {
3029 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Saeed Mahameed938fe832015-05-28 22:28:41 +03003030 if (attr->pkey_index >=
Haggai Eran158abf82016-02-29 15:45:04 +02003031 dev->mdev->port_caps[port - 1].pkey_table_len) {
3032 mlx5_ib_dbg(dev, "invalid pkey index %d\n",
3033 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03003034 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003035 }
Eli Cohene126ba92013-07-07 17:25:49 +03003036 }
3037
3038 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003039 attr->max_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02003040 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_res_qp))) {
3041 mlx5_ib_dbg(dev, "invalid max_rd_atomic value %d\n",
3042 attr->max_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03003043 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003044 }
Eli Cohene126ba92013-07-07 17:25:49 +03003045
3046 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03003047 attr->max_dest_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02003048 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_req_qp))) {
3049 mlx5_ib_dbg(dev, "invalid max_dest_rd_atomic value %d\n",
3050 attr->max_dest_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03003051 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02003052 }
Eli Cohene126ba92013-07-07 17:25:49 +03003053
3054 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
3055 err = 0;
3056 goto out;
3057 }
3058
3059 err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
3060
3061out:
3062 mutex_unlock(&qp->mutex);
3063 return err;
3064}
3065
3066static int mlx5_wq_overflow(struct mlx5_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
3067{
3068 struct mlx5_ib_cq *cq;
3069 unsigned cur;
3070
3071 cur = wq->head - wq->tail;
3072 if (likely(cur + nreq < wq->max_post))
3073 return 0;
3074
3075 cq = to_mcq(ib_cq);
3076 spin_lock(&cq->lock);
3077 cur = wq->head - wq->tail;
3078 spin_unlock(&cq->lock);
3079
3080 return cur + nreq >= wq->max_post;
3081}
3082
3083static __always_inline void set_raddr_seg(struct mlx5_wqe_raddr_seg *rseg,
3084 u64 remote_addr, u32 rkey)
3085{
3086 rseg->raddr = cpu_to_be64(remote_addr);
3087 rseg->rkey = cpu_to_be32(rkey);
3088 rseg->reserved = 0;
3089}
3090
Erez Shitritf0313962016-02-21 16:27:17 +02003091static void *set_eth_seg(struct mlx5_wqe_eth_seg *eseg,
3092 struct ib_send_wr *wr, void *qend,
3093 struct mlx5_ib_qp *qp, int *size)
3094{
3095 void *seg = eseg;
3096
3097 memset(eseg, 0, sizeof(struct mlx5_wqe_eth_seg));
3098
3099 if (wr->send_flags & IB_SEND_IP_CSUM)
3100 eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM |
3101 MLX5_ETH_WQE_L4_CSUM;
3102
3103 seg += sizeof(struct mlx5_wqe_eth_seg);
3104 *size += sizeof(struct mlx5_wqe_eth_seg) / 16;
3105
3106 if (wr->opcode == IB_WR_LSO) {
3107 struct ib_ud_wr *ud_wr = container_of(wr, struct ib_ud_wr, wr);
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02003108 int size_of_inl_hdr_start = sizeof(eseg->inline_hdr.start);
Erez Shitritf0313962016-02-21 16:27:17 +02003109 u64 left, leftlen, copysz;
3110 void *pdata = ud_wr->header;
3111
3112 left = ud_wr->hlen;
3113 eseg->mss = cpu_to_be16(ud_wr->mss);
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02003114 eseg->inline_hdr.sz = cpu_to_be16(left);
Erez Shitritf0313962016-02-21 16:27:17 +02003115
3116 /*
3117 * check if there is space till the end of queue, if yes,
3118 * copy all in one shot, otherwise copy till the end of queue,
3119 * rollback and than the copy the left
3120 */
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02003121 leftlen = qend - (void *)eseg->inline_hdr.start;
Erez Shitritf0313962016-02-21 16:27:17 +02003122 copysz = min_t(u64, leftlen, left);
3123
3124 memcpy(seg - size_of_inl_hdr_start, pdata, copysz);
3125
3126 if (likely(copysz > size_of_inl_hdr_start)) {
3127 seg += ALIGN(copysz - size_of_inl_hdr_start, 16);
3128 *size += ALIGN(copysz - size_of_inl_hdr_start, 16) / 16;
3129 }
3130
3131 if (unlikely(copysz < left)) { /* the last wqe in the queue */
3132 seg = mlx5_get_send_wqe(qp, 0);
3133 left -= copysz;
3134 pdata += copysz;
3135 memcpy(seg, pdata, left);
3136 seg += ALIGN(left, 16);
3137 *size += ALIGN(left, 16) / 16;
3138 }
3139 }
3140
3141 return seg;
3142}
3143
Eli Cohene126ba92013-07-07 17:25:49 +03003144static void set_datagram_seg(struct mlx5_wqe_datagram_seg *dseg,
3145 struct ib_send_wr *wr)
3146{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003147 memcpy(&dseg->av, &to_mah(ud_wr(wr)->ah)->av, sizeof(struct mlx5_av));
3148 dseg->av.dqp_dct = cpu_to_be32(ud_wr(wr)->remote_qpn | MLX5_EXTENDED_UD_AV);
3149 dseg->av.key.qkey.qkey = cpu_to_be32(ud_wr(wr)->remote_qkey);
Eli Cohene126ba92013-07-07 17:25:49 +03003150}
3151
3152static void set_data_ptr_seg(struct mlx5_wqe_data_seg *dseg, struct ib_sge *sg)
3153{
3154 dseg->byte_count = cpu_to_be32(sg->length);
3155 dseg->lkey = cpu_to_be32(sg->lkey);
3156 dseg->addr = cpu_to_be64(sg->addr);
3157}
3158
Artemy Kovalyov31616252017-01-02 11:37:42 +02003159static u64 get_xlt_octo(u64 bytes)
Eli Cohene126ba92013-07-07 17:25:49 +03003160{
Artemy Kovalyov31616252017-01-02 11:37:42 +02003161 return ALIGN(bytes, MLX5_IB_UMR_XLT_ALIGNMENT) /
3162 MLX5_IB_UMR_OCTOWORD;
Eli Cohene126ba92013-07-07 17:25:49 +03003163}
3164
3165static __be64 frwr_mkey_mask(void)
3166{
3167 u64 result;
3168
3169 result = MLX5_MKEY_MASK_LEN |
3170 MLX5_MKEY_MASK_PAGE_SIZE |
3171 MLX5_MKEY_MASK_START_ADDR |
3172 MLX5_MKEY_MASK_EN_RINVAL |
3173 MLX5_MKEY_MASK_KEY |
3174 MLX5_MKEY_MASK_LR |
3175 MLX5_MKEY_MASK_LW |
3176 MLX5_MKEY_MASK_RR |
3177 MLX5_MKEY_MASK_RW |
3178 MLX5_MKEY_MASK_A |
3179 MLX5_MKEY_MASK_SMALL_FENCE |
3180 MLX5_MKEY_MASK_FREE;
3181
3182 return cpu_to_be64(result);
3183}
3184
Sagi Grimberge6631812014-02-23 14:19:11 +02003185static __be64 sig_mkey_mask(void)
3186{
3187 u64 result;
3188
3189 result = MLX5_MKEY_MASK_LEN |
3190 MLX5_MKEY_MASK_PAGE_SIZE |
3191 MLX5_MKEY_MASK_START_ADDR |
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003192 MLX5_MKEY_MASK_EN_SIGERR |
Sagi Grimberge6631812014-02-23 14:19:11 +02003193 MLX5_MKEY_MASK_EN_RINVAL |
3194 MLX5_MKEY_MASK_KEY |
3195 MLX5_MKEY_MASK_LR |
3196 MLX5_MKEY_MASK_LW |
3197 MLX5_MKEY_MASK_RR |
3198 MLX5_MKEY_MASK_RW |
3199 MLX5_MKEY_MASK_SMALL_FENCE |
3200 MLX5_MKEY_MASK_FREE |
3201 MLX5_MKEY_MASK_BSF_EN;
3202
3203 return cpu_to_be64(result);
3204}
3205
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003206static void set_reg_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02003207 struct mlx5_ib_mr *mr)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003208{
Artemy Kovalyov31616252017-01-02 11:37:42 +02003209 int size = mr->ndescs * mr->desc_size;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003210
3211 memset(umr, 0, sizeof(*umr));
Sagi Grimbergb005d312016-02-29 19:07:33 +02003212
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003213 umr->flags = MLX5_UMR_CHECK_NOT_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02003214 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003215 umr->mkey_mask = frwr_mkey_mask();
3216}
3217
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003218static void set_linv_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr)
Eli Cohene126ba92013-07-07 17:25:49 +03003219{
3220 memset(umr, 0, sizeof(*umr));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003221 umr->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
Max Gurtovoy2d221582016-10-27 16:36:36 +03003222 umr->flags = MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03003223}
3224
Artemy Kovalyov31616252017-01-02 11:37:42 +02003225static __be64 get_umr_enable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02003226{
3227 u64 result;
3228
Artemy Kovalyov31616252017-01-02 11:37:42 +02003229 result = MLX5_MKEY_MASK_KEY |
Haggai Eran968e78d2014-12-11 17:04:11 +02003230 MLX5_MKEY_MASK_FREE;
3231
3232 return cpu_to_be64(result);
3233}
3234
Artemy Kovalyov31616252017-01-02 11:37:42 +02003235static __be64 get_umr_disable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02003236{
3237 u64 result;
3238
3239 result = MLX5_MKEY_MASK_FREE;
3240
3241 return cpu_to_be64(result);
3242}
3243
Noa Osherovich56e11d62016-02-29 16:46:51 +02003244static __be64 get_umr_update_translation_mask(void)
3245{
3246 u64 result;
3247
3248 result = MLX5_MKEY_MASK_LEN |
3249 MLX5_MKEY_MASK_PAGE_SIZE |
Artemy Kovalyov31616252017-01-02 11:37:42 +02003250 MLX5_MKEY_MASK_START_ADDR;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003251
3252 return cpu_to_be64(result);
3253}
3254
Artemy Kovalyov31616252017-01-02 11:37:42 +02003255static __be64 get_umr_update_access_mask(int atomic)
Noa Osherovich56e11d62016-02-29 16:46:51 +02003256{
3257 u64 result;
3258
Artemy Kovalyov31616252017-01-02 11:37:42 +02003259 result = MLX5_MKEY_MASK_LR |
3260 MLX5_MKEY_MASK_LW |
Noa Osherovich56e11d62016-02-29 16:46:51 +02003261 MLX5_MKEY_MASK_RR |
Artemy Kovalyov31616252017-01-02 11:37:42 +02003262 MLX5_MKEY_MASK_RW;
3263
3264 if (atomic)
3265 result |= MLX5_MKEY_MASK_A;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003266
3267 return cpu_to_be64(result);
3268}
3269
3270static __be64 get_umr_update_pd_mask(void)
3271{
3272 u64 result;
3273
Artemy Kovalyov31616252017-01-02 11:37:42 +02003274 result = MLX5_MKEY_MASK_PD;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003275
3276 return cpu_to_be64(result);
3277}
3278
Eli Cohene126ba92013-07-07 17:25:49 +03003279static void set_reg_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Maor Gottlieb578e7262016-10-27 16:36:37 +03003280 struct ib_send_wr *wr, int atomic)
Eli Cohene126ba92013-07-07 17:25:49 +03003281{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003282 struct mlx5_umr_wr *umrwr = umr_wr(wr);
Eli Cohene126ba92013-07-07 17:25:49 +03003283
3284 memset(umr, 0, sizeof(*umr));
3285
Haggai Eran968e78d2014-12-11 17:04:11 +02003286 if (wr->send_flags & MLX5_IB_SEND_UMR_FAIL_IF_FREE)
3287 umr->flags = MLX5_UMR_CHECK_FREE; /* fail if free */
3288 else
3289 umr->flags = MLX5_UMR_CHECK_NOT_FREE; /* fail if not free */
3290
Artemy Kovalyov31616252017-01-02 11:37:42 +02003291 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(umrwr->xlt_size));
3292 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_XLT) {
3293 u64 offset = get_xlt_octo(umrwr->offset);
3294
3295 umr->xlt_offset = cpu_to_be16(offset & 0xffff);
3296 umr->xlt_offset_47_16 = cpu_to_be32(offset >> 16);
3297 umr->flags |= MLX5_UMR_TRANSLATION_OFFSET_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03003298 }
Artemy Kovalyov31616252017-01-02 11:37:42 +02003299 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION)
3300 umr->mkey_mask |= get_umr_update_translation_mask();
3301 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS) {
3302 umr->mkey_mask |= get_umr_update_access_mask(atomic);
3303 umr->mkey_mask |= get_umr_update_pd_mask();
3304 }
3305 if (wr->send_flags & MLX5_IB_SEND_UMR_ENABLE_MR)
3306 umr->mkey_mask |= get_umr_enable_mr_mask();
3307 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
3308 umr->mkey_mask |= get_umr_disable_mr_mask();
Eli Cohene126ba92013-07-07 17:25:49 +03003309
3310 if (!wr->num_sge)
Haggai Eran968e78d2014-12-11 17:04:11 +02003311 umr->flags |= MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03003312}
3313
3314static u8 get_umr_flags(int acc)
3315{
3316 return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC : 0) |
3317 (acc & IB_ACCESS_REMOTE_WRITE ? MLX5_PERM_REMOTE_WRITE : 0) |
3318 (acc & IB_ACCESS_REMOTE_READ ? MLX5_PERM_REMOTE_READ : 0) |
3319 (acc & IB_ACCESS_LOCAL_WRITE ? MLX5_PERM_LOCAL_WRITE : 0) |
Sagi Grimberg2ac45932014-02-23 14:19:09 +02003320 MLX5_PERM_LOCAL_READ | MLX5_PERM_UMR_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03003321}
3322
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003323static void set_reg_mkey_seg(struct mlx5_mkey_seg *seg,
3324 struct mlx5_ib_mr *mr,
3325 u32 key, int access)
3326{
3327 int ndescs = ALIGN(mr->ndescs, 8) >> 1;
3328
3329 memset(seg, 0, sizeof(*seg));
Sagi Grimbergb005d312016-02-29 19:07:33 +02003330
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003331 if (mr->access_mode == MLX5_MKC_ACCESS_MODE_MTT)
Sagi Grimbergb005d312016-02-29 19:07:33 +02003332 seg->log2_page_size = ilog2(mr->ibmr.page_size);
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003333 else if (mr->access_mode == MLX5_MKC_ACCESS_MODE_KLMS)
Sagi Grimbergb005d312016-02-29 19:07:33 +02003334 /* KLMs take twice the size of MTTs */
3335 ndescs *= 2;
3336
3337 seg->flags = get_umr_flags(access) | mr->access_mode;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003338 seg->qpn_mkey7_0 = cpu_to_be32((key & 0xff) | 0xffffff00);
3339 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL);
3340 seg->start_addr = cpu_to_be64(mr->ibmr.iova);
3341 seg->len = cpu_to_be64(mr->ibmr.length);
3342 seg->xlt_oct_size = cpu_to_be32(ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003343}
3344
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003345static void set_linv_mkey_seg(struct mlx5_mkey_seg *seg)
Eli Cohene126ba92013-07-07 17:25:49 +03003346{
3347 memset(seg, 0, sizeof(*seg));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003348 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03003349}
3350
3351static void set_reg_mkey_segment(struct mlx5_mkey_seg *seg, struct ib_send_wr *wr)
3352{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003353 struct mlx5_umr_wr *umrwr = umr_wr(wr);
Haggai Eran968e78d2014-12-11 17:04:11 +02003354
Eli Cohene126ba92013-07-07 17:25:49 +03003355 memset(seg, 0, sizeof(*seg));
Artemy Kovalyov31616252017-01-02 11:37:42 +02003356 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
Haggai Eran968e78d2014-12-11 17:04:11 +02003357 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03003358
Haggai Eran968e78d2014-12-11 17:04:11 +02003359 seg->flags = convert_access(umrwr->access_flags);
Artemy Kovalyov31616252017-01-02 11:37:42 +02003360 if (umrwr->pd)
3361 seg->flags_pd = cpu_to_be32(to_mpd(umrwr->pd)->pdn);
3362 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION &&
3363 !umrwr->length)
3364 seg->flags_pd |= cpu_to_be32(MLX5_MKEY_LEN64);
3365
3366 seg->start_addr = cpu_to_be64(umrwr->virt_addr);
Haggai Eran968e78d2014-12-11 17:04:11 +02003367 seg->len = cpu_to_be64(umrwr->length);
3368 seg->log2_page_size = umrwr->page_shift;
Eli Cohen746b5582013-10-23 09:53:14 +03003369 seg->qpn_mkey7_0 = cpu_to_be32(0xffffff00 |
Haggai Eran968e78d2014-12-11 17:04:11 +02003370 mlx5_mkey_variant(umrwr->mkey));
Eli Cohene126ba92013-07-07 17:25:49 +03003371}
3372
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003373static void set_reg_data_seg(struct mlx5_wqe_data_seg *dseg,
3374 struct mlx5_ib_mr *mr,
3375 struct mlx5_ib_pd *pd)
3376{
3377 int bcount = mr->desc_size * mr->ndescs;
3378
3379 dseg->addr = cpu_to_be64(mr->desc_map);
3380 dseg->byte_count = cpu_to_be32(ALIGN(bcount, 64));
3381 dseg->lkey = cpu_to_be32(pd->ibpd.local_dma_lkey);
3382}
3383
Eli Cohene126ba92013-07-07 17:25:49 +03003384static __be32 send_ieth(struct ib_send_wr *wr)
3385{
3386 switch (wr->opcode) {
3387 case IB_WR_SEND_WITH_IMM:
3388 case IB_WR_RDMA_WRITE_WITH_IMM:
3389 return wr->ex.imm_data;
3390
3391 case IB_WR_SEND_WITH_INV:
3392 return cpu_to_be32(wr->ex.invalidate_rkey);
3393
3394 default:
3395 return 0;
3396 }
3397}
3398
3399static u8 calc_sig(void *wqe, int size)
3400{
3401 u8 *p = wqe;
3402 u8 res = 0;
3403 int i;
3404
3405 for (i = 0; i < size; i++)
3406 res ^= p[i];
3407
3408 return ~res;
3409}
3410
3411static u8 wq_sig(void *wqe)
3412{
3413 return calc_sig(wqe, (*((u8 *)wqe + 8) & 0x3f) << 4);
3414}
3415
3416static int set_data_inl_seg(struct mlx5_ib_qp *qp, struct ib_send_wr *wr,
3417 void *wqe, int *sz)
3418{
3419 struct mlx5_wqe_inline_seg *seg;
3420 void *qend = qp->sq.qend;
3421 void *addr;
3422 int inl = 0;
3423 int copy;
3424 int len;
3425 int i;
3426
3427 seg = wqe;
3428 wqe += sizeof(*seg);
3429 for (i = 0; i < wr->num_sge; i++) {
3430 addr = (void *)(unsigned long)(wr->sg_list[i].addr);
3431 len = wr->sg_list[i].length;
3432 inl += len;
3433
3434 if (unlikely(inl > qp->max_inline_data))
3435 return -ENOMEM;
3436
3437 if (unlikely(wqe + len > qend)) {
3438 copy = qend - wqe;
3439 memcpy(wqe, addr, copy);
3440 addr += copy;
3441 len -= copy;
3442 wqe = mlx5_get_send_wqe(qp, 0);
3443 }
3444 memcpy(wqe, addr, len);
3445 wqe += len;
3446 }
3447
3448 seg->byte_count = cpu_to_be32(inl | MLX5_INLINE_SEG);
3449
3450 *sz = ALIGN(inl + sizeof(seg->byte_count), 16) / 16;
3451
3452 return 0;
3453}
3454
Sagi Grimberge6631812014-02-23 14:19:11 +02003455static u16 prot_field_size(enum ib_signature_type type)
3456{
3457 switch (type) {
3458 case IB_SIG_TYPE_T10_DIF:
3459 return MLX5_DIF_SIZE;
3460 default:
3461 return 0;
3462 }
3463}
3464
3465static u8 bs_selector(int block_size)
3466{
3467 switch (block_size) {
3468 case 512: return 0x1;
3469 case 520: return 0x2;
3470 case 4096: return 0x3;
3471 case 4160: return 0x4;
3472 case 1073741824: return 0x5;
3473 default: return 0;
3474 }
3475}
3476
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003477static void mlx5_fill_inl_bsf(struct ib_sig_domain *domain,
3478 struct mlx5_bsf_inl *inl)
Sagi Grimberge6631812014-02-23 14:19:11 +02003479{
Sagi Grimberg142537f2014-08-13 19:54:32 +03003480 /* Valid inline section and allow BSF refresh */
3481 inl->vld_refresh = cpu_to_be16(MLX5_BSF_INL_VALID |
3482 MLX5_BSF_REFRESH_DIF);
3483 inl->dif_apptag = cpu_to_be16(domain->sig.dif.app_tag);
3484 inl->dif_reftag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003485 /* repeating block */
3486 inl->rp_inv_seed = MLX5_BSF_REPEAT_BLOCK;
3487 inl->sig_type = domain->sig.dif.bg_type == IB_T10DIF_CRC ?
3488 MLX5_DIF_CRC : MLX5_DIF_IPCS;
Sagi Grimberge6631812014-02-23 14:19:11 +02003489
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003490 if (domain->sig.dif.ref_remap)
3491 inl->dif_inc_ref_guard_check |= MLX5_BSF_INC_REFTAG;
Sagi Grimberge6631812014-02-23 14:19:11 +02003492
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003493 if (domain->sig.dif.app_escape) {
3494 if (domain->sig.dif.ref_escape)
3495 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPREF_ESCAPE;
3496 else
3497 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPTAG_ESCAPE;
Sagi Grimberge6631812014-02-23 14:19:11 +02003498 }
3499
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003500 inl->dif_app_bitmask_check =
3501 cpu_to_be16(domain->sig.dif.apptag_check_mask);
Sagi Grimberge6631812014-02-23 14:19:11 +02003502}
3503
3504static int mlx5_set_bsf(struct ib_mr *sig_mr,
3505 struct ib_sig_attrs *sig_attrs,
3506 struct mlx5_bsf *bsf, u32 data_size)
3507{
3508 struct mlx5_core_sig_ctx *msig = to_mmr(sig_mr)->sig;
3509 struct mlx5_bsf_basic *basic = &bsf->basic;
3510 struct ib_sig_domain *mem = &sig_attrs->mem;
3511 struct ib_sig_domain *wire = &sig_attrs->wire;
Sagi Grimberge6631812014-02-23 14:19:11 +02003512
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003513 memset(bsf, 0, sizeof(*bsf));
Sagi Grimberge6631812014-02-23 14:19:11 +02003514
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003515 /* Basic + Extended + Inline */
3516 basic->bsf_size_sbs = 1 << 7;
3517 /* Input domain check byte mask */
3518 basic->check_byte_mask = sig_attrs->check_mask;
3519 basic->raw_data_size = cpu_to_be32(data_size);
3520
3521 /* Memory domain */
3522 switch (sig_attrs->mem.sig_type) {
3523 case IB_SIG_TYPE_NONE:
3524 break;
3525 case IB_SIG_TYPE_T10_DIF:
3526 basic->mem.bs_selector = bs_selector(mem->sig.dif.pi_interval);
3527 basic->m_bfs_psv = cpu_to_be32(msig->psv_memory.psv_idx);
3528 mlx5_fill_inl_bsf(mem, &bsf->m_inl);
3529 break;
3530 default:
3531 return -EINVAL;
3532 }
3533
3534 /* Wire domain */
3535 switch (sig_attrs->wire.sig_type) {
3536 case IB_SIG_TYPE_NONE:
3537 break;
3538 case IB_SIG_TYPE_T10_DIF:
Sagi Grimberge6631812014-02-23 14:19:11 +02003539 if (mem->sig.dif.pi_interval == wire->sig.dif.pi_interval &&
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003540 mem->sig_type == wire->sig_type) {
Sagi Grimberge6631812014-02-23 14:19:11 +02003541 /* Same block structure */
Sagi Grimberg142537f2014-08-13 19:54:32 +03003542 basic->bsf_size_sbs |= 1 << 4;
Sagi Grimberge6631812014-02-23 14:19:11 +02003543 if (mem->sig.dif.bg_type == wire->sig.dif.bg_type)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003544 basic->wire.copy_byte_mask |= MLX5_CPY_GRD_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003545 if (mem->sig.dif.app_tag == wire->sig.dif.app_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003546 basic->wire.copy_byte_mask |= MLX5_CPY_APP_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003547 if (mem->sig.dif.ref_tag == wire->sig.dif.ref_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003548 basic->wire.copy_byte_mask |= MLX5_CPY_REF_MASK;
Sagi Grimberge6631812014-02-23 14:19:11 +02003549 } else
3550 basic->wire.bs_selector = bs_selector(wire->sig.dif.pi_interval);
3551
Sagi Grimberg142537f2014-08-13 19:54:32 +03003552 basic->w_bfs_psv = cpu_to_be32(msig->psv_wire.psv_idx);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003553 mlx5_fill_inl_bsf(wire, &bsf->w_inl);
Sagi Grimberge6631812014-02-23 14:19:11 +02003554 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003555 default:
3556 return -EINVAL;
3557 }
3558
3559 return 0;
3560}
3561
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003562static int set_sig_data_segment(struct ib_sig_handover_wr *wr,
3563 struct mlx5_ib_qp *qp, void **seg, int *size)
Sagi Grimberge6631812014-02-23 14:19:11 +02003564{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003565 struct ib_sig_attrs *sig_attrs = wr->sig_attrs;
3566 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003567 struct mlx5_bsf *bsf;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003568 u32 data_len = wr->wr.sg_list->length;
3569 u32 data_key = wr->wr.sg_list->lkey;
3570 u64 data_va = wr->wr.sg_list->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003571 int ret;
3572 int wqe_size;
3573
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003574 if (!wr->prot ||
3575 (data_key == wr->prot->lkey &&
3576 data_va == wr->prot->addr &&
3577 data_len == wr->prot->length)) {
Sagi Grimberge6631812014-02-23 14:19:11 +02003578 /**
3579 * Source domain doesn't contain signature information
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003580 * or data and protection are interleaved in memory.
Sagi Grimberge6631812014-02-23 14:19:11 +02003581 * So need construct:
3582 * ------------------
3583 * | data_klm |
3584 * ------------------
3585 * | BSF |
3586 * ------------------
3587 **/
3588 struct mlx5_klm *data_klm = *seg;
3589
3590 data_klm->bcount = cpu_to_be32(data_len);
3591 data_klm->key = cpu_to_be32(data_key);
3592 data_klm->va = cpu_to_be64(data_va);
3593 wqe_size = ALIGN(sizeof(*data_klm), 64);
3594 } else {
3595 /**
3596 * Source domain contains signature information
3597 * So need construct a strided block format:
3598 * ---------------------------
3599 * | stride_block_ctrl |
3600 * ---------------------------
3601 * | data_klm |
3602 * ---------------------------
3603 * | prot_klm |
3604 * ---------------------------
3605 * | BSF |
3606 * ---------------------------
3607 **/
3608 struct mlx5_stride_block_ctrl_seg *sblock_ctrl;
3609 struct mlx5_stride_block_entry *data_sentry;
3610 struct mlx5_stride_block_entry *prot_sentry;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003611 u32 prot_key = wr->prot->lkey;
3612 u64 prot_va = wr->prot->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003613 u16 block_size = sig_attrs->mem.sig.dif.pi_interval;
3614 int prot_size;
3615
3616 sblock_ctrl = *seg;
3617 data_sentry = (void *)sblock_ctrl + sizeof(*sblock_ctrl);
3618 prot_sentry = (void *)data_sentry + sizeof(*data_sentry);
3619
3620 prot_size = prot_field_size(sig_attrs->mem.sig_type);
3621 if (!prot_size) {
3622 pr_err("Bad block size given: %u\n", block_size);
3623 return -EINVAL;
3624 }
3625 sblock_ctrl->bcount_per_cycle = cpu_to_be32(block_size +
3626 prot_size);
3627 sblock_ctrl->op = cpu_to_be32(MLX5_STRIDE_BLOCK_OP);
3628 sblock_ctrl->repeat_count = cpu_to_be32(data_len / block_size);
3629 sblock_ctrl->num_entries = cpu_to_be16(2);
3630
3631 data_sentry->bcount = cpu_to_be16(block_size);
3632 data_sentry->key = cpu_to_be32(data_key);
3633 data_sentry->va = cpu_to_be64(data_va);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003634 data_sentry->stride = cpu_to_be16(block_size);
3635
Sagi Grimberge6631812014-02-23 14:19:11 +02003636 prot_sentry->bcount = cpu_to_be16(prot_size);
3637 prot_sentry->key = cpu_to_be32(prot_key);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003638 prot_sentry->va = cpu_to_be64(prot_va);
3639 prot_sentry->stride = cpu_to_be16(prot_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02003640
Sagi Grimberge6631812014-02-23 14:19:11 +02003641 wqe_size = ALIGN(sizeof(*sblock_ctrl) + sizeof(*data_sentry) +
3642 sizeof(*prot_sentry), 64);
3643 }
3644
3645 *seg += wqe_size;
3646 *size += wqe_size / 16;
3647 if (unlikely((*seg == qp->sq.qend)))
3648 *seg = mlx5_get_send_wqe(qp, 0);
3649
3650 bsf = *seg;
3651 ret = mlx5_set_bsf(sig_mr, sig_attrs, bsf, data_len);
3652 if (ret)
3653 return -EINVAL;
3654
3655 *seg += sizeof(*bsf);
3656 *size += sizeof(*bsf) / 16;
3657 if (unlikely((*seg == qp->sq.qend)))
3658 *seg = mlx5_get_send_wqe(qp, 0);
3659
3660 return 0;
3661}
3662
3663static void set_sig_mkey_segment(struct mlx5_mkey_seg *seg,
Artemy Kovalyov31616252017-01-02 11:37:42 +02003664 struct ib_sig_handover_wr *wr, u32 size,
Sagi Grimberge6631812014-02-23 14:19:11 +02003665 u32 length, u32 pdn)
3666{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003667 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003668 u32 sig_key = sig_mr->rkey;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003669 u8 sigerr = to_mmr(sig_mr)->sig->sigerr_count & 1;
Sagi Grimberge6631812014-02-23 14:19:11 +02003670
3671 memset(seg, 0, sizeof(*seg));
3672
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003673 seg->flags = get_umr_flags(wr->access_flags) |
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003674 MLX5_MKC_ACCESS_MODE_KLMS;
Sagi Grimberge6631812014-02-23 14:19:11 +02003675 seg->qpn_mkey7_0 = cpu_to_be32((sig_key & 0xff) | 0xffffff00);
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003676 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL | sigerr << 26 |
Sagi Grimberge6631812014-02-23 14:19:11 +02003677 MLX5_MKEY_BSF_EN | pdn);
3678 seg->len = cpu_to_be64(length);
Artemy Kovalyov31616252017-01-02 11:37:42 +02003679 seg->xlt_oct_size = cpu_to_be32(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02003680 seg->bsfs_octo_size = cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE);
3681}
3682
3683static void set_sig_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02003684 u32 size)
Sagi Grimberge6631812014-02-23 14:19:11 +02003685{
3686 memset(umr, 0, sizeof(*umr));
3687
3688 umr->flags = MLX5_FLAGS_INLINE | MLX5_FLAGS_CHECK_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02003689 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02003690 umr->bsf_octowords = cpu_to_be16(MLX5_MKEY_BSF_OCTO_SIZE);
3691 umr->mkey_mask = sig_mkey_mask();
3692}
3693
3694
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003695static int set_sig_umr_wr(struct ib_send_wr *send_wr, struct mlx5_ib_qp *qp,
Sagi Grimberge6631812014-02-23 14:19:11 +02003696 void **seg, int *size)
3697{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003698 struct ib_sig_handover_wr *wr = sig_handover_wr(send_wr);
3699 struct mlx5_ib_mr *sig_mr = to_mmr(wr->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02003700 u32 pdn = get_pd(qp)->pdn;
Artemy Kovalyov31616252017-01-02 11:37:42 +02003701 u32 xlt_size;
Sagi Grimberge6631812014-02-23 14:19:11 +02003702 int region_len, ret;
3703
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003704 if (unlikely(wr->wr.num_sge != 1) ||
3705 unlikely(wr->access_flags & IB_ACCESS_REMOTE_ATOMIC) ||
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003706 unlikely(!sig_mr->sig) || unlikely(!qp->signature_en) ||
3707 unlikely(!sig_mr->sig->sig_status_checked))
Sagi Grimberge6631812014-02-23 14:19:11 +02003708 return -EINVAL;
3709
3710 /* length of the protected region, data + protection */
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003711 region_len = wr->wr.sg_list->length;
3712 if (wr->prot &&
3713 (wr->prot->lkey != wr->wr.sg_list->lkey ||
3714 wr->prot->addr != wr->wr.sg_list->addr ||
3715 wr->prot->length != wr->wr.sg_list->length))
3716 region_len += wr->prot->length;
Sagi Grimberge6631812014-02-23 14:19:11 +02003717
3718 /**
3719 * KLM octoword size - if protection was provided
3720 * then we use strided block format (3 octowords),
3721 * else we use single KLM (1 octoword)
3722 **/
Artemy Kovalyov31616252017-01-02 11:37:42 +02003723 xlt_size = wr->prot ? 0x30 : sizeof(struct mlx5_klm);
Sagi Grimberge6631812014-02-23 14:19:11 +02003724
Artemy Kovalyov31616252017-01-02 11:37:42 +02003725 set_sig_umr_segment(*seg, xlt_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02003726 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
3727 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
3728 if (unlikely((*seg == qp->sq.qend)))
3729 *seg = mlx5_get_send_wqe(qp, 0);
3730
Artemy Kovalyov31616252017-01-02 11:37:42 +02003731 set_sig_mkey_segment(*seg, wr, xlt_size, region_len, pdn);
Sagi Grimberge6631812014-02-23 14:19:11 +02003732 *seg += sizeof(struct mlx5_mkey_seg);
3733 *size += sizeof(struct mlx5_mkey_seg) / 16;
3734 if (unlikely((*seg == qp->sq.qend)))
3735 *seg = mlx5_get_send_wqe(qp, 0);
3736
3737 ret = set_sig_data_segment(wr, qp, seg, size);
3738 if (ret)
3739 return ret;
3740
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003741 sig_mr->sig->sig_status_checked = false;
Sagi Grimberge6631812014-02-23 14:19:11 +02003742 return 0;
3743}
3744
3745static int set_psv_wr(struct ib_sig_domain *domain,
3746 u32 psv_idx, void **seg, int *size)
3747{
3748 struct mlx5_seg_set_psv *psv_seg = *seg;
3749
3750 memset(psv_seg, 0, sizeof(*psv_seg));
3751 psv_seg->psv_num = cpu_to_be32(psv_idx);
3752 switch (domain->sig_type) {
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003753 case IB_SIG_TYPE_NONE:
3754 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003755 case IB_SIG_TYPE_T10_DIF:
3756 psv_seg->transient_sig = cpu_to_be32(domain->sig.dif.bg << 16 |
3757 domain->sig.dif.app_tag);
3758 psv_seg->ref_tag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberge6631812014-02-23 14:19:11 +02003759 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003760 default:
Leon Romanovsky12bbf1e2017-01-18 14:10:31 +02003761 pr_err("Bad signature type (%d) is given.\n",
3762 domain->sig_type);
3763 return -EINVAL;
Sagi Grimberge6631812014-02-23 14:19:11 +02003764 }
3765
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003766 *seg += sizeof(*psv_seg);
3767 *size += sizeof(*psv_seg) / 16;
3768
Sagi Grimberge6631812014-02-23 14:19:11 +02003769 return 0;
3770}
3771
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003772static int set_reg_wr(struct mlx5_ib_qp *qp,
3773 struct ib_reg_wr *wr,
3774 void **seg, int *size)
3775{
3776 struct mlx5_ib_mr *mr = to_mmr(wr->mr);
3777 struct mlx5_ib_pd *pd = to_mpd(qp->ibqp.pd);
3778
3779 if (unlikely(wr->wr.send_flags & IB_SEND_INLINE)) {
3780 mlx5_ib_warn(to_mdev(qp->ibqp.device),
3781 "Invalid IB_SEND_INLINE send flag\n");
3782 return -EINVAL;
3783 }
3784
3785 set_reg_umr_seg(*seg, mr);
3786 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
3787 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
3788 if (unlikely((*seg == qp->sq.qend)))
3789 *seg = mlx5_get_send_wqe(qp, 0);
3790
3791 set_reg_mkey_seg(*seg, mr, wr->key, wr->access);
3792 *seg += sizeof(struct mlx5_mkey_seg);
3793 *size += sizeof(struct mlx5_mkey_seg) / 16;
3794 if (unlikely((*seg == qp->sq.qend)))
3795 *seg = mlx5_get_send_wqe(qp, 0);
3796
3797 set_reg_data_seg(*seg, mr, pd);
3798 *seg += sizeof(struct mlx5_wqe_data_seg);
3799 *size += (sizeof(struct mlx5_wqe_data_seg) / 16);
3800
3801 return 0;
3802}
3803
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003804static void set_linv_wr(struct mlx5_ib_qp *qp, void **seg, int *size)
Eli Cohene126ba92013-07-07 17:25:49 +03003805{
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003806 set_linv_umr_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03003807 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
3808 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
3809 if (unlikely((*seg == qp->sq.qend)))
3810 *seg = mlx5_get_send_wqe(qp, 0);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003811 set_linv_mkey_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03003812 *seg += sizeof(struct mlx5_mkey_seg);
3813 *size += sizeof(struct mlx5_mkey_seg) / 16;
3814 if (unlikely((*seg == qp->sq.qend)))
3815 *seg = mlx5_get_send_wqe(qp, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03003816}
3817
3818static void dump_wqe(struct mlx5_ib_qp *qp, int idx, int size_16)
3819{
3820 __be32 *p = NULL;
3821 int tidx = idx;
3822 int i, j;
3823
3824 pr_debug("dump wqe at %p\n", mlx5_get_send_wqe(qp, tidx));
3825 for (i = 0, j = 0; i < size_16 * 4; i += 4, j += 4) {
3826 if ((i & 0xf) == 0) {
3827 void *buf = mlx5_get_send_wqe(qp, tidx);
3828 tidx = (tidx + 1) & (qp->sq.wqe_cnt - 1);
3829 p = buf;
3830 j = 0;
3831 }
3832 pr_debug("%08x %08x %08x %08x\n", be32_to_cpu(p[j]),
3833 be32_to_cpu(p[j + 1]), be32_to_cpu(p[j + 2]),
3834 be32_to_cpu(p[j + 3]));
3835 }
3836}
3837
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003838static int begin_wqe(struct mlx5_ib_qp *qp, void **seg,
3839 struct mlx5_wqe_ctrl_seg **ctrl,
Eli Cohen6a4f1392014-12-02 12:26:18 +02003840 struct ib_send_wr *wr, unsigned *idx,
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003841 int *size, int nreq)
3842{
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03003843 if (unlikely(mlx5_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)))
3844 return -ENOMEM;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003845
3846 *idx = qp->sq.cur_post & (qp->sq.wqe_cnt - 1);
3847 *seg = mlx5_get_send_wqe(qp, *idx);
3848 *ctrl = *seg;
3849 *(uint32_t *)(*seg + 8) = 0;
3850 (*ctrl)->imm = send_ieth(wr);
3851 (*ctrl)->fm_ce_se = qp->sq_signal_bits |
3852 (wr->send_flags & IB_SEND_SIGNALED ?
3853 MLX5_WQE_CTRL_CQ_UPDATE : 0) |
3854 (wr->send_flags & IB_SEND_SOLICITED ?
3855 MLX5_WQE_CTRL_SOLICITED : 0);
3856
3857 *seg += sizeof(**ctrl);
3858 *size = sizeof(**ctrl) / 16;
3859
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03003860 return 0;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003861}
3862
3863static void finish_wqe(struct mlx5_ib_qp *qp,
3864 struct mlx5_wqe_ctrl_seg *ctrl,
3865 u8 size, unsigned idx, u64 wr_id,
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003866 int nreq, u8 fence, u32 mlx5_opcode)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003867{
3868 u8 opmod = 0;
3869
3870 ctrl->opmod_idx_opcode = cpu_to_be32(((u32)(qp->sq.cur_post) << 8) |
3871 mlx5_opcode | ((u32)opmod << 24));
majd@mellanox.com19098df2016-01-14 19:13:03 +02003872 ctrl->qpn_ds = cpu_to_be32(size | (qp->trans_qp.base.mqp.qpn << 8));
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003873 ctrl->fm_ce_se |= fence;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003874 if (unlikely(qp->wq_sig))
3875 ctrl->signature = wq_sig(ctrl);
3876
3877 qp->sq.wrid[idx] = wr_id;
3878 qp->sq.w_list[idx].opcode = mlx5_opcode;
3879 qp->sq.wqe_head[idx] = qp->sq.head + nreq;
3880 qp->sq.cur_post += DIV_ROUND_UP(size * 16, MLX5_SEND_WQE_BB);
3881 qp->sq.w_list[idx].next = qp->sq.cur_post;
3882}
3883
3884
Eli Cohene126ba92013-07-07 17:25:49 +03003885int mlx5_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
3886 struct ib_send_wr **bad_wr)
3887{
3888 struct mlx5_wqe_ctrl_seg *ctrl = NULL; /* compiler warning */
3889 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003890 struct mlx5_core_dev *mdev = dev->mdev;
Haggai Erand16e91d2016-02-29 15:45:05 +02003891 struct mlx5_ib_qp *qp;
Sagi Grimberge6631812014-02-23 14:19:11 +02003892 struct mlx5_ib_mr *mr;
Eli Cohene126ba92013-07-07 17:25:49 +03003893 struct mlx5_wqe_data_seg *dpseg;
3894 struct mlx5_wqe_xrc_seg *xrc;
Haggai Erand16e91d2016-02-29 15:45:05 +02003895 struct mlx5_bf *bf;
Eli Cohene126ba92013-07-07 17:25:49 +03003896 int uninitialized_var(size);
Haggai Erand16e91d2016-02-29 15:45:05 +02003897 void *qend;
Eli Cohene126ba92013-07-07 17:25:49 +03003898 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03003899 unsigned idx;
3900 int err = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003901 int num_sge;
3902 void *seg;
3903 int nreq;
3904 int i;
3905 u8 next_fence = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003906 u8 fence;
3907
Haggai Erand16e91d2016-02-29 15:45:05 +02003908 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
3909 return mlx5_ib_gsi_post_send(ibqp, wr, bad_wr);
3910
3911 qp = to_mqp(ibqp);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003912 bf = &qp->bf;
Haggai Erand16e91d2016-02-29 15:45:05 +02003913 qend = qp->sq.qend;
3914
Eli Cohene126ba92013-07-07 17:25:49 +03003915 spin_lock_irqsave(&qp->sq.lock, flags);
3916
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003917 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
3918 err = -EIO;
3919 *bad_wr = wr;
3920 nreq = 0;
3921 goto out;
3922 }
3923
Eli Cohene126ba92013-07-07 17:25:49 +03003924 for (nreq = 0; wr; nreq++, wr = wr->next) {
Fabian Fredericka8f731e2014-08-12 19:20:08 -04003925 if (unlikely(wr->opcode >= ARRAY_SIZE(mlx5_ib_opcode))) {
Eli Cohene126ba92013-07-07 17:25:49 +03003926 mlx5_ib_warn(dev, "\n");
3927 err = -EINVAL;
3928 *bad_wr = wr;
3929 goto out;
3930 }
3931
Eli Cohene126ba92013-07-07 17:25:49 +03003932 num_sge = wr->num_sge;
3933 if (unlikely(num_sge > qp->sq.max_gs)) {
3934 mlx5_ib_warn(dev, "\n");
Chuck Lever24be4092016-08-28 10:58:34 +03003935 err = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03003936 *bad_wr = wr;
3937 goto out;
3938 }
3939
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003940 err = begin_wqe(qp, &seg, &ctrl, wr, &idx, &size, nreq);
3941 if (err) {
3942 mlx5_ib_warn(dev, "\n");
3943 err = -ENOMEM;
3944 *bad_wr = wr;
3945 goto out;
3946 }
Eli Cohene126ba92013-07-07 17:25:49 +03003947
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003948 if (wr->opcode == IB_WR_LOCAL_INV ||
3949 wr->opcode == IB_WR_REG_MR) {
3950 fence = dev->umr_fence;
3951 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
3952 } else if (wr->send_flags & IB_SEND_FENCE) {
3953 if (qp->next_fence)
3954 fence = MLX5_FENCE_MODE_SMALL_AND_FENCE;
3955 else
3956 fence = MLX5_FENCE_MODE_FENCE;
3957 } else {
3958 fence = qp->next_fence;
3959 }
3960
Eli Cohene126ba92013-07-07 17:25:49 +03003961 switch (ibqp->qp_type) {
3962 case IB_QPT_XRC_INI:
3963 xrc = seg;
Eli Cohene126ba92013-07-07 17:25:49 +03003964 seg += sizeof(*xrc);
3965 size += sizeof(*xrc) / 16;
3966 /* fall through */
3967 case IB_QPT_RC:
3968 switch (wr->opcode) {
3969 case IB_WR_RDMA_READ:
3970 case IB_WR_RDMA_WRITE:
3971 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003972 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
3973 rdma_wr(wr)->rkey);
Jack Morgensteinf241e742014-07-28 23:30:23 +03003974 seg += sizeof(struct mlx5_wqe_raddr_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03003975 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
3976 break;
3977
3978 case IB_WR_ATOMIC_CMP_AND_SWP:
3979 case IB_WR_ATOMIC_FETCH_AND_ADD:
Eli Cohene126ba92013-07-07 17:25:49 +03003980 case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
Eli Cohen81bea282013-09-11 16:35:30 +03003981 mlx5_ib_warn(dev, "Atomic operations are not supported yet\n");
3982 err = -ENOSYS;
3983 *bad_wr = wr;
3984 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03003985
3986 case IB_WR_LOCAL_INV:
Eli Cohene126ba92013-07-07 17:25:49 +03003987 qp->sq.wr_data[idx] = IB_WR_LOCAL_INV;
3988 ctrl->imm = cpu_to_be32(wr->ex.invalidate_rkey);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003989 set_linv_wr(qp, &seg, &size);
Eli Cohene126ba92013-07-07 17:25:49 +03003990 num_sge = 0;
3991 break;
3992
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003993 case IB_WR_REG_MR:
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003994 qp->sq.wr_data[idx] = IB_WR_REG_MR;
3995 ctrl->imm = cpu_to_be32(reg_wr(wr)->key);
3996 err = set_reg_wr(qp, reg_wr(wr), &seg, &size);
3997 if (err) {
3998 *bad_wr = wr;
3999 goto out;
4000 }
4001 num_sge = 0;
4002 break;
4003
Sagi Grimberge6631812014-02-23 14:19:11 +02004004 case IB_WR_REG_SIG_MR:
4005 qp->sq.wr_data[idx] = IB_WR_REG_SIG_MR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004006 mr = to_mmr(sig_handover_wr(wr)->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02004007
4008 ctrl->imm = cpu_to_be32(mr->ibmr.rkey);
4009 err = set_sig_umr_wr(wr, qp, &seg, &size);
4010 if (err) {
4011 mlx5_ib_warn(dev, "\n");
4012 *bad_wr = wr;
4013 goto out;
4014 }
4015
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004016 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
4017 fence, MLX5_OPCODE_UMR);
Sagi Grimberge6631812014-02-23 14:19:11 +02004018 /*
4019 * SET_PSV WQEs are not signaled and solicited
4020 * on error
4021 */
4022 wr->send_flags &= ~IB_SEND_SIGNALED;
4023 wr->send_flags |= IB_SEND_SOLICITED;
4024 err = begin_wqe(qp, &seg, &ctrl, wr,
4025 &idx, &size, nreq);
4026 if (err) {
4027 mlx5_ib_warn(dev, "\n");
4028 err = -ENOMEM;
4029 *bad_wr = wr;
4030 goto out;
4031 }
4032
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004033 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->mem,
Sagi Grimberge6631812014-02-23 14:19:11 +02004034 mr->sig->psv_memory.psv_idx, &seg,
4035 &size);
4036 if (err) {
4037 mlx5_ib_warn(dev, "\n");
4038 *bad_wr = wr;
4039 goto out;
4040 }
4041
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004042 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
4043 fence, MLX5_OPCODE_SET_PSV);
Sagi Grimberge6631812014-02-23 14:19:11 +02004044 err = begin_wqe(qp, &seg, &ctrl, wr,
4045 &idx, &size, nreq);
4046 if (err) {
4047 mlx5_ib_warn(dev, "\n");
4048 err = -ENOMEM;
4049 *bad_wr = wr;
4050 goto out;
4051 }
4052
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004053 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->wire,
Sagi Grimberge6631812014-02-23 14:19:11 +02004054 mr->sig->psv_wire.psv_idx, &seg,
4055 &size);
4056 if (err) {
4057 mlx5_ib_warn(dev, "\n");
4058 *bad_wr = wr;
4059 goto out;
4060 }
4061
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004062 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
4063 fence, MLX5_OPCODE_SET_PSV);
4064 qp->next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
Sagi Grimberge6631812014-02-23 14:19:11 +02004065 num_sge = 0;
4066 goto skip_psv;
4067
Eli Cohene126ba92013-07-07 17:25:49 +03004068 default:
4069 break;
4070 }
4071 break;
4072
4073 case IB_QPT_UC:
4074 switch (wr->opcode) {
4075 case IB_WR_RDMA_WRITE:
4076 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004077 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
4078 rdma_wr(wr)->rkey);
Eli Cohene126ba92013-07-07 17:25:49 +03004079 seg += sizeof(struct mlx5_wqe_raddr_seg);
4080 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
4081 break;
4082
4083 default:
4084 break;
4085 }
4086 break;
4087
Eli Cohene126ba92013-07-07 17:25:49 +03004088 case IB_QPT_SMI:
Maor Gottlieb1e0e50b2017-01-18 14:10:34 +02004089 if (unlikely(!mdev->port_caps[qp->port - 1].has_smi)) {
4090 mlx5_ib_warn(dev, "Send SMP MADs is not allowed\n");
4091 err = -EPERM;
4092 *bad_wr = wr;
4093 goto out;
4094 }
Bart Van Asschef6b1ee32017-10-11 10:49:07 -07004095 /* fall through */
Haggai Erand16e91d2016-02-29 15:45:05 +02004096 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03004097 set_datagram_seg(seg, wr);
Jack Morgensteinf241e742014-07-28 23:30:23 +03004098 seg += sizeof(struct mlx5_wqe_datagram_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004099 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
4100 if (unlikely((seg == qend)))
4101 seg = mlx5_get_send_wqe(qp, 0);
4102 break;
Erez Shitritf0313962016-02-21 16:27:17 +02004103 case IB_QPT_UD:
4104 set_datagram_seg(seg, wr);
4105 seg += sizeof(struct mlx5_wqe_datagram_seg);
4106 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Eli Cohene126ba92013-07-07 17:25:49 +03004107
Erez Shitritf0313962016-02-21 16:27:17 +02004108 if (unlikely((seg == qend)))
4109 seg = mlx5_get_send_wqe(qp, 0);
4110
4111 /* handle qp that supports ud offload */
4112 if (qp->flags & IB_QP_CREATE_IPOIB_UD_LSO) {
4113 struct mlx5_wqe_eth_pad *pad;
4114
4115 pad = seg;
4116 memset(pad, 0, sizeof(struct mlx5_wqe_eth_pad));
4117 seg += sizeof(struct mlx5_wqe_eth_pad);
4118 size += sizeof(struct mlx5_wqe_eth_pad) / 16;
4119
4120 seg = set_eth_seg(seg, wr, qend, qp, &size);
4121
4122 if (unlikely((seg == qend)))
4123 seg = mlx5_get_send_wqe(qp, 0);
4124 }
4125 break;
Eli Cohene126ba92013-07-07 17:25:49 +03004126 case MLX5_IB_QPT_REG_UMR:
4127 if (wr->opcode != MLX5_IB_WR_UMR) {
4128 err = -EINVAL;
4129 mlx5_ib_warn(dev, "bad opcode\n");
4130 goto out;
4131 }
4132 qp->sq.wr_data[idx] = MLX5_IB_WR_UMR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004133 ctrl->imm = cpu_to_be32(umr_wr(wr)->mkey);
Maor Gottlieb578e7262016-10-27 16:36:37 +03004134 set_reg_umr_segment(seg, wr, !!(MLX5_CAP_GEN(mdev, atomic)));
Eli Cohene126ba92013-07-07 17:25:49 +03004135 seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4136 size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4137 if (unlikely((seg == qend)))
4138 seg = mlx5_get_send_wqe(qp, 0);
4139 set_reg_mkey_segment(seg, wr);
4140 seg += sizeof(struct mlx5_mkey_seg);
4141 size += sizeof(struct mlx5_mkey_seg) / 16;
4142 if (unlikely((seg == qend)))
4143 seg = mlx5_get_send_wqe(qp, 0);
4144 break;
4145
4146 default:
4147 break;
4148 }
4149
4150 if (wr->send_flags & IB_SEND_INLINE && num_sge) {
4151 int uninitialized_var(sz);
4152
4153 err = set_data_inl_seg(qp, wr, seg, &sz);
4154 if (unlikely(err)) {
4155 mlx5_ib_warn(dev, "\n");
4156 *bad_wr = wr;
4157 goto out;
4158 }
Eli Cohene126ba92013-07-07 17:25:49 +03004159 size += sz;
4160 } else {
4161 dpseg = seg;
4162 for (i = 0; i < num_sge; i++) {
4163 if (unlikely(dpseg == qend)) {
4164 seg = mlx5_get_send_wqe(qp, 0);
4165 dpseg = seg;
4166 }
4167 if (likely(wr->sg_list[i].length)) {
4168 set_data_ptr_seg(dpseg, wr->sg_list + i);
4169 size += sizeof(struct mlx5_wqe_data_seg) / 16;
4170 dpseg++;
4171 }
4172 }
4173 }
4174
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004175 qp->next_fence = next_fence;
4176 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq, fence,
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004177 mlx5_ib_opcode[wr->opcode]);
Sagi Grimberge6631812014-02-23 14:19:11 +02004178skip_psv:
Eli Cohene126ba92013-07-07 17:25:49 +03004179 if (0)
4180 dump_wqe(qp, idx, size);
4181 }
4182
4183out:
4184 if (likely(nreq)) {
4185 qp->sq.head += nreq;
4186
4187 /* Make sure that descriptors are written before
4188 * updating doorbell record and ringing the doorbell
4189 */
4190 wmb();
4191
4192 qp->db.db[MLX5_SND_DBR] = cpu_to_be32(qp->sq.cur_post);
4193
Eli Cohenada388f2014-01-14 17:45:16 +02004194 /* Make sure doorbell record is visible to the HCA before
4195 * we hit doorbell */
4196 wmb();
4197
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004198 /* currently we support only regular doorbells */
4199 mlx5_write64((__be32 *)ctrl, bf->bfreg->map + bf->offset, NULL);
4200 /* Make sure doorbells don't leak out of SQ spinlock
4201 * and reach the HCA out of order.
4202 */
4203 mmiowb();
Eli Cohene126ba92013-07-07 17:25:49 +03004204 bf->offset ^= bf->buf_size;
Eli Cohene126ba92013-07-07 17:25:49 +03004205 }
4206
4207 spin_unlock_irqrestore(&qp->sq.lock, flags);
4208
4209 return err;
4210}
4211
4212static void set_sig_seg(struct mlx5_rwqe_sig *sig, int size)
4213{
4214 sig->signature = calc_sig(sig, size);
4215}
4216
4217int mlx5_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
4218 struct ib_recv_wr **bad_wr)
4219{
4220 struct mlx5_ib_qp *qp = to_mqp(ibqp);
4221 struct mlx5_wqe_data_seg *scat;
4222 struct mlx5_rwqe_sig *sig;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004223 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4224 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004225 unsigned long flags;
4226 int err = 0;
4227 int nreq;
4228 int ind;
4229 int i;
4230
Haggai Erand16e91d2016-02-29 15:45:05 +02004231 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4232 return mlx5_ib_gsi_post_recv(ibqp, wr, bad_wr);
4233
Eli Cohene126ba92013-07-07 17:25:49 +03004234 spin_lock_irqsave(&qp->rq.lock, flags);
4235
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004236 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
4237 err = -EIO;
4238 *bad_wr = wr;
4239 nreq = 0;
4240 goto out;
4241 }
4242
Eli Cohene126ba92013-07-07 17:25:49 +03004243 ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
4244
4245 for (nreq = 0; wr; nreq++, wr = wr->next) {
4246 if (mlx5_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
4247 err = -ENOMEM;
4248 *bad_wr = wr;
4249 goto out;
4250 }
4251
4252 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
4253 err = -EINVAL;
4254 *bad_wr = wr;
4255 goto out;
4256 }
4257
4258 scat = get_recv_wqe(qp, ind);
4259 if (qp->wq_sig)
4260 scat++;
4261
4262 for (i = 0; i < wr->num_sge; i++)
4263 set_data_ptr_seg(scat + i, wr->sg_list + i);
4264
4265 if (i < qp->rq.max_gs) {
4266 scat[i].byte_count = 0;
4267 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
4268 scat[i].addr = 0;
4269 }
4270
4271 if (qp->wq_sig) {
4272 sig = (struct mlx5_rwqe_sig *)scat;
4273 set_sig_seg(sig, (qp->rq.max_gs + 1) << 2);
4274 }
4275
4276 qp->rq.wrid[ind] = wr->wr_id;
4277
4278 ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
4279 }
4280
4281out:
4282 if (likely(nreq)) {
4283 qp->rq.head += nreq;
4284
4285 /* Make sure that descriptors are written before
4286 * doorbell record.
4287 */
4288 wmb();
4289
4290 *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
4291 }
4292
4293 spin_unlock_irqrestore(&qp->rq.lock, flags);
4294
4295 return err;
4296}
4297
4298static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
4299{
4300 switch (mlx5_state) {
4301 case MLX5_QP_STATE_RST: return IB_QPS_RESET;
4302 case MLX5_QP_STATE_INIT: return IB_QPS_INIT;
4303 case MLX5_QP_STATE_RTR: return IB_QPS_RTR;
4304 case MLX5_QP_STATE_RTS: return IB_QPS_RTS;
4305 case MLX5_QP_STATE_SQ_DRAINING:
4306 case MLX5_QP_STATE_SQD: return IB_QPS_SQD;
4307 case MLX5_QP_STATE_SQER: return IB_QPS_SQE;
4308 case MLX5_QP_STATE_ERR: return IB_QPS_ERR;
4309 default: return -1;
4310 }
4311}
4312
4313static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
4314{
4315 switch (mlx5_mig_state) {
4316 case MLX5_QP_PM_ARMED: return IB_MIG_ARMED;
4317 case MLX5_QP_PM_REARM: return IB_MIG_REARM;
4318 case MLX5_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
4319 default: return -1;
4320 }
4321}
4322
4323static int to_ib_qp_access_flags(int mlx5_flags)
4324{
4325 int ib_flags = 0;
4326
4327 if (mlx5_flags & MLX5_QP_BIT_RRE)
4328 ib_flags |= IB_ACCESS_REMOTE_READ;
4329 if (mlx5_flags & MLX5_QP_BIT_RWE)
4330 ib_flags |= IB_ACCESS_REMOTE_WRITE;
4331 if (mlx5_flags & MLX5_QP_BIT_RAE)
4332 ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
4333
4334 return ib_flags;
4335}
4336
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04004337static void to_rdma_ah_attr(struct mlx5_ib_dev *ibdev,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004338 struct rdma_ah_attr *ah_attr,
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04004339 struct mlx5_qp_path *path)
Eli Cohene126ba92013-07-07 17:25:49 +03004340{
Jack Morgenstein9603b612014-07-28 23:30:22 +03004341 struct mlx5_core_dev *dev = ibdev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004342
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004343 memset(ah_attr, 0, sizeof(*ah_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03004344
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04004345 ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, path->port);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004346 rdma_ah_set_port_num(ah_attr, path->port);
4347 if (rdma_ah_get_port_num(ah_attr) == 0 ||
4348 rdma_ah_get_port_num(ah_attr) > MLX5_CAP_GEN(dev, num_ports))
Eli Cohene126ba92013-07-07 17:25:49 +03004349 return;
4350
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004351 rdma_ah_set_port_num(ah_attr, path->port);
4352 rdma_ah_set_sl(ah_attr, path->dci_cfi_prio_sl & 0xf);
Eli Cohene126ba92013-07-07 17:25:49 +03004353
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004354 rdma_ah_set_dlid(ah_attr, be16_to_cpu(path->rlid));
4355 rdma_ah_set_path_bits(ah_attr, path->grh_mlid & 0x7f);
4356 rdma_ah_set_static_rate(ah_attr,
4357 path->static_rate ? path->static_rate - 5 : 0);
4358 if (path->grh_mlid & (1 << 7)) {
4359 u32 tc_fl = be32_to_cpu(path->tclass_flowlabel);
4360
4361 rdma_ah_set_grh(ah_attr, NULL,
4362 tc_fl & 0xfffff,
4363 path->mgid_index,
4364 path->hop_limit,
4365 (tc_fl >> 20) & 0xff);
4366 rdma_ah_set_dgid_raw(ah_attr, path->rgid);
Eli Cohene126ba92013-07-07 17:25:49 +03004367 }
4368}
4369
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004370static int query_raw_packet_qp_sq_state(struct mlx5_ib_dev *dev,
4371 struct mlx5_ib_sq *sq,
4372 u8 *sq_state)
Eli Cohene126ba92013-07-07 17:25:49 +03004373{
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004374 void *out;
4375 void *sqc;
4376 int inlen;
4377 int err;
4378
4379 inlen = MLX5_ST_SZ_BYTES(query_sq_out);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004380 out = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004381 if (!out)
4382 return -ENOMEM;
4383
4384 err = mlx5_core_query_sq(dev->mdev, sq->base.mqp.qpn, out);
4385 if (err)
4386 goto out;
4387
4388 sqc = MLX5_ADDR_OF(query_sq_out, out, sq_context);
4389 *sq_state = MLX5_GET(sqc, sqc, state);
4390 sq->state = *sq_state;
4391
4392out:
4393 kvfree(out);
4394 return err;
4395}
4396
4397static int query_raw_packet_qp_rq_state(struct mlx5_ib_dev *dev,
4398 struct mlx5_ib_rq *rq,
4399 u8 *rq_state)
4400{
4401 void *out;
4402 void *rqc;
4403 int inlen;
4404 int err;
4405
4406 inlen = MLX5_ST_SZ_BYTES(query_rq_out);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004407 out = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004408 if (!out)
4409 return -ENOMEM;
4410
4411 err = mlx5_core_query_rq(dev->mdev, rq->base.mqp.qpn, out);
4412 if (err)
4413 goto out;
4414
4415 rqc = MLX5_ADDR_OF(query_rq_out, out, rq_context);
4416 *rq_state = MLX5_GET(rqc, rqc, state);
4417 rq->state = *rq_state;
4418
4419out:
4420 kvfree(out);
4421 return err;
4422}
4423
4424static int sqrq_state_to_qp_state(u8 sq_state, u8 rq_state,
4425 struct mlx5_ib_qp *qp, u8 *qp_state)
4426{
4427 static const u8 sqrq_trans[MLX5_RQ_NUM_STATE][MLX5_SQ_NUM_STATE] = {
4428 [MLX5_RQC_STATE_RST] = {
4429 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
4430 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
4431 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE_BAD,
4432 [MLX5_SQ_STATE_NA] = IB_QPS_RESET,
4433 },
4434 [MLX5_RQC_STATE_RDY] = {
4435 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
4436 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
4437 [MLX5_SQC_STATE_ERR] = IB_QPS_SQE,
4438 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE,
4439 },
4440 [MLX5_RQC_STATE_ERR] = {
4441 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
4442 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
4443 [MLX5_SQC_STATE_ERR] = IB_QPS_ERR,
4444 [MLX5_SQ_STATE_NA] = IB_QPS_ERR,
4445 },
4446 [MLX5_RQ_STATE_NA] = {
4447 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
4448 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
4449 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE,
4450 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE_BAD,
4451 },
4452 };
4453
4454 *qp_state = sqrq_trans[rq_state][sq_state];
4455
4456 if (*qp_state == MLX5_QP_STATE_BAD) {
4457 WARN(1, "Buggy Raw Packet QP state, SQ 0x%x state: 0x%x, RQ 0x%x state: 0x%x",
4458 qp->raw_packet_qp.sq.base.mqp.qpn, sq_state,
4459 qp->raw_packet_qp.rq.base.mqp.qpn, rq_state);
4460 return -EINVAL;
4461 }
4462
4463 if (*qp_state == MLX5_QP_STATE)
4464 *qp_state = qp->state;
4465
4466 return 0;
4467}
4468
4469static int query_raw_packet_qp_state(struct mlx5_ib_dev *dev,
4470 struct mlx5_ib_qp *qp,
4471 u8 *raw_packet_qp_state)
4472{
4473 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
4474 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
4475 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
4476 int err;
4477 u8 sq_state = MLX5_SQ_STATE_NA;
4478 u8 rq_state = MLX5_RQ_STATE_NA;
4479
4480 if (qp->sq.wqe_cnt) {
4481 err = query_raw_packet_qp_sq_state(dev, sq, &sq_state);
4482 if (err)
4483 return err;
4484 }
4485
4486 if (qp->rq.wqe_cnt) {
4487 err = query_raw_packet_qp_rq_state(dev, rq, &rq_state);
4488 if (err)
4489 return err;
4490 }
4491
4492 return sqrq_state_to_qp_state(sq_state, rq_state, qp,
4493 raw_packet_qp_state);
4494}
4495
4496static int query_qp_attr(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
4497 struct ib_qp_attr *qp_attr)
4498{
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004499 int outlen = MLX5_ST_SZ_BYTES(query_qp_out);
Eli Cohene126ba92013-07-07 17:25:49 +03004500 struct mlx5_qp_context *context;
4501 int mlx5_state;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004502 u32 *outb;
Eli Cohene126ba92013-07-07 17:25:49 +03004503 int err = 0;
4504
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004505 outb = kzalloc(outlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004506 if (!outb)
4507 return -ENOMEM;
4508
majd@mellanox.com19098df2016-01-14 19:13:03 +02004509 err = mlx5_core_qp_query(dev->mdev, &qp->trans_qp.base.mqp, outb,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004510 outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03004511 if (err)
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004512 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004513
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004514 /* FIXME: use MLX5_GET rather than mlx5_qp_context manual struct */
4515 context = (struct mlx5_qp_context *)MLX5_ADDR_OF(query_qp_out, outb, qpc);
4516
Eli Cohene126ba92013-07-07 17:25:49 +03004517 mlx5_state = be32_to_cpu(context->flags) >> 28;
4518
4519 qp->state = to_ib_qp_state(mlx5_state);
Eli Cohene126ba92013-07-07 17:25:49 +03004520 qp_attr->path_mtu = context->mtu_msgmax >> 5;
4521 qp_attr->path_mig_state =
4522 to_ib_mig_state((be32_to_cpu(context->flags) >> 11) & 0x3);
4523 qp_attr->qkey = be32_to_cpu(context->qkey);
4524 qp_attr->rq_psn = be32_to_cpu(context->rnr_nextrecvpsn) & 0xffffff;
4525 qp_attr->sq_psn = be32_to_cpu(context->next_send_psn) & 0xffffff;
4526 qp_attr->dest_qp_num = be32_to_cpu(context->log_pg_sz_remote_qpn) & 0xffffff;
4527 qp_attr->qp_access_flags =
4528 to_ib_qp_access_flags(be32_to_cpu(context->params2));
4529
4530 if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04004531 to_rdma_ah_attr(dev, &qp_attr->ah_attr, &context->pri_path);
4532 to_rdma_ah_attr(dev, &qp_attr->alt_ah_attr, &context->alt_path);
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03004533 qp_attr->alt_pkey_index =
4534 be16_to_cpu(context->alt_path.pkey_index);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004535 qp_attr->alt_port_num =
4536 rdma_ah_get_port_num(&qp_attr->alt_ah_attr);
Eli Cohene126ba92013-07-07 17:25:49 +03004537 }
4538
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03004539 qp_attr->pkey_index = be16_to_cpu(context->pri_path.pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03004540 qp_attr->port_num = context->pri_path.port;
4541
4542 /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
4543 qp_attr->sq_draining = mlx5_state == MLX5_QP_STATE_SQ_DRAINING;
4544
4545 qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context->params1) >> 21) & 0x7);
4546
4547 qp_attr->max_dest_rd_atomic =
4548 1 << ((be32_to_cpu(context->params2) >> 21) & 0x7);
4549 qp_attr->min_rnr_timer =
4550 (be32_to_cpu(context->rnr_nextrecvpsn) >> 24) & 0x1f;
4551 qp_attr->timeout = context->pri_path.ackto_lt >> 3;
4552 qp_attr->retry_cnt = (be32_to_cpu(context->params1) >> 16) & 0x7;
4553 qp_attr->rnr_retry = (be32_to_cpu(context->params1) >> 13) & 0x7;
4554 qp_attr->alt_timeout = context->alt_path.ackto_lt >> 3;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004555
4556out:
4557 kfree(outb);
4558 return err;
4559}
4560
4561int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
4562 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
4563{
4564 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4565 struct mlx5_ib_qp *qp = to_mqp(ibqp);
4566 int err = 0;
4567 u8 raw_packet_qp_state;
4568
Yishai Hadas28d61372016-05-23 15:20:56 +03004569 if (ibqp->rwq_ind_tbl)
4570 return -ENOSYS;
4571
Haggai Erand16e91d2016-02-29 15:45:05 +02004572 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4573 return mlx5_ib_gsi_query_qp(ibqp, qp_attr, qp_attr_mask,
4574 qp_init_attr);
4575
Yishai Hadasc2e53b22017-06-08 16:15:08 +03004576 /* Not all of output fields are applicable, make sure to zero them */
4577 memset(qp_init_attr, 0, sizeof(*qp_init_attr));
4578 memset(qp_attr, 0, sizeof(*qp_attr));
4579
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004580 mutex_lock(&qp->mutex);
4581
Yishai Hadasc2e53b22017-06-08 16:15:08 +03004582 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
4583 qp->flags & MLX5_IB_QP_UNDERLAY) {
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004584 err = query_raw_packet_qp_state(dev, qp, &raw_packet_qp_state);
4585 if (err)
4586 goto out;
4587 qp->state = raw_packet_qp_state;
4588 qp_attr->port_num = 1;
4589 } else {
4590 err = query_qp_attr(dev, qp, qp_attr);
4591 if (err)
4592 goto out;
4593 }
4594
4595 qp_attr->qp_state = qp->state;
Eli Cohene126ba92013-07-07 17:25:49 +03004596 qp_attr->cur_qp_state = qp_attr->qp_state;
4597 qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
4598 qp_attr->cap.max_recv_sge = qp->rq.max_gs;
4599
4600 if (!ibqp->uobject) {
Noa Osherovich0540d812016-06-04 15:15:32 +03004601 qp_attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +03004602 qp_attr->cap.max_send_sge = qp->sq.max_gs;
Noa Osherovich0540d812016-06-04 15:15:32 +03004603 qp_init_attr->qp_context = ibqp->qp_context;
Eli Cohene126ba92013-07-07 17:25:49 +03004604 } else {
4605 qp_attr->cap.max_send_wr = 0;
4606 qp_attr->cap.max_send_sge = 0;
4607 }
4608
Noa Osherovich0540d812016-06-04 15:15:32 +03004609 qp_init_attr->qp_type = ibqp->qp_type;
4610 qp_init_attr->recv_cq = ibqp->recv_cq;
4611 qp_init_attr->send_cq = ibqp->send_cq;
4612 qp_init_attr->srq = ibqp->srq;
4613 qp_attr->cap.max_inline_data = qp->max_inline_data;
Eli Cohene126ba92013-07-07 17:25:49 +03004614
4615 qp_init_attr->cap = qp_attr->cap;
4616
4617 qp_init_attr->create_flags = 0;
4618 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
4619 qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
4620
Leon Romanovsky051f2632015-12-20 12:16:11 +02004621 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
4622 qp_init_attr->create_flags |= IB_QP_CREATE_CROSS_CHANNEL;
4623 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
4624 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_SEND;
4625 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
4626 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_RECV;
Haggai Eranb11a4f92016-02-29 15:45:03 +02004627 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
4628 qp_init_attr->create_flags |= mlx5_ib_create_qp_sqpn_qp1();
Leon Romanovsky051f2632015-12-20 12:16:11 +02004629
Eli Cohene126ba92013-07-07 17:25:49 +03004630 qp_init_attr->sq_sig_type = qp->sq_signal_bits & MLX5_WQE_CTRL_CQ_UPDATE ?
4631 IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
4632
Eli Cohene126ba92013-07-07 17:25:49 +03004633out:
4634 mutex_unlock(&qp->mutex);
4635 return err;
4636}
4637
4638struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
4639 struct ib_ucontext *context,
4640 struct ib_udata *udata)
4641{
4642 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4643 struct mlx5_ib_xrcd *xrcd;
4644 int err;
4645
Saeed Mahameed938fe832015-05-28 22:28:41 +03004646 if (!MLX5_CAP_GEN(dev->mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +03004647 return ERR_PTR(-ENOSYS);
4648
4649 xrcd = kmalloc(sizeof(*xrcd), GFP_KERNEL);
4650 if (!xrcd)
4651 return ERR_PTR(-ENOMEM);
4652
Jack Morgenstein9603b612014-07-28 23:30:22 +03004653 err = mlx5_core_xrcd_alloc(dev->mdev, &xrcd->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03004654 if (err) {
4655 kfree(xrcd);
4656 return ERR_PTR(-ENOMEM);
4657 }
4658
4659 return &xrcd->ibxrcd;
4660}
4661
4662int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
4663{
4664 struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
4665 u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
4666 int err;
4667
Jack Morgenstein9603b612014-07-28 23:30:22 +03004668 err = mlx5_core_xrcd_dealloc(dev->mdev, xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03004669 if (err) {
4670 mlx5_ib_warn(dev, "failed to dealloc xrcdn 0x%x\n", xrcdn);
4671 return err;
4672 }
4673
4674 kfree(xrcd);
4675
4676 return 0;
4677}
Yishai Hadas79b20a62016-05-23 15:20:50 +03004678
Yishai Hadas350d0e42016-08-28 14:58:18 +03004679static void mlx5_ib_wq_event(struct mlx5_core_qp *core_qp, int type)
4680{
4681 struct mlx5_ib_rwq *rwq = to_mibrwq(core_qp);
4682 struct mlx5_ib_dev *dev = to_mdev(rwq->ibwq.device);
4683 struct ib_event event;
4684
4685 if (rwq->ibwq.event_handler) {
4686 event.device = rwq->ibwq.device;
4687 event.element.wq = &rwq->ibwq;
4688 switch (type) {
4689 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
4690 event.event = IB_EVENT_WQ_FATAL;
4691 break;
4692 default:
4693 mlx5_ib_warn(dev, "Unexpected event type %d on WQ %06x\n", type, core_qp->qpn);
4694 return;
4695 }
4696
4697 rwq->ibwq.event_handler(&event, rwq->ibwq.wq_context);
4698 }
4699}
4700
Maor Gottlieb03404e82017-05-30 10:29:13 +03004701static int set_delay_drop(struct mlx5_ib_dev *dev)
4702{
4703 int err = 0;
4704
4705 mutex_lock(&dev->delay_drop.lock);
4706 if (dev->delay_drop.activate)
4707 goto out;
4708
4709 err = mlx5_core_set_delay_drop(dev->mdev, dev->delay_drop.timeout);
4710 if (err)
4711 goto out;
4712
4713 dev->delay_drop.activate = true;
4714out:
4715 mutex_unlock(&dev->delay_drop.lock);
Maor Gottliebfe248c32017-05-30 10:29:14 +03004716
4717 if (!err)
4718 atomic_inc(&dev->delay_drop.rqs_cnt);
Maor Gottlieb03404e82017-05-30 10:29:13 +03004719 return err;
4720}
4721
Yishai Hadas79b20a62016-05-23 15:20:50 +03004722static int create_rq(struct mlx5_ib_rwq *rwq, struct ib_pd *pd,
4723 struct ib_wq_init_attr *init_attr)
4724{
4725 struct mlx5_ib_dev *dev;
Noa Osherovich4be6da12017-01-18 15:40:04 +02004726 int has_net_offloads;
Yishai Hadas79b20a62016-05-23 15:20:50 +03004727 __be64 *rq_pas0;
4728 void *in;
4729 void *rqc;
4730 void *wq;
4731 int inlen;
4732 int err;
4733
4734 dev = to_mdev(pd->device);
4735
4736 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + sizeof(u64) * rwq->rq_num_pas;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004737 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004738 if (!in)
4739 return -ENOMEM;
4740
4741 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
4742 MLX5_SET(rqc, rqc, mem_rq_type,
4743 MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
4744 MLX5_SET(rqc, rqc, user_index, rwq->user_index);
4745 MLX5_SET(rqc, rqc, cqn, to_mcq(init_attr->cq)->mcq.cqn);
4746 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
4747 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
4748 wq = MLX5_ADDR_OF(rqc, rqc, wq);
Noa Osherovichccc87082017-10-17 18:01:13 +03004749 MLX5_SET(wq, wq, wq_type,
4750 rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ ?
4751 MLX5_WQ_TYPE_CYCLIC_STRIDING_RQ : MLX5_WQ_TYPE_CYCLIC);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004752 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
4753 MLX5_SET(wq, wq, log_wq_stride, rwq->log_rq_stride);
Noa Osherovichccc87082017-10-17 18:01:13 +03004754 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ) {
4755 MLX5_SET(wq, wq, two_byte_shift_en, rwq->two_byte_shift_en);
4756 MLX5_SET(wq, wq, log_wqe_stride_size,
4757 rwq->single_stride_log_num_of_bytes -
4758 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES);
4759 MLX5_SET(wq, wq, log_wqe_num_of_strides, rwq->log_num_strides -
4760 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES);
4761 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03004762 MLX5_SET(wq, wq, log_wq_sz, rwq->log_rq_size);
4763 MLX5_SET(wq, wq, pd, to_mpd(pd)->pdn);
4764 MLX5_SET(wq, wq, page_offset, rwq->rq_page_offset);
4765 MLX5_SET(wq, wq, log_wq_pg_sz, rwq->log_page_size);
4766 MLX5_SET(wq, wq, wq_signature, rwq->wq_sig);
4767 MLX5_SET64(wq, wq, dbr_addr, rwq->db.dma);
Noa Osherovich4be6da12017-01-18 15:40:04 +02004768 has_net_offloads = MLX5_CAP_GEN(dev->mdev, eth_net_offloads);
Noa Osherovichb1f74a82017-01-18 15:40:02 +02004769 if (init_attr->create_flags & IB_WQ_FLAGS_CVLAN_STRIPPING) {
Noa Osherovich4be6da12017-01-18 15:40:04 +02004770 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
Noa Osherovichb1f74a82017-01-18 15:40:02 +02004771 mlx5_ib_dbg(dev, "VLAN offloads are not supported\n");
4772 err = -EOPNOTSUPP;
4773 goto out;
4774 }
4775 } else {
4776 MLX5_SET(rqc, rqc, vsd, 1);
4777 }
Noa Osherovich4be6da12017-01-18 15:40:04 +02004778 if (init_attr->create_flags & IB_WQ_FLAGS_SCATTER_FCS) {
4779 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, scatter_fcs))) {
4780 mlx5_ib_dbg(dev, "Scatter FCS is not supported\n");
4781 err = -EOPNOTSUPP;
4782 goto out;
4783 }
4784 MLX5_SET(rqc, rqc, scatter_fcs, 1);
4785 }
Maor Gottlieb03404e82017-05-30 10:29:13 +03004786 if (init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
4787 if (!(dev->ib_dev.attrs.raw_packet_caps &
4788 IB_RAW_PACKET_CAP_DELAY_DROP)) {
4789 mlx5_ib_dbg(dev, "Delay drop is not supported\n");
4790 err = -EOPNOTSUPP;
4791 goto out;
4792 }
4793 MLX5_SET(rqc, rqc, delay_drop_en, 1);
4794 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03004795 rq_pas0 = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
4796 mlx5_ib_populate_pas(dev, rwq->umem, rwq->page_shift, rq_pas0, 0);
Yishai Hadas350d0e42016-08-28 14:58:18 +03004797 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rwq->core_qp);
Maor Gottlieb03404e82017-05-30 10:29:13 +03004798 if (!err && init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
4799 err = set_delay_drop(dev);
4800 if (err) {
4801 mlx5_ib_warn(dev, "Failed to enable delay drop err=%d\n",
4802 err);
4803 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
4804 } else {
4805 rwq->create_flags |= MLX5_IB_WQ_FLAGS_DELAY_DROP;
4806 }
4807 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02004808out:
Yishai Hadas79b20a62016-05-23 15:20:50 +03004809 kvfree(in);
4810 return err;
4811}
4812
4813static int set_user_rq_size(struct mlx5_ib_dev *dev,
4814 struct ib_wq_init_attr *wq_init_attr,
4815 struct mlx5_ib_create_wq *ucmd,
4816 struct mlx5_ib_rwq *rwq)
4817{
4818 /* Sanity check RQ size before proceeding */
4819 if (wq_init_attr->max_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_wq_sz)))
4820 return -EINVAL;
4821
4822 if (!ucmd->rq_wqe_count)
4823 return -EINVAL;
4824
4825 rwq->wqe_count = ucmd->rq_wqe_count;
4826 rwq->wqe_shift = ucmd->rq_wqe_shift;
4827 rwq->buf_size = (rwq->wqe_count << rwq->wqe_shift);
4828 rwq->log_rq_stride = rwq->wqe_shift;
4829 rwq->log_rq_size = ilog2(rwq->wqe_count);
4830 return 0;
4831}
4832
4833static int prepare_user_rq(struct ib_pd *pd,
4834 struct ib_wq_init_attr *init_attr,
4835 struct ib_udata *udata,
4836 struct mlx5_ib_rwq *rwq)
4837{
4838 struct mlx5_ib_dev *dev = to_mdev(pd->device);
4839 struct mlx5_ib_create_wq ucmd = {};
4840 int err;
4841 size_t required_cmd_sz;
4842
Noa Osherovichccc87082017-10-17 18:01:13 +03004843 required_cmd_sz = offsetof(typeof(ucmd), single_stride_log_num_of_bytes)
4844 + sizeof(ucmd.single_stride_log_num_of_bytes);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004845 if (udata->inlen < required_cmd_sz) {
4846 mlx5_ib_dbg(dev, "invalid inlen\n");
4847 return -EINVAL;
4848 }
4849
4850 if (udata->inlen > sizeof(ucmd) &&
4851 !ib_is_udata_cleared(udata, sizeof(ucmd),
4852 udata->inlen - sizeof(ucmd))) {
4853 mlx5_ib_dbg(dev, "inlen is not supported\n");
4854 return -EOPNOTSUPP;
4855 }
4856
4857 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
4858 mlx5_ib_dbg(dev, "copy failed\n");
4859 return -EFAULT;
4860 }
4861
Noa Osherovichccc87082017-10-17 18:01:13 +03004862 if (ucmd.comp_mask & (~MLX5_IB_CREATE_WQ_STRIDING_RQ)) {
Yishai Hadas79b20a62016-05-23 15:20:50 +03004863 mlx5_ib_dbg(dev, "invalid comp mask\n");
4864 return -EOPNOTSUPP;
Noa Osherovichccc87082017-10-17 18:01:13 +03004865 } else if (ucmd.comp_mask & MLX5_IB_CREATE_WQ_STRIDING_RQ) {
4866 if (!MLX5_CAP_GEN(dev->mdev, striding_rq)) {
4867 mlx5_ib_dbg(dev, "Striding RQ is not supported\n");
4868 return -EOPNOTSUPP;
4869 }
4870 if ((ucmd.single_stride_log_num_of_bytes <
4871 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES) ||
4872 (ucmd.single_stride_log_num_of_bytes >
4873 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES)) {
4874 mlx5_ib_dbg(dev, "Invalid log stride size (%u. Range is %u - %u)\n",
4875 ucmd.single_stride_log_num_of_bytes,
4876 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES,
4877 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES);
4878 return -EINVAL;
4879 }
4880 if ((ucmd.single_wqe_log_num_of_strides >
4881 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES) ||
4882 (ucmd.single_wqe_log_num_of_strides <
4883 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES)) {
4884 mlx5_ib_dbg(dev, "Invalid log num strides (%u. Range is %u - %u)\n",
4885 ucmd.single_wqe_log_num_of_strides,
4886 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES,
4887 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES);
4888 return -EINVAL;
4889 }
4890 rwq->single_stride_log_num_of_bytes =
4891 ucmd.single_stride_log_num_of_bytes;
4892 rwq->log_num_strides = ucmd.single_wqe_log_num_of_strides;
4893 rwq->two_byte_shift_en = !!ucmd.two_byte_shift_en;
4894 rwq->create_flags |= MLX5_IB_WQ_FLAGS_STRIDING_RQ;
Yishai Hadas79b20a62016-05-23 15:20:50 +03004895 }
4896
4897 err = set_user_rq_size(dev, init_attr, &ucmd, rwq);
4898 if (err) {
4899 mlx5_ib_dbg(dev, "err %d\n", err);
4900 return err;
4901 }
4902
4903 err = create_user_rq(dev, pd, rwq, &ucmd);
4904 if (err) {
4905 mlx5_ib_dbg(dev, "err %d\n", err);
4906 if (err)
4907 return err;
4908 }
4909
4910 rwq->user_index = ucmd.user_index;
4911 return 0;
4912}
4913
4914struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
4915 struct ib_wq_init_attr *init_attr,
4916 struct ib_udata *udata)
4917{
4918 struct mlx5_ib_dev *dev;
4919 struct mlx5_ib_rwq *rwq;
4920 struct mlx5_ib_create_wq_resp resp = {};
4921 size_t min_resp_len;
4922 int err;
4923
4924 if (!udata)
4925 return ERR_PTR(-ENOSYS);
4926
4927 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
4928 if (udata->outlen && udata->outlen < min_resp_len)
4929 return ERR_PTR(-EINVAL);
4930
4931 dev = to_mdev(pd->device);
4932 switch (init_attr->wq_type) {
4933 case IB_WQT_RQ:
4934 rwq = kzalloc(sizeof(*rwq), GFP_KERNEL);
4935 if (!rwq)
4936 return ERR_PTR(-ENOMEM);
4937 err = prepare_user_rq(pd, init_attr, udata, rwq);
4938 if (err)
4939 goto err;
4940 err = create_rq(rwq, pd, init_attr);
4941 if (err)
4942 goto err_user_rq;
4943 break;
4944 default:
4945 mlx5_ib_dbg(dev, "unsupported wq type %d\n",
4946 init_attr->wq_type);
4947 return ERR_PTR(-EINVAL);
4948 }
4949
Yishai Hadas350d0e42016-08-28 14:58:18 +03004950 rwq->ibwq.wq_num = rwq->core_qp.qpn;
Yishai Hadas79b20a62016-05-23 15:20:50 +03004951 rwq->ibwq.state = IB_WQS_RESET;
4952 if (udata->outlen) {
4953 resp.response_length = offsetof(typeof(resp), response_length) +
4954 sizeof(resp.response_length);
4955 err = ib_copy_to_udata(udata, &resp, resp.response_length);
4956 if (err)
4957 goto err_copy;
4958 }
4959
Yishai Hadas350d0e42016-08-28 14:58:18 +03004960 rwq->core_qp.event = mlx5_ib_wq_event;
4961 rwq->ibwq.event_handler = init_attr->event_handler;
Yishai Hadas79b20a62016-05-23 15:20:50 +03004962 return &rwq->ibwq;
4963
4964err_copy:
Yishai Hadas350d0e42016-08-28 14:58:18 +03004965 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004966err_user_rq:
Maor Gottliebfe248c32017-05-30 10:29:14 +03004967 destroy_user_rq(dev, pd, rwq);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004968err:
4969 kfree(rwq);
4970 return ERR_PTR(err);
4971}
4972
4973int mlx5_ib_destroy_wq(struct ib_wq *wq)
4974{
4975 struct mlx5_ib_dev *dev = to_mdev(wq->device);
4976 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
4977
Yishai Hadas350d0e42016-08-28 14:58:18 +03004978 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Maor Gottliebfe248c32017-05-30 10:29:14 +03004979 destroy_user_rq(dev, wq->pd, rwq);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004980 kfree(rwq);
4981
4982 return 0;
4983}
4984
Yishai Hadasc5f90922016-05-23 15:20:53 +03004985struct ib_rwq_ind_table *mlx5_ib_create_rwq_ind_table(struct ib_device *device,
4986 struct ib_rwq_ind_table_init_attr *init_attr,
4987 struct ib_udata *udata)
4988{
4989 struct mlx5_ib_dev *dev = to_mdev(device);
4990 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl;
4991 int sz = 1 << init_attr->log_ind_tbl_size;
4992 struct mlx5_ib_create_rwq_ind_tbl_resp resp = {};
4993 size_t min_resp_len;
4994 int inlen;
4995 int err;
4996 int i;
4997 u32 *in;
4998 void *rqtc;
4999
5000 if (udata->inlen > 0 &&
5001 !ib_is_udata_cleared(udata, 0,
5002 udata->inlen))
5003 return ERR_PTR(-EOPNOTSUPP);
5004
Maor Gottliebefd7f402016-10-27 16:36:40 +03005005 if (init_attr->log_ind_tbl_size >
5006 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size)) {
5007 mlx5_ib_dbg(dev, "log_ind_tbl_size = %d is bigger than supported = %d\n",
5008 init_attr->log_ind_tbl_size,
5009 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size));
5010 return ERR_PTR(-EINVAL);
5011 }
5012
Yishai Hadasc5f90922016-05-23 15:20:53 +03005013 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
5014 if (udata->outlen && udata->outlen < min_resp_len)
5015 return ERR_PTR(-EINVAL);
5016
5017 rwq_ind_tbl = kzalloc(sizeof(*rwq_ind_tbl), GFP_KERNEL);
5018 if (!rwq_ind_tbl)
5019 return ERR_PTR(-ENOMEM);
5020
5021 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005022 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadasc5f90922016-05-23 15:20:53 +03005023 if (!in) {
5024 err = -ENOMEM;
5025 goto err;
5026 }
5027
5028 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
5029
5030 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
5031 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
5032
5033 for (i = 0; i < sz; i++)
5034 MLX5_SET(rqtc, rqtc, rq_num[i], init_attr->ind_tbl[i]->wq_num);
5035
5036 err = mlx5_core_create_rqt(dev->mdev, in, inlen, &rwq_ind_tbl->rqtn);
5037 kvfree(in);
5038
5039 if (err)
5040 goto err;
5041
5042 rwq_ind_tbl->ib_rwq_ind_tbl.ind_tbl_num = rwq_ind_tbl->rqtn;
5043 if (udata->outlen) {
5044 resp.response_length = offsetof(typeof(resp), response_length) +
5045 sizeof(resp.response_length);
5046 err = ib_copy_to_udata(udata, &resp, resp.response_length);
5047 if (err)
5048 goto err_copy;
5049 }
5050
5051 return &rwq_ind_tbl->ib_rwq_ind_tbl;
5052
5053err_copy:
5054 mlx5_core_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn);
5055err:
5056 kfree(rwq_ind_tbl);
5057 return ERR_PTR(err);
5058}
5059
5060int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
5061{
5062 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl = to_mrwq_ind_table(ib_rwq_ind_tbl);
5063 struct mlx5_ib_dev *dev = to_mdev(ib_rwq_ind_tbl->device);
5064
5065 mlx5_core_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn);
5066
5067 kfree(rwq_ind_tbl);
5068 return 0;
5069}
5070
Yishai Hadas79b20a62016-05-23 15:20:50 +03005071int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
5072 u32 wq_attr_mask, struct ib_udata *udata)
5073{
5074 struct mlx5_ib_dev *dev = to_mdev(wq->device);
5075 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
5076 struct mlx5_ib_modify_wq ucmd = {};
5077 size_t required_cmd_sz;
5078 int curr_wq_state;
5079 int wq_state;
5080 int inlen;
5081 int err;
5082 void *rqc;
5083 void *in;
5084
5085 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
5086 if (udata->inlen < required_cmd_sz)
5087 return -EINVAL;
5088
5089 if (udata->inlen > sizeof(ucmd) &&
5090 !ib_is_udata_cleared(udata, sizeof(ucmd),
5091 udata->inlen - sizeof(ucmd)))
5092 return -EOPNOTSUPP;
5093
5094 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
5095 return -EFAULT;
5096
5097 if (ucmd.comp_mask || ucmd.reserved)
5098 return -EOPNOTSUPP;
5099
5100 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005101 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005102 if (!in)
5103 return -ENOMEM;
5104
5105 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
5106
5107 curr_wq_state = (wq_attr_mask & IB_WQ_CUR_STATE) ?
5108 wq_attr->curr_wq_state : wq->state;
5109 wq_state = (wq_attr_mask & IB_WQ_STATE) ?
5110 wq_attr->wq_state : curr_wq_state;
5111 if (curr_wq_state == IB_WQS_ERR)
5112 curr_wq_state = MLX5_RQC_STATE_ERR;
5113 if (wq_state == IB_WQS_ERR)
5114 wq_state = MLX5_RQC_STATE_ERR;
5115 MLX5_SET(modify_rq_in, in, rq_state, curr_wq_state);
5116 MLX5_SET(rqc, rqc, state, wq_state);
5117
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005118 if (wq_attr_mask & IB_WQ_FLAGS) {
5119 if (wq_attr->flags_mask & IB_WQ_FLAGS_CVLAN_STRIPPING) {
5120 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
5121 MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
5122 mlx5_ib_dbg(dev, "VLAN offloads are not "
5123 "supported\n");
5124 err = -EOPNOTSUPP;
5125 goto out;
5126 }
5127 MLX5_SET64(modify_rq_in, in, modify_bitmask,
5128 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
5129 MLX5_SET(rqc, rqc, vsd,
5130 (wq_attr->flags & IB_WQ_FLAGS_CVLAN_STRIPPING) ? 0 : 1);
5131 }
5132 }
5133
Majd Dibbiny23a69642017-01-18 15:25:10 +02005134 if (curr_wq_state == IB_WQS_RESET && wq_state == IB_WQS_RDY) {
5135 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
5136 MLX5_SET64(modify_rq_in, in, modify_bitmask,
5137 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Parav Pandite1f24a72017-04-16 07:29:29 +03005138 MLX5_SET(rqc, rqc, counter_set_id,
5139 dev->port->cnts.set_id);
Majd Dibbiny23a69642017-01-18 15:25:10 +02005140 } else
5141 pr_info_once("%s: Receive WQ counters are not supported on current FW\n",
5142 dev->ib_dev.name);
5143 }
5144
Yishai Hadas350d0e42016-08-28 14:58:18 +03005145 err = mlx5_core_modify_rq(dev->mdev, rwq->core_qp.qpn, in, inlen);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005146 if (!err)
5147 rwq->ibwq.state = (wq_state == MLX5_RQC_STATE_ERR) ? IB_WQS_ERR : wq_state;
5148
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005149out:
5150 kvfree(in);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005151 return err;
5152}