blob: c5d8ec839e99a2464b18bd2ff1c26cea05f79832 [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,
Eli Cohene126ba92013-07-07 17:25:49 +030055};
56
57static const u32 mlx5_ib_opcode[] = {
58 [IB_WR_SEND] = MLX5_OPCODE_SEND,
Erez Shitritf0313962016-02-21 16:27:17 +020059 [IB_WR_LSO] = MLX5_OPCODE_LSO,
Eli Cohene126ba92013-07-07 17:25:49 +030060 [IB_WR_SEND_WITH_IMM] = MLX5_OPCODE_SEND_IMM,
61 [IB_WR_RDMA_WRITE] = MLX5_OPCODE_RDMA_WRITE,
62 [IB_WR_RDMA_WRITE_WITH_IMM] = MLX5_OPCODE_RDMA_WRITE_IMM,
63 [IB_WR_RDMA_READ] = MLX5_OPCODE_RDMA_READ,
64 [IB_WR_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_CS,
65 [IB_WR_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_FA,
66 [IB_WR_SEND_WITH_INV] = MLX5_OPCODE_SEND_INVAL,
67 [IB_WR_LOCAL_INV] = MLX5_OPCODE_UMR,
Sagi Grimberg8a187ee2015-10-13 19:11:26 +030068 [IB_WR_REG_MR] = MLX5_OPCODE_UMR,
Eli Cohene126ba92013-07-07 17:25:49 +030069 [IB_WR_MASKED_ATOMIC_CMP_AND_SWP] = MLX5_OPCODE_ATOMIC_MASKED_CS,
70 [IB_WR_MASKED_ATOMIC_FETCH_AND_ADD] = MLX5_OPCODE_ATOMIC_MASKED_FA,
71 [MLX5_IB_WR_UMR] = MLX5_OPCODE_UMR,
72};
73
Erez Shitritf0313962016-02-21 16:27:17 +020074struct mlx5_wqe_eth_pad {
75 u8 rsvd0[16];
76};
Eli Cohene126ba92013-07-07 17:25:49 +030077
Alex Veskereb49ab02016-08-28 12:25:53 +030078enum raw_qp_set_mask_map {
79 MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID = 1UL << 0,
Bodong Wang7d29f342016-12-01 13:43:16 +020080 MLX5_RAW_QP_RATE_LIMIT = 1UL << 1,
Alex Veskereb49ab02016-08-28 12:25:53 +030081};
82
Alex Vesker0680efa2016-08-28 12:25:52 +030083struct mlx5_modify_raw_qp_param {
84 u16 operation;
Alex Veskereb49ab02016-08-28 12:25:53 +030085
86 u32 set_mask; /* raw_qp_set_mask_map */
Bodong Wang7d29f342016-12-01 13:43:16 +020087 u32 rate_limit;
Alex Veskereb49ab02016-08-28 12:25:53 +030088 u8 rq_q_ctr_id;
Alex Vesker0680efa2016-08-28 12:25:52 +030089};
90
Maor Gottlieb89ea94a72016-06-17 15:01:38 +030091static void get_cqs(enum ib_qp_type qp_type,
92 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
93 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq);
94
Eli Cohene126ba92013-07-07 17:25:49 +030095static int is_qp0(enum ib_qp_type qp_type)
96{
97 return qp_type == IB_QPT_SMI;
98}
99
Eli Cohene126ba92013-07-07 17:25:49 +0300100static int is_sqp(enum ib_qp_type qp_type)
101{
102 return is_qp0(qp_type) || is_qp1(qp_type);
103}
104
105static void *get_wqe(struct mlx5_ib_qp *qp, int offset)
106{
107 return mlx5_buf_offset(&qp->buf, offset);
108}
109
110static void *get_recv_wqe(struct mlx5_ib_qp *qp, int n)
111{
112 return get_wqe(qp, qp->rq.offset + (n << qp->rq.wqe_shift));
113}
114
115void *mlx5_get_send_wqe(struct mlx5_ib_qp *qp, int n)
116{
117 return get_wqe(qp, qp->sq.offset + (n << MLX5_IB_SQ_STRIDE));
118}
119
Haggai Eranc1395a22014-12-11 17:04:14 +0200120/**
121 * mlx5_ib_read_user_wqe() - Copy a user-space WQE to kernel space.
122 *
123 * @qp: QP to copy from.
124 * @send: copy from the send queue when non-zero, use the receive queue
125 * otherwise.
126 * @wqe_index: index to start copying from. For send work queues, the
127 * wqe_index is in units of MLX5_SEND_WQE_BB.
128 * For receive work queue, it is the number of work queue
129 * element in the queue.
130 * @buffer: destination buffer.
131 * @length: maximum number of bytes to copy.
132 *
133 * Copies at least a single WQE, but may copy more data.
134 *
135 * Return: the number of bytes copied, or an error code.
136 */
137int mlx5_ib_read_user_wqe(struct mlx5_ib_qp *qp, int send, int wqe_index,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200138 void *buffer, u32 length,
139 struct mlx5_ib_qp_base *base)
Haggai Eranc1395a22014-12-11 17:04:14 +0200140{
141 struct ib_device *ibdev = qp->ibqp.device;
142 struct mlx5_ib_dev *dev = to_mdev(ibdev);
143 struct mlx5_ib_wq *wq = send ? &qp->sq : &qp->rq;
144 size_t offset;
145 size_t wq_end;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200146 struct ib_umem *umem = base->ubuffer.umem;
Haggai Eranc1395a22014-12-11 17:04:14 +0200147 u32 first_copy_length;
148 int wqe_length;
149 int ret;
150
151 if (wq->wqe_cnt == 0) {
152 mlx5_ib_dbg(dev, "mlx5_ib_read_user_wqe for a QP with wqe_cnt == 0. qp_type: 0x%x\n",
153 qp->ibqp.qp_type);
154 return -EINVAL;
155 }
156
157 offset = wq->offset + ((wqe_index % wq->wqe_cnt) << wq->wqe_shift);
158 wq_end = wq->offset + (wq->wqe_cnt << wq->wqe_shift);
159
160 if (send && length < sizeof(struct mlx5_wqe_ctrl_seg))
161 return -EINVAL;
162
163 if (offset > umem->length ||
164 (send && offset + sizeof(struct mlx5_wqe_ctrl_seg) > umem->length))
165 return -EINVAL;
166
167 first_copy_length = min_t(u32, offset + length, wq_end) - offset;
168 ret = ib_umem_copy_from(buffer, umem, offset, first_copy_length);
169 if (ret)
170 return ret;
171
172 if (send) {
173 struct mlx5_wqe_ctrl_seg *ctrl = buffer;
174 int ds = be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_DS_MASK;
175
176 wqe_length = ds * MLX5_WQE_DS_UNITS;
177 } else {
178 wqe_length = 1 << wq->wqe_shift;
179 }
180
181 if (wqe_length <= first_copy_length)
182 return first_copy_length;
183
184 ret = ib_umem_copy_from(buffer + first_copy_length, umem, wq->offset,
185 wqe_length - first_copy_length);
186 if (ret)
187 return ret;
188
189 return wqe_length;
190}
191
Eli Cohene126ba92013-07-07 17:25:49 +0300192static void mlx5_ib_qp_event(struct mlx5_core_qp *qp, int type)
193{
194 struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
195 struct ib_event event;
196
majd@mellanox.com19098df2016-01-14 19:13:03 +0200197 if (type == MLX5_EVENT_TYPE_PATH_MIG) {
198 /* This event is only valid for trans_qps */
199 to_mibqp(qp)->port = to_mibqp(qp)->trans_qp.alt_port;
200 }
Eli Cohene126ba92013-07-07 17:25:49 +0300201
202 if (ibqp->event_handler) {
203 event.device = ibqp->device;
204 event.element.qp = ibqp;
205 switch (type) {
206 case MLX5_EVENT_TYPE_PATH_MIG:
207 event.event = IB_EVENT_PATH_MIG;
208 break;
209 case MLX5_EVENT_TYPE_COMM_EST:
210 event.event = IB_EVENT_COMM_EST;
211 break;
212 case MLX5_EVENT_TYPE_SQ_DRAINED:
213 event.event = IB_EVENT_SQ_DRAINED;
214 break;
215 case MLX5_EVENT_TYPE_SRQ_LAST_WQE:
216 event.event = IB_EVENT_QP_LAST_WQE_REACHED;
217 break;
218 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
219 event.event = IB_EVENT_QP_FATAL;
220 break;
221 case MLX5_EVENT_TYPE_PATH_MIG_FAILED:
222 event.event = IB_EVENT_PATH_MIG_ERR;
223 break;
224 case MLX5_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
225 event.event = IB_EVENT_QP_REQ_ERR;
226 break;
227 case MLX5_EVENT_TYPE_WQ_ACCESS_ERROR:
228 event.event = IB_EVENT_QP_ACCESS_ERR;
229 break;
230 default:
231 pr_warn("mlx5_ib: Unexpected event type %d on QP %06x\n", type, qp->qpn);
232 return;
233 }
234
235 ibqp->event_handler(&event, ibqp->qp_context);
236 }
237}
238
239static int set_rq_size(struct mlx5_ib_dev *dev, struct ib_qp_cap *cap,
240 int has_rq, struct mlx5_ib_qp *qp, struct mlx5_ib_create_qp *ucmd)
241{
242 int wqe_size;
243 int wq_size;
244
245 /* Sanity check RQ size before proceeding */
Saeed Mahameed938fe832015-05-28 22:28:41 +0300246 if (cap->max_recv_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz)))
Eli Cohene126ba92013-07-07 17:25:49 +0300247 return -EINVAL;
248
249 if (!has_rq) {
250 qp->rq.max_gs = 0;
251 qp->rq.wqe_cnt = 0;
252 qp->rq.wqe_shift = 0;
Noa Osherovich0540d812016-06-04 15:15:32 +0300253 cap->max_recv_wr = 0;
254 cap->max_recv_sge = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300255 } else {
256 if (ucmd) {
257 qp->rq.wqe_cnt = ucmd->rq_wqe_count;
258 qp->rq.wqe_shift = ucmd->rq_wqe_shift;
259 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
260 qp->rq.max_post = qp->rq.wqe_cnt;
261 } else {
262 wqe_size = qp->wq_sig ? sizeof(struct mlx5_wqe_signature_seg) : 0;
263 wqe_size += cap->max_recv_sge * sizeof(struct mlx5_wqe_data_seg);
264 wqe_size = roundup_pow_of_two(wqe_size);
265 wq_size = roundup_pow_of_two(cap->max_recv_wr) * wqe_size;
266 wq_size = max_t(int, wq_size, MLX5_SEND_WQE_BB);
267 qp->rq.wqe_cnt = wq_size / wqe_size;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300268 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq)) {
Eli Cohene126ba92013-07-07 17:25:49 +0300269 mlx5_ib_dbg(dev, "wqe_size %d, max %d\n",
270 wqe_size,
Saeed Mahameed938fe832015-05-28 22:28:41 +0300271 MLX5_CAP_GEN(dev->mdev,
272 max_wqe_sz_rq));
Eli Cohene126ba92013-07-07 17:25:49 +0300273 return -EINVAL;
274 }
275 qp->rq.wqe_shift = ilog2(wqe_size);
276 qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
277 qp->rq.max_post = qp->rq.wqe_cnt;
278 }
279 }
280
281 return 0;
282}
283
Erez Shitritf0313962016-02-21 16:27:17 +0200284static int sq_overhead(struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +0300285{
Andi Shyti618af382013-07-16 15:35:01 +0200286 int size = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300287
Erez Shitritf0313962016-02-21 16:27:17 +0200288 switch (attr->qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +0300289 case IB_QPT_XRC_INI:
Eli Cohenb125a542013-09-11 16:35:22 +0300290 size += sizeof(struct mlx5_wqe_xrc_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300291 /* fall through */
292 case IB_QPT_RC:
293 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200294 max(sizeof(struct mlx5_wqe_atomic_seg) +
295 sizeof(struct mlx5_wqe_raddr_seg),
296 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
297 sizeof(struct mlx5_mkey_seg));
Eli Cohene126ba92013-07-07 17:25:49 +0300298 break;
299
Eli Cohenb125a542013-09-11 16:35:22 +0300300 case IB_QPT_XRC_TGT:
301 return 0;
302
Eli Cohene126ba92013-07-07 17:25:49 +0300303 case IB_QPT_UC:
Eli Cohenb125a542013-09-11 16:35:22 +0300304 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Leon Romanovsky75c1657e2016-02-11 21:09:57 +0200305 max(sizeof(struct mlx5_wqe_raddr_seg),
306 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
307 sizeof(struct mlx5_mkey_seg));
Eli Cohene126ba92013-07-07 17:25:49 +0300308 break;
309
310 case IB_QPT_UD:
Erez Shitritf0313962016-02-21 16:27:17 +0200311 if (attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
312 size += sizeof(struct mlx5_wqe_eth_pad) +
313 sizeof(struct mlx5_wqe_eth_seg);
314 /* fall through */
Eli Cohene126ba92013-07-07 17:25:49 +0300315 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +0200316 case MLX5_IB_QPT_HW_GSI:
Eli Cohenb125a542013-09-11 16:35:22 +0300317 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300318 sizeof(struct mlx5_wqe_datagram_seg);
319 break;
320
321 case MLX5_IB_QPT_REG_UMR:
Eli Cohenb125a542013-09-11 16:35:22 +0300322 size += sizeof(struct mlx5_wqe_ctrl_seg) +
Eli Cohene126ba92013-07-07 17:25:49 +0300323 sizeof(struct mlx5_wqe_umr_ctrl_seg) +
324 sizeof(struct mlx5_mkey_seg);
325 break;
326
327 default:
328 return -EINVAL;
329 }
330
331 return size;
332}
333
334static int calc_send_wqe(struct ib_qp_init_attr *attr)
335{
336 int inl_size = 0;
337 int size;
338
Erez Shitritf0313962016-02-21 16:27:17 +0200339 size = sq_overhead(attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300340 if (size < 0)
341 return size;
342
343 if (attr->cap.max_inline_data) {
344 inl_size = size + sizeof(struct mlx5_wqe_inline_seg) +
345 attr->cap.max_inline_data;
346 }
347
348 size += attr->cap.max_send_sge * sizeof(struct mlx5_wqe_data_seg);
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200349 if (attr->create_flags & IB_QP_CREATE_SIGNATURE_EN &&
350 ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB) < MLX5_SIG_WQE_SIZE)
351 return MLX5_SIG_WQE_SIZE;
352 else
353 return ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB);
Eli Cohene126ba92013-07-07 17:25:49 +0300354}
355
Eli Cohen288c01b2016-10-27 16:36:45 +0300356static int get_send_sge(struct ib_qp_init_attr *attr, int wqe_size)
357{
358 int max_sge;
359
360 if (attr->qp_type == IB_QPT_RC)
361 max_sge = (min_t(int, wqe_size, 512) -
362 sizeof(struct mlx5_wqe_ctrl_seg) -
363 sizeof(struct mlx5_wqe_raddr_seg)) /
364 sizeof(struct mlx5_wqe_data_seg);
365 else if (attr->qp_type == IB_QPT_XRC_INI)
366 max_sge = (min_t(int, wqe_size, 512) -
367 sizeof(struct mlx5_wqe_ctrl_seg) -
368 sizeof(struct mlx5_wqe_xrc_seg) -
369 sizeof(struct mlx5_wqe_raddr_seg)) /
370 sizeof(struct mlx5_wqe_data_seg);
371 else
372 max_sge = (wqe_size - sq_overhead(attr)) /
373 sizeof(struct mlx5_wqe_data_seg);
374
375 return min_t(int, max_sge, wqe_size - sq_overhead(attr) /
376 sizeof(struct mlx5_wqe_data_seg));
377}
378
Eli Cohene126ba92013-07-07 17:25:49 +0300379static int calc_sq_size(struct mlx5_ib_dev *dev, struct ib_qp_init_attr *attr,
380 struct mlx5_ib_qp *qp)
381{
382 int wqe_size;
383 int wq_size;
384
385 if (!attr->cap.max_send_wr)
386 return 0;
387
388 wqe_size = calc_send_wqe(attr);
389 mlx5_ib_dbg(dev, "wqe_size %d\n", wqe_size);
390 if (wqe_size < 0)
391 return wqe_size;
392
Saeed Mahameed938fe832015-05-28 22:28:41 +0300393 if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
Eli Cohenb125a542013-09-11 16:35:22 +0300394 mlx5_ib_dbg(dev, "wqe_size(%d) > max_sq_desc_sz(%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +0300395 wqe_size, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
Eli Cohene126ba92013-07-07 17:25:49 +0300396 return -EINVAL;
397 }
398
Erez Shitritf0313962016-02-21 16:27:17 +0200399 qp->max_inline_data = wqe_size - sq_overhead(attr) -
400 sizeof(struct mlx5_wqe_inline_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300401 attr->cap.max_inline_data = qp->max_inline_data;
402
Sagi Grimberge1e66cc2014-02-23 14:19:07 +0200403 if (attr->create_flags & IB_QP_CREATE_SIGNATURE_EN)
404 qp->signature_en = true;
405
Eli Cohene126ba92013-07-07 17:25:49 +0300406 wq_size = roundup_pow_of_two(attr->cap.max_send_wr * wqe_size);
407 qp->sq.wqe_cnt = wq_size / MLX5_SEND_WQE_BB;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300408 if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
Bart Van Assche1974ab92016-12-05 17:19:52 -0800409 mlx5_ib_dbg(dev, "send queue size (%d * %d / %d -> %d) exceeds limits(%d)\n",
410 attr->cap.max_send_wr, wqe_size, MLX5_SEND_WQE_BB,
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 Cohen2f5ff262017-01-03 23:55:21 +0200478static int first_med_bfreg(void)
Eli Cohenc1be5232014-01-14 17:45:12 +0200479{
480 return 1;
481}
482
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200483enum {
484 /* this is the first blue flame register in the array of bfregs assigned
485 * to a processes. Since we do not use it for blue flame but rather
486 * regular 64 bit doorbells, we do not need a lock for maintaiing
487 * "odd/even" order
488 */
489 NUM_NON_BLUE_FLAME_BFREGS = 1,
490};
491
Eli Cohenb037c292017-01-03 23:55:26 +0200492static int max_bfregs(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi)
493{
494 return get_num_uars(dev, bfregi) * MLX5_NON_FP_BFREGS_PER_UAR;
495}
496
497static int num_med_bfreg(struct mlx5_ib_dev *dev,
498 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200499{
500 int n;
501
Eli Cohenb037c292017-01-03 23:55:26 +0200502 n = max_bfregs(dev, bfregi) - bfregi->num_low_latency_bfregs -
503 NUM_NON_BLUE_FLAME_BFREGS;
Eli Cohenc1be5232014-01-14 17:45:12 +0200504
505 return n >= 0 ? n : 0;
506}
507
Eli Cohenb037c292017-01-03 23:55:26 +0200508static int first_hi_bfreg(struct mlx5_ib_dev *dev,
509 struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200510{
511 int med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200512
Eli Cohenb037c292017-01-03 23:55:26 +0200513 med = num_med_bfreg(dev, bfregi);
514 return ++med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200515}
516
Eli Cohenb037c292017-01-03 23:55:26 +0200517static int alloc_high_class_bfreg(struct mlx5_ib_dev *dev,
518 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300519{
Eli Cohene126ba92013-07-07 17:25:49 +0300520 int i;
521
Eli Cohenb037c292017-01-03 23:55:26 +0200522 for (i = first_hi_bfreg(dev, bfregi); i < max_bfregs(dev, bfregi); i++) {
523 if (!bfregi->count[i]) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200524 bfregi->count[i]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300525 return i;
526 }
527 }
528
529 return -ENOMEM;
530}
531
Eli Cohenb037c292017-01-03 23:55:26 +0200532static int alloc_med_class_bfreg(struct mlx5_ib_dev *dev,
533 struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300534{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200535 int minidx = first_med_bfreg();
Eli Cohene126ba92013-07-07 17:25:49 +0300536 int i;
537
Eli Cohenb037c292017-01-03 23:55:26 +0200538 for (i = first_med_bfreg(); i < first_hi_bfreg(dev, bfregi); i++) {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200539 if (bfregi->count[i] < bfregi->count[minidx])
Eli Cohene126ba92013-07-07 17:25:49 +0300540 minidx = i;
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200541 if (!bfregi->count[minidx])
542 break;
Eli Cohene126ba92013-07-07 17:25:49 +0300543 }
544
Eli Cohen2f5ff262017-01-03 23:55:21 +0200545 bfregi->count[minidx]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300546 return minidx;
547}
548
Eli Cohenb037c292017-01-03 23:55:26 +0200549static int alloc_bfreg(struct mlx5_ib_dev *dev,
550 struct mlx5_bfreg_info *bfregi,
Eli Cohen2f5ff262017-01-03 23:55:21 +0200551 enum mlx5_ib_latency_class lat)
Eli Cohene126ba92013-07-07 17:25:49 +0300552{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200553 int bfregn = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300554
Eli Cohen2f5ff262017-01-03 23:55:21 +0200555 mutex_lock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300556 switch (lat) {
557 case MLX5_IB_LATENCY_CLASS_LOW:
Eli Cohen0b80c14f02017-01-03 23:55:22 +0200558 BUILD_BUG_ON(NUM_NON_BLUE_FLAME_BFREGS != 1);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200559 bfregn = 0;
560 bfregi->count[bfregn]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300561 break;
562
563 case MLX5_IB_LATENCY_CLASS_MEDIUM:
Eli Cohen2f5ff262017-01-03 23:55:21 +0200564 if (bfregi->ver < 2)
565 bfregn = -ENOMEM;
Eli Cohen78c0f982014-01-30 13:49:48 +0200566 else
Eli Cohenb037c292017-01-03 23:55:26 +0200567 bfregn = alloc_med_class_bfreg(dev, bfregi);
Eli Cohene126ba92013-07-07 17:25:49 +0300568 break;
569
570 case MLX5_IB_LATENCY_CLASS_HIGH:
Eli Cohen2f5ff262017-01-03 23:55:21 +0200571 if (bfregi->ver < 2)
572 bfregn = -ENOMEM;
Eli Cohen78c0f982014-01-30 13:49:48 +0200573 else
Eli Cohenb037c292017-01-03 23:55:26 +0200574 bfregn = alloc_high_class_bfreg(dev, bfregi);
Eli Cohene126ba92013-07-07 17:25:49 +0300575 break;
576 }
Eli Cohen2f5ff262017-01-03 23:55:21 +0200577 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300578
Eli Cohen2f5ff262017-01-03 23:55:21 +0200579 return bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300580}
581
Eli Cohenb037c292017-01-03 23:55:26 +0200582static void free_bfreg(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi, int bfregn)
Eli Cohene126ba92013-07-07 17:25:49 +0300583{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200584 mutex_lock(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +0200585 bfregi->count[bfregn]--;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200586 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300587}
588
589static enum mlx5_qp_state to_mlx5_state(enum ib_qp_state state)
590{
591 switch (state) {
592 case IB_QPS_RESET: return MLX5_QP_STATE_RST;
593 case IB_QPS_INIT: return MLX5_QP_STATE_INIT;
594 case IB_QPS_RTR: return MLX5_QP_STATE_RTR;
595 case IB_QPS_RTS: return MLX5_QP_STATE_RTS;
596 case IB_QPS_SQD: return MLX5_QP_STATE_SQD;
597 case IB_QPS_SQE: return MLX5_QP_STATE_SQER;
598 case IB_QPS_ERR: return MLX5_QP_STATE_ERR;
599 default: return -1;
600 }
601}
602
603static int to_mlx5_st(enum ib_qp_type type)
604{
605 switch (type) {
606 case IB_QPT_RC: return MLX5_QP_ST_RC;
607 case IB_QPT_UC: return MLX5_QP_ST_UC;
608 case IB_QPT_UD: return MLX5_QP_ST_UD;
609 case MLX5_IB_QPT_REG_UMR: return MLX5_QP_ST_REG_UMR;
610 case IB_QPT_XRC_INI:
611 case IB_QPT_XRC_TGT: return MLX5_QP_ST_XRC;
612 case IB_QPT_SMI: return MLX5_QP_ST_QP0;
Haggai Erand16e91d2016-02-29 15:45:05 +0200613 case MLX5_IB_QPT_HW_GSI: return MLX5_QP_ST_QP1;
Eli Cohene126ba92013-07-07 17:25:49 +0300614 case IB_QPT_RAW_IPV6: return MLX5_QP_ST_RAW_IPV6;
Eli Cohene126ba92013-07-07 17:25:49 +0300615 case IB_QPT_RAW_PACKET:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200616 case IB_QPT_RAW_ETHERTYPE: return MLX5_QP_ST_RAW_ETHERTYPE;
Eli Cohene126ba92013-07-07 17:25:49 +0300617 case IB_QPT_MAX:
618 default: return -EINVAL;
619 }
620}
621
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300622static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq,
623 struct mlx5_ib_cq *recv_cq);
624static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq,
625 struct mlx5_ib_cq *recv_cq);
626
Eli Cohenb037c292017-01-03 23:55:26 +0200627static int bfregn_to_uar_index(struct mlx5_ib_dev *dev,
628 struct mlx5_bfreg_info *bfregi, int bfregn)
Eli Cohene126ba92013-07-07 17:25:49 +0300629{
Eli Cohenb037c292017-01-03 23:55:26 +0200630 int bfregs_per_sys_page;
631 int index_of_sys_page;
632 int offset;
633
634 bfregs_per_sys_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k) *
635 MLX5_NON_FP_BFREGS_PER_UAR;
636 index_of_sys_page = bfregn / bfregs_per_sys_page;
637
638 offset = bfregn % bfregs_per_sys_page / MLX5_NON_FP_BFREGS_PER_UAR;
639
640 return bfregi->sys_pages[index_of_sys_page] + offset;
Eli Cohene126ba92013-07-07 17:25:49 +0300641}
642
majd@mellanox.com19098df2016-01-14 19:13:03 +0200643static int mlx5_ib_umem_get(struct mlx5_ib_dev *dev,
644 struct ib_pd *pd,
645 unsigned long addr, size_t size,
646 struct ib_umem **umem,
647 int *npages, int *page_shift, int *ncont,
648 u32 *offset)
649{
650 int err;
651
652 *umem = ib_umem_get(pd->uobject->context, addr, size, 0, 0);
653 if (IS_ERR(*umem)) {
654 mlx5_ib_dbg(dev, "umem_get failed\n");
655 return PTR_ERR(*umem);
656 }
657
Majd Dibbiny762f8992016-10-27 16:36:47 +0300658 mlx5_ib_cont_pages(*umem, addr, 0, npages, page_shift, ncont, NULL);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200659
660 err = mlx5_ib_get_buf_offset(addr, *page_shift, offset);
661 if (err) {
662 mlx5_ib_warn(dev, "bad offset\n");
663 goto err_umem;
664 }
665
666 mlx5_ib_dbg(dev, "addr 0x%lx, size %zu, npages %d, page_shift %d, ncont %d, offset %d\n",
667 addr, size, *npages, *page_shift, *ncont, *offset);
668
669 return 0;
670
671err_umem:
672 ib_umem_release(*umem);
673 *umem = NULL;
674
675 return err;
676}
677
Maor Gottliebfe248c32017-05-30 10:29:14 +0300678static void destroy_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
679 struct mlx5_ib_rwq *rwq)
Yishai Hadas79b20a62016-05-23 15:20:50 +0300680{
681 struct mlx5_ib_ucontext *context;
682
Maor Gottliebfe248c32017-05-30 10:29:14 +0300683 if (rwq->create_flags & MLX5_IB_WQ_FLAGS_DELAY_DROP)
684 atomic_dec(&dev->delay_drop.rqs_cnt);
685
Yishai Hadas79b20a62016-05-23 15:20:50 +0300686 context = to_mucontext(pd->uobject->context);
687 mlx5_ib_db_unmap_user(context, &rwq->db);
688 if (rwq->umem)
689 ib_umem_release(rwq->umem);
690}
691
692static int create_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
693 struct mlx5_ib_rwq *rwq,
694 struct mlx5_ib_create_wq *ucmd)
695{
696 struct mlx5_ib_ucontext *context;
697 int page_shift = 0;
698 int npages;
699 u32 offset = 0;
700 int ncont = 0;
701 int err;
702
703 if (!ucmd->buf_addr)
704 return -EINVAL;
705
706 context = to_mucontext(pd->uobject->context);
707 rwq->umem = ib_umem_get(pd->uobject->context, ucmd->buf_addr,
708 rwq->buf_size, 0, 0);
709 if (IS_ERR(rwq->umem)) {
710 mlx5_ib_dbg(dev, "umem_get failed\n");
711 err = PTR_ERR(rwq->umem);
712 return err;
713 }
714
Majd Dibbiny762f8992016-10-27 16:36:47 +0300715 mlx5_ib_cont_pages(rwq->umem, ucmd->buf_addr, 0, &npages, &page_shift,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300716 &ncont, NULL);
717 err = mlx5_ib_get_buf_offset(ucmd->buf_addr, page_shift,
718 &rwq->rq_page_offset);
719 if (err) {
720 mlx5_ib_warn(dev, "bad offset\n");
721 goto err_umem;
722 }
723
724 rwq->rq_num_pas = ncont;
725 rwq->page_shift = page_shift;
726 rwq->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
727 rwq->wq_sig = !!(ucmd->flags & MLX5_WQ_FLAG_SIGNATURE);
728
729 mlx5_ib_dbg(dev, "addr 0x%llx, size %zd, npages %d, page_shift %d, ncont %d, offset %d\n",
730 (unsigned long long)ucmd->buf_addr, rwq->buf_size,
731 npages, page_shift, ncont, offset);
732
733 err = mlx5_ib_db_map_user(context, ucmd->db_addr, &rwq->db);
734 if (err) {
735 mlx5_ib_dbg(dev, "map failed\n");
736 goto err_umem;
737 }
738
739 rwq->create_type = MLX5_WQ_USER;
740 return 0;
741
742err_umem:
743 ib_umem_release(rwq->umem);
744 return err;
745}
746
Eli Cohenb037c292017-01-03 23:55:26 +0200747static int adjust_bfregn(struct mlx5_ib_dev *dev,
748 struct mlx5_bfreg_info *bfregi, int bfregn)
749{
750 return bfregn / MLX5_NON_FP_BFREGS_PER_UAR * MLX5_BFREGS_PER_UAR +
751 bfregn % MLX5_NON_FP_BFREGS_PER_UAR;
752}
753
Eli Cohene126ba92013-07-07 17:25:49 +0300754static int create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
755 struct mlx5_ib_qp *qp, struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200756 struct ib_qp_init_attr *attr,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300757 u32 **in,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200758 struct mlx5_ib_create_qp_resp *resp, int *inlen,
759 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300760{
761 struct mlx5_ib_ucontext *context;
762 struct mlx5_ib_create_qp ucmd;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200763 struct mlx5_ib_ubuffer *ubuffer = &base->ubuffer;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200764 int page_shift = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300765 int uar_index;
766 int npages;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200767 u32 offset = 0;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200768 int bfregn;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200769 int ncont = 0;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300770 __be64 *pas;
771 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300772 int err;
773
774 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
775 if (err) {
776 mlx5_ib_dbg(dev, "copy failed\n");
777 return err;
778 }
779
780 context = to_mucontext(pd->uobject->context);
781 /*
782 * TBD: should come from the verbs when we have the API
783 */
Leon Romanovsky051f2632015-12-20 12:16:11 +0200784 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
785 /* In CROSS_CHANNEL CQ and QP must use the same UAR */
Eli Cohen2f5ff262017-01-03 23:55:21 +0200786 bfregn = MLX5_CROSS_CHANNEL_BFREG;
Leon Romanovsky051f2632015-12-20 12:16:11 +0200787 else {
Eli Cohenb037c292017-01-03 23:55:26 +0200788 bfregn = alloc_bfreg(dev, &context->bfregi, MLX5_IB_LATENCY_CLASS_HIGH);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200789 if (bfregn < 0) {
790 mlx5_ib_dbg(dev, "failed to allocate low latency BFREG\n");
Leon Romanovsky051f2632015-12-20 12:16:11 +0200791 mlx5_ib_dbg(dev, "reverting to medium latency\n");
Eli Cohenb037c292017-01-03 23:55:26 +0200792 bfregn = alloc_bfreg(dev, &context->bfregi, MLX5_IB_LATENCY_CLASS_MEDIUM);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200793 if (bfregn < 0) {
794 mlx5_ib_dbg(dev, "failed to allocate medium latency BFREG\n");
Leon Romanovsky051f2632015-12-20 12:16:11 +0200795 mlx5_ib_dbg(dev, "reverting to high latency\n");
Eli Cohenb037c292017-01-03 23:55:26 +0200796 bfregn = alloc_bfreg(dev, &context->bfregi, MLX5_IB_LATENCY_CLASS_LOW);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200797 if (bfregn < 0) {
798 mlx5_ib_warn(dev, "bfreg allocation failed\n");
799 return bfregn;
Leon Romanovsky051f2632015-12-20 12:16:11 +0200800 }
Eli Cohenc1be5232014-01-14 17:45:12 +0200801 }
Eli Cohene126ba92013-07-07 17:25:49 +0300802 }
803 }
804
Eli Cohenb037c292017-01-03 23:55:26 +0200805 uar_index = bfregn_to_uar_index(dev, &context->bfregi, bfregn);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200806 mlx5_ib_dbg(dev, "bfregn 0x%x, uar_index 0x%x\n", bfregn, uar_index);
Eli Cohene126ba92013-07-07 17:25:49 +0300807
Haggai Eran48fea832014-05-22 14:50:11 +0300808 qp->rq.offset = 0;
809 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
810 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
811
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200812 err = set_user_buf_size(dev, qp, &ucmd, base, attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300813 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200814 goto err_bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300815
majd@mellanox.com19098df2016-01-14 19:13:03 +0200816 if (ucmd.buf_addr && ubuffer->buf_size) {
817 ubuffer->buf_addr = ucmd.buf_addr;
818 err = mlx5_ib_umem_get(dev, pd, ubuffer->buf_addr,
819 ubuffer->buf_size,
820 &ubuffer->umem, &npages, &page_shift,
821 &ncont, &offset);
822 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200823 goto err_bfreg;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200824 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +0200825 ubuffer->umem = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300826 }
Eli Cohene126ba92013-07-07 17:25:49 +0300827
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300828 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
829 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300830 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300831 if (!*in) {
832 err = -ENOMEM;
833 goto err_umem;
834 }
Eli Cohene126ba92013-07-07 17:25:49 +0300835
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300836 pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas);
837 if (ubuffer->umem)
838 mlx5_ib_populate_pas(dev, ubuffer->umem, page_shift, pas, 0);
839
840 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
841
842 MLX5_SET(qpc, qpc, log_page_size, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
843 MLX5_SET(qpc, qpc, page_offset, offset);
844
845 MLX5_SET(qpc, qpc, uar_page, uar_index);
Eli Cohenb037c292017-01-03 23:55:26 +0200846 resp->bfreg_index = adjust_bfregn(dev, &context->bfregi, bfregn);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200847 qp->bfregn = bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300848
849 err = mlx5_ib_db_map_user(context, ucmd.db_addr, &qp->db);
850 if (err) {
851 mlx5_ib_dbg(dev, "map failed\n");
852 goto err_free;
853 }
854
855 err = ib_copy_to_udata(udata, resp, sizeof(*resp));
856 if (err) {
857 mlx5_ib_dbg(dev, "copy failed\n");
858 goto err_unmap;
859 }
860 qp->create_type = MLX5_QP_USER;
861
862 return 0;
863
864err_unmap:
865 mlx5_ib_db_unmap_user(context, &qp->db);
866
867err_free:
Al Viro479163f2014-11-20 08:13:57 +0000868 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +0300869
870err_umem:
majd@mellanox.com19098df2016-01-14 19:13:03 +0200871 if (ubuffer->umem)
872 ib_umem_release(ubuffer->umem);
Eli Cohene126ba92013-07-07 17:25:49 +0300873
Eli Cohen2f5ff262017-01-03 23:55:21 +0200874err_bfreg:
Eli Cohenb037c292017-01-03 23:55:26 +0200875 free_bfreg(dev, &context->bfregi, bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300876 return err;
877}
878
Eli Cohenb037c292017-01-03 23:55:26 +0200879static void destroy_qp_user(struct mlx5_ib_dev *dev, struct ib_pd *pd,
880 struct mlx5_ib_qp *qp, struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300881{
882 struct mlx5_ib_ucontext *context;
883
884 context = to_mucontext(pd->uobject->context);
885 mlx5_ib_db_unmap_user(context, &qp->db);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200886 if (base->ubuffer.umem)
887 ib_umem_release(base->ubuffer.umem);
Eli Cohenb037c292017-01-03 23:55:26 +0200888 free_bfreg(dev, &context->bfregi, qp->bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300889}
890
891static int create_kernel_qp(struct mlx5_ib_dev *dev,
892 struct ib_qp_init_attr *init_attr,
893 struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300894 u32 **in, int *inlen,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200895 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300896{
Eli Cohene126ba92013-07-07 17:25:49 +0300897 int uar_index;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300898 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300899 int err;
900
Erez Shitritf0313962016-02-21 16:27:17 +0200901 if (init_attr->create_flags & ~(IB_QP_CREATE_SIGNATURE_EN |
902 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
Haggai Eranb11a4f92016-02-29 15:45:03 +0200903 IB_QP_CREATE_IPOIB_UD_LSO |
Erez Shitrit93d576a2017-04-13 06:37:06 +0300904 IB_QP_CREATE_NETIF_QP |
Haggai Eranb11a4f92016-02-29 15:45:03 +0200905 mlx5_ib_create_qp_sqpn_qp1()))
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200906 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300907
908 if (init_attr->qp_type == MLX5_IB_QPT_REG_UMR)
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200909 qp->bf.bfreg = &dev->fp_bfreg;
910 else
911 qp->bf.bfreg = &dev->bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300912
Eli Cohend8030b02017-02-09 19:31:47 +0200913 /* We need to divide by two since each register is comprised of
914 * two buffers of identical size, namely odd and even
915 */
916 qp->bf.buf_size = (1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size)) / 2;
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200917 uar_index = qp->bf.bfreg->index;
Eli Cohene126ba92013-07-07 17:25:49 +0300918
919 err = calc_sq_size(dev, init_attr, qp);
920 if (err < 0) {
921 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200922 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300923 }
924
925 qp->rq.offset = 0;
926 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200927 base->ubuffer.buf_size = err + (qp->rq.wqe_cnt << qp->rq.wqe_shift);
Eli Cohene126ba92013-07-07 17:25:49 +0300928
majd@mellanox.com19098df2016-01-14 19:13:03 +0200929 err = mlx5_buf_alloc(dev->mdev, base->ubuffer.buf_size, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +0300930 if (err) {
931 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200932 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300933 }
934
935 qp->sq.qend = mlx5_get_send_wqe(qp, qp->sq.wqe_cnt);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300936 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
937 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * qp->buf.npages;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +0300938 *in = kvzalloc(*inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300939 if (!*in) {
940 err = -ENOMEM;
941 goto err_buf;
942 }
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300943
944 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
945 MLX5_SET(qpc, qpc, uar_page, uar_index);
946 MLX5_SET(qpc, qpc, log_page_size, qp->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
947
Eli Cohene126ba92013-07-07 17:25:49 +0300948 /* Set "fast registration enabled" for all kernel QPs */
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300949 MLX5_SET(qpc, qpc, fre, 1);
950 MLX5_SET(qpc, qpc, rlky, 1);
Eli Cohene126ba92013-07-07 17:25:49 +0300951
Haggai Eranb11a4f92016-02-29 15:45:03 +0200952 if (init_attr->create_flags & mlx5_ib_create_qp_sqpn_qp1()) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300953 MLX5_SET(qpc, qpc, deth_sqpn, 1);
Haggai Eranb11a4f92016-02-29 15:45:03 +0200954 qp->flags |= MLX5_IB_QP_SQPN_QP1;
955 }
956
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300957 mlx5_fill_page_array(&qp->buf,
958 (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas));
Eli Cohene126ba92013-07-07 17:25:49 +0300959
Jack Morgenstein9603b612014-07-28 23:30:22 +0300960 err = mlx5_db_alloc(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300961 if (err) {
962 mlx5_ib_dbg(dev, "err %d\n", err);
963 goto err_free;
964 }
965
Eli Cohene126ba92013-07-07 17:25:49 +0300966 qp->sq.wrid = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wrid), GFP_KERNEL);
967 qp->sq.wr_data = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wr_data), GFP_KERNEL);
968 qp->rq.wrid = kmalloc(qp->rq.wqe_cnt * sizeof(*qp->rq.wrid), GFP_KERNEL);
969 qp->sq.w_list = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.w_list), GFP_KERNEL);
970 qp->sq.wqe_head = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wqe_head), GFP_KERNEL);
971
972 if (!qp->sq.wrid || !qp->sq.wr_data || !qp->rq.wrid ||
973 !qp->sq.w_list || !qp->sq.wqe_head) {
974 err = -ENOMEM;
975 goto err_wrid;
976 }
977 qp->create_type = MLX5_QP_KERNEL;
978
979 return 0;
980
981err_wrid:
Eli Cohene126ba92013-07-07 17:25:49 +0300982 kfree(qp->sq.wqe_head);
983 kfree(qp->sq.w_list);
984 kfree(qp->sq.wrid);
985 kfree(qp->sq.wr_data);
986 kfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +0200987 mlx5_db_free(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300988
989err_free:
Al Viro479163f2014-11-20 08:13:57 +0000990 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +0300991
992err_buf:
Jack Morgenstein9603b612014-07-28 23:30:22 +0300993 mlx5_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +0300994 return err;
995}
996
997static void destroy_qp_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
998{
Eli Cohene126ba92013-07-07 17:25:49 +0300999 kfree(qp->sq.wqe_head);
1000 kfree(qp->sq.w_list);
1001 kfree(qp->sq.wrid);
1002 kfree(qp->sq.wr_data);
1003 kfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001004 mlx5_db_free(dev->mdev, &qp->db);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001005 mlx5_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001006}
1007
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001008static u32 get_rx_type(struct mlx5_ib_qp *qp, struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +03001009{
1010 if (attr->srq || (attr->qp_type == IB_QPT_XRC_TGT) ||
1011 (attr->qp_type == IB_QPT_XRC_INI))
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001012 return MLX5_SRQ_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001013 else if (!qp->has_rq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001014 return MLX5_ZERO_LEN_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001015 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001016 return MLX5_NON_ZERO_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001017}
1018
1019static int is_connected(enum ib_qp_type qp_type)
1020{
1021 if (qp_type == IB_QPT_RC || qp_type == IB_QPT_UC)
1022 return 1;
1023
1024 return 0;
1025}
1026
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001027static int create_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
1028 struct mlx5_ib_sq *sq, u32 tdn)
1029{
Saeed Mahameedc4f287c2016-07-19 20:17:12 +03001030 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001031 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1032
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001033 MLX5_SET(tisc, tisc, transport_domain, tdn);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001034 return mlx5_core_create_tis(dev->mdev, in, sizeof(in), &sq->tisn);
1035}
1036
1037static void destroy_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
1038 struct mlx5_ib_sq *sq)
1039{
1040 mlx5_core_destroy_tis(dev->mdev, sq->tisn);
1041}
1042
1043static int create_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1044 struct mlx5_ib_sq *sq, void *qpin,
1045 struct ib_pd *pd)
1046{
1047 struct mlx5_ib_ubuffer *ubuffer = &sq->ubuffer;
1048 __be64 *pas;
1049 void *in;
1050 void *sqc;
1051 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1052 void *wq;
1053 int inlen;
1054 int err;
1055 int page_shift = 0;
1056 int npages;
1057 int ncont = 0;
1058 u32 offset = 0;
1059
1060 err = mlx5_ib_umem_get(dev, pd, ubuffer->buf_addr, ubuffer->buf_size,
1061 &sq->ubuffer.umem, &npages, &page_shift,
1062 &ncont, &offset);
1063 if (err)
1064 return err;
1065
1066 inlen = MLX5_ST_SZ_BYTES(create_sq_in) + sizeof(u64) * ncont;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001067 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001068 if (!in) {
1069 err = -ENOMEM;
1070 goto err_umem;
1071 }
1072
1073 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1074 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
1075 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1076 MLX5_SET(sqc, sqc, user_index, MLX5_GET(qpc, qpc, user_index));
1077 MLX5_SET(sqc, sqc, cqn, MLX5_GET(qpc, qpc, cqn_snd));
1078 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1079 MLX5_SET(sqc, sqc, tis_num_0, sq->tisn);
1080
1081 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1082 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1083 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1084 MLX5_SET(wq, wq, uar_page, MLX5_GET(qpc, qpc, uar_page));
1085 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1086 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1087 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_sq_size));
1088 MLX5_SET(wq, wq, log_wq_pg_sz, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1089 MLX5_SET(wq, wq, page_offset, offset);
1090
1091 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1092 mlx5_ib_populate_pas(dev, sq->ubuffer.umem, page_shift, pas, 0);
1093
1094 err = mlx5_core_create_sq_tracked(dev->mdev, in, inlen, &sq->base.mqp);
1095
1096 kvfree(in);
1097
1098 if (err)
1099 goto err_umem;
1100
1101 return 0;
1102
1103err_umem:
1104 ib_umem_release(sq->ubuffer.umem);
1105 sq->ubuffer.umem = NULL;
1106
1107 return err;
1108}
1109
1110static void destroy_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1111 struct mlx5_ib_sq *sq)
1112{
1113 mlx5_core_destroy_sq_tracked(dev->mdev, &sq->base.mqp);
1114 ib_umem_release(sq->ubuffer.umem);
1115}
1116
1117static int get_rq_pas_size(void *qpc)
1118{
1119 u32 log_page_size = MLX5_GET(qpc, qpc, log_page_size) + 12;
1120 u32 log_rq_stride = MLX5_GET(qpc, qpc, log_rq_stride);
1121 u32 log_rq_size = MLX5_GET(qpc, qpc, log_rq_size);
1122 u32 page_offset = MLX5_GET(qpc, qpc, page_offset);
1123 u32 po_quanta = 1 << (log_page_size - 6);
1124 u32 rq_sz = 1 << (log_rq_size + 4 + log_rq_stride);
1125 u32 page_size = 1 << log_page_size;
1126 u32 rq_sz_po = rq_sz + (page_offset * po_quanta);
1127 u32 rq_num_pas = (rq_sz_po + page_size - 1) / page_size;
1128
1129 return rq_num_pas * sizeof(u64);
1130}
1131
1132static int create_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1133 struct mlx5_ib_rq *rq, void *qpin)
1134{
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001135 struct mlx5_ib_qp *mqp = rq->base.container_mibqp;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001136 __be64 *pas;
1137 __be64 *qp_pas;
1138 void *in;
1139 void *rqc;
1140 void *wq;
1141 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1142 int inlen;
1143 int err;
1144 u32 rq_pas_size = get_rq_pas_size(qpc);
1145
1146 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + rq_pas_size;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001147 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001148 if (!in)
1149 return -ENOMEM;
1150
1151 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001152 if (!(rq->flags & MLX5_IB_RQ_CVLAN_STRIPPING))
1153 MLX5_SET(rqc, rqc, vsd, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001154 MLX5_SET(rqc, rqc, mem_rq_type, MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
1155 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
1156 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
1157 MLX5_SET(rqc, rqc, user_index, MLX5_GET(qpc, qpc, user_index));
1158 MLX5_SET(rqc, rqc, cqn, MLX5_GET(qpc, qpc, cqn_rcv));
1159
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001160 if (mqp->flags & MLX5_IB_QP_CAP_SCATTER_FCS)
1161 MLX5_SET(rqc, rqc, scatter_fcs, 1);
1162
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001163 wq = MLX5_ADDR_OF(rqc, rqc, wq);
1164 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1165 MLX5_SET(wq, wq, end_padding_mode,
Maor Gottlieb01581fb2016-01-28 17:51:49 +02001166 MLX5_GET(qpc, qpc, end_padding_mode));
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001167 MLX5_SET(wq, wq, page_offset, MLX5_GET(qpc, qpc, page_offset));
1168 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1169 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1170 MLX5_SET(wq, wq, log_wq_stride, MLX5_GET(qpc, qpc, log_rq_stride) + 4);
1171 MLX5_SET(wq, wq, log_wq_pg_sz, MLX5_GET(qpc, qpc, log_page_size));
1172 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_rq_size));
1173
1174 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1175 qp_pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, qpin, pas);
1176 memcpy(pas, qp_pas, rq_pas_size);
1177
1178 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rq->base.mqp);
1179
1180 kvfree(in);
1181
1182 return err;
1183}
1184
1185static void destroy_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1186 struct mlx5_ib_rq *rq)
1187{
1188 mlx5_core_destroy_rq_tracked(dev->mdev, &rq->base.mqp);
1189}
1190
1191static int create_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1192 struct mlx5_ib_rq *rq, u32 tdn)
1193{
1194 u32 *in;
1195 void *tirc;
1196 int inlen;
1197 int err;
1198
1199 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001200 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001201 if (!in)
1202 return -ENOMEM;
1203
1204 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1205 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
1206 MLX5_SET(tirc, tirc, inline_rqn, rq->base.mqp.qpn);
1207 MLX5_SET(tirc, tirc, transport_domain, tdn);
1208
1209 err = mlx5_core_create_tir(dev->mdev, in, inlen, &rq->tirn);
1210
1211 kvfree(in);
1212
1213 return err;
1214}
1215
1216static void destroy_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1217 struct mlx5_ib_rq *rq)
1218{
1219 mlx5_core_destroy_tir(dev->mdev, rq->tirn);
1220}
1221
1222static int create_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001223 u32 *in,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001224 struct ib_pd *pd)
1225{
1226 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1227 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1228 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1229 struct ib_uobject *uobj = pd->uobject;
1230 struct ib_ucontext *ucontext = uobj->context;
1231 struct mlx5_ib_ucontext *mucontext = to_mucontext(ucontext);
1232 int err;
1233 u32 tdn = mucontext->tdn;
1234
1235 if (qp->sq.wqe_cnt) {
1236 err = create_raw_packet_qp_tis(dev, sq, tdn);
1237 if (err)
1238 return err;
1239
1240 err = create_raw_packet_qp_sq(dev, sq, in, pd);
1241 if (err)
1242 goto err_destroy_tis;
1243
1244 sq->base.container_mibqp = qp;
1245 }
1246
1247 if (qp->rq.wqe_cnt) {
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001248 rq->base.container_mibqp = qp;
1249
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001250 if (qp->flags & MLX5_IB_QP_CVLAN_STRIPPING)
1251 rq->flags |= MLX5_IB_RQ_CVLAN_STRIPPING;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001252 err = create_raw_packet_qp_rq(dev, rq, in);
1253 if (err)
1254 goto err_destroy_sq;
1255
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001256
1257 err = create_raw_packet_qp_tir(dev, rq, tdn);
1258 if (err)
1259 goto err_destroy_rq;
1260 }
1261
1262 qp->trans_qp.base.mqp.qpn = qp->sq.wqe_cnt ? sq->base.mqp.qpn :
1263 rq->base.mqp.qpn;
1264
1265 return 0;
1266
1267err_destroy_rq:
1268 destroy_raw_packet_qp_rq(dev, rq);
1269err_destroy_sq:
1270 if (!qp->sq.wqe_cnt)
1271 return err;
1272 destroy_raw_packet_qp_sq(dev, sq);
1273err_destroy_tis:
1274 destroy_raw_packet_qp_tis(dev, sq);
1275
1276 return err;
1277}
1278
1279static void destroy_raw_packet_qp(struct mlx5_ib_dev *dev,
1280 struct mlx5_ib_qp *qp)
1281{
1282 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1283 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1284 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1285
1286 if (qp->rq.wqe_cnt) {
1287 destroy_raw_packet_qp_tir(dev, rq);
1288 destroy_raw_packet_qp_rq(dev, rq);
1289 }
1290
1291 if (qp->sq.wqe_cnt) {
1292 destroy_raw_packet_qp_sq(dev, sq);
1293 destroy_raw_packet_qp_tis(dev, sq);
1294 }
1295}
1296
1297static void raw_packet_qp_copy_info(struct mlx5_ib_qp *qp,
1298 struct mlx5_ib_raw_packet_qp *raw_packet_qp)
1299{
1300 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1301 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1302
1303 sq->sq = &qp->sq;
1304 rq->rq = &qp->rq;
1305 sq->doorbell = &qp->db;
1306 rq->doorbell = &qp->db;
1307}
1308
Yishai Hadas28d61372016-05-23 15:20:56 +03001309static void destroy_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1310{
1311 mlx5_core_destroy_tir(dev->mdev, qp->rss_qp.tirn);
1312}
1313
1314static int create_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
1315 struct ib_pd *pd,
1316 struct ib_qp_init_attr *init_attr,
1317 struct ib_udata *udata)
1318{
1319 struct ib_uobject *uobj = pd->uobject;
1320 struct ib_ucontext *ucontext = uobj->context;
1321 struct mlx5_ib_ucontext *mucontext = to_mucontext(ucontext);
1322 struct mlx5_ib_create_qp_resp resp = {};
1323 int inlen;
1324 int err;
1325 u32 *in;
1326 void *tirc;
1327 void *hfso;
1328 u32 selected_fields = 0;
1329 size_t min_resp_len;
1330 u32 tdn = mucontext->tdn;
1331 struct mlx5_ib_create_qp_rss ucmd = {};
1332 size_t required_cmd_sz;
1333
1334 if (init_attr->qp_type != IB_QPT_RAW_PACKET)
1335 return -EOPNOTSUPP;
1336
1337 if (init_attr->create_flags || init_attr->send_cq)
1338 return -EINVAL;
1339
Eli Cohen2f5ff262017-01-03 23:55:21 +02001340 min_resp_len = offsetof(typeof(resp), bfreg_index) + sizeof(resp.bfreg_index);
Yishai Hadas28d61372016-05-23 15:20:56 +03001341 if (udata->outlen < min_resp_len)
1342 return -EINVAL;
1343
1344 required_cmd_sz = offsetof(typeof(ucmd), reserved1) + sizeof(ucmd.reserved1);
1345 if (udata->inlen < required_cmd_sz) {
1346 mlx5_ib_dbg(dev, "invalid inlen\n");
1347 return -EINVAL;
1348 }
1349
1350 if (udata->inlen > sizeof(ucmd) &&
1351 !ib_is_udata_cleared(udata, sizeof(ucmd),
1352 udata->inlen - sizeof(ucmd))) {
1353 mlx5_ib_dbg(dev, "inlen is not supported\n");
1354 return -EOPNOTSUPP;
1355 }
1356
1357 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
1358 mlx5_ib_dbg(dev, "copy failed\n");
1359 return -EFAULT;
1360 }
1361
1362 if (ucmd.comp_mask) {
1363 mlx5_ib_dbg(dev, "invalid comp mask\n");
1364 return -EOPNOTSUPP;
1365 }
1366
1367 if (memchr_inv(ucmd.reserved, 0, sizeof(ucmd.reserved)) || ucmd.reserved1) {
1368 mlx5_ib_dbg(dev, "invalid reserved\n");
1369 return -EOPNOTSUPP;
1370 }
1371
1372 err = ib_copy_to_udata(udata, &resp, min_resp_len);
1373 if (err) {
1374 mlx5_ib_dbg(dev, "copy failed\n");
1375 return -EINVAL;
1376 }
1377
1378 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001379 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas28d61372016-05-23 15:20:56 +03001380 if (!in)
1381 return -ENOMEM;
1382
1383 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1384 MLX5_SET(tirc, tirc, disp_type,
1385 MLX5_TIRC_DISP_TYPE_INDIRECT);
1386 MLX5_SET(tirc, tirc, indirect_table,
1387 init_attr->rwq_ind_tbl->ind_tbl_num);
1388 MLX5_SET(tirc, tirc, transport_domain, tdn);
1389
1390 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1391 switch (ucmd.rx_hash_function) {
1392 case MLX5_RX_HASH_FUNC_TOEPLITZ:
1393 {
1394 void *rss_key = MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
1395 size_t len = MLX5_FLD_SZ_BYTES(tirc, rx_hash_toeplitz_key);
1396
1397 if (len != ucmd.rx_key_len) {
1398 err = -EINVAL;
1399 goto err;
1400 }
1401
1402 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_TOEPLITZ);
1403 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1404 memcpy(rss_key, ucmd.rx_hash_key, len);
1405 break;
1406 }
1407 default:
1408 err = -EOPNOTSUPP;
1409 goto err;
1410 }
1411
1412 if (!ucmd.rx_hash_fields_mask) {
1413 /* special case when this TIR serves as steering entry without hashing */
1414 if (!init_attr->rwq_ind_tbl->log_ind_tbl_size)
1415 goto create_tir;
1416 err = -EINVAL;
1417 goto err;
1418 }
1419
1420 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1421 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4)) &&
1422 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1423 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))) {
1424 err = -EINVAL;
1425 goto err;
1426 }
1427
1428 /* If none of IPV4 & IPV6 SRC/DST was set - this bit field is ignored */
1429 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1430 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4))
1431 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1432 MLX5_L3_PROT_TYPE_IPV4);
1433 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1434 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1435 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1436 MLX5_L3_PROT_TYPE_IPV6);
1437
1438 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1439 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP)) &&
1440 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1441 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))) {
1442 err = -EINVAL;
1443 goto err;
1444 }
1445
1446 /* If none of TCP & UDP SRC/DST was set - this bit field is ignored */
1447 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1448 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP))
1449 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1450 MLX5_L4_PROT_TYPE_TCP);
1451 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1452 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1453 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1454 MLX5_L4_PROT_TYPE_UDP);
1455
1456 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1457 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6))
1458 selected_fields |= MLX5_HASH_FIELD_SEL_SRC_IP;
1459
1460 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4) ||
1461 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1462 selected_fields |= MLX5_HASH_FIELD_SEL_DST_IP;
1463
1464 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1465 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP))
1466 selected_fields |= MLX5_HASH_FIELD_SEL_L4_SPORT;
1467
1468 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP) ||
1469 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1470 selected_fields |= MLX5_HASH_FIELD_SEL_L4_DPORT;
1471
1472 MLX5_SET(rx_hash_field_select, hfso, selected_fields, selected_fields);
1473
1474create_tir:
1475 err = mlx5_core_create_tir(dev->mdev, in, inlen, &qp->rss_qp.tirn);
1476
1477 if (err)
1478 goto err;
1479
1480 kvfree(in);
1481 /* qpn is reserved for that QP */
1482 qp->trans_qp.base.mqp.qpn = 0;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03001483 qp->flags |= MLX5_IB_QP_RSS;
Yishai Hadas28d61372016-05-23 15:20:56 +03001484 return 0;
1485
1486err:
1487 kvfree(in);
1488 return err;
1489}
1490
Eli Cohene126ba92013-07-07 17:25:49 +03001491static int create_qp_common(struct mlx5_ib_dev *dev, struct ib_pd *pd,
1492 struct ib_qp_init_attr *init_attr,
1493 struct ib_udata *udata, struct mlx5_ib_qp *qp)
1494{
1495 struct mlx5_ib_resources *devr = &dev->devr;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001496 int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
Saeed Mahameed938fe832015-05-28 22:28:41 +03001497 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001498 struct mlx5_ib_create_qp_resp resp;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001499 struct mlx5_ib_cq *send_cq;
1500 struct mlx5_ib_cq *recv_cq;
1501 unsigned long flags;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001502 u32 uidx = MLX5_IB_DEFAULT_UIDX;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001503 struct mlx5_ib_create_qp ucmd;
1504 struct mlx5_ib_qp_base *base;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001505 void *qpc;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001506 u32 *in;
1507 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03001508
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001509 base = init_attr->qp_type == IB_QPT_RAW_PACKET ?
1510 &qp->raw_packet_qp.rq.base :
1511 &qp->trans_qp.base;
1512
Eli Cohene126ba92013-07-07 17:25:49 +03001513 mutex_init(&qp->mutex);
1514 spin_lock_init(&qp->sq.lock);
1515 spin_lock_init(&qp->rq.lock);
1516
Yishai Hadas28d61372016-05-23 15:20:56 +03001517 if (init_attr->rwq_ind_tbl) {
1518 if (!udata)
1519 return -ENOSYS;
1520
1521 err = create_rss_raw_qp_tir(dev, qp, pd, init_attr, udata);
1522 return err;
1523 }
1524
Eli Cohenf360d882014-04-02 00:10:16 +03001525 if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001526 if (!MLX5_CAP_GEN(mdev, block_lb_mc)) {
Eli Cohenf360d882014-04-02 00:10:16 +03001527 mlx5_ib_dbg(dev, "block multicast loopback isn't supported\n");
1528 return -EINVAL;
1529 } else {
1530 qp->flags |= MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK;
1531 }
1532 }
1533
Leon Romanovsky051f2632015-12-20 12:16:11 +02001534 if (init_attr->create_flags &
1535 (IB_QP_CREATE_CROSS_CHANNEL |
1536 IB_QP_CREATE_MANAGED_SEND |
1537 IB_QP_CREATE_MANAGED_RECV)) {
1538 if (!MLX5_CAP_GEN(mdev, cd)) {
1539 mlx5_ib_dbg(dev, "cross-channel isn't supported\n");
1540 return -EINVAL;
1541 }
1542 if (init_attr->create_flags & IB_QP_CREATE_CROSS_CHANNEL)
1543 qp->flags |= MLX5_IB_QP_CROSS_CHANNEL;
1544 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_SEND)
1545 qp->flags |= MLX5_IB_QP_MANAGED_SEND;
1546 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_RECV)
1547 qp->flags |= MLX5_IB_QP_MANAGED_RECV;
1548 }
Erez Shitritf0313962016-02-21 16:27:17 +02001549
1550 if (init_attr->qp_type == IB_QPT_UD &&
1551 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO))
1552 if (!MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
1553 mlx5_ib_dbg(dev, "ipoib UD lso qp isn't supported\n");
1554 return -EOPNOTSUPP;
1555 }
1556
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001557 if (init_attr->create_flags & IB_QP_CREATE_SCATTER_FCS) {
1558 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
1559 mlx5_ib_dbg(dev, "Scatter FCS is supported only for Raw Packet QPs");
1560 return -EOPNOTSUPP;
1561 }
1562 if (!MLX5_CAP_GEN(dev->mdev, eth_net_offloads) ||
1563 !MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
1564 mlx5_ib_dbg(dev, "Scatter FCS isn't supported\n");
1565 return -EOPNOTSUPP;
1566 }
1567 qp->flags |= MLX5_IB_QP_CAP_SCATTER_FCS;
1568 }
1569
Eli Cohene126ba92013-07-07 17:25:49 +03001570 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
1571 qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;
1572
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001573 if (init_attr->create_flags & IB_QP_CREATE_CVLAN_STRIPPING) {
1574 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
1575 MLX5_CAP_ETH(dev->mdev, vlan_cap)) ||
1576 (init_attr->qp_type != IB_QPT_RAW_PACKET))
1577 return -EOPNOTSUPP;
1578 qp->flags |= MLX5_IB_QP_CVLAN_STRIPPING;
1579 }
1580
Eli Cohene126ba92013-07-07 17:25:49 +03001581 if (pd && pd->uobject) {
1582 if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
1583 mlx5_ib_dbg(dev, "copy failed\n");
1584 return -EFAULT;
1585 }
1586
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001587 err = get_qp_user_index(to_mucontext(pd->uobject->context),
1588 &ucmd, udata->inlen, &uidx);
1589 if (err)
1590 return err;
1591
Eli Cohene126ba92013-07-07 17:25:49 +03001592 qp->wq_sig = !!(ucmd.flags & MLX5_QP_FLAG_SIGNATURE);
1593 qp->scat_cqe = !!(ucmd.flags & MLX5_QP_FLAG_SCATTER_CQE);
1594 } else {
1595 qp->wq_sig = !!wq_signature;
1596 }
1597
1598 qp->has_rq = qp_has_rq(init_attr);
1599 err = set_rq_size(dev, &init_attr->cap, qp->has_rq,
1600 qp, (pd && pd->uobject) ? &ucmd : NULL);
1601 if (err) {
1602 mlx5_ib_dbg(dev, "err %d\n", err);
1603 return err;
1604 }
1605
1606 if (pd) {
1607 if (pd->uobject) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001608 __u32 max_wqes =
1609 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Eli Cohene126ba92013-07-07 17:25:49 +03001610 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d)\n", ucmd.sq_wqe_count);
1611 if (ucmd.rq_wqe_shift != qp->rq.wqe_shift ||
1612 ucmd.rq_wqe_count != qp->rq.wqe_cnt) {
1613 mlx5_ib_dbg(dev, "invalid rq params\n");
1614 return -EINVAL;
1615 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03001616 if (ucmd.sq_wqe_count > max_wqes) {
Eli Cohene126ba92013-07-07 17:25:49 +03001617 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d) > max allowed (%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +03001618 ucmd.sq_wqe_count, max_wqes);
Eli Cohene126ba92013-07-07 17:25:49 +03001619 return -EINVAL;
1620 }
Haggai Eranb11a4f92016-02-29 15:45:03 +02001621 if (init_attr->create_flags &
1622 mlx5_ib_create_qp_sqpn_qp1()) {
1623 mlx5_ib_dbg(dev, "user-space is not allowed to create UD QPs spoofing as QP1\n");
1624 return -EINVAL;
1625 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001626 err = create_user_qp(dev, pd, qp, udata, init_attr, &in,
1627 &resp, &inlen, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001628 if (err)
1629 mlx5_ib_dbg(dev, "err %d\n", err);
1630 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +02001631 err = create_kernel_qp(dev, init_attr, qp, &in, &inlen,
1632 base);
Eli Cohene126ba92013-07-07 17:25:49 +03001633 if (err)
1634 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohene126ba92013-07-07 17:25:49 +03001635 }
1636
1637 if (err)
1638 return err;
1639 } else {
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03001640 in = kvzalloc(inlen, GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001641 if (!in)
1642 return -ENOMEM;
1643
1644 qp->create_type = MLX5_QP_EMPTY;
1645 }
1646
1647 if (is_sqp(init_attr->qp_type))
1648 qp->port = init_attr->port_num;
1649
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001650 qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
1651
1652 MLX5_SET(qpc, qpc, st, to_mlx5_st(init_attr->qp_type));
1653 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
Eli Cohene126ba92013-07-07 17:25:49 +03001654
1655 if (init_attr->qp_type != MLX5_IB_QPT_REG_UMR)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001656 MLX5_SET(qpc, qpc, pd, to_mpd(pd ? pd : devr->p0)->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001657 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001658 MLX5_SET(qpc, qpc, latency_sensitive, 1);
1659
Eli Cohene126ba92013-07-07 17:25:49 +03001660
1661 if (qp->wq_sig)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001662 MLX5_SET(qpc, qpc, wq_signature, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001663
Eli Cohenf360d882014-04-02 00:10:16 +03001664 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001665 MLX5_SET(qpc, qpc, block_lb_mc, 1);
Eli Cohenf360d882014-04-02 00:10:16 +03001666
Leon Romanovsky051f2632015-12-20 12:16:11 +02001667 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001668 MLX5_SET(qpc, qpc, cd_master, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001669 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001670 MLX5_SET(qpc, qpc, cd_slave_send, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001671 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001672 MLX5_SET(qpc, qpc, cd_slave_receive, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001673
Eli Cohene126ba92013-07-07 17:25:49 +03001674 if (qp->scat_cqe && is_connected(init_attr->qp_type)) {
1675 int rcqe_sz;
1676 int scqe_sz;
1677
1678 rcqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->recv_cq);
1679 scqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->send_cq);
1680
1681 if (rcqe_sz == 128)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001682 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA64_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001683 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001684 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA32_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001685
1686 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR) {
1687 if (scqe_sz == 128)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001688 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA64_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001689 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001690 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA32_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001691 }
1692 }
1693
1694 if (qp->rq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001695 MLX5_SET(qpc, qpc, log_rq_stride, qp->rq.wqe_shift - 4);
1696 MLX5_SET(qpc, qpc, log_rq_size, ilog2(qp->rq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03001697 }
1698
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001699 MLX5_SET(qpc, qpc, rq_type, get_rx_type(qp, init_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03001700
1701 if (qp->sq.wqe_cnt)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001702 MLX5_SET(qpc, qpc, log_sq_size, ilog2(qp->sq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03001703 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001704 MLX5_SET(qpc, qpc, no_sq, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001705
1706 /* Set default resources */
1707 switch (init_attr->qp_type) {
1708 case IB_QPT_XRC_TGT:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001709 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
1710 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(devr->c0)->mcq.cqn);
1711 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
1712 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(init_attr->xrcd)->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001713 break;
1714 case IB_QPT_XRC_INI:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001715 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
1716 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
1717 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001718 break;
1719 default:
1720 if (init_attr->srq) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001721 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x0)->xrcdn);
1722 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(init_attr->srq)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001723 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001724 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
1725 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s1)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001726 }
1727 }
1728
1729 if (init_attr->send_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001730 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(init_attr->send_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001731
1732 if (init_attr->recv_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001733 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(init_attr->recv_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001734
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001735 MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
Eli Cohene126ba92013-07-07 17:25:49 +03001736
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001737 /* 0xffffff means we ask to work with cqe version 0 */
1738 if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1)
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001739 MLX5_SET(qpc, qpc, user_index, uidx);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001740
Erez Shitritf0313962016-02-21 16:27:17 +02001741 /* we use IB_QP_CREATE_IPOIB_UD_LSO to indicates ipoib qp */
1742 if (init_attr->qp_type == IB_QPT_UD &&
1743 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)) {
Erez Shitritf0313962016-02-21 16:27:17 +02001744 MLX5_SET(qpc, qpc, ulp_stateless_offload_mode, 1);
1745 qp->flags |= MLX5_IB_QP_LSO;
1746 }
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001747
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001748 if (init_attr->qp_type == IB_QPT_RAW_PACKET) {
1749 qp->raw_packet_qp.sq.ubuffer.buf_addr = ucmd.sq_buf_addr;
1750 raw_packet_qp_copy_info(qp, &qp->raw_packet_qp);
1751 err = create_raw_packet_qp(dev, qp, in, pd);
1752 } else {
1753 err = mlx5_core_create_qp(dev->mdev, &base->mqp, in, inlen);
1754 }
1755
Eli Cohene126ba92013-07-07 17:25:49 +03001756 if (err) {
1757 mlx5_ib_dbg(dev, "create qp failed\n");
1758 goto err_create;
1759 }
1760
Al Viro479163f2014-11-20 08:13:57 +00001761 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001762
majd@mellanox.com19098df2016-01-14 19:13:03 +02001763 base->container_mibqp = qp;
1764 base->mqp.event = mlx5_ib_qp_event;
Eli Cohene126ba92013-07-07 17:25:49 +03001765
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001766 get_cqs(init_attr->qp_type, init_attr->send_cq, init_attr->recv_cq,
1767 &send_cq, &recv_cq);
1768 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
1769 mlx5_ib_lock_cqs(send_cq, recv_cq);
1770 /* Maintain device to QPs access, needed for further handling via reset
1771 * flow
1772 */
1773 list_add_tail(&qp->qps_list, &dev->qp_list);
1774 /* Maintain CQ to QPs access, needed for further handling via reset flow
1775 */
1776 if (send_cq)
1777 list_add_tail(&qp->cq_send_list, &send_cq->list_send_qp);
1778 if (recv_cq)
1779 list_add_tail(&qp->cq_recv_list, &recv_cq->list_recv_qp);
1780 mlx5_ib_unlock_cqs(send_cq, recv_cq);
1781 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
1782
Eli Cohene126ba92013-07-07 17:25:49 +03001783 return 0;
1784
1785err_create:
1786 if (qp->create_type == MLX5_QP_USER)
Eli Cohenb037c292017-01-03 23:55:26 +02001787 destroy_qp_user(dev, pd, qp, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001788 else if (qp->create_type == MLX5_QP_KERNEL)
1789 destroy_qp_kernel(dev, qp);
1790
Al Viro479163f2014-11-20 08:13:57 +00001791 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001792 return err;
1793}
1794
1795static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
1796 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
1797{
1798 if (send_cq) {
1799 if (recv_cq) {
1800 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001801 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001802 spin_lock_nested(&recv_cq->lock,
1803 SINGLE_DEPTH_NESTING);
1804 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001805 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001806 __acquire(&recv_cq->lock);
1807 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001808 spin_lock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001809 spin_lock_nested(&send_cq->lock,
1810 SINGLE_DEPTH_NESTING);
1811 }
1812 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001813 spin_lock(&send_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001814 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001815 }
1816 } else if (recv_cq) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001817 spin_lock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001818 __acquire(&send_cq->lock);
1819 } else {
1820 __acquire(&send_cq->lock);
1821 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001822 }
1823}
1824
1825static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
1826 __releases(&send_cq->lock) __releases(&recv_cq->lock)
1827{
1828 if (send_cq) {
1829 if (recv_cq) {
1830 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
1831 spin_unlock(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001832 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001833 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
1834 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001835 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001836 } else {
1837 spin_unlock(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001838 spin_unlock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001839 }
1840 } else {
Eli Cohen6a4f1392014-12-02 12:26:18 +02001841 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001842 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001843 }
1844 } else if (recv_cq) {
Eli Cohen6a4f1392014-12-02 12:26:18 +02001845 __release(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001846 spin_unlock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001847 } else {
1848 __release(&recv_cq->lock);
1849 __release(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001850 }
1851}
1852
1853static struct mlx5_ib_pd *get_pd(struct mlx5_ib_qp *qp)
1854{
1855 return to_mpd(qp->ibqp.pd);
1856}
1857
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001858static void get_cqs(enum ib_qp_type qp_type,
1859 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
Eli Cohene126ba92013-07-07 17:25:49 +03001860 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq)
1861{
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001862 switch (qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +03001863 case IB_QPT_XRC_TGT:
1864 *send_cq = NULL;
1865 *recv_cq = NULL;
1866 break;
1867 case MLX5_IB_QPT_REG_UMR:
1868 case IB_QPT_XRC_INI:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001869 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03001870 *recv_cq = NULL;
1871 break;
1872
1873 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02001874 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03001875 case IB_QPT_RC:
1876 case IB_QPT_UC:
1877 case IB_QPT_UD:
1878 case IB_QPT_RAW_IPV6:
1879 case IB_QPT_RAW_ETHERTYPE:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001880 case IB_QPT_RAW_PACKET:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001881 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
1882 *recv_cq = ib_recv_cq ? to_mcq(ib_recv_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03001883 break;
1884
Eli Cohene126ba92013-07-07 17:25:49 +03001885 case IB_QPT_MAX:
1886 default:
1887 *send_cq = NULL;
1888 *recv_cq = NULL;
1889 break;
1890 }
1891}
1892
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001893static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03001894 const struct mlx5_modify_raw_qp_param *raw_qp_param,
1895 u8 lag_tx_affinity);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001896
Eli Cohene126ba92013-07-07 17:25:49 +03001897static void destroy_qp_common(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1898{
1899 struct mlx5_ib_cq *send_cq, *recv_cq;
majd@mellanox.com19098df2016-01-14 19:13:03 +02001900 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001901 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03001902 int err;
1903
Yishai Hadas28d61372016-05-23 15:20:56 +03001904 if (qp->ibqp.rwq_ind_tbl) {
1905 destroy_rss_raw_qp_tir(dev, qp);
1906 return;
1907 }
1908
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001909 base = qp->ibqp.qp_type == IB_QPT_RAW_PACKET ?
1910 &qp->raw_packet_qp.rq.base :
1911 &qp->trans_qp.base;
1912
Haggai Eran6aec21f2014-12-11 17:04:23 +02001913 if (qp->state != IB_QPS_RESET) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001914 if (qp->ibqp.qp_type != IB_QPT_RAW_PACKET) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001915 err = mlx5_core_qp_modify(dev->mdev,
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03001916 MLX5_CMD_OP_2RST_QP, 0,
1917 NULL, &base->mqp);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001918 } else {
Alex Vesker0680efa2016-08-28 12:25:52 +03001919 struct mlx5_modify_raw_qp_param raw_qp_param = {
1920 .operation = MLX5_CMD_OP_2RST_QP
1921 };
1922
Aviv Heller13eab212016-09-18 20:48:04 +03001923 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, 0);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001924 }
1925 if (err)
majd@mellanox.com427c1e72016-01-14 19:13:07 +02001926 mlx5_ib_warn(dev, "mlx5_ib: modify QP 0x%06x to RESET failed\n",
majd@mellanox.com19098df2016-01-14 19:13:03 +02001927 base->mqp.qpn);
Haggai Eran6aec21f2014-12-11 17:04:23 +02001928 }
Eli Cohene126ba92013-07-07 17:25:49 +03001929
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001930 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
1931 &send_cq, &recv_cq);
1932
1933 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
1934 mlx5_ib_lock_cqs(send_cq, recv_cq);
1935 /* del from lists under both locks above to protect reset flow paths */
1936 list_del(&qp->qps_list);
1937 if (send_cq)
1938 list_del(&qp->cq_send_list);
1939
1940 if (recv_cq)
1941 list_del(&qp->cq_recv_list);
Eli Cohene126ba92013-07-07 17:25:49 +03001942
1943 if (qp->create_type == MLX5_QP_KERNEL) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02001944 __mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03001945 qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
1946 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02001947 __mlx5_ib_cq_clean(send_cq, base->mqp.qpn,
1948 NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03001949 }
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001950 mlx5_ib_unlock_cqs(send_cq, recv_cq);
1951 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
Eli Cohene126ba92013-07-07 17:25:49 +03001952
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001953 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET) {
1954 destroy_raw_packet_qp(dev, qp);
1955 } else {
1956 err = mlx5_core_destroy_qp(dev->mdev, &base->mqp);
1957 if (err)
1958 mlx5_ib_warn(dev, "failed to destroy QP 0x%x\n",
1959 base->mqp.qpn);
1960 }
Eli Cohene126ba92013-07-07 17:25:49 +03001961
Eli Cohene126ba92013-07-07 17:25:49 +03001962 if (qp->create_type == MLX5_QP_KERNEL)
1963 destroy_qp_kernel(dev, qp);
1964 else if (qp->create_type == MLX5_QP_USER)
Eli Cohenb037c292017-01-03 23:55:26 +02001965 destroy_qp_user(dev, &get_pd(qp)->ibpd, qp, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001966}
1967
1968static const char *ib_qp_type_str(enum ib_qp_type type)
1969{
1970 switch (type) {
1971 case IB_QPT_SMI:
1972 return "IB_QPT_SMI";
1973 case IB_QPT_GSI:
1974 return "IB_QPT_GSI";
1975 case IB_QPT_RC:
1976 return "IB_QPT_RC";
1977 case IB_QPT_UC:
1978 return "IB_QPT_UC";
1979 case IB_QPT_UD:
1980 return "IB_QPT_UD";
1981 case IB_QPT_RAW_IPV6:
1982 return "IB_QPT_RAW_IPV6";
1983 case IB_QPT_RAW_ETHERTYPE:
1984 return "IB_QPT_RAW_ETHERTYPE";
1985 case IB_QPT_XRC_INI:
1986 return "IB_QPT_XRC_INI";
1987 case IB_QPT_XRC_TGT:
1988 return "IB_QPT_XRC_TGT";
1989 case IB_QPT_RAW_PACKET:
1990 return "IB_QPT_RAW_PACKET";
1991 case MLX5_IB_QPT_REG_UMR:
1992 return "MLX5_IB_QPT_REG_UMR";
1993 case IB_QPT_MAX:
1994 default:
1995 return "Invalid QP type";
1996 }
1997}
1998
1999struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd,
2000 struct ib_qp_init_attr *init_attr,
2001 struct ib_udata *udata)
2002{
2003 struct mlx5_ib_dev *dev;
2004 struct mlx5_ib_qp *qp;
2005 u16 xrcdn = 0;
2006 int err;
2007
2008 if (pd) {
2009 dev = to_mdev(pd->device);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002010
2011 if (init_attr->qp_type == IB_QPT_RAW_PACKET) {
2012 if (!pd->uobject) {
2013 mlx5_ib_dbg(dev, "Raw Packet QP is not supported for kernel consumers\n");
2014 return ERR_PTR(-EINVAL);
2015 } else if (!to_mucontext(pd->uobject->context)->cqe_version) {
2016 mlx5_ib_dbg(dev, "Raw Packet QP is only supported for CQE version > 0\n");
2017 return ERR_PTR(-EINVAL);
2018 }
2019 }
Majd Dibbiny09f16cf2016-01-28 17:51:48 +02002020 } else {
2021 /* being cautious here */
2022 if (init_attr->qp_type != IB_QPT_XRC_TGT &&
2023 init_attr->qp_type != MLX5_IB_QPT_REG_UMR) {
2024 pr_warn("%s: no PD for transport %s\n", __func__,
2025 ib_qp_type_str(init_attr->qp_type));
2026 return ERR_PTR(-EINVAL);
2027 }
2028 dev = to_mdev(to_mxrcd(init_attr->xrcd)->ibxrcd.device);
Eli Cohene126ba92013-07-07 17:25:49 +03002029 }
2030
2031 switch (init_attr->qp_type) {
2032 case IB_QPT_XRC_TGT:
2033 case IB_QPT_XRC_INI:
Saeed Mahameed938fe832015-05-28 22:28:41 +03002034 if (!MLX5_CAP_GEN(dev->mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002035 mlx5_ib_dbg(dev, "XRC not supported\n");
2036 return ERR_PTR(-ENOSYS);
2037 }
2038 init_attr->recv_cq = NULL;
2039 if (init_attr->qp_type == IB_QPT_XRC_TGT) {
2040 xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
2041 init_attr->send_cq = NULL;
2042 }
2043
2044 /* fall through */
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002045 case IB_QPT_RAW_PACKET:
Eli Cohene126ba92013-07-07 17:25:49 +03002046 case IB_QPT_RC:
2047 case IB_QPT_UC:
2048 case IB_QPT_UD:
2049 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002050 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002051 case MLX5_IB_QPT_REG_UMR:
2052 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2053 if (!qp)
2054 return ERR_PTR(-ENOMEM);
2055
2056 err = create_qp_common(dev, pd, init_attr, udata, qp);
2057 if (err) {
2058 mlx5_ib_dbg(dev, "create_qp_common failed\n");
2059 kfree(qp);
2060 return ERR_PTR(err);
2061 }
2062
2063 if (is_qp0(init_attr->qp_type))
2064 qp->ibqp.qp_num = 0;
2065 else if (is_qp1(init_attr->qp_type))
2066 qp->ibqp.qp_num = 1;
2067 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002068 qp->ibqp.qp_num = qp->trans_qp.base.mqp.qpn;
Eli Cohene126ba92013-07-07 17:25:49 +03002069
2070 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 +02002071 qp->ibqp.qp_num, qp->trans_qp.base.mqp.qpn,
Eli Cohena1ab8402016-10-27 16:36:46 +03002072 init_attr->recv_cq ? to_mcq(init_attr->recv_cq)->mcq.cqn : -1,
2073 init_attr->send_cq ? to_mcq(init_attr->send_cq)->mcq.cqn : -1);
Eli Cohene126ba92013-07-07 17:25:49 +03002074
majd@mellanox.com19098df2016-01-14 19:13:03 +02002075 qp->trans_qp.xrcdn = xrcdn;
Eli Cohene126ba92013-07-07 17:25:49 +03002076
2077 break;
2078
Haggai Erand16e91d2016-02-29 15:45:05 +02002079 case IB_QPT_GSI:
2080 return mlx5_ib_gsi_create_qp(pd, init_attr);
2081
Eli Cohene126ba92013-07-07 17:25:49 +03002082 case IB_QPT_RAW_IPV6:
2083 case IB_QPT_RAW_ETHERTYPE:
Eli Cohene126ba92013-07-07 17:25:49 +03002084 case IB_QPT_MAX:
2085 default:
2086 mlx5_ib_dbg(dev, "unsupported qp type %d\n",
2087 init_attr->qp_type);
2088 /* Don't support raw QPs */
2089 return ERR_PTR(-EINVAL);
2090 }
2091
2092 return &qp->ibqp;
2093}
2094
2095int mlx5_ib_destroy_qp(struct ib_qp *qp)
2096{
2097 struct mlx5_ib_dev *dev = to_mdev(qp->device);
2098 struct mlx5_ib_qp *mqp = to_mqp(qp);
2099
Haggai Erand16e91d2016-02-29 15:45:05 +02002100 if (unlikely(qp->qp_type == IB_QPT_GSI))
2101 return mlx5_ib_gsi_destroy_qp(qp);
2102
Eli Cohene126ba92013-07-07 17:25:49 +03002103 destroy_qp_common(dev, mqp);
2104
2105 kfree(mqp);
2106
2107 return 0;
2108}
2109
2110static __be32 to_mlx5_access_flags(struct mlx5_ib_qp *qp, const struct ib_qp_attr *attr,
2111 int attr_mask)
2112{
2113 u32 hw_access_flags = 0;
2114 u8 dest_rd_atomic;
2115 u32 access_flags;
2116
2117 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
2118 dest_rd_atomic = attr->max_dest_rd_atomic;
2119 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002120 dest_rd_atomic = qp->trans_qp.resp_depth;
Eli Cohene126ba92013-07-07 17:25:49 +03002121
2122 if (attr_mask & IB_QP_ACCESS_FLAGS)
2123 access_flags = attr->qp_access_flags;
2124 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002125 access_flags = qp->trans_qp.atomic_rd_en;
Eli Cohene126ba92013-07-07 17:25:49 +03002126
2127 if (!dest_rd_atomic)
2128 access_flags &= IB_ACCESS_REMOTE_WRITE;
2129
2130 if (access_flags & IB_ACCESS_REMOTE_READ)
2131 hw_access_flags |= MLX5_QP_BIT_RRE;
2132 if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
2133 hw_access_flags |= (MLX5_QP_BIT_RAE | MLX5_ATOMIC_MODE_CX);
2134 if (access_flags & IB_ACCESS_REMOTE_WRITE)
2135 hw_access_flags |= MLX5_QP_BIT_RWE;
2136
2137 return cpu_to_be32(hw_access_flags);
2138}
2139
2140enum {
2141 MLX5_PATH_FLAG_FL = 1 << 0,
2142 MLX5_PATH_FLAG_FREE_AR = 1 << 1,
2143 MLX5_PATH_FLAG_COUNTER = 1 << 2,
2144};
2145
2146static int ib_rate_to_mlx5(struct mlx5_ib_dev *dev, u8 rate)
2147{
2148 if (rate == IB_RATE_PORT_CURRENT) {
2149 return 0;
2150 } else if (rate < IB_RATE_2_5_GBPS || rate > IB_RATE_300_GBPS) {
2151 return -EINVAL;
2152 } else {
2153 while (rate != IB_RATE_2_5_GBPS &&
2154 !(1 << (rate + MLX5_STAT_RATE_OFFSET) &
Saeed Mahameed938fe832015-05-28 22:28:41 +03002155 MLX5_CAP_GEN(dev->mdev, stat_rate_support)))
Eli Cohene126ba92013-07-07 17:25:49 +03002156 --rate;
2157 }
2158
2159 return rate + MLX5_STAT_RATE_OFFSET;
2160}
2161
majd@mellanox.com75850d02016-01-14 19:13:06 +02002162static int modify_raw_packet_eth_prio(struct mlx5_core_dev *dev,
2163 struct mlx5_ib_sq *sq, u8 sl)
2164{
2165 void *in;
2166 void *tisc;
2167 int inlen;
2168 int err;
2169
2170 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002171 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002172 if (!in)
2173 return -ENOMEM;
2174
2175 MLX5_SET(modify_tis_in, in, bitmask.prio, 1);
2176
2177 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2178 MLX5_SET(tisc, tisc, prio, ((sl & 0x7) << 1));
2179
2180 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2181
2182 kvfree(in);
2183
2184 return err;
2185}
2186
Aviv Heller13eab212016-09-18 20:48:04 +03002187static int modify_raw_packet_tx_affinity(struct mlx5_core_dev *dev,
2188 struct mlx5_ib_sq *sq, u8 tx_affinity)
2189{
2190 void *in;
2191 void *tisc;
2192 int inlen;
2193 int err;
2194
2195 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002196 in = kvzalloc(inlen, GFP_KERNEL);
Aviv Heller13eab212016-09-18 20:48:04 +03002197 if (!in)
2198 return -ENOMEM;
2199
2200 MLX5_SET(modify_tis_in, in, bitmask.lag_tx_port_affinity, 1);
2201
2202 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2203 MLX5_SET(tisc, tisc, lag_tx_port_affinity, tx_affinity);
2204
2205 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2206
2207 kvfree(in);
2208
2209 return err;
2210}
2211
majd@mellanox.com75850d02016-01-14 19:13:06 +02002212static int mlx5_set_path(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -04002213 const struct rdma_ah_attr *ah,
Eli Cohene126ba92013-07-07 17:25:49 +03002214 struct mlx5_qp_path *path, u8 port, int attr_mask,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002215 u32 path_flags, const struct ib_qp_attr *attr,
2216 bool alt)
Eli Cohene126ba92013-07-07 17:25:49 +03002217{
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002218 const struct ib_global_route *grh = rdma_ah_read_grh(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002219 int err;
Majd Dibbinyed884512017-01-18 14:10:35 +02002220 enum ib_gid_type gid_type;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002221 u8 ah_flags = rdma_ah_get_ah_flags(ah);
2222 u8 sl = rdma_ah_get_sl(ah);
Eli Cohene126ba92013-07-07 17:25:49 +03002223
Eli Cohene126ba92013-07-07 17:25:49 +03002224 if (attr_mask & IB_QP_PKEY_INDEX)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002225 path->pkey_index = cpu_to_be16(alt ? attr->alt_pkey_index :
2226 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002227
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002228 if (ah_flags & IB_AH_GRH) {
2229 if (grh->sgid_index >=
Saeed Mahameed938fe832015-05-28 22:28:41 +03002230 dev->mdev->port_caps[port - 1].gid_table_len) {
Joe Perchesf4f01b52015-05-08 15:58:07 -07002231 pr_err("sgid_index (%u) too large. max is %d\n",
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002232 grh->sgid_index,
Saeed Mahameed938fe832015-05-28 22:28:41 +03002233 dev->mdev->port_caps[port - 1].gid_table_len);
Eli Cohenf83b4262014-09-14 16:47:54 +03002234 return -EINVAL;
2235 }
Achiad Shochat2811ba52015-12-23 18:47:24 +02002236 }
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002237
2238 if (ah->type == RDMA_AH_ATTR_TYPE_ROCE) {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002239 if (!(ah_flags & IB_AH_GRH))
Achiad Shochat2811ba52015-12-23 18:47:24 +02002240 return -EINVAL;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002241 err = mlx5_get_roce_gid_type(dev, port, grh->sgid_index,
Majd Dibbinyed884512017-01-18 14:10:35 +02002242 &gid_type);
2243 if (err)
2244 return err;
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04002245 memcpy(path->rmac, ah->roce.dmac, sizeof(ah->roce.dmac));
Achiad Shochat2811ba52015-12-23 18:47:24 +02002246 path->udp_sport = mlx5_get_roce_udp_sport(dev, port,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002247 grh->sgid_index);
2248 path->dci_cfi_prio_sl = (sl & 0x7) << 4;
Majd Dibbinyed884512017-01-18 14:10:35 +02002249 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002250 path->ecn_dscp = (grh->traffic_class >> 2) & 0x3f;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002251 } else {
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03002252 path->fl_free_ar = (path_flags & MLX5_PATH_FLAG_FL) ? 0x80 : 0;
2253 path->fl_free_ar |=
2254 (path_flags & MLX5_PATH_FLAG_FREE_AR) ? 0x40 : 0;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002255 path->rlid = cpu_to_be16(rdma_ah_get_dlid(ah));
2256 path->grh_mlid = rdma_ah_get_path_bits(ah) & 0x7f;
2257 if (ah_flags & IB_AH_GRH)
Achiad Shochat2811ba52015-12-23 18:47:24 +02002258 path->grh_mlid |= 1 << 7;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002259 path->dci_cfi_prio_sl = sl & 0xf;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002260 }
2261
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002262 if (ah_flags & IB_AH_GRH) {
2263 path->mgid_index = grh->sgid_index;
2264 path->hop_limit = grh->hop_limit;
Eli Cohene126ba92013-07-07 17:25:49 +03002265 path->tclass_flowlabel =
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002266 cpu_to_be32((grh->traffic_class << 20) |
2267 (grh->flow_label));
2268 memcpy(path->rgid, grh->dgid.raw, 16);
Eli Cohene126ba92013-07-07 17:25:49 +03002269 }
2270
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002271 err = ib_rate_to_mlx5(dev, rdma_ah_get_static_rate(ah));
Eli Cohene126ba92013-07-07 17:25:49 +03002272 if (err < 0)
2273 return err;
2274 path->static_rate = err;
2275 path->port = port;
2276
Eli Cohene126ba92013-07-07 17:25:49 +03002277 if (attr_mask & IB_QP_TIMEOUT)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002278 path->ackto_lt = (alt ? attr->alt_timeout : attr->timeout) << 3;
Eli Cohene126ba92013-07-07 17:25:49 +03002279
majd@mellanox.com75850d02016-01-14 19:13:06 +02002280 if ((qp->ibqp.qp_type == IB_QPT_RAW_PACKET) && qp->sq.wqe_cnt)
2281 return modify_raw_packet_eth_prio(dev->mdev,
2282 &qp->raw_packet_qp.sq,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04002283 sl & 0xf);
majd@mellanox.com75850d02016-01-14 19:13:06 +02002284
Eli Cohene126ba92013-07-07 17:25:49 +03002285 return 0;
2286}
2287
2288static enum mlx5_qp_optpar opt_mask[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE][MLX5_QP_ST_MAX] = {
2289 [MLX5_QP_STATE_INIT] = {
2290 [MLX5_QP_STATE_INIT] = {
2291 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2292 MLX5_QP_OPTPAR_RAE |
2293 MLX5_QP_OPTPAR_RWE |
2294 MLX5_QP_OPTPAR_PKEY_INDEX |
2295 MLX5_QP_OPTPAR_PRI_PORT,
2296 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
2297 MLX5_QP_OPTPAR_PKEY_INDEX |
2298 MLX5_QP_OPTPAR_PRI_PORT,
2299 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2300 MLX5_QP_OPTPAR_Q_KEY |
2301 MLX5_QP_OPTPAR_PRI_PORT,
2302 },
2303 [MLX5_QP_STATE_RTR] = {
2304 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2305 MLX5_QP_OPTPAR_RRE |
2306 MLX5_QP_OPTPAR_RAE |
2307 MLX5_QP_OPTPAR_RWE |
2308 MLX5_QP_OPTPAR_PKEY_INDEX,
2309 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2310 MLX5_QP_OPTPAR_RWE |
2311 MLX5_QP_OPTPAR_PKEY_INDEX,
2312 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2313 MLX5_QP_OPTPAR_Q_KEY,
2314 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_PKEY_INDEX |
2315 MLX5_QP_OPTPAR_Q_KEY,
Eli Cohena4774e92013-09-11 16:35:32 +03002316 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2317 MLX5_QP_OPTPAR_RRE |
2318 MLX5_QP_OPTPAR_RAE |
2319 MLX5_QP_OPTPAR_RWE |
2320 MLX5_QP_OPTPAR_PKEY_INDEX,
Eli Cohene126ba92013-07-07 17:25:49 +03002321 },
2322 },
2323 [MLX5_QP_STATE_RTR] = {
2324 [MLX5_QP_STATE_RTS] = {
2325 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2326 MLX5_QP_OPTPAR_RRE |
2327 MLX5_QP_OPTPAR_RAE |
2328 MLX5_QP_OPTPAR_RWE |
2329 MLX5_QP_OPTPAR_PM_STATE |
2330 MLX5_QP_OPTPAR_RNR_TIMEOUT,
2331 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2332 MLX5_QP_OPTPAR_RWE |
2333 MLX5_QP_OPTPAR_PM_STATE,
2334 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
2335 },
2336 },
2337 [MLX5_QP_STATE_RTS] = {
2338 [MLX5_QP_STATE_RTS] = {
2339 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2340 MLX5_QP_OPTPAR_RAE |
2341 MLX5_QP_OPTPAR_RWE |
2342 MLX5_QP_OPTPAR_RNR_TIMEOUT |
Eli Cohenc2a34312013-10-24 12:01:02 +03002343 MLX5_QP_OPTPAR_PM_STATE |
2344 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03002345 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
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_UD] = MLX5_QP_OPTPAR_Q_KEY |
2349 MLX5_QP_OPTPAR_SRQN |
2350 MLX5_QP_OPTPAR_CQN_RCV,
2351 },
2352 },
2353 [MLX5_QP_STATE_SQER] = {
2354 [MLX5_QP_STATE_RTS] = {
2355 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
2356 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_Q_KEY,
Eli Cohen75959f52013-09-11 16:35:31 +03002357 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE,
Eli Cohena4774e92013-09-11 16:35:32 +03002358 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
2359 MLX5_QP_OPTPAR_RWE |
2360 MLX5_QP_OPTPAR_RAE |
2361 MLX5_QP_OPTPAR_RRE,
Eli Cohene126ba92013-07-07 17:25:49 +03002362 },
2363 },
2364};
2365
2366static int ib_nr_to_mlx5_nr(int ib_mask)
2367{
2368 switch (ib_mask) {
2369 case IB_QP_STATE:
2370 return 0;
2371 case IB_QP_CUR_STATE:
2372 return 0;
2373 case IB_QP_EN_SQD_ASYNC_NOTIFY:
2374 return 0;
2375 case IB_QP_ACCESS_FLAGS:
2376 return MLX5_QP_OPTPAR_RWE | MLX5_QP_OPTPAR_RRE |
2377 MLX5_QP_OPTPAR_RAE;
2378 case IB_QP_PKEY_INDEX:
2379 return MLX5_QP_OPTPAR_PKEY_INDEX;
2380 case IB_QP_PORT:
2381 return MLX5_QP_OPTPAR_PRI_PORT;
2382 case IB_QP_QKEY:
2383 return MLX5_QP_OPTPAR_Q_KEY;
2384 case IB_QP_AV:
2385 return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH |
2386 MLX5_QP_OPTPAR_PRI_PORT;
2387 case IB_QP_PATH_MTU:
2388 return 0;
2389 case IB_QP_TIMEOUT:
2390 return MLX5_QP_OPTPAR_ACK_TIMEOUT;
2391 case IB_QP_RETRY_CNT:
2392 return MLX5_QP_OPTPAR_RETRY_COUNT;
2393 case IB_QP_RNR_RETRY:
2394 return MLX5_QP_OPTPAR_RNR_RETRY;
2395 case IB_QP_RQ_PSN:
2396 return 0;
2397 case IB_QP_MAX_QP_RD_ATOMIC:
2398 return MLX5_QP_OPTPAR_SRA_MAX;
2399 case IB_QP_ALT_PATH:
2400 return MLX5_QP_OPTPAR_ALT_ADDR_PATH;
2401 case IB_QP_MIN_RNR_TIMER:
2402 return MLX5_QP_OPTPAR_RNR_TIMEOUT;
2403 case IB_QP_SQ_PSN:
2404 return 0;
2405 case IB_QP_MAX_DEST_RD_ATOMIC:
2406 return MLX5_QP_OPTPAR_RRA_MAX | MLX5_QP_OPTPAR_RWE |
2407 MLX5_QP_OPTPAR_RRE | MLX5_QP_OPTPAR_RAE;
2408 case IB_QP_PATH_MIG_STATE:
2409 return MLX5_QP_OPTPAR_PM_STATE;
2410 case IB_QP_CAP:
2411 return 0;
2412 case IB_QP_DEST_QPN:
2413 return 0;
2414 }
2415 return 0;
2416}
2417
2418static int ib_mask_to_mlx5_opt(int ib_mask)
2419{
2420 int result = 0;
2421 int i;
2422
2423 for (i = 0; i < 8 * sizeof(int); i++) {
2424 if ((1 << i) & ib_mask)
2425 result |= ib_nr_to_mlx5_nr(1 << i);
2426 }
2427
2428 return result;
2429}
2430
Alex Veskereb49ab02016-08-28 12:25:53 +03002431static int modify_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
2432 struct mlx5_ib_rq *rq, int new_state,
2433 const struct mlx5_modify_raw_qp_param *raw_qp_param)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002434{
2435 void *in;
2436 void *rqc;
2437 int inlen;
2438 int err;
2439
2440 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002441 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002442 if (!in)
2443 return -ENOMEM;
2444
2445 MLX5_SET(modify_rq_in, in, rq_state, rq->state);
2446
2447 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
2448 MLX5_SET(rqc, rqc, state, new_state);
2449
Alex Veskereb49ab02016-08-28 12:25:53 +03002450 if (raw_qp_param->set_mask & MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID) {
2451 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
2452 MLX5_SET64(modify_rq_in, in, modify_bitmask,
Majd Dibbiny23a69642017-01-18 15:25:10 +02002453 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Alex Veskereb49ab02016-08-28 12:25:53 +03002454 MLX5_SET(rqc, rqc, counter_set_id, raw_qp_param->rq_q_ctr_id);
2455 } else
2456 pr_info_once("%s: RAW PACKET QP counters are not supported on current FW\n",
2457 dev->ib_dev.name);
2458 }
2459
2460 err = mlx5_core_modify_rq(dev->mdev, rq->base.mqp.qpn, in, inlen);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002461 if (err)
2462 goto out;
2463
2464 rq->state = new_state;
2465
2466out:
2467 kvfree(in);
2468 return err;
2469}
2470
2471static int modify_raw_packet_qp_sq(struct mlx5_core_dev *dev,
Bodong Wang7d29f342016-12-01 13:43:16 +02002472 struct mlx5_ib_sq *sq,
2473 int new_state,
2474 const struct mlx5_modify_raw_qp_param *raw_qp_param)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002475{
Bodong Wang7d29f342016-12-01 13:43:16 +02002476 struct mlx5_ib_qp *ibqp = sq->base.container_mibqp;
2477 u32 old_rate = ibqp->rate_limit;
2478 u32 new_rate = old_rate;
2479 u16 rl_index = 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002480 void *in;
2481 void *sqc;
2482 int inlen;
2483 int err;
2484
2485 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002486 in = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002487 if (!in)
2488 return -ENOMEM;
2489
2490 MLX5_SET(modify_sq_in, in, sq_state, sq->state);
2491
2492 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
2493 MLX5_SET(sqc, sqc, state, new_state);
2494
Bodong Wang7d29f342016-12-01 13:43:16 +02002495 if (raw_qp_param->set_mask & MLX5_RAW_QP_RATE_LIMIT) {
2496 if (new_state != MLX5_SQC_STATE_RDY)
2497 pr_warn("%s: Rate limit can only be changed when SQ is moving to RDY\n",
2498 __func__);
2499 else
2500 new_rate = raw_qp_param->rate_limit;
2501 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002502
Bodong Wang7d29f342016-12-01 13:43:16 +02002503 if (old_rate != new_rate) {
2504 if (new_rate) {
2505 err = mlx5_rl_add_rate(dev, new_rate, &rl_index);
2506 if (err) {
2507 pr_err("Failed configuring rate %u: %d\n",
2508 new_rate, err);
2509 goto out;
2510 }
2511 }
2512
2513 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
2514 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
2515 }
2516
2517 err = mlx5_core_modify_sq(dev, sq->base.mqp.qpn, in, inlen);
2518 if (err) {
2519 /* Remove new rate from table if failed */
2520 if (new_rate &&
2521 old_rate != new_rate)
2522 mlx5_rl_remove_rate(dev, new_rate);
2523 goto out;
2524 }
2525
2526 /* Only remove the old rate after new rate was set */
2527 if ((old_rate &&
2528 (old_rate != new_rate)) ||
2529 (new_state != MLX5_SQC_STATE_RDY))
2530 mlx5_rl_remove_rate(dev, old_rate);
2531
2532 ibqp->rate_limit = new_rate;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002533 sq->state = new_state;
2534
2535out:
2536 kvfree(in);
2537 return err;
2538}
2539
2540static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03002541 const struct mlx5_modify_raw_qp_param *raw_qp_param,
2542 u8 tx_affinity)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002543{
2544 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
2545 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
2546 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
Bodong Wang7d29f342016-12-01 13:43:16 +02002547 int modify_rq = !!qp->rq.wqe_cnt;
2548 int modify_sq = !!qp->sq.wqe_cnt;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002549 int rq_state;
2550 int sq_state;
2551 int err;
2552
Alex Vesker0680efa2016-08-28 12:25:52 +03002553 switch (raw_qp_param->operation) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002554 case MLX5_CMD_OP_RST2INIT_QP:
2555 rq_state = MLX5_RQC_STATE_RDY;
2556 sq_state = MLX5_SQC_STATE_RDY;
2557 break;
2558 case MLX5_CMD_OP_2ERR_QP:
2559 rq_state = MLX5_RQC_STATE_ERR;
2560 sq_state = MLX5_SQC_STATE_ERR;
2561 break;
2562 case MLX5_CMD_OP_2RST_QP:
2563 rq_state = MLX5_RQC_STATE_RST;
2564 sq_state = MLX5_SQC_STATE_RST;
2565 break;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002566 case MLX5_CMD_OP_RTR2RTS_QP:
2567 case MLX5_CMD_OP_RTS2RTS_QP:
Bodong Wang7d29f342016-12-01 13:43:16 +02002568 if (raw_qp_param->set_mask ==
2569 MLX5_RAW_QP_RATE_LIMIT) {
2570 modify_rq = 0;
2571 sq_state = sq->state;
2572 } else {
2573 return raw_qp_param->set_mask ? -EINVAL : 0;
2574 }
2575 break;
2576 case MLX5_CMD_OP_INIT2INIT_QP:
2577 case MLX5_CMD_OP_INIT2RTR_QP:
Alex Veskereb49ab02016-08-28 12:25:53 +03002578 if (raw_qp_param->set_mask)
2579 return -EINVAL;
2580 else
2581 return 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002582 default:
2583 WARN_ON(1);
2584 return -EINVAL;
2585 }
2586
Bodong Wang7d29f342016-12-01 13:43:16 +02002587 if (modify_rq) {
2588 err = modify_raw_packet_qp_rq(dev, rq, rq_state, raw_qp_param);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002589 if (err)
2590 return err;
2591 }
2592
Bodong Wang7d29f342016-12-01 13:43:16 +02002593 if (modify_sq) {
Aviv Heller13eab212016-09-18 20:48:04 +03002594 if (tx_affinity) {
2595 err = modify_raw_packet_tx_affinity(dev->mdev, sq,
2596 tx_affinity);
2597 if (err)
2598 return err;
2599 }
2600
Bodong Wang7d29f342016-12-01 13:43:16 +02002601 return modify_raw_packet_qp_sq(dev->mdev, sq, sq_state, raw_qp_param);
Aviv Heller13eab212016-09-18 20:48:04 +03002602 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002603
2604 return 0;
2605}
2606
Eli Cohene126ba92013-07-07 17:25:49 +03002607static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
2608 const struct ib_qp_attr *attr, int attr_mask,
2609 enum ib_qp_state cur_state, enum ib_qp_state new_state)
2610{
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002611 static const u16 optab[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE] = {
2612 [MLX5_QP_STATE_RST] = {
2613 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2614 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2615 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_RST2INIT_QP,
2616 },
2617 [MLX5_QP_STATE_INIT] = {
2618 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2619 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2620 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_INIT2INIT_QP,
2621 [MLX5_QP_STATE_RTR] = MLX5_CMD_OP_INIT2RTR_QP,
2622 },
2623 [MLX5_QP_STATE_RTR] = {
2624 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2625 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2626 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTR2RTS_QP,
2627 },
2628 [MLX5_QP_STATE_RTS] = {
2629 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2630 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2631 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTS2RTS_QP,
2632 },
2633 [MLX5_QP_STATE_SQD] = {
2634 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2635 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2636 },
2637 [MLX5_QP_STATE_SQER] = {
2638 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2639 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2640 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_SQERR2RTS_QP,
2641 },
2642 [MLX5_QP_STATE_ERR] = {
2643 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2644 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2645 }
2646 };
2647
Eli Cohene126ba92013-07-07 17:25:49 +03002648 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2649 struct mlx5_ib_qp *qp = to_mqp(ibqp);
majd@mellanox.com19098df2016-01-14 19:13:03 +02002650 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Eli Cohene126ba92013-07-07 17:25:49 +03002651 struct mlx5_ib_cq *send_cq, *recv_cq;
2652 struct mlx5_qp_context *context;
Eli Cohene126ba92013-07-07 17:25:49 +03002653 struct mlx5_ib_pd *pd;
Alex Veskereb49ab02016-08-28 12:25:53 +03002654 struct mlx5_ib_port *mibport = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002655 enum mlx5_qp_state mlx5_cur, mlx5_new;
2656 enum mlx5_qp_optpar optpar;
Eli Cohene126ba92013-07-07 17:25:49 +03002657 int mlx5_st;
2658 int err;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002659 u16 op;
Aviv Heller13eab212016-09-18 20:48:04 +03002660 u8 tx_affinity = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002661
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002662 context = kzalloc(sizeof(*context), GFP_KERNEL);
2663 if (!context)
Eli Cohene126ba92013-07-07 17:25:49 +03002664 return -ENOMEM;
2665
Eli Cohene126ba92013-07-07 17:25:49 +03002666 err = to_mlx5_st(ibqp->qp_type);
Haggai Eran158abf82016-02-29 15:45:04 +02002667 if (err < 0) {
2668 mlx5_ib_dbg(dev, "unsupported qp type %d\n", ibqp->qp_type);
Eli Cohene126ba92013-07-07 17:25:49 +03002669 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002670 }
Eli Cohene126ba92013-07-07 17:25:49 +03002671
2672 context->flags = cpu_to_be32(err << 16);
2673
2674 if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
2675 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
2676 } else {
2677 switch (attr->path_mig_state) {
2678 case IB_MIG_MIGRATED:
2679 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
2680 break;
2681 case IB_MIG_REARM:
2682 context->flags |= cpu_to_be32(MLX5_QP_PM_REARM << 11);
2683 break;
2684 case IB_MIG_ARMED:
2685 context->flags |= cpu_to_be32(MLX5_QP_PM_ARMED << 11);
2686 break;
2687 }
2688 }
2689
Aviv Heller13eab212016-09-18 20:48:04 +03002690 if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
2691 if ((ibqp->qp_type == IB_QPT_RC) ||
2692 (ibqp->qp_type == IB_QPT_UD &&
2693 !(qp->flags & MLX5_IB_QP_SQPN_QP1)) ||
2694 (ibqp->qp_type == IB_QPT_UC) ||
2695 (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
2696 (ibqp->qp_type == IB_QPT_XRC_INI) ||
2697 (ibqp->qp_type == IB_QPT_XRC_TGT)) {
2698 if (mlx5_lag_is_active(dev->mdev)) {
2699 tx_affinity = (unsigned int)atomic_add_return(1,
2700 &dev->roce.next_port) %
2701 MLX5_MAX_PORTS + 1;
2702 context->flags |= cpu_to_be32(tx_affinity << 24);
2703 }
2704 }
2705 }
2706
Haggai Erand16e91d2016-02-29 15:45:05 +02002707 if (is_sqp(ibqp->qp_type)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002708 context->mtu_msgmax = (IB_MTU_256 << 5) | 8;
2709 } else if (ibqp->qp_type == IB_QPT_UD ||
2710 ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
2711 context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
2712 } else if (attr_mask & IB_QP_PATH_MTU) {
2713 if (attr->path_mtu < IB_MTU_256 ||
2714 attr->path_mtu > IB_MTU_4096) {
2715 mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
2716 err = -EINVAL;
2717 goto out;
2718 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03002719 context->mtu_msgmax = (attr->path_mtu << 5) |
2720 (u8)MLX5_CAP_GEN(dev->mdev, log_max_msg);
Eli Cohene126ba92013-07-07 17:25:49 +03002721 }
2722
2723 if (attr_mask & IB_QP_DEST_QPN)
2724 context->log_pg_sz_remote_qpn = cpu_to_be32(attr->dest_qp_num);
2725
2726 if (attr_mask & IB_QP_PKEY_INDEX)
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03002727 context->pri_path.pkey_index = cpu_to_be16(attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002728
2729 /* todo implement counter_index functionality */
2730
2731 if (is_sqp(ibqp->qp_type))
2732 context->pri_path.port = qp->port;
2733
2734 if (attr_mask & IB_QP_PORT)
2735 context->pri_path.port = attr->port_num;
2736
2737 if (attr_mask & IB_QP_AV) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02002738 err = mlx5_set_path(dev, qp, &attr->ah_attr, &context->pri_path,
Eli Cohene126ba92013-07-07 17:25:49 +03002739 attr_mask & IB_QP_PORT ? attr->port_num : qp->port,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002740 attr_mask, 0, attr, false);
Eli Cohene126ba92013-07-07 17:25:49 +03002741 if (err)
2742 goto out;
2743 }
2744
2745 if (attr_mask & IB_QP_TIMEOUT)
2746 context->pri_path.ackto_lt |= attr->timeout << 3;
2747
2748 if (attr_mask & IB_QP_ALT_PATH) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02002749 err = mlx5_set_path(dev, qp, &attr->alt_ah_attr,
2750 &context->alt_path,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002751 attr->alt_port_num,
2752 attr_mask | IB_QP_PKEY_INDEX | IB_QP_TIMEOUT,
2753 0, attr, true);
Eli Cohene126ba92013-07-07 17:25:49 +03002754 if (err)
2755 goto out;
2756 }
2757
2758 pd = get_pd(qp);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002759 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
2760 &send_cq, &recv_cq);
Eli Cohene126ba92013-07-07 17:25:49 +03002761
2762 context->flags_pd = cpu_to_be32(pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
2763 context->cqn_send = send_cq ? cpu_to_be32(send_cq->mcq.cqn) : 0;
2764 context->cqn_recv = recv_cq ? cpu_to_be32(recv_cq->mcq.cqn) : 0;
2765 context->params1 = cpu_to_be32(MLX5_IB_ACK_REQ_FREQ << 28);
2766
2767 if (attr_mask & IB_QP_RNR_RETRY)
2768 context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
2769
2770 if (attr_mask & IB_QP_RETRY_CNT)
2771 context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
2772
2773 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
2774 if (attr->max_rd_atomic)
2775 context->params1 |=
2776 cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
2777 }
2778
2779 if (attr_mask & IB_QP_SQ_PSN)
2780 context->next_send_psn = cpu_to_be32(attr->sq_psn);
2781
2782 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
2783 if (attr->max_dest_rd_atomic)
2784 context->params2 |=
2785 cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
2786 }
2787
2788 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC))
2789 context->params2 |= to_mlx5_access_flags(qp, attr, attr_mask);
2790
2791 if (attr_mask & IB_QP_MIN_RNR_TIMER)
2792 context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
2793
2794 if (attr_mask & IB_QP_RQ_PSN)
2795 context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
2796
2797 if (attr_mask & IB_QP_QKEY)
2798 context->qkey = cpu_to_be32(attr->qkey);
2799
2800 if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
2801 context->db_rec_addr = cpu_to_be64(qp->db.dma);
2802
Mark Bloch0837e862016-06-17 15:10:55 +03002803 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
2804 u8 port_num = (attr_mask & IB_QP_PORT ? attr->port_num :
2805 qp->port) - 1;
Alex Veskereb49ab02016-08-28 12:25:53 +03002806 mibport = &dev->port[port_num];
Mark Bloch0837e862016-06-17 15:10:55 +03002807 context->qp_counter_set_usr_page |=
Parav Pandite1f24a72017-04-16 07:29:29 +03002808 cpu_to_be32((u32)(mibport->cnts.set_id) << 24);
Mark Bloch0837e862016-06-17 15:10:55 +03002809 }
2810
Eli Cohene126ba92013-07-07 17:25:49 +03002811 if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
2812 context->sq_crq_size |= cpu_to_be16(1 << 4);
2813
Haggai Eranb11a4f92016-02-29 15:45:03 +02002814 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
2815 context->deth_sqpn = cpu_to_be32(1);
Eli Cohene126ba92013-07-07 17:25:49 +03002816
2817 mlx5_cur = to_mlx5_state(cur_state);
2818 mlx5_new = to_mlx5_state(new_state);
2819 mlx5_st = to_mlx5_st(ibqp->qp_type);
Eli Cohen07c91132013-10-24 12:01:01 +03002820 if (mlx5_st < 0)
Eli Cohene126ba92013-07-07 17:25:49 +03002821 goto out;
2822
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002823 if (mlx5_cur >= MLX5_QP_NUM_STATE || mlx5_new >= MLX5_QP_NUM_STATE ||
2824 !optab[mlx5_cur][mlx5_new])
2825 goto out;
2826
2827 op = optab[mlx5_cur][mlx5_new];
Eli Cohene126ba92013-07-07 17:25:49 +03002828 optpar = ib_mask_to_mlx5_opt(attr_mask);
2829 optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002830
Alex Vesker0680efa2016-08-28 12:25:52 +03002831 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET) {
2832 struct mlx5_modify_raw_qp_param raw_qp_param = {};
2833
2834 raw_qp_param.operation = op;
Alex Veskereb49ab02016-08-28 12:25:53 +03002835 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Parav Pandite1f24a72017-04-16 07:29:29 +03002836 raw_qp_param.rq_q_ctr_id = mibport->cnts.set_id;
Alex Veskereb49ab02016-08-28 12:25:53 +03002837 raw_qp_param.set_mask |= MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID;
2838 }
Bodong Wang7d29f342016-12-01 13:43:16 +02002839
2840 if (attr_mask & IB_QP_RATE_LIMIT) {
2841 raw_qp_param.rate_limit = attr->rate_limit;
2842 raw_qp_param.set_mask |= MLX5_RAW_QP_RATE_LIMIT;
2843 }
2844
Aviv Heller13eab212016-09-18 20:48:04 +03002845 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, tx_affinity);
Alex Vesker0680efa2016-08-28 12:25:52 +03002846 } else {
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002847 err = mlx5_core_qp_modify(dev->mdev, op, optpar, context,
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002848 &base->mqp);
Alex Vesker0680efa2016-08-28 12:25:52 +03002849 }
2850
Eli Cohene126ba92013-07-07 17:25:49 +03002851 if (err)
2852 goto out;
2853
2854 qp->state = new_state;
2855
2856 if (attr_mask & IB_QP_ACCESS_FLAGS)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002857 qp->trans_qp.atomic_rd_en = attr->qp_access_flags;
Eli Cohene126ba92013-07-07 17:25:49 +03002858 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002859 qp->trans_qp.resp_depth = attr->max_dest_rd_atomic;
Eli Cohene126ba92013-07-07 17:25:49 +03002860 if (attr_mask & IB_QP_PORT)
2861 qp->port = attr->port_num;
2862 if (attr_mask & IB_QP_ALT_PATH)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002863 qp->trans_qp.alt_port = attr->alt_port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03002864
2865 /*
2866 * If we moved a kernel QP to RESET, clean up all old CQ
2867 * entries and reinitialize the QP.
2868 */
2869 if (new_state == IB_QPS_RESET && !ibqp->uobject) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002870 mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03002871 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
2872 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002873 mlx5_ib_cq_clean(send_cq, base->mqp.qpn, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002874
2875 qp->rq.head = 0;
2876 qp->rq.tail = 0;
2877 qp->sq.head = 0;
2878 qp->sq.tail = 0;
2879 qp->sq.cur_post = 0;
2880 qp->sq.last_poll = 0;
2881 qp->db.db[MLX5_RCV_DBR] = 0;
2882 qp->db.db[MLX5_SND_DBR] = 0;
2883 }
2884
2885out:
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002886 kfree(context);
Eli Cohene126ba92013-07-07 17:25:49 +03002887 return err;
2888}
2889
2890int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2891 int attr_mask, struct ib_udata *udata)
2892{
2893 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2894 struct mlx5_ib_qp *qp = to_mqp(ibqp);
Haggai Erand16e91d2016-02-29 15:45:05 +02002895 enum ib_qp_type qp_type;
Eli Cohene126ba92013-07-07 17:25:49 +03002896 enum ib_qp_state cur_state, new_state;
2897 int err = -EINVAL;
2898 int port;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002899 enum rdma_link_layer ll = IB_LINK_LAYER_UNSPECIFIED;
Eli Cohene126ba92013-07-07 17:25:49 +03002900
Yishai Hadas28d61372016-05-23 15:20:56 +03002901 if (ibqp->rwq_ind_tbl)
2902 return -ENOSYS;
2903
Haggai Erand16e91d2016-02-29 15:45:05 +02002904 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
2905 return mlx5_ib_gsi_modify_qp(ibqp, attr, attr_mask);
2906
2907 qp_type = (unlikely(ibqp->qp_type == MLX5_IB_QPT_HW_GSI)) ?
2908 IB_QPT_GSI : ibqp->qp_type;
2909
Eli Cohene126ba92013-07-07 17:25:49 +03002910 mutex_lock(&qp->mutex);
2911
2912 cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
2913 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
2914
Achiad Shochat2811ba52015-12-23 18:47:24 +02002915 if (!(cur_state == new_state && cur_state == IB_QPS_RESET)) {
2916 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
2917 ll = dev->ib_dev.get_link_layer(&dev->ib_dev, port);
2918 }
2919
Haggai Erand16e91d2016-02-29 15:45:05 +02002920 if (qp_type != MLX5_IB_QPT_REG_UMR &&
2921 !ib_modify_qp_is_ok(cur_state, new_state, qp_type, attr_mask, ll)) {
Haggai Eran158abf82016-02-29 15:45:04 +02002922 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
2923 cur_state, new_state, ibqp->qp_type, attr_mask);
Eli Cohene126ba92013-07-07 17:25:49 +03002924 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002925 }
Eli Cohene126ba92013-07-07 17:25:49 +03002926
2927 if ((attr_mask & IB_QP_PORT) &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03002928 (attr->port_num == 0 ||
Haggai Eran158abf82016-02-29 15:45:04 +02002929 attr->port_num > MLX5_CAP_GEN(dev->mdev, num_ports))) {
2930 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
2931 attr->port_num, dev->num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03002932 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002933 }
Eli Cohene126ba92013-07-07 17:25:49 +03002934
2935 if (attr_mask & IB_QP_PKEY_INDEX) {
2936 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Saeed Mahameed938fe832015-05-28 22:28:41 +03002937 if (attr->pkey_index >=
Haggai Eran158abf82016-02-29 15:45:04 +02002938 dev->mdev->port_caps[port - 1].pkey_table_len) {
2939 mlx5_ib_dbg(dev, "invalid pkey index %d\n",
2940 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002941 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002942 }
Eli Cohene126ba92013-07-07 17:25:49 +03002943 }
2944
2945 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03002946 attr->max_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02002947 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_res_qp))) {
2948 mlx5_ib_dbg(dev, "invalid max_rd_atomic value %d\n",
2949 attr->max_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03002950 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002951 }
Eli Cohene126ba92013-07-07 17:25:49 +03002952
2953 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03002954 attr->max_dest_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02002955 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_req_qp))) {
2956 mlx5_ib_dbg(dev, "invalid max_dest_rd_atomic value %d\n",
2957 attr->max_dest_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03002958 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002959 }
Eli Cohene126ba92013-07-07 17:25:49 +03002960
2961 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
2962 err = 0;
2963 goto out;
2964 }
2965
2966 err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
2967
2968out:
2969 mutex_unlock(&qp->mutex);
2970 return err;
2971}
2972
2973static int mlx5_wq_overflow(struct mlx5_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
2974{
2975 struct mlx5_ib_cq *cq;
2976 unsigned cur;
2977
2978 cur = wq->head - wq->tail;
2979 if (likely(cur + nreq < wq->max_post))
2980 return 0;
2981
2982 cq = to_mcq(ib_cq);
2983 spin_lock(&cq->lock);
2984 cur = wq->head - wq->tail;
2985 spin_unlock(&cq->lock);
2986
2987 return cur + nreq >= wq->max_post;
2988}
2989
2990static __always_inline void set_raddr_seg(struct mlx5_wqe_raddr_seg *rseg,
2991 u64 remote_addr, u32 rkey)
2992{
2993 rseg->raddr = cpu_to_be64(remote_addr);
2994 rseg->rkey = cpu_to_be32(rkey);
2995 rseg->reserved = 0;
2996}
2997
Erez Shitritf0313962016-02-21 16:27:17 +02002998static void *set_eth_seg(struct mlx5_wqe_eth_seg *eseg,
2999 struct ib_send_wr *wr, void *qend,
3000 struct mlx5_ib_qp *qp, int *size)
3001{
3002 void *seg = eseg;
3003
3004 memset(eseg, 0, sizeof(struct mlx5_wqe_eth_seg));
3005
3006 if (wr->send_flags & IB_SEND_IP_CSUM)
3007 eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM |
3008 MLX5_ETH_WQE_L4_CSUM;
3009
3010 seg += sizeof(struct mlx5_wqe_eth_seg);
3011 *size += sizeof(struct mlx5_wqe_eth_seg) / 16;
3012
3013 if (wr->opcode == IB_WR_LSO) {
3014 struct ib_ud_wr *ud_wr = container_of(wr, struct ib_ud_wr, wr);
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02003015 int size_of_inl_hdr_start = sizeof(eseg->inline_hdr.start);
Erez Shitritf0313962016-02-21 16:27:17 +02003016 u64 left, leftlen, copysz;
3017 void *pdata = ud_wr->header;
3018
3019 left = ud_wr->hlen;
3020 eseg->mss = cpu_to_be16(ud_wr->mss);
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02003021 eseg->inline_hdr.sz = cpu_to_be16(left);
Erez Shitritf0313962016-02-21 16:27:17 +02003022
3023 /*
3024 * check if there is space till the end of queue, if yes,
3025 * copy all in one shot, otherwise copy till the end of queue,
3026 * rollback and than the copy the left
3027 */
Saeed Mahameed2b31f7a2016-11-28 18:04:50 +02003028 leftlen = qend - (void *)eseg->inline_hdr.start;
Erez Shitritf0313962016-02-21 16:27:17 +02003029 copysz = min_t(u64, leftlen, left);
3030
3031 memcpy(seg - size_of_inl_hdr_start, pdata, copysz);
3032
3033 if (likely(copysz > size_of_inl_hdr_start)) {
3034 seg += ALIGN(copysz - size_of_inl_hdr_start, 16);
3035 *size += ALIGN(copysz - size_of_inl_hdr_start, 16) / 16;
3036 }
3037
3038 if (unlikely(copysz < left)) { /* the last wqe in the queue */
3039 seg = mlx5_get_send_wqe(qp, 0);
3040 left -= copysz;
3041 pdata += copysz;
3042 memcpy(seg, pdata, left);
3043 seg += ALIGN(left, 16);
3044 *size += ALIGN(left, 16) / 16;
3045 }
3046 }
3047
3048 return seg;
3049}
3050
Eli Cohene126ba92013-07-07 17:25:49 +03003051static void set_datagram_seg(struct mlx5_wqe_datagram_seg *dseg,
3052 struct ib_send_wr *wr)
3053{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003054 memcpy(&dseg->av, &to_mah(ud_wr(wr)->ah)->av, sizeof(struct mlx5_av));
3055 dseg->av.dqp_dct = cpu_to_be32(ud_wr(wr)->remote_qpn | MLX5_EXTENDED_UD_AV);
3056 dseg->av.key.qkey.qkey = cpu_to_be32(ud_wr(wr)->remote_qkey);
Eli Cohene126ba92013-07-07 17:25:49 +03003057}
3058
3059static void set_data_ptr_seg(struct mlx5_wqe_data_seg *dseg, struct ib_sge *sg)
3060{
3061 dseg->byte_count = cpu_to_be32(sg->length);
3062 dseg->lkey = cpu_to_be32(sg->lkey);
3063 dseg->addr = cpu_to_be64(sg->addr);
3064}
3065
Artemy Kovalyov31616252017-01-02 11:37:42 +02003066static u64 get_xlt_octo(u64 bytes)
Eli Cohene126ba92013-07-07 17:25:49 +03003067{
Artemy Kovalyov31616252017-01-02 11:37:42 +02003068 return ALIGN(bytes, MLX5_IB_UMR_XLT_ALIGNMENT) /
3069 MLX5_IB_UMR_OCTOWORD;
Eli Cohene126ba92013-07-07 17:25:49 +03003070}
3071
3072static __be64 frwr_mkey_mask(void)
3073{
3074 u64 result;
3075
3076 result = MLX5_MKEY_MASK_LEN |
3077 MLX5_MKEY_MASK_PAGE_SIZE |
3078 MLX5_MKEY_MASK_START_ADDR |
3079 MLX5_MKEY_MASK_EN_RINVAL |
3080 MLX5_MKEY_MASK_KEY |
3081 MLX5_MKEY_MASK_LR |
3082 MLX5_MKEY_MASK_LW |
3083 MLX5_MKEY_MASK_RR |
3084 MLX5_MKEY_MASK_RW |
3085 MLX5_MKEY_MASK_A |
3086 MLX5_MKEY_MASK_SMALL_FENCE |
3087 MLX5_MKEY_MASK_FREE;
3088
3089 return cpu_to_be64(result);
3090}
3091
Sagi Grimberge6631812014-02-23 14:19:11 +02003092static __be64 sig_mkey_mask(void)
3093{
3094 u64 result;
3095
3096 result = MLX5_MKEY_MASK_LEN |
3097 MLX5_MKEY_MASK_PAGE_SIZE |
3098 MLX5_MKEY_MASK_START_ADDR |
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003099 MLX5_MKEY_MASK_EN_SIGERR |
Sagi Grimberge6631812014-02-23 14:19:11 +02003100 MLX5_MKEY_MASK_EN_RINVAL |
3101 MLX5_MKEY_MASK_KEY |
3102 MLX5_MKEY_MASK_LR |
3103 MLX5_MKEY_MASK_LW |
3104 MLX5_MKEY_MASK_RR |
3105 MLX5_MKEY_MASK_RW |
3106 MLX5_MKEY_MASK_SMALL_FENCE |
3107 MLX5_MKEY_MASK_FREE |
3108 MLX5_MKEY_MASK_BSF_EN;
3109
3110 return cpu_to_be64(result);
3111}
3112
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003113static void set_reg_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02003114 struct mlx5_ib_mr *mr)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003115{
Artemy Kovalyov31616252017-01-02 11:37:42 +02003116 int size = mr->ndescs * mr->desc_size;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003117
3118 memset(umr, 0, sizeof(*umr));
Sagi Grimbergb005d312016-02-29 19:07:33 +02003119
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003120 umr->flags = MLX5_UMR_CHECK_NOT_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02003121 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003122 umr->mkey_mask = frwr_mkey_mask();
3123}
3124
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003125static void set_linv_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr)
Eli Cohene126ba92013-07-07 17:25:49 +03003126{
3127 memset(umr, 0, sizeof(*umr));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003128 umr->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
Max Gurtovoy2d221582016-10-27 16:36:36 +03003129 umr->flags = MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03003130}
3131
Artemy Kovalyov31616252017-01-02 11:37:42 +02003132static __be64 get_umr_enable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02003133{
3134 u64 result;
3135
Artemy Kovalyov31616252017-01-02 11:37:42 +02003136 result = MLX5_MKEY_MASK_KEY |
Haggai Eran968e78d2014-12-11 17:04:11 +02003137 MLX5_MKEY_MASK_FREE;
3138
3139 return cpu_to_be64(result);
3140}
3141
Artemy Kovalyov31616252017-01-02 11:37:42 +02003142static __be64 get_umr_disable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02003143{
3144 u64 result;
3145
3146 result = MLX5_MKEY_MASK_FREE;
3147
3148 return cpu_to_be64(result);
3149}
3150
Noa Osherovich56e11d62016-02-29 16:46:51 +02003151static __be64 get_umr_update_translation_mask(void)
3152{
3153 u64 result;
3154
3155 result = MLX5_MKEY_MASK_LEN |
3156 MLX5_MKEY_MASK_PAGE_SIZE |
Artemy Kovalyov31616252017-01-02 11:37:42 +02003157 MLX5_MKEY_MASK_START_ADDR;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003158
3159 return cpu_to_be64(result);
3160}
3161
Artemy Kovalyov31616252017-01-02 11:37:42 +02003162static __be64 get_umr_update_access_mask(int atomic)
Noa Osherovich56e11d62016-02-29 16:46:51 +02003163{
3164 u64 result;
3165
Artemy Kovalyov31616252017-01-02 11:37:42 +02003166 result = MLX5_MKEY_MASK_LR |
3167 MLX5_MKEY_MASK_LW |
Noa Osherovich56e11d62016-02-29 16:46:51 +02003168 MLX5_MKEY_MASK_RR |
Artemy Kovalyov31616252017-01-02 11:37:42 +02003169 MLX5_MKEY_MASK_RW;
3170
3171 if (atomic)
3172 result |= MLX5_MKEY_MASK_A;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003173
3174 return cpu_to_be64(result);
3175}
3176
3177static __be64 get_umr_update_pd_mask(void)
3178{
3179 u64 result;
3180
Artemy Kovalyov31616252017-01-02 11:37:42 +02003181 result = MLX5_MKEY_MASK_PD;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003182
3183 return cpu_to_be64(result);
3184}
3185
Eli Cohene126ba92013-07-07 17:25:49 +03003186static void set_reg_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Maor Gottlieb578e7262016-10-27 16:36:37 +03003187 struct ib_send_wr *wr, int atomic)
Eli Cohene126ba92013-07-07 17:25:49 +03003188{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003189 struct mlx5_umr_wr *umrwr = umr_wr(wr);
Eli Cohene126ba92013-07-07 17:25:49 +03003190
3191 memset(umr, 0, sizeof(*umr));
3192
Haggai Eran968e78d2014-12-11 17:04:11 +02003193 if (wr->send_flags & MLX5_IB_SEND_UMR_FAIL_IF_FREE)
3194 umr->flags = MLX5_UMR_CHECK_FREE; /* fail if free */
3195 else
3196 umr->flags = MLX5_UMR_CHECK_NOT_FREE; /* fail if not free */
3197
Artemy Kovalyov31616252017-01-02 11:37:42 +02003198 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(umrwr->xlt_size));
3199 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_XLT) {
3200 u64 offset = get_xlt_octo(umrwr->offset);
3201
3202 umr->xlt_offset = cpu_to_be16(offset & 0xffff);
3203 umr->xlt_offset_47_16 = cpu_to_be32(offset >> 16);
3204 umr->flags |= MLX5_UMR_TRANSLATION_OFFSET_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03003205 }
Artemy Kovalyov31616252017-01-02 11:37:42 +02003206 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION)
3207 umr->mkey_mask |= get_umr_update_translation_mask();
3208 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS) {
3209 umr->mkey_mask |= get_umr_update_access_mask(atomic);
3210 umr->mkey_mask |= get_umr_update_pd_mask();
3211 }
3212 if (wr->send_flags & MLX5_IB_SEND_UMR_ENABLE_MR)
3213 umr->mkey_mask |= get_umr_enable_mr_mask();
3214 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
3215 umr->mkey_mask |= get_umr_disable_mr_mask();
Eli Cohene126ba92013-07-07 17:25:49 +03003216
3217 if (!wr->num_sge)
Haggai Eran968e78d2014-12-11 17:04:11 +02003218 umr->flags |= MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03003219}
3220
3221static u8 get_umr_flags(int acc)
3222{
3223 return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC : 0) |
3224 (acc & IB_ACCESS_REMOTE_WRITE ? MLX5_PERM_REMOTE_WRITE : 0) |
3225 (acc & IB_ACCESS_REMOTE_READ ? MLX5_PERM_REMOTE_READ : 0) |
3226 (acc & IB_ACCESS_LOCAL_WRITE ? MLX5_PERM_LOCAL_WRITE : 0) |
Sagi Grimberg2ac45932014-02-23 14:19:09 +02003227 MLX5_PERM_LOCAL_READ | MLX5_PERM_UMR_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03003228}
3229
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003230static void set_reg_mkey_seg(struct mlx5_mkey_seg *seg,
3231 struct mlx5_ib_mr *mr,
3232 u32 key, int access)
3233{
3234 int ndescs = ALIGN(mr->ndescs, 8) >> 1;
3235
3236 memset(seg, 0, sizeof(*seg));
Sagi Grimbergb005d312016-02-29 19:07:33 +02003237
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003238 if (mr->access_mode == MLX5_MKC_ACCESS_MODE_MTT)
Sagi Grimbergb005d312016-02-29 19:07:33 +02003239 seg->log2_page_size = ilog2(mr->ibmr.page_size);
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003240 else if (mr->access_mode == MLX5_MKC_ACCESS_MODE_KLMS)
Sagi Grimbergb005d312016-02-29 19:07:33 +02003241 /* KLMs take twice the size of MTTs */
3242 ndescs *= 2;
3243
3244 seg->flags = get_umr_flags(access) | mr->access_mode;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003245 seg->qpn_mkey7_0 = cpu_to_be32((key & 0xff) | 0xffffff00);
3246 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL);
3247 seg->start_addr = cpu_to_be64(mr->ibmr.iova);
3248 seg->len = cpu_to_be64(mr->ibmr.length);
3249 seg->xlt_oct_size = cpu_to_be32(ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003250}
3251
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003252static void set_linv_mkey_seg(struct mlx5_mkey_seg *seg)
Eli Cohene126ba92013-07-07 17:25:49 +03003253{
3254 memset(seg, 0, sizeof(*seg));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003255 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03003256}
3257
3258static void set_reg_mkey_segment(struct mlx5_mkey_seg *seg, struct ib_send_wr *wr)
3259{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003260 struct mlx5_umr_wr *umrwr = umr_wr(wr);
Haggai Eran968e78d2014-12-11 17:04:11 +02003261
Eli Cohene126ba92013-07-07 17:25:49 +03003262 memset(seg, 0, sizeof(*seg));
Artemy Kovalyov31616252017-01-02 11:37:42 +02003263 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
Haggai Eran968e78d2014-12-11 17:04:11 +02003264 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03003265
Haggai Eran968e78d2014-12-11 17:04:11 +02003266 seg->flags = convert_access(umrwr->access_flags);
Artemy Kovalyov31616252017-01-02 11:37:42 +02003267 if (umrwr->pd)
3268 seg->flags_pd = cpu_to_be32(to_mpd(umrwr->pd)->pdn);
3269 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION &&
3270 !umrwr->length)
3271 seg->flags_pd |= cpu_to_be32(MLX5_MKEY_LEN64);
3272
3273 seg->start_addr = cpu_to_be64(umrwr->virt_addr);
Haggai Eran968e78d2014-12-11 17:04:11 +02003274 seg->len = cpu_to_be64(umrwr->length);
3275 seg->log2_page_size = umrwr->page_shift;
Eli Cohen746b5582013-10-23 09:53:14 +03003276 seg->qpn_mkey7_0 = cpu_to_be32(0xffffff00 |
Haggai Eran968e78d2014-12-11 17:04:11 +02003277 mlx5_mkey_variant(umrwr->mkey));
Eli Cohene126ba92013-07-07 17:25:49 +03003278}
3279
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003280static void set_reg_data_seg(struct mlx5_wqe_data_seg *dseg,
3281 struct mlx5_ib_mr *mr,
3282 struct mlx5_ib_pd *pd)
3283{
3284 int bcount = mr->desc_size * mr->ndescs;
3285
3286 dseg->addr = cpu_to_be64(mr->desc_map);
3287 dseg->byte_count = cpu_to_be32(ALIGN(bcount, 64));
3288 dseg->lkey = cpu_to_be32(pd->ibpd.local_dma_lkey);
3289}
3290
Eli Cohene126ba92013-07-07 17:25:49 +03003291static __be32 send_ieth(struct ib_send_wr *wr)
3292{
3293 switch (wr->opcode) {
3294 case IB_WR_SEND_WITH_IMM:
3295 case IB_WR_RDMA_WRITE_WITH_IMM:
3296 return wr->ex.imm_data;
3297
3298 case IB_WR_SEND_WITH_INV:
3299 return cpu_to_be32(wr->ex.invalidate_rkey);
3300
3301 default:
3302 return 0;
3303 }
3304}
3305
3306static u8 calc_sig(void *wqe, int size)
3307{
3308 u8 *p = wqe;
3309 u8 res = 0;
3310 int i;
3311
3312 for (i = 0; i < size; i++)
3313 res ^= p[i];
3314
3315 return ~res;
3316}
3317
3318static u8 wq_sig(void *wqe)
3319{
3320 return calc_sig(wqe, (*((u8 *)wqe + 8) & 0x3f) << 4);
3321}
3322
3323static int set_data_inl_seg(struct mlx5_ib_qp *qp, struct ib_send_wr *wr,
3324 void *wqe, int *sz)
3325{
3326 struct mlx5_wqe_inline_seg *seg;
3327 void *qend = qp->sq.qend;
3328 void *addr;
3329 int inl = 0;
3330 int copy;
3331 int len;
3332 int i;
3333
3334 seg = wqe;
3335 wqe += sizeof(*seg);
3336 for (i = 0; i < wr->num_sge; i++) {
3337 addr = (void *)(unsigned long)(wr->sg_list[i].addr);
3338 len = wr->sg_list[i].length;
3339 inl += len;
3340
3341 if (unlikely(inl > qp->max_inline_data))
3342 return -ENOMEM;
3343
3344 if (unlikely(wqe + len > qend)) {
3345 copy = qend - wqe;
3346 memcpy(wqe, addr, copy);
3347 addr += copy;
3348 len -= copy;
3349 wqe = mlx5_get_send_wqe(qp, 0);
3350 }
3351 memcpy(wqe, addr, len);
3352 wqe += len;
3353 }
3354
3355 seg->byte_count = cpu_to_be32(inl | MLX5_INLINE_SEG);
3356
3357 *sz = ALIGN(inl + sizeof(seg->byte_count), 16) / 16;
3358
3359 return 0;
3360}
3361
Sagi Grimberge6631812014-02-23 14:19:11 +02003362static u16 prot_field_size(enum ib_signature_type type)
3363{
3364 switch (type) {
3365 case IB_SIG_TYPE_T10_DIF:
3366 return MLX5_DIF_SIZE;
3367 default:
3368 return 0;
3369 }
3370}
3371
3372static u8 bs_selector(int block_size)
3373{
3374 switch (block_size) {
3375 case 512: return 0x1;
3376 case 520: return 0x2;
3377 case 4096: return 0x3;
3378 case 4160: return 0x4;
3379 case 1073741824: return 0x5;
3380 default: return 0;
3381 }
3382}
3383
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003384static void mlx5_fill_inl_bsf(struct ib_sig_domain *domain,
3385 struct mlx5_bsf_inl *inl)
Sagi Grimberge6631812014-02-23 14:19:11 +02003386{
Sagi Grimberg142537f2014-08-13 19:54:32 +03003387 /* Valid inline section and allow BSF refresh */
3388 inl->vld_refresh = cpu_to_be16(MLX5_BSF_INL_VALID |
3389 MLX5_BSF_REFRESH_DIF);
3390 inl->dif_apptag = cpu_to_be16(domain->sig.dif.app_tag);
3391 inl->dif_reftag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003392 /* repeating block */
3393 inl->rp_inv_seed = MLX5_BSF_REPEAT_BLOCK;
3394 inl->sig_type = domain->sig.dif.bg_type == IB_T10DIF_CRC ?
3395 MLX5_DIF_CRC : MLX5_DIF_IPCS;
Sagi Grimberge6631812014-02-23 14:19:11 +02003396
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003397 if (domain->sig.dif.ref_remap)
3398 inl->dif_inc_ref_guard_check |= MLX5_BSF_INC_REFTAG;
Sagi Grimberge6631812014-02-23 14:19:11 +02003399
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003400 if (domain->sig.dif.app_escape) {
3401 if (domain->sig.dif.ref_escape)
3402 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPREF_ESCAPE;
3403 else
3404 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPTAG_ESCAPE;
Sagi Grimberge6631812014-02-23 14:19:11 +02003405 }
3406
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003407 inl->dif_app_bitmask_check =
3408 cpu_to_be16(domain->sig.dif.apptag_check_mask);
Sagi Grimberge6631812014-02-23 14:19:11 +02003409}
3410
3411static int mlx5_set_bsf(struct ib_mr *sig_mr,
3412 struct ib_sig_attrs *sig_attrs,
3413 struct mlx5_bsf *bsf, u32 data_size)
3414{
3415 struct mlx5_core_sig_ctx *msig = to_mmr(sig_mr)->sig;
3416 struct mlx5_bsf_basic *basic = &bsf->basic;
3417 struct ib_sig_domain *mem = &sig_attrs->mem;
3418 struct ib_sig_domain *wire = &sig_attrs->wire;
Sagi Grimberge6631812014-02-23 14:19:11 +02003419
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003420 memset(bsf, 0, sizeof(*bsf));
Sagi Grimberge6631812014-02-23 14:19:11 +02003421
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003422 /* Basic + Extended + Inline */
3423 basic->bsf_size_sbs = 1 << 7;
3424 /* Input domain check byte mask */
3425 basic->check_byte_mask = sig_attrs->check_mask;
3426 basic->raw_data_size = cpu_to_be32(data_size);
3427
3428 /* Memory domain */
3429 switch (sig_attrs->mem.sig_type) {
3430 case IB_SIG_TYPE_NONE:
3431 break;
3432 case IB_SIG_TYPE_T10_DIF:
3433 basic->mem.bs_selector = bs_selector(mem->sig.dif.pi_interval);
3434 basic->m_bfs_psv = cpu_to_be32(msig->psv_memory.psv_idx);
3435 mlx5_fill_inl_bsf(mem, &bsf->m_inl);
3436 break;
3437 default:
3438 return -EINVAL;
3439 }
3440
3441 /* Wire domain */
3442 switch (sig_attrs->wire.sig_type) {
3443 case IB_SIG_TYPE_NONE:
3444 break;
3445 case IB_SIG_TYPE_T10_DIF:
Sagi Grimberge6631812014-02-23 14:19:11 +02003446 if (mem->sig.dif.pi_interval == wire->sig.dif.pi_interval &&
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003447 mem->sig_type == wire->sig_type) {
Sagi Grimberge6631812014-02-23 14:19:11 +02003448 /* Same block structure */
Sagi Grimberg142537f2014-08-13 19:54:32 +03003449 basic->bsf_size_sbs |= 1 << 4;
Sagi Grimberge6631812014-02-23 14:19:11 +02003450 if (mem->sig.dif.bg_type == wire->sig.dif.bg_type)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003451 basic->wire.copy_byte_mask |= MLX5_CPY_GRD_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003452 if (mem->sig.dif.app_tag == wire->sig.dif.app_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003453 basic->wire.copy_byte_mask |= MLX5_CPY_APP_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003454 if (mem->sig.dif.ref_tag == wire->sig.dif.ref_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003455 basic->wire.copy_byte_mask |= MLX5_CPY_REF_MASK;
Sagi Grimberge6631812014-02-23 14:19:11 +02003456 } else
3457 basic->wire.bs_selector = bs_selector(wire->sig.dif.pi_interval);
3458
Sagi Grimberg142537f2014-08-13 19:54:32 +03003459 basic->w_bfs_psv = cpu_to_be32(msig->psv_wire.psv_idx);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003460 mlx5_fill_inl_bsf(wire, &bsf->w_inl);
Sagi Grimberge6631812014-02-23 14:19:11 +02003461 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003462 default:
3463 return -EINVAL;
3464 }
3465
3466 return 0;
3467}
3468
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003469static int set_sig_data_segment(struct ib_sig_handover_wr *wr,
3470 struct mlx5_ib_qp *qp, void **seg, int *size)
Sagi Grimberge6631812014-02-23 14:19:11 +02003471{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003472 struct ib_sig_attrs *sig_attrs = wr->sig_attrs;
3473 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003474 struct mlx5_bsf *bsf;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003475 u32 data_len = wr->wr.sg_list->length;
3476 u32 data_key = wr->wr.sg_list->lkey;
3477 u64 data_va = wr->wr.sg_list->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003478 int ret;
3479 int wqe_size;
3480
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003481 if (!wr->prot ||
3482 (data_key == wr->prot->lkey &&
3483 data_va == wr->prot->addr &&
3484 data_len == wr->prot->length)) {
Sagi Grimberge6631812014-02-23 14:19:11 +02003485 /**
3486 * Source domain doesn't contain signature information
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003487 * or data and protection are interleaved in memory.
Sagi Grimberge6631812014-02-23 14:19:11 +02003488 * So need construct:
3489 * ------------------
3490 * | data_klm |
3491 * ------------------
3492 * | BSF |
3493 * ------------------
3494 **/
3495 struct mlx5_klm *data_klm = *seg;
3496
3497 data_klm->bcount = cpu_to_be32(data_len);
3498 data_klm->key = cpu_to_be32(data_key);
3499 data_klm->va = cpu_to_be64(data_va);
3500 wqe_size = ALIGN(sizeof(*data_klm), 64);
3501 } else {
3502 /**
3503 * Source domain contains signature information
3504 * So need construct a strided block format:
3505 * ---------------------------
3506 * | stride_block_ctrl |
3507 * ---------------------------
3508 * | data_klm |
3509 * ---------------------------
3510 * | prot_klm |
3511 * ---------------------------
3512 * | BSF |
3513 * ---------------------------
3514 **/
3515 struct mlx5_stride_block_ctrl_seg *sblock_ctrl;
3516 struct mlx5_stride_block_entry *data_sentry;
3517 struct mlx5_stride_block_entry *prot_sentry;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003518 u32 prot_key = wr->prot->lkey;
3519 u64 prot_va = wr->prot->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003520 u16 block_size = sig_attrs->mem.sig.dif.pi_interval;
3521 int prot_size;
3522
3523 sblock_ctrl = *seg;
3524 data_sentry = (void *)sblock_ctrl + sizeof(*sblock_ctrl);
3525 prot_sentry = (void *)data_sentry + sizeof(*data_sentry);
3526
3527 prot_size = prot_field_size(sig_attrs->mem.sig_type);
3528 if (!prot_size) {
3529 pr_err("Bad block size given: %u\n", block_size);
3530 return -EINVAL;
3531 }
3532 sblock_ctrl->bcount_per_cycle = cpu_to_be32(block_size +
3533 prot_size);
3534 sblock_ctrl->op = cpu_to_be32(MLX5_STRIDE_BLOCK_OP);
3535 sblock_ctrl->repeat_count = cpu_to_be32(data_len / block_size);
3536 sblock_ctrl->num_entries = cpu_to_be16(2);
3537
3538 data_sentry->bcount = cpu_to_be16(block_size);
3539 data_sentry->key = cpu_to_be32(data_key);
3540 data_sentry->va = cpu_to_be64(data_va);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003541 data_sentry->stride = cpu_to_be16(block_size);
3542
Sagi Grimberge6631812014-02-23 14:19:11 +02003543 prot_sentry->bcount = cpu_to_be16(prot_size);
3544 prot_sentry->key = cpu_to_be32(prot_key);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003545 prot_sentry->va = cpu_to_be64(prot_va);
3546 prot_sentry->stride = cpu_to_be16(prot_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02003547
Sagi Grimberge6631812014-02-23 14:19:11 +02003548 wqe_size = ALIGN(sizeof(*sblock_ctrl) + sizeof(*data_sentry) +
3549 sizeof(*prot_sentry), 64);
3550 }
3551
3552 *seg += wqe_size;
3553 *size += wqe_size / 16;
3554 if (unlikely((*seg == qp->sq.qend)))
3555 *seg = mlx5_get_send_wqe(qp, 0);
3556
3557 bsf = *seg;
3558 ret = mlx5_set_bsf(sig_mr, sig_attrs, bsf, data_len);
3559 if (ret)
3560 return -EINVAL;
3561
3562 *seg += sizeof(*bsf);
3563 *size += sizeof(*bsf) / 16;
3564 if (unlikely((*seg == qp->sq.qend)))
3565 *seg = mlx5_get_send_wqe(qp, 0);
3566
3567 return 0;
3568}
3569
3570static void set_sig_mkey_segment(struct mlx5_mkey_seg *seg,
Artemy Kovalyov31616252017-01-02 11:37:42 +02003571 struct ib_sig_handover_wr *wr, u32 size,
Sagi Grimberge6631812014-02-23 14:19:11 +02003572 u32 length, u32 pdn)
3573{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003574 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003575 u32 sig_key = sig_mr->rkey;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003576 u8 sigerr = to_mmr(sig_mr)->sig->sigerr_count & 1;
Sagi Grimberge6631812014-02-23 14:19:11 +02003577
3578 memset(seg, 0, sizeof(*seg));
3579
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003580 seg->flags = get_umr_flags(wr->access_flags) |
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003581 MLX5_MKC_ACCESS_MODE_KLMS;
Sagi Grimberge6631812014-02-23 14:19:11 +02003582 seg->qpn_mkey7_0 = cpu_to_be32((sig_key & 0xff) | 0xffffff00);
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003583 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL | sigerr << 26 |
Sagi Grimberge6631812014-02-23 14:19:11 +02003584 MLX5_MKEY_BSF_EN | pdn);
3585 seg->len = cpu_to_be64(length);
Artemy Kovalyov31616252017-01-02 11:37:42 +02003586 seg->xlt_oct_size = cpu_to_be32(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02003587 seg->bsfs_octo_size = cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE);
3588}
3589
3590static void set_sig_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02003591 u32 size)
Sagi Grimberge6631812014-02-23 14:19:11 +02003592{
3593 memset(umr, 0, sizeof(*umr));
3594
3595 umr->flags = MLX5_FLAGS_INLINE | MLX5_FLAGS_CHECK_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02003596 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02003597 umr->bsf_octowords = cpu_to_be16(MLX5_MKEY_BSF_OCTO_SIZE);
3598 umr->mkey_mask = sig_mkey_mask();
3599}
3600
3601
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003602static int set_sig_umr_wr(struct ib_send_wr *send_wr, struct mlx5_ib_qp *qp,
Sagi Grimberge6631812014-02-23 14:19:11 +02003603 void **seg, int *size)
3604{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003605 struct ib_sig_handover_wr *wr = sig_handover_wr(send_wr);
3606 struct mlx5_ib_mr *sig_mr = to_mmr(wr->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02003607 u32 pdn = get_pd(qp)->pdn;
Artemy Kovalyov31616252017-01-02 11:37:42 +02003608 u32 xlt_size;
Sagi Grimberge6631812014-02-23 14:19:11 +02003609 int region_len, ret;
3610
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003611 if (unlikely(wr->wr.num_sge != 1) ||
3612 unlikely(wr->access_flags & IB_ACCESS_REMOTE_ATOMIC) ||
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003613 unlikely(!sig_mr->sig) || unlikely(!qp->signature_en) ||
3614 unlikely(!sig_mr->sig->sig_status_checked))
Sagi Grimberge6631812014-02-23 14:19:11 +02003615 return -EINVAL;
3616
3617 /* length of the protected region, data + protection */
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003618 region_len = wr->wr.sg_list->length;
3619 if (wr->prot &&
3620 (wr->prot->lkey != wr->wr.sg_list->lkey ||
3621 wr->prot->addr != wr->wr.sg_list->addr ||
3622 wr->prot->length != wr->wr.sg_list->length))
3623 region_len += wr->prot->length;
Sagi Grimberge6631812014-02-23 14:19:11 +02003624
3625 /**
3626 * KLM octoword size - if protection was provided
3627 * then we use strided block format (3 octowords),
3628 * else we use single KLM (1 octoword)
3629 **/
Artemy Kovalyov31616252017-01-02 11:37:42 +02003630 xlt_size = wr->prot ? 0x30 : sizeof(struct mlx5_klm);
Sagi Grimberge6631812014-02-23 14:19:11 +02003631
Artemy Kovalyov31616252017-01-02 11:37:42 +02003632 set_sig_umr_segment(*seg, xlt_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02003633 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
3634 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
3635 if (unlikely((*seg == qp->sq.qend)))
3636 *seg = mlx5_get_send_wqe(qp, 0);
3637
Artemy Kovalyov31616252017-01-02 11:37:42 +02003638 set_sig_mkey_segment(*seg, wr, xlt_size, region_len, pdn);
Sagi Grimberge6631812014-02-23 14:19:11 +02003639 *seg += sizeof(struct mlx5_mkey_seg);
3640 *size += sizeof(struct mlx5_mkey_seg) / 16;
3641 if (unlikely((*seg == qp->sq.qend)))
3642 *seg = mlx5_get_send_wqe(qp, 0);
3643
3644 ret = set_sig_data_segment(wr, qp, seg, size);
3645 if (ret)
3646 return ret;
3647
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003648 sig_mr->sig->sig_status_checked = false;
Sagi Grimberge6631812014-02-23 14:19:11 +02003649 return 0;
3650}
3651
3652static int set_psv_wr(struct ib_sig_domain *domain,
3653 u32 psv_idx, void **seg, int *size)
3654{
3655 struct mlx5_seg_set_psv *psv_seg = *seg;
3656
3657 memset(psv_seg, 0, sizeof(*psv_seg));
3658 psv_seg->psv_num = cpu_to_be32(psv_idx);
3659 switch (domain->sig_type) {
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003660 case IB_SIG_TYPE_NONE:
3661 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003662 case IB_SIG_TYPE_T10_DIF:
3663 psv_seg->transient_sig = cpu_to_be32(domain->sig.dif.bg << 16 |
3664 domain->sig.dif.app_tag);
3665 psv_seg->ref_tag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberge6631812014-02-23 14:19:11 +02003666 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003667 default:
Leon Romanovsky12bbf1e2017-01-18 14:10:31 +02003668 pr_err("Bad signature type (%d) is given.\n",
3669 domain->sig_type);
3670 return -EINVAL;
Sagi Grimberge6631812014-02-23 14:19:11 +02003671 }
3672
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003673 *seg += sizeof(*psv_seg);
3674 *size += sizeof(*psv_seg) / 16;
3675
Sagi Grimberge6631812014-02-23 14:19:11 +02003676 return 0;
3677}
3678
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003679static int set_reg_wr(struct mlx5_ib_qp *qp,
3680 struct ib_reg_wr *wr,
3681 void **seg, int *size)
3682{
3683 struct mlx5_ib_mr *mr = to_mmr(wr->mr);
3684 struct mlx5_ib_pd *pd = to_mpd(qp->ibqp.pd);
3685
3686 if (unlikely(wr->wr.send_flags & IB_SEND_INLINE)) {
3687 mlx5_ib_warn(to_mdev(qp->ibqp.device),
3688 "Invalid IB_SEND_INLINE send flag\n");
3689 return -EINVAL;
3690 }
3691
3692 set_reg_umr_seg(*seg, mr);
3693 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
3694 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
3695 if (unlikely((*seg == qp->sq.qend)))
3696 *seg = mlx5_get_send_wqe(qp, 0);
3697
3698 set_reg_mkey_seg(*seg, mr, wr->key, wr->access);
3699 *seg += sizeof(struct mlx5_mkey_seg);
3700 *size += sizeof(struct mlx5_mkey_seg) / 16;
3701 if (unlikely((*seg == qp->sq.qend)))
3702 *seg = mlx5_get_send_wqe(qp, 0);
3703
3704 set_reg_data_seg(*seg, mr, pd);
3705 *seg += sizeof(struct mlx5_wqe_data_seg);
3706 *size += (sizeof(struct mlx5_wqe_data_seg) / 16);
3707
3708 return 0;
3709}
3710
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003711static void set_linv_wr(struct mlx5_ib_qp *qp, void **seg, int *size)
Eli Cohene126ba92013-07-07 17:25:49 +03003712{
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003713 set_linv_umr_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03003714 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
3715 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
3716 if (unlikely((*seg == qp->sq.qend)))
3717 *seg = mlx5_get_send_wqe(qp, 0);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003718 set_linv_mkey_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03003719 *seg += sizeof(struct mlx5_mkey_seg);
3720 *size += sizeof(struct mlx5_mkey_seg) / 16;
3721 if (unlikely((*seg == qp->sq.qend)))
3722 *seg = mlx5_get_send_wqe(qp, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03003723}
3724
3725static void dump_wqe(struct mlx5_ib_qp *qp, int idx, int size_16)
3726{
3727 __be32 *p = NULL;
3728 int tidx = idx;
3729 int i, j;
3730
3731 pr_debug("dump wqe at %p\n", mlx5_get_send_wqe(qp, tidx));
3732 for (i = 0, j = 0; i < size_16 * 4; i += 4, j += 4) {
3733 if ((i & 0xf) == 0) {
3734 void *buf = mlx5_get_send_wqe(qp, tidx);
3735 tidx = (tidx + 1) & (qp->sq.wqe_cnt - 1);
3736 p = buf;
3737 j = 0;
3738 }
3739 pr_debug("%08x %08x %08x %08x\n", be32_to_cpu(p[j]),
3740 be32_to_cpu(p[j + 1]), be32_to_cpu(p[j + 2]),
3741 be32_to_cpu(p[j + 3]));
3742 }
3743}
3744
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003745static int begin_wqe(struct mlx5_ib_qp *qp, void **seg,
3746 struct mlx5_wqe_ctrl_seg **ctrl,
Eli Cohen6a4f1392014-12-02 12:26:18 +02003747 struct ib_send_wr *wr, unsigned *idx,
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003748 int *size, int nreq)
3749{
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03003750 if (unlikely(mlx5_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)))
3751 return -ENOMEM;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003752
3753 *idx = qp->sq.cur_post & (qp->sq.wqe_cnt - 1);
3754 *seg = mlx5_get_send_wqe(qp, *idx);
3755 *ctrl = *seg;
3756 *(uint32_t *)(*seg + 8) = 0;
3757 (*ctrl)->imm = send_ieth(wr);
3758 (*ctrl)->fm_ce_se = qp->sq_signal_bits |
3759 (wr->send_flags & IB_SEND_SIGNALED ?
3760 MLX5_WQE_CTRL_CQ_UPDATE : 0) |
3761 (wr->send_flags & IB_SEND_SOLICITED ?
3762 MLX5_WQE_CTRL_SOLICITED : 0);
3763
3764 *seg += sizeof(**ctrl);
3765 *size = sizeof(**ctrl) / 16;
3766
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03003767 return 0;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003768}
3769
3770static void finish_wqe(struct mlx5_ib_qp *qp,
3771 struct mlx5_wqe_ctrl_seg *ctrl,
3772 u8 size, unsigned idx, u64 wr_id,
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003773 int nreq, u8 fence, u32 mlx5_opcode)
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003774{
3775 u8 opmod = 0;
3776
3777 ctrl->opmod_idx_opcode = cpu_to_be32(((u32)(qp->sq.cur_post) << 8) |
3778 mlx5_opcode | ((u32)opmod << 24));
majd@mellanox.com19098df2016-01-14 19:13:03 +02003779 ctrl->qpn_ds = cpu_to_be32(size | (qp->trans_qp.base.mqp.qpn << 8));
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003780 ctrl->fm_ce_se |= fence;
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003781 if (unlikely(qp->wq_sig))
3782 ctrl->signature = wq_sig(ctrl);
3783
3784 qp->sq.wrid[idx] = wr_id;
3785 qp->sq.w_list[idx].opcode = mlx5_opcode;
3786 qp->sq.wqe_head[idx] = qp->sq.head + nreq;
3787 qp->sq.cur_post += DIV_ROUND_UP(size * 16, MLX5_SEND_WQE_BB);
3788 qp->sq.w_list[idx].next = qp->sq.cur_post;
3789}
3790
3791
Eli Cohene126ba92013-07-07 17:25:49 +03003792int mlx5_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
3793 struct ib_send_wr **bad_wr)
3794{
3795 struct mlx5_wqe_ctrl_seg *ctrl = NULL; /* compiler warning */
3796 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003797 struct mlx5_core_dev *mdev = dev->mdev;
Haggai Erand16e91d2016-02-29 15:45:05 +02003798 struct mlx5_ib_qp *qp;
Sagi Grimberge6631812014-02-23 14:19:11 +02003799 struct mlx5_ib_mr *mr;
Eli Cohene126ba92013-07-07 17:25:49 +03003800 struct mlx5_wqe_data_seg *dpseg;
3801 struct mlx5_wqe_xrc_seg *xrc;
Haggai Erand16e91d2016-02-29 15:45:05 +02003802 struct mlx5_bf *bf;
Eli Cohene126ba92013-07-07 17:25:49 +03003803 int uninitialized_var(size);
Haggai Erand16e91d2016-02-29 15:45:05 +02003804 void *qend;
Eli Cohene126ba92013-07-07 17:25:49 +03003805 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03003806 unsigned idx;
3807 int err = 0;
3808 int inl = 0;
3809 int num_sge;
3810 void *seg;
3811 int nreq;
3812 int i;
3813 u8 next_fence = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003814 u8 fence;
3815
Haggai Erand16e91d2016-02-29 15:45:05 +02003816 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
3817 return mlx5_ib_gsi_post_send(ibqp, wr, bad_wr);
3818
3819 qp = to_mqp(ibqp);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003820 bf = &qp->bf;
Haggai Erand16e91d2016-02-29 15:45:05 +02003821 qend = qp->sq.qend;
3822
Eli Cohene126ba92013-07-07 17:25:49 +03003823 spin_lock_irqsave(&qp->sq.lock, flags);
3824
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003825 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
3826 err = -EIO;
3827 *bad_wr = wr;
3828 nreq = 0;
3829 goto out;
3830 }
3831
Eli Cohene126ba92013-07-07 17:25:49 +03003832 for (nreq = 0; wr; nreq++, wr = wr->next) {
Fabian Fredericka8f731e2014-08-12 19:20:08 -04003833 if (unlikely(wr->opcode >= ARRAY_SIZE(mlx5_ib_opcode))) {
Eli Cohene126ba92013-07-07 17:25:49 +03003834 mlx5_ib_warn(dev, "\n");
3835 err = -EINVAL;
3836 *bad_wr = wr;
3837 goto out;
3838 }
3839
Eli Cohene126ba92013-07-07 17:25:49 +03003840 num_sge = wr->num_sge;
3841 if (unlikely(num_sge > qp->sq.max_gs)) {
3842 mlx5_ib_warn(dev, "\n");
Chuck Lever24be4092016-08-28 10:58:34 +03003843 err = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03003844 *bad_wr = wr;
3845 goto out;
3846 }
3847
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02003848 err = begin_wqe(qp, &seg, &ctrl, wr, &idx, &size, nreq);
3849 if (err) {
3850 mlx5_ib_warn(dev, "\n");
3851 err = -ENOMEM;
3852 *bad_wr = wr;
3853 goto out;
3854 }
Eli Cohene126ba92013-07-07 17:25:49 +03003855
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003856 if (wr->opcode == IB_WR_LOCAL_INV ||
3857 wr->opcode == IB_WR_REG_MR) {
3858 fence = dev->umr_fence;
3859 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
3860 } else if (wr->send_flags & IB_SEND_FENCE) {
3861 if (qp->next_fence)
3862 fence = MLX5_FENCE_MODE_SMALL_AND_FENCE;
3863 else
3864 fence = MLX5_FENCE_MODE_FENCE;
3865 } else {
3866 fence = qp->next_fence;
3867 }
3868
Eli Cohene126ba92013-07-07 17:25:49 +03003869 switch (ibqp->qp_type) {
3870 case IB_QPT_XRC_INI:
3871 xrc = seg;
Eli Cohene126ba92013-07-07 17:25:49 +03003872 seg += sizeof(*xrc);
3873 size += sizeof(*xrc) / 16;
3874 /* fall through */
3875 case IB_QPT_RC:
3876 switch (wr->opcode) {
3877 case IB_WR_RDMA_READ:
3878 case IB_WR_RDMA_WRITE:
3879 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003880 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
3881 rdma_wr(wr)->rkey);
Jack Morgensteinf241e742014-07-28 23:30:23 +03003882 seg += sizeof(struct mlx5_wqe_raddr_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03003883 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
3884 break;
3885
3886 case IB_WR_ATOMIC_CMP_AND_SWP:
3887 case IB_WR_ATOMIC_FETCH_AND_ADD:
Eli Cohene126ba92013-07-07 17:25:49 +03003888 case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
Eli Cohen81bea282013-09-11 16:35:30 +03003889 mlx5_ib_warn(dev, "Atomic operations are not supported yet\n");
3890 err = -ENOSYS;
3891 *bad_wr = wr;
3892 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03003893
3894 case IB_WR_LOCAL_INV:
Eli Cohene126ba92013-07-07 17:25:49 +03003895 qp->sq.wr_data[idx] = IB_WR_LOCAL_INV;
3896 ctrl->imm = cpu_to_be32(wr->ex.invalidate_rkey);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003897 set_linv_wr(qp, &seg, &size);
Eli Cohene126ba92013-07-07 17:25:49 +03003898 num_sge = 0;
3899 break;
3900
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003901 case IB_WR_REG_MR:
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003902 qp->sq.wr_data[idx] = IB_WR_REG_MR;
3903 ctrl->imm = cpu_to_be32(reg_wr(wr)->key);
3904 err = set_reg_wr(qp, reg_wr(wr), &seg, &size);
3905 if (err) {
3906 *bad_wr = wr;
3907 goto out;
3908 }
3909 num_sge = 0;
3910 break;
3911
Sagi Grimberge6631812014-02-23 14:19:11 +02003912 case IB_WR_REG_SIG_MR:
3913 qp->sq.wr_data[idx] = IB_WR_REG_SIG_MR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003914 mr = to_mmr(sig_handover_wr(wr)->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02003915
3916 ctrl->imm = cpu_to_be32(mr->ibmr.rkey);
3917 err = set_sig_umr_wr(wr, qp, &seg, &size);
3918 if (err) {
3919 mlx5_ib_warn(dev, "\n");
3920 *bad_wr = wr;
3921 goto out;
3922 }
3923
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003924 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
3925 fence, MLX5_OPCODE_UMR);
Sagi Grimberge6631812014-02-23 14:19:11 +02003926 /*
3927 * SET_PSV WQEs are not signaled and solicited
3928 * on error
3929 */
3930 wr->send_flags &= ~IB_SEND_SIGNALED;
3931 wr->send_flags |= IB_SEND_SOLICITED;
3932 err = begin_wqe(qp, &seg, &ctrl, wr,
3933 &idx, &size, nreq);
3934 if (err) {
3935 mlx5_ib_warn(dev, "\n");
3936 err = -ENOMEM;
3937 *bad_wr = wr;
3938 goto out;
3939 }
3940
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003941 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->mem,
Sagi Grimberge6631812014-02-23 14:19:11 +02003942 mr->sig->psv_memory.psv_idx, &seg,
3943 &size);
3944 if (err) {
3945 mlx5_ib_warn(dev, "\n");
3946 *bad_wr = wr;
3947 goto out;
3948 }
3949
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003950 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
3951 fence, MLX5_OPCODE_SET_PSV);
Sagi Grimberge6631812014-02-23 14:19:11 +02003952 err = begin_wqe(qp, &seg, &ctrl, wr,
3953 &idx, &size, nreq);
3954 if (err) {
3955 mlx5_ib_warn(dev, "\n");
3956 err = -ENOMEM;
3957 *bad_wr = wr;
3958 goto out;
3959 }
3960
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003961 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->wire,
Sagi Grimberge6631812014-02-23 14:19:11 +02003962 mr->sig->psv_wire.psv_idx, &seg,
3963 &size);
3964 if (err) {
3965 mlx5_ib_warn(dev, "\n");
3966 *bad_wr = wr;
3967 goto out;
3968 }
3969
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003970 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
3971 fence, MLX5_OPCODE_SET_PSV);
3972 qp->next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
Sagi Grimberge6631812014-02-23 14:19:11 +02003973 num_sge = 0;
3974 goto skip_psv;
3975
Eli Cohene126ba92013-07-07 17:25:49 +03003976 default:
3977 break;
3978 }
3979 break;
3980
3981 case IB_QPT_UC:
3982 switch (wr->opcode) {
3983 case IB_WR_RDMA_WRITE:
3984 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003985 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
3986 rdma_wr(wr)->rkey);
Eli Cohene126ba92013-07-07 17:25:49 +03003987 seg += sizeof(struct mlx5_wqe_raddr_seg);
3988 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
3989 break;
3990
3991 default:
3992 break;
3993 }
3994 break;
3995
Eli Cohene126ba92013-07-07 17:25:49 +03003996 case IB_QPT_SMI:
Maor Gottlieb1e0e50b2017-01-18 14:10:34 +02003997 if (unlikely(!mdev->port_caps[qp->port - 1].has_smi)) {
3998 mlx5_ib_warn(dev, "Send SMP MADs is not allowed\n");
3999 err = -EPERM;
4000 *bad_wr = wr;
4001 goto out;
4002 }
Haggai Erand16e91d2016-02-29 15:45:05 +02004003 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03004004 set_datagram_seg(seg, wr);
Jack Morgensteinf241e742014-07-28 23:30:23 +03004005 seg += sizeof(struct mlx5_wqe_datagram_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004006 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
4007 if (unlikely((seg == qend)))
4008 seg = mlx5_get_send_wqe(qp, 0);
4009 break;
Erez Shitritf0313962016-02-21 16:27:17 +02004010 case IB_QPT_UD:
4011 set_datagram_seg(seg, wr);
4012 seg += sizeof(struct mlx5_wqe_datagram_seg);
4013 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Eli Cohene126ba92013-07-07 17:25:49 +03004014
Erez Shitritf0313962016-02-21 16:27:17 +02004015 if (unlikely((seg == qend)))
4016 seg = mlx5_get_send_wqe(qp, 0);
4017
4018 /* handle qp that supports ud offload */
4019 if (qp->flags & IB_QP_CREATE_IPOIB_UD_LSO) {
4020 struct mlx5_wqe_eth_pad *pad;
4021
4022 pad = seg;
4023 memset(pad, 0, sizeof(struct mlx5_wqe_eth_pad));
4024 seg += sizeof(struct mlx5_wqe_eth_pad);
4025 size += sizeof(struct mlx5_wqe_eth_pad) / 16;
4026
4027 seg = set_eth_seg(seg, wr, qend, qp, &size);
4028
4029 if (unlikely((seg == qend)))
4030 seg = mlx5_get_send_wqe(qp, 0);
4031 }
4032 break;
Eli Cohene126ba92013-07-07 17:25:49 +03004033 case MLX5_IB_QPT_REG_UMR:
4034 if (wr->opcode != MLX5_IB_WR_UMR) {
4035 err = -EINVAL;
4036 mlx5_ib_warn(dev, "bad opcode\n");
4037 goto out;
4038 }
4039 qp->sq.wr_data[idx] = MLX5_IB_WR_UMR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004040 ctrl->imm = cpu_to_be32(umr_wr(wr)->mkey);
Maor Gottlieb578e7262016-10-27 16:36:37 +03004041 set_reg_umr_segment(seg, wr, !!(MLX5_CAP_GEN(mdev, atomic)));
Eli Cohene126ba92013-07-07 17:25:49 +03004042 seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4043 size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4044 if (unlikely((seg == qend)))
4045 seg = mlx5_get_send_wqe(qp, 0);
4046 set_reg_mkey_segment(seg, wr);
4047 seg += sizeof(struct mlx5_mkey_seg);
4048 size += sizeof(struct mlx5_mkey_seg) / 16;
4049 if (unlikely((seg == qend)))
4050 seg = mlx5_get_send_wqe(qp, 0);
4051 break;
4052
4053 default:
4054 break;
4055 }
4056
4057 if (wr->send_flags & IB_SEND_INLINE && num_sge) {
4058 int uninitialized_var(sz);
4059
4060 err = set_data_inl_seg(qp, wr, seg, &sz);
4061 if (unlikely(err)) {
4062 mlx5_ib_warn(dev, "\n");
4063 *bad_wr = wr;
4064 goto out;
4065 }
4066 inl = 1;
4067 size += sz;
4068 } else {
4069 dpseg = seg;
4070 for (i = 0; i < num_sge; i++) {
4071 if (unlikely(dpseg == qend)) {
4072 seg = mlx5_get_send_wqe(qp, 0);
4073 dpseg = seg;
4074 }
4075 if (likely(wr->sg_list[i].length)) {
4076 set_data_ptr_seg(dpseg, wr->sg_list + i);
4077 size += sizeof(struct mlx5_wqe_data_seg) / 16;
4078 dpseg++;
4079 }
4080 }
4081 }
4082
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004083 qp->next_fence = next_fence;
4084 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq, fence,
Sagi Grimberg6e5eadace2014-02-23 14:19:08 +02004085 mlx5_ib_opcode[wr->opcode]);
Sagi Grimberge6631812014-02-23 14:19:11 +02004086skip_psv:
Eli Cohene126ba92013-07-07 17:25:49 +03004087 if (0)
4088 dump_wqe(qp, idx, size);
4089 }
4090
4091out:
4092 if (likely(nreq)) {
4093 qp->sq.head += nreq;
4094
4095 /* Make sure that descriptors are written before
4096 * updating doorbell record and ringing the doorbell
4097 */
4098 wmb();
4099
4100 qp->db.db[MLX5_SND_DBR] = cpu_to_be32(qp->sq.cur_post);
4101
Eli Cohenada388f2014-01-14 17:45:16 +02004102 /* Make sure doorbell record is visible to the HCA before
4103 * we hit doorbell */
4104 wmb();
4105
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004106 /* currently we support only regular doorbells */
4107 mlx5_write64((__be32 *)ctrl, bf->bfreg->map + bf->offset, NULL);
4108 /* Make sure doorbells don't leak out of SQ spinlock
4109 * and reach the HCA out of order.
4110 */
4111 mmiowb();
Eli Cohene126ba92013-07-07 17:25:49 +03004112 bf->offset ^= bf->buf_size;
Eli Cohene126ba92013-07-07 17:25:49 +03004113 }
4114
4115 spin_unlock_irqrestore(&qp->sq.lock, flags);
4116
4117 return err;
4118}
4119
4120static void set_sig_seg(struct mlx5_rwqe_sig *sig, int size)
4121{
4122 sig->signature = calc_sig(sig, size);
4123}
4124
4125int mlx5_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
4126 struct ib_recv_wr **bad_wr)
4127{
4128 struct mlx5_ib_qp *qp = to_mqp(ibqp);
4129 struct mlx5_wqe_data_seg *scat;
4130 struct mlx5_rwqe_sig *sig;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004131 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4132 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004133 unsigned long flags;
4134 int err = 0;
4135 int nreq;
4136 int ind;
4137 int i;
4138
Haggai Erand16e91d2016-02-29 15:45:05 +02004139 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4140 return mlx5_ib_gsi_post_recv(ibqp, wr, bad_wr);
4141
Eli Cohene126ba92013-07-07 17:25:49 +03004142 spin_lock_irqsave(&qp->rq.lock, flags);
4143
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004144 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
4145 err = -EIO;
4146 *bad_wr = wr;
4147 nreq = 0;
4148 goto out;
4149 }
4150
Eli Cohene126ba92013-07-07 17:25:49 +03004151 ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
4152
4153 for (nreq = 0; wr; nreq++, wr = wr->next) {
4154 if (mlx5_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
4155 err = -ENOMEM;
4156 *bad_wr = wr;
4157 goto out;
4158 }
4159
4160 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
4161 err = -EINVAL;
4162 *bad_wr = wr;
4163 goto out;
4164 }
4165
4166 scat = get_recv_wqe(qp, ind);
4167 if (qp->wq_sig)
4168 scat++;
4169
4170 for (i = 0; i < wr->num_sge; i++)
4171 set_data_ptr_seg(scat + i, wr->sg_list + i);
4172
4173 if (i < qp->rq.max_gs) {
4174 scat[i].byte_count = 0;
4175 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
4176 scat[i].addr = 0;
4177 }
4178
4179 if (qp->wq_sig) {
4180 sig = (struct mlx5_rwqe_sig *)scat;
4181 set_sig_seg(sig, (qp->rq.max_gs + 1) << 2);
4182 }
4183
4184 qp->rq.wrid[ind] = wr->wr_id;
4185
4186 ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
4187 }
4188
4189out:
4190 if (likely(nreq)) {
4191 qp->rq.head += nreq;
4192
4193 /* Make sure that descriptors are written before
4194 * doorbell record.
4195 */
4196 wmb();
4197
4198 *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
4199 }
4200
4201 spin_unlock_irqrestore(&qp->rq.lock, flags);
4202
4203 return err;
4204}
4205
4206static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
4207{
4208 switch (mlx5_state) {
4209 case MLX5_QP_STATE_RST: return IB_QPS_RESET;
4210 case MLX5_QP_STATE_INIT: return IB_QPS_INIT;
4211 case MLX5_QP_STATE_RTR: return IB_QPS_RTR;
4212 case MLX5_QP_STATE_RTS: return IB_QPS_RTS;
4213 case MLX5_QP_STATE_SQ_DRAINING:
4214 case MLX5_QP_STATE_SQD: return IB_QPS_SQD;
4215 case MLX5_QP_STATE_SQER: return IB_QPS_SQE;
4216 case MLX5_QP_STATE_ERR: return IB_QPS_ERR;
4217 default: return -1;
4218 }
4219}
4220
4221static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
4222{
4223 switch (mlx5_mig_state) {
4224 case MLX5_QP_PM_ARMED: return IB_MIG_ARMED;
4225 case MLX5_QP_PM_REARM: return IB_MIG_REARM;
4226 case MLX5_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
4227 default: return -1;
4228 }
4229}
4230
4231static int to_ib_qp_access_flags(int mlx5_flags)
4232{
4233 int ib_flags = 0;
4234
4235 if (mlx5_flags & MLX5_QP_BIT_RRE)
4236 ib_flags |= IB_ACCESS_REMOTE_READ;
4237 if (mlx5_flags & MLX5_QP_BIT_RWE)
4238 ib_flags |= IB_ACCESS_REMOTE_WRITE;
4239 if (mlx5_flags & MLX5_QP_BIT_RAE)
4240 ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
4241
4242 return ib_flags;
4243}
4244
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04004245static void to_rdma_ah_attr(struct mlx5_ib_dev *ibdev,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004246 struct rdma_ah_attr *ah_attr,
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04004247 struct mlx5_qp_path *path)
Eli Cohene126ba92013-07-07 17:25:49 +03004248{
Jack Morgenstein9603b612014-07-28 23:30:22 +03004249 struct mlx5_core_dev *dev = ibdev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004250
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004251 memset(ah_attr, 0, sizeof(*ah_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03004252
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04004253 ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, path->port);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004254 rdma_ah_set_port_num(ah_attr, path->port);
4255 if (rdma_ah_get_port_num(ah_attr) == 0 ||
4256 rdma_ah_get_port_num(ah_attr) > MLX5_CAP_GEN(dev, num_ports))
Eli Cohene126ba92013-07-07 17:25:49 +03004257 return;
4258
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004259 rdma_ah_set_port_num(ah_attr, path->port);
4260 rdma_ah_set_sl(ah_attr, path->dci_cfi_prio_sl & 0xf);
Eli Cohene126ba92013-07-07 17:25:49 +03004261
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004262 rdma_ah_set_dlid(ah_attr, be16_to_cpu(path->rlid));
4263 rdma_ah_set_path_bits(ah_attr, path->grh_mlid & 0x7f);
4264 rdma_ah_set_static_rate(ah_attr,
4265 path->static_rate ? path->static_rate - 5 : 0);
4266 if (path->grh_mlid & (1 << 7)) {
4267 u32 tc_fl = be32_to_cpu(path->tclass_flowlabel);
4268
4269 rdma_ah_set_grh(ah_attr, NULL,
4270 tc_fl & 0xfffff,
4271 path->mgid_index,
4272 path->hop_limit,
4273 (tc_fl >> 20) & 0xff);
4274 rdma_ah_set_dgid_raw(ah_attr, path->rgid);
Eli Cohene126ba92013-07-07 17:25:49 +03004275 }
4276}
4277
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004278static int query_raw_packet_qp_sq_state(struct mlx5_ib_dev *dev,
4279 struct mlx5_ib_sq *sq,
4280 u8 *sq_state)
Eli Cohene126ba92013-07-07 17:25:49 +03004281{
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004282 void *out;
4283 void *sqc;
4284 int inlen;
4285 int err;
4286
4287 inlen = MLX5_ST_SZ_BYTES(query_sq_out);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004288 out = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004289 if (!out)
4290 return -ENOMEM;
4291
4292 err = mlx5_core_query_sq(dev->mdev, sq->base.mqp.qpn, out);
4293 if (err)
4294 goto out;
4295
4296 sqc = MLX5_ADDR_OF(query_sq_out, out, sq_context);
4297 *sq_state = MLX5_GET(sqc, sqc, state);
4298 sq->state = *sq_state;
4299
4300out:
4301 kvfree(out);
4302 return err;
4303}
4304
4305static int query_raw_packet_qp_rq_state(struct mlx5_ib_dev *dev,
4306 struct mlx5_ib_rq *rq,
4307 u8 *rq_state)
4308{
4309 void *out;
4310 void *rqc;
4311 int inlen;
4312 int err;
4313
4314 inlen = MLX5_ST_SZ_BYTES(query_rq_out);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004315 out = kvzalloc(inlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004316 if (!out)
4317 return -ENOMEM;
4318
4319 err = mlx5_core_query_rq(dev->mdev, rq->base.mqp.qpn, out);
4320 if (err)
4321 goto out;
4322
4323 rqc = MLX5_ADDR_OF(query_rq_out, out, rq_context);
4324 *rq_state = MLX5_GET(rqc, rqc, state);
4325 rq->state = *rq_state;
4326
4327out:
4328 kvfree(out);
4329 return err;
4330}
4331
4332static int sqrq_state_to_qp_state(u8 sq_state, u8 rq_state,
4333 struct mlx5_ib_qp *qp, u8 *qp_state)
4334{
4335 static const u8 sqrq_trans[MLX5_RQ_NUM_STATE][MLX5_SQ_NUM_STATE] = {
4336 [MLX5_RQC_STATE_RST] = {
4337 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
4338 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
4339 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE_BAD,
4340 [MLX5_SQ_STATE_NA] = IB_QPS_RESET,
4341 },
4342 [MLX5_RQC_STATE_RDY] = {
4343 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
4344 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
4345 [MLX5_SQC_STATE_ERR] = IB_QPS_SQE,
4346 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE,
4347 },
4348 [MLX5_RQC_STATE_ERR] = {
4349 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
4350 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
4351 [MLX5_SQC_STATE_ERR] = IB_QPS_ERR,
4352 [MLX5_SQ_STATE_NA] = IB_QPS_ERR,
4353 },
4354 [MLX5_RQ_STATE_NA] = {
4355 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
4356 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
4357 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE,
4358 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE_BAD,
4359 },
4360 };
4361
4362 *qp_state = sqrq_trans[rq_state][sq_state];
4363
4364 if (*qp_state == MLX5_QP_STATE_BAD) {
4365 WARN(1, "Buggy Raw Packet QP state, SQ 0x%x state: 0x%x, RQ 0x%x state: 0x%x",
4366 qp->raw_packet_qp.sq.base.mqp.qpn, sq_state,
4367 qp->raw_packet_qp.rq.base.mqp.qpn, rq_state);
4368 return -EINVAL;
4369 }
4370
4371 if (*qp_state == MLX5_QP_STATE)
4372 *qp_state = qp->state;
4373
4374 return 0;
4375}
4376
4377static int query_raw_packet_qp_state(struct mlx5_ib_dev *dev,
4378 struct mlx5_ib_qp *qp,
4379 u8 *raw_packet_qp_state)
4380{
4381 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
4382 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
4383 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
4384 int err;
4385 u8 sq_state = MLX5_SQ_STATE_NA;
4386 u8 rq_state = MLX5_RQ_STATE_NA;
4387
4388 if (qp->sq.wqe_cnt) {
4389 err = query_raw_packet_qp_sq_state(dev, sq, &sq_state);
4390 if (err)
4391 return err;
4392 }
4393
4394 if (qp->rq.wqe_cnt) {
4395 err = query_raw_packet_qp_rq_state(dev, rq, &rq_state);
4396 if (err)
4397 return err;
4398 }
4399
4400 return sqrq_state_to_qp_state(sq_state, rq_state, qp,
4401 raw_packet_qp_state);
4402}
4403
4404static int query_qp_attr(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
4405 struct ib_qp_attr *qp_attr)
4406{
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004407 int outlen = MLX5_ST_SZ_BYTES(query_qp_out);
Eli Cohene126ba92013-07-07 17:25:49 +03004408 struct mlx5_qp_context *context;
4409 int mlx5_state;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004410 u32 *outb;
Eli Cohene126ba92013-07-07 17:25:49 +03004411 int err = 0;
4412
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004413 outb = kzalloc(outlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004414 if (!outb)
4415 return -ENOMEM;
4416
majd@mellanox.com19098df2016-01-14 19:13:03 +02004417 err = mlx5_core_qp_query(dev->mdev, &qp->trans_qp.base.mqp, outb,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004418 outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03004419 if (err)
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004420 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004421
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004422 /* FIXME: use MLX5_GET rather than mlx5_qp_context manual struct */
4423 context = (struct mlx5_qp_context *)MLX5_ADDR_OF(query_qp_out, outb, qpc);
4424
Eli Cohene126ba92013-07-07 17:25:49 +03004425 mlx5_state = be32_to_cpu(context->flags) >> 28;
4426
4427 qp->state = to_ib_qp_state(mlx5_state);
Eli Cohene126ba92013-07-07 17:25:49 +03004428 qp_attr->path_mtu = context->mtu_msgmax >> 5;
4429 qp_attr->path_mig_state =
4430 to_ib_mig_state((be32_to_cpu(context->flags) >> 11) & 0x3);
4431 qp_attr->qkey = be32_to_cpu(context->qkey);
4432 qp_attr->rq_psn = be32_to_cpu(context->rnr_nextrecvpsn) & 0xffffff;
4433 qp_attr->sq_psn = be32_to_cpu(context->next_send_psn) & 0xffffff;
4434 qp_attr->dest_qp_num = be32_to_cpu(context->log_pg_sz_remote_qpn) & 0xffffff;
4435 qp_attr->qp_access_flags =
4436 to_ib_qp_access_flags(be32_to_cpu(context->params2));
4437
4438 if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
Dasaratharaman Chandramouli38349382017-04-29 14:41:24 -04004439 to_rdma_ah_attr(dev, &qp_attr->ah_attr, &context->pri_path);
4440 to_rdma_ah_attr(dev, &qp_attr->alt_ah_attr, &context->alt_path);
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03004441 qp_attr->alt_pkey_index =
4442 be16_to_cpu(context->alt_path.pkey_index);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04004443 qp_attr->alt_port_num =
4444 rdma_ah_get_port_num(&qp_attr->alt_ah_attr);
Eli Cohene126ba92013-07-07 17:25:49 +03004445 }
4446
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03004447 qp_attr->pkey_index = be16_to_cpu(context->pri_path.pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03004448 qp_attr->port_num = context->pri_path.port;
4449
4450 /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
4451 qp_attr->sq_draining = mlx5_state == MLX5_QP_STATE_SQ_DRAINING;
4452
4453 qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context->params1) >> 21) & 0x7);
4454
4455 qp_attr->max_dest_rd_atomic =
4456 1 << ((be32_to_cpu(context->params2) >> 21) & 0x7);
4457 qp_attr->min_rnr_timer =
4458 (be32_to_cpu(context->rnr_nextrecvpsn) >> 24) & 0x1f;
4459 qp_attr->timeout = context->pri_path.ackto_lt >> 3;
4460 qp_attr->retry_cnt = (be32_to_cpu(context->params1) >> 16) & 0x7;
4461 qp_attr->rnr_retry = (be32_to_cpu(context->params1) >> 13) & 0x7;
4462 qp_attr->alt_timeout = context->alt_path.ackto_lt >> 3;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004463
4464out:
4465 kfree(outb);
4466 return err;
4467}
4468
4469int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
4470 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
4471{
4472 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4473 struct mlx5_ib_qp *qp = to_mqp(ibqp);
4474 int err = 0;
4475 u8 raw_packet_qp_state;
4476
Yishai Hadas28d61372016-05-23 15:20:56 +03004477 if (ibqp->rwq_ind_tbl)
4478 return -ENOSYS;
4479
Haggai Erand16e91d2016-02-29 15:45:05 +02004480 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4481 return mlx5_ib_gsi_query_qp(ibqp, qp_attr, qp_attr_mask,
4482 qp_init_attr);
4483
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004484 mutex_lock(&qp->mutex);
4485
4486 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET) {
4487 err = query_raw_packet_qp_state(dev, qp, &raw_packet_qp_state);
4488 if (err)
4489 goto out;
4490 qp->state = raw_packet_qp_state;
4491 qp_attr->port_num = 1;
4492 } else {
4493 err = query_qp_attr(dev, qp, qp_attr);
4494 if (err)
4495 goto out;
4496 }
4497
4498 qp_attr->qp_state = qp->state;
Eli Cohene126ba92013-07-07 17:25:49 +03004499 qp_attr->cur_qp_state = qp_attr->qp_state;
4500 qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
4501 qp_attr->cap.max_recv_sge = qp->rq.max_gs;
4502
4503 if (!ibqp->uobject) {
Noa Osherovich0540d812016-06-04 15:15:32 +03004504 qp_attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +03004505 qp_attr->cap.max_send_sge = qp->sq.max_gs;
Noa Osherovich0540d812016-06-04 15:15:32 +03004506 qp_init_attr->qp_context = ibqp->qp_context;
Eli Cohene126ba92013-07-07 17:25:49 +03004507 } else {
4508 qp_attr->cap.max_send_wr = 0;
4509 qp_attr->cap.max_send_sge = 0;
4510 }
4511
Noa Osherovich0540d812016-06-04 15:15:32 +03004512 qp_init_attr->qp_type = ibqp->qp_type;
4513 qp_init_attr->recv_cq = ibqp->recv_cq;
4514 qp_init_attr->send_cq = ibqp->send_cq;
4515 qp_init_attr->srq = ibqp->srq;
4516 qp_attr->cap.max_inline_data = qp->max_inline_data;
Eli Cohene126ba92013-07-07 17:25:49 +03004517
4518 qp_init_attr->cap = qp_attr->cap;
4519
4520 qp_init_attr->create_flags = 0;
4521 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
4522 qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
4523
Leon Romanovsky051f2632015-12-20 12:16:11 +02004524 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
4525 qp_init_attr->create_flags |= IB_QP_CREATE_CROSS_CHANNEL;
4526 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
4527 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_SEND;
4528 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
4529 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_RECV;
Haggai Eranb11a4f92016-02-29 15:45:03 +02004530 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
4531 qp_init_attr->create_flags |= mlx5_ib_create_qp_sqpn_qp1();
Leon Romanovsky051f2632015-12-20 12:16:11 +02004532
Eli Cohene126ba92013-07-07 17:25:49 +03004533 qp_init_attr->sq_sig_type = qp->sq_signal_bits & MLX5_WQE_CTRL_CQ_UPDATE ?
4534 IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
4535
Eli Cohene126ba92013-07-07 17:25:49 +03004536out:
4537 mutex_unlock(&qp->mutex);
4538 return err;
4539}
4540
4541struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
4542 struct ib_ucontext *context,
4543 struct ib_udata *udata)
4544{
4545 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4546 struct mlx5_ib_xrcd *xrcd;
4547 int err;
4548
Saeed Mahameed938fe832015-05-28 22:28:41 +03004549 if (!MLX5_CAP_GEN(dev->mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +03004550 return ERR_PTR(-ENOSYS);
4551
4552 xrcd = kmalloc(sizeof(*xrcd), GFP_KERNEL);
4553 if (!xrcd)
4554 return ERR_PTR(-ENOMEM);
4555
Jack Morgenstein9603b612014-07-28 23:30:22 +03004556 err = mlx5_core_xrcd_alloc(dev->mdev, &xrcd->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03004557 if (err) {
4558 kfree(xrcd);
4559 return ERR_PTR(-ENOMEM);
4560 }
4561
4562 return &xrcd->ibxrcd;
4563}
4564
4565int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
4566{
4567 struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
4568 u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
4569 int err;
4570
Jack Morgenstein9603b612014-07-28 23:30:22 +03004571 err = mlx5_core_xrcd_dealloc(dev->mdev, xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03004572 if (err) {
4573 mlx5_ib_warn(dev, "failed to dealloc xrcdn 0x%x\n", xrcdn);
4574 return err;
4575 }
4576
4577 kfree(xrcd);
4578
4579 return 0;
4580}
Yishai Hadas79b20a62016-05-23 15:20:50 +03004581
Yishai Hadas350d0e42016-08-28 14:58:18 +03004582static void mlx5_ib_wq_event(struct mlx5_core_qp *core_qp, int type)
4583{
4584 struct mlx5_ib_rwq *rwq = to_mibrwq(core_qp);
4585 struct mlx5_ib_dev *dev = to_mdev(rwq->ibwq.device);
4586 struct ib_event event;
4587
4588 if (rwq->ibwq.event_handler) {
4589 event.device = rwq->ibwq.device;
4590 event.element.wq = &rwq->ibwq;
4591 switch (type) {
4592 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
4593 event.event = IB_EVENT_WQ_FATAL;
4594 break;
4595 default:
4596 mlx5_ib_warn(dev, "Unexpected event type %d on WQ %06x\n", type, core_qp->qpn);
4597 return;
4598 }
4599
4600 rwq->ibwq.event_handler(&event, rwq->ibwq.wq_context);
4601 }
4602}
4603
Maor Gottlieb03404e82017-05-30 10:29:13 +03004604static int set_delay_drop(struct mlx5_ib_dev *dev)
4605{
4606 int err = 0;
4607
4608 mutex_lock(&dev->delay_drop.lock);
4609 if (dev->delay_drop.activate)
4610 goto out;
4611
4612 err = mlx5_core_set_delay_drop(dev->mdev, dev->delay_drop.timeout);
4613 if (err)
4614 goto out;
4615
4616 dev->delay_drop.activate = true;
4617out:
4618 mutex_unlock(&dev->delay_drop.lock);
Maor Gottliebfe248c32017-05-30 10:29:14 +03004619
4620 if (!err)
4621 atomic_inc(&dev->delay_drop.rqs_cnt);
Maor Gottlieb03404e82017-05-30 10:29:13 +03004622 return err;
4623}
4624
Yishai Hadas79b20a62016-05-23 15:20:50 +03004625static int create_rq(struct mlx5_ib_rwq *rwq, struct ib_pd *pd,
4626 struct ib_wq_init_attr *init_attr)
4627{
4628 struct mlx5_ib_dev *dev;
Noa Osherovich4be6da12017-01-18 15:40:04 +02004629 int has_net_offloads;
Yishai Hadas79b20a62016-05-23 15:20:50 +03004630 __be64 *rq_pas0;
4631 void *in;
4632 void *rqc;
4633 void *wq;
4634 int inlen;
4635 int err;
4636
4637 dev = to_mdev(pd->device);
4638
4639 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + sizeof(u64) * rwq->rq_num_pas;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004640 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004641 if (!in)
4642 return -ENOMEM;
4643
4644 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
4645 MLX5_SET(rqc, rqc, mem_rq_type,
4646 MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
4647 MLX5_SET(rqc, rqc, user_index, rwq->user_index);
4648 MLX5_SET(rqc, rqc, cqn, to_mcq(init_attr->cq)->mcq.cqn);
4649 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
4650 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
4651 wq = MLX5_ADDR_OF(rqc, rqc, wq);
4652 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
4653 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
4654 MLX5_SET(wq, wq, log_wq_stride, rwq->log_rq_stride);
4655 MLX5_SET(wq, wq, log_wq_sz, rwq->log_rq_size);
4656 MLX5_SET(wq, wq, pd, to_mpd(pd)->pdn);
4657 MLX5_SET(wq, wq, page_offset, rwq->rq_page_offset);
4658 MLX5_SET(wq, wq, log_wq_pg_sz, rwq->log_page_size);
4659 MLX5_SET(wq, wq, wq_signature, rwq->wq_sig);
4660 MLX5_SET64(wq, wq, dbr_addr, rwq->db.dma);
Noa Osherovich4be6da12017-01-18 15:40:04 +02004661 has_net_offloads = MLX5_CAP_GEN(dev->mdev, eth_net_offloads);
Noa Osherovichb1f74a82017-01-18 15:40:02 +02004662 if (init_attr->create_flags & IB_WQ_FLAGS_CVLAN_STRIPPING) {
Noa Osherovich4be6da12017-01-18 15:40:04 +02004663 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
Noa Osherovichb1f74a82017-01-18 15:40:02 +02004664 mlx5_ib_dbg(dev, "VLAN offloads are not supported\n");
4665 err = -EOPNOTSUPP;
4666 goto out;
4667 }
4668 } else {
4669 MLX5_SET(rqc, rqc, vsd, 1);
4670 }
Noa Osherovich4be6da12017-01-18 15:40:04 +02004671 if (init_attr->create_flags & IB_WQ_FLAGS_SCATTER_FCS) {
4672 if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, scatter_fcs))) {
4673 mlx5_ib_dbg(dev, "Scatter FCS is not supported\n");
4674 err = -EOPNOTSUPP;
4675 goto out;
4676 }
4677 MLX5_SET(rqc, rqc, scatter_fcs, 1);
4678 }
Maor Gottlieb03404e82017-05-30 10:29:13 +03004679 if (init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
4680 if (!(dev->ib_dev.attrs.raw_packet_caps &
4681 IB_RAW_PACKET_CAP_DELAY_DROP)) {
4682 mlx5_ib_dbg(dev, "Delay drop is not supported\n");
4683 err = -EOPNOTSUPP;
4684 goto out;
4685 }
4686 MLX5_SET(rqc, rqc, delay_drop_en, 1);
4687 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03004688 rq_pas0 = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
4689 mlx5_ib_populate_pas(dev, rwq->umem, rwq->page_shift, rq_pas0, 0);
Yishai Hadas350d0e42016-08-28 14:58:18 +03004690 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rwq->core_qp);
Maor Gottlieb03404e82017-05-30 10:29:13 +03004691 if (!err && init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
4692 err = set_delay_drop(dev);
4693 if (err) {
4694 mlx5_ib_warn(dev, "Failed to enable delay drop err=%d\n",
4695 err);
4696 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
4697 } else {
4698 rwq->create_flags |= MLX5_IB_WQ_FLAGS_DELAY_DROP;
4699 }
4700 }
Noa Osherovichb1f74a82017-01-18 15:40:02 +02004701out:
Yishai Hadas79b20a62016-05-23 15:20:50 +03004702 kvfree(in);
4703 return err;
4704}
4705
4706static int set_user_rq_size(struct mlx5_ib_dev *dev,
4707 struct ib_wq_init_attr *wq_init_attr,
4708 struct mlx5_ib_create_wq *ucmd,
4709 struct mlx5_ib_rwq *rwq)
4710{
4711 /* Sanity check RQ size before proceeding */
4712 if (wq_init_attr->max_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_wq_sz)))
4713 return -EINVAL;
4714
4715 if (!ucmd->rq_wqe_count)
4716 return -EINVAL;
4717
4718 rwq->wqe_count = ucmd->rq_wqe_count;
4719 rwq->wqe_shift = ucmd->rq_wqe_shift;
4720 rwq->buf_size = (rwq->wqe_count << rwq->wqe_shift);
4721 rwq->log_rq_stride = rwq->wqe_shift;
4722 rwq->log_rq_size = ilog2(rwq->wqe_count);
4723 return 0;
4724}
4725
4726static int prepare_user_rq(struct ib_pd *pd,
4727 struct ib_wq_init_attr *init_attr,
4728 struct ib_udata *udata,
4729 struct mlx5_ib_rwq *rwq)
4730{
4731 struct mlx5_ib_dev *dev = to_mdev(pd->device);
4732 struct mlx5_ib_create_wq ucmd = {};
4733 int err;
4734 size_t required_cmd_sz;
4735
4736 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
4737 if (udata->inlen < required_cmd_sz) {
4738 mlx5_ib_dbg(dev, "invalid inlen\n");
4739 return -EINVAL;
4740 }
4741
4742 if (udata->inlen > sizeof(ucmd) &&
4743 !ib_is_udata_cleared(udata, sizeof(ucmd),
4744 udata->inlen - sizeof(ucmd))) {
4745 mlx5_ib_dbg(dev, "inlen is not supported\n");
4746 return -EOPNOTSUPP;
4747 }
4748
4749 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
4750 mlx5_ib_dbg(dev, "copy failed\n");
4751 return -EFAULT;
4752 }
4753
4754 if (ucmd.comp_mask) {
4755 mlx5_ib_dbg(dev, "invalid comp mask\n");
4756 return -EOPNOTSUPP;
4757 }
4758
4759 if (ucmd.reserved) {
4760 mlx5_ib_dbg(dev, "invalid reserved\n");
4761 return -EOPNOTSUPP;
4762 }
4763
4764 err = set_user_rq_size(dev, init_attr, &ucmd, rwq);
4765 if (err) {
4766 mlx5_ib_dbg(dev, "err %d\n", err);
4767 return err;
4768 }
4769
4770 err = create_user_rq(dev, pd, rwq, &ucmd);
4771 if (err) {
4772 mlx5_ib_dbg(dev, "err %d\n", err);
4773 if (err)
4774 return err;
4775 }
4776
4777 rwq->user_index = ucmd.user_index;
4778 return 0;
4779}
4780
4781struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
4782 struct ib_wq_init_attr *init_attr,
4783 struct ib_udata *udata)
4784{
4785 struct mlx5_ib_dev *dev;
4786 struct mlx5_ib_rwq *rwq;
4787 struct mlx5_ib_create_wq_resp resp = {};
4788 size_t min_resp_len;
4789 int err;
4790
4791 if (!udata)
4792 return ERR_PTR(-ENOSYS);
4793
4794 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
4795 if (udata->outlen && udata->outlen < min_resp_len)
4796 return ERR_PTR(-EINVAL);
4797
4798 dev = to_mdev(pd->device);
4799 switch (init_attr->wq_type) {
4800 case IB_WQT_RQ:
4801 rwq = kzalloc(sizeof(*rwq), GFP_KERNEL);
4802 if (!rwq)
4803 return ERR_PTR(-ENOMEM);
4804 err = prepare_user_rq(pd, init_attr, udata, rwq);
4805 if (err)
4806 goto err;
4807 err = create_rq(rwq, pd, init_attr);
4808 if (err)
4809 goto err_user_rq;
4810 break;
4811 default:
4812 mlx5_ib_dbg(dev, "unsupported wq type %d\n",
4813 init_attr->wq_type);
4814 return ERR_PTR(-EINVAL);
4815 }
4816
Yishai Hadas350d0e42016-08-28 14:58:18 +03004817 rwq->ibwq.wq_num = rwq->core_qp.qpn;
Yishai Hadas79b20a62016-05-23 15:20:50 +03004818 rwq->ibwq.state = IB_WQS_RESET;
4819 if (udata->outlen) {
4820 resp.response_length = offsetof(typeof(resp), response_length) +
4821 sizeof(resp.response_length);
4822 err = ib_copy_to_udata(udata, &resp, resp.response_length);
4823 if (err)
4824 goto err_copy;
4825 }
4826
Yishai Hadas350d0e42016-08-28 14:58:18 +03004827 rwq->core_qp.event = mlx5_ib_wq_event;
4828 rwq->ibwq.event_handler = init_attr->event_handler;
Yishai Hadas79b20a62016-05-23 15:20:50 +03004829 return &rwq->ibwq;
4830
4831err_copy:
Yishai Hadas350d0e42016-08-28 14:58:18 +03004832 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004833err_user_rq:
Maor Gottliebfe248c32017-05-30 10:29:14 +03004834 destroy_user_rq(dev, pd, rwq);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004835err:
4836 kfree(rwq);
4837 return ERR_PTR(err);
4838}
4839
4840int mlx5_ib_destroy_wq(struct ib_wq *wq)
4841{
4842 struct mlx5_ib_dev *dev = to_mdev(wq->device);
4843 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
4844
Yishai Hadas350d0e42016-08-28 14:58:18 +03004845 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Maor Gottliebfe248c32017-05-30 10:29:14 +03004846 destroy_user_rq(dev, wq->pd, rwq);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004847 kfree(rwq);
4848
4849 return 0;
4850}
4851
Yishai Hadasc5f90922016-05-23 15:20:53 +03004852struct ib_rwq_ind_table *mlx5_ib_create_rwq_ind_table(struct ib_device *device,
4853 struct ib_rwq_ind_table_init_attr *init_attr,
4854 struct ib_udata *udata)
4855{
4856 struct mlx5_ib_dev *dev = to_mdev(device);
4857 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl;
4858 int sz = 1 << init_attr->log_ind_tbl_size;
4859 struct mlx5_ib_create_rwq_ind_tbl_resp resp = {};
4860 size_t min_resp_len;
4861 int inlen;
4862 int err;
4863 int i;
4864 u32 *in;
4865 void *rqtc;
4866
4867 if (udata->inlen > 0 &&
4868 !ib_is_udata_cleared(udata, 0,
4869 udata->inlen))
4870 return ERR_PTR(-EOPNOTSUPP);
4871
Maor Gottliebefd7f402016-10-27 16:36:40 +03004872 if (init_attr->log_ind_tbl_size >
4873 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size)) {
4874 mlx5_ib_dbg(dev, "log_ind_tbl_size = %d is bigger than supported = %d\n",
4875 init_attr->log_ind_tbl_size,
4876 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size));
4877 return ERR_PTR(-EINVAL);
4878 }
4879
Yishai Hadasc5f90922016-05-23 15:20:53 +03004880 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
4881 if (udata->outlen && udata->outlen < min_resp_len)
4882 return ERR_PTR(-EINVAL);
4883
4884 rwq_ind_tbl = kzalloc(sizeof(*rwq_ind_tbl), GFP_KERNEL);
4885 if (!rwq_ind_tbl)
4886 return ERR_PTR(-ENOMEM);
4887
4888 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004889 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadasc5f90922016-05-23 15:20:53 +03004890 if (!in) {
4891 err = -ENOMEM;
4892 goto err;
4893 }
4894
4895 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
4896
4897 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
4898 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
4899
4900 for (i = 0; i < sz; i++)
4901 MLX5_SET(rqtc, rqtc, rq_num[i], init_attr->ind_tbl[i]->wq_num);
4902
4903 err = mlx5_core_create_rqt(dev->mdev, in, inlen, &rwq_ind_tbl->rqtn);
4904 kvfree(in);
4905
4906 if (err)
4907 goto err;
4908
4909 rwq_ind_tbl->ib_rwq_ind_tbl.ind_tbl_num = rwq_ind_tbl->rqtn;
4910 if (udata->outlen) {
4911 resp.response_length = offsetof(typeof(resp), response_length) +
4912 sizeof(resp.response_length);
4913 err = ib_copy_to_udata(udata, &resp, resp.response_length);
4914 if (err)
4915 goto err_copy;
4916 }
4917
4918 return &rwq_ind_tbl->ib_rwq_ind_tbl;
4919
4920err_copy:
4921 mlx5_core_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn);
4922err:
4923 kfree(rwq_ind_tbl);
4924 return ERR_PTR(err);
4925}
4926
4927int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
4928{
4929 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl = to_mrwq_ind_table(ib_rwq_ind_tbl);
4930 struct mlx5_ib_dev *dev = to_mdev(ib_rwq_ind_tbl->device);
4931
4932 mlx5_core_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn);
4933
4934 kfree(rwq_ind_tbl);
4935 return 0;
4936}
4937
Yishai Hadas79b20a62016-05-23 15:20:50 +03004938int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
4939 u32 wq_attr_mask, struct ib_udata *udata)
4940{
4941 struct mlx5_ib_dev *dev = to_mdev(wq->device);
4942 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
4943 struct mlx5_ib_modify_wq ucmd = {};
4944 size_t required_cmd_sz;
4945 int curr_wq_state;
4946 int wq_state;
4947 int inlen;
4948 int err;
4949 void *rqc;
4950 void *in;
4951
4952 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
4953 if (udata->inlen < required_cmd_sz)
4954 return -EINVAL;
4955
4956 if (udata->inlen > sizeof(ucmd) &&
4957 !ib_is_udata_cleared(udata, sizeof(ucmd),
4958 udata->inlen - sizeof(ucmd)))
4959 return -EOPNOTSUPP;
4960
4961 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
4962 return -EFAULT;
4963
4964 if (ucmd.comp_mask || ucmd.reserved)
4965 return -EOPNOTSUPP;
4966
4967 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004968 in = kvzalloc(inlen, GFP_KERNEL);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004969 if (!in)
4970 return -ENOMEM;
4971
4972 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
4973
4974 curr_wq_state = (wq_attr_mask & IB_WQ_CUR_STATE) ?
4975 wq_attr->curr_wq_state : wq->state;
4976 wq_state = (wq_attr_mask & IB_WQ_STATE) ?
4977 wq_attr->wq_state : curr_wq_state;
4978 if (curr_wq_state == IB_WQS_ERR)
4979 curr_wq_state = MLX5_RQC_STATE_ERR;
4980 if (wq_state == IB_WQS_ERR)
4981 wq_state = MLX5_RQC_STATE_ERR;
4982 MLX5_SET(modify_rq_in, in, rq_state, curr_wq_state);
4983 MLX5_SET(rqc, rqc, state, wq_state);
4984
Noa Osherovichb1f74a82017-01-18 15:40:02 +02004985 if (wq_attr_mask & IB_WQ_FLAGS) {
4986 if (wq_attr->flags_mask & IB_WQ_FLAGS_CVLAN_STRIPPING) {
4987 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
4988 MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
4989 mlx5_ib_dbg(dev, "VLAN offloads are not "
4990 "supported\n");
4991 err = -EOPNOTSUPP;
4992 goto out;
4993 }
4994 MLX5_SET64(modify_rq_in, in, modify_bitmask,
4995 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
4996 MLX5_SET(rqc, rqc, vsd,
4997 (wq_attr->flags & IB_WQ_FLAGS_CVLAN_STRIPPING) ? 0 : 1);
4998 }
4999 }
5000
Majd Dibbiny23a69642017-01-18 15:25:10 +02005001 if (curr_wq_state == IB_WQS_RESET && wq_state == IB_WQS_RDY) {
5002 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
5003 MLX5_SET64(modify_rq_in, in, modify_bitmask,
5004 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Parav Pandite1f24a72017-04-16 07:29:29 +03005005 MLX5_SET(rqc, rqc, counter_set_id,
5006 dev->port->cnts.set_id);
Majd Dibbiny23a69642017-01-18 15:25:10 +02005007 } else
5008 pr_info_once("%s: Receive WQ counters are not supported on current FW\n",
5009 dev->ib_dev.name);
5010 }
5011
Yishai Hadas350d0e42016-08-28 14:58:18 +03005012 err = mlx5_core_modify_rq(dev->mdev, rwq->core_qp.qpn, in, inlen);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005013 if (!err)
5014 rwq->ibwq.state = (wq_state == MLX5_RQC_STATE_ERR) ? IB_WQS_ERR : wq_state;
5015
Noa Osherovichb1f74a82017-01-18 15:40:02 +02005016out:
5017 kvfree(in);
Yishai Hadas79b20a62016-05-23 15:20:50 +03005018 return err;
5019}