blob: a69524fb6032a785438bba3f4a1323a380f4157d [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>
Eli Cohene126ba92013-07-07 17:25:49 +030037#include "mlx5_ib.h"
Eli Cohene126ba92013-07-07 17:25:49 +030038
39/* not supported currently */
40static int wq_signature;
41
42enum {
43 MLX5_IB_ACK_REQ_FREQ = 8,
44};
45
46enum {
47 MLX5_IB_DEFAULT_SCHED_QUEUE = 0x83,
48 MLX5_IB_DEFAULT_QP0_SCHED_QUEUE = 0x3f,
49 MLX5_IB_LINK_TYPE_IB = 0,
50 MLX5_IB_LINK_TYPE_ETH = 1
51};
52
53enum {
54 MLX5_IB_SQ_STRIDE = 6,
55 MLX5_IB_CACHE_LINE_SIZE = 64,
56};
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))) {
Eli Cohenb125a542013-09-11 16:35:22 +0300410 mlx5_ib_dbg(dev, "wqe count(%d) exceeds limits(%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300411 qp->sq.wqe_cnt,
412 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohenb125a542013-09-11 16:35:22 +0300413 return -ENOMEM;
414 }
Eli Cohene126ba92013-07-07 17:25:49 +0300415 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
Eli Cohen288c01b2016-10-27 16:36:45 +0300416 qp->sq.max_gs = get_send_sge(attr, wqe_size);
417 if (qp->sq.max_gs < attr->cap.max_send_sge)
418 return -ENOMEM;
419
420 attr->cap.max_send_sge = qp->sq.max_gs;
Eli Cohenb125a542013-09-11 16:35:22 +0300421 qp->sq.max_post = wq_size / wqe_size;
422 attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +0300423
424 return wq_size;
425}
426
427static int set_user_buf_size(struct mlx5_ib_dev *dev,
428 struct mlx5_ib_qp *qp,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200429 struct mlx5_ib_create_qp *ucmd,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200430 struct mlx5_ib_qp_base *base,
431 struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300432{
433 int desc_sz = 1 << qp->sq.wqe_shift;
434
Saeed Mahameed938fe832015-05-28 22:28:41 +0300435 if (desc_sz > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300436 mlx5_ib_warn(dev, "desc_sz %d, max_sq_desc_sz %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300437 desc_sz, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300438 return -EINVAL;
439 }
440
441 if (ucmd->sq_wqe_count && ((1 << ilog2(ucmd->sq_wqe_count)) != ucmd->sq_wqe_count)) {
442 mlx5_ib_warn(dev, "sq_wqe_count %d, sq_wqe_count %d\n",
443 ucmd->sq_wqe_count, ucmd->sq_wqe_count);
444 return -EINVAL;
445 }
446
447 qp->sq.wqe_cnt = ucmd->sq_wqe_count;
448
Saeed Mahameed938fe832015-05-28 22:28:41 +0300449 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Eli Cohene126ba92013-07-07 17:25:49 +0300450 mlx5_ib_warn(dev, "wqe_cnt %d, max_wqes %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300451 qp->sq.wqe_cnt,
452 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
Eli Cohene126ba92013-07-07 17:25:49 +0300453 return -EINVAL;
454 }
455
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200456 if (attr->qp_type == IB_QPT_RAW_PACKET) {
457 base->ubuffer.buf_size = qp->rq.wqe_cnt << qp->rq.wqe_shift;
458 qp->raw_packet_qp.sq.ubuffer.buf_size = qp->sq.wqe_cnt << 6;
459 } else {
460 base->ubuffer.buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
461 (qp->sq.wqe_cnt << 6);
462 }
Eli Cohene126ba92013-07-07 17:25:49 +0300463
464 return 0;
465}
466
467static int qp_has_rq(struct ib_qp_init_attr *attr)
468{
469 if (attr->qp_type == IB_QPT_XRC_INI ||
470 attr->qp_type == IB_QPT_XRC_TGT || attr->srq ||
471 attr->qp_type == MLX5_IB_QPT_REG_UMR ||
472 !attr->cap.max_recv_wr)
473 return 0;
474
475 return 1;
476}
477
Eli Cohenc1be5232014-01-14 17:45:12 +0200478static int first_med_uuar(void)
479{
480 return 1;
481}
482
483static int next_uuar(int n)
484{
485 n++;
486
487 while (((n % 4) & 2))
488 n++;
489
490 return n;
491}
492
493static int num_med_uuar(struct mlx5_uuar_info *uuari)
494{
495 int n;
496
497 n = uuari->num_uars * MLX5_NON_FP_BF_REGS_PER_PAGE -
498 uuari->num_low_latency_uuars - 1;
499
500 return n >= 0 ? n : 0;
501}
502
503static int max_uuari(struct mlx5_uuar_info *uuari)
504{
505 return uuari->num_uars * 4;
506}
507
508static int first_hi_uuar(struct mlx5_uuar_info *uuari)
509{
510 int med;
511 int i;
512 int t;
513
514 med = num_med_uuar(uuari);
515 for (t = 0, i = first_med_uuar();; i = next_uuar(i)) {
516 t++;
517 if (t == med)
518 return next_uuar(i);
519 }
520
521 return 0;
522}
523
Eli Cohene126ba92013-07-07 17:25:49 +0300524static int alloc_high_class_uuar(struct mlx5_uuar_info *uuari)
525{
Eli Cohene126ba92013-07-07 17:25:49 +0300526 int i;
527
Eli Cohenc1be5232014-01-14 17:45:12 +0200528 for (i = first_hi_uuar(uuari); i < max_uuari(uuari); i = next_uuar(i)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300529 if (!test_bit(i, uuari->bitmap)) {
530 set_bit(i, uuari->bitmap);
531 uuari->count[i]++;
532 return i;
533 }
534 }
535
536 return -ENOMEM;
537}
538
539static int alloc_med_class_uuar(struct mlx5_uuar_info *uuari)
540{
Eli Cohenc1be5232014-01-14 17:45:12 +0200541 int minidx = first_med_uuar();
Eli Cohene126ba92013-07-07 17:25:49 +0300542 int i;
543
Eli Cohenc1be5232014-01-14 17:45:12 +0200544 for (i = first_med_uuar(); i < first_hi_uuar(uuari); i = next_uuar(i)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300545 if (uuari->count[i] < uuari->count[minidx])
546 minidx = i;
547 }
548
549 uuari->count[minidx]++;
550 return minidx;
551}
552
553static int alloc_uuar(struct mlx5_uuar_info *uuari,
554 enum mlx5_ib_latency_class lat)
555{
556 int uuarn = -EINVAL;
557
558 mutex_lock(&uuari->lock);
559 switch (lat) {
560 case MLX5_IB_LATENCY_CLASS_LOW:
561 uuarn = 0;
562 uuari->count[uuarn]++;
563 break;
564
565 case MLX5_IB_LATENCY_CLASS_MEDIUM:
Eli Cohen78c0f982014-01-30 13:49:48 +0200566 if (uuari->ver < 2)
567 uuarn = -ENOMEM;
568 else
569 uuarn = alloc_med_class_uuar(uuari);
Eli Cohene126ba92013-07-07 17:25:49 +0300570 break;
571
572 case MLX5_IB_LATENCY_CLASS_HIGH:
Eli Cohen78c0f982014-01-30 13:49:48 +0200573 if (uuari->ver < 2)
574 uuarn = -ENOMEM;
575 else
576 uuarn = alloc_high_class_uuar(uuari);
Eli Cohene126ba92013-07-07 17:25:49 +0300577 break;
578
579 case MLX5_IB_LATENCY_CLASS_FAST_PATH:
580 uuarn = 2;
581 break;
582 }
583 mutex_unlock(&uuari->lock);
584
585 return uuarn;
586}
587
588static void free_med_class_uuar(struct mlx5_uuar_info *uuari, int uuarn)
589{
590 clear_bit(uuarn, uuari->bitmap);
591 --uuari->count[uuarn];
592}
593
594static void free_high_class_uuar(struct mlx5_uuar_info *uuari, int uuarn)
595{
596 clear_bit(uuarn, uuari->bitmap);
597 --uuari->count[uuarn];
598}
599
600static void free_uuar(struct mlx5_uuar_info *uuari, int uuarn)
601{
602 int nuuars = uuari->num_uars * MLX5_BF_REGS_PER_PAGE;
603 int high_uuar = nuuars - uuari->num_low_latency_uuars;
604
605 mutex_lock(&uuari->lock);
606 if (uuarn == 0) {
607 --uuari->count[uuarn];
608 goto out;
609 }
610
611 if (uuarn < high_uuar) {
612 free_med_class_uuar(uuari, uuarn);
613 goto out;
614 }
615
616 free_high_class_uuar(uuari, uuarn);
617
618out:
619 mutex_unlock(&uuari->lock);
620}
621
622static enum mlx5_qp_state to_mlx5_state(enum ib_qp_state state)
623{
624 switch (state) {
625 case IB_QPS_RESET: return MLX5_QP_STATE_RST;
626 case IB_QPS_INIT: return MLX5_QP_STATE_INIT;
627 case IB_QPS_RTR: return MLX5_QP_STATE_RTR;
628 case IB_QPS_RTS: return MLX5_QP_STATE_RTS;
629 case IB_QPS_SQD: return MLX5_QP_STATE_SQD;
630 case IB_QPS_SQE: return MLX5_QP_STATE_SQER;
631 case IB_QPS_ERR: return MLX5_QP_STATE_ERR;
632 default: return -1;
633 }
634}
635
636static int to_mlx5_st(enum ib_qp_type type)
637{
638 switch (type) {
639 case IB_QPT_RC: return MLX5_QP_ST_RC;
640 case IB_QPT_UC: return MLX5_QP_ST_UC;
641 case IB_QPT_UD: return MLX5_QP_ST_UD;
642 case MLX5_IB_QPT_REG_UMR: return MLX5_QP_ST_REG_UMR;
643 case IB_QPT_XRC_INI:
644 case IB_QPT_XRC_TGT: return MLX5_QP_ST_XRC;
645 case IB_QPT_SMI: return MLX5_QP_ST_QP0;
Haggai Erand16e91d2016-02-29 15:45:05 +0200646 case MLX5_IB_QPT_HW_GSI: return MLX5_QP_ST_QP1;
Eli Cohene126ba92013-07-07 17:25:49 +0300647 case IB_QPT_RAW_IPV6: return MLX5_QP_ST_RAW_IPV6;
Eli Cohene126ba92013-07-07 17:25:49 +0300648 case IB_QPT_RAW_PACKET:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200649 case IB_QPT_RAW_ETHERTYPE: return MLX5_QP_ST_RAW_ETHERTYPE;
Eli Cohene126ba92013-07-07 17:25:49 +0300650 case IB_QPT_MAX:
651 default: return -EINVAL;
652 }
653}
654
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300655static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq,
656 struct mlx5_ib_cq *recv_cq);
657static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq,
658 struct mlx5_ib_cq *recv_cq);
659
Eli Cohene126ba92013-07-07 17:25:49 +0300660static int uuarn_to_uar_index(struct mlx5_uuar_info *uuari, int uuarn)
661{
662 return uuari->uars[uuarn / MLX5_BF_REGS_PER_PAGE].index;
663}
664
majd@mellanox.com19098df2016-01-14 19:13:03 +0200665static int mlx5_ib_umem_get(struct mlx5_ib_dev *dev,
666 struct ib_pd *pd,
667 unsigned long addr, size_t size,
668 struct ib_umem **umem,
669 int *npages, int *page_shift, int *ncont,
670 u32 *offset)
671{
672 int err;
673
674 *umem = ib_umem_get(pd->uobject->context, addr, size, 0, 0);
675 if (IS_ERR(*umem)) {
676 mlx5_ib_dbg(dev, "umem_get failed\n");
677 return PTR_ERR(*umem);
678 }
679
Majd Dibbiny762f8992016-10-27 16:36:47 +0300680 mlx5_ib_cont_pages(*umem, addr, 0, npages, page_shift, ncont, NULL);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200681
682 err = mlx5_ib_get_buf_offset(addr, *page_shift, offset);
683 if (err) {
684 mlx5_ib_warn(dev, "bad offset\n");
685 goto err_umem;
686 }
687
688 mlx5_ib_dbg(dev, "addr 0x%lx, size %zu, npages %d, page_shift %d, ncont %d, offset %d\n",
689 addr, size, *npages, *page_shift, *ncont, *offset);
690
691 return 0;
692
693err_umem:
694 ib_umem_release(*umem);
695 *umem = NULL;
696
697 return err;
698}
699
Yishai Hadas79b20a62016-05-23 15:20:50 +0300700static void destroy_user_rq(struct ib_pd *pd, struct mlx5_ib_rwq *rwq)
701{
702 struct mlx5_ib_ucontext *context;
703
704 context = to_mucontext(pd->uobject->context);
705 mlx5_ib_db_unmap_user(context, &rwq->db);
706 if (rwq->umem)
707 ib_umem_release(rwq->umem);
708}
709
710static int create_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
711 struct mlx5_ib_rwq *rwq,
712 struct mlx5_ib_create_wq *ucmd)
713{
714 struct mlx5_ib_ucontext *context;
715 int page_shift = 0;
716 int npages;
717 u32 offset = 0;
718 int ncont = 0;
719 int err;
720
721 if (!ucmd->buf_addr)
722 return -EINVAL;
723
724 context = to_mucontext(pd->uobject->context);
725 rwq->umem = ib_umem_get(pd->uobject->context, ucmd->buf_addr,
726 rwq->buf_size, 0, 0);
727 if (IS_ERR(rwq->umem)) {
728 mlx5_ib_dbg(dev, "umem_get failed\n");
729 err = PTR_ERR(rwq->umem);
730 return err;
731 }
732
Majd Dibbiny762f8992016-10-27 16:36:47 +0300733 mlx5_ib_cont_pages(rwq->umem, ucmd->buf_addr, 0, &npages, &page_shift,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300734 &ncont, NULL);
735 err = mlx5_ib_get_buf_offset(ucmd->buf_addr, page_shift,
736 &rwq->rq_page_offset);
737 if (err) {
738 mlx5_ib_warn(dev, "bad offset\n");
739 goto err_umem;
740 }
741
742 rwq->rq_num_pas = ncont;
743 rwq->page_shift = page_shift;
744 rwq->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
745 rwq->wq_sig = !!(ucmd->flags & MLX5_WQ_FLAG_SIGNATURE);
746
747 mlx5_ib_dbg(dev, "addr 0x%llx, size %zd, npages %d, page_shift %d, ncont %d, offset %d\n",
748 (unsigned long long)ucmd->buf_addr, rwq->buf_size,
749 npages, page_shift, ncont, offset);
750
751 err = mlx5_ib_db_map_user(context, ucmd->db_addr, &rwq->db);
752 if (err) {
753 mlx5_ib_dbg(dev, "map failed\n");
754 goto err_umem;
755 }
756
757 rwq->create_type = MLX5_WQ_USER;
758 return 0;
759
760err_umem:
761 ib_umem_release(rwq->umem);
762 return err;
763}
764
Eli Cohene126ba92013-07-07 17:25:49 +0300765static int create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
766 struct mlx5_ib_qp *qp, struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200767 struct ib_qp_init_attr *attr,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300768 u32 **in,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200769 struct mlx5_ib_create_qp_resp *resp, int *inlen,
770 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300771{
772 struct mlx5_ib_ucontext *context;
773 struct mlx5_ib_create_qp ucmd;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200774 struct mlx5_ib_ubuffer *ubuffer = &base->ubuffer;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200775 int page_shift = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300776 int uar_index;
777 int npages;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200778 u32 offset = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300779 int uuarn;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200780 int ncont = 0;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300781 __be64 *pas;
782 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300783 int err;
784
785 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
786 if (err) {
787 mlx5_ib_dbg(dev, "copy failed\n");
788 return err;
789 }
790
791 context = to_mucontext(pd->uobject->context);
792 /*
793 * TBD: should come from the verbs when we have the API
794 */
Leon Romanovsky051f2632015-12-20 12:16:11 +0200795 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
796 /* In CROSS_CHANNEL CQ and QP must use the same UAR */
797 uuarn = MLX5_CROSS_CHANNEL_UUAR;
798 else {
799 uuarn = alloc_uuar(&context->uuari, MLX5_IB_LATENCY_CLASS_HIGH);
Eli Cohene126ba92013-07-07 17:25:49 +0300800 if (uuarn < 0) {
Leon Romanovsky051f2632015-12-20 12:16:11 +0200801 mlx5_ib_dbg(dev, "failed to allocate low latency UUAR\n");
802 mlx5_ib_dbg(dev, "reverting to medium latency\n");
803 uuarn = alloc_uuar(&context->uuari, MLX5_IB_LATENCY_CLASS_MEDIUM);
Eli Cohenc1be5232014-01-14 17:45:12 +0200804 if (uuarn < 0) {
Leon Romanovsky051f2632015-12-20 12:16:11 +0200805 mlx5_ib_dbg(dev, "failed to allocate medium latency UUAR\n");
806 mlx5_ib_dbg(dev, "reverting to high latency\n");
807 uuarn = alloc_uuar(&context->uuari, MLX5_IB_LATENCY_CLASS_LOW);
808 if (uuarn < 0) {
809 mlx5_ib_warn(dev, "uuar allocation failed\n");
810 return uuarn;
811 }
Eli Cohenc1be5232014-01-14 17:45:12 +0200812 }
Eli Cohene126ba92013-07-07 17:25:49 +0300813 }
814 }
815
816 uar_index = uuarn_to_uar_index(&context->uuari, uuarn);
817 mlx5_ib_dbg(dev, "uuarn 0x%x, uar_index 0x%x\n", uuarn, uar_index);
818
Haggai Eran48fea832014-05-22 14:50:11 +0300819 qp->rq.offset = 0;
820 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
821 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
822
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200823 err = set_user_buf_size(dev, qp, &ucmd, base, attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300824 if (err)
825 goto err_uuar;
826
majd@mellanox.com19098df2016-01-14 19:13:03 +0200827 if (ucmd.buf_addr && ubuffer->buf_size) {
828 ubuffer->buf_addr = ucmd.buf_addr;
829 err = mlx5_ib_umem_get(dev, pd, ubuffer->buf_addr,
830 ubuffer->buf_size,
831 &ubuffer->umem, &npages, &page_shift,
832 &ncont, &offset);
833 if (err)
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200834 goto err_uuar;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200835 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +0200836 ubuffer->umem = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300837 }
Eli Cohene126ba92013-07-07 17:25:49 +0300838
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300839 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
840 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * ncont;
Eli Cohene126ba92013-07-07 17:25:49 +0300841 *in = mlx5_vzalloc(*inlen);
842 if (!*in) {
843 err = -ENOMEM;
844 goto err_umem;
845 }
Eli Cohene126ba92013-07-07 17:25:49 +0300846
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300847 pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas);
848 if (ubuffer->umem)
849 mlx5_ib_populate_pas(dev, ubuffer->umem, page_shift, pas, 0);
850
851 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
852
853 MLX5_SET(qpc, qpc, log_page_size, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
854 MLX5_SET(qpc, qpc, page_offset, offset);
855
856 MLX5_SET(qpc, qpc, uar_page, uar_index);
Eli Cohene126ba92013-07-07 17:25:49 +0300857 resp->uuar_index = uuarn;
858 qp->uuarn = uuarn;
859
860 err = mlx5_ib_db_map_user(context, ucmd.db_addr, &qp->db);
861 if (err) {
862 mlx5_ib_dbg(dev, "map failed\n");
863 goto err_free;
864 }
865
866 err = ib_copy_to_udata(udata, resp, sizeof(*resp));
867 if (err) {
868 mlx5_ib_dbg(dev, "copy failed\n");
869 goto err_unmap;
870 }
871 qp->create_type = MLX5_QP_USER;
872
873 return 0;
874
875err_unmap:
876 mlx5_ib_db_unmap_user(context, &qp->db);
877
878err_free:
Al Viro479163f2014-11-20 08:13:57 +0000879 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +0300880
881err_umem:
majd@mellanox.com19098df2016-01-14 19:13:03 +0200882 if (ubuffer->umem)
883 ib_umem_release(ubuffer->umem);
Eli Cohene126ba92013-07-07 17:25:49 +0300884
885err_uuar:
886 free_uuar(&context->uuari, uuarn);
887 return err;
888}
889
majd@mellanox.com19098df2016-01-14 19:13:03 +0200890static void destroy_qp_user(struct ib_pd *pd, struct mlx5_ib_qp *qp,
891 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300892{
893 struct mlx5_ib_ucontext *context;
894
895 context = to_mucontext(pd->uobject->context);
896 mlx5_ib_db_unmap_user(context, &qp->db);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200897 if (base->ubuffer.umem)
898 ib_umem_release(base->ubuffer.umem);
Eli Cohene126ba92013-07-07 17:25:49 +0300899 free_uuar(&context->uuari, qp->uuarn);
900}
901
902static int create_kernel_qp(struct mlx5_ib_dev *dev,
903 struct ib_qp_init_attr *init_attr,
904 struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300905 u32 **in, int *inlen,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200906 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300907{
908 enum mlx5_ib_latency_class lc = MLX5_IB_LATENCY_CLASS_LOW;
909 struct mlx5_uuar_info *uuari;
910 int uar_index;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300911 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300912 int uuarn;
913 int err;
914
Jack Morgenstein9603b612014-07-28 23:30:22 +0300915 uuari = &dev->mdev->priv.uuari;
Erez Shitritf0313962016-02-21 16:27:17 +0200916 if (init_attr->create_flags & ~(IB_QP_CREATE_SIGNATURE_EN |
917 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
Haggai Eranb11a4f92016-02-29 15:45:03 +0200918 IB_QP_CREATE_IPOIB_UD_LSO |
919 mlx5_ib_create_qp_sqpn_qp1()))
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200920 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300921
922 if (init_attr->qp_type == MLX5_IB_QPT_REG_UMR)
923 lc = MLX5_IB_LATENCY_CLASS_FAST_PATH;
924
925 uuarn = alloc_uuar(uuari, lc);
926 if (uuarn < 0) {
927 mlx5_ib_dbg(dev, "\n");
928 return -ENOMEM;
929 }
930
931 qp->bf = &uuari->bfs[uuarn];
932 uar_index = qp->bf->uar->index;
933
934 err = calc_sq_size(dev, init_attr, qp);
935 if (err < 0) {
936 mlx5_ib_dbg(dev, "err %d\n", err);
937 goto err_uuar;
938 }
939
940 qp->rq.offset = 0;
941 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200942 base->ubuffer.buf_size = err + (qp->rq.wqe_cnt << qp->rq.wqe_shift);
Eli Cohene126ba92013-07-07 17:25:49 +0300943
majd@mellanox.com19098df2016-01-14 19:13:03 +0200944 err = mlx5_buf_alloc(dev->mdev, base->ubuffer.buf_size, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +0300945 if (err) {
946 mlx5_ib_dbg(dev, "err %d\n", err);
947 goto err_uuar;
948 }
949
950 qp->sq.qend = mlx5_get_send_wqe(qp, qp->sq.wqe_cnt);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300951 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
952 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * qp->buf.npages;
Eli Cohene126ba92013-07-07 17:25:49 +0300953 *in = mlx5_vzalloc(*inlen);
954 if (!*in) {
955 err = -ENOMEM;
956 goto err_buf;
957 }
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300958
959 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
960 MLX5_SET(qpc, qpc, uar_page, uar_index);
961 MLX5_SET(qpc, qpc, log_page_size, qp->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
962
Eli Cohene126ba92013-07-07 17:25:49 +0300963 /* Set "fast registration enabled" for all kernel QPs */
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300964 MLX5_SET(qpc, qpc, fre, 1);
965 MLX5_SET(qpc, qpc, rlky, 1);
Eli Cohene126ba92013-07-07 17:25:49 +0300966
Haggai Eranb11a4f92016-02-29 15:45:03 +0200967 if (init_attr->create_flags & mlx5_ib_create_qp_sqpn_qp1()) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300968 MLX5_SET(qpc, qpc, deth_sqpn, 1);
Haggai Eranb11a4f92016-02-29 15:45:03 +0200969 qp->flags |= MLX5_IB_QP_SQPN_QP1;
970 }
971
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300972 mlx5_fill_page_array(&qp->buf,
973 (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas));
Eli Cohene126ba92013-07-07 17:25:49 +0300974
Jack Morgenstein9603b612014-07-28 23:30:22 +0300975 err = mlx5_db_alloc(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300976 if (err) {
977 mlx5_ib_dbg(dev, "err %d\n", err);
978 goto err_free;
979 }
980
Eli Cohene126ba92013-07-07 17:25:49 +0300981 qp->sq.wrid = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wrid), GFP_KERNEL);
982 qp->sq.wr_data = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wr_data), GFP_KERNEL);
983 qp->rq.wrid = kmalloc(qp->rq.wqe_cnt * sizeof(*qp->rq.wrid), GFP_KERNEL);
984 qp->sq.w_list = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.w_list), GFP_KERNEL);
985 qp->sq.wqe_head = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wqe_head), GFP_KERNEL);
986
987 if (!qp->sq.wrid || !qp->sq.wr_data || !qp->rq.wrid ||
988 !qp->sq.w_list || !qp->sq.wqe_head) {
989 err = -ENOMEM;
990 goto err_wrid;
991 }
992 qp->create_type = MLX5_QP_KERNEL;
993
994 return 0;
995
996err_wrid:
Jack Morgenstein9603b612014-07-28 23:30:22 +0300997 mlx5_db_free(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300998 kfree(qp->sq.wqe_head);
999 kfree(qp->sq.w_list);
1000 kfree(qp->sq.wrid);
1001 kfree(qp->sq.wr_data);
1002 kfree(qp->rq.wrid);
1003
1004err_free:
Al Viro479163f2014-11-20 08:13:57 +00001005 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +03001006
1007err_buf:
Jack Morgenstein9603b612014-07-28 23:30:22 +03001008 mlx5_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001009
1010err_uuar:
Jack Morgenstein9603b612014-07-28 23:30:22 +03001011 free_uuar(&dev->mdev->priv.uuari, uuarn);
Eli Cohene126ba92013-07-07 17:25:49 +03001012 return err;
1013}
1014
1015static void destroy_qp_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1016{
Jack Morgenstein9603b612014-07-28 23:30:22 +03001017 mlx5_db_free(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001018 kfree(qp->sq.wqe_head);
1019 kfree(qp->sq.w_list);
1020 kfree(qp->sq.wrid);
1021 kfree(qp->sq.wr_data);
1022 kfree(qp->rq.wrid);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001023 mlx5_buf_free(dev->mdev, &qp->buf);
1024 free_uuar(&dev->mdev->priv.uuari, qp->bf->uuarn);
Eli Cohene126ba92013-07-07 17:25:49 +03001025}
1026
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001027static u32 get_rx_type(struct mlx5_ib_qp *qp, struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +03001028{
1029 if (attr->srq || (attr->qp_type == IB_QPT_XRC_TGT) ||
1030 (attr->qp_type == IB_QPT_XRC_INI))
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001031 return MLX5_SRQ_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001032 else if (!qp->has_rq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001033 return MLX5_ZERO_LEN_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001034 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001035 return MLX5_NON_ZERO_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001036}
1037
1038static int is_connected(enum ib_qp_type qp_type)
1039{
1040 if (qp_type == IB_QPT_RC || qp_type == IB_QPT_UC)
1041 return 1;
1042
1043 return 0;
1044}
1045
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001046static int create_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
1047 struct mlx5_ib_sq *sq, u32 tdn)
1048{
Saeed Mahameedc4f287c2016-07-19 20:17:12 +03001049 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001050 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1051
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001052 MLX5_SET(tisc, tisc, transport_domain, tdn);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001053 return mlx5_core_create_tis(dev->mdev, in, sizeof(in), &sq->tisn);
1054}
1055
1056static void destroy_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
1057 struct mlx5_ib_sq *sq)
1058{
1059 mlx5_core_destroy_tis(dev->mdev, sq->tisn);
1060}
1061
1062static int create_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1063 struct mlx5_ib_sq *sq, void *qpin,
1064 struct ib_pd *pd)
1065{
1066 struct mlx5_ib_ubuffer *ubuffer = &sq->ubuffer;
1067 __be64 *pas;
1068 void *in;
1069 void *sqc;
1070 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1071 void *wq;
1072 int inlen;
1073 int err;
1074 int page_shift = 0;
1075 int npages;
1076 int ncont = 0;
1077 u32 offset = 0;
1078
1079 err = mlx5_ib_umem_get(dev, pd, ubuffer->buf_addr, ubuffer->buf_size,
1080 &sq->ubuffer.umem, &npages, &page_shift,
1081 &ncont, &offset);
1082 if (err)
1083 return err;
1084
1085 inlen = MLX5_ST_SZ_BYTES(create_sq_in) + sizeof(u64) * ncont;
1086 in = mlx5_vzalloc(inlen);
1087 if (!in) {
1088 err = -ENOMEM;
1089 goto err_umem;
1090 }
1091
1092 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1093 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
1094 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1095 MLX5_SET(sqc, sqc, user_index, MLX5_GET(qpc, qpc, user_index));
1096 MLX5_SET(sqc, sqc, cqn, MLX5_GET(qpc, qpc, cqn_snd));
1097 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1098 MLX5_SET(sqc, sqc, tis_num_0, sq->tisn);
1099
1100 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1101 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1102 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1103 MLX5_SET(wq, wq, uar_page, MLX5_GET(qpc, qpc, uar_page));
1104 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1105 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1106 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_sq_size));
1107 MLX5_SET(wq, wq, log_wq_pg_sz, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1108 MLX5_SET(wq, wq, page_offset, offset);
1109
1110 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1111 mlx5_ib_populate_pas(dev, sq->ubuffer.umem, page_shift, pas, 0);
1112
1113 err = mlx5_core_create_sq_tracked(dev->mdev, in, inlen, &sq->base.mqp);
1114
1115 kvfree(in);
1116
1117 if (err)
1118 goto err_umem;
1119
1120 return 0;
1121
1122err_umem:
1123 ib_umem_release(sq->ubuffer.umem);
1124 sq->ubuffer.umem = NULL;
1125
1126 return err;
1127}
1128
1129static void destroy_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1130 struct mlx5_ib_sq *sq)
1131{
1132 mlx5_core_destroy_sq_tracked(dev->mdev, &sq->base.mqp);
1133 ib_umem_release(sq->ubuffer.umem);
1134}
1135
1136static int get_rq_pas_size(void *qpc)
1137{
1138 u32 log_page_size = MLX5_GET(qpc, qpc, log_page_size) + 12;
1139 u32 log_rq_stride = MLX5_GET(qpc, qpc, log_rq_stride);
1140 u32 log_rq_size = MLX5_GET(qpc, qpc, log_rq_size);
1141 u32 page_offset = MLX5_GET(qpc, qpc, page_offset);
1142 u32 po_quanta = 1 << (log_page_size - 6);
1143 u32 rq_sz = 1 << (log_rq_size + 4 + log_rq_stride);
1144 u32 page_size = 1 << log_page_size;
1145 u32 rq_sz_po = rq_sz + (page_offset * po_quanta);
1146 u32 rq_num_pas = (rq_sz_po + page_size - 1) / page_size;
1147
1148 return rq_num_pas * sizeof(u64);
1149}
1150
1151static int create_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1152 struct mlx5_ib_rq *rq, void *qpin)
1153{
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001154 struct mlx5_ib_qp *mqp = rq->base.container_mibqp;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001155 __be64 *pas;
1156 __be64 *qp_pas;
1157 void *in;
1158 void *rqc;
1159 void *wq;
1160 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1161 int inlen;
1162 int err;
1163 u32 rq_pas_size = get_rq_pas_size(qpc);
1164
1165 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + rq_pas_size;
1166 in = mlx5_vzalloc(inlen);
1167 if (!in)
1168 return -ENOMEM;
1169
1170 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
1171 MLX5_SET(rqc, rqc, vsd, 1);
1172 MLX5_SET(rqc, rqc, mem_rq_type, MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
1173 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
1174 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
1175 MLX5_SET(rqc, rqc, user_index, MLX5_GET(qpc, qpc, user_index));
1176 MLX5_SET(rqc, rqc, cqn, MLX5_GET(qpc, qpc, cqn_rcv));
1177
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001178 if (mqp->flags & MLX5_IB_QP_CAP_SCATTER_FCS)
1179 MLX5_SET(rqc, rqc, scatter_fcs, 1);
1180
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001181 wq = MLX5_ADDR_OF(rqc, rqc, wq);
1182 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1183 MLX5_SET(wq, wq, end_padding_mode,
Maor Gottlieb01581fb2016-01-28 17:51:49 +02001184 MLX5_GET(qpc, qpc, end_padding_mode));
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001185 MLX5_SET(wq, wq, page_offset, MLX5_GET(qpc, qpc, page_offset));
1186 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1187 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1188 MLX5_SET(wq, wq, log_wq_stride, MLX5_GET(qpc, qpc, log_rq_stride) + 4);
1189 MLX5_SET(wq, wq, log_wq_pg_sz, MLX5_GET(qpc, qpc, log_page_size));
1190 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_rq_size));
1191
1192 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1193 qp_pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, qpin, pas);
1194 memcpy(pas, qp_pas, rq_pas_size);
1195
1196 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rq->base.mqp);
1197
1198 kvfree(in);
1199
1200 return err;
1201}
1202
1203static void destroy_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1204 struct mlx5_ib_rq *rq)
1205{
1206 mlx5_core_destroy_rq_tracked(dev->mdev, &rq->base.mqp);
1207}
1208
1209static int create_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1210 struct mlx5_ib_rq *rq, u32 tdn)
1211{
1212 u32 *in;
1213 void *tirc;
1214 int inlen;
1215 int err;
1216
1217 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
1218 in = mlx5_vzalloc(inlen);
1219 if (!in)
1220 return -ENOMEM;
1221
1222 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1223 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
1224 MLX5_SET(tirc, tirc, inline_rqn, rq->base.mqp.qpn);
1225 MLX5_SET(tirc, tirc, transport_domain, tdn);
1226
1227 err = mlx5_core_create_tir(dev->mdev, in, inlen, &rq->tirn);
1228
1229 kvfree(in);
1230
1231 return err;
1232}
1233
1234static void destroy_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1235 struct mlx5_ib_rq *rq)
1236{
1237 mlx5_core_destroy_tir(dev->mdev, rq->tirn);
1238}
1239
1240static int create_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001241 u32 *in,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001242 struct ib_pd *pd)
1243{
1244 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1245 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1246 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1247 struct ib_uobject *uobj = pd->uobject;
1248 struct ib_ucontext *ucontext = uobj->context;
1249 struct mlx5_ib_ucontext *mucontext = to_mucontext(ucontext);
1250 int err;
1251 u32 tdn = mucontext->tdn;
1252
1253 if (qp->sq.wqe_cnt) {
1254 err = create_raw_packet_qp_tis(dev, sq, tdn);
1255 if (err)
1256 return err;
1257
1258 err = create_raw_packet_qp_sq(dev, sq, in, pd);
1259 if (err)
1260 goto err_destroy_tis;
1261
1262 sq->base.container_mibqp = qp;
1263 }
1264
1265 if (qp->rq.wqe_cnt) {
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001266 rq->base.container_mibqp = qp;
1267
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001268 err = create_raw_packet_qp_rq(dev, rq, in);
1269 if (err)
1270 goto err_destroy_sq;
1271
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001272
1273 err = create_raw_packet_qp_tir(dev, rq, tdn);
1274 if (err)
1275 goto err_destroy_rq;
1276 }
1277
1278 qp->trans_qp.base.mqp.qpn = qp->sq.wqe_cnt ? sq->base.mqp.qpn :
1279 rq->base.mqp.qpn;
1280
1281 return 0;
1282
1283err_destroy_rq:
1284 destroy_raw_packet_qp_rq(dev, rq);
1285err_destroy_sq:
1286 if (!qp->sq.wqe_cnt)
1287 return err;
1288 destroy_raw_packet_qp_sq(dev, sq);
1289err_destroy_tis:
1290 destroy_raw_packet_qp_tis(dev, sq);
1291
1292 return err;
1293}
1294
1295static void destroy_raw_packet_qp(struct mlx5_ib_dev *dev,
1296 struct mlx5_ib_qp *qp)
1297{
1298 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1299 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1300 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1301
1302 if (qp->rq.wqe_cnt) {
1303 destroy_raw_packet_qp_tir(dev, rq);
1304 destroy_raw_packet_qp_rq(dev, rq);
1305 }
1306
1307 if (qp->sq.wqe_cnt) {
1308 destroy_raw_packet_qp_sq(dev, sq);
1309 destroy_raw_packet_qp_tis(dev, sq);
1310 }
1311}
1312
1313static void raw_packet_qp_copy_info(struct mlx5_ib_qp *qp,
1314 struct mlx5_ib_raw_packet_qp *raw_packet_qp)
1315{
1316 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1317 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1318
1319 sq->sq = &qp->sq;
1320 rq->rq = &qp->rq;
1321 sq->doorbell = &qp->db;
1322 rq->doorbell = &qp->db;
1323}
1324
Yishai Hadas28d61372016-05-23 15:20:56 +03001325static void destroy_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1326{
1327 mlx5_core_destroy_tir(dev->mdev, qp->rss_qp.tirn);
1328}
1329
1330static int create_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
1331 struct ib_pd *pd,
1332 struct ib_qp_init_attr *init_attr,
1333 struct ib_udata *udata)
1334{
1335 struct ib_uobject *uobj = pd->uobject;
1336 struct ib_ucontext *ucontext = uobj->context;
1337 struct mlx5_ib_ucontext *mucontext = to_mucontext(ucontext);
1338 struct mlx5_ib_create_qp_resp resp = {};
1339 int inlen;
1340 int err;
1341 u32 *in;
1342 void *tirc;
1343 void *hfso;
1344 u32 selected_fields = 0;
1345 size_t min_resp_len;
1346 u32 tdn = mucontext->tdn;
1347 struct mlx5_ib_create_qp_rss ucmd = {};
1348 size_t required_cmd_sz;
1349
1350 if (init_attr->qp_type != IB_QPT_RAW_PACKET)
1351 return -EOPNOTSUPP;
1352
1353 if (init_attr->create_flags || init_attr->send_cq)
1354 return -EINVAL;
1355
1356 min_resp_len = offsetof(typeof(resp), uuar_index) + sizeof(resp.uuar_index);
1357 if (udata->outlen < min_resp_len)
1358 return -EINVAL;
1359
1360 required_cmd_sz = offsetof(typeof(ucmd), reserved1) + sizeof(ucmd.reserved1);
1361 if (udata->inlen < required_cmd_sz) {
1362 mlx5_ib_dbg(dev, "invalid inlen\n");
1363 return -EINVAL;
1364 }
1365
1366 if (udata->inlen > sizeof(ucmd) &&
1367 !ib_is_udata_cleared(udata, sizeof(ucmd),
1368 udata->inlen - sizeof(ucmd))) {
1369 mlx5_ib_dbg(dev, "inlen is not supported\n");
1370 return -EOPNOTSUPP;
1371 }
1372
1373 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
1374 mlx5_ib_dbg(dev, "copy failed\n");
1375 return -EFAULT;
1376 }
1377
1378 if (ucmd.comp_mask) {
1379 mlx5_ib_dbg(dev, "invalid comp mask\n");
1380 return -EOPNOTSUPP;
1381 }
1382
1383 if (memchr_inv(ucmd.reserved, 0, sizeof(ucmd.reserved)) || ucmd.reserved1) {
1384 mlx5_ib_dbg(dev, "invalid reserved\n");
1385 return -EOPNOTSUPP;
1386 }
1387
1388 err = ib_copy_to_udata(udata, &resp, min_resp_len);
1389 if (err) {
1390 mlx5_ib_dbg(dev, "copy failed\n");
1391 return -EINVAL;
1392 }
1393
1394 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
1395 in = mlx5_vzalloc(inlen);
1396 if (!in)
1397 return -ENOMEM;
1398
1399 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1400 MLX5_SET(tirc, tirc, disp_type,
1401 MLX5_TIRC_DISP_TYPE_INDIRECT);
1402 MLX5_SET(tirc, tirc, indirect_table,
1403 init_attr->rwq_ind_tbl->ind_tbl_num);
1404 MLX5_SET(tirc, tirc, transport_domain, tdn);
1405
1406 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1407 switch (ucmd.rx_hash_function) {
1408 case MLX5_RX_HASH_FUNC_TOEPLITZ:
1409 {
1410 void *rss_key = MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
1411 size_t len = MLX5_FLD_SZ_BYTES(tirc, rx_hash_toeplitz_key);
1412
1413 if (len != ucmd.rx_key_len) {
1414 err = -EINVAL;
1415 goto err;
1416 }
1417
1418 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_TOEPLITZ);
1419 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1420 memcpy(rss_key, ucmd.rx_hash_key, len);
1421 break;
1422 }
1423 default:
1424 err = -EOPNOTSUPP;
1425 goto err;
1426 }
1427
1428 if (!ucmd.rx_hash_fields_mask) {
1429 /* special case when this TIR serves as steering entry without hashing */
1430 if (!init_attr->rwq_ind_tbl->log_ind_tbl_size)
1431 goto create_tir;
1432 err = -EINVAL;
1433 goto err;
1434 }
1435
1436 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1437 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4)) &&
1438 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1439 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))) {
1440 err = -EINVAL;
1441 goto err;
1442 }
1443
1444 /* If none of IPV4 & IPV6 SRC/DST was set - this bit field is ignored */
1445 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1446 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4))
1447 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1448 MLX5_L3_PROT_TYPE_IPV4);
1449 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1450 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1451 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1452 MLX5_L3_PROT_TYPE_IPV6);
1453
1454 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1455 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP)) &&
1456 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1457 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))) {
1458 err = -EINVAL;
1459 goto err;
1460 }
1461
1462 /* If none of TCP & UDP SRC/DST was set - this bit field is ignored */
1463 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1464 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP))
1465 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1466 MLX5_L4_PROT_TYPE_TCP);
1467 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1468 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1469 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1470 MLX5_L4_PROT_TYPE_UDP);
1471
1472 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1473 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6))
1474 selected_fields |= MLX5_HASH_FIELD_SEL_SRC_IP;
1475
1476 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4) ||
1477 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1478 selected_fields |= MLX5_HASH_FIELD_SEL_DST_IP;
1479
1480 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1481 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP))
1482 selected_fields |= MLX5_HASH_FIELD_SEL_L4_SPORT;
1483
1484 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP) ||
1485 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1486 selected_fields |= MLX5_HASH_FIELD_SEL_L4_DPORT;
1487
1488 MLX5_SET(rx_hash_field_select, hfso, selected_fields, selected_fields);
1489
1490create_tir:
1491 err = mlx5_core_create_tir(dev->mdev, in, inlen, &qp->rss_qp.tirn);
1492
1493 if (err)
1494 goto err;
1495
1496 kvfree(in);
1497 /* qpn is reserved for that QP */
1498 qp->trans_qp.base.mqp.qpn = 0;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03001499 qp->flags |= MLX5_IB_QP_RSS;
Yishai Hadas28d61372016-05-23 15:20:56 +03001500 return 0;
1501
1502err:
1503 kvfree(in);
1504 return err;
1505}
1506
Eli Cohene126ba92013-07-07 17:25:49 +03001507static int create_qp_common(struct mlx5_ib_dev *dev, struct ib_pd *pd,
1508 struct ib_qp_init_attr *init_attr,
1509 struct ib_udata *udata, struct mlx5_ib_qp *qp)
1510{
1511 struct mlx5_ib_resources *devr = &dev->devr;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001512 int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
Saeed Mahameed938fe832015-05-28 22:28:41 +03001513 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001514 struct mlx5_ib_create_qp_resp resp;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001515 struct mlx5_ib_cq *send_cq;
1516 struct mlx5_ib_cq *recv_cq;
1517 unsigned long flags;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001518 u32 uidx = MLX5_IB_DEFAULT_UIDX;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001519 struct mlx5_ib_create_qp ucmd;
1520 struct mlx5_ib_qp_base *base;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001521 void *qpc;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001522 u32 *in;
1523 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03001524
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001525 base = init_attr->qp_type == IB_QPT_RAW_PACKET ?
1526 &qp->raw_packet_qp.rq.base :
1527 &qp->trans_qp.base;
1528
1529 if (init_attr->qp_type != IB_QPT_RAW_PACKET)
1530 mlx5_ib_odp_create_qp(qp);
Haggai Eran6aec21f2014-12-11 17:04:23 +02001531
Eli Cohene126ba92013-07-07 17:25:49 +03001532 mutex_init(&qp->mutex);
1533 spin_lock_init(&qp->sq.lock);
1534 spin_lock_init(&qp->rq.lock);
1535
Yishai Hadas28d61372016-05-23 15:20:56 +03001536 if (init_attr->rwq_ind_tbl) {
1537 if (!udata)
1538 return -ENOSYS;
1539
1540 err = create_rss_raw_qp_tir(dev, qp, pd, init_attr, udata);
1541 return err;
1542 }
1543
Eli Cohenf360d882014-04-02 00:10:16 +03001544 if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001545 if (!MLX5_CAP_GEN(mdev, block_lb_mc)) {
Eli Cohenf360d882014-04-02 00:10:16 +03001546 mlx5_ib_dbg(dev, "block multicast loopback isn't supported\n");
1547 return -EINVAL;
1548 } else {
1549 qp->flags |= MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK;
1550 }
1551 }
1552
Leon Romanovsky051f2632015-12-20 12:16:11 +02001553 if (init_attr->create_flags &
1554 (IB_QP_CREATE_CROSS_CHANNEL |
1555 IB_QP_CREATE_MANAGED_SEND |
1556 IB_QP_CREATE_MANAGED_RECV)) {
1557 if (!MLX5_CAP_GEN(mdev, cd)) {
1558 mlx5_ib_dbg(dev, "cross-channel isn't supported\n");
1559 return -EINVAL;
1560 }
1561 if (init_attr->create_flags & IB_QP_CREATE_CROSS_CHANNEL)
1562 qp->flags |= MLX5_IB_QP_CROSS_CHANNEL;
1563 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_SEND)
1564 qp->flags |= MLX5_IB_QP_MANAGED_SEND;
1565 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_RECV)
1566 qp->flags |= MLX5_IB_QP_MANAGED_RECV;
1567 }
Erez Shitritf0313962016-02-21 16:27:17 +02001568
1569 if (init_attr->qp_type == IB_QPT_UD &&
1570 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO))
1571 if (!MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
1572 mlx5_ib_dbg(dev, "ipoib UD lso qp isn't supported\n");
1573 return -EOPNOTSUPP;
1574 }
1575
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001576 if (init_attr->create_flags & IB_QP_CREATE_SCATTER_FCS) {
1577 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
1578 mlx5_ib_dbg(dev, "Scatter FCS is supported only for Raw Packet QPs");
1579 return -EOPNOTSUPP;
1580 }
1581 if (!MLX5_CAP_GEN(dev->mdev, eth_net_offloads) ||
1582 !MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
1583 mlx5_ib_dbg(dev, "Scatter FCS isn't supported\n");
1584 return -EOPNOTSUPP;
1585 }
1586 qp->flags |= MLX5_IB_QP_CAP_SCATTER_FCS;
1587 }
1588
Eli Cohene126ba92013-07-07 17:25:49 +03001589 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
1590 qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;
1591
1592 if (pd && pd->uobject) {
1593 if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
1594 mlx5_ib_dbg(dev, "copy failed\n");
1595 return -EFAULT;
1596 }
1597
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001598 err = get_qp_user_index(to_mucontext(pd->uobject->context),
1599 &ucmd, udata->inlen, &uidx);
1600 if (err)
1601 return err;
1602
Eli Cohene126ba92013-07-07 17:25:49 +03001603 qp->wq_sig = !!(ucmd.flags & MLX5_QP_FLAG_SIGNATURE);
1604 qp->scat_cqe = !!(ucmd.flags & MLX5_QP_FLAG_SCATTER_CQE);
1605 } else {
1606 qp->wq_sig = !!wq_signature;
1607 }
1608
1609 qp->has_rq = qp_has_rq(init_attr);
1610 err = set_rq_size(dev, &init_attr->cap, qp->has_rq,
1611 qp, (pd && pd->uobject) ? &ucmd : NULL);
1612 if (err) {
1613 mlx5_ib_dbg(dev, "err %d\n", err);
1614 return err;
1615 }
1616
1617 if (pd) {
1618 if (pd->uobject) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001619 __u32 max_wqes =
1620 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Eli Cohene126ba92013-07-07 17:25:49 +03001621 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d)\n", ucmd.sq_wqe_count);
1622 if (ucmd.rq_wqe_shift != qp->rq.wqe_shift ||
1623 ucmd.rq_wqe_count != qp->rq.wqe_cnt) {
1624 mlx5_ib_dbg(dev, "invalid rq params\n");
1625 return -EINVAL;
1626 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03001627 if (ucmd.sq_wqe_count > max_wqes) {
Eli Cohene126ba92013-07-07 17:25:49 +03001628 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d) > max allowed (%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +03001629 ucmd.sq_wqe_count, max_wqes);
Eli Cohene126ba92013-07-07 17:25:49 +03001630 return -EINVAL;
1631 }
Haggai Eranb11a4f92016-02-29 15:45:03 +02001632 if (init_attr->create_flags &
1633 mlx5_ib_create_qp_sqpn_qp1()) {
1634 mlx5_ib_dbg(dev, "user-space is not allowed to create UD QPs spoofing as QP1\n");
1635 return -EINVAL;
1636 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001637 err = create_user_qp(dev, pd, qp, udata, init_attr, &in,
1638 &resp, &inlen, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001639 if (err)
1640 mlx5_ib_dbg(dev, "err %d\n", err);
1641 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +02001642 err = create_kernel_qp(dev, init_attr, qp, &in, &inlen,
1643 base);
Eli Cohene126ba92013-07-07 17:25:49 +03001644 if (err)
1645 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohene126ba92013-07-07 17:25:49 +03001646 }
1647
1648 if (err)
1649 return err;
1650 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001651 in = mlx5_vzalloc(inlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001652 if (!in)
1653 return -ENOMEM;
1654
1655 qp->create_type = MLX5_QP_EMPTY;
1656 }
1657
1658 if (is_sqp(init_attr->qp_type))
1659 qp->port = init_attr->port_num;
1660
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001661 qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
1662
1663 MLX5_SET(qpc, qpc, st, to_mlx5_st(init_attr->qp_type));
1664 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
Eli Cohene126ba92013-07-07 17:25:49 +03001665
1666 if (init_attr->qp_type != MLX5_IB_QPT_REG_UMR)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001667 MLX5_SET(qpc, qpc, pd, to_mpd(pd ? pd : devr->p0)->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001668 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001669 MLX5_SET(qpc, qpc, latency_sensitive, 1);
1670
Eli Cohene126ba92013-07-07 17:25:49 +03001671
1672 if (qp->wq_sig)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001673 MLX5_SET(qpc, qpc, wq_signature, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001674
Eli Cohenf360d882014-04-02 00:10:16 +03001675 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001676 MLX5_SET(qpc, qpc, block_lb_mc, 1);
Eli Cohenf360d882014-04-02 00:10:16 +03001677
Leon Romanovsky051f2632015-12-20 12:16:11 +02001678 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001679 MLX5_SET(qpc, qpc, cd_master, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001680 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001681 MLX5_SET(qpc, qpc, cd_slave_send, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001682 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001683 MLX5_SET(qpc, qpc, cd_slave_receive, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001684
Eli Cohene126ba92013-07-07 17:25:49 +03001685 if (qp->scat_cqe && is_connected(init_attr->qp_type)) {
1686 int rcqe_sz;
1687 int scqe_sz;
1688
1689 rcqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->recv_cq);
1690 scqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->send_cq);
1691
1692 if (rcqe_sz == 128)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001693 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA64_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001694 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001695 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA32_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001696
1697 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR) {
1698 if (scqe_sz == 128)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001699 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA64_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001700 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001701 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA32_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001702 }
1703 }
1704
1705 if (qp->rq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001706 MLX5_SET(qpc, qpc, log_rq_stride, qp->rq.wqe_shift - 4);
1707 MLX5_SET(qpc, qpc, log_rq_size, ilog2(qp->rq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03001708 }
1709
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001710 MLX5_SET(qpc, qpc, rq_type, get_rx_type(qp, init_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03001711
1712 if (qp->sq.wqe_cnt)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001713 MLX5_SET(qpc, qpc, log_sq_size, ilog2(qp->sq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03001714 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001715 MLX5_SET(qpc, qpc, no_sq, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001716
1717 /* Set default resources */
1718 switch (init_attr->qp_type) {
1719 case IB_QPT_XRC_TGT:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001720 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
1721 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(devr->c0)->mcq.cqn);
1722 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
1723 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(init_attr->xrcd)->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001724 break;
1725 case IB_QPT_XRC_INI:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001726 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
1727 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
1728 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001729 break;
1730 default:
1731 if (init_attr->srq) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001732 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x0)->xrcdn);
1733 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(init_attr->srq)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001734 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001735 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
1736 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s1)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001737 }
1738 }
1739
1740 if (init_attr->send_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001741 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(init_attr->send_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001742
1743 if (init_attr->recv_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001744 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(init_attr->recv_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001745
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001746 MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
Eli Cohene126ba92013-07-07 17:25:49 +03001747
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001748 /* 0xffffff means we ask to work with cqe version 0 */
1749 if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1)
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001750 MLX5_SET(qpc, qpc, user_index, uidx);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001751
Erez Shitritf0313962016-02-21 16:27:17 +02001752 /* we use IB_QP_CREATE_IPOIB_UD_LSO to indicates ipoib qp */
1753 if (init_attr->qp_type == IB_QPT_UD &&
1754 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)) {
Erez Shitritf0313962016-02-21 16:27:17 +02001755 MLX5_SET(qpc, qpc, ulp_stateless_offload_mode, 1);
1756 qp->flags |= MLX5_IB_QP_LSO;
1757 }
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001758
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001759 if (init_attr->qp_type == IB_QPT_RAW_PACKET) {
1760 qp->raw_packet_qp.sq.ubuffer.buf_addr = ucmd.sq_buf_addr;
1761 raw_packet_qp_copy_info(qp, &qp->raw_packet_qp);
1762 err = create_raw_packet_qp(dev, qp, in, pd);
1763 } else {
1764 err = mlx5_core_create_qp(dev->mdev, &base->mqp, in, inlen);
1765 }
1766
Eli Cohene126ba92013-07-07 17:25:49 +03001767 if (err) {
1768 mlx5_ib_dbg(dev, "create qp failed\n");
1769 goto err_create;
1770 }
1771
Al Viro479163f2014-11-20 08:13:57 +00001772 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001773
majd@mellanox.com19098df2016-01-14 19:13:03 +02001774 base->container_mibqp = qp;
1775 base->mqp.event = mlx5_ib_qp_event;
Eli Cohene126ba92013-07-07 17:25:49 +03001776
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001777 get_cqs(init_attr->qp_type, init_attr->send_cq, init_attr->recv_cq,
1778 &send_cq, &recv_cq);
1779 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
1780 mlx5_ib_lock_cqs(send_cq, recv_cq);
1781 /* Maintain device to QPs access, needed for further handling via reset
1782 * flow
1783 */
1784 list_add_tail(&qp->qps_list, &dev->qp_list);
1785 /* Maintain CQ to QPs access, needed for further handling via reset flow
1786 */
1787 if (send_cq)
1788 list_add_tail(&qp->cq_send_list, &send_cq->list_send_qp);
1789 if (recv_cq)
1790 list_add_tail(&qp->cq_recv_list, &recv_cq->list_recv_qp);
1791 mlx5_ib_unlock_cqs(send_cq, recv_cq);
1792 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
1793
Eli Cohene126ba92013-07-07 17:25:49 +03001794 return 0;
1795
1796err_create:
1797 if (qp->create_type == MLX5_QP_USER)
majd@mellanox.com19098df2016-01-14 19:13:03 +02001798 destroy_qp_user(pd, qp, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001799 else if (qp->create_type == MLX5_QP_KERNEL)
1800 destroy_qp_kernel(dev, qp);
1801
Al Viro479163f2014-11-20 08:13:57 +00001802 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001803 return err;
1804}
1805
1806static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
1807 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
1808{
1809 if (send_cq) {
1810 if (recv_cq) {
1811 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001812 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001813 spin_lock_nested(&recv_cq->lock,
1814 SINGLE_DEPTH_NESTING);
1815 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001816 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001817 __acquire(&recv_cq->lock);
1818 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001819 spin_lock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001820 spin_lock_nested(&send_cq->lock,
1821 SINGLE_DEPTH_NESTING);
1822 }
1823 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001824 spin_lock(&send_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001825 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001826 }
1827 } else if (recv_cq) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001828 spin_lock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001829 __acquire(&send_cq->lock);
1830 } else {
1831 __acquire(&send_cq->lock);
1832 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001833 }
1834}
1835
1836static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
1837 __releases(&send_cq->lock) __releases(&recv_cq->lock)
1838{
1839 if (send_cq) {
1840 if (recv_cq) {
1841 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
1842 spin_unlock(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001843 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001844 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
1845 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001846 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001847 } else {
1848 spin_unlock(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001849 spin_unlock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001850 }
1851 } else {
Eli Cohen6a4f1392014-12-02 12:26:18 +02001852 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001853 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001854 }
1855 } else if (recv_cq) {
Eli Cohen6a4f1392014-12-02 12:26:18 +02001856 __release(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001857 spin_unlock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001858 } else {
1859 __release(&recv_cq->lock);
1860 __release(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001861 }
1862}
1863
1864static struct mlx5_ib_pd *get_pd(struct mlx5_ib_qp *qp)
1865{
1866 return to_mpd(qp->ibqp.pd);
1867}
1868
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001869static void get_cqs(enum ib_qp_type qp_type,
1870 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
Eli Cohene126ba92013-07-07 17:25:49 +03001871 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq)
1872{
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001873 switch (qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +03001874 case IB_QPT_XRC_TGT:
1875 *send_cq = NULL;
1876 *recv_cq = NULL;
1877 break;
1878 case MLX5_IB_QPT_REG_UMR:
1879 case IB_QPT_XRC_INI:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001880 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03001881 *recv_cq = NULL;
1882 break;
1883
1884 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02001885 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03001886 case IB_QPT_RC:
1887 case IB_QPT_UC:
1888 case IB_QPT_UD:
1889 case IB_QPT_RAW_IPV6:
1890 case IB_QPT_RAW_ETHERTYPE:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001891 case IB_QPT_RAW_PACKET:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001892 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
1893 *recv_cq = ib_recv_cq ? to_mcq(ib_recv_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03001894 break;
1895
Eli Cohene126ba92013-07-07 17:25:49 +03001896 case IB_QPT_MAX:
1897 default:
1898 *send_cq = NULL;
1899 *recv_cq = NULL;
1900 break;
1901 }
1902}
1903
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001904static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03001905 const struct mlx5_modify_raw_qp_param *raw_qp_param,
1906 u8 lag_tx_affinity);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001907
Eli Cohene126ba92013-07-07 17:25:49 +03001908static void destroy_qp_common(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1909{
1910 struct mlx5_ib_cq *send_cq, *recv_cq;
majd@mellanox.com19098df2016-01-14 19:13:03 +02001911 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001912 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03001913 int err;
1914
Yishai Hadas28d61372016-05-23 15:20:56 +03001915 if (qp->ibqp.rwq_ind_tbl) {
1916 destroy_rss_raw_qp_tir(dev, qp);
1917 return;
1918 }
1919
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001920 base = qp->ibqp.qp_type == IB_QPT_RAW_PACKET ?
1921 &qp->raw_packet_qp.rq.base :
1922 &qp->trans_qp.base;
1923
Haggai Eran6aec21f2014-12-11 17:04:23 +02001924 if (qp->state != IB_QPS_RESET) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001925 if (qp->ibqp.qp_type != IB_QPT_RAW_PACKET) {
1926 mlx5_ib_qp_disable_pagefaults(qp);
1927 err = mlx5_core_qp_modify(dev->mdev,
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03001928 MLX5_CMD_OP_2RST_QP, 0,
1929 NULL, &base->mqp);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001930 } else {
Alex Vesker0680efa2016-08-28 12:25:52 +03001931 struct mlx5_modify_raw_qp_param raw_qp_param = {
1932 .operation = MLX5_CMD_OP_2RST_QP
1933 };
1934
Aviv Heller13eab212016-09-18 20:48:04 +03001935 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, 0);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001936 }
1937 if (err)
majd@mellanox.com427c1e72016-01-14 19:13:07 +02001938 mlx5_ib_warn(dev, "mlx5_ib: modify QP 0x%06x to RESET failed\n",
majd@mellanox.com19098df2016-01-14 19:13:03 +02001939 base->mqp.qpn);
Haggai Eran6aec21f2014-12-11 17:04:23 +02001940 }
Eli Cohene126ba92013-07-07 17:25:49 +03001941
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001942 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
1943 &send_cq, &recv_cq);
1944
1945 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
1946 mlx5_ib_lock_cqs(send_cq, recv_cq);
1947 /* del from lists under both locks above to protect reset flow paths */
1948 list_del(&qp->qps_list);
1949 if (send_cq)
1950 list_del(&qp->cq_send_list);
1951
1952 if (recv_cq)
1953 list_del(&qp->cq_recv_list);
Eli Cohene126ba92013-07-07 17:25:49 +03001954
1955 if (qp->create_type == MLX5_QP_KERNEL) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02001956 __mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03001957 qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
1958 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02001959 __mlx5_ib_cq_clean(send_cq, base->mqp.qpn,
1960 NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03001961 }
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001962 mlx5_ib_unlock_cqs(send_cq, recv_cq);
1963 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
Eli Cohene126ba92013-07-07 17:25:49 +03001964
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001965 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET) {
1966 destroy_raw_packet_qp(dev, qp);
1967 } else {
1968 err = mlx5_core_destroy_qp(dev->mdev, &base->mqp);
1969 if (err)
1970 mlx5_ib_warn(dev, "failed to destroy QP 0x%x\n",
1971 base->mqp.qpn);
1972 }
Eli Cohene126ba92013-07-07 17:25:49 +03001973
Eli Cohene126ba92013-07-07 17:25:49 +03001974 if (qp->create_type == MLX5_QP_KERNEL)
1975 destroy_qp_kernel(dev, qp);
1976 else if (qp->create_type == MLX5_QP_USER)
majd@mellanox.com19098df2016-01-14 19:13:03 +02001977 destroy_qp_user(&get_pd(qp)->ibpd, qp, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001978}
1979
1980static const char *ib_qp_type_str(enum ib_qp_type type)
1981{
1982 switch (type) {
1983 case IB_QPT_SMI:
1984 return "IB_QPT_SMI";
1985 case IB_QPT_GSI:
1986 return "IB_QPT_GSI";
1987 case IB_QPT_RC:
1988 return "IB_QPT_RC";
1989 case IB_QPT_UC:
1990 return "IB_QPT_UC";
1991 case IB_QPT_UD:
1992 return "IB_QPT_UD";
1993 case IB_QPT_RAW_IPV6:
1994 return "IB_QPT_RAW_IPV6";
1995 case IB_QPT_RAW_ETHERTYPE:
1996 return "IB_QPT_RAW_ETHERTYPE";
1997 case IB_QPT_XRC_INI:
1998 return "IB_QPT_XRC_INI";
1999 case IB_QPT_XRC_TGT:
2000 return "IB_QPT_XRC_TGT";
2001 case IB_QPT_RAW_PACKET:
2002 return "IB_QPT_RAW_PACKET";
2003 case MLX5_IB_QPT_REG_UMR:
2004 return "MLX5_IB_QPT_REG_UMR";
2005 case IB_QPT_MAX:
2006 default:
2007 return "Invalid QP type";
2008 }
2009}
2010
2011struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd,
2012 struct ib_qp_init_attr *init_attr,
2013 struct ib_udata *udata)
2014{
2015 struct mlx5_ib_dev *dev;
2016 struct mlx5_ib_qp *qp;
2017 u16 xrcdn = 0;
2018 int err;
2019
2020 if (pd) {
2021 dev = to_mdev(pd->device);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002022
2023 if (init_attr->qp_type == IB_QPT_RAW_PACKET) {
2024 if (!pd->uobject) {
2025 mlx5_ib_dbg(dev, "Raw Packet QP is not supported for kernel consumers\n");
2026 return ERR_PTR(-EINVAL);
2027 } else if (!to_mucontext(pd->uobject->context)->cqe_version) {
2028 mlx5_ib_dbg(dev, "Raw Packet QP is only supported for CQE version > 0\n");
2029 return ERR_PTR(-EINVAL);
2030 }
2031 }
Majd Dibbiny09f16cf2016-01-28 17:51:48 +02002032 } else {
2033 /* being cautious here */
2034 if (init_attr->qp_type != IB_QPT_XRC_TGT &&
2035 init_attr->qp_type != MLX5_IB_QPT_REG_UMR) {
2036 pr_warn("%s: no PD for transport %s\n", __func__,
2037 ib_qp_type_str(init_attr->qp_type));
2038 return ERR_PTR(-EINVAL);
2039 }
2040 dev = to_mdev(to_mxrcd(init_attr->xrcd)->ibxrcd.device);
Eli Cohene126ba92013-07-07 17:25:49 +03002041 }
2042
2043 switch (init_attr->qp_type) {
2044 case IB_QPT_XRC_TGT:
2045 case IB_QPT_XRC_INI:
Saeed Mahameed938fe832015-05-28 22:28:41 +03002046 if (!MLX5_CAP_GEN(dev->mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002047 mlx5_ib_dbg(dev, "XRC not supported\n");
2048 return ERR_PTR(-ENOSYS);
2049 }
2050 init_attr->recv_cq = NULL;
2051 if (init_attr->qp_type == IB_QPT_XRC_TGT) {
2052 xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
2053 init_attr->send_cq = NULL;
2054 }
2055
2056 /* fall through */
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002057 case IB_QPT_RAW_PACKET:
Eli Cohene126ba92013-07-07 17:25:49 +03002058 case IB_QPT_RC:
2059 case IB_QPT_UC:
2060 case IB_QPT_UD:
2061 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002062 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002063 case MLX5_IB_QPT_REG_UMR:
2064 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2065 if (!qp)
2066 return ERR_PTR(-ENOMEM);
2067
2068 err = create_qp_common(dev, pd, init_attr, udata, qp);
2069 if (err) {
2070 mlx5_ib_dbg(dev, "create_qp_common failed\n");
2071 kfree(qp);
2072 return ERR_PTR(err);
2073 }
2074
2075 if (is_qp0(init_attr->qp_type))
2076 qp->ibqp.qp_num = 0;
2077 else if (is_qp1(init_attr->qp_type))
2078 qp->ibqp.qp_num = 1;
2079 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002080 qp->ibqp.qp_num = qp->trans_qp.base.mqp.qpn;
Eli Cohene126ba92013-07-07 17:25:49 +03002081
2082 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 +02002083 qp->ibqp.qp_num, qp->trans_qp.base.mqp.qpn,
2084 to_mcq(init_attr->recv_cq)->mcq.cqn,
Eli Cohene126ba92013-07-07 17:25:49 +03002085 to_mcq(init_attr->send_cq)->mcq.cqn);
2086
majd@mellanox.com19098df2016-01-14 19:13:03 +02002087 qp->trans_qp.xrcdn = xrcdn;
Eli Cohene126ba92013-07-07 17:25:49 +03002088
2089 break;
2090
Haggai Erand16e91d2016-02-29 15:45:05 +02002091 case IB_QPT_GSI:
2092 return mlx5_ib_gsi_create_qp(pd, init_attr);
2093
Eli Cohene126ba92013-07-07 17:25:49 +03002094 case IB_QPT_RAW_IPV6:
2095 case IB_QPT_RAW_ETHERTYPE:
Eli Cohene126ba92013-07-07 17:25:49 +03002096 case IB_QPT_MAX:
2097 default:
2098 mlx5_ib_dbg(dev, "unsupported qp type %d\n",
2099 init_attr->qp_type);
2100 /* Don't support raw QPs */
2101 return ERR_PTR(-EINVAL);
2102 }
2103
2104 return &qp->ibqp;
2105}
2106
2107int mlx5_ib_destroy_qp(struct ib_qp *qp)
2108{
2109 struct mlx5_ib_dev *dev = to_mdev(qp->device);
2110 struct mlx5_ib_qp *mqp = to_mqp(qp);
2111
Haggai Erand16e91d2016-02-29 15:45:05 +02002112 if (unlikely(qp->qp_type == IB_QPT_GSI))
2113 return mlx5_ib_gsi_destroy_qp(qp);
2114
Eli Cohene126ba92013-07-07 17:25:49 +03002115 destroy_qp_common(dev, mqp);
2116
2117 kfree(mqp);
2118
2119 return 0;
2120}
2121
2122static __be32 to_mlx5_access_flags(struct mlx5_ib_qp *qp, const struct ib_qp_attr *attr,
2123 int attr_mask)
2124{
2125 u32 hw_access_flags = 0;
2126 u8 dest_rd_atomic;
2127 u32 access_flags;
2128
2129 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
2130 dest_rd_atomic = attr->max_dest_rd_atomic;
2131 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002132 dest_rd_atomic = qp->trans_qp.resp_depth;
Eli Cohene126ba92013-07-07 17:25:49 +03002133
2134 if (attr_mask & IB_QP_ACCESS_FLAGS)
2135 access_flags = attr->qp_access_flags;
2136 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002137 access_flags = qp->trans_qp.atomic_rd_en;
Eli Cohene126ba92013-07-07 17:25:49 +03002138
2139 if (!dest_rd_atomic)
2140 access_flags &= IB_ACCESS_REMOTE_WRITE;
2141
2142 if (access_flags & IB_ACCESS_REMOTE_READ)
2143 hw_access_flags |= MLX5_QP_BIT_RRE;
2144 if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
2145 hw_access_flags |= (MLX5_QP_BIT_RAE | MLX5_ATOMIC_MODE_CX);
2146 if (access_flags & IB_ACCESS_REMOTE_WRITE)
2147 hw_access_flags |= MLX5_QP_BIT_RWE;
2148
2149 return cpu_to_be32(hw_access_flags);
2150}
2151
2152enum {
2153 MLX5_PATH_FLAG_FL = 1 << 0,
2154 MLX5_PATH_FLAG_FREE_AR = 1 << 1,
2155 MLX5_PATH_FLAG_COUNTER = 1 << 2,
2156};
2157
2158static int ib_rate_to_mlx5(struct mlx5_ib_dev *dev, u8 rate)
2159{
2160 if (rate == IB_RATE_PORT_CURRENT) {
2161 return 0;
2162 } else if (rate < IB_RATE_2_5_GBPS || rate > IB_RATE_300_GBPS) {
2163 return -EINVAL;
2164 } else {
2165 while (rate != IB_RATE_2_5_GBPS &&
2166 !(1 << (rate + MLX5_STAT_RATE_OFFSET) &
Saeed Mahameed938fe832015-05-28 22:28:41 +03002167 MLX5_CAP_GEN(dev->mdev, stat_rate_support)))
Eli Cohene126ba92013-07-07 17:25:49 +03002168 --rate;
2169 }
2170
2171 return rate + MLX5_STAT_RATE_OFFSET;
2172}
2173
majd@mellanox.com75850d02016-01-14 19:13:06 +02002174static int modify_raw_packet_eth_prio(struct mlx5_core_dev *dev,
2175 struct mlx5_ib_sq *sq, u8 sl)
2176{
2177 void *in;
2178 void *tisc;
2179 int inlen;
2180 int err;
2181
2182 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
2183 in = mlx5_vzalloc(inlen);
2184 if (!in)
2185 return -ENOMEM;
2186
2187 MLX5_SET(modify_tis_in, in, bitmask.prio, 1);
2188
2189 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2190 MLX5_SET(tisc, tisc, prio, ((sl & 0x7) << 1));
2191
2192 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2193
2194 kvfree(in);
2195
2196 return err;
2197}
2198
Aviv Heller13eab212016-09-18 20:48:04 +03002199static int modify_raw_packet_tx_affinity(struct mlx5_core_dev *dev,
2200 struct mlx5_ib_sq *sq, u8 tx_affinity)
2201{
2202 void *in;
2203 void *tisc;
2204 int inlen;
2205 int err;
2206
2207 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
2208 in = mlx5_vzalloc(inlen);
2209 if (!in)
2210 return -ENOMEM;
2211
2212 MLX5_SET(modify_tis_in, in, bitmask.lag_tx_port_affinity, 1);
2213
2214 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2215 MLX5_SET(tisc, tisc, lag_tx_port_affinity, tx_affinity);
2216
2217 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2218
2219 kvfree(in);
2220
2221 return err;
2222}
2223
majd@mellanox.com75850d02016-01-14 19:13:06 +02002224static int mlx5_set_path(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
2225 const struct ib_ah_attr *ah,
Eli Cohene126ba92013-07-07 17:25:49 +03002226 struct mlx5_qp_path *path, u8 port, int attr_mask,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002227 u32 path_flags, const struct ib_qp_attr *attr,
2228 bool alt)
Eli Cohene126ba92013-07-07 17:25:49 +03002229{
Achiad Shochat2811ba52015-12-23 18:47:24 +02002230 enum rdma_link_layer ll = rdma_port_get_link_layer(&dev->ib_dev, port);
Eli Cohene126ba92013-07-07 17:25:49 +03002231 int err;
2232
Eli Cohene126ba92013-07-07 17:25:49 +03002233 if (attr_mask & IB_QP_PKEY_INDEX)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002234 path->pkey_index = cpu_to_be16(alt ? attr->alt_pkey_index :
2235 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002236
Eli Cohene126ba92013-07-07 17:25:49 +03002237 if (ah->ah_flags & IB_AH_GRH) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03002238 if (ah->grh.sgid_index >=
2239 dev->mdev->port_caps[port - 1].gid_table_len) {
Joe Perchesf4f01b52015-05-08 15:58:07 -07002240 pr_err("sgid_index (%u) too large. max is %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +03002241 ah->grh.sgid_index,
2242 dev->mdev->port_caps[port - 1].gid_table_len);
Eli Cohenf83b4262014-09-14 16:47:54 +03002243 return -EINVAL;
2244 }
Achiad Shochat2811ba52015-12-23 18:47:24 +02002245 }
2246
2247 if (ll == IB_LINK_LAYER_ETHERNET) {
2248 if (!(ah->ah_flags & IB_AH_GRH))
2249 return -EINVAL;
2250 memcpy(path->rmac, ah->dmac, sizeof(ah->dmac));
2251 path->udp_sport = mlx5_get_roce_udp_sport(dev, port,
2252 ah->grh.sgid_index);
2253 path->dci_cfi_prio_sl = (ah->sl & 0x7) << 4;
2254 } else {
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03002255 path->fl_free_ar = (path_flags & MLX5_PATH_FLAG_FL) ? 0x80 : 0;
2256 path->fl_free_ar |=
2257 (path_flags & MLX5_PATH_FLAG_FREE_AR) ? 0x40 : 0;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002258 path->rlid = cpu_to_be16(ah->dlid);
2259 path->grh_mlid = ah->src_path_bits & 0x7f;
2260 if (ah->ah_flags & IB_AH_GRH)
2261 path->grh_mlid |= 1 << 7;
2262 path->dci_cfi_prio_sl = ah->sl & 0xf;
2263 }
2264
2265 if (ah->ah_flags & IB_AH_GRH) {
Eli Cohene126ba92013-07-07 17:25:49 +03002266 path->mgid_index = ah->grh.sgid_index;
2267 path->hop_limit = ah->grh.hop_limit;
2268 path->tclass_flowlabel =
2269 cpu_to_be32((ah->grh.traffic_class << 20) |
2270 (ah->grh.flow_label));
2271 memcpy(path->rgid, ah->grh.dgid.raw, 16);
2272 }
2273
2274 err = ib_rate_to_mlx5(dev, ah->static_rate);
2275 if (err < 0)
2276 return err;
2277 path->static_rate = err;
2278 path->port = port;
2279
Eli Cohene126ba92013-07-07 17:25:49 +03002280 if (attr_mask & IB_QP_TIMEOUT)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002281 path->ackto_lt = (alt ? attr->alt_timeout : attr->timeout) << 3;
Eli Cohene126ba92013-07-07 17:25:49 +03002282
majd@mellanox.com75850d02016-01-14 19:13:06 +02002283 if ((qp->ibqp.qp_type == IB_QPT_RAW_PACKET) && qp->sq.wqe_cnt)
2284 return modify_raw_packet_eth_prio(dev->mdev,
2285 &qp->raw_packet_qp.sq,
2286 ah->sl & 0xf);
2287
Eli Cohene126ba92013-07-07 17:25:49 +03002288 return 0;
2289}
2290
2291static enum mlx5_qp_optpar opt_mask[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE][MLX5_QP_ST_MAX] = {
2292 [MLX5_QP_STATE_INIT] = {
2293 [MLX5_QP_STATE_INIT] = {
2294 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2295 MLX5_QP_OPTPAR_RAE |
2296 MLX5_QP_OPTPAR_RWE |
2297 MLX5_QP_OPTPAR_PKEY_INDEX |
2298 MLX5_QP_OPTPAR_PRI_PORT,
2299 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
2300 MLX5_QP_OPTPAR_PKEY_INDEX |
2301 MLX5_QP_OPTPAR_PRI_PORT,
2302 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2303 MLX5_QP_OPTPAR_Q_KEY |
2304 MLX5_QP_OPTPAR_PRI_PORT,
2305 },
2306 [MLX5_QP_STATE_RTR] = {
2307 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2308 MLX5_QP_OPTPAR_RRE |
2309 MLX5_QP_OPTPAR_RAE |
2310 MLX5_QP_OPTPAR_RWE |
2311 MLX5_QP_OPTPAR_PKEY_INDEX,
2312 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2313 MLX5_QP_OPTPAR_RWE |
2314 MLX5_QP_OPTPAR_PKEY_INDEX,
2315 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2316 MLX5_QP_OPTPAR_Q_KEY,
2317 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_PKEY_INDEX |
2318 MLX5_QP_OPTPAR_Q_KEY,
Eli Cohena4774e92013-09-11 16:35:32 +03002319 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2320 MLX5_QP_OPTPAR_RRE |
2321 MLX5_QP_OPTPAR_RAE |
2322 MLX5_QP_OPTPAR_RWE |
2323 MLX5_QP_OPTPAR_PKEY_INDEX,
Eli Cohene126ba92013-07-07 17:25:49 +03002324 },
2325 },
2326 [MLX5_QP_STATE_RTR] = {
2327 [MLX5_QP_STATE_RTS] = {
2328 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2329 MLX5_QP_OPTPAR_RRE |
2330 MLX5_QP_OPTPAR_RAE |
2331 MLX5_QP_OPTPAR_RWE |
2332 MLX5_QP_OPTPAR_PM_STATE |
2333 MLX5_QP_OPTPAR_RNR_TIMEOUT,
2334 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2335 MLX5_QP_OPTPAR_RWE |
2336 MLX5_QP_OPTPAR_PM_STATE,
2337 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
2338 },
2339 },
2340 [MLX5_QP_STATE_RTS] = {
2341 [MLX5_QP_STATE_RTS] = {
2342 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2343 MLX5_QP_OPTPAR_RAE |
2344 MLX5_QP_OPTPAR_RWE |
2345 MLX5_QP_OPTPAR_RNR_TIMEOUT |
Eli Cohenc2a34312013-10-24 12:01:02 +03002346 MLX5_QP_OPTPAR_PM_STATE |
2347 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03002348 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
Eli Cohenc2a34312013-10-24 12:01:02 +03002349 MLX5_QP_OPTPAR_PM_STATE |
2350 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03002351 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY |
2352 MLX5_QP_OPTPAR_SRQN |
2353 MLX5_QP_OPTPAR_CQN_RCV,
2354 },
2355 },
2356 [MLX5_QP_STATE_SQER] = {
2357 [MLX5_QP_STATE_RTS] = {
2358 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
2359 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_Q_KEY,
Eli Cohen75959f52013-09-11 16:35:31 +03002360 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE,
Eli Cohena4774e92013-09-11 16:35:32 +03002361 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
2362 MLX5_QP_OPTPAR_RWE |
2363 MLX5_QP_OPTPAR_RAE |
2364 MLX5_QP_OPTPAR_RRE,
Eli Cohene126ba92013-07-07 17:25:49 +03002365 },
2366 },
2367};
2368
2369static int ib_nr_to_mlx5_nr(int ib_mask)
2370{
2371 switch (ib_mask) {
2372 case IB_QP_STATE:
2373 return 0;
2374 case IB_QP_CUR_STATE:
2375 return 0;
2376 case IB_QP_EN_SQD_ASYNC_NOTIFY:
2377 return 0;
2378 case IB_QP_ACCESS_FLAGS:
2379 return MLX5_QP_OPTPAR_RWE | MLX5_QP_OPTPAR_RRE |
2380 MLX5_QP_OPTPAR_RAE;
2381 case IB_QP_PKEY_INDEX:
2382 return MLX5_QP_OPTPAR_PKEY_INDEX;
2383 case IB_QP_PORT:
2384 return MLX5_QP_OPTPAR_PRI_PORT;
2385 case IB_QP_QKEY:
2386 return MLX5_QP_OPTPAR_Q_KEY;
2387 case IB_QP_AV:
2388 return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH |
2389 MLX5_QP_OPTPAR_PRI_PORT;
2390 case IB_QP_PATH_MTU:
2391 return 0;
2392 case IB_QP_TIMEOUT:
2393 return MLX5_QP_OPTPAR_ACK_TIMEOUT;
2394 case IB_QP_RETRY_CNT:
2395 return MLX5_QP_OPTPAR_RETRY_COUNT;
2396 case IB_QP_RNR_RETRY:
2397 return MLX5_QP_OPTPAR_RNR_RETRY;
2398 case IB_QP_RQ_PSN:
2399 return 0;
2400 case IB_QP_MAX_QP_RD_ATOMIC:
2401 return MLX5_QP_OPTPAR_SRA_MAX;
2402 case IB_QP_ALT_PATH:
2403 return MLX5_QP_OPTPAR_ALT_ADDR_PATH;
2404 case IB_QP_MIN_RNR_TIMER:
2405 return MLX5_QP_OPTPAR_RNR_TIMEOUT;
2406 case IB_QP_SQ_PSN:
2407 return 0;
2408 case IB_QP_MAX_DEST_RD_ATOMIC:
2409 return MLX5_QP_OPTPAR_RRA_MAX | MLX5_QP_OPTPAR_RWE |
2410 MLX5_QP_OPTPAR_RRE | MLX5_QP_OPTPAR_RAE;
2411 case IB_QP_PATH_MIG_STATE:
2412 return MLX5_QP_OPTPAR_PM_STATE;
2413 case IB_QP_CAP:
2414 return 0;
2415 case IB_QP_DEST_QPN:
2416 return 0;
2417 }
2418 return 0;
2419}
2420
2421static int ib_mask_to_mlx5_opt(int ib_mask)
2422{
2423 int result = 0;
2424 int i;
2425
2426 for (i = 0; i < 8 * sizeof(int); i++) {
2427 if ((1 << i) & ib_mask)
2428 result |= ib_nr_to_mlx5_nr(1 << i);
2429 }
2430
2431 return result;
2432}
2433
Alex Veskereb49ab02016-08-28 12:25:53 +03002434static int modify_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
2435 struct mlx5_ib_rq *rq, int new_state,
2436 const struct mlx5_modify_raw_qp_param *raw_qp_param)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002437{
2438 void *in;
2439 void *rqc;
2440 int inlen;
2441 int err;
2442
2443 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
2444 in = mlx5_vzalloc(inlen);
2445 if (!in)
2446 return -ENOMEM;
2447
2448 MLX5_SET(modify_rq_in, in, rq_state, rq->state);
2449
2450 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
2451 MLX5_SET(rqc, rqc, state, new_state);
2452
Alex Veskereb49ab02016-08-28 12:25:53 +03002453 if (raw_qp_param->set_mask & MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID) {
2454 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
2455 MLX5_SET64(modify_rq_in, in, modify_bitmask,
2456 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_MODIFY_RQ_COUNTER_SET_ID);
2457 MLX5_SET(rqc, rqc, counter_set_id, raw_qp_param->rq_q_ctr_id);
2458 } else
2459 pr_info_once("%s: RAW PACKET QP counters are not supported on current FW\n",
2460 dev->ib_dev.name);
2461 }
2462
2463 err = mlx5_core_modify_rq(dev->mdev, rq->base.mqp.qpn, in, inlen);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002464 if (err)
2465 goto out;
2466
2467 rq->state = new_state;
2468
2469out:
2470 kvfree(in);
2471 return err;
2472}
2473
2474static int modify_raw_packet_qp_sq(struct mlx5_core_dev *dev,
Bodong Wang7d29f342016-12-01 13:43:16 +02002475 struct mlx5_ib_sq *sq,
2476 int new_state,
2477 const struct mlx5_modify_raw_qp_param *raw_qp_param)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002478{
Bodong Wang7d29f342016-12-01 13:43:16 +02002479 struct mlx5_ib_qp *ibqp = sq->base.container_mibqp;
2480 u32 old_rate = ibqp->rate_limit;
2481 u32 new_rate = old_rate;
2482 u16 rl_index = 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002483 void *in;
2484 void *sqc;
2485 int inlen;
2486 int err;
2487
2488 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
2489 in = mlx5_vzalloc(inlen);
2490 if (!in)
2491 return -ENOMEM;
2492
2493 MLX5_SET(modify_sq_in, in, sq_state, sq->state);
2494
2495 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
2496 MLX5_SET(sqc, sqc, state, new_state);
2497
Bodong Wang7d29f342016-12-01 13:43:16 +02002498 if (raw_qp_param->set_mask & MLX5_RAW_QP_RATE_LIMIT) {
2499 if (new_state != MLX5_SQC_STATE_RDY)
2500 pr_warn("%s: Rate limit can only be changed when SQ is moving to RDY\n",
2501 __func__);
2502 else
2503 new_rate = raw_qp_param->rate_limit;
2504 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002505
Bodong Wang7d29f342016-12-01 13:43:16 +02002506 if (old_rate != new_rate) {
2507 if (new_rate) {
2508 err = mlx5_rl_add_rate(dev, new_rate, &rl_index);
2509 if (err) {
2510 pr_err("Failed configuring rate %u: %d\n",
2511 new_rate, err);
2512 goto out;
2513 }
2514 }
2515
2516 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
2517 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
2518 }
2519
2520 err = mlx5_core_modify_sq(dev, sq->base.mqp.qpn, in, inlen);
2521 if (err) {
2522 /* Remove new rate from table if failed */
2523 if (new_rate &&
2524 old_rate != new_rate)
2525 mlx5_rl_remove_rate(dev, new_rate);
2526 goto out;
2527 }
2528
2529 /* Only remove the old rate after new rate was set */
2530 if ((old_rate &&
2531 (old_rate != new_rate)) ||
2532 (new_state != MLX5_SQC_STATE_RDY))
2533 mlx5_rl_remove_rate(dev, old_rate);
2534
2535 ibqp->rate_limit = new_rate;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002536 sq->state = new_state;
2537
2538out:
2539 kvfree(in);
2540 return err;
2541}
2542
2543static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03002544 const struct mlx5_modify_raw_qp_param *raw_qp_param,
2545 u8 tx_affinity)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002546{
2547 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
2548 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
2549 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
Bodong Wang7d29f342016-12-01 13:43:16 +02002550 int modify_rq = !!qp->rq.wqe_cnt;
2551 int modify_sq = !!qp->sq.wqe_cnt;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002552 int rq_state;
2553 int sq_state;
2554 int err;
2555
Alex Vesker0680efa2016-08-28 12:25:52 +03002556 switch (raw_qp_param->operation) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002557 case MLX5_CMD_OP_RST2INIT_QP:
2558 rq_state = MLX5_RQC_STATE_RDY;
2559 sq_state = MLX5_SQC_STATE_RDY;
2560 break;
2561 case MLX5_CMD_OP_2ERR_QP:
2562 rq_state = MLX5_RQC_STATE_ERR;
2563 sq_state = MLX5_SQC_STATE_ERR;
2564 break;
2565 case MLX5_CMD_OP_2RST_QP:
2566 rq_state = MLX5_RQC_STATE_RST;
2567 sq_state = MLX5_SQC_STATE_RST;
2568 break;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002569 case MLX5_CMD_OP_RTR2RTS_QP:
2570 case MLX5_CMD_OP_RTS2RTS_QP:
Bodong Wang7d29f342016-12-01 13:43:16 +02002571 if (raw_qp_param->set_mask ==
2572 MLX5_RAW_QP_RATE_LIMIT) {
2573 modify_rq = 0;
2574 sq_state = sq->state;
2575 } else {
2576 return raw_qp_param->set_mask ? -EINVAL : 0;
2577 }
2578 break;
2579 case MLX5_CMD_OP_INIT2INIT_QP:
2580 case MLX5_CMD_OP_INIT2RTR_QP:
Alex Veskereb49ab02016-08-28 12:25:53 +03002581 if (raw_qp_param->set_mask)
2582 return -EINVAL;
2583 else
2584 return 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002585 default:
2586 WARN_ON(1);
2587 return -EINVAL;
2588 }
2589
Bodong Wang7d29f342016-12-01 13:43:16 +02002590 if (modify_rq) {
2591 err = modify_raw_packet_qp_rq(dev, rq, rq_state, raw_qp_param);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002592 if (err)
2593 return err;
2594 }
2595
Bodong Wang7d29f342016-12-01 13:43:16 +02002596 if (modify_sq) {
Aviv Heller13eab212016-09-18 20:48:04 +03002597 if (tx_affinity) {
2598 err = modify_raw_packet_tx_affinity(dev->mdev, sq,
2599 tx_affinity);
2600 if (err)
2601 return err;
2602 }
2603
Bodong Wang7d29f342016-12-01 13:43:16 +02002604 return modify_raw_packet_qp_sq(dev->mdev, sq, sq_state, raw_qp_param);
Aviv Heller13eab212016-09-18 20:48:04 +03002605 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002606
2607 return 0;
2608}
2609
Eli Cohene126ba92013-07-07 17:25:49 +03002610static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
2611 const struct ib_qp_attr *attr, int attr_mask,
2612 enum ib_qp_state cur_state, enum ib_qp_state new_state)
2613{
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002614 static const u16 optab[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE] = {
2615 [MLX5_QP_STATE_RST] = {
2616 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2617 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2618 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_RST2INIT_QP,
2619 },
2620 [MLX5_QP_STATE_INIT] = {
2621 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2622 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2623 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_INIT2INIT_QP,
2624 [MLX5_QP_STATE_RTR] = MLX5_CMD_OP_INIT2RTR_QP,
2625 },
2626 [MLX5_QP_STATE_RTR] = {
2627 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2628 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2629 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTR2RTS_QP,
2630 },
2631 [MLX5_QP_STATE_RTS] = {
2632 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2633 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2634 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTS2RTS_QP,
2635 },
2636 [MLX5_QP_STATE_SQD] = {
2637 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2638 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2639 },
2640 [MLX5_QP_STATE_SQER] = {
2641 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2642 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2643 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_SQERR2RTS_QP,
2644 },
2645 [MLX5_QP_STATE_ERR] = {
2646 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2647 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2648 }
2649 };
2650
Eli Cohene126ba92013-07-07 17:25:49 +03002651 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2652 struct mlx5_ib_qp *qp = to_mqp(ibqp);
majd@mellanox.com19098df2016-01-14 19:13:03 +02002653 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Eli Cohene126ba92013-07-07 17:25:49 +03002654 struct mlx5_ib_cq *send_cq, *recv_cq;
2655 struct mlx5_qp_context *context;
Eli Cohene126ba92013-07-07 17:25:49 +03002656 struct mlx5_ib_pd *pd;
Alex Veskereb49ab02016-08-28 12:25:53 +03002657 struct mlx5_ib_port *mibport = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002658 enum mlx5_qp_state mlx5_cur, mlx5_new;
2659 enum mlx5_qp_optpar optpar;
2660 int sqd_event;
2661 int mlx5_st;
2662 int err;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002663 u16 op;
Aviv Heller13eab212016-09-18 20:48:04 +03002664 u8 tx_affinity = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002665
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002666 context = kzalloc(sizeof(*context), GFP_KERNEL);
2667 if (!context)
Eli Cohene126ba92013-07-07 17:25:49 +03002668 return -ENOMEM;
2669
Eli Cohene126ba92013-07-07 17:25:49 +03002670 err = to_mlx5_st(ibqp->qp_type);
Haggai Eran158abf82016-02-29 15:45:04 +02002671 if (err < 0) {
2672 mlx5_ib_dbg(dev, "unsupported qp type %d\n", ibqp->qp_type);
Eli Cohene126ba92013-07-07 17:25:49 +03002673 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002674 }
Eli Cohene126ba92013-07-07 17:25:49 +03002675
2676 context->flags = cpu_to_be32(err << 16);
2677
2678 if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
2679 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
2680 } else {
2681 switch (attr->path_mig_state) {
2682 case IB_MIG_MIGRATED:
2683 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
2684 break;
2685 case IB_MIG_REARM:
2686 context->flags |= cpu_to_be32(MLX5_QP_PM_REARM << 11);
2687 break;
2688 case IB_MIG_ARMED:
2689 context->flags |= cpu_to_be32(MLX5_QP_PM_ARMED << 11);
2690 break;
2691 }
2692 }
2693
Aviv Heller13eab212016-09-18 20:48:04 +03002694 if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
2695 if ((ibqp->qp_type == IB_QPT_RC) ||
2696 (ibqp->qp_type == IB_QPT_UD &&
2697 !(qp->flags & MLX5_IB_QP_SQPN_QP1)) ||
2698 (ibqp->qp_type == IB_QPT_UC) ||
2699 (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
2700 (ibqp->qp_type == IB_QPT_XRC_INI) ||
2701 (ibqp->qp_type == IB_QPT_XRC_TGT)) {
2702 if (mlx5_lag_is_active(dev->mdev)) {
2703 tx_affinity = (unsigned int)atomic_add_return(1,
2704 &dev->roce.next_port) %
2705 MLX5_MAX_PORTS + 1;
2706 context->flags |= cpu_to_be32(tx_affinity << 24);
2707 }
2708 }
2709 }
2710
Haggai Erand16e91d2016-02-29 15:45:05 +02002711 if (is_sqp(ibqp->qp_type)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002712 context->mtu_msgmax = (IB_MTU_256 << 5) | 8;
2713 } else if (ibqp->qp_type == IB_QPT_UD ||
2714 ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
2715 context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
2716 } else if (attr_mask & IB_QP_PATH_MTU) {
2717 if (attr->path_mtu < IB_MTU_256 ||
2718 attr->path_mtu > IB_MTU_4096) {
2719 mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
2720 err = -EINVAL;
2721 goto out;
2722 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03002723 context->mtu_msgmax = (attr->path_mtu << 5) |
2724 (u8)MLX5_CAP_GEN(dev->mdev, log_max_msg);
Eli Cohene126ba92013-07-07 17:25:49 +03002725 }
2726
2727 if (attr_mask & IB_QP_DEST_QPN)
2728 context->log_pg_sz_remote_qpn = cpu_to_be32(attr->dest_qp_num);
2729
2730 if (attr_mask & IB_QP_PKEY_INDEX)
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03002731 context->pri_path.pkey_index = cpu_to_be16(attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002732
2733 /* todo implement counter_index functionality */
2734
2735 if (is_sqp(ibqp->qp_type))
2736 context->pri_path.port = qp->port;
2737
2738 if (attr_mask & IB_QP_PORT)
2739 context->pri_path.port = attr->port_num;
2740
2741 if (attr_mask & IB_QP_AV) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02002742 err = mlx5_set_path(dev, qp, &attr->ah_attr, &context->pri_path,
Eli Cohene126ba92013-07-07 17:25:49 +03002743 attr_mask & IB_QP_PORT ? attr->port_num : qp->port,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002744 attr_mask, 0, attr, false);
Eli Cohene126ba92013-07-07 17:25:49 +03002745 if (err)
2746 goto out;
2747 }
2748
2749 if (attr_mask & IB_QP_TIMEOUT)
2750 context->pri_path.ackto_lt |= attr->timeout << 3;
2751
2752 if (attr_mask & IB_QP_ALT_PATH) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02002753 err = mlx5_set_path(dev, qp, &attr->alt_ah_attr,
2754 &context->alt_path,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002755 attr->alt_port_num,
2756 attr_mask | IB_QP_PKEY_INDEX | IB_QP_TIMEOUT,
2757 0, attr, true);
Eli Cohene126ba92013-07-07 17:25:49 +03002758 if (err)
2759 goto out;
2760 }
2761
2762 pd = get_pd(qp);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002763 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
2764 &send_cq, &recv_cq);
Eli Cohene126ba92013-07-07 17:25:49 +03002765
2766 context->flags_pd = cpu_to_be32(pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
2767 context->cqn_send = send_cq ? cpu_to_be32(send_cq->mcq.cqn) : 0;
2768 context->cqn_recv = recv_cq ? cpu_to_be32(recv_cq->mcq.cqn) : 0;
2769 context->params1 = cpu_to_be32(MLX5_IB_ACK_REQ_FREQ << 28);
2770
2771 if (attr_mask & IB_QP_RNR_RETRY)
2772 context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
2773
2774 if (attr_mask & IB_QP_RETRY_CNT)
2775 context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
2776
2777 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
2778 if (attr->max_rd_atomic)
2779 context->params1 |=
2780 cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
2781 }
2782
2783 if (attr_mask & IB_QP_SQ_PSN)
2784 context->next_send_psn = cpu_to_be32(attr->sq_psn);
2785
2786 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
2787 if (attr->max_dest_rd_atomic)
2788 context->params2 |=
2789 cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
2790 }
2791
2792 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC))
2793 context->params2 |= to_mlx5_access_flags(qp, attr, attr_mask);
2794
2795 if (attr_mask & IB_QP_MIN_RNR_TIMER)
2796 context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
2797
2798 if (attr_mask & IB_QP_RQ_PSN)
2799 context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
2800
2801 if (attr_mask & IB_QP_QKEY)
2802 context->qkey = cpu_to_be32(attr->qkey);
2803
2804 if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
2805 context->db_rec_addr = cpu_to_be64(qp->db.dma);
2806
2807 if (cur_state == IB_QPS_RTS && new_state == IB_QPS_SQD &&
2808 attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY && attr->en_sqd_async_notify)
2809 sqd_event = 1;
2810 else
2811 sqd_event = 0;
2812
Mark Bloch0837e862016-06-17 15:10:55 +03002813 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
2814 u8 port_num = (attr_mask & IB_QP_PORT ? attr->port_num :
2815 qp->port) - 1;
Alex Veskereb49ab02016-08-28 12:25:53 +03002816 mibport = &dev->port[port_num];
Mark Bloch0837e862016-06-17 15:10:55 +03002817 context->qp_counter_set_usr_page |=
Alex Vesker321a9e32016-07-13 16:25:11 +03002818 cpu_to_be32((u32)(mibport->q_cnt_id) << 24);
Mark Bloch0837e862016-06-17 15:10:55 +03002819 }
2820
Eli Cohene126ba92013-07-07 17:25:49 +03002821 if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
2822 context->sq_crq_size |= cpu_to_be16(1 << 4);
2823
Haggai Eranb11a4f92016-02-29 15:45:03 +02002824 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
2825 context->deth_sqpn = cpu_to_be32(1);
Eli Cohene126ba92013-07-07 17:25:49 +03002826
2827 mlx5_cur = to_mlx5_state(cur_state);
2828 mlx5_new = to_mlx5_state(new_state);
2829 mlx5_st = to_mlx5_st(ibqp->qp_type);
Eli Cohen07c91132013-10-24 12:01:01 +03002830 if (mlx5_st < 0)
Eli Cohene126ba92013-07-07 17:25:49 +03002831 goto out;
2832
Haggai Eran6aec21f2014-12-11 17:04:23 +02002833 /* If moving to a reset or error state, we must disable page faults on
2834 * this QP and flush all current page faults. Otherwise a stale page
2835 * fault may attempt to work on this QP after it is reset and moved
2836 * again to RTS, and may cause the driver and the device to get out of
2837 * sync. */
2838 if (cur_state != IB_QPS_RESET && cur_state != IB_QPS_ERR &&
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002839 (new_state == IB_QPS_RESET || new_state == IB_QPS_ERR) &&
2840 (qp->ibqp.qp_type != IB_QPT_RAW_PACKET))
Haggai Eran6aec21f2014-12-11 17:04:23 +02002841 mlx5_ib_qp_disable_pagefaults(qp);
2842
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002843 if (mlx5_cur >= MLX5_QP_NUM_STATE || mlx5_new >= MLX5_QP_NUM_STATE ||
2844 !optab[mlx5_cur][mlx5_new])
2845 goto out;
2846
2847 op = optab[mlx5_cur][mlx5_new];
Eli Cohene126ba92013-07-07 17:25:49 +03002848 optpar = ib_mask_to_mlx5_opt(attr_mask);
2849 optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002850
Alex Vesker0680efa2016-08-28 12:25:52 +03002851 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET) {
2852 struct mlx5_modify_raw_qp_param raw_qp_param = {};
2853
2854 raw_qp_param.operation = op;
Alex Veskereb49ab02016-08-28 12:25:53 +03002855 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
2856 raw_qp_param.rq_q_ctr_id = mibport->q_cnt_id;
2857 raw_qp_param.set_mask |= MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID;
2858 }
Bodong Wang7d29f342016-12-01 13:43:16 +02002859
2860 if (attr_mask & IB_QP_RATE_LIMIT) {
2861 raw_qp_param.rate_limit = attr->rate_limit;
2862 raw_qp_param.set_mask |= MLX5_RAW_QP_RATE_LIMIT;
2863 }
2864
Aviv Heller13eab212016-09-18 20:48:04 +03002865 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, tx_affinity);
Alex Vesker0680efa2016-08-28 12:25:52 +03002866 } else {
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002867 err = mlx5_core_qp_modify(dev->mdev, op, optpar, context,
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002868 &base->mqp);
Alex Vesker0680efa2016-08-28 12:25:52 +03002869 }
2870
Eli Cohene126ba92013-07-07 17:25:49 +03002871 if (err)
2872 goto out;
2873
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002874 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT &&
2875 (qp->ibqp.qp_type != IB_QPT_RAW_PACKET))
Haggai Eran6aec21f2014-12-11 17:04:23 +02002876 mlx5_ib_qp_enable_pagefaults(qp);
2877
Eli Cohene126ba92013-07-07 17:25:49 +03002878 qp->state = new_state;
2879
2880 if (attr_mask & IB_QP_ACCESS_FLAGS)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002881 qp->trans_qp.atomic_rd_en = attr->qp_access_flags;
Eli Cohene126ba92013-07-07 17:25:49 +03002882 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002883 qp->trans_qp.resp_depth = attr->max_dest_rd_atomic;
Eli Cohene126ba92013-07-07 17:25:49 +03002884 if (attr_mask & IB_QP_PORT)
2885 qp->port = attr->port_num;
2886 if (attr_mask & IB_QP_ALT_PATH)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002887 qp->trans_qp.alt_port = attr->alt_port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03002888
2889 /*
2890 * If we moved a kernel QP to RESET, clean up all old CQ
2891 * entries and reinitialize the QP.
2892 */
2893 if (new_state == IB_QPS_RESET && !ibqp->uobject) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002894 mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03002895 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
2896 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002897 mlx5_ib_cq_clean(send_cq, base->mqp.qpn, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002898
2899 qp->rq.head = 0;
2900 qp->rq.tail = 0;
2901 qp->sq.head = 0;
2902 qp->sq.tail = 0;
2903 qp->sq.cur_post = 0;
2904 qp->sq.last_poll = 0;
2905 qp->db.db[MLX5_RCV_DBR] = 0;
2906 qp->db.db[MLX5_SND_DBR] = 0;
2907 }
2908
2909out:
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002910 kfree(context);
Eli Cohene126ba92013-07-07 17:25:49 +03002911 return err;
2912}
2913
2914int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2915 int attr_mask, struct ib_udata *udata)
2916{
2917 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2918 struct mlx5_ib_qp *qp = to_mqp(ibqp);
Haggai Erand16e91d2016-02-29 15:45:05 +02002919 enum ib_qp_type qp_type;
Eli Cohene126ba92013-07-07 17:25:49 +03002920 enum ib_qp_state cur_state, new_state;
2921 int err = -EINVAL;
2922 int port;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002923 enum rdma_link_layer ll = IB_LINK_LAYER_UNSPECIFIED;
Eli Cohene126ba92013-07-07 17:25:49 +03002924
Yishai Hadas28d61372016-05-23 15:20:56 +03002925 if (ibqp->rwq_ind_tbl)
2926 return -ENOSYS;
2927
Haggai Erand16e91d2016-02-29 15:45:05 +02002928 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
2929 return mlx5_ib_gsi_modify_qp(ibqp, attr, attr_mask);
2930
2931 qp_type = (unlikely(ibqp->qp_type == MLX5_IB_QPT_HW_GSI)) ?
2932 IB_QPT_GSI : ibqp->qp_type;
2933
Eli Cohene126ba92013-07-07 17:25:49 +03002934 mutex_lock(&qp->mutex);
2935
2936 cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
2937 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
2938
Achiad Shochat2811ba52015-12-23 18:47:24 +02002939 if (!(cur_state == new_state && cur_state == IB_QPS_RESET)) {
2940 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
2941 ll = dev->ib_dev.get_link_layer(&dev->ib_dev, port);
2942 }
2943
Haggai Erand16e91d2016-02-29 15:45:05 +02002944 if (qp_type != MLX5_IB_QPT_REG_UMR &&
2945 !ib_modify_qp_is_ok(cur_state, new_state, qp_type, attr_mask, ll)) {
Haggai Eran158abf82016-02-29 15:45:04 +02002946 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
2947 cur_state, new_state, ibqp->qp_type, attr_mask);
Eli Cohene126ba92013-07-07 17:25:49 +03002948 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002949 }
Eli Cohene126ba92013-07-07 17:25:49 +03002950
2951 if ((attr_mask & IB_QP_PORT) &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03002952 (attr->port_num == 0 ||
Haggai Eran158abf82016-02-29 15:45:04 +02002953 attr->port_num > MLX5_CAP_GEN(dev->mdev, num_ports))) {
2954 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
2955 attr->port_num, dev->num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03002956 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002957 }
Eli Cohene126ba92013-07-07 17:25:49 +03002958
2959 if (attr_mask & IB_QP_PKEY_INDEX) {
2960 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Saeed Mahameed938fe832015-05-28 22:28:41 +03002961 if (attr->pkey_index >=
Haggai Eran158abf82016-02-29 15:45:04 +02002962 dev->mdev->port_caps[port - 1].pkey_table_len) {
2963 mlx5_ib_dbg(dev, "invalid pkey index %d\n",
2964 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002965 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002966 }
Eli Cohene126ba92013-07-07 17:25:49 +03002967 }
2968
2969 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03002970 attr->max_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02002971 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_res_qp))) {
2972 mlx5_ib_dbg(dev, "invalid max_rd_atomic value %d\n",
2973 attr->max_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03002974 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002975 }
Eli Cohene126ba92013-07-07 17:25:49 +03002976
2977 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03002978 attr->max_dest_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02002979 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_req_qp))) {
2980 mlx5_ib_dbg(dev, "invalid max_dest_rd_atomic value %d\n",
2981 attr->max_dest_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03002982 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002983 }
Eli Cohene126ba92013-07-07 17:25:49 +03002984
2985 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
2986 err = 0;
2987 goto out;
2988 }
2989
2990 err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
2991
2992out:
2993 mutex_unlock(&qp->mutex);
2994 return err;
2995}
2996
2997static int mlx5_wq_overflow(struct mlx5_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
2998{
2999 struct mlx5_ib_cq *cq;
3000 unsigned cur;
3001
3002 cur = wq->head - wq->tail;
3003 if (likely(cur + nreq < wq->max_post))
3004 return 0;
3005
3006 cq = to_mcq(ib_cq);
3007 spin_lock(&cq->lock);
3008 cur = wq->head - wq->tail;
3009 spin_unlock(&cq->lock);
3010
3011 return cur + nreq >= wq->max_post;
3012}
3013
3014static __always_inline void set_raddr_seg(struct mlx5_wqe_raddr_seg *rseg,
3015 u64 remote_addr, u32 rkey)
3016{
3017 rseg->raddr = cpu_to_be64(remote_addr);
3018 rseg->rkey = cpu_to_be32(rkey);
3019 rseg->reserved = 0;
3020}
3021
Erez Shitritf0313962016-02-21 16:27:17 +02003022static void *set_eth_seg(struct mlx5_wqe_eth_seg *eseg,
3023 struct ib_send_wr *wr, void *qend,
3024 struct mlx5_ib_qp *qp, int *size)
3025{
3026 void *seg = eseg;
3027
3028 memset(eseg, 0, sizeof(struct mlx5_wqe_eth_seg));
3029
3030 if (wr->send_flags & IB_SEND_IP_CSUM)
3031 eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM |
3032 MLX5_ETH_WQE_L4_CSUM;
3033
3034 seg += sizeof(struct mlx5_wqe_eth_seg);
3035 *size += sizeof(struct mlx5_wqe_eth_seg) / 16;
3036
3037 if (wr->opcode == IB_WR_LSO) {
3038 struct ib_ud_wr *ud_wr = container_of(wr, struct ib_ud_wr, wr);
3039 int size_of_inl_hdr_start = sizeof(eseg->inline_hdr_start);
3040 u64 left, leftlen, copysz;
3041 void *pdata = ud_wr->header;
3042
3043 left = ud_wr->hlen;
3044 eseg->mss = cpu_to_be16(ud_wr->mss);
3045 eseg->inline_hdr_sz = cpu_to_be16(left);
3046
3047 /*
3048 * check if there is space till the end of queue, if yes,
3049 * copy all in one shot, otherwise copy till the end of queue,
3050 * rollback and than the copy the left
3051 */
3052 leftlen = qend - (void *)eseg->inline_hdr_start;
3053 copysz = min_t(u64, leftlen, left);
3054
3055 memcpy(seg - size_of_inl_hdr_start, pdata, copysz);
3056
3057 if (likely(copysz > size_of_inl_hdr_start)) {
3058 seg += ALIGN(copysz - size_of_inl_hdr_start, 16);
3059 *size += ALIGN(copysz - size_of_inl_hdr_start, 16) / 16;
3060 }
3061
3062 if (unlikely(copysz < left)) { /* the last wqe in the queue */
3063 seg = mlx5_get_send_wqe(qp, 0);
3064 left -= copysz;
3065 pdata += copysz;
3066 memcpy(seg, pdata, left);
3067 seg += ALIGN(left, 16);
3068 *size += ALIGN(left, 16) / 16;
3069 }
3070 }
3071
3072 return seg;
3073}
3074
Eli Cohene126ba92013-07-07 17:25:49 +03003075static void set_datagram_seg(struct mlx5_wqe_datagram_seg *dseg,
3076 struct ib_send_wr *wr)
3077{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003078 memcpy(&dseg->av, &to_mah(ud_wr(wr)->ah)->av, sizeof(struct mlx5_av));
3079 dseg->av.dqp_dct = cpu_to_be32(ud_wr(wr)->remote_qpn | MLX5_EXTENDED_UD_AV);
3080 dseg->av.key.qkey.qkey = cpu_to_be32(ud_wr(wr)->remote_qkey);
Eli Cohene126ba92013-07-07 17:25:49 +03003081}
3082
3083static void set_data_ptr_seg(struct mlx5_wqe_data_seg *dseg, struct ib_sge *sg)
3084{
3085 dseg->byte_count = cpu_to_be32(sg->length);
3086 dseg->lkey = cpu_to_be32(sg->lkey);
3087 dseg->addr = cpu_to_be64(sg->addr);
3088}
3089
3090static __be16 get_klm_octo(int npages)
3091{
3092 return cpu_to_be16(ALIGN(npages, 8) / 2);
3093}
3094
3095static __be64 frwr_mkey_mask(void)
3096{
3097 u64 result;
3098
3099 result = MLX5_MKEY_MASK_LEN |
3100 MLX5_MKEY_MASK_PAGE_SIZE |
3101 MLX5_MKEY_MASK_START_ADDR |
3102 MLX5_MKEY_MASK_EN_RINVAL |
3103 MLX5_MKEY_MASK_KEY |
3104 MLX5_MKEY_MASK_LR |
3105 MLX5_MKEY_MASK_LW |
3106 MLX5_MKEY_MASK_RR |
3107 MLX5_MKEY_MASK_RW |
3108 MLX5_MKEY_MASK_A |
3109 MLX5_MKEY_MASK_SMALL_FENCE |
3110 MLX5_MKEY_MASK_FREE;
3111
3112 return cpu_to_be64(result);
3113}
3114
Sagi Grimberge6631812014-02-23 14:19:11 +02003115static __be64 sig_mkey_mask(void)
3116{
3117 u64 result;
3118
3119 result = MLX5_MKEY_MASK_LEN |
3120 MLX5_MKEY_MASK_PAGE_SIZE |
3121 MLX5_MKEY_MASK_START_ADDR |
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003122 MLX5_MKEY_MASK_EN_SIGERR |
Sagi Grimberge6631812014-02-23 14:19:11 +02003123 MLX5_MKEY_MASK_EN_RINVAL |
3124 MLX5_MKEY_MASK_KEY |
3125 MLX5_MKEY_MASK_LR |
3126 MLX5_MKEY_MASK_LW |
3127 MLX5_MKEY_MASK_RR |
3128 MLX5_MKEY_MASK_RW |
3129 MLX5_MKEY_MASK_SMALL_FENCE |
3130 MLX5_MKEY_MASK_FREE |
3131 MLX5_MKEY_MASK_BSF_EN;
3132
3133 return cpu_to_be64(result);
3134}
3135
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003136static void set_reg_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr,
3137 struct mlx5_ib_mr *mr)
3138{
3139 int ndescs = mr->ndescs;
3140
3141 memset(umr, 0, sizeof(*umr));
Sagi Grimbergb005d312016-02-29 19:07:33 +02003142
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003143 if (mr->access_mode == MLX5_MKC_ACCESS_MODE_KLMS)
Sagi Grimbergb005d312016-02-29 19:07:33 +02003144 /* KLMs take twice the size of MTTs */
3145 ndescs *= 2;
3146
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003147 umr->flags = MLX5_UMR_CHECK_NOT_FREE;
3148 umr->klm_octowords = get_klm_octo(ndescs);
3149 umr->mkey_mask = frwr_mkey_mask();
3150}
3151
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003152static void set_linv_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr)
Eli Cohene126ba92013-07-07 17:25:49 +03003153{
3154 memset(umr, 0, sizeof(*umr));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003155 umr->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
Max Gurtovoy2d221582016-10-27 16:36:36 +03003156 umr->flags = MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03003157}
3158
Maor Gottlieb578e7262016-10-27 16:36:37 +03003159static __be64 get_umr_reg_mr_mask(int atomic)
Haggai Eran968e78d2014-12-11 17:04:11 +02003160{
3161 u64 result;
3162
3163 result = MLX5_MKEY_MASK_LEN |
3164 MLX5_MKEY_MASK_PAGE_SIZE |
3165 MLX5_MKEY_MASK_START_ADDR |
3166 MLX5_MKEY_MASK_PD |
3167 MLX5_MKEY_MASK_LR |
3168 MLX5_MKEY_MASK_LW |
3169 MLX5_MKEY_MASK_KEY |
3170 MLX5_MKEY_MASK_RR |
3171 MLX5_MKEY_MASK_RW |
Haggai Eran968e78d2014-12-11 17:04:11 +02003172 MLX5_MKEY_MASK_FREE;
3173
Maor Gottlieb578e7262016-10-27 16:36:37 +03003174 if (atomic)
3175 result |= MLX5_MKEY_MASK_A;
3176
Haggai Eran968e78d2014-12-11 17:04:11 +02003177 return cpu_to_be64(result);
3178}
3179
3180static __be64 get_umr_unreg_mr_mask(void)
3181{
3182 u64 result;
3183
3184 result = MLX5_MKEY_MASK_FREE;
3185
3186 return cpu_to_be64(result);
3187}
3188
3189static __be64 get_umr_update_mtt_mask(void)
3190{
3191 u64 result;
3192
3193 result = MLX5_MKEY_MASK_FREE;
3194
3195 return cpu_to_be64(result);
3196}
3197
Noa Osherovich56e11d62016-02-29 16:46:51 +02003198static __be64 get_umr_update_translation_mask(void)
3199{
3200 u64 result;
3201
3202 result = MLX5_MKEY_MASK_LEN |
3203 MLX5_MKEY_MASK_PAGE_SIZE |
3204 MLX5_MKEY_MASK_START_ADDR |
3205 MLX5_MKEY_MASK_KEY |
3206 MLX5_MKEY_MASK_FREE;
3207
3208 return cpu_to_be64(result);
3209}
3210
3211static __be64 get_umr_update_access_mask(void)
3212{
3213 u64 result;
3214
3215 result = MLX5_MKEY_MASK_LW |
3216 MLX5_MKEY_MASK_RR |
3217 MLX5_MKEY_MASK_RW |
3218 MLX5_MKEY_MASK_A |
3219 MLX5_MKEY_MASK_KEY |
3220 MLX5_MKEY_MASK_FREE;
3221
3222 return cpu_to_be64(result);
3223}
3224
3225static __be64 get_umr_update_pd_mask(void)
3226{
3227 u64 result;
3228
3229 result = MLX5_MKEY_MASK_PD |
3230 MLX5_MKEY_MASK_KEY |
3231 MLX5_MKEY_MASK_FREE;
3232
3233 return cpu_to_be64(result);
3234}
3235
Eli Cohene126ba92013-07-07 17:25:49 +03003236static void set_reg_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Maor Gottlieb578e7262016-10-27 16:36:37 +03003237 struct ib_send_wr *wr, int atomic)
Eli Cohene126ba92013-07-07 17:25:49 +03003238{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003239 struct mlx5_umr_wr *umrwr = umr_wr(wr);
Eli Cohene126ba92013-07-07 17:25:49 +03003240
3241 memset(umr, 0, sizeof(*umr));
3242
Haggai Eran968e78d2014-12-11 17:04:11 +02003243 if (wr->send_flags & MLX5_IB_SEND_UMR_FAIL_IF_FREE)
3244 umr->flags = MLX5_UMR_CHECK_FREE; /* fail if free */
3245 else
3246 umr->flags = MLX5_UMR_CHECK_NOT_FREE; /* fail if not free */
3247
Eli Cohene126ba92013-07-07 17:25:49 +03003248 if (!(wr->send_flags & MLX5_IB_SEND_UMR_UNREG)) {
Eli Cohene126ba92013-07-07 17:25:49 +03003249 umr->klm_octowords = get_klm_octo(umrwr->npages);
Haggai Eran968e78d2014-12-11 17:04:11 +02003250 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_MTT) {
3251 umr->mkey_mask = get_umr_update_mtt_mask();
3252 umr->bsf_octowords = get_klm_octo(umrwr->target.offset);
3253 umr->flags |= MLX5_UMR_TRANSLATION_OFFSET_EN;
Haggai Eran968e78d2014-12-11 17:04:11 +02003254 }
Noa Osherovich56e11d62016-02-29 16:46:51 +02003255 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION)
3256 umr->mkey_mask |= get_umr_update_translation_mask();
3257 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_ACCESS)
3258 umr->mkey_mask |= get_umr_update_access_mask();
3259 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_PD)
3260 umr->mkey_mask |= get_umr_update_pd_mask();
3261 if (!umr->mkey_mask)
Maor Gottlieb578e7262016-10-27 16:36:37 +03003262 umr->mkey_mask = get_umr_reg_mr_mask(atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03003263 } else {
Haggai Eran968e78d2014-12-11 17:04:11 +02003264 umr->mkey_mask = get_umr_unreg_mr_mask();
Eli Cohene126ba92013-07-07 17:25:49 +03003265 }
3266
3267 if (!wr->num_sge)
Haggai Eran968e78d2014-12-11 17:04:11 +02003268 umr->flags |= MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03003269}
3270
3271static u8 get_umr_flags(int acc)
3272{
3273 return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC : 0) |
3274 (acc & IB_ACCESS_REMOTE_WRITE ? MLX5_PERM_REMOTE_WRITE : 0) |
3275 (acc & IB_ACCESS_REMOTE_READ ? MLX5_PERM_REMOTE_READ : 0) |
3276 (acc & IB_ACCESS_LOCAL_WRITE ? MLX5_PERM_LOCAL_WRITE : 0) |
Sagi Grimberg2ac45932014-02-23 14:19:09 +02003277 MLX5_PERM_LOCAL_READ | MLX5_PERM_UMR_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03003278}
3279
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003280static void set_reg_mkey_seg(struct mlx5_mkey_seg *seg,
3281 struct mlx5_ib_mr *mr,
3282 u32 key, int access)
3283{
3284 int ndescs = ALIGN(mr->ndescs, 8) >> 1;
3285
3286 memset(seg, 0, sizeof(*seg));
Sagi Grimbergb005d312016-02-29 19:07:33 +02003287
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003288 if (mr->access_mode == MLX5_MKC_ACCESS_MODE_MTT)
Sagi Grimbergb005d312016-02-29 19:07:33 +02003289 seg->log2_page_size = ilog2(mr->ibmr.page_size);
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003290 else if (mr->access_mode == MLX5_MKC_ACCESS_MODE_KLMS)
Sagi Grimbergb005d312016-02-29 19:07:33 +02003291 /* KLMs take twice the size of MTTs */
3292 ndescs *= 2;
3293
3294 seg->flags = get_umr_flags(access) | mr->access_mode;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003295 seg->qpn_mkey7_0 = cpu_to_be32((key & 0xff) | 0xffffff00);
3296 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL);
3297 seg->start_addr = cpu_to_be64(mr->ibmr.iova);
3298 seg->len = cpu_to_be64(mr->ibmr.length);
3299 seg->xlt_oct_size = cpu_to_be32(ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003300}
3301
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003302static void set_linv_mkey_seg(struct mlx5_mkey_seg *seg)
Eli Cohene126ba92013-07-07 17:25:49 +03003303{
3304 memset(seg, 0, sizeof(*seg));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003305 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03003306}
3307
3308static void set_reg_mkey_segment(struct mlx5_mkey_seg *seg, struct ib_send_wr *wr)
3309{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003310 struct mlx5_umr_wr *umrwr = umr_wr(wr);
Haggai Eran968e78d2014-12-11 17:04:11 +02003311
Eli Cohene126ba92013-07-07 17:25:49 +03003312 memset(seg, 0, sizeof(*seg));
3313 if (wr->send_flags & MLX5_IB_SEND_UMR_UNREG) {
Haggai Eran968e78d2014-12-11 17:04:11 +02003314 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03003315 return;
3316 }
3317
Haggai Eran968e78d2014-12-11 17:04:11 +02003318 seg->flags = convert_access(umrwr->access_flags);
3319 if (!(wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_MTT)) {
Noa Osherovich56e11d62016-02-29 16:46:51 +02003320 if (umrwr->pd)
3321 seg->flags_pd = cpu_to_be32(to_mpd(umrwr->pd)->pdn);
Haggai Eran968e78d2014-12-11 17:04:11 +02003322 seg->start_addr = cpu_to_be64(umrwr->target.virt_addr);
3323 }
3324 seg->len = cpu_to_be64(umrwr->length);
3325 seg->log2_page_size = umrwr->page_shift;
Eli Cohen746b5582013-10-23 09:53:14 +03003326 seg->qpn_mkey7_0 = cpu_to_be32(0xffffff00 |
Haggai Eran968e78d2014-12-11 17:04:11 +02003327 mlx5_mkey_variant(umrwr->mkey));
Eli Cohene126ba92013-07-07 17:25:49 +03003328}
3329
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003330static void set_reg_data_seg(struct mlx5_wqe_data_seg *dseg,
3331 struct mlx5_ib_mr *mr,
3332 struct mlx5_ib_pd *pd)
3333{
3334 int bcount = mr->desc_size * mr->ndescs;
3335
3336 dseg->addr = cpu_to_be64(mr->desc_map);
3337 dseg->byte_count = cpu_to_be32(ALIGN(bcount, 64));
3338 dseg->lkey = cpu_to_be32(pd->ibpd.local_dma_lkey);
3339}
3340
Eli Cohene126ba92013-07-07 17:25:49 +03003341static __be32 send_ieth(struct ib_send_wr *wr)
3342{
3343 switch (wr->opcode) {
3344 case IB_WR_SEND_WITH_IMM:
3345 case IB_WR_RDMA_WRITE_WITH_IMM:
3346 return wr->ex.imm_data;
3347
3348 case IB_WR_SEND_WITH_INV:
3349 return cpu_to_be32(wr->ex.invalidate_rkey);
3350
3351 default:
3352 return 0;
3353 }
3354}
3355
3356static u8 calc_sig(void *wqe, int size)
3357{
3358 u8 *p = wqe;
3359 u8 res = 0;
3360 int i;
3361
3362 for (i = 0; i < size; i++)
3363 res ^= p[i];
3364
3365 return ~res;
3366}
3367
3368static u8 wq_sig(void *wqe)
3369{
3370 return calc_sig(wqe, (*((u8 *)wqe + 8) & 0x3f) << 4);
3371}
3372
3373static int set_data_inl_seg(struct mlx5_ib_qp *qp, struct ib_send_wr *wr,
3374 void *wqe, int *sz)
3375{
3376 struct mlx5_wqe_inline_seg *seg;
3377 void *qend = qp->sq.qend;
3378 void *addr;
3379 int inl = 0;
3380 int copy;
3381 int len;
3382 int i;
3383
3384 seg = wqe;
3385 wqe += sizeof(*seg);
3386 for (i = 0; i < wr->num_sge; i++) {
3387 addr = (void *)(unsigned long)(wr->sg_list[i].addr);
3388 len = wr->sg_list[i].length;
3389 inl += len;
3390
3391 if (unlikely(inl > qp->max_inline_data))
3392 return -ENOMEM;
3393
3394 if (unlikely(wqe + len > qend)) {
3395 copy = qend - wqe;
3396 memcpy(wqe, addr, copy);
3397 addr += copy;
3398 len -= copy;
3399 wqe = mlx5_get_send_wqe(qp, 0);
3400 }
3401 memcpy(wqe, addr, len);
3402 wqe += len;
3403 }
3404
3405 seg->byte_count = cpu_to_be32(inl | MLX5_INLINE_SEG);
3406
3407 *sz = ALIGN(inl + sizeof(seg->byte_count), 16) / 16;
3408
3409 return 0;
3410}
3411
Sagi Grimberge6631812014-02-23 14:19:11 +02003412static u16 prot_field_size(enum ib_signature_type type)
3413{
3414 switch (type) {
3415 case IB_SIG_TYPE_T10_DIF:
3416 return MLX5_DIF_SIZE;
3417 default:
3418 return 0;
3419 }
3420}
3421
3422static u8 bs_selector(int block_size)
3423{
3424 switch (block_size) {
3425 case 512: return 0x1;
3426 case 520: return 0x2;
3427 case 4096: return 0x3;
3428 case 4160: return 0x4;
3429 case 1073741824: return 0x5;
3430 default: return 0;
3431 }
3432}
3433
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003434static void mlx5_fill_inl_bsf(struct ib_sig_domain *domain,
3435 struct mlx5_bsf_inl *inl)
Sagi Grimberge6631812014-02-23 14:19:11 +02003436{
Sagi Grimberg142537f2014-08-13 19:54:32 +03003437 /* Valid inline section and allow BSF refresh */
3438 inl->vld_refresh = cpu_to_be16(MLX5_BSF_INL_VALID |
3439 MLX5_BSF_REFRESH_DIF);
3440 inl->dif_apptag = cpu_to_be16(domain->sig.dif.app_tag);
3441 inl->dif_reftag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003442 /* repeating block */
3443 inl->rp_inv_seed = MLX5_BSF_REPEAT_BLOCK;
3444 inl->sig_type = domain->sig.dif.bg_type == IB_T10DIF_CRC ?
3445 MLX5_DIF_CRC : MLX5_DIF_IPCS;
Sagi Grimberge6631812014-02-23 14:19:11 +02003446
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003447 if (domain->sig.dif.ref_remap)
3448 inl->dif_inc_ref_guard_check |= MLX5_BSF_INC_REFTAG;
Sagi Grimberge6631812014-02-23 14:19:11 +02003449
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003450 if (domain->sig.dif.app_escape) {
3451 if (domain->sig.dif.ref_escape)
3452 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPREF_ESCAPE;
3453 else
3454 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPTAG_ESCAPE;
Sagi Grimberge6631812014-02-23 14:19:11 +02003455 }
3456
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003457 inl->dif_app_bitmask_check =
3458 cpu_to_be16(domain->sig.dif.apptag_check_mask);
Sagi Grimberge6631812014-02-23 14:19:11 +02003459}
3460
3461static int mlx5_set_bsf(struct ib_mr *sig_mr,
3462 struct ib_sig_attrs *sig_attrs,
3463 struct mlx5_bsf *bsf, u32 data_size)
3464{
3465 struct mlx5_core_sig_ctx *msig = to_mmr(sig_mr)->sig;
3466 struct mlx5_bsf_basic *basic = &bsf->basic;
3467 struct ib_sig_domain *mem = &sig_attrs->mem;
3468 struct ib_sig_domain *wire = &sig_attrs->wire;
Sagi Grimberge6631812014-02-23 14:19:11 +02003469
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003470 memset(bsf, 0, sizeof(*bsf));
Sagi Grimberge6631812014-02-23 14:19:11 +02003471
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003472 /* Basic + Extended + Inline */
3473 basic->bsf_size_sbs = 1 << 7;
3474 /* Input domain check byte mask */
3475 basic->check_byte_mask = sig_attrs->check_mask;
3476 basic->raw_data_size = cpu_to_be32(data_size);
3477
3478 /* Memory domain */
3479 switch (sig_attrs->mem.sig_type) {
3480 case IB_SIG_TYPE_NONE:
3481 break;
3482 case IB_SIG_TYPE_T10_DIF:
3483 basic->mem.bs_selector = bs_selector(mem->sig.dif.pi_interval);
3484 basic->m_bfs_psv = cpu_to_be32(msig->psv_memory.psv_idx);
3485 mlx5_fill_inl_bsf(mem, &bsf->m_inl);
3486 break;
3487 default:
3488 return -EINVAL;
3489 }
3490
3491 /* Wire domain */
3492 switch (sig_attrs->wire.sig_type) {
3493 case IB_SIG_TYPE_NONE:
3494 break;
3495 case IB_SIG_TYPE_T10_DIF:
Sagi Grimberge6631812014-02-23 14:19:11 +02003496 if (mem->sig.dif.pi_interval == wire->sig.dif.pi_interval &&
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003497 mem->sig_type == wire->sig_type) {
Sagi Grimberge6631812014-02-23 14:19:11 +02003498 /* Same block structure */
Sagi Grimberg142537f2014-08-13 19:54:32 +03003499 basic->bsf_size_sbs |= 1 << 4;
Sagi Grimberge6631812014-02-23 14:19:11 +02003500 if (mem->sig.dif.bg_type == wire->sig.dif.bg_type)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003501 basic->wire.copy_byte_mask |= MLX5_CPY_GRD_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003502 if (mem->sig.dif.app_tag == wire->sig.dif.app_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003503 basic->wire.copy_byte_mask |= MLX5_CPY_APP_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003504 if (mem->sig.dif.ref_tag == wire->sig.dif.ref_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003505 basic->wire.copy_byte_mask |= MLX5_CPY_REF_MASK;
Sagi Grimberge6631812014-02-23 14:19:11 +02003506 } else
3507 basic->wire.bs_selector = bs_selector(wire->sig.dif.pi_interval);
3508
Sagi Grimberg142537f2014-08-13 19:54:32 +03003509 basic->w_bfs_psv = cpu_to_be32(msig->psv_wire.psv_idx);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003510 mlx5_fill_inl_bsf(wire, &bsf->w_inl);
Sagi Grimberge6631812014-02-23 14:19:11 +02003511 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003512 default:
3513 return -EINVAL;
3514 }
3515
3516 return 0;
3517}
3518
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003519static int set_sig_data_segment(struct ib_sig_handover_wr *wr,
3520 struct mlx5_ib_qp *qp, void **seg, int *size)
Sagi Grimberge6631812014-02-23 14:19:11 +02003521{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003522 struct ib_sig_attrs *sig_attrs = wr->sig_attrs;
3523 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003524 struct mlx5_bsf *bsf;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003525 u32 data_len = wr->wr.sg_list->length;
3526 u32 data_key = wr->wr.sg_list->lkey;
3527 u64 data_va = wr->wr.sg_list->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003528 int ret;
3529 int wqe_size;
3530
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003531 if (!wr->prot ||
3532 (data_key == wr->prot->lkey &&
3533 data_va == wr->prot->addr &&
3534 data_len == wr->prot->length)) {
Sagi Grimberge6631812014-02-23 14:19:11 +02003535 /**
3536 * Source domain doesn't contain signature information
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003537 * or data and protection are interleaved in memory.
Sagi Grimberge6631812014-02-23 14:19:11 +02003538 * So need construct:
3539 * ------------------
3540 * | data_klm |
3541 * ------------------
3542 * | BSF |
3543 * ------------------
3544 **/
3545 struct mlx5_klm *data_klm = *seg;
3546
3547 data_klm->bcount = cpu_to_be32(data_len);
3548 data_klm->key = cpu_to_be32(data_key);
3549 data_klm->va = cpu_to_be64(data_va);
3550 wqe_size = ALIGN(sizeof(*data_klm), 64);
3551 } else {
3552 /**
3553 * Source domain contains signature information
3554 * So need construct a strided block format:
3555 * ---------------------------
3556 * | stride_block_ctrl |
3557 * ---------------------------
3558 * | data_klm |
3559 * ---------------------------
3560 * | prot_klm |
3561 * ---------------------------
3562 * | BSF |
3563 * ---------------------------
3564 **/
3565 struct mlx5_stride_block_ctrl_seg *sblock_ctrl;
3566 struct mlx5_stride_block_entry *data_sentry;
3567 struct mlx5_stride_block_entry *prot_sentry;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003568 u32 prot_key = wr->prot->lkey;
3569 u64 prot_va = wr->prot->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003570 u16 block_size = sig_attrs->mem.sig.dif.pi_interval;
3571 int prot_size;
3572
3573 sblock_ctrl = *seg;
3574 data_sentry = (void *)sblock_ctrl + sizeof(*sblock_ctrl);
3575 prot_sentry = (void *)data_sentry + sizeof(*data_sentry);
3576
3577 prot_size = prot_field_size(sig_attrs->mem.sig_type);
3578 if (!prot_size) {
3579 pr_err("Bad block size given: %u\n", block_size);
3580 return -EINVAL;
3581 }
3582 sblock_ctrl->bcount_per_cycle = cpu_to_be32(block_size +
3583 prot_size);
3584 sblock_ctrl->op = cpu_to_be32(MLX5_STRIDE_BLOCK_OP);
3585 sblock_ctrl->repeat_count = cpu_to_be32(data_len / block_size);
3586 sblock_ctrl->num_entries = cpu_to_be16(2);
3587
3588 data_sentry->bcount = cpu_to_be16(block_size);
3589 data_sentry->key = cpu_to_be32(data_key);
3590 data_sentry->va = cpu_to_be64(data_va);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003591 data_sentry->stride = cpu_to_be16(block_size);
3592
Sagi Grimberge6631812014-02-23 14:19:11 +02003593 prot_sentry->bcount = cpu_to_be16(prot_size);
3594 prot_sentry->key = cpu_to_be32(prot_key);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003595 prot_sentry->va = cpu_to_be64(prot_va);
3596 prot_sentry->stride = cpu_to_be16(prot_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02003597
Sagi Grimberge6631812014-02-23 14:19:11 +02003598 wqe_size = ALIGN(sizeof(*sblock_ctrl) + sizeof(*data_sentry) +
3599 sizeof(*prot_sentry), 64);
3600 }
3601
3602 *seg += wqe_size;
3603 *size += wqe_size / 16;
3604 if (unlikely((*seg == qp->sq.qend)))
3605 *seg = mlx5_get_send_wqe(qp, 0);
3606
3607 bsf = *seg;
3608 ret = mlx5_set_bsf(sig_mr, sig_attrs, bsf, data_len);
3609 if (ret)
3610 return -EINVAL;
3611
3612 *seg += sizeof(*bsf);
3613 *size += sizeof(*bsf) / 16;
3614 if (unlikely((*seg == qp->sq.qend)))
3615 *seg = mlx5_get_send_wqe(qp, 0);
3616
3617 return 0;
3618}
3619
3620static void set_sig_mkey_segment(struct mlx5_mkey_seg *seg,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003621 struct ib_sig_handover_wr *wr, u32 nelements,
Sagi Grimberge6631812014-02-23 14:19:11 +02003622 u32 length, u32 pdn)
3623{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003624 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003625 u32 sig_key = sig_mr->rkey;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003626 u8 sigerr = to_mmr(sig_mr)->sig->sigerr_count & 1;
Sagi Grimberge6631812014-02-23 14:19:11 +02003627
3628 memset(seg, 0, sizeof(*seg));
3629
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003630 seg->flags = get_umr_flags(wr->access_flags) |
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003631 MLX5_MKC_ACCESS_MODE_KLMS;
Sagi Grimberge6631812014-02-23 14:19:11 +02003632 seg->qpn_mkey7_0 = cpu_to_be32((sig_key & 0xff) | 0xffffff00);
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003633 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL | sigerr << 26 |
Sagi Grimberge6631812014-02-23 14:19:11 +02003634 MLX5_MKEY_BSF_EN | pdn);
3635 seg->len = cpu_to_be64(length);
3636 seg->xlt_oct_size = cpu_to_be32(be16_to_cpu(get_klm_octo(nelements)));
3637 seg->bsfs_octo_size = cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE);
3638}
3639
3640static void set_sig_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003641 u32 nelements)
Sagi Grimberge6631812014-02-23 14:19:11 +02003642{
3643 memset(umr, 0, sizeof(*umr));
3644
3645 umr->flags = MLX5_FLAGS_INLINE | MLX5_FLAGS_CHECK_FREE;
3646 umr->klm_octowords = get_klm_octo(nelements);
3647 umr->bsf_octowords = cpu_to_be16(MLX5_MKEY_BSF_OCTO_SIZE);
3648 umr->mkey_mask = sig_mkey_mask();
3649}
3650
3651
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003652static int set_sig_umr_wr(struct ib_send_wr *send_wr, struct mlx5_ib_qp *qp,
Sagi Grimberge6631812014-02-23 14:19:11 +02003653 void **seg, int *size)
3654{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003655 struct ib_sig_handover_wr *wr = sig_handover_wr(send_wr);
3656 struct mlx5_ib_mr *sig_mr = to_mmr(wr->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02003657 u32 pdn = get_pd(qp)->pdn;
3658 u32 klm_oct_size;
3659 int region_len, ret;
3660
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003661 if (unlikely(wr->wr.num_sge != 1) ||
3662 unlikely(wr->access_flags & IB_ACCESS_REMOTE_ATOMIC) ||
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003663 unlikely(!sig_mr->sig) || unlikely(!qp->signature_en) ||
3664 unlikely(!sig_mr->sig->sig_status_checked))
Sagi Grimberge6631812014-02-23 14:19:11 +02003665 return -EINVAL;
3666
3667 /* length of the protected region, data + protection */
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003668 region_len = wr->wr.sg_list->length;
3669 if (wr->prot &&
3670 (wr->prot->lkey != wr->wr.sg_list->lkey ||
3671 wr->prot->addr != wr->wr.sg_list->addr ||
3672 wr->prot->length != wr->wr.sg_list->length))
3673 region_len += wr->prot->length;
Sagi Grimberge6631812014-02-23 14:19:11 +02003674
3675 /**
3676 * KLM octoword size - if protection was provided
3677 * then we use strided block format (3 octowords),
3678 * else we use single KLM (1 octoword)
3679 **/
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003680 klm_oct_size = wr->prot ? 3 : 1;
Sagi Grimberge6631812014-02-23 14:19:11 +02003681
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003682 set_sig_umr_segment(*seg, klm_oct_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02003683 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
3684 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
3685 if (unlikely((*seg == qp->sq.qend)))
3686 *seg = mlx5_get_send_wqe(qp, 0);
3687
3688 set_sig_mkey_segment(*seg, wr, klm_oct_size, region_len, pdn);
3689 *seg += sizeof(struct mlx5_mkey_seg);
3690 *size += sizeof(struct mlx5_mkey_seg) / 16;
3691 if (unlikely((*seg == qp->sq.qend)))
3692 *seg = mlx5_get_send_wqe(qp, 0);
3693
3694 ret = set_sig_data_segment(wr, qp, seg, size);
3695 if (ret)
3696 return ret;
3697
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003698 sig_mr->sig->sig_status_checked = false;
Sagi Grimberge6631812014-02-23 14:19:11 +02003699 return 0;
3700}
3701
3702static int set_psv_wr(struct ib_sig_domain *domain,
3703 u32 psv_idx, void **seg, int *size)
3704{
3705 struct mlx5_seg_set_psv *psv_seg = *seg;
3706
3707 memset(psv_seg, 0, sizeof(*psv_seg));
3708 psv_seg->psv_num = cpu_to_be32(psv_idx);
3709 switch (domain->sig_type) {
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003710 case IB_SIG_TYPE_NONE:
3711 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003712 case IB_SIG_TYPE_T10_DIF:
3713 psv_seg->transient_sig = cpu_to_be32(domain->sig.dif.bg << 16 |
3714 domain->sig.dif.app_tag);
3715 psv_seg->ref_tag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberge6631812014-02-23 14:19:11 +02003716 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003717 default:
3718 pr_err("Bad signature type given.\n");
3719 return 1;
3720 }
3721
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003722 *seg += sizeof(*psv_seg);
3723 *size += sizeof(*psv_seg) / 16;
3724
Sagi Grimberge6631812014-02-23 14:19:11 +02003725 return 0;
3726}
3727
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003728static int set_reg_wr(struct mlx5_ib_qp *qp,
3729 struct ib_reg_wr *wr,
3730 void **seg, int *size)
3731{
3732 struct mlx5_ib_mr *mr = to_mmr(wr->mr);
3733 struct mlx5_ib_pd *pd = to_mpd(qp->ibqp.pd);
3734
3735 if (unlikely(wr->wr.send_flags & IB_SEND_INLINE)) {
3736 mlx5_ib_warn(to_mdev(qp->ibqp.device),
3737 "Invalid IB_SEND_INLINE send flag\n");
3738 return -EINVAL;
3739 }
3740
3741 set_reg_umr_seg(*seg, mr);
3742 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
3743 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
3744 if (unlikely((*seg == qp->sq.qend)))
3745 *seg = mlx5_get_send_wqe(qp, 0);
3746
3747 set_reg_mkey_seg(*seg, mr, wr->key, wr->access);
3748 *seg += sizeof(struct mlx5_mkey_seg);
3749 *size += sizeof(struct mlx5_mkey_seg) / 16;
3750 if (unlikely((*seg == qp->sq.qend)))
3751 *seg = mlx5_get_send_wqe(qp, 0);
3752
3753 set_reg_data_seg(*seg, mr, pd);
3754 *seg += sizeof(struct mlx5_wqe_data_seg);
3755 *size += (sizeof(struct mlx5_wqe_data_seg) / 16);
3756
3757 return 0;
3758}
3759
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003760static void set_linv_wr(struct mlx5_ib_qp *qp, void **seg, int *size)
Eli Cohene126ba92013-07-07 17:25:49 +03003761{
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003762 set_linv_umr_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03003763 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
3764 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
3765 if (unlikely((*seg == qp->sq.qend)))
3766 *seg = mlx5_get_send_wqe(qp, 0);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003767 set_linv_mkey_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03003768 *seg += sizeof(struct mlx5_mkey_seg);
3769 *size += sizeof(struct mlx5_mkey_seg) / 16;
3770 if (unlikely((*seg == qp->sq.qend)))
3771 *seg = mlx5_get_send_wqe(qp, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03003772}
3773
3774static void dump_wqe(struct mlx5_ib_qp *qp, int idx, int size_16)
3775{
3776 __be32 *p = NULL;
3777 int tidx = idx;
3778 int i, j;
3779
3780 pr_debug("dump wqe at %p\n", mlx5_get_send_wqe(qp, tidx));
3781 for (i = 0, j = 0; i < size_16 * 4; i += 4, j += 4) {
3782 if ((i & 0xf) == 0) {
3783 void *buf = mlx5_get_send_wqe(qp, tidx);
3784 tidx = (tidx + 1) & (qp->sq.wqe_cnt - 1);
3785 p = buf;
3786 j = 0;
3787 }
3788 pr_debug("%08x %08x %08x %08x\n", be32_to_cpu(p[j]),
3789 be32_to_cpu(p[j + 1]), be32_to_cpu(p[j + 2]),
3790 be32_to_cpu(p[j + 3]));
3791 }
3792}
3793
3794static void mlx5_bf_copy(u64 __iomem *dst, u64 *src,
3795 unsigned bytecnt, struct mlx5_ib_qp *qp)
3796{
3797 while (bytecnt > 0) {
3798 __iowrite64_copy(dst++, src++, 8);
3799 __iowrite64_copy(dst++, src++, 8);
3800 __iowrite64_copy(dst++, src++, 8);
3801 __iowrite64_copy(dst++, src++, 8);
3802 __iowrite64_copy(dst++, src++, 8);
3803 __iowrite64_copy(dst++, src++, 8);
3804 __iowrite64_copy(dst++, src++, 8);
3805 __iowrite64_copy(dst++, src++, 8);
3806 bytecnt -= 64;
3807 if (unlikely(src == qp->sq.qend))
3808 src = mlx5_get_send_wqe(qp, 0);
3809 }
3810}
3811
3812static u8 get_fence(u8 fence, struct ib_send_wr *wr)
3813{
3814 if (unlikely(wr->opcode == IB_WR_LOCAL_INV &&
3815 wr->send_flags & IB_SEND_FENCE))
3816 return MLX5_FENCE_MODE_STRONG_ORDERING;
3817
3818 if (unlikely(fence)) {
3819 if (wr->send_flags & IB_SEND_FENCE)
3820 return MLX5_FENCE_MODE_SMALL_AND_FENCE;
3821 else
3822 return fence;
Eli Cohenc9b25492016-06-22 17:27:26 +03003823 } else if (unlikely(wr->send_flags & IB_SEND_FENCE)) {
3824 return MLX5_FENCE_MODE_FENCE;
Eli Cohene126ba92013-07-07 17:25:49 +03003825 }
Eli Cohenc9b25492016-06-22 17:27:26 +03003826
3827 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003828}
3829
Sagi Grimberg6e5eada2014-02-23 14:19:08 +02003830static int begin_wqe(struct mlx5_ib_qp *qp, void **seg,
3831 struct mlx5_wqe_ctrl_seg **ctrl,
Eli Cohen6a4f1392014-12-02 12:26:18 +02003832 struct ib_send_wr *wr, unsigned *idx,
Sagi Grimberg6e5eada2014-02-23 14:19:08 +02003833 int *size, int nreq)
3834{
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03003835 if (unlikely(mlx5_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)))
3836 return -ENOMEM;
Sagi Grimberg6e5eada2014-02-23 14:19:08 +02003837
3838 *idx = qp->sq.cur_post & (qp->sq.wqe_cnt - 1);
3839 *seg = mlx5_get_send_wqe(qp, *idx);
3840 *ctrl = *seg;
3841 *(uint32_t *)(*seg + 8) = 0;
3842 (*ctrl)->imm = send_ieth(wr);
3843 (*ctrl)->fm_ce_se = qp->sq_signal_bits |
3844 (wr->send_flags & IB_SEND_SIGNALED ?
3845 MLX5_WQE_CTRL_CQ_UPDATE : 0) |
3846 (wr->send_flags & IB_SEND_SOLICITED ?
3847 MLX5_WQE_CTRL_SOLICITED : 0);
3848
3849 *seg += sizeof(**ctrl);
3850 *size = sizeof(**ctrl) / 16;
3851
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03003852 return 0;
Sagi Grimberg6e5eada2014-02-23 14:19:08 +02003853}
3854
3855static void finish_wqe(struct mlx5_ib_qp *qp,
3856 struct mlx5_wqe_ctrl_seg *ctrl,
3857 u8 size, unsigned idx, u64 wr_id,
3858 int nreq, u8 fence, u8 next_fence,
3859 u32 mlx5_opcode)
3860{
3861 u8 opmod = 0;
3862
3863 ctrl->opmod_idx_opcode = cpu_to_be32(((u32)(qp->sq.cur_post) << 8) |
3864 mlx5_opcode | ((u32)opmod << 24));
majd@mellanox.com19098df2016-01-14 19:13:03 +02003865 ctrl->qpn_ds = cpu_to_be32(size | (qp->trans_qp.base.mqp.qpn << 8));
Sagi Grimberg6e5eada2014-02-23 14:19:08 +02003866 ctrl->fm_ce_se |= fence;
3867 qp->fm_cache = next_fence;
3868 if (unlikely(qp->wq_sig))
3869 ctrl->signature = wq_sig(ctrl);
3870
3871 qp->sq.wrid[idx] = wr_id;
3872 qp->sq.w_list[idx].opcode = mlx5_opcode;
3873 qp->sq.wqe_head[idx] = qp->sq.head + nreq;
3874 qp->sq.cur_post += DIV_ROUND_UP(size * 16, MLX5_SEND_WQE_BB);
3875 qp->sq.w_list[idx].next = qp->sq.cur_post;
3876}
3877
3878
Eli Cohene126ba92013-07-07 17:25:49 +03003879int mlx5_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
3880 struct ib_send_wr **bad_wr)
3881{
3882 struct mlx5_wqe_ctrl_seg *ctrl = NULL; /* compiler warning */
3883 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003884 struct mlx5_core_dev *mdev = dev->mdev;
Haggai Erand16e91d2016-02-29 15:45:05 +02003885 struct mlx5_ib_qp *qp;
Sagi Grimberge6631812014-02-23 14:19:11 +02003886 struct mlx5_ib_mr *mr;
Eli Cohene126ba92013-07-07 17:25:49 +03003887 struct mlx5_wqe_data_seg *dpseg;
3888 struct mlx5_wqe_xrc_seg *xrc;
Haggai Erand16e91d2016-02-29 15:45:05 +02003889 struct mlx5_bf *bf;
Eli Cohene126ba92013-07-07 17:25:49 +03003890 int uninitialized_var(size);
Haggai Erand16e91d2016-02-29 15:45:05 +02003891 void *qend;
Eli Cohene126ba92013-07-07 17:25:49 +03003892 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03003893 unsigned idx;
3894 int err = 0;
3895 int inl = 0;
3896 int num_sge;
3897 void *seg;
3898 int nreq;
3899 int i;
3900 u8 next_fence = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003901 u8 fence;
3902
Haggai Erand16e91d2016-02-29 15:45:05 +02003903 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
3904 return mlx5_ib_gsi_post_send(ibqp, wr, bad_wr);
3905
3906 qp = to_mqp(ibqp);
3907 bf = qp->bf;
3908 qend = qp->sq.qend;
3909
Eli Cohene126ba92013-07-07 17:25:49 +03003910 spin_lock_irqsave(&qp->sq.lock, flags);
3911
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003912 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
3913 err = -EIO;
3914 *bad_wr = wr;
3915 nreq = 0;
3916 goto out;
3917 }
3918
Eli Cohene126ba92013-07-07 17:25:49 +03003919 for (nreq = 0; wr; nreq++, wr = wr->next) {
Fabian Fredericka8f731e2014-08-12 19:20:08 -04003920 if (unlikely(wr->opcode >= ARRAY_SIZE(mlx5_ib_opcode))) {
Eli Cohene126ba92013-07-07 17:25:49 +03003921 mlx5_ib_warn(dev, "\n");
3922 err = -EINVAL;
3923 *bad_wr = wr;
3924 goto out;
3925 }
3926
Eli Cohene126ba92013-07-07 17:25:49 +03003927 fence = qp->fm_cache;
3928 num_sge = wr->num_sge;
3929 if (unlikely(num_sge > qp->sq.max_gs)) {
3930 mlx5_ib_warn(dev, "\n");
Chuck Lever24be4092016-08-28 10:58:34 +03003931 err = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03003932 *bad_wr = wr;
3933 goto out;
3934 }
3935
Sagi Grimberg6e5eada2014-02-23 14:19:08 +02003936 err = begin_wqe(qp, &seg, &ctrl, wr, &idx, &size, nreq);
3937 if (err) {
3938 mlx5_ib_warn(dev, "\n");
3939 err = -ENOMEM;
3940 *bad_wr = wr;
3941 goto out;
3942 }
Eli Cohene126ba92013-07-07 17:25:49 +03003943
3944 switch (ibqp->qp_type) {
3945 case IB_QPT_XRC_INI:
3946 xrc = seg;
Eli Cohene126ba92013-07-07 17:25:49 +03003947 seg += sizeof(*xrc);
3948 size += sizeof(*xrc) / 16;
3949 /* fall through */
3950 case IB_QPT_RC:
3951 switch (wr->opcode) {
3952 case IB_WR_RDMA_READ:
3953 case IB_WR_RDMA_WRITE:
3954 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003955 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
3956 rdma_wr(wr)->rkey);
Jack Morgensteinf241e742014-07-28 23:30:23 +03003957 seg += sizeof(struct mlx5_wqe_raddr_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03003958 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
3959 break;
3960
3961 case IB_WR_ATOMIC_CMP_AND_SWP:
3962 case IB_WR_ATOMIC_FETCH_AND_ADD:
Eli Cohene126ba92013-07-07 17:25:49 +03003963 case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
Eli Cohen81bea282013-09-11 16:35:30 +03003964 mlx5_ib_warn(dev, "Atomic operations are not supported yet\n");
3965 err = -ENOSYS;
3966 *bad_wr = wr;
3967 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03003968
3969 case IB_WR_LOCAL_INV:
3970 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
3971 qp->sq.wr_data[idx] = IB_WR_LOCAL_INV;
3972 ctrl->imm = cpu_to_be32(wr->ex.invalidate_rkey);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003973 set_linv_wr(qp, &seg, &size);
Eli Cohene126ba92013-07-07 17:25:49 +03003974 num_sge = 0;
3975 break;
3976
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003977 case IB_WR_REG_MR:
3978 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
3979 qp->sq.wr_data[idx] = IB_WR_REG_MR;
3980 ctrl->imm = cpu_to_be32(reg_wr(wr)->key);
3981 err = set_reg_wr(qp, reg_wr(wr), &seg, &size);
3982 if (err) {
3983 *bad_wr = wr;
3984 goto out;
3985 }
3986 num_sge = 0;
3987 break;
3988
Sagi Grimberge6631812014-02-23 14:19:11 +02003989 case IB_WR_REG_SIG_MR:
3990 qp->sq.wr_data[idx] = IB_WR_REG_SIG_MR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003991 mr = to_mmr(sig_handover_wr(wr)->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02003992
3993 ctrl->imm = cpu_to_be32(mr->ibmr.rkey);
3994 err = set_sig_umr_wr(wr, qp, &seg, &size);
3995 if (err) {
3996 mlx5_ib_warn(dev, "\n");
3997 *bad_wr = wr;
3998 goto out;
3999 }
4000
4001 finish_wqe(qp, ctrl, size, idx, wr->wr_id,
4002 nreq, get_fence(fence, wr),
4003 next_fence, MLX5_OPCODE_UMR);
4004 /*
4005 * SET_PSV WQEs are not signaled and solicited
4006 * on error
4007 */
4008 wr->send_flags &= ~IB_SEND_SIGNALED;
4009 wr->send_flags |= IB_SEND_SOLICITED;
4010 err = begin_wqe(qp, &seg, &ctrl, wr,
4011 &idx, &size, nreq);
4012 if (err) {
4013 mlx5_ib_warn(dev, "\n");
4014 err = -ENOMEM;
4015 *bad_wr = wr;
4016 goto out;
4017 }
4018
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004019 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->mem,
Sagi Grimberge6631812014-02-23 14:19:11 +02004020 mr->sig->psv_memory.psv_idx, &seg,
4021 &size);
4022 if (err) {
4023 mlx5_ib_warn(dev, "\n");
4024 *bad_wr = wr;
4025 goto out;
4026 }
4027
4028 finish_wqe(qp, ctrl, size, idx, wr->wr_id,
4029 nreq, get_fence(fence, wr),
4030 next_fence, MLX5_OPCODE_SET_PSV);
4031 err = begin_wqe(qp, &seg, &ctrl, wr,
4032 &idx, &size, nreq);
4033 if (err) {
4034 mlx5_ib_warn(dev, "\n");
4035 err = -ENOMEM;
4036 *bad_wr = wr;
4037 goto out;
4038 }
4039
4040 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004041 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->wire,
Sagi Grimberge6631812014-02-23 14:19:11 +02004042 mr->sig->psv_wire.psv_idx, &seg,
4043 &size);
4044 if (err) {
4045 mlx5_ib_warn(dev, "\n");
4046 *bad_wr = wr;
4047 goto out;
4048 }
4049
4050 finish_wqe(qp, ctrl, size, idx, wr->wr_id,
4051 nreq, get_fence(fence, wr),
4052 next_fence, MLX5_OPCODE_SET_PSV);
4053 num_sge = 0;
4054 goto skip_psv;
4055
Eli Cohene126ba92013-07-07 17:25:49 +03004056 default:
4057 break;
4058 }
4059 break;
4060
4061 case IB_QPT_UC:
4062 switch (wr->opcode) {
4063 case IB_WR_RDMA_WRITE:
4064 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004065 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
4066 rdma_wr(wr)->rkey);
Eli Cohene126ba92013-07-07 17:25:49 +03004067 seg += sizeof(struct mlx5_wqe_raddr_seg);
4068 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
4069 break;
4070
4071 default:
4072 break;
4073 }
4074 break;
4075
Eli Cohene126ba92013-07-07 17:25:49 +03004076 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02004077 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03004078 set_datagram_seg(seg, wr);
Jack Morgensteinf241e742014-07-28 23:30:23 +03004079 seg += sizeof(struct mlx5_wqe_datagram_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004080 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
4081 if (unlikely((seg == qend)))
4082 seg = mlx5_get_send_wqe(qp, 0);
4083 break;
Erez Shitritf0313962016-02-21 16:27:17 +02004084 case IB_QPT_UD:
4085 set_datagram_seg(seg, wr);
4086 seg += sizeof(struct mlx5_wqe_datagram_seg);
4087 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Eli Cohene126ba92013-07-07 17:25:49 +03004088
Erez Shitritf0313962016-02-21 16:27:17 +02004089 if (unlikely((seg == qend)))
4090 seg = mlx5_get_send_wqe(qp, 0);
4091
4092 /* handle qp that supports ud offload */
4093 if (qp->flags & IB_QP_CREATE_IPOIB_UD_LSO) {
4094 struct mlx5_wqe_eth_pad *pad;
4095
4096 pad = seg;
4097 memset(pad, 0, sizeof(struct mlx5_wqe_eth_pad));
4098 seg += sizeof(struct mlx5_wqe_eth_pad);
4099 size += sizeof(struct mlx5_wqe_eth_pad) / 16;
4100
4101 seg = set_eth_seg(seg, wr, qend, qp, &size);
4102
4103 if (unlikely((seg == qend)))
4104 seg = mlx5_get_send_wqe(qp, 0);
4105 }
4106 break;
Eli Cohene126ba92013-07-07 17:25:49 +03004107 case MLX5_IB_QPT_REG_UMR:
4108 if (wr->opcode != MLX5_IB_WR_UMR) {
4109 err = -EINVAL;
4110 mlx5_ib_warn(dev, "bad opcode\n");
4111 goto out;
4112 }
4113 qp->sq.wr_data[idx] = MLX5_IB_WR_UMR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004114 ctrl->imm = cpu_to_be32(umr_wr(wr)->mkey);
Maor Gottlieb578e7262016-10-27 16:36:37 +03004115 set_reg_umr_segment(seg, wr, !!(MLX5_CAP_GEN(mdev, atomic)));
Eli Cohene126ba92013-07-07 17:25:49 +03004116 seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4117 size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4118 if (unlikely((seg == qend)))
4119 seg = mlx5_get_send_wqe(qp, 0);
4120 set_reg_mkey_segment(seg, wr);
4121 seg += sizeof(struct mlx5_mkey_seg);
4122 size += sizeof(struct mlx5_mkey_seg) / 16;
4123 if (unlikely((seg == qend)))
4124 seg = mlx5_get_send_wqe(qp, 0);
4125 break;
4126
4127 default:
4128 break;
4129 }
4130
4131 if (wr->send_flags & IB_SEND_INLINE && num_sge) {
4132 int uninitialized_var(sz);
4133
4134 err = set_data_inl_seg(qp, wr, seg, &sz);
4135 if (unlikely(err)) {
4136 mlx5_ib_warn(dev, "\n");
4137 *bad_wr = wr;
4138 goto out;
4139 }
4140 inl = 1;
4141 size += sz;
4142 } else {
4143 dpseg = seg;
4144 for (i = 0; i < num_sge; i++) {
4145 if (unlikely(dpseg == qend)) {
4146 seg = mlx5_get_send_wqe(qp, 0);
4147 dpseg = seg;
4148 }
4149 if (likely(wr->sg_list[i].length)) {
4150 set_data_ptr_seg(dpseg, wr->sg_list + i);
4151 size += sizeof(struct mlx5_wqe_data_seg) / 16;
4152 dpseg++;
4153 }
4154 }
4155 }
4156
Sagi Grimberg6e5eada2014-02-23 14:19:08 +02004157 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
4158 get_fence(fence, wr), next_fence,
4159 mlx5_ib_opcode[wr->opcode]);
Sagi Grimberge6631812014-02-23 14:19:11 +02004160skip_psv:
Eli Cohene126ba92013-07-07 17:25:49 +03004161 if (0)
4162 dump_wqe(qp, idx, size);
4163 }
4164
4165out:
4166 if (likely(nreq)) {
4167 qp->sq.head += nreq;
4168
4169 /* Make sure that descriptors are written before
4170 * updating doorbell record and ringing the doorbell
4171 */
4172 wmb();
4173
4174 qp->db.db[MLX5_SND_DBR] = cpu_to_be32(qp->sq.cur_post);
4175
Eli Cohenada388f2014-01-14 17:45:16 +02004176 /* Make sure doorbell record is visible to the HCA before
4177 * we hit doorbell */
4178 wmb();
4179
Eli Cohene126ba92013-07-07 17:25:49 +03004180 if (bf->need_lock)
4181 spin_lock(&bf->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02004182 else
4183 __acquire(&bf->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03004184
4185 /* TBD enable WC */
4186 if (0 && nreq == 1 && bf->uuarn && inl && size > 1 && size <= bf->buf_size / 16) {
4187 mlx5_bf_copy(bf->reg + bf->offset, (u64 *)ctrl, ALIGN(size * 16, 64), qp);
4188 /* wc_wmb(); */
4189 } else {
4190 mlx5_write64((__be32 *)ctrl, bf->regreg + bf->offset,
4191 MLX5_GET_DOORBELL_LOCK(&bf->lock32));
4192 /* Make sure doorbells don't leak out of SQ spinlock
4193 * and reach the HCA out of order.
4194 */
4195 mmiowb();
4196 }
4197 bf->offset ^= bf->buf_size;
4198 if (bf->need_lock)
4199 spin_unlock(&bf->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02004200 else
4201 __release(&bf->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03004202 }
4203
4204 spin_unlock_irqrestore(&qp->sq.lock, flags);
4205
4206 return err;
4207}
4208
4209static void set_sig_seg(struct mlx5_rwqe_sig *sig, int size)
4210{
4211 sig->signature = calc_sig(sig, size);
4212}
4213
4214int mlx5_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
4215 struct ib_recv_wr **bad_wr)
4216{
4217 struct mlx5_ib_qp *qp = to_mqp(ibqp);
4218 struct mlx5_wqe_data_seg *scat;
4219 struct mlx5_rwqe_sig *sig;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004220 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4221 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004222 unsigned long flags;
4223 int err = 0;
4224 int nreq;
4225 int ind;
4226 int i;
4227
Haggai Erand16e91d2016-02-29 15:45:05 +02004228 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4229 return mlx5_ib_gsi_post_recv(ibqp, wr, bad_wr);
4230
Eli Cohene126ba92013-07-07 17:25:49 +03004231 spin_lock_irqsave(&qp->rq.lock, flags);
4232
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004233 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
4234 err = -EIO;
4235 *bad_wr = wr;
4236 nreq = 0;
4237 goto out;
4238 }
4239
Eli Cohene126ba92013-07-07 17:25:49 +03004240 ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
4241
4242 for (nreq = 0; wr; nreq++, wr = wr->next) {
4243 if (mlx5_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
4244 err = -ENOMEM;
4245 *bad_wr = wr;
4246 goto out;
4247 }
4248
4249 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
4250 err = -EINVAL;
4251 *bad_wr = wr;
4252 goto out;
4253 }
4254
4255 scat = get_recv_wqe(qp, ind);
4256 if (qp->wq_sig)
4257 scat++;
4258
4259 for (i = 0; i < wr->num_sge; i++)
4260 set_data_ptr_seg(scat + i, wr->sg_list + i);
4261
4262 if (i < qp->rq.max_gs) {
4263 scat[i].byte_count = 0;
4264 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
4265 scat[i].addr = 0;
4266 }
4267
4268 if (qp->wq_sig) {
4269 sig = (struct mlx5_rwqe_sig *)scat;
4270 set_sig_seg(sig, (qp->rq.max_gs + 1) << 2);
4271 }
4272
4273 qp->rq.wrid[ind] = wr->wr_id;
4274
4275 ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
4276 }
4277
4278out:
4279 if (likely(nreq)) {
4280 qp->rq.head += nreq;
4281
4282 /* Make sure that descriptors are written before
4283 * doorbell record.
4284 */
4285 wmb();
4286
4287 *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
4288 }
4289
4290 spin_unlock_irqrestore(&qp->rq.lock, flags);
4291
4292 return err;
4293}
4294
4295static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
4296{
4297 switch (mlx5_state) {
4298 case MLX5_QP_STATE_RST: return IB_QPS_RESET;
4299 case MLX5_QP_STATE_INIT: return IB_QPS_INIT;
4300 case MLX5_QP_STATE_RTR: return IB_QPS_RTR;
4301 case MLX5_QP_STATE_RTS: return IB_QPS_RTS;
4302 case MLX5_QP_STATE_SQ_DRAINING:
4303 case MLX5_QP_STATE_SQD: return IB_QPS_SQD;
4304 case MLX5_QP_STATE_SQER: return IB_QPS_SQE;
4305 case MLX5_QP_STATE_ERR: return IB_QPS_ERR;
4306 default: return -1;
4307 }
4308}
4309
4310static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
4311{
4312 switch (mlx5_mig_state) {
4313 case MLX5_QP_PM_ARMED: return IB_MIG_ARMED;
4314 case MLX5_QP_PM_REARM: return IB_MIG_REARM;
4315 case MLX5_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
4316 default: return -1;
4317 }
4318}
4319
4320static int to_ib_qp_access_flags(int mlx5_flags)
4321{
4322 int ib_flags = 0;
4323
4324 if (mlx5_flags & MLX5_QP_BIT_RRE)
4325 ib_flags |= IB_ACCESS_REMOTE_READ;
4326 if (mlx5_flags & MLX5_QP_BIT_RWE)
4327 ib_flags |= IB_ACCESS_REMOTE_WRITE;
4328 if (mlx5_flags & MLX5_QP_BIT_RAE)
4329 ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
4330
4331 return ib_flags;
4332}
4333
4334static void to_ib_ah_attr(struct mlx5_ib_dev *ibdev, struct ib_ah_attr *ib_ah_attr,
4335 struct mlx5_qp_path *path)
4336{
Jack Morgenstein9603b612014-07-28 23:30:22 +03004337 struct mlx5_core_dev *dev = ibdev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004338
4339 memset(ib_ah_attr, 0, sizeof(*ib_ah_attr));
4340 ib_ah_attr->port_num = path->port;
4341
Eli Cohenc7a08ac2014-10-02 12:19:42 +03004342 if (ib_ah_attr->port_num == 0 ||
Saeed Mahameed938fe832015-05-28 22:28:41 +03004343 ib_ah_attr->port_num > MLX5_CAP_GEN(dev, num_ports))
Eli Cohene126ba92013-07-07 17:25:49 +03004344 return;
4345
Achiad Shochat2811ba52015-12-23 18:47:24 +02004346 ib_ah_attr->sl = path->dci_cfi_prio_sl & 0xf;
Eli Cohene126ba92013-07-07 17:25:49 +03004347
4348 ib_ah_attr->dlid = be16_to_cpu(path->rlid);
4349 ib_ah_attr->src_path_bits = path->grh_mlid & 0x7f;
4350 ib_ah_attr->static_rate = path->static_rate ? path->static_rate - 5 : 0;
4351 ib_ah_attr->ah_flags = (path->grh_mlid & (1 << 7)) ? IB_AH_GRH : 0;
4352 if (ib_ah_attr->ah_flags) {
4353 ib_ah_attr->grh.sgid_index = path->mgid_index;
4354 ib_ah_attr->grh.hop_limit = path->hop_limit;
4355 ib_ah_attr->grh.traffic_class =
4356 (be32_to_cpu(path->tclass_flowlabel) >> 20) & 0xff;
4357 ib_ah_attr->grh.flow_label =
4358 be32_to_cpu(path->tclass_flowlabel) & 0xfffff;
4359 memcpy(ib_ah_attr->grh.dgid.raw,
4360 path->rgid, sizeof(ib_ah_attr->grh.dgid.raw));
4361 }
4362}
4363
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004364static int query_raw_packet_qp_sq_state(struct mlx5_ib_dev *dev,
4365 struct mlx5_ib_sq *sq,
4366 u8 *sq_state)
Eli Cohene126ba92013-07-07 17:25:49 +03004367{
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004368 void *out;
4369 void *sqc;
4370 int inlen;
4371 int err;
4372
4373 inlen = MLX5_ST_SZ_BYTES(query_sq_out);
4374 out = mlx5_vzalloc(inlen);
4375 if (!out)
4376 return -ENOMEM;
4377
4378 err = mlx5_core_query_sq(dev->mdev, sq->base.mqp.qpn, out);
4379 if (err)
4380 goto out;
4381
4382 sqc = MLX5_ADDR_OF(query_sq_out, out, sq_context);
4383 *sq_state = MLX5_GET(sqc, sqc, state);
4384 sq->state = *sq_state;
4385
4386out:
4387 kvfree(out);
4388 return err;
4389}
4390
4391static int query_raw_packet_qp_rq_state(struct mlx5_ib_dev *dev,
4392 struct mlx5_ib_rq *rq,
4393 u8 *rq_state)
4394{
4395 void *out;
4396 void *rqc;
4397 int inlen;
4398 int err;
4399
4400 inlen = MLX5_ST_SZ_BYTES(query_rq_out);
4401 out = mlx5_vzalloc(inlen);
4402 if (!out)
4403 return -ENOMEM;
4404
4405 err = mlx5_core_query_rq(dev->mdev, rq->base.mqp.qpn, out);
4406 if (err)
4407 goto out;
4408
4409 rqc = MLX5_ADDR_OF(query_rq_out, out, rq_context);
4410 *rq_state = MLX5_GET(rqc, rqc, state);
4411 rq->state = *rq_state;
4412
4413out:
4414 kvfree(out);
4415 return err;
4416}
4417
4418static int sqrq_state_to_qp_state(u8 sq_state, u8 rq_state,
4419 struct mlx5_ib_qp *qp, u8 *qp_state)
4420{
4421 static const u8 sqrq_trans[MLX5_RQ_NUM_STATE][MLX5_SQ_NUM_STATE] = {
4422 [MLX5_RQC_STATE_RST] = {
4423 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
4424 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
4425 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE_BAD,
4426 [MLX5_SQ_STATE_NA] = IB_QPS_RESET,
4427 },
4428 [MLX5_RQC_STATE_RDY] = {
4429 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
4430 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
4431 [MLX5_SQC_STATE_ERR] = IB_QPS_SQE,
4432 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE,
4433 },
4434 [MLX5_RQC_STATE_ERR] = {
4435 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
4436 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
4437 [MLX5_SQC_STATE_ERR] = IB_QPS_ERR,
4438 [MLX5_SQ_STATE_NA] = IB_QPS_ERR,
4439 },
4440 [MLX5_RQ_STATE_NA] = {
4441 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
4442 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
4443 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE,
4444 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE_BAD,
4445 },
4446 };
4447
4448 *qp_state = sqrq_trans[rq_state][sq_state];
4449
4450 if (*qp_state == MLX5_QP_STATE_BAD) {
4451 WARN(1, "Buggy Raw Packet QP state, SQ 0x%x state: 0x%x, RQ 0x%x state: 0x%x",
4452 qp->raw_packet_qp.sq.base.mqp.qpn, sq_state,
4453 qp->raw_packet_qp.rq.base.mqp.qpn, rq_state);
4454 return -EINVAL;
4455 }
4456
4457 if (*qp_state == MLX5_QP_STATE)
4458 *qp_state = qp->state;
4459
4460 return 0;
4461}
4462
4463static int query_raw_packet_qp_state(struct mlx5_ib_dev *dev,
4464 struct mlx5_ib_qp *qp,
4465 u8 *raw_packet_qp_state)
4466{
4467 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
4468 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
4469 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
4470 int err;
4471 u8 sq_state = MLX5_SQ_STATE_NA;
4472 u8 rq_state = MLX5_RQ_STATE_NA;
4473
4474 if (qp->sq.wqe_cnt) {
4475 err = query_raw_packet_qp_sq_state(dev, sq, &sq_state);
4476 if (err)
4477 return err;
4478 }
4479
4480 if (qp->rq.wqe_cnt) {
4481 err = query_raw_packet_qp_rq_state(dev, rq, &rq_state);
4482 if (err)
4483 return err;
4484 }
4485
4486 return sqrq_state_to_qp_state(sq_state, rq_state, qp,
4487 raw_packet_qp_state);
4488}
4489
4490static int query_qp_attr(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
4491 struct ib_qp_attr *qp_attr)
4492{
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004493 int outlen = MLX5_ST_SZ_BYTES(query_qp_out);
Eli Cohene126ba92013-07-07 17:25:49 +03004494 struct mlx5_qp_context *context;
4495 int mlx5_state;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004496 u32 *outb;
Eli Cohene126ba92013-07-07 17:25:49 +03004497 int err = 0;
4498
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004499 outb = kzalloc(outlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004500 if (!outb)
4501 return -ENOMEM;
4502
majd@mellanox.com19098df2016-01-14 19:13:03 +02004503 err = mlx5_core_qp_query(dev->mdev, &qp->trans_qp.base.mqp, outb,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004504 outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03004505 if (err)
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004506 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004507
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004508 /* FIXME: use MLX5_GET rather than mlx5_qp_context manual struct */
4509 context = (struct mlx5_qp_context *)MLX5_ADDR_OF(query_qp_out, outb, qpc);
4510
Eli Cohene126ba92013-07-07 17:25:49 +03004511 mlx5_state = be32_to_cpu(context->flags) >> 28;
4512
4513 qp->state = to_ib_qp_state(mlx5_state);
Eli Cohene126ba92013-07-07 17:25:49 +03004514 qp_attr->path_mtu = context->mtu_msgmax >> 5;
4515 qp_attr->path_mig_state =
4516 to_ib_mig_state((be32_to_cpu(context->flags) >> 11) & 0x3);
4517 qp_attr->qkey = be32_to_cpu(context->qkey);
4518 qp_attr->rq_psn = be32_to_cpu(context->rnr_nextrecvpsn) & 0xffffff;
4519 qp_attr->sq_psn = be32_to_cpu(context->next_send_psn) & 0xffffff;
4520 qp_attr->dest_qp_num = be32_to_cpu(context->log_pg_sz_remote_qpn) & 0xffffff;
4521 qp_attr->qp_access_flags =
4522 to_ib_qp_access_flags(be32_to_cpu(context->params2));
4523
4524 if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
4525 to_ib_ah_attr(dev, &qp_attr->ah_attr, &context->pri_path);
4526 to_ib_ah_attr(dev, &qp_attr->alt_ah_attr, &context->alt_path);
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03004527 qp_attr->alt_pkey_index =
4528 be16_to_cpu(context->alt_path.pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03004529 qp_attr->alt_port_num = qp_attr->alt_ah_attr.port_num;
4530 }
4531
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03004532 qp_attr->pkey_index = be16_to_cpu(context->pri_path.pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03004533 qp_attr->port_num = context->pri_path.port;
4534
4535 /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
4536 qp_attr->sq_draining = mlx5_state == MLX5_QP_STATE_SQ_DRAINING;
4537
4538 qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context->params1) >> 21) & 0x7);
4539
4540 qp_attr->max_dest_rd_atomic =
4541 1 << ((be32_to_cpu(context->params2) >> 21) & 0x7);
4542 qp_attr->min_rnr_timer =
4543 (be32_to_cpu(context->rnr_nextrecvpsn) >> 24) & 0x1f;
4544 qp_attr->timeout = context->pri_path.ackto_lt >> 3;
4545 qp_attr->retry_cnt = (be32_to_cpu(context->params1) >> 16) & 0x7;
4546 qp_attr->rnr_retry = (be32_to_cpu(context->params1) >> 13) & 0x7;
4547 qp_attr->alt_timeout = context->alt_path.ackto_lt >> 3;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004548
4549out:
4550 kfree(outb);
4551 return err;
4552}
4553
4554int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
4555 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
4556{
4557 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4558 struct mlx5_ib_qp *qp = to_mqp(ibqp);
4559 int err = 0;
4560 u8 raw_packet_qp_state;
4561
Yishai Hadas28d61372016-05-23 15:20:56 +03004562 if (ibqp->rwq_ind_tbl)
4563 return -ENOSYS;
4564
Haggai Erand16e91d2016-02-29 15:45:05 +02004565 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4566 return mlx5_ib_gsi_query_qp(ibqp, qp_attr, qp_attr_mask,
4567 qp_init_attr);
4568
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004569#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
4570 /*
4571 * Wait for any outstanding page faults, in case the user frees memory
4572 * based upon this query's result.
4573 */
4574 flush_workqueue(mlx5_ib_page_fault_wq);
4575#endif
4576
4577 mutex_lock(&qp->mutex);
4578
4579 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET) {
4580 err = query_raw_packet_qp_state(dev, qp, &raw_packet_qp_state);
4581 if (err)
4582 goto out;
4583 qp->state = raw_packet_qp_state;
4584 qp_attr->port_num = 1;
4585 } else {
4586 err = query_qp_attr(dev, qp, qp_attr);
4587 if (err)
4588 goto out;
4589 }
4590
4591 qp_attr->qp_state = qp->state;
Eli Cohene126ba92013-07-07 17:25:49 +03004592 qp_attr->cur_qp_state = qp_attr->qp_state;
4593 qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
4594 qp_attr->cap.max_recv_sge = qp->rq.max_gs;
4595
4596 if (!ibqp->uobject) {
Noa Osherovich0540d812016-06-04 15:15:32 +03004597 qp_attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +03004598 qp_attr->cap.max_send_sge = qp->sq.max_gs;
Noa Osherovich0540d812016-06-04 15:15:32 +03004599 qp_init_attr->qp_context = ibqp->qp_context;
Eli Cohene126ba92013-07-07 17:25:49 +03004600 } else {
4601 qp_attr->cap.max_send_wr = 0;
4602 qp_attr->cap.max_send_sge = 0;
4603 }
4604
Noa Osherovich0540d812016-06-04 15:15:32 +03004605 qp_init_attr->qp_type = ibqp->qp_type;
4606 qp_init_attr->recv_cq = ibqp->recv_cq;
4607 qp_init_attr->send_cq = ibqp->send_cq;
4608 qp_init_attr->srq = ibqp->srq;
4609 qp_attr->cap.max_inline_data = qp->max_inline_data;
Eli Cohene126ba92013-07-07 17:25:49 +03004610
4611 qp_init_attr->cap = qp_attr->cap;
4612
4613 qp_init_attr->create_flags = 0;
4614 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
4615 qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
4616
Leon Romanovsky051f2632015-12-20 12:16:11 +02004617 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
4618 qp_init_attr->create_flags |= IB_QP_CREATE_CROSS_CHANNEL;
4619 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
4620 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_SEND;
4621 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
4622 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_RECV;
Haggai Eranb11a4f92016-02-29 15:45:03 +02004623 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
4624 qp_init_attr->create_flags |= mlx5_ib_create_qp_sqpn_qp1();
Leon Romanovsky051f2632015-12-20 12:16:11 +02004625
Eli Cohene126ba92013-07-07 17:25:49 +03004626 qp_init_attr->sq_sig_type = qp->sq_signal_bits & MLX5_WQE_CTRL_CQ_UPDATE ?
4627 IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
4628
Eli Cohene126ba92013-07-07 17:25:49 +03004629out:
4630 mutex_unlock(&qp->mutex);
4631 return err;
4632}
4633
4634struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
4635 struct ib_ucontext *context,
4636 struct ib_udata *udata)
4637{
4638 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4639 struct mlx5_ib_xrcd *xrcd;
4640 int err;
4641
Saeed Mahameed938fe832015-05-28 22:28:41 +03004642 if (!MLX5_CAP_GEN(dev->mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +03004643 return ERR_PTR(-ENOSYS);
4644
4645 xrcd = kmalloc(sizeof(*xrcd), GFP_KERNEL);
4646 if (!xrcd)
4647 return ERR_PTR(-ENOMEM);
4648
Jack Morgenstein9603b612014-07-28 23:30:22 +03004649 err = mlx5_core_xrcd_alloc(dev->mdev, &xrcd->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03004650 if (err) {
4651 kfree(xrcd);
4652 return ERR_PTR(-ENOMEM);
4653 }
4654
4655 return &xrcd->ibxrcd;
4656}
4657
4658int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
4659{
4660 struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
4661 u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
4662 int err;
4663
Jack Morgenstein9603b612014-07-28 23:30:22 +03004664 err = mlx5_core_xrcd_dealloc(dev->mdev, xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03004665 if (err) {
4666 mlx5_ib_warn(dev, "failed to dealloc xrcdn 0x%x\n", xrcdn);
4667 return err;
4668 }
4669
4670 kfree(xrcd);
4671
4672 return 0;
4673}
Yishai Hadas79b20a62016-05-23 15:20:50 +03004674
Yishai Hadas350d0e42016-08-28 14:58:18 +03004675static void mlx5_ib_wq_event(struct mlx5_core_qp *core_qp, int type)
4676{
4677 struct mlx5_ib_rwq *rwq = to_mibrwq(core_qp);
4678 struct mlx5_ib_dev *dev = to_mdev(rwq->ibwq.device);
4679 struct ib_event event;
4680
4681 if (rwq->ibwq.event_handler) {
4682 event.device = rwq->ibwq.device;
4683 event.element.wq = &rwq->ibwq;
4684 switch (type) {
4685 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
4686 event.event = IB_EVENT_WQ_FATAL;
4687 break;
4688 default:
4689 mlx5_ib_warn(dev, "Unexpected event type %d on WQ %06x\n", type, core_qp->qpn);
4690 return;
4691 }
4692
4693 rwq->ibwq.event_handler(&event, rwq->ibwq.wq_context);
4694 }
4695}
4696
Yishai Hadas79b20a62016-05-23 15:20:50 +03004697static int create_rq(struct mlx5_ib_rwq *rwq, struct ib_pd *pd,
4698 struct ib_wq_init_attr *init_attr)
4699{
4700 struct mlx5_ib_dev *dev;
4701 __be64 *rq_pas0;
4702 void *in;
4703 void *rqc;
4704 void *wq;
4705 int inlen;
4706 int err;
4707
4708 dev = to_mdev(pd->device);
4709
4710 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + sizeof(u64) * rwq->rq_num_pas;
4711 in = mlx5_vzalloc(inlen);
4712 if (!in)
4713 return -ENOMEM;
4714
4715 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
4716 MLX5_SET(rqc, rqc, mem_rq_type,
4717 MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
4718 MLX5_SET(rqc, rqc, user_index, rwq->user_index);
4719 MLX5_SET(rqc, rqc, cqn, to_mcq(init_attr->cq)->mcq.cqn);
4720 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
4721 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
4722 wq = MLX5_ADDR_OF(rqc, rqc, wq);
4723 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
4724 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
4725 MLX5_SET(wq, wq, log_wq_stride, rwq->log_rq_stride);
4726 MLX5_SET(wq, wq, log_wq_sz, rwq->log_rq_size);
4727 MLX5_SET(wq, wq, pd, to_mpd(pd)->pdn);
4728 MLX5_SET(wq, wq, page_offset, rwq->rq_page_offset);
4729 MLX5_SET(wq, wq, log_wq_pg_sz, rwq->log_page_size);
4730 MLX5_SET(wq, wq, wq_signature, rwq->wq_sig);
4731 MLX5_SET64(wq, wq, dbr_addr, rwq->db.dma);
4732 rq_pas0 = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
4733 mlx5_ib_populate_pas(dev, rwq->umem, rwq->page_shift, rq_pas0, 0);
Yishai Hadas350d0e42016-08-28 14:58:18 +03004734 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004735 kvfree(in);
4736 return err;
4737}
4738
4739static int set_user_rq_size(struct mlx5_ib_dev *dev,
4740 struct ib_wq_init_attr *wq_init_attr,
4741 struct mlx5_ib_create_wq *ucmd,
4742 struct mlx5_ib_rwq *rwq)
4743{
4744 /* Sanity check RQ size before proceeding */
4745 if (wq_init_attr->max_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_wq_sz)))
4746 return -EINVAL;
4747
4748 if (!ucmd->rq_wqe_count)
4749 return -EINVAL;
4750
4751 rwq->wqe_count = ucmd->rq_wqe_count;
4752 rwq->wqe_shift = ucmd->rq_wqe_shift;
4753 rwq->buf_size = (rwq->wqe_count << rwq->wqe_shift);
4754 rwq->log_rq_stride = rwq->wqe_shift;
4755 rwq->log_rq_size = ilog2(rwq->wqe_count);
4756 return 0;
4757}
4758
4759static int prepare_user_rq(struct ib_pd *pd,
4760 struct ib_wq_init_attr *init_attr,
4761 struct ib_udata *udata,
4762 struct mlx5_ib_rwq *rwq)
4763{
4764 struct mlx5_ib_dev *dev = to_mdev(pd->device);
4765 struct mlx5_ib_create_wq ucmd = {};
4766 int err;
4767 size_t required_cmd_sz;
4768
4769 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
4770 if (udata->inlen < required_cmd_sz) {
4771 mlx5_ib_dbg(dev, "invalid inlen\n");
4772 return -EINVAL;
4773 }
4774
4775 if (udata->inlen > sizeof(ucmd) &&
4776 !ib_is_udata_cleared(udata, sizeof(ucmd),
4777 udata->inlen - sizeof(ucmd))) {
4778 mlx5_ib_dbg(dev, "inlen is not supported\n");
4779 return -EOPNOTSUPP;
4780 }
4781
4782 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
4783 mlx5_ib_dbg(dev, "copy failed\n");
4784 return -EFAULT;
4785 }
4786
4787 if (ucmd.comp_mask) {
4788 mlx5_ib_dbg(dev, "invalid comp mask\n");
4789 return -EOPNOTSUPP;
4790 }
4791
4792 if (ucmd.reserved) {
4793 mlx5_ib_dbg(dev, "invalid reserved\n");
4794 return -EOPNOTSUPP;
4795 }
4796
4797 err = set_user_rq_size(dev, init_attr, &ucmd, rwq);
4798 if (err) {
4799 mlx5_ib_dbg(dev, "err %d\n", err);
4800 return err;
4801 }
4802
4803 err = create_user_rq(dev, pd, rwq, &ucmd);
4804 if (err) {
4805 mlx5_ib_dbg(dev, "err %d\n", err);
4806 if (err)
4807 return err;
4808 }
4809
4810 rwq->user_index = ucmd.user_index;
4811 return 0;
4812}
4813
4814struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
4815 struct ib_wq_init_attr *init_attr,
4816 struct ib_udata *udata)
4817{
4818 struct mlx5_ib_dev *dev;
4819 struct mlx5_ib_rwq *rwq;
4820 struct mlx5_ib_create_wq_resp resp = {};
4821 size_t min_resp_len;
4822 int err;
4823
4824 if (!udata)
4825 return ERR_PTR(-ENOSYS);
4826
4827 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
4828 if (udata->outlen && udata->outlen < min_resp_len)
4829 return ERR_PTR(-EINVAL);
4830
4831 dev = to_mdev(pd->device);
4832 switch (init_attr->wq_type) {
4833 case IB_WQT_RQ:
4834 rwq = kzalloc(sizeof(*rwq), GFP_KERNEL);
4835 if (!rwq)
4836 return ERR_PTR(-ENOMEM);
4837 err = prepare_user_rq(pd, init_attr, udata, rwq);
4838 if (err)
4839 goto err;
4840 err = create_rq(rwq, pd, init_attr);
4841 if (err)
4842 goto err_user_rq;
4843 break;
4844 default:
4845 mlx5_ib_dbg(dev, "unsupported wq type %d\n",
4846 init_attr->wq_type);
4847 return ERR_PTR(-EINVAL);
4848 }
4849
Yishai Hadas350d0e42016-08-28 14:58:18 +03004850 rwq->ibwq.wq_num = rwq->core_qp.qpn;
Yishai Hadas79b20a62016-05-23 15:20:50 +03004851 rwq->ibwq.state = IB_WQS_RESET;
4852 if (udata->outlen) {
4853 resp.response_length = offsetof(typeof(resp), response_length) +
4854 sizeof(resp.response_length);
4855 err = ib_copy_to_udata(udata, &resp, resp.response_length);
4856 if (err)
4857 goto err_copy;
4858 }
4859
Yishai Hadas350d0e42016-08-28 14:58:18 +03004860 rwq->core_qp.event = mlx5_ib_wq_event;
4861 rwq->ibwq.event_handler = init_attr->event_handler;
Yishai Hadas79b20a62016-05-23 15:20:50 +03004862 return &rwq->ibwq;
4863
4864err_copy:
Yishai Hadas350d0e42016-08-28 14:58:18 +03004865 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004866err_user_rq:
4867 destroy_user_rq(pd, rwq);
4868err:
4869 kfree(rwq);
4870 return ERR_PTR(err);
4871}
4872
4873int mlx5_ib_destroy_wq(struct ib_wq *wq)
4874{
4875 struct mlx5_ib_dev *dev = to_mdev(wq->device);
4876 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
4877
Yishai Hadas350d0e42016-08-28 14:58:18 +03004878 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004879 destroy_user_rq(wq->pd, rwq);
4880 kfree(rwq);
4881
4882 return 0;
4883}
4884
Yishai Hadasc5f90922016-05-23 15:20:53 +03004885struct ib_rwq_ind_table *mlx5_ib_create_rwq_ind_table(struct ib_device *device,
4886 struct ib_rwq_ind_table_init_attr *init_attr,
4887 struct ib_udata *udata)
4888{
4889 struct mlx5_ib_dev *dev = to_mdev(device);
4890 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl;
4891 int sz = 1 << init_attr->log_ind_tbl_size;
4892 struct mlx5_ib_create_rwq_ind_tbl_resp resp = {};
4893 size_t min_resp_len;
4894 int inlen;
4895 int err;
4896 int i;
4897 u32 *in;
4898 void *rqtc;
4899
4900 if (udata->inlen > 0 &&
4901 !ib_is_udata_cleared(udata, 0,
4902 udata->inlen))
4903 return ERR_PTR(-EOPNOTSUPP);
4904
4905 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
4906 if (udata->outlen && udata->outlen < min_resp_len)
4907 return ERR_PTR(-EINVAL);
4908
4909 rwq_ind_tbl = kzalloc(sizeof(*rwq_ind_tbl), GFP_KERNEL);
4910 if (!rwq_ind_tbl)
4911 return ERR_PTR(-ENOMEM);
4912
4913 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
4914 in = mlx5_vzalloc(inlen);
4915 if (!in) {
4916 err = -ENOMEM;
4917 goto err;
4918 }
4919
4920 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
4921
4922 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
4923 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
4924
4925 for (i = 0; i < sz; i++)
4926 MLX5_SET(rqtc, rqtc, rq_num[i], init_attr->ind_tbl[i]->wq_num);
4927
4928 err = mlx5_core_create_rqt(dev->mdev, in, inlen, &rwq_ind_tbl->rqtn);
4929 kvfree(in);
4930
4931 if (err)
4932 goto err;
4933
4934 rwq_ind_tbl->ib_rwq_ind_tbl.ind_tbl_num = rwq_ind_tbl->rqtn;
4935 if (udata->outlen) {
4936 resp.response_length = offsetof(typeof(resp), response_length) +
4937 sizeof(resp.response_length);
4938 err = ib_copy_to_udata(udata, &resp, resp.response_length);
4939 if (err)
4940 goto err_copy;
4941 }
4942
4943 return &rwq_ind_tbl->ib_rwq_ind_tbl;
4944
4945err_copy:
4946 mlx5_core_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn);
4947err:
4948 kfree(rwq_ind_tbl);
4949 return ERR_PTR(err);
4950}
4951
4952int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
4953{
4954 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl = to_mrwq_ind_table(ib_rwq_ind_tbl);
4955 struct mlx5_ib_dev *dev = to_mdev(ib_rwq_ind_tbl->device);
4956
4957 mlx5_core_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn);
4958
4959 kfree(rwq_ind_tbl);
4960 return 0;
4961}
4962
Yishai Hadas79b20a62016-05-23 15:20:50 +03004963int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
4964 u32 wq_attr_mask, struct ib_udata *udata)
4965{
4966 struct mlx5_ib_dev *dev = to_mdev(wq->device);
4967 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
4968 struct mlx5_ib_modify_wq ucmd = {};
4969 size_t required_cmd_sz;
4970 int curr_wq_state;
4971 int wq_state;
4972 int inlen;
4973 int err;
4974 void *rqc;
4975 void *in;
4976
4977 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
4978 if (udata->inlen < required_cmd_sz)
4979 return -EINVAL;
4980
4981 if (udata->inlen > sizeof(ucmd) &&
4982 !ib_is_udata_cleared(udata, sizeof(ucmd),
4983 udata->inlen - sizeof(ucmd)))
4984 return -EOPNOTSUPP;
4985
4986 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
4987 return -EFAULT;
4988
4989 if (ucmd.comp_mask || ucmd.reserved)
4990 return -EOPNOTSUPP;
4991
4992 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
4993 in = mlx5_vzalloc(inlen);
4994 if (!in)
4995 return -ENOMEM;
4996
4997 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
4998
4999 curr_wq_state = (wq_attr_mask & IB_WQ_CUR_STATE) ?
5000 wq_attr->curr_wq_state : wq->state;
5001 wq_state = (wq_attr_mask & IB_WQ_STATE) ?
5002 wq_attr->wq_state : curr_wq_state;
5003 if (curr_wq_state == IB_WQS_ERR)
5004 curr_wq_state = MLX5_RQC_STATE_ERR;
5005 if (wq_state == IB_WQS_ERR)
5006 wq_state = MLX5_RQC_STATE_ERR;
5007 MLX5_SET(modify_rq_in, in, rq_state, curr_wq_state);
5008 MLX5_SET(rqc, rqc, state, wq_state);
5009
Yishai Hadas350d0e42016-08-28 14:58:18 +03005010 err = mlx5_core_modify_rq(dev->mdev, rwq->core_qp.qpn, in, inlen);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005011 kvfree(in);
5012 if (!err)
5013 rwq->ibwq.state = (wq_state == MLX5_RQC_STATE_ERR) ? IB_WQS_ERR : wq_state;
5014
5015 return err;
5016}