blob: 0b2b17d267a86dfe107391b980c4d48464074244 [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 Cohen0b80c142017-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 Cohen0b80c142017-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 Cohen0b80c142017-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
Yishai Hadas79b20a62016-05-23 15:20:50 +0300678static void destroy_user_rq(struct ib_pd *pd, struct mlx5_ib_rwq *rwq)
679{
680 struct mlx5_ib_ucontext *context;
681
682 context = to_mucontext(pd->uobject->context);
683 mlx5_ib_db_unmap_user(context, &rwq->db);
684 if (rwq->umem)
685 ib_umem_release(rwq->umem);
686}
687
688static int create_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
689 struct mlx5_ib_rwq *rwq,
690 struct mlx5_ib_create_wq *ucmd)
691{
692 struct mlx5_ib_ucontext *context;
693 int page_shift = 0;
694 int npages;
695 u32 offset = 0;
696 int ncont = 0;
697 int err;
698
699 if (!ucmd->buf_addr)
700 return -EINVAL;
701
702 context = to_mucontext(pd->uobject->context);
703 rwq->umem = ib_umem_get(pd->uobject->context, ucmd->buf_addr,
704 rwq->buf_size, 0, 0);
705 if (IS_ERR(rwq->umem)) {
706 mlx5_ib_dbg(dev, "umem_get failed\n");
707 err = PTR_ERR(rwq->umem);
708 return err;
709 }
710
Majd Dibbiny762f8992016-10-27 16:36:47 +0300711 mlx5_ib_cont_pages(rwq->umem, ucmd->buf_addr, 0, &npages, &page_shift,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300712 &ncont, NULL);
713 err = mlx5_ib_get_buf_offset(ucmd->buf_addr, page_shift,
714 &rwq->rq_page_offset);
715 if (err) {
716 mlx5_ib_warn(dev, "bad offset\n");
717 goto err_umem;
718 }
719
720 rwq->rq_num_pas = ncont;
721 rwq->page_shift = page_shift;
722 rwq->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
723 rwq->wq_sig = !!(ucmd->flags & MLX5_WQ_FLAG_SIGNATURE);
724
725 mlx5_ib_dbg(dev, "addr 0x%llx, size %zd, npages %d, page_shift %d, ncont %d, offset %d\n",
726 (unsigned long long)ucmd->buf_addr, rwq->buf_size,
727 npages, page_shift, ncont, offset);
728
729 err = mlx5_ib_db_map_user(context, ucmd->db_addr, &rwq->db);
730 if (err) {
731 mlx5_ib_dbg(dev, "map failed\n");
732 goto err_umem;
733 }
734
735 rwq->create_type = MLX5_WQ_USER;
736 return 0;
737
738err_umem:
739 ib_umem_release(rwq->umem);
740 return err;
741}
742
Eli Cohenb037c292017-01-03 23:55:26 +0200743static int adjust_bfregn(struct mlx5_ib_dev *dev,
744 struct mlx5_bfreg_info *bfregi, int bfregn)
745{
746 return bfregn / MLX5_NON_FP_BFREGS_PER_UAR * MLX5_BFREGS_PER_UAR +
747 bfregn % MLX5_NON_FP_BFREGS_PER_UAR;
748}
749
Eli Cohene126ba92013-07-07 17:25:49 +0300750static int create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
751 struct mlx5_ib_qp *qp, struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200752 struct ib_qp_init_attr *attr,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300753 u32 **in,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200754 struct mlx5_ib_create_qp_resp *resp, int *inlen,
755 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300756{
757 struct mlx5_ib_ucontext *context;
758 struct mlx5_ib_create_qp ucmd;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200759 struct mlx5_ib_ubuffer *ubuffer = &base->ubuffer;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200760 int page_shift = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300761 int uar_index;
762 int npages;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200763 u32 offset = 0;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200764 int bfregn;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200765 int ncont = 0;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300766 __be64 *pas;
767 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300768 int err;
769
770 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
771 if (err) {
772 mlx5_ib_dbg(dev, "copy failed\n");
773 return err;
774 }
775
776 context = to_mucontext(pd->uobject->context);
777 /*
778 * TBD: should come from the verbs when we have the API
779 */
Leon Romanovsky051f2632015-12-20 12:16:11 +0200780 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
781 /* In CROSS_CHANNEL CQ and QP must use the same UAR */
Eli Cohen2f5ff262017-01-03 23:55:21 +0200782 bfregn = MLX5_CROSS_CHANNEL_BFREG;
Leon Romanovsky051f2632015-12-20 12:16:11 +0200783 else {
Eli Cohenb037c292017-01-03 23:55:26 +0200784 bfregn = alloc_bfreg(dev, &context->bfregi, MLX5_IB_LATENCY_CLASS_HIGH);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200785 if (bfregn < 0) {
786 mlx5_ib_dbg(dev, "failed to allocate low latency BFREG\n");
Leon Romanovsky051f2632015-12-20 12:16:11 +0200787 mlx5_ib_dbg(dev, "reverting to medium latency\n");
Eli Cohenb037c292017-01-03 23:55:26 +0200788 bfregn = alloc_bfreg(dev, &context->bfregi, MLX5_IB_LATENCY_CLASS_MEDIUM);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200789 if (bfregn < 0) {
790 mlx5_ib_dbg(dev, "failed to allocate medium latency BFREG\n");
Leon Romanovsky051f2632015-12-20 12:16:11 +0200791 mlx5_ib_dbg(dev, "reverting to high latency\n");
Eli Cohenb037c292017-01-03 23:55:26 +0200792 bfregn = alloc_bfreg(dev, &context->bfregi, MLX5_IB_LATENCY_CLASS_LOW);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200793 if (bfregn < 0) {
794 mlx5_ib_warn(dev, "bfreg allocation failed\n");
795 return bfregn;
Leon Romanovsky051f2632015-12-20 12:16:11 +0200796 }
Eli Cohenc1be5232014-01-14 17:45:12 +0200797 }
Eli Cohene126ba92013-07-07 17:25:49 +0300798 }
799 }
800
Eli Cohenb037c292017-01-03 23:55:26 +0200801 uar_index = bfregn_to_uar_index(dev, &context->bfregi, bfregn);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200802 mlx5_ib_dbg(dev, "bfregn 0x%x, uar_index 0x%x\n", bfregn, uar_index);
Eli Cohene126ba92013-07-07 17:25:49 +0300803
Haggai Eran48fea832014-05-22 14:50:11 +0300804 qp->rq.offset = 0;
805 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
806 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
807
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200808 err = set_user_buf_size(dev, qp, &ucmd, base, attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300809 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200810 goto err_bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300811
majd@mellanox.com19098df2016-01-14 19:13:03 +0200812 if (ucmd.buf_addr && ubuffer->buf_size) {
813 ubuffer->buf_addr = ucmd.buf_addr;
814 err = mlx5_ib_umem_get(dev, pd, ubuffer->buf_addr,
815 ubuffer->buf_size,
816 &ubuffer->umem, &npages, &page_shift,
817 &ncont, &offset);
818 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200819 goto err_bfreg;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200820 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +0200821 ubuffer->umem = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300822 }
Eli Cohene126ba92013-07-07 17:25:49 +0300823
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300824 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
825 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * ncont;
Eli Cohene126ba92013-07-07 17:25:49 +0300826 *in = mlx5_vzalloc(*inlen);
827 if (!*in) {
828 err = -ENOMEM;
829 goto err_umem;
830 }
Eli Cohene126ba92013-07-07 17:25:49 +0300831
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300832 pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas);
833 if (ubuffer->umem)
834 mlx5_ib_populate_pas(dev, ubuffer->umem, page_shift, pas, 0);
835
836 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
837
838 MLX5_SET(qpc, qpc, log_page_size, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
839 MLX5_SET(qpc, qpc, page_offset, offset);
840
841 MLX5_SET(qpc, qpc, uar_page, uar_index);
Eli Cohenb037c292017-01-03 23:55:26 +0200842 resp->bfreg_index = adjust_bfregn(dev, &context->bfregi, bfregn);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200843 qp->bfregn = bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300844
845 err = mlx5_ib_db_map_user(context, ucmd.db_addr, &qp->db);
846 if (err) {
847 mlx5_ib_dbg(dev, "map failed\n");
848 goto err_free;
849 }
850
851 err = ib_copy_to_udata(udata, resp, sizeof(*resp));
852 if (err) {
853 mlx5_ib_dbg(dev, "copy failed\n");
854 goto err_unmap;
855 }
856 qp->create_type = MLX5_QP_USER;
857
858 return 0;
859
860err_unmap:
861 mlx5_ib_db_unmap_user(context, &qp->db);
862
863err_free:
Al Viro479163f2014-11-20 08:13:57 +0000864 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +0300865
866err_umem:
majd@mellanox.com19098df2016-01-14 19:13:03 +0200867 if (ubuffer->umem)
868 ib_umem_release(ubuffer->umem);
Eli Cohene126ba92013-07-07 17:25:49 +0300869
Eli Cohen2f5ff262017-01-03 23:55:21 +0200870err_bfreg:
Eli Cohenb037c292017-01-03 23:55:26 +0200871 free_bfreg(dev, &context->bfregi, bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300872 return err;
873}
874
Eli Cohenb037c292017-01-03 23:55:26 +0200875static void destroy_qp_user(struct mlx5_ib_dev *dev, struct ib_pd *pd,
876 struct mlx5_ib_qp *qp, struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300877{
878 struct mlx5_ib_ucontext *context;
879
880 context = to_mucontext(pd->uobject->context);
881 mlx5_ib_db_unmap_user(context, &qp->db);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200882 if (base->ubuffer.umem)
883 ib_umem_release(base->ubuffer.umem);
Eli Cohenb037c292017-01-03 23:55:26 +0200884 free_bfreg(dev, &context->bfregi, qp->bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300885}
886
887static int create_kernel_qp(struct mlx5_ib_dev *dev,
888 struct ib_qp_init_attr *init_attr,
889 struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300890 u32 **in, int *inlen,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200891 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300892{
Eli Cohene126ba92013-07-07 17:25:49 +0300893 int uar_index;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300894 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300895 int err;
896
Erez Shitritf0313962016-02-21 16:27:17 +0200897 if (init_attr->create_flags & ~(IB_QP_CREATE_SIGNATURE_EN |
898 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
Haggai Eranb11a4f92016-02-29 15:45:03 +0200899 IB_QP_CREATE_IPOIB_UD_LSO |
900 mlx5_ib_create_qp_sqpn_qp1()))
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200901 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300902
903 if (init_attr->qp_type == MLX5_IB_QPT_REG_UMR)
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200904 qp->bf.bfreg = &dev->fp_bfreg;
905 else
906 qp->bf.bfreg = &dev->bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300907
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200908 qp->bf.buf_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
909 uar_index = qp->bf.bfreg->index;
Eli Cohene126ba92013-07-07 17:25:49 +0300910
911 err = calc_sq_size(dev, init_attr, qp);
912 if (err < 0) {
913 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200914 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300915 }
916
917 qp->rq.offset = 0;
918 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200919 base->ubuffer.buf_size = err + (qp->rq.wqe_cnt << qp->rq.wqe_shift);
Eli Cohene126ba92013-07-07 17:25:49 +0300920
majd@mellanox.com19098df2016-01-14 19:13:03 +0200921 err = mlx5_buf_alloc(dev->mdev, base->ubuffer.buf_size, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +0300922 if (err) {
923 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200924 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300925 }
926
927 qp->sq.qend = mlx5_get_send_wqe(qp, qp->sq.wqe_cnt);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300928 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
929 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * qp->buf.npages;
Eli Cohene126ba92013-07-07 17:25:49 +0300930 *in = mlx5_vzalloc(*inlen);
931 if (!*in) {
932 err = -ENOMEM;
933 goto err_buf;
934 }
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300935
936 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
937 MLX5_SET(qpc, qpc, uar_page, uar_index);
938 MLX5_SET(qpc, qpc, log_page_size, qp->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
939
Eli Cohene126ba92013-07-07 17:25:49 +0300940 /* Set "fast registration enabled" for all kernel QPs */
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300941 MLX5_SET(qpc, qpc, fre, 1);
942 MLX5_SET(qpc, qpc, rlky, 1);
Eli Cohene126ba92013-07-07 17:25:49 +0300943
Haggai Eranb11a4f92016-02-29 15:45:03 +0200944 if (init_attr->create_flags & mlx5_ib_create_qp_sqpn_qp1()) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300945 MLX5_SET(qpc, qpc, deth_sqpn, 1);
Haggai Eranb11a4f92016-02-29 15:45:03 +0200946 qp->flags |= MLX5_IB_QP_SQPN_QP1;
947 }
948
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300949 mlx5_fill_page_array(&qp->buf,
950 (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas));
Eli Cohene126ba92013-07-07 17:25:49 +0300951
Jack Morgenstein9603b612014-07-28 23:30:22 +0300952 err = mlx5_db_alloc(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300953 if (err) {
954 mlx5_ib_dbg(dev, "err %d\n", err);
955 goto err_free;
956 }
957
Eli Cohene126ba92013-07-07 17:25:49 +0300958 qp->sq.wrid = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wrid), GFP_KERNEL);
959 qp->sq.wr_data = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wr_data), GFP_KERNEL);
960 qp->rq.wrid = kmalloc(qp->rq.wqe_cnt * sizeof(*qp->rq.wrid), GFP_KERNEL);
961 qp->sq.w_list = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.w_list), GFP_KERNEL);
962 qp->sq.wqe_head = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wqe_head), GFP_KERNEL);
963
964 if (!qp->sq.wrid || !qp->sq.wr_data || !qp->rq.wrid ||
965 !qp->sq.w_list || !qp->sq.wqe_head) {
966 err = -ENOMEM;
967 goto err_wrid;
968 }
969 qp->create_type = MLX5_QP_KERNEL;
970
971 return 0;
972
973err_wrid:
Eli Cohene126ba92013-07-07 17:25:49 +0300974 kfree(qp->sq.wqe_head);
975 kfree(qp->sq.w_list);
976 kfree(qp->sq.wrid);
977 kfree(qp->sq.wr_data);
978 kfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +0200979 mlx5_db_free(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300980
981err_free:
Al Viro479163f2014-11-20 08:13:57 +0000982 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +0300983
984err_buf:
Jack Morgenstein9603b612014-07-28 23:30:22 +0300985 mlx5_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +0300986 return err;
987}
988
989static void destroy_qp_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
990{
Eli Cohene126ba92013-07-07 17:25:49 +0300991 kfree(qp->sq.wqe_head);
992 kfree(qp->sq.w_list);
993 kfree(qp->sq.wrid);
994 kfree(qp->sq.wr_data);
995 kfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +0200996 mlx5_db_free(dev->mdev, &qp->db);
Jack Morgenstein9603b612014-07-28 23:30:22 +0300997 mlx5_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +0300998}
999
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001000static u32 get_rx_type(struct mlx5_ib_qp *qp, struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +03001001{
1002 if (attr->srq || (attr->qp_type == IB_QPT_XRC_TGT) ||
1003 (attr->qp_type == IB_QPT_XRC_INI))
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001004 return MLX5_SRQ_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001005 else if (!qp->has_rq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001006 return MLX5_ZERO_LEN_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001007 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001008 return MLX5_NON_ZERO_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001009}
1010
1011static int is_connected(enum ib_qp_type qp_type)
1012{
1013 if (qp_type == IB_QPT_RC || qp_type == IB_QPT_UC)
1014 return 1;
1015
1016 return 0;
1017}
1018
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001019static int create_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
1020 struct mlx5_ib_sq *sq, u32 tdn)
1021{
Saeed Mahameedc4f287c2016-07-19 20:17:12 +03001022 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001023 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1024
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001025 MLX5_SET(tisc, tisc, transport_domain, tdn);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001026 return mlx5_core_create_tis(dev->mdev, in, sizeof(in), &sq->tisn);
1027}
1028
1029static void destroy_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
1030 struct mlx5_ib_sq *sq)
1031{
1032 mlx5_core_destroy_tis(dev->mdev, sq->tisn);
1033}
1034
1035static int create_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1036 struct mlx5_ib_sq *sq, void *qpin,
1037 struct ib_pd *pd)
1038{
1039 struct mlx5_ib_ubuffer *ubuffer = &sq->ubuffer;
1040 __be64 *pas;
1041 void *in;
1042 void *sqc;
1043 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1044 void *wq;
1045 int inlen;
1046 int err;
1047 int page_shift = 0;
1048 int npages;
1049 int ncont = 0;
1050 u32 offset = 0;
1051
1052 err = mlx5_ib_umem_get(dev, pd, ubuffer->buf_addr, ubuffer->buf_size,
1053 &sq->ubuffer.umem, &npages, &page_shift,
1054 &ncont, &offset);
1055 if (err)
1056 return err;
1057
1058 inlen = MLX5_ST_SZ_BYTES(create_sq_in) + sizeof(u64) * ncont;
1059 in = mlx5_vzalloc(inlen);
1060 if (!in) {
1061 err = -ENOMEM;
1062 goto err_umem;
1063 }
1064
1065 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1066 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
1067 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1068 MLX5_SET(sqc, sqc, user_index, MLX5_GET(qpc, qpc, user_index));
1069 MLX5_SET(sqc, sqc, cqn, MLX5_GET(qpc, qpc, cqn_snd));
1070 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1071 MLX5_SET(sqc, sqc, tis_num_0, sq->tisn);
1072
1073 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1074 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1075 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1076 MLX5_SET(wq, wq, uar_page, MLX5_GET(qpc, qpc, uar_page));
1077 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1078 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1079 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_sq_size));
1080 MLX5_SET(wq, wq, log_wq_pg_sz, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1081 MLX5_SET(wq, wq, page_offset, offset);
1082
1083 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1084 mlx5_ib_populate_pas(dev, sq->ubuffer.umem, page_shift, pas, 0);
1085
1086 err = mlx5_core_create_sq_tracked(dev->mdev, in, inlen, &sq->base.mqp);
1087
1088 kvfree(in);
1089
1090 if (err)
1091 goto err_umem;
1092
1093 return 0;
1094
1095err_umem:
1096 ib_umem_release(sq->ubuffer.umem);
1097 sq->ubuffer.umem = NULL;
1098
1099 return err;
1100}
1101
1102static void destroy_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1103 struct mlx5_ib_sq *sq)
1104{
1105 mlx5_core_destroy_sq_tracked(dev->mdev, &sq->base.mqp);
1106 ib_umem_release(sq->ubuffer.umem);
1107}
1108
1109static int get_rq_pas_size(void *qpc)
1110{
1111 u32 log_page_size = MLX5_GET(qpc, qpc, log_page_size) + 12;
1112 u32 log_rq_stride = MLX5_GET(qpc, qpc, log_rq_stride);
1113 u32 log_rq_size = MLX5_GET(qpc, qpc, log_rq_size);
1114 u32 page_offset = MLX5_GET(qpc, qpc, page_offset);
1115 u32 po_quanta = 1 << (log_page_size - 6);
1116 u32 rq_sz = 1 << (log_rq_size + 4 + log_rq_stride);
1117 u32 page_size = 1 << log_page_size;
1118 u32 rq_sz_po = rq_sz + (page_offset * po_quanta);
1119 u32 rq_num_pas = (rq_sz_po + page_size - 1) / page_size;
1120
1121 return rq_num_pas * sizeof(u64);
1122}
1123
1124static int create_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1125 struct mlx5_ib_rq *rq, void *qpin)
1126{
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001127 struct mlx5_ib_qp *mqp = rq->base.container_mibqp;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001128 __be64 *pas;
1129 __be64 *qp_pas;
1130 void *in;
1131 void *rqc;
1132 void *wq;
1133 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1134 int inlen;
1135 int err;
1136 u32 rq_pas_size = get_rq_pas_size(qpc);
1137
1138 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + rq_pas_size;
1139 in = mlx5_vzalloc(inlen);
1140 if (!in)
1141 return -ENOMEM;
1142
1143 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001144 if (!(rq->flags & MLX5_IB_RQ_CVLAN_STRIPPING))
1145 MLX5_SET(rqc, rqc, vsd, 1);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001146 MLX5_SET(rqc, rqc, mem_rq_type, MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
1147 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
1148 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
1149 MLX5_SET(rqc, rqc, user_index, MLX5_GET(qpc, qpc, user_index));
1150 MLX5_SET(rqc, rqc, cqn, MLX5_GET(qpc, qpc, cqn_rcv));
1151
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001152 if (mqp->flags & MLX5_IB_QP_CAP_SCATTER_FCS)
1153 MLX5_SET(rqc, rqc, scatter_fcs, 1);
1154
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001155 wq = MLX5_ADDR_OF(rqc, rqc, wq);
1156 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1157 MLX5_SET(wq, wq, end_padding_mode,
Maor Gottlieb01581fb2016-01-28 17:51:49 +02001158 MLX5_GET(qpc, qpc, end_padding_mode));
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001159 MLX5_SET(wq, wq, page_offset, MLX5_GET(qpc, qpc, page_offset));
1160 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1161 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1162 MLX5_SET(wq, wq, log_wq_stride, MLX5_GET(qpc, qpc, log_rq_stride) + 4);
1163 MLX5_SET(wq, wq, log_wq_pg_sz, MLX5_GET(qpc, qpc, log_page_size));
1164 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_rq_size));
1165
1166 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1167 qp_pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, qpin, pas);
1168 memcpy(pas, qp_pas, rq_pas_size);
1169
1170 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rq->base.mqp);
1171
1172 kvfree(in);
1173
1174 return err;
1175}
1176
1177static void destroy_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1178 struct mlx5_ib_rq *rq)
1179{
1180 mlx5_core_destroy_rq_tracked(dev->mdev, &rq->base.mqp);
1181}
1182
1183static int create_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1184 struct mlx5_ib_rq *rq, u32 tdn)
1185{
1186 u32 *in;
1187 void *tirc;
1188 int inlen;
1189 int err;
1190
1191 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
1192 in = mlx5_vzalloc(inlen);
1193 if (!in)
1194 return -ENOMEM;
1195
1196 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1197 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
1198 MLX5_SET(tirc, tirc, inline_rqn, rq->base.mqp.qpn);
1199 MLX5_SET(tirc, tirc, transport_domain, tdn);
1200
1201 err = mlx5_core_create_tir(dev->mdev, in, inlen, &rq->tirn);
1202
1203 kvfree(in);
1204
1205 return err;
1206}
1207
1208static void destroy_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1209 struct mlx5_ib_rq *rq)
1210{
1211 mlx5_core_destroy_tir(dev->mdev, rq->tirn);
1212}
1213
1214static int create_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001215 u32 *in,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001216 struct ib_pd *pd)
1217{
1218 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1219 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1220 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1221 struct ib_uobject *uobj = pd->uobject;
1222 struct ib_ucontext *ucontext = uobj->context;
1223 struct mlx5_ib_ucontext *mucontext = to_mucontext(ucontext);
1224 int err;
1225 u32 tdn = mucontext->tdn;
1226
1227 if (qp->sq.wqe_cnt) {
1228 err = create_raw_packet_qp_tis(dev, sq, tdn);
1229 if (err)
1230 return err;
1231
1232 err = create_raw_packet_qp_sq(dev, sq, in, pd);
1233 if (err)
1234 goto err_destroy_tis;
1235
1236 sq->base.container_mibqp = qp;
1237 }
1238
1239 if (qp->rq.wqe_cnt) {
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001240 rq->base.container_mibqp = qp;
1241
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001242 if (qp->flags & MLX5_IB_QP_CVLAN_STRIPPING)
1243 rq->flags |= MLX5_IB_RQ_CVLAN_STRIPPING;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001244 err = create_raw_packet_qp_rq(dev, rq, in);
1245 if (err)
1246 goto err_destroy_sq;
1247
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001248
1249 err = create_raw_packet_qp_tir(dev, rq, tdn);
1250 if (err)
1251 goto err_destroy_rq;
1252 }
1253
1254 qp->trans_qp.base.mqp.qpn = qp->sq.wqe_cnt ? sq->base.mqp.qpn :
1255 rq->base.mqp.qpn;
1256
1257 return 0;
1258
1259err_destroy_rq:
1260 destroy_raw_packet_qp_rq(dev, rq);
1261err_destroy_sq:
1262 if (!qp->sq.wqe_cnt)
1263 return err;
1264 destroy_raw_packet_qp_sq(dev, sq);
1265err_destroy_tis:
1266 destroy_raw_packet_qp_tis(dev, sq);
1267
1268 return err;
1269}
1270
1271static void destroy_raw_packet_qp(struct mlx5_ib_dev *dev,
1272 struct mlx5_ib_qp *qp)
1273{
1274 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1275 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1276 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1277
1278 if (qp->rq.wqe_cnt) {
1279 destroy_raw_packet_qp_tir(dev, rq);
1280 destroy_raw_packet_qp_rq(dev, rq);
1281 }
1282
1283 if (qp->sq.wqe_cnt) {
1284 destroy_raw_packet_qp_sq(dev, sq);
1285 destroy_raw_packet_qp_tis(dev, sq);
1286 }
1287}
1288
1289static void raw_packet_qp_copy_info(struct mlx5_ib_qp *qp,
1290 struct mlx5_ib_raw_packet_qp *raw_packet_qp)
1291{
1292 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1293 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1294
1295 sq->sq = &qp->sq;
1296 rq->rq = &qp->rq;
1297 sq->doorbell = &qp->db;
1298 rq->doorbell = &qp->db;
1299}
1300
Yishai Hadas28d61372016-05-23 15:20:56 +03001301static void destroy_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1302{
1303 mlx5_core_destroy_tir(dev->mdev, qp->rss_qp.tirn);
1304}
1305
1306static int create_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
1307 struct ib_pd *pd,
1308 struct ib_qp_init_attr *init_attr,
1309 struct ib_udata *udata)
1310{
1311 struct ib_uobject *uobj = pd->uobject;
1312 struct ib_ucontext *ucontext = uobj->context;
1313 struct mlx5_ib_ucontext *mucontext = to_mucontext(ucontext);
1314 struct mlx5_ib_create_qp_resp resp = {};
1315 int inlen;
1316 int err;
1317 u32 *in;
1318 void *tirc;
1319 void *hfso;
1320 u32 selected_fields = 0;
1321 size_t min_resp_len;
1322 u32 tdn = mucontext->tdn;
1323 struct mlx5_ib_create_qp_rss ucmd = {};
1324 size_t required_cmd_sz;
1325
1326 if (init_attr->qp_type != IB_QPT_RAW_PACKET)
1327 return -EOPNOTSUPP;
1328
1329 if (init_attr->create_flags || init_attr->send_cq)
1330 return -EINVAL;
1331
Eli Cohen2f5ff262017-01-03 23:55:21 +02001332 min_resp_len = offsetof(typeof(resp), bfreg_index) + sizeof(resp.bfreg_index);
Yishai Hadas28d61372016-05-23 15:20:56 +03001333 if (udata->outlen < min_resp_len)
1334 return -EINVAL;
1335
1336 required_cmd_sz = offsetof(typeof(ucmd), reserved1) + sizeof(ucmd.reserved1);
1337 if (udata->inlen < required_cmd_sz) {
1338 mlx5_ib_dbg(dev, "invalid inlen\n");
1339 return -EINVAL;
1340 }
1341
1342 if (udata->inlen > sizeof(ucmd) &&
1343 !ib_is_udata_cleared(udata, sizeof(ucmd),
1344 udata->inlen - sizeof(ucmd))) {
1345 mlx5_ib_dbg(dev, "inlen is not supported\n");
1346 return -EOPNOTSUPP;
1347 }
1348
1349 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
1350 mlx5_ib_dbg(dev, "copy failed\n");
1351 return -EFAULT;
1352 }
1353
1354 if (ucmd.comp_mask) {
1355 mlx5_ib_dbg(dev, "invalid comp mask\n");
1356 return -EOPNOTSUPP;
1357 }
1358
1359 if (memchr_inv(ucmd.reserved, 0, sizeof(ucmd.reserved)) || ucmd.reserved1) {
1360 mlx5_ib_dbg(dev, "invalid reserved\n");
1361 return -EOPNOTSUPP;
1362 }
1363
1364 err = ib_copy_to_udata(udata, &resp, min_resp_len);
1365 if (err) {
1366 mlx5_ib_dbg(dev, "copy failed\n");
1367 return -EINVAL;
1368 }
1369
1370 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
1371 in = mlx5_vzalloc(inlen);
1372 if (!in)
1373 return -ENOMEM;
1374
1375 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1376 MLX5_SET(tirc, tirc, disp_type,
1377 MLX5_TIRC_DISP_TYPE_INDIRECT);
1378 MLX5_SET(tirc, tirc, indirect_table,
1379 init_attr->rwq_ind_tbl->ind_tbl_num);
1380 MLX5_SET(tirc, tirc, transport_domain, tdn);
1381
1382 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1383 switch (ucmd.rx_hash_function) {
1384 case MLX5_RX_HASH_FUNC_TOEPLITZ:
1385 {
1386 void *rss_key = MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
1387 size_t len = MLX5_FLD_SZ_BYTES(tirc, rx_hash_toeplitz_key);
1388
1389 if (len != ucmd.rx_key_len) {
1390 err = -EINVAL;
1391 goto err;
1392 }
1393
1394 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_TOEPLITZ);
1395 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1396 memcpy(rss_key, ucmd.rx_hash_key, len);
1397 break;
1398 }
1399 default:
1400 err = -EOPNOTSUPP;
1401 goto err;
1402 }
1403
1404 if (!ucmd.rx_hash_fields_mask) {
1405 /* special case when this TIR serves as steering entry without hashing */
1406 if (!init_attr->rwq_ind_tbl->log_ind_tbl_size)
1407 goto create_tir;
1408 err = -EINVAL;
1409 goto err;
1410 }
1411
1412 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1413 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4)) &&
1414 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1415 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))) {
1416 err = -EINVAL;
1417 goto err;
1418 }
1419
1420 /* If none of IPV4 & IPV6 SRC/DST was set - this bit field is ignored */
1421 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1422 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4))
1423 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1424 MLX5_L3_PROT_TYPE_IPV4);
1425 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1426 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1427 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1428 MLX5_L3_PROT_TYPE_IPV6);
1429
1430 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1431 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP)) &&
1432 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1433 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))) {
1434 err = -EINVAL;
1435 goto err;
1436 }
1437
1438 /* If none of TCP & UDP SRC/DST was set - this bit field is ignored */
1439 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1440 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP))
1441 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1442 MLX5_L4_PROT_TYPE_TCP);
1443 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1444 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1445 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1446 MLX5_L4_PROT_TYPE_UDP);
1447
1448 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1449 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6))
1450 selected_fields |= MLX5_HASH_FIELD_SEL_SRC_IP;
1451
1452 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4) ||
1453 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1454 selected_fields |= MLX5_HASH_FIELD_SEL_DST_IP;
1455
1456 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1457 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP))
1458 selected_fields |= MLX5_HASH_FIELD_SEL_L4_SPORT;
1459
1460 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP) ||
1461 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1462 selected_fields |= MLX5_HASH_FIELD_SEL_L4_DPORT;
1463
1464 MLX5_SET(rx_hash_field_select, hfso, selected_fields, selected_fields);
1465
1466create_tir:
1467 err = mlx5_core_create_tir(dev->mdev, in, inlen, &qp->rss_qp.tirn);
1468
1469 if (err)
1470 goto err;
1471
1472 kvfree(in);
1473 /* qpn is reserved for that QP */
1474 qp->trans_qp.base.mqp.qpn = 0;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03001475 qp->flags |= MLX5_IB_QP_RSS;
Yishai Hadas28d61372016-05-23 15:20:56 +03001476 return 0;
1477
1478err:
1479 kvfree(in);
1480 return err;
1481}
1482
Eli Cohene126ba92013-07-07 17:25:49 +03001483static int create_qp_common(struct mlx5_ib_dev *dev, struct ib_pd *pd,
1484 struct ib_qp_init_attr *init_attr,
1485 struct ib_udata *udata, struct mlx5_ib_qp *qp)
1486{
1487 struct mlx5_ib_resources *devr = &dev->devr;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001488 int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
Saeed Mahameed938fe832015-05-28 22:28:41 +03001489 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001490 struct mlx5_ib_create_qp_resp resp;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001491 struct mlx5_ib_cq *send_cq;
1492 struct mlx5_ib_cq *recv_cq;
1493 unsigned long flags;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001494 u32 uidx = MLX5_IB_DEFAULT_UIDX;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001495 struct mlx5_ib_create_qp ucmd;
1496 struct mlx5_ib_qp_base *base;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001497 void *qpc;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001498 u32 *in;
1499 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03001500
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001501 base = init_attr->qp_type == IB_QPT_RAW_PACKET ?
1502 &qp->raw_packet_qp.rq.base :
1503 &qp->trans_qp.base;
1504
Eli Cohene126ba92013-07-07 17:25:49 +03001505 mutex_init(&qp->mutex);
1506 spin_lock_init(&qp->sq.lock);
1507 spin_lock_init(&qp->rq.lock);
1508
Yishai Hadas28d61372016-05-23 15:20:56 +03001509 if (init_attr->rwq_ind_tbl) {
1510 if (!udata)
1511 return -ENOSYS;
1512
1513 err = create_rss_raw_qp_tir(dev, qp, pd, init_attr, udata);
1514 return err;
1515 }
1516
Eli Cohenf360d882014-04-02 00:10:16 +03001517 if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001518 if (!MLX5_CAP_GEN(mdev, block_lb_mc)) {
Eli Cohenf360d882014-04-02 00:10:16 +03001519 mlx5_ib_dbg(dev, "block multicast loopback isn't supported\n");
1520 return -EINVAL;
1521 } else {
1522 qp->flags |= MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK;
1523 }
1524 }
1525
Leon Romanovsky051f2632015-12-20 12:16:11 +02001526 if (init_attr->create_flags &
1527 (IB_QP_CREATE_CROSS_CHANNEL |
1528 IB_QP_CREATE_MANAGED_SEND |
1529 IB_QP_CREATE_MANAGED_RECV)) {
1530 if (!MLX5_CAP_GEN(mdev, cd)) {
1531 mlx5_ib_dbg(dev, "cross-channel isn't supported\n");
1532 return -EINVAL;
1533 }
1534 if (init_attr->create_flags & IB_QP_CREATE_CROSS_CHANNEL)
1535 qp->flags |= MLX5_IB_QP_CROSS_CHANNEL;
1536 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_SEND)
1537 qp->flags |= MLX5_IB_QP_MANAGED_SEND;
1538 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_RECV)
1539 qp->flags |= MLX5_IB_QP_MANAGED_RECV;
1540 }
Erez Shitritf0313962016-02-21 16:27:17 +02001541
1542 if (init_attr->qp_type == IB_QPT_UD &&
1543 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO))
1544 if (!MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
1545 mlx5_ib_dbg(dev, "ipoib UD lso qp isn't supported\n");
1546 return -EOPNOTSUPP;
1547 }
1548
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001549 if (init_attr->create_flags & IB_QP_CREATE_SCATTER_FCS) {
1550 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
1551 mlx5_ib_dbg(dev, "Scatter FCS is supported only for Raw Packet QPs");
1552 return -EOPNOTSUPP;
1553 }
1554 if (!MLX5_CAP_GEN(dev->mdev, eth_net_offloads) ||
1555 !MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
1556 mlx5_ib_dbg(dev, "Scatter FCS isn't supported\n");
1557 return -EOPNOTSUPP;
1558 }
1559 qp->flags |= MLX5_IB_QP_CAP_SCATTER_FCS;
1560 }
1561
Eli Cohene126ba92013-07-07 17:25:49 +03001562 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
1563 qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;
1564
Noa Osheroviche4cc4fa2017-01-18 15:40:03 +02001565 if (init_attr->create_flags & IB_QP_CREATE_CVLAN_STRIPPING) {
1566 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
1567 MLX5_CAP_ETH(dev->mdev, vlan_cap)) ||
1568 (init_attr->qp_type != IB_QPT_RAW_PACKET))
1569 return -EOPNOTSUPP;
1570 qp->flags |= MLX5_IB_QP_CVLAN_STRIPPING;
1571 }
1572
Eli Cohene126ba92013-07-07 17:25:49 +03001573 if (pd && pd->uobject) {
1574 if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
1575 mlx5_ib_dbg(dev, "copy failed\n");
1576 return -EFAULT;
1577 }
1578
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001579 err = get_qp_user_index(to_mucontext(pd->uobject->context),
1580 &ucmd, udata->inlen, &uidx);
1581 if (err)
1582 return err;
1583
Eli Cohene126ba92013-07-07 17:25:49 +03001584 qp->wq_sig = !!(ucmd.flags & MLX5_QP_FLAG_SIGNATURE);
1585 qp->scat_cqe = !!(ucmd.flags & MLX5_QP_FLAG_SCATTER_CQE);
1586 } else {
1587 qp->wq_sig = !!wq_signature;
1588 }
1589
1590 qp->has_rq = qp_has_rq(init_attr);
1591 err = set_rq_size(dev, &init_attr->cap, qp->has_rq,
1592 qp, (pd && pd->uobject) ? &ucmd : NULL);
1593 if (err) {
1594 mlx5_ib_dbg(dev, "err %d\n", err);
1595 return err;
1596 }
1597
1598 if (pd) {
1599 if (pd->uobject) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001600 __u32 max_wqes =
1601 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Eli Cohene126ba92013-07-07 17:25:49 +03001602 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d)\n", ucmd.sq_wqe_count);
1603 if (ucmd.rq_wqe_shift != qp->rq.wqe_shift ||
1604 ucmd.rq_wqe_count != qp->rq.wqe_cnt) {
1605 mlx5_ib_dbg(dev, "invalid rq params\n");
1606 return -EINVAL;
1607 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03001608 if (ucmd.sq_wqe_count > max_wqes) {
Eli Cohene126ba92013-07-07 17:25:49 +03001609 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d) > max allowed (%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +03001610 ucmd.sq_wqe_count, max_wqes);
Eli Cohene126ba92013-07-07 17:25:49 +03001611 return -EINVAL;
1612 }
Haggai Eranb11a4f92016-02-29 15:45:03 +02001613 if (init_attr->create_flags &
1614 mlx5_ib_create_qp_sqpn_qp1()) {
1615 mlx5_ib_dbg(dev, "user-space is not allowed to create UD QPs spoofing as QP1\n");
1616 return -EINVAL;
1617 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001618 err = create_user_qp(dev, pd, qp, udata, init_attr, &in,
1619 &resp, &inlen, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001620 if (err)
1621 mlx5_ib_dbg(dev, "err %d\n", err);
1622 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +02001623 err = create_kernel_qp(dev, init_attr, qp, &in, &inlen,
1624 base);
Eli Cohene126ba92013-07-07 17:25:49 +03001625 if (err)
1626 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohene126ba92013-07-07 17:25:49 +03001627 }
1628
1629 if (err)
1630 return err;
1631 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001632 in = mlx5_vzalloc(inlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001633 if (!in)
1634 return -ENOMEM;
1635
1636 qp->create_type = MLX5_QP_EMPTY;
1637 }
1638
1639 if (is_sqp(init_attr->qp_type))
1640 qp->port = init_attr->port_num;
1641
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001642 qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
1643
1644 MLX5_SET(qpc, qpc, st, to_mlx5_st(init_attr->qp_type));
1645 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
Eli Cohene126ba92013-07-07 17:25:49 +03001646
1647 if (init_attr->qp_type != MLX5_IB_QPT_REG_UMR)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001648 MLX5_SET(qpc, qpc, pd, to_mpd(pd ? pd : devr->p0)->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001649 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001650 MLX5_SET(qpc, qpc, latency_sensitive, 1);
1651
Eli Cohene126ba92013-07-07 17:25:49 +03001652
1653 if (qp->wq_sig)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001654 MLX5_SET(qpc, qpc, wq_signature, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001655
Eli Cohenf360d882014-04-02 00:10:16 +03001656 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001657 MLX5_SET(qpc, qpc, block_lb_mc, 1);
Eli Cohenf360d882014-04-02 00:10:16 +03001658
Leon Romanovsky051f2632015-12-20 12:16:11 +02001659 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001660 MLX5_SET(qpc, qpc, cd_master, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001661 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001662 MLX5_SET(qpc, qpc, cd_slave_send, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001663 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001664 MLX5_SET(qpc, qpc, cd_slave_receive, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001665
Eli Cohene126ba92013-07-07 17:25:49 +03001666 if (qp->scat_cqe && is_connected(init_attr->qp_type)) {
1667 int rcqe_sz;
1668 int scqe_sz;
1669
1670 rcqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->recv_cq);
1671 scqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->send_cq);
1672
1673 if (rcqe_sz == 128)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001674 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA64_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001675 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001676 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA32_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001677
1678 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR) {
1679 if (scqe_sz == 128)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001680 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA64_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001681 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001682 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA32_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001683 }
1684 }
1685
1686 if (qp->rq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001687 MLX5_SET(qpc, qpc, log_rq_stride, qp->rq.wqe_shift - 4);
1688 MLX5_SET(qpc, qpc, log_rq_size, ilog2(qp->rq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03001689 }
1690
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001691 MLX5_SET(qpc, qpc, rq_type, get_rx_type(qp, init_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03001692
1693 if (qp->sq.wqe_cnt)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001694 MLX5_SET(qpc, qpc, log_sq_size, ilog2(qp->sq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03001695 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001696 MLX5_SET(qpc, qpc, no_sq, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001697
1698 /* Set default resources */
1699 switch (init_attr->qp_type) {
1700 case IB_QPT_XRC_TGT:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001701 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
1702 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(devr->c0)->mcq.cqn);
1703 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
1704 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(init_attr->xrcd)->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001705 break;
1706 case IB_QPT_XRC_INI:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001707 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
1708 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
1709 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001710 break;
1711 default:
1712 if (init_attr->srq) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001713 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x0)->xrcdn);
1714 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(init_attr->srq)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001715 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001716 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
1717 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s1)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001718 }
1719 }
1720
1721 if (init_attr->send_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001722 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(init_attr->send_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001723
1724 if (init_attr->recv_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001725 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(init_attr->recv_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001726
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001727 MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
Eli Cohene126ba92013-07-07 17:25:49 +03001728
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001729 /* 0xffffff means we ask to work with cqe version 0 */
1730 if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1)
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001731 MLX5_SET(qpc, qpc, user_index, uidx);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001732
Erez Shitritf0313962016-02-21 16:27:17 +02001733 /* we use IB_QP_CREATE_IPOIB_UD_LSO to indicates ipoib qp */
1734 if (init_attr->qp_type == IB_QPT_UD &&
1735 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)) {
Erez Shitritf0313962016-02-21 16:27:17 +02001736 MLX5_SET(qpc, qpc, ulp_stateless_offload_mode, 1);
1737 qp->flags |= MLX5_IB_QP_LSO;
1738 }
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001739
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001740 if (init_attr->qp_type == IB_QPT_RAW_PACKET) {
1741 qp->raw_packet_qp.sq.ubuffer.buf_addr = ucmd.sq_buf_addr;
1742 raw_packet_qp_copy_info(qp, &qp->raw_packet_qp);
1743 err = create_raw_packet_qp(dev, qp, in, pd);
1744 } else {
1745 err = mlx5_core_create_qp(dev->mdev, &base->mqp, in, inlen);
1746 }
1747
Eli Cohene126ba92013-07-07 17:25:49 +03001748 if (err) {
1749 mlx5_ib_dbg(dev, "create qp failed\n");
1750 goto err_create;
1751 }
1752
Al Viro479163f2014-11-20 08:13:57 +00001753 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001754
majd@mellanox.com19098df2016-01-14 19:13:03 +02001755 base->container_mibqp = qp;
1756 base->mqp.event = mlx5_ib_qp_event;
Eli Cohene126ba92013-07-07 17:25:49 +03001757
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001758 get_cqs(init_attr->qp_type, init_attr->send_cq, init_attr->recv_cq,
1759 &send_cq, &recv_cq);
1760 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
1761 mlx5_ib_lock_cqs(send_cq, recv_cq);
1762 /* Maintain device to QPs access, needed for further handling via reset
1763 * flow
1764 */
1765 list_add_tail(&qp->qps_list, &dev->qp_list);
1766 /* Maintain CQ to QPs access, needed for further handling via reset flow
1767 */
1768 if (send_cq)
1769 list_add_tail(&qp->cq_send_list, &send_cq->list_send_qp);
1770 if (recv_cq)
1771 list_add_tail(&qp->cq_recv_list, &recv_cq->list_recv_qp);
1772 mlx5_ib_unlock_cqs(send_cq, recv_cq);
1773 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
1774
Eli Cohene126ba92013-07-07 17:25:49 +03001775 return 0;
1776
1777err_create:
1778 if (qp->create_type == MLX5_QP_USER)
Eli Cohenb037c292017-01-03 23:55:26 +02001779 destroy_qp_user(dev, pd, qp, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001780 else if (qp->create_type == MLX5_QP_KERNEL)
1781 destroy_qp_kernel(dev, qp);
1782
Al Viro479163f2014-11-20 08:13:57 +00001783 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001784 return err;
1785}
1786
1787static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
1788 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
1789{
1790 if (send_cq) {
1791 if (recv_cq) {
1792 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001793 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001794 spin_lock_nested(&recv_cq->lock,
1795 SINGLE_DEPTH_NESTING);
1796 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001797 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001798 __acquire(&recv_cq->lock);
1799 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001800 spin_lock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001801 spin_lock_nested(&send_cq->lock,
1802 SINGLE_DEPTH_NESTING);
1803 }
1804 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001805 spin_lock(&send_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001806 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001807 }
1808 } else if (recv_cq) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001809 spin_lock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001810 __acquire(&send_cq->lock);
1811 } else {
1812 __acquire(&send_cq->lock);
1813 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001814 }
1815}
1816
1817static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
1818 __releases(&send_cq->lock) __releases(&recv_cq->lock)
1819{
1820 if (send_cq) {
1821 if (recv_cq) {
1822 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
1823 spin_unlock(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001824 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001825 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
1826 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001827 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001828 } else {
1829 spin_unlock(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001830 spin_unlock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001831 }
1832 } else {
Eli Cohen6a4f1392014-12-02 12:26:18 +02001833 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001834 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001835 }
1836 } else if (recv_cq) {
Eli Cohen6a4f1392014-12-02 12:26:18 +02001837 __release(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001838 spin_unlock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001839 } else {
1840 __release(&recv_cq->lock);
1841 __release(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001842 }
1843}
1844
1845static struct mlx5_ib_pd *get_pd(struct mlx5_ib_qp *qp)
1846{
1847 return to_mpd(qp->ibqp.pd);
1848}
1849
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001850static void get_cqs(enum ib_qp_type qp_type,
1851 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
Eli Cohene126ba92013-07-07 17:25:49 +03001852 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq)
1853{
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001854 switch (qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +03001855 case IB_QPT_XRC_TGT:
1856 *send_cq = NULL;
1857 *recv_cq = NULL;
1858 break;
1859 case MLX5_IB_QPT_REG_UMR:
1860 case IB_QPT_XRC_INI:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001861 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03001862 *recv_cq = NULL;
1863 break;
1864
1865 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02001866 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03001867 case IB_QPT_RC:
1868 case IB_QPT_UC:
1869 case IB_QPT_UD:
1870 case IB_QPT_RAW_IPV6:
1871 case IB_QPT_RAW_ETHERTYPE:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001872 case IB_QPT_RAW_PACKET:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001873 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
1874 *recv_cq = ib_recv_cq ? to_mcq(ib_recv_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03001875 break;
1876
Eli Cohene126ba92013-07-07 17:25:49 +03001877 case IB_QPT_MAX:
1878 default:
1879 *send_cq = NULL;
1880 *recv_cq = NULL;
1881 break;
1882 }
1883}
1884
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001885static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03001886 const struct mlx5_modify_raw_qp_param *raw_qp_param,
1887 u8 lag_tx_affinity);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001888
Eli Cohene126ba92013-07-07 17:25:49 +03001889static void destroy_qp_common(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1890{
1891 struct mlx5_ib_cq *send_cq, *recv_cq;
majd@mellanox.com19098df2016-01-14 19:13:03 +02001892 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001893 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03001894 int err;
1895
Yishai Hadas28d61372016-05-23 15:20:56 +03001896 if (qp->ibqp.rwq_ind_tbl) {
1897 destroy_rss_raw_qp_tir(dev, qp);
1898 return;
1899 }
1900
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001901 base = qp->ibqp.qp_type == IB_QPT_RAW_PACKET ?
1902 &qp->raw_packet_qp.rq.base :
1903 &qp->trans_qp.base;
1904
Haggai Eran6aec21f2014-12-11 17:04:23 +02001905 if (qp->state != IB_QPS_RESET) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001906 if (qp->ibqp.qp_type != IB_QPT_RAW_PACKET) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001907 err = mlx5_core_qp_modify(dev->mdev,
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03001908 MLX5_CMD_OP_2RST_QP, 0,
1909 NULL, &base->mqp);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001910 } else {
Alex Vesker0680efa2016-08-28 12:25:52 +03001911 struct mlx5_modify_raw_qp_param raw_qp_param = {
1912 .operation = MLX5_CMD_OP_2RST_QP
1913 };
1914
Aviv Heller13eab212016-09-18 20:48:04 +03001915 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, 0);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001916 }
1917 if (err)
majd@mellanox.com427c1e72016-01-14 19:13:07 +02001918 mlx5_ib_warn(dev, "mlx5_ib: modify QP 0x%06x to RESET failed\n",
majd@mellanox.com19098df2016-01-14 19:13:03 +02001919 base->mqp.qpn);
Haggai Eran6aec21f2014-12-11 17:04:23 +02001920 }
Eli Cohene126ba92013-07-07 17:25:49 +03001921
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001922 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
1923 &send_cq, &recv_cq);
1924
1925 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
1926 mlx5_ib_lock_cqs(send_cq, recv_cq);
1927 /* del from lists under both locks above to protect reset flow paths */
1928 list_del(&qp->qps_list);
1929 if (send_cq)
1930 list_del(&qp->cq_send_list);
1931
1932 if (recv_cq)
1933 list_del(&qp->cq_recv_list);
Eli Cohene126ba92013-07-07 17:25:49 +03001934
1935 if (qp->create_type == MLX5_QP_KERNEL) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02001936 __mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03001937 qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
1938 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02001939 __mlx5_ib_cq_clean(send_cq, base->mqp.qpn,
1940 NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03001941 }
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001942 mlx5_ib_unlock_cqs(send_cq, recv_cq);
1943 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
Eli Cohene126ba92013-07-07 17:25:49 +03001944
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001945 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET) {
1946 destroy_raw_packet_qp(dev, qp);
1947 } else {
1948 err = mlx5_core_destroy_qp(dev->mdev, &base->mqp);
1949 if (err)
1950 mlx5_ib_warn(dev, "failed to destroy QP 0x%x\n",
1951 base->mqp.qpn);
1952 }
Eli Cohene126ba92013-07-07 17:25:49 +03001953
Eli Cohene126ba92013-07-07 17:25:49 +03001954 if (qp->create_type == MLX5_QP_KERNEL)
1955 destroy_qp_kernel(dev, qp);
1956 else if (qp->create_type == MLX5_QP_USER)
Eli Cohenb037c292017-01-03 23:55:26 +02001957 destroy_qp_user(dev, &get_pd(qp)->ibpd, qp, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001958}
1959
1960static const char *ib_qp_type_str(enum ib_qp_type type)
1961{
1962 switch (type) {
1963 case IB_QPT_SMI:
1964 return "IB_QPT_SMI";
1965 case IB_QPT_GSI:
1966 return "IB_QPT_GSI";
1967 case IB_QPT_RC:
1968 return "IB_QPT_RC";
1969 case IB_QPT_UC:
1970 return "IB_QPT_UC";
1971 case IB_QPT_UD:
1972 return "IB_QPT_UD";
1973 case IB_QPT_RAW_IPV6:
1974 return "IB_QPT_RAW_IPV6";
1975 case IB_QPT_RAW_ETHERTYPE:
1976 return "IB_QPT_RAW_ETHERTYPE";
1977 case IB_QPT_XRC_INI:
1978 return "IB_QPT_XRC_INI";
1979 case IB_QPT_XRC_TGT:
1980 return "IB_QPT_XRC_TGT";
1981 case IB_QPT_RAW_PACKET:
1982 return "IB_QPT_RAW_PACKET";
1983 case MLX5_IB_QPT_REG_UMR:
1984 return "MLX5_IB_QPT_REG_UMR";
1985 case IB_QPT_MAX:
1986 default:
1987 return "Invalid QP type";
1988 }
1989}
1990
1991struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd,
1992 struct ib_qp_init_attr *init_attr,
1993 struct ib_udata *udata)
1994{
1995 struct mlx5_ib_dev *dev;
1996 struct mlx5_ib_qp *qp;
1997 u16 xrcdn = 0;
1998 int err;
1999
2000 if (pd) {
2001 dev = to_mdev(pd->device);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002002
2003 if (init_attr->qp_type == IB_QPT_RAW_PACKET) {
2004 if (!pd->uobject) {
2005 mlx5_ib_dbg(dev, "Raw Packet QP is not supported for kernel consumers\n");
2006 return ERR_PTR(-EINVAL);
2007 } else if (!to_mucontext(pd->uobject->context)->cqe_version) {
2008 mlx5_ib_dbg(dev, "Raw Packet QP is only supported for CQE version > 0\n");
2009 return ERR_PTR(-EINVAL);
2010 }
2011 }
Majd Dibbiny09f16cf2016-01-28 17:51:48 +02002012 } else {
2013 /* being cautious here */
2014 if (init_attr->qp_type != IB_QPT_XRC_TGT &&
2015 init_attr->qp_type != MLX5_IB_QPT_REG_UMR) {
2016 pr_warn("%s: no PD for transport %s\n", __func__,
2017 ib_qp_type_str(init_attr->qp_type));
2018 return ERR_PTR(-EINVAL);
2019 }
2020 dev = to_mdev(to_mxrcd(init_attr->xrcd)->ibxrcd.device);
Eli Cohene126ba92013-07-07 17:25:49 +03002021 }
2022
2023 switch (init_attr->qp_type) {
2024 case IB_QPT_XRC_TGT:
2025 case IB_QPT_XRC_INI:
Saeed Mahameed938fe832015-05-28 22:28:41 +03002026 if (!MLX5_CAP_GEN(dev->mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002027 mlx5_ib_dbg(dev, "XRC not supported\n");
2028 return ERR_PTR(-ENOSYS);
2029 }
2030 init_attr->recv_cq = NULL;
2031 if (init_attr->qp_type == IB_QPT_XRC_TGT) {
2032 xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
2033 init_attr->send_cq = NULL;
2034 }
2035
2036 /* fall through */
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002037 case IB_QPT_RAW_PACKET:
Eli Cohene126ba92013-07-07 17:25:49 +03002038 case IB_QPT_RC:
2039 case IB_QPT_UC:
2040 case IB_QPT_UD:
2041 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002042 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002043 case MLX5_IB_QPT_REG_UMR:
2044 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2045 if (!qp)
2046 return ERR_PTR(-ENOMEM);
2047
2048 err = create_qp_common(dev, pd, init_attr, udata, qp);
2049 if (err) {
2050 mlx5_ib_dbg(dev, "create_qp_common failed\n");
2051 kfree(qp);
2052 return ERR_PTR(err);
2053 }
2054
2055 if (is_qp0(init_attr->qp_type))
2056 qp->ibqp.qp_num = 0;
2057 else if (is_qp1(init_attr->qp_type))
2058 qp->ibqp.qp_num = 1;
2059 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002060 qp->ibqp.qp_num = qp->trans_qp.base.mqp.qpn;
Eli Cohene126ba92013-07-07 17:25:49 +03002061
2062 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 +02002063 qp->ibqp.qp_num, qp->trans_qp.base.mqp.qpn,
Eli Cohena1ab8402016-10-27 16:36:46 +03002064 init_attr->recv_cq ? to_mcq(init_attr->recv_cq)->mcq.cqn : -1,
2065 init_attr->send_cq ? to_mcq(init_attr->send_cq)->mcq.cqn : -1);
Eli Cohene126ba92013-07-07 17:25:49 +03002066
majd@mellanox.com19098df2016-01-14 19:13:03 +02002067 qp->trans_qp.xrcdn = xrcdn;
Eli Cohene126ba92013-07-07 17:25:49 +03002068
2069 break;
2070
Haggai Erand16e91d2016-02-29 15:45:05 +02002071 case IB_QPT_GSI:
2072 return mlx5_ib_gsi_create_qp(pd, init_attr);
2073
Eli Cohene126ba92013-07-07 17:25:49 +03002074 case IB_QPT_RAW_IPV6:
2075 case IB_QPT_RAW_ETHERTYPE:
Eli Cohene126ba92013-07-07 17:25:49 +03002076 case IB_QPT_MAX:
2077 default:
2078 mlx5_ib_dbg(dev, "unsupported qp type %d\n",
2079 init_attr->qp_type);
2080 /* Don't support raw QPs */
2081 return ERR_PTR(-EINVAL);
2082 }
2083
2084 return &qp->ibqp;
2085}
2086
2087int mlx5_ib_destroy_qp(struct ib_qp *qp)
2088{
2089 struct mlx5_ib_dev *dev = to_mdev(qp->device);
2090 struct mlx5_ib_qp *mqp = to_mqp(qp);
2091
Haggai Erand16e91d2016-02-29 15:45:05 +02002092 if (unlikely(qp->qp_type == IB_QPT_GSI))
2093 return mlx5_ib_gsi_destroy_qp(qp);
2094
Eli Cohene126ba92013-07-07 17:25:49 +03002095 destroy_qp_common(dev, mqp);
2096
2097 kfree(mqp);
2098
2099 return 0;
2100}
2101
2102static __be32 to_mlx5_access_flags(struct mlx5_ib_qp *qp, const struct ib_qp_attr *attr,
2103 int attr_mask)
2104{
2105 u32 hw_access_flags = 0;
2106 u8 dest_rd_atomic;
2107 u32 access_flags;
2108
2109 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
2110 dest_rd_atomic = attr->max_dest_rd_atomic;
2111 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002112 dest_rd_atomic = qp->trans_qp.resp_depth;
Eli Cohene126ba92013-07-07 17:25:49 +03002113
2114 if (attr_mask & IB_QP_ACCESS_FLAGS)
2115 access_flags = attr->qp_access_flags;
2116 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002117 access_flags = qp->trans_qp.atomic_rd_en;
Eli Cohene126ba92013-07-07 17:25:49 +03002118
2119 if (!dest_rd_atomic)
2120 access_flags &= IB_ACCESS_REMOTE_WRITE;
2121
2122 if (access_flags & IB_ACCESS_REMOTE_READ)
2123 hw_access_flags |= MLX5_QP_BIT_RRE;
2124 if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
2125 hw_access_flags |= (MLX5_QP_BIT_RAE | MLX5_ATOMIC_MODE_CX);
2126 if (access_flags & IB_ACCESS_REMOTE_WRITE)
2127 hw_access_flags |= MLX5_QP_BIT_RWE;
2128
2129 return cpu_to_be32(hw_access_flags);
2130}
2131
2132enum {
2133 MLX5_PATH_FLAG_FL = 1 << 0,
2134 MLX5_PATH_FLAG_FREE_AR = 1 << 1,
2135 MLX5_PATH_FLAG_COUNTER = 1 << 2,
2136};
2137
2138static int ib_rate_to_mlx5(struct mlx5_ib_dev *dev, u8 rate)
2139{
2140 if (rate == IB_RATE_PORT_CURRENT) {
2141 return 0;
2142 } else if (rate < IB_RATE_2_5_GBPS || rate > IB_RATE_300_GBPS) {
2143 return -EINVAL;
2144 } else {
2145 while (rate != IB_RATE_2_5_GBPS &&
2146 !(1 << (rate + MLX5_STAT_RATE_OFFSET) &
Saeed Mahameed938fe832015-05-28 22:28:41 +03002147 MLX5_CAP_GEN(dev->mdev, stat_rate_support)))
Eli Cohene126ba92013-07-07 17:25:49 +03002148 --rate;
2149 }
2150
2151 return rate + MLX5_STAT_RATE_OFFSET;
2152}
2153
majd@mellanox.com75850d02016-01-14 19:13:06 +02002154static int modify_raw_packet_eth_prio(struct mlx5_core_dev *dev,
2155 struct mlx5_ib_sq *sq, u8 sl)
2156{
2157 void *in;
2158 void *tisc;
2159 int inlen;
2160 int err;
2161
2162 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
2163 in = mlx5_vzalloc(inlen);
2164 if (!in)
2165 return -ENOMEM;
2166
2167 MLX5_SET(modify_tis_in, in, bitmask.prio, 1);
2168
2169 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2170 MLX5_SET(tisc, tisc, prio, ((sl & 0x7) << 1));
2171
2172 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2173
2174 kvfree(in);
2175
2176 return err;
2177}
2178
Aviv Heller13eab212016-09-18 20:48:04 +03002179static int modify_raw_packet_tx_affinity(struct mlx5_core_dev *dev,
2180 struct mlx5_ib_sq *sq, u8 tx_affinity)
2181{
2182 void *in;
2183 void *tisc;
2184 int inlen;
2185 int err;
2186
2187 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
2188 in = mlx5_vzalloc(inlen);
2189 if (!in)
2190 return -ENOMEM;
2191
2192 MLX5_SET(modify_tis_in, in, bitmask.lag_tx_port_affinity, 1);
2193
2194 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2195 MLX5_SET(tisc, tisc, lag_tx_port_affinity, tx_affinity);
2196
2197 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2198
2199 kvfree(in);
2200
2201 return err;
2202}
2203
majd@mellanox.com75850d02016-01-14 19:13:06 +02002204static int mlx5_set_path(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
2205 const struct ib_ah_attr *ah,
Eli Cohene126ba92013-07-07 17:25:49 +03002206 struct mlx5_qp_path *path, u8 port, int attr_mask,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002207 u32 path_flags, const struct ib_qp_attr *attr,
2208 bool alt)
Eli Cohene126ba92013-07-07 17:25:49 +03002209{
Achiad Shochat2811ba52015-12-23 18:47:24 +02002210 enum rdma_link_layer ll = rdma_port_get_link_layer(&dev->ib_dev, port);
Eli Cohene126ba92013-07-07 17:25:49 +03002211 int err;
Majd Dibbinyed884512017-01-18 14:10:35 +02002212 enum ib_gid_type gid_type;
Eli Cohene126ba92013-07-07 17:25:49 +03002213
Eli Cohene126ba92013-07-07 17:25:49 +03002214 if (attr_mask & IB_QP_PKEY_INDEX)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002215 path->pkey_index = cpu_to_be16(alt ? attr->alt_pkey_index :
2216 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002217
Eli Cohene126ba92013-07-07 17:25:49 +03002218 if (ah->ah_flags & IB_AH_GRH) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03002219 if (ah->grh.sgid_index >=
2220 dev->mdev->port_caps[port - 1].gid_table_len) {
Joe Perchesf4f01b52015-05-08 15:58:07 -07002221 pr_err("sgid_index (%u) too large. max is %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +03002222 ah->grh.sgid_index,
2223 dev->mdev->port_caps[port - 1].gid_table_len);
Eli Cohenf83b4262014-09-14 16:47:54 +03002224 return -EINVAL;
2225 }
Achiad Shochat2811ba52015-12-23 18:47:24 +02002226 }
2227
2228 if (ll == IB_LINK_LAYER_ETHERNET) {
2229 if (!(ah->ah_flags & IB_AH_GRH))
2230 return -EINVAL;
Majd Dibbinyed884512017-01-18 14:10:35 +02002231 err = mlx5_get_roce_gid_type(dev, port, ah->grh.sgid_index,
2232 &gid_type);
2233 if (err)
2234 return err;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002235 memcpy(path->rmac, ah->dmac, sizeof(ah->dmac));
2236 path->udp_sport = mlx5_get_roce_udp_sport(dev, port,
2237 ah->grh.sgid_index);
2238 path->dci_cfi_prio_sl = (ah->sl & 0x7) << 4;
Majd Dibbinyed884512017-01-18 14:10:35 +02002239 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
2240 path->ecn_dscp = (ah->grh.traffic_class >> 2) & 0x3f;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002241 } else {
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03002242 path->fl_free_ar = (path_flags & MLX5_PATH_FLAG_FL) ? 0x80 : 0;
2243 path->fl_free_ar |=
2244 (path_flags & MLX5_PATH_FLAG_FREE_AR) ? 0x40 : 0;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002245 path->rlid = cpu_to_be16(ah->dlid);
2246 path->grh_mlid = ah->src_path_bits & 0x7f;
2247 if (ah->ah_flags & IB_AH_GRH)
2248 path->grh_mlid |= 1 << 7;
2249 path->dci_cfi_prio_sl = ah->sl & 0xf;
2250 }
2251
2252 if (ah->ah_flags & IB_AH_GRH) {
Eli Cohene126ba92013-07-07 17:25:49 +03002253 path->mgid_index = ah->grh.sgid_index;
2254 path->hop_limit = ah->grh.hop_limit;
2255 path->tclass_flowlabel =
2256 cpu_to_be32((ah->grh.traffic_class << 20) |
2257 (ah->grh.flow_label));
2258 memcpy(path->rgid, ah->grh.dgid.raw, 16);
2259 }
2260
2261 err = ib_rate_to_mlx5(dev, ah->static_rate);
2262 if (err < 0)
2263 return err;
2264 path->static_rate = err;
2265 path->port = port;
2266
Eli Cohene126ba92013-07-07 17:25:49 +03002267 if (attr_mask & IB_QP_TIMEOUT)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002268 path->ackto_lt = (alt ? attr->alt_timeout : attr->timeout) << 3;
Eli Cohene126ba92013-07-07 17:25:49 +03002269
majd@mellanox.com75850d02016-01-14 19:13:06 +02002270 if ((qp->ibqp.qp_type == IB_QPT_RAW_PACKET) && qp->sq.wqe_cnt)
2271 return modify_raw_packet_eth_prio(dev->mdev,
2272 &qp->raw_packet_qp.sq,
2273 ah->sl & 0xf);
2274
Eli Cohene126ba92013-07-07 17:25:49 +03002275 return 0;
2276}
2277
2278static enum mlx5_qp_optpar opt_mask[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE][MLX5_QP_ST_MAX] = {
2279 [MLX5_QP_STATE_INIT] = {
2280 [MLX5_QP_STATE_INIT] = {
2281 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2282 MLX5_QP_OPTPAR_RAE |
2283 MLX5_QP_OPTPAR_RWE |
2284 MLX5_QP_OPTPAR_PKEY_INDEX |
2285 MLX5_QP_OPTPAR_PRI_PORT,
2286 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
2287 MLX5_QP_OPTPAR_PKEY_INDEX |
2288 MLX5_QP_OPTPAR_PRI_PORT,
2289 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2290 MLX5_QP_OPTPAR_Q_KEY |
2291 MLX5_QP_OPTPAR_PRI_PORT,
2292 },
2293 [MLX5_QP_STATE_RTR] = {
2294 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2295 MLX5_QP_OPTPAR_RRE |
2296 MLX5_QP_OPTPAR_RAE |
2297 MLX5_QP_OPTPAR_RWE |
2298 MLX5_QP_OPTPAR_PKEY_INDEX,
2299 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2300 MLX5_QP_OPTPAR_RWE |
2301 MLX5_QP_OPTPAR_PKEY_INDEX,
2302 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2303 MLX5_QP_OPTPAR_Q_KEY,
2304 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_PKEY_INDEX |
2305 MLX5_QP_OPTPAR_Q_KEY,
Eli Cohena4774e92013-09-11 16:35:32 +03002306 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2307 MLX5_QP_OPTPAR_RRE |
2308 MLX5_QP_OPTPAR_RAE |
2309 MLX5_QP_OPTPAR_RWE |
2310 MLX5_QP_OPTPAR_PKEY_INDEX,
Eli Cohene126ba92013-07-07 17:25:49 +03002311 },
2312 },
2313 [MLX5_QP_STATE_RTR] = {
2314 [MLX5_QP_STATE_RTS] = {
2315 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2316 MLX5_QP_OPTPAR_RRE |
2317 MLX5_QP_OPTPAR_RAE |
2318 MLX5_QP_OPTPAR_RWE |
2319 MLX5_QP_OPTPAR_PM_STATE |
2320 MLX5_QP_OPTPAR_RNR_TIMEOUT,
2321 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2322 MLX5_QP_OPTPAR_RWE |
2323 MLX5_QP_OPTPAR_PM_STATE,
2324 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
2325 },
2326 },
2327 [MLX5_QP_STATE_RTS] = {
2328 [MLX5_QP_STATE_RTS] = {
2329 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2330 MLX5_QP_OPTPAR_RAE |
2331 MLX5_QP_OPTPAR_RWE |
2332 MLX5_QP_OPTPAR_RNR_TIMEOUT |
Eli Cohenc2a34312013-10-24 12:01:02 +03002333 MLX5_QP_OPTPAR_PM_STATE |
2334 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03002335 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
Eli Cohenc2a34312013-10-24 12:01:02 +03002336 MLX5_QP_OPTPAR_PM_STATE |
2337 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03002338 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY |
2339 MLX5_QP_OPTPAR_SRQN |
2340 MLX5_QP_OPTPAR_CQN_RCV,
2341 },
2342 },
2343 [MLX5_QP_STATE_SQER] = {
2344 [MLX5_QP_STATE_RTS] = {
2345 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
2346 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_Q_KEY,
Eli Cohen75959f52013-09-11 16:35:31 +03002347 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE,
Eli Cohena4774e92013-09-11 16:35:32 +03002348 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
2349 MLX5_QP_OPTPAR_RWE |
2350 MLX5_QP_OPTPAR_RAE |
2351 MLX5_QP_OPTPAR_RRE,
Eli Cohene126ba92013-07-07 17:25:49 +03002352 },
2353 },
2354};
2355
2356static int ib_nr_to_mlx5_nr(int ib_mask)
2357{
2358 switch (ib_mask) {
2359 case IB_QP_STATE:
2360 return 0;
2361 case IB_QP_CUR_STATE:
2362 return 0;
2363 case IB_QP_EN_SQD_ASYNC_NOTIFY:
2364 return 0;
2365 case IB_QP_ACCESS_FLAGS:
2366 return MLX5_QP_OPTPAR_RWE | MLX5_QP_OPTPAR_RRE |
2367 MLX5_QP_OPTPAR_RAE;
2368 case IB_QP_PKEY_INDEX:
2369 return MLX5_QP_OPTPAR_PKEY_INDEX;
2370 case IB_QP_PORT:
2371 return MLX5_QP_OPTPAR_PRI_PORT;
2372 case IB_QP_QKEY:
2373 return MLX5_QP_OPTPAR_Q_KEY;
2374 case IB_QP_AV:
2375 return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH |
2376 MLX5_QP_OPTPAR_PRI_PORT;
2377 case IB_QP_PATH_MTU:
2378 return 0;
2379 case IB_QP_TIMEOUT:
2380 return MLX5_QP_OPTPAR_ACK_TIMEOUT;
2381 case IB_QP_RETRY_CNT:
2382 return MLX5_QP_OPTPAR_RETRY_COUNT;
2383 case IB_QP_RNR_RETRY:
2384 return MLX5_QP_OPTPAR_RNR_RETRY;
2385 case IB_QP_RQ_PSN:
2386 return 0;
2387 case IB_QP_MAX_QP_RD_ATOMIC:
2388 return MLX5_QP_OPTPAR_SRA_MAX;
2389 case IB_QP_ALT_PATH:
2390 return MLX5_QP_OPTPAR_ALT_ADDR_PATH;
2391 case IB_QP_MIN_RNR_TIMER:
2392 return MLX5_QP_OPTPAR_RNR_TIMEOUT;
2393 case IB_QP_SQ_PSN:
2394 return 0;
2395 case IB_QP_MAX_DEST_RD_ATOMIC:
2396 return MLX5_QP_OPTPAR_RRA_MAX | MLX5_QP_OPTPAR_RWE |
2397 MLX5_QP_OPTPAR_RRE | MLX5_QP_OPTPAR_RAE;
2398 case IB_QP_PATH_MIG_STATE:
2399 return MLX5_QP_OPTPAR_PM_STATE;
2400 case IB_QP_CAP:
2401 return 0;
2402 case IB_QP_DEST_QPN:
2403 return 0;
2404 }
2405 return 0;
2406}
2407
2408static int ib_mask_to_mlx5_opt(int ib_mask)
2409{
2410 int result = 0;
2411 int i;
2412
2413 for (i = 0; i < 8 * sizeof(int); i++) {
2414 if ((1 << i) & ib_mask)
2415 result |= ib_nr_to_mlx5_nr(1 << i);
2416 }
2417
2418 return result;
2419}
2420
Alex Veskereb49ab02016-08-28 12:25:53 +03002421static int modify_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
2422 struct mlx5_ib_rq *rq, int new_state,
2423 const struct mlx5_modify_raw_qp_param *raw_qp_param)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002424{
2425 void *in;
2426 void *rqc;
2427 int inlen;
2428 int err;
2429
2430 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
2431 in = mlx5_vzalloc(inlen);
2432 if (!in)
2433 return -ENOMEM;
2434
2435 MLX5_SET(modify_rq_in, in, rq_state, rq->state);
2436
2437 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
2438 MLX5_SET(rqc, rqc, state, new_state);
2439
Alex Veskereb49ab02016-08-28 12:25:53 +03002440 if (raw_qp_param->set_mask & MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID) {
2441 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
2442 MLX5_SET64(modify_rq_in, in, modify_bitmask,
Majd Dibbiny23a69642017-01-18 15:25:10 +02002443 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
Alex Veskereb49ab02016-08-28 12:25:53 +03002444 MLX5_SET(rqc, rqc, counter_set_id, raw_qp_param->rq_q_ctr_id);
2445 } else
2446 pr_info_once("%s: RAW PACKET QP counters are not supported on current FW\n",
2447 dev->ib_dev.name);
2448 }
2449
2450 err = mlx5_core_modify_rq(dev->mdev, rq->base.mqp.qpn, in, inlen);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002451 if (err)
2452 goto out;
2453
2454 rq->state = new_state;
2455
2456out:
2457 kvfree(in);
2458 return err;
2459}
2460
2461static int modify_raw_packet_qp_sq(struct mlx5_core_dev *dev,
Bodong Wang7d29f342016-12-01 13:43:16 +02002462 struct mlx5_ib_sq *sq,
2463 int new_state,
2464 const struct mlx5_modify_raw_qp_param *raw_qp_param)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002465{
Bodong Wang7d29f342016-12-01 13:43:16 +02002466 struct mlx5_ib_qp *ibqp = sq->base.container_mibqp;
2467 u32 old_rate = ibqp->rate_limit;
2468 u32 new_rate = old_rate;
2469 u16 rl_index = 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002470 void *in;
2471 void *sqc;
2472 int inlen;
2473 int err;
2474
2475 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
2476 in = mlx5_vzalloc(inlen);
2477 if (!in)
2478 return -ENOMEM;
2479
2480 MLX5_SET(modify_sq_in, in, sq_state, sq->state);
2481
2482 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
2483 MLX5_SET(sqc, sqc, state, new_state);
2484
Bodong Wang7d29f342016-12-01 13:43:16 +02002485 if (raw_qp_param->set_mask & MLX5_RAW_QP_RATE_LIMIT) {
2486 if (new_state != MLX5_SQC_STATE_RDY)
2487 pr_warn("%s: Rate limit can only be changed when SQ is moving to RDY\n",
2488 __func__);
2489 else
2490 new_rate = raw_qp_param->rate_limit;
2491 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002492
Bodong Wang7d29f342016-12-01 13:43:16 +02002493 if (old_rate != new_rate) {
2494 if (new_rate) {
2495 err = mlx5_rl_add_rate(dev, new_rate, &rl_index);
2496 if (err) {
2497 pr_err("Failed configuring rate %u: %d\n",
2498 new_rate, err);
2499 goto out;
2500 }
2501 }
2502
2503 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
2504 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
2505 }
2506
2507 err = mlx5_core_modify_sq(dev, sq->base.mqp.qpn, in, inlen);
2508 if (err) {
2509 /* Remove new rate from table if failed */
2510 if (new_rate &&
2511 old_rate != new_rate)
2512 mlx5_rl_remove_rate(dev, new_rate);
2513 goto out;
2514 }
2515
2516 /* Only remove the old rate after new rate was set */
2517 if ((old_rate &&
2518 (old_rate != new_rate)) ||
2519 (new_state != MLX5_SQC_STATE_RDY))
2520 mlx5_rl_remove_rate(dev, old_rate);
2521
2522 ibqp->rate_limit = new_rate;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002523 sq->state = new_state;
2524
2525out:
2526 kvfree(in);
2527 return err;
2528}
2529
2530static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03002531 const struct mlx5_modify_raw_qp_param *raw_qp_param,
2532 u8 tx_affinity)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002533{
2534 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
2535 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
2536 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
Bodong Wang7d29f342016-12-01 13:43:16 +02002537 int modify_rq = !!qp->rq.wqe_cnt;
2538 int modify_sq = !!qp->sq.wqe_cnt;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002539 int rq_state;
2540 int sq_state;
2541 int err;
2542
Alex Vesker0680efa2016-08-28 12:25:52 +03002543 switch (raw_qp_param->operation) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002544 case MLX5_CMD_OP_RST2INIT_QP:
2545 rq_state = MLX5_RQC_STATE_RDY;
2546 sq_state = MLX5_SQC_STATE_RDY;
2547 break;
2548 case MLX5_CMD_OP_2ERR_QP:
2549 rq_state = MLX5_RQC_STATE_ERR;
2550 sq_state = MLX5_SQC_STATE_ERR;
2551 break;
2552 case MLX5_CMD_OP_2RST_QP:
2553 rq_state = MLX5_RQC_STATE_RST;
2554 sq_state = MLX5_SQC_STATE_RST;
2555 break;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002556 case MLX5_CMD_OP_RTR2RTS_QP:
2557 case MLX5_CMD_OP_RTS2RTS_QP:
Bodong Wang7d29f342016-12-01 13:43:16 +02002558 if (raw_qp_param->set_mask ==
2559 MLX5_RAW_QP_RATE_LIMIT) {
2560 modify_rq = 0;
2561 sq_state = sq->state;
2562 } else {
2563 return raw_qp_param->set_mask ? -EINVAL : 0;
2564 }
2565 break;
2566 case MLX5_CMD_OP_INIT2INIT_QP:
2567 case MLX5_CMD_OP_INIT2RTR_QP:
Alex Veskereb49ab02016-08-28 12:25:53 +03002568 if (raw_qp_param->set_mask)
2569 return -EINVAL;
2570 else
2571 return 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002572 default:
2573 WARN_ON(1);
2574 return -EINVAL;
2575 }
2576
Bodong Wang7d29f342016-12-01 13:43:16 +02002577 if (modify_rq) {
2578 err = modify_raw_packet_qp_rq(dev, rq, rq_state, raw_qp_param);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002579 if (err)
2580 return err;
2581 }
2582
Bodong Wang7d29f342016-12-01 13:43:16 +02002583 if (modify_sq) {
Aviv Heller13eab212016-09-18 20:48:04 +03002584 if (tx_affinity) {
2585 err = modify_raw_packet_tx_affinity(dev->mdev, sq,
2586 tx_affinity);
2587 if (err)
2588 return err;
2589 }
2590
Bodong Wang7d29f342016-12-01 13:43:16 +02002591 return modify_raw_packet_qp_sq(dev->mdev, sq, sq_state, raw_qp_param);
Aviv Heller13eab212016-09-18 20:48:04 +03002592 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002593
2594 return 0;
2595}
2596
Eli Cohene126ba92013-07-07 17:25:49 +03002597static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
2598 const struct ib_qp_attr *attr, int attr_mask,
2599 enum ib_qp_state cur_state, enum ib_qp_state new_state)
2600{
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002601 static const u16 optab[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE] = {
2602 [MLX5_QP_STATE_RST] = {
2603 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2604 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2605 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_RST2INIT_QP,
2606 },
2607 [MLX5_QP_STATE_INIT] = {
2608 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2609 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2610 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_INIT2INIT_QP,
2611 [MLX5_QP_STATE_RTR] = MLX5_CMD_OP_INIT2RTR_QP,
2612 },
2613 [MLX5_QP_STATE_RTR] = {
2614 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2615 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2616 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTR2RTS_QP,
2617 },
2618 [MLX5_QP_STATE_RTS] = {
2619 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2620 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2621 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTS2RTS_QP,
2622 },
2623 [MLX5_QP_STATE_SQD] = {
2624 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2625 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2626 },
2627 [MLX5_QP_STATE_SQER] = {
2628 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2629 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2630 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_SQERR2RTS_QP,
2631 },
2632 [MLX5_QP_STATE_ERR] = {
2633 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2634 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2635 }
2636 };
2637
Eli Cohene126ba92013-07-07 17:25:49 +03002638 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2639 struct mlx5_ib_qp *qp = to_mqp(ibqp);
majd@mellanox.com19098df2016-01-14 19:13:03 +02002640 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Eli Cohene126ba92013-07-07 17:25:49 +03002641 struct mlx5_ib_cq *send_cq, *recv_cq;
2642 struct mlx5_qp_context *context;
Eli Cohene126ba92013-07-07 17:25:49 +03002643 struct mlx5_ib_pd *pd;
Alex Veskereb49ab02016-08-28 12:25:53 +03002644 struct mlx5_ib_port *mibport = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002645 enum mlx5_qp_state mlx5_cur, mlx5_new;
2646 enum mlx5_qp_optpar optpar;
Eli Cohene126ba92013-07-07 17:25:49 +03002647 int mlx5_st;
2648 int err;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002649 u16 op;
Aviv Heller13eab212016-09-18 20:48:04 +03002650 u8 tx_affinity = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002651
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002652 context = kzalloc(sizeof(*context), GFP_KERNEL);
2653 if (!context)
Eli Cohene126ba92013-07-07 17:25:49 +03002654 return -ENOMEM;
2655
Eli Cohene126ba92013-07-07 17:25:49 +03002656 err = to_mlx5_st(ibqp->qp_type);
Haggai Eran158abf82016-02-29 15:45:04 +02002657 if (err < 0) {
2658 mlx5_ib_dbg(dev, "unsupported qp type %d\n", ibqp->qp_type);
Eli Cohene126ba92013-07-07 17:25:49 +03002659 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002660 }
Eli Cohene126ba92013-07-07 17:25:49 +03002661
2662 context->flags = cpu_to_be32(err << 16);
2663
2664 if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
2665 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
2666 } else {
2667 switch (attr->path_mig_state) {
2668 case IB_MIG_MIGRATED:
2669 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
2670 break;
2671 case IB_MIG_REARM:
2672 context->flags |= cpu_to_be32(MLX5_QP_PM_REARM << 11);
2673 break;
2674 case IB_MIG_ARMED:
2675 context->flags |= cpu_to_be32(MLX5_QP_PM_ARMED << 11);
2676 break;
2677 }
2678 }
2679
Aviv Heller13eab212016-09-18 20:48:04 +03002680 if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
2681 if ((ibqp->qp_type == IB_QPT_RC) ||
2682 (ibqp->qp_type == IB_QPT_UD &&
2683 !(qp->flags & MLX5_IB_QP_SQPN_QP1)) ||
2684 (ibqp->qp_type == IB_QPT_UC) ||
2685 (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
2686 (ibqp->qp_type == IB_QPT_XRC_INI) ||
2687 (ibqp->qp_type == IB_QPT_XRC_TGT)) {
2688 if (mlx5_lag_is_active(dev->mdev)) {
2689 tx_affinity = (unsigned int)atomic_add_return(1,
2690 &dev->roce.next_port) %
2691 MLX5_MAX_PORTS + 1;
2692 context->flags |= cpu_to_be32(tx_affinity << 24);
2693 }
2694 }
2695 }
2696
Haggai Erand16e91d2016-02-29 15:45:05 +02002697 if (is_sqp(ibqp->qp_type)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002698 context->mtu_msgmax = (IB_MTU_256 << 5) | 8;
2699 } else if (ibqp->qp_type == IB_QPT_UD ||
2700 ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
2701 context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
2702 } else if (attr_mask & IB_QP_PATH_MTU) {
2703 if (attr->path_mtu < IB_MTU_256 ||
2704 attr->path_mtu > IB_MTU_4096) {
2705 mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
2706 err = -EINVAL;
2707 goto out;
2708 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03002709 context->mtu_msgmax = (attr->path_mtu << 5) |
2710 (u8)MLX5_CAP_GEN(dev->mdev, log_max_msg);
Eli Cohene126ba92013-07-07 17:25:49 +03002711 }
2712
2713 if (attr_mask & IB_QP_DEST_QPN)
2714 context->log_pg_sz_remote_qpn = cpu_to_be32(attr->dest_qp_num);
2715
2716 if (attr_mask & IB_QP_PKEY_INDEX)
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03002717 context->pri_path.pkey_index = cpu_to_be16(attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002718
2719 /* todo implement counter_index functionality */
2720
2721 if (is_sqp(ibqp->qp_type))
2722 context->pri_path.port = qp->port;
2723
2724 if (attr_mask & IB_QP_PORT)
2725 context->pri_path.port = attr->port_num;
2726
2727 if (attr_mask & IB_QP_AV) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02002728 err = mlx5_set_path(dev, qp, &attr->ah_attr, &context->pri_path,
Eli Cohene126ba92013-07-07 17:25:49 +03002729 attr_mask & IB_QP_PORT ? attr->port_num : qp->port,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002730 attr_mask, 0, attr, false);
Eli Cohene126ba92013-07-07 17:25:49 +03002731 if (err)
2732 goto out;
2733 }
2734
2735 if (attr_mask & IB_QP_TIMEOUT)
2736 context->pri_path.ackto_lt |= attr->timeout << 3;
2737
2738 if (attr_mask & IB_QP_ALT_PATH) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02002739 err = mlx5_set_path(dev, qp, &attr->alt_ah_attr,
2740 &context->alt_path,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002741 attr->alt_port_num,
2742 attr_mask | IB_QP_PKEY_INDEX | IB_QP_TIMEOUT,
2743 0, attr, true);
Eli Cohene126ba92013-07-07 17:25:49 +03002744 if (err)
2745 goto out;
2746 }
2747
2748 pd = get_pd(qp);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002749 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
2750 &send_cq, &recv_cq);
Eli Cohene126ba92013-07-07 17:25:49 +03002751
2752 context->flags_pd = cpu_to_be32(pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
2753 context->cqn_send = send_cq ? cpu_to_be32(send_cq->mcq.cqn) : 0;
2754 context->cqn_recv = recv_cq ? cpu_to_be32(recv_cq->mcq.cqn) : 0;
2755 context->params1 = cpu_to_be32(MLX5_IB_ACK_REQ_FREQ << 28);
2756
2757 if (attr_mask & IB_QP_RNR_RETRY)
2758 context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
2759
2760 if (attr_mask & IB_QP_RETRY_CNT)
2761 context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
2762
2763 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
2764 if (attr->max_rd_atomic)
2765 context->params1 |=
2766 cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
2767 }
2768
2769 if (attr_mask & IB_QP_SQ_PSN)
2770 context->next_send_psn = cpu_to_be32(attr->sq_psn);
2771
2772 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
2773 if (attr->max_dest_rd_atomic)
2774 context->params2 |=
2775 cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
2776 }
2777
2778 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC))
2779 context->params2 |= to_mlx5_access_flags(qp, attr, attr_mask);
2780
2781 if (attr_mask & IB_QP_MIN_RNR_TIMER)
2782 context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
2783
2784 if (attr_mask & IB_QP_RQ_PSN)
2785 context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
2786
2787 if (attr_mask & IB_QP_QKEY)
2788 context->qkey = cpu_to_be32(attr->qkey);
2789
2790 if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
2791 context->db_rec_addr = cpu_to_be64(qp->db.dma);
2792
Mark Bloch0837e862016-06-17 15:10:55 +03002793 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
2794 u8 port_num = (attr_mask & IB_QP_PORT ? attr->port_num :
2795 qp->port) - 1;
Alex Veskereb49ab02016-08-28 12:25:53 +03002796 mibport = &dev->port[port_num];
Mark Bloch0837e862016-06-17 15:10:55 +03002797 context->qp_counter_set_usr_page |=
Kamal Heib7c16f472017-01-18 15:25:09 +02002798 cpu_to_be32((u32)(mibport->q_cnts.set_id) << 24);
Mark Bloch0837e862016-06-17 15:10:55 +03002799 }
2800
Eli Cohene126ba92013-07-07 17:25:49 +03002801 if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
2802 context->sq_crq_size |= cpu_to_be16(1 << 4);
2803
Haggai Eranb11a4f92016-02-29 15:45:03 +02002804 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
2805 context->deth_sqpn = cpu_to_be32(1);
Eli Cohene126ba92013-07-07 17:25:49 +03002806
2807 mlx5_cur = to_mlx5_state(cur_state);
2808 mlx5_new = to_mlx5_state(new_state);
2809 mlx5_st = to_mlx5_st(ibqp->qp_type);
Eli Cohen07c91132013-10-24 12:01:01 +03002810 if (mlx5_st < 0)
Eli Cohene126ba92013-07-07 17:25:49 +03002811 goto out;
2812
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002813 if (mlx5_cur >= MLX5_QP_NUM_STATE || mlx5_new >= MLX5_QP_NUM_STATE ||
2814 !optab[mlx5_cur][mlx5_new])
2815 goto out;
2816
2817 op = optab[mlx5_cur][mlx5_new];
Eli Cohene126ba92013-07-07 17:25:49 +03002818 optpar = ib_mask_to_mlx5_opt(attr_mask);
2819 optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002820
Alex Vesker0680efa2016-08-28 12:25:52 +03002821 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET) {
2822 struct mlx5_modify_raw_qp_param raw_qp_param = {};
2823
2824 raw_qp_param.operation = op;
Alex Veskereb49ab02016-08-28 12:25:53 +03002825 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
Kamal Heib7c16f472017-01-18 15:25:09 +02002826 raw_qp_param.rq_q_ctr_id = mibport->q_cnts.set_id;
Alex Veskereb49ab02016-08-28 12:25:53 +03002827 raw_qp_param.set_mask |= MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID;
2828 }
Bodong Wang7d29f342016-12-01 13:43:16 +02002829
2830 if (attr_mask & IB_QP_RATE_LIMIT) {
2831 raw_qp_param.rate_limit = attr->rate_limit;
2832 raw_qp_param.set_mask |= MLX5_RAW_QP_RATE_LIMIT;
2833 }
2834
Aviv Heller13eab212016-09-18 20:48:04 +03002835 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, tx_affinity);
Alex Vesker0680efa2016-08-28 12:25:52 +03002836 } else {
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002837 err = mlx5_core_qp_modify(dev->mdev, op, optpar, context,
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002838 &base->mqp);
Alex Vesker0680efa2016-08-28 12:25:52 +03002839 }
2840
Eli Cohene126ba92013-07-07 17:25:49 +03002841 if (err)
2842 goto out;
2843
2844 qp->state = new_state;
2845
2846 if (attr_mask & IB_QP_ACCESS_FLAGS)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002847 qp->trans_qp.atomic_rd_en = attr->qp_access_flags;
Eli Cohene126ba92013-07-07 17:25:49 +03002848 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002849 qp->trans_qp.resp_depth = attr->max_dest_rd_atomic;
Eli Cohene126ba92013-07-07 17:25:49 +03002850 if (attr_mask & IB_QP_PORT)
2851 qp->port = attr->port_num;
2852 if (attr_mask & IB_QP_ALT_PATH)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002853 qp->trans_qp.alt_port = attr->alt_port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03002854
2855 /*
2856 * If we moved a kernel QP to RESET, clean up all old CQ
2857 * entries and reinitialize the QP.
2858 */
2859 if (new_state == IB_QPS_RESET && !ibqp->uobject) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002860 mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03002861 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
2862 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002863 mlx5_ib_cq_clean(send_cq, base->mqp.qpn, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002864
2865 qp->rq.head = 0;
2866 qp->rq.tail = 0;
2867 qp->sq.head = 0;
2868 qp->sq.tail = 0;
2869 qp->sq.cur_post = 0;
2870 qp->sq.last_poll = 0;
2871 qp->db.db[MLX5_RCV_DBR] = 0;
2872 qp->db.db[MLX5_SND_DBR] = 0;
2873 }
2874
2875out:
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002876 kfree(context);
Eli Cohene126ba92013-07-07 17:25:49 +03002877 return err;
2878}
2879
2880int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2881 int attr_mask, struct ib_udata *udata)
2882{
2883 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2884 struct mlx5_ib_qp *qp = to_mqp(ibqp);
Haggai Erand16e91d2016-02-29 15:45:05 +02002885 enum ib_qp_type qp_type;
Eli Cohene126ba92013-07-07 17:25:49 +03002886 enum ib_qp_state cur_state, new_state;
2887 int err = -EINVAL;
2888 int port;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002889 enum rdma_link_layer ll = IB_LINK_LAYER_UNSPECIFIED;
Eli Cohene126ba92013-07-07 17:25:49 +03002890
Yishai Hadas28d61372016-05-23 15:20:56 +03002891 if (ibqp->rwq_ind_tbl)
2892 return -ENOSYS;
2893
Haggai Erand16e91d2016-02-29 15:45:05 +02002894 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
2895 return mlx5_ib_gsi_modify_qp(ibqp, attr, attr_mask);
2896
2897 qp_type = (unlikely(ibqp->qp_type == MLX5_IB_QPT_HW_GSI)) ?
2898 IB_QPT_GSI : ibqp->qp_type;
2899
Eli Cohene126ba92013-07-07 17:25:49 +03002900 mutex_lock(&qp->mutex);
2901
2902 cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
2903 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
2904
Achiad Shochat2811ba52015-12-23 18:47:24 +02002905 if (!(cur_state == new_state && cur_state == IB_QPS_RESET)) {
2906 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
2907 ll = dev->ib_dev.get_link_layer(&dev->ib_dev, port);
2908 }
2909
Haggai Erand16e91d2016-02-29 15:45:05 +02002910 if (qp_type != MLX5_IB_QPT_REG_UMR &&
2911 !ib_modify_qp_is_ok(cur_state, new_state, qp_type, attr_mask, ll)) {
Haggai Eran158abf82016-02-29 15:45:04 +02002912 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
2913 cur_state, new_state, ibqp->qp_type, attr_mask);
Eli Cohene126ba92013-07-07 17:25:49 +03002914 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002915 }
Eli Cohene126ba92013-07-07 17:25:49 +03002916
2917 if ((attr_mask & IB_QP_PORT) &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03002918 (attr->port_num == 0 ||
Haggai Eran158abf82016-02-29 15:45:04 +02002919 attr->port_num > MLX5_CAP_GEN(dev->mdev, num_ports))) {
2920 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
2921 attr->port_num, dev->num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03002922 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002923 }
Eli Cohene126ba92013-07-07 17:25:49 +03002924
2925 if (attr_mask & IB_QP_PKEY_INDEX) {
2926 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Saeed Mahameed938fe832015-05-28 22:28:41 +03002927 if (attr->pkey_index >=
Haggai Eran158abf82016-02-29 15:45:04 +02002928 dev->mdev->port_caps[port - 1].pkey_table_len) {
2929 mlx5_ib_dbg(dev, "invalid pkey index %d\n",
2930 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002931 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002932 }
Eli Cohene126ba92013-07-07 17:25:49 +03002933 }
2934
2935 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03002936 attr->max_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02002937 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_res_qp))) {
2938 mlx5_ib_dbg(dev, "invalid max_rd_atomic value %d\n",
2939 attr->max_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03002940 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002941 }
Eli Cohene126ba92013-07-07 17:25:49 +03002942
2943 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03002944 attr->max_dest_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02002945 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_req_qp))) {
2946 mlx5_ib_dbg(dev, "invalid max_dest_rd_atomic value %d\n",
2947 attr->max_dest_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03002948 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002949 }
Eli Cohene126ba92013-07-07 17:25:49 +03002950
2951 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
2952 err = 0;
2953 goto out;
2954 }
2955
2956 err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
2957
2958out:
2959 mutex_unlock(&qp->mutex);
2960 return err;
2961}
2962
2963static int mlx5_wq_overflow(struct mlx5_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
2964{
2965 struct mlx5_ib_cq *cq;
2966 unsigned cur;
2967
2968 cur = wq->head - wq->tail;
2969 if (likely(cur + nreq < wq->max_post))
2970 return 0;
2971
2972 cq = to_mcq(ib_cq);
2973 spin_lock(&cq->lock);
2974 cur = wq->head - wq->tail;
2975 spin_unlock(&cq->lock);
2976
2977 return cur + nreq >= wq->max_post;
2978}
2979
2980static __always_inline void set_raddr_seg(struct mlx5_wqe_raddr_seg *rseg,
2981 u64 remote_addr, u32 rkey)
2982{
2983 rseg->raddr = cpu_to_be64(remote_addr);
2984 rseg->rkey = cpu_to_be32(rkey);
2985 rseg->reserved = 0;
2986}
2987
Erez Shitritf0313962016-02-21 16:27:17 +02002988static void *set_eth_seg(struct mlx5_wqe_eth_seg *eseg,
2989 struct ib_send_wr *wr, void *qend,
2990 struct mlx5_ib_qp *qp, int *size)
2991{
2992 void *seg = eseg;
2993
2994 memset(eseg, 0, sizeof(struct mlx5_wqe_eth_seg));
2995
2996 if (wr->send_flags & IB_SEND_IP_CSUM)
2997 eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM |
2998 MLX5_ETH_WQE_L4_CSUM;
2999
3000 seg += sizeof(struct mlx5_wqe_eth_seg);
3001 *size += sizeof(struct mlx5_wqe_eth_seg) / 16;
3002
3003 if (wr->opcode == IB_WR_LSO) {
3004 struct ib_ud_wr *ud_wr = container_of(wr, struct ib_ud_wr, wr);
3005 int size_of_inl_hdr_start = sizeof(eseg->inline_hdr_start);
3006 u64 left, leftlen, copysz;
3007 void *pdata = ud_wr->header;
3008
3009 left = ud_wr->hlen;
3010 eseg->mss = cpu_to_be16(ud_wr->mss);
3011 eseg->inline_hdr_sz = cpu_to_be16(left);
3012
3013 /*
3014 * check if there is space till the end of queue, if yes,
3015 * copy all in one shot, otherwise copy till the end of queue,
3016 * rollback and than the copy the left
3017 */
3018 leftlen = qend - (void *)eseg->inline_hdr_start;
3019 copysz = min_t(u64, leftlen, left);
3020
3021 memcpy(seg - size_of_inl_hdr_start, pdata, copysz);
3022
3023 if (likely(copysz > size_of_inl_hdr_start)) {
3024 seg += ALIGN(copysz - size_of_inl_hdr_start, 16);
3025 *size += ALIGN(copysz - size_of_inl_hdr_start, 16) / 16;
3026 }
3027
3028 if (unlikely(copysz < left)) { /* the last wqe in the queue */
3029 seg = mlx5_get_send_wqe(qp, 0);
3030 left -= copysz;
3031 pdata += copysz;
3032 memcpy(seg, pdata, left);
3033 seg += ALIGN(left, 16);
3034 *size += ALIGN(left, 16) / 16;
3035 }
3036 }
3037
3038 return seg;
3039}
3040
Eli Cohene126ba92013-07-07 17:25:49 +03003041static void set_datagram_seg(struct mlx5_wqe_datagram_seg *dseg,
3042 struct ib_send_wr *wr)
3043{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003044 memcpy(&dseg->av, &to_mah(ud_wr(wr)->ah)->av, sizeof(struct mlx5_av));
3045 dseg->av.dqp_dct = cpu_to_be32(ud_wr(wr)->remote_qpn | MLX5_EXTENDED_UD_AV);
3046 dseg->av.key.qkey.qkey = cpu_to_be32(ud_wr(wr)->remote_qkey);
Eli Cohene126ba92013-07-07 17:25:49 +03003047}
3048
3049static void set_data_ptr_seg(struct mlx5_wqe_data_seg *dseg, struct ib_sge *sg)
3050{
3051 dseg->byte_count = cpu_to_be32(sg->length);
3052 dseg->lkey = cpu_to_be32(sg->lkey);
3053 dseg->addr = cpu_to_be64(sg->addr);
3054}
3055
Artemy Kovalyov31616252017-01-02 11:37:42 +02003056static u64 get_xlt_octo(u64 bytes)
Eli Cohene126ba92013-07-07 17:25:49 +03003057{
Artemy Kovalyov31616252017-01-02 11:37:42 +02003058 return ALIGN(bytes, MLX5_IB_UMR_XLT_ALIGNMENT) /
3059 MLX5_IB_UMR_OCTOWORD;
Eli Cohene126ba92013-07-07 17:25:49 +03003060}
3061
3062static __be64 frwr_mkey_mask(void)
3063{
3064 u64 result;
3065
3066 result = MLX5_MKEY_MASK_LEN |
3067 MLX5_MKEY_MASK_PAGE_SIZE |
3068 MLX5_MKEY_MASK_START_ADDR |
3069 MLX5_MKEY_MASK_EN_RINVAL |
3070 MLX5_MKEY_MASK_KEY |
3071 MLX5_MKEY_MASK_LR |
3072 MLX5_MKEY_MASK_LW |
3073 MLX5_MKEY_MASK_RR |
3074 MLX5_MKEY_MASK_RW |
3075 MLX5_MKEY_MASK_A |
3076 MLX5_MKEY_MASK_SMALL_FENCE |
3077 MLX5_MKEY_MASK_FREE;
3078
3079 return cpu_to_be64(result);
3080}
3081
Sagi Grimberge6631812014-02-23 14:19:11 +02003082static __be64 sig_mkey_mask(void)
3083{
3084 u64 result;
3085
3086 result = MLX5_MKEY_MASK_LEN |
3087 MLX5_MKEY_MASK_PAGE_SIZE |
3088 MLX5_MKEY_MASK_START_ADDR |
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003089 MLX5_MKEY_MASK_EN_SIGERR |
Sagi Grimberge6631812014-02-23 14:19:11 +02003090 MLX5_MKEY_MASK_EN_RINVAL |
3091 MLX5_MKEY_MASK_KEY |
3092 MLX5_MKEY_MASK_LR |
3093 MLX5_MKEY_MASK_LW |
3094 MLX5_MKEY_MASK_RR |
3095 MLX5_MKEY_MASK_RW |
3096 MLX5_MKEY_MASK_SMALL_FENCE |
3097 MLX5_MKEY_MASK_FREE |
3098 MLX5_MKEY_MASK_BSF_EN;
3099
3100 return cpu_to_be64(result);
3101}
3102
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003103static void set_reg_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02003104 struct mlx5_ib_mr *mr)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003105{
Artemy Kovalyov31616252017-01-02 11:37:42 +02003106 int size = mr->ndescs * mr->desc_size;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003107
3108 memset(umr, 0, sizeof(*umr));
Sagi Grimbergb005d312016-02-29 19:07:33 +02003109
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003110 umr->flags = MLX5_UMR_CHECK_NOT_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02003111 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003112 umr->mkey_mask = frwr_mkey_mask();
3113}
3114
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003115static void set_linv_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr)
Eli Cohene126ba92013-07-07 17:25:49 +03003116{
3117 memset(umr, 0, sizeof(*umr));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003118 umr->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
Max Gurtovoy2d221582016-10-27 16:36:36 +03003119 umr->flags = MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03003120}
3121
Artemy Kovalyov31616252017-01-02 11:37:42 +02003122static __be64 get_umr_enable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02003123{
3124 u64 result;
3125
Artemy Kovalyov31616252017-01-02 11:37:42 +02003126 result = MLX5_MKEY_MASK_KEY |
Haggai Eran968e78d2014-12-11 17:04:11 +02003127 MLX5_MKEY_MASK_FREE;
3128
3129 return cpu_to_be64(result);
3130}
3131
Artemy Kovalyov31616252017-01-02 11:37:42 +02003132static __be64 get_umr_disable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02003133{
3134 u64 result;
3135
3136 result = MLX5_MKEY_MASK_FREE;
3137
3138 return cpu_to_be64(result);
3139}
3140
Noa Osherovich56e11d62016-02-29 16:46:51 +02003141static __be64 get_umr_update_translation_mask(void)
3142{
3143 u64 result;
3144
3145 result = MLX5_MKEY_MASK_LEN |
3146 MLX5_MKEY_MASK_PAGE_SIZE |
Artemy Kovalyov31616252017-01-02 11:37:42 +02003147 MLX5_MKEY_MASK_START_ADDR;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003148
3149 return cpu_to_be64(result);
3150}
3151
Artemy Kovalyov31616252017-01-02 11:37:42 +02003152static __be64 get_umr_update_access_mask(int atomic)
Noa Osherovich56e11d62016-02-29 16:46:51 +02003153{
3154 u64 result;
3155
Artemy Kovalyov31616252017-01-02 11:37:42 +02003156 result = MLX5_MKEY_MASK_LR |
3157 MLX5_MKEY_MASK_LW |
Noa Osherovich56e11d62016-02-29 16:46:51 +02003158 MLX5_MKEY_MASK_RR |
Artemy Kovalyov31616252017-01-02 11:37:42 +02003159 MLX5_MKEY_MASK_RW;
3160
3161 if (atomic)
3162 result |= MLX5_MKEY_MASK_A;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003163
3164 return cpu_to_be64(result);
3165}
3166
3167static __be64 get_umr_update_pd_mask(void)
3168{
3169 u64 result;
3170
Artemy Kovalyov31616252017-01-02 11:37:42 +02003171 result = MLX5_MKEY_MASK_PD;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003172
3173 return cpu_to_be64(result);
3174}
3175
Eli Cohene126ba92013-07-07 17:25:49 +03003176static void set_reg_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Maor Gottlieb578e7262016-10-27 16:36:37 +03003177 struct ib_send_wr *wr, int atomic)
Eli Cohene126ba92013-07-07 17:25:49 +03003178{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003179 struct mlx5_umr_wr *umrwr = umr_wr(wr);
Eli Cohene126ba92013-07-07 17:25:49 +03003180
3181 memset(umr, 0, sizeof(*umr));
3182
Haggai Eran968e78d2014-12-11 17:04:11 +02003183 if (wr->send_flags & MLX5_IB_SEND_UMR_FAIL_IF_FREE)
3184 umr->flags = MLX5_UMR_CHECK_FREE; /* fail if free */
3185 else
3186 umr->flags = MLX5_UMR_CHECK_NOT_FREE; /* fail if not free */
3187
Artemy Kovalyov31616252017-01-02 11:37:42 +02003188 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(umrwr->xlt_size));
3189 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_XLT) {
3190 u64 offset = get_xlt_octo(umrwr->offset);
3191
3192 umr->xlt_offset = cpu_to_be16(offset & 0xffff);
3193 umr->xlt_offset_47_16 = cpu_to_be32(offset >> 16);
3194 umr->flags |= MLX5_UMR_TRANSLATION_OFFSET_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03003195 }
Artemy Kovalyov31616252017-01-02 11:37:42 +02003196 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION)
3197 umr->mkey_mask |= get_umr_update_translation_mask();
3198 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS) {
3199 umr->mkey_mask |= get_umr_update_access_mask(atomic);
3200 umr->mkey_mask |= get_umr_update_pd_mask();
3201 }
3202 if (wr->send_flags & MLX5_IB_SEND_UMR_ENABLE_MR)
3203 umr->mkey_mask |= get_umr_enable_mr_mask();
3204 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
3205 umr->mkey_mask |= get_umr_disable_mr_mask();
Eli Cohene126ba92013-07-07 17:25:49 +03003206
3207 if (!wr->num_sge)
Haggai Eran968e78d2014-12-11 17:04:11 +02003208 umr->flags |= MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03003209}
3210
3211static u8 get_umr_flags(int acc)
3212{
3213 return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC : 0) |
3214 (acc & IB_ACCESS_REMOTE_WRITE ? MLX5_PERM_REMOTE_WRITE : 0) |
3215 (acc & IB_ACCESS_REMOTE_READ ? MLX5_PERM_REMOTE_READ : 0) |
3216 (acc & IB_ACCESS_LOCAL_WRITE ? MLX5_PERM_LOCAL_WRITE : 0) |
Sagi Grimberg2ac45932014-02-23 14:19:09 +02003217 MLX5_PERM_LOCAL_READ | MLX5_PERM_UMR_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03003218}
3219
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003220static void set_reg_mkey_seg(struct mlx5_mkey_seg *seg,
3221 struct mlx5_ib_mr *mr,
3222 u32 key, int access)
3223{
3224 int ndescs = ALIGN(mr->ndescs, 8) >> 1;
3225
3226 memset(seg, 0, sizeof(*seg));
Sagi Grimbergb005d312016-02-29 19:07:33 +02003227
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003228 if (mr->access_mode == MLX5_MKC_ACCESS_MODE_MTT)
Sagi Grimbergb005d312016-02-29 19:07:33 +02003229 seg->log2_page_size = ilog2(mr->ibmr.page_size);
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003230 else if (mr->access_mode == MLX5_MKC_ACCESS_MODE_KLMS)
Sagi Grimbergb005d312016-02-29 19:07:33 +02003231 /* KLMs take twice the size of MTTs */
3232 ndescs *= 2;
3233
3234 seg->flags = get_umr_flags(access) | mr->access_mode;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003235 seg->qpn_mkey7_0 = cpu_to_be32((key & 0xff) | 0xffffff00);
3236 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL);
3237 seg->start_addr = cpu_to_be64(mr->ibmr.iova);
3238 seg->len = cpu_to_be64(mr->ibmr.length);
3239 seg->xlt_oct_size = cpu_to_be32(ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003240}
3241
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003242static void set_linv_mkey_seg(struct mlx5_mkey_seg *seg)
Eli Cohene126ba92013-07-07 17:25:49 +03003243{
3244 memset(seg, 0, sizeof(*seg));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003245 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03003246}
3247
3248static void set_reg_mkey_segment(struct mlx5_mkey_seg *seg, struct ib_send_wr *wr)
3249{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003250 struct mlx5_umr_wr *umrwr = umr_wr(wr);
Haggai Eran968e78d2014-12-11 17:04:11 +02003251
Eli Cohene126ba92013-07-07 17:25:49 +03003252 memset(seg, 0, sizeof(*seg));
Artemy Kovalyov31616252017-01-02 11:37:42 +02003253 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
Haggai Eran968e78d2014-12-11 17:04:11 +02003254 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03003255
Haggai Eran968e78d2014-12-11 17:04:11 +02003256 seg->flags = convert_access(umrwr->access_flags);
Artemy Kovalyov31616252017-01-02 11:37:42 +02003257 if (umrwr->pd)
3258 seg->flags_pd = cpu_to_be32(to_mpd(umrwr->pd)->pdn);
3259 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION &&
3260 !umrwr->length)
3261 seg->flags_pd |= cpu_to_be32(MLX5_MKEY_LEN64);
3262
3263 seg->start_addr = cpu_to_be64(umrwr->virt_addr);
Haggai Eran968e78d2014-12-11 17:04:11 +02003264 seg->len = cpu_to_be64(umrwr->length);
3265 seg->log2_page_size = umrwr->page_shift;
Eli Cohen746b5582013-10-23 09:53:14 +03003266 seg->qpn_mkey7_0 = cpu_to_be32(0xffffff00 |
Haggai Eran968e78d2014-12-11 17:04:11 +02003267 mlx5_mkey_variant(umrwr->mkey));
Eli Cohene126ba92013-07-07 17:25:49 +03003268}
3269
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003270static void set_reg_data_seg(struct mlx5_wqe_data_seg *dseg,
3271 struct mlx5_ib_mr *mr,
3272 struct mlx5_ib_pd *pd)
3273{
3274 int bcount = mr->desc_size * mr->ndescs;
3275
3276 dseg->addr = cpu_to_be64(mr->desc_map);
3277 dseg->byte_count = cpu_to_be32(ALIGN(bcount, 64));
3278 dseg->lkey = cpu_to_be32(pd->ibpd.local_dma_lkey);
3279}
3280
Eli Cohene126ba92013-07-07 17:25:49 +03003281static __be32 send_ieth(struct ib_send_wr *wr)
3282{
3283 switch (wr->opcode) {
3284 case IB_WR_SEND_WITH_IMM:
3285 case IB_WR_RDMA_WRITE_WITH_IMM:
3286 return wr->ex.imm_data;
3287
3288 case IB_WR_SEND_WITH_INV:
3289 return cpu_to_be32(wr->ex.invalidate_rkey);
3290
3291 default:
3292 return 0;
3293 }
3294}
3295
3296static u8 calc_sig(void *wqe, int size)
3297{
3298 u8 *p = wqe;
3299 u8 res = 0;
3300 int i;
3301
3302 for (i = 0; i < size; i++)
3303 res ^= p[i];
3304
3305 return ~res;
3306}
3307
3308static u8 wq_sig(void *wqe)
3309{
3310 return calc_sig(wqe, (*((u8 *)wqe + 8) & 0x3f) << 4);
3311}
3312
3313static int set_data_inl_seg(struct mlx5_ib_qp *qp, struct ib_send_wr *wr,
3314 void *wqe, int *sz)
3315{
3316 struct mlx5_wqe_inline_seg *seg;
3317 void *qend = qp->sq.qend;
3318 void *addr;
3319 int inl = 0;
3320 int copy;
3321 int len;
3322 int i;
3323
3324 seg = wqe;
3325 wqe += sizeof(*seg);
3326 for (i = 0; i < wr->num_sge; i++) {
3327 addr = (void *)(unsigned long)(wr->sg_list[i].addr);
3328 len = wr->sg_list[i].length;
3329 inl += len;
3330
3331 if (unlikely(inl > qp->max_inline_data))
3332 return -ENOMEM;
3333
3334 if (unlikely(wqe + len > qend)) {
3335 copy = qend - wqe;
3336 memcpy(wqe, addr, copy);
3337 addr += copy;
3338 len -= copy;
3339 wqe = mlx5_get_send_wqe(qp, 0);
3340 }
3341 memcpy(wqe, addr, len);
3342 wqe += len;
3343 }
3344
3345 seg->byte_count = cpu_to_be32(inl | MLX5_INLINE_SEG);
3346
3347 *sz = ALIGN(inl + sizeof(seg->byte_count), 16) / 16;
3348
3349 return 0;
3350}
3351
Sagi Grimberge6631812014-02-23 14:19:11 +02003352static u16 prot_field_size(enum ib_signature_type type)
3353{
3354 switch (type) {
3355 case IB_SIG_TYPE_T10_DIF:
3356 return MLX5_DIF_SIZE;
3357 default:
3358 return 0;
3359 }
3360}
3361
3362static u8 bs_selector(int block_size)
3363{
3364 switch (block_size) {
3365 case 512: return 0x1;
3366 case 520: return 0x2;
3367 case 4096: return 0x3;
3368 case 4160: return 0x4;
3369 case 1073741824: return 0x5;
3370 default: return 0;
3371 }
3372}
3373
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003374static void mlx5_fill_inl_bsf(struct ib_sig_domain *domain,
3375 struct mlx5_bsf_inl *inl)
Sagi Grimberge6631812014-02-23 14:19:11 +02003376{
Sagi Grimberg142537f2014-08-13 19:54:32 +03003377 /* Valid inline section and allow BSF refresh */
3378 inl->vld_refresh = cpu_to_be16(MLX5_BSF_INL_VALID |
3379 MLX5_BSF_REFRESH_DIF);
3380 inl->dif_apptag = cpu_to_be16(domain->sig.dif.app_tag);
3381 inl->dif_reftag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003382 /* repeating block */
3383 inl->rp_inv_seed = MLX5_BSF_REPEAT_BLOCK;
3384 inl->sig_type = domain->sig.dif.bg_type == IB_T10DIF_CRC ?
3385 MLX5_DIF_CRC : MLX5_DIF_IPCS;
Sagi Grimberge6631812014-02-23 14:19:11 +02003386
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003387 if (domain->sig.dif.ref_remap)
3388 inl->dif_inc_ref_guard_check |= MLX5_BSF_INC_REFTAG;
Sagi Grimberge6631812014-02-23 14:19:11 +02003389
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003390 if (domain->sig.dif.app_escape) {
3391 if (domain->sig.dif.ref_escape)
3392 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPREF_ESCAPE;
3393 else
3394 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPTAG_ESCAPE;
Sagi Grimberge6631812014-02-23 14:19:11 +02003395 }
3396
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003397 inl->dif_app_bitmask_check =
3398 cpu_to_be16(domain->sig.dif.apptag_check_mask);
Sagi Grimberge6631812014-02-23 14:19:11 +02003399}
3400
3401static int mlx5_set_bsf(struct ib_mr *sig_mr,
3402 struct ib_sig_attrs *sig_attrs,
3403 struct mlx5_bsf *bsf, u32 data_size)
3404{
3405 struct mlx5_core_sig_ctx *msig = to_mmr(sig_mr)->sig;
3406 struct mlx5_bsf_basic *basic = &bsf->basic;
3407 struct ib_sig_domain *mem = &sig_attrs->mem;
3408 struct ib_sig_domain *wire = &sig_attrs->wire;
Sagi Grimberge6631812014-02-23 14:19:11 +02003409
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003410 memset(bsf, 0, sizeof(*bsf));
Sagi Grimberge6631812014-02-23 14:19:11 +02003411
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003412 /* Basic + Extended + Inline */
3413 basic->bsf_size_sbs = 1 << 7;
3414 /* Input domain check byte mask */
3415 basic->check_byte_mask = sig_attrs->check_mask;
3416 basic->raw_data_size = cpu_to_be32(data_size);
3417
3418 /* Memory domain */
3419 switch (sig_attrs->mem.sig_type) {
3420 case IB_SIG_TYPE_NONE:
3421 break;
3422 case IB_SIG_TYPE_T10_DIF:
3423 basic->mem.bs_selector = bs_selector(mem->sig.dif.pi_interval);
3424 basic->m_bfs_psv = cpu_to_be32(msig->psv_memory.psv_idx);
3425 mlx5_fill_inl_bsf(mem, &bsf->m_inl);
3426 break;
3427 default:
3428 return -EINVAL;
3429 }
3430
3431 /* Wire domain */
3432 switch (sig_attrs->wire.sig_type) {
3433 case IB_SIG_TYPE_NONE:
3434 break;
3435 case IB_SIG_TYPE_T10_DIF:
Sagi Grimberge6631812014-02-23 14:19:11 +02003436 if (mem->sig.dif.pi_interval == wire->sig.dif.pi_interval &&
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003437 mem->sig_type == wire->sig_type) {
Sagi Grimberge6631812014-02-23 14:19:11 +02003438 /* Same block structure */
Sagi Grimberg142537f2014-08-13 19:54:32 +03003439 basic->bsf_size_sbs |= 1 << 4;
Sagi Grimberge6631812014-02-23 14:19:11 +02003440 if (mem->sig.dif.bg_type == wire->sig.dif.bg_type)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003441 basic->wire.copy_byte_mask |= MLX5_CPY_GRD_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003442 if (mem->sig.dif.app_tag == wire->sig.dif.app_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003443 basic->wire.copy_byte_mask |= MLX5_CPY_APP_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003444 if (mem->sig.dif.ref_tag == wire->sig.dif.ref_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003445 basic->wire.copy_byte_mask |= MLX5_CPY_REF_MASK;
Sagi Grimberge6631812014-02-23 14:19:11 +02003446 } else
3447 basic->wire.bs_selector = bs_selector(wire->sig.dif.pi_interval);
3448
Sagi Grimberg142537f2014-08-13 19:54:32 +03003449 basic->w_bfs_psv = cpu_to_be32(msig->psv_wire.psv_idx);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003450 mlx5_fill_inl_bsf(wire, &bsf->w_inl);
Sagi Grimberge6631812014-02-23 14:19:11 +02003451 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003452 default:
3453 return -EINVAL;
3454 }
3455
3456 return 0;
3457}
3458
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003459static int set_sig_data_segment(struct ib_sig_handover_wr *wr,
3460 struct mlx5_ib_qp *qp, void **seg, int *size)
Sagi Grimberge6631812014-02-23 14:19:11 +02003461{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003462 struct ib_sig_attrs *sig_attrs = wr->sig_attrs;
3463 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003464 struct mlx5_bsf *bsf;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003465 u32 data_len = wr->wr.sg_list->length;
3466 u32 data_key = wr->wr.sg_list->lkey;
3467 u64 data_va = wr->wr.sg_list->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003468 int ret;
3469 int wqe_size;
3470
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003471 if (!wr->prot ||
3472 (data_key == wr->prot->lkey &&
3473 data_va == wr->prot->addr &&
3474 data_len == wr->prot->length)) {
Sagi Grimberge6631812014-02-23 14:19:11 +02003475 /**
3476 * Source domain doesn't contain signature information
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003477 * or data and protection are interleaved in memory.
Sagi Grimberge6631812014-02-23 14:19:11 +02003478 * So need construct:
3479 * ------------------
3480 * | data_klm |
3481 * ------------------
3482 * | BSF |
3483 * ------------------
3484 **/
3485 struct mlx5_klm *data_klm = *seg;
3486
3487 data_klm->bcount = cpu_to_be32(data_len);
3488 data_klm->key = cpu_to_be32(data_key);
3489 data_klm->va = cpu_to_be64(data_va);
3490 wqe_size = ALIGN(sizeof(*data_klm), 64);
3491 } else {
3492 /**
3493 * Source domain contains signature information
3494 * So need construct a strided block format:
3495 * ---------------------------
3496 * | stride_block_ctrl |
3497 * ---------------------------
3498 * | data_klm |
3499 * ---------------------------
3500 * | prot_klm |
3501 * ---------------------------
3502 * | BSF |
3503 * ---------------------------
3504 **/
3505 struct mlx5_stride_block_ctrl_seg *sblock_ctrl;
3506 struct mlx5_stride_block_entry *data_sentry;
3507 struct mlx5_stride_block_entry *prot_sentry;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003508 u32 prot_key = wr->prot->lkey;
3509 u64 prot_va = wr->prot->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003510 u16 block_size = sig_attrs->mem.sig.dif.pi_interval;
3511 int prot_size;
3512
3513 sblock_ctrl = *seg;
3514 data_sentry = (void *)sblock_ctrl + sizeof(*sblock_ctrl);
3515 prot_sentry = (void *)data_sentry + sizeof(*data_sentry);
3516
3517 prot_size = prot_field_size(sig_attrs->mem.sig_type);
3518 if (!prot_size) {
3519 pr_err("Bad block size given: %u\n", block_size);
3520 return -EINVAL;
3521 }
3522 sblock_ctrl->bcount_per_cycle = cpu_to_be32(block_size +
3523 prot_size);
3524 sblock_ctrl->op = cpu_to_be32(MLX5_STRIDE_BLOCK_OP);
3525 sblock_ctrl->repeat_count = cpu_to_be32(data_len / block_size);
3526 sblock_ctrl->num_entries = cpu_to_be16(2);
3527
3528 data_sentry->bcount = cpu_to_be16(block_size);
3529 data_sentry->key = cpu_to_be32(data_key);
3530 data_sentry->va = cpu_to_be64(data_va);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003531 data_sentry->stride = cpu_to_be16(block_size);
3532
Sagi Grimberge6631812014-02-23 14:19:11 +02003533 prot_sentry->bcount = cpu_to_be16(prot_size);
3534 prot_sentry->key = cpu_to_be32(prot_key);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003535 prot_sentry->va = cpu_to_be64(prot_va);
3536 prot_sentry->stride = cpu_to_be16(prot_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02003537
Sagi Grimberge6631812014-02-23 14:19:11 +02003538 wqe_size = ALIGN(sizeof(*sblock_ctrl) + sizeof(*data_sentry) +
3539 sizeof(*prot_sentry), 64);
3540 }
3541
3542 *seg += wqe_size;
3543 *size += wqe_size / 16;
3544 if (unlikely((*seg == qp->sq.qend)))
3545 *seg = mlx5_get_send_wqe(qp, 0);
3546
3547 bsf = *seg;
3548 ret = mlx5_set_bsf(sig_mr, sig_attrs, bsf, data_len);
3549 if (ret)
3550 return -EINVAL;
3551
3552 *seg += sizeof(*bsf);
3553 *size += sizeof(*bsf) / 16;
3554 if (unlikely((*seg == qp->sq.qend)))
3555 *seg = mlx5_get_send_wqe(qp, 0);
3556
3557 return 0;
3558}
3559
3560static void set_sig_mkey_segment(struct mlx5_mkey_seg *seg,
Artemy Kovalyov31616252017-01-02 11:37:42 +02003561 struct ib_sig_handover_wr *wr, u32 size,
Sagi Grimberge6631812014-02-23 14:19:11 +02003562 u32 length, u32 pdn)
3563{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003564 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003565 u32 sig_key = sig_mr->rkey;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003566 u8 sigerr = to_mmr(sig_mr)->sig->sigerr_count & 1;
Sagi Grimberge6631812014-02-23 14:19:11 +02003567
3568 memset(seg, 0, sizeof(*seg));
3569
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003570 seg->flags = get_umr_flags(wr->access_flags) |
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003571 MLX5_MKC_ACCESS_MODE_KLMS;
Sagi Grimberge6631812014-02-23 14:19:11 +02003572 seg->qpn_mkey7_0 = cpu_to_be32((sig_key & 0xff) | 0xffffff00);
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003573 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL | sigerr << 26 |
Sagi Grimberge6631812014-02-23 14:19:11 +02003574 MLX5_MKEY_BSF_EN | pdn);
3575 seg->len = cpu_to_be64(length);
Artemy Kovalyov31616252017-01-02 11:37:42 +02003576 seg->xlt_oct_size = cpu_to_be32(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02003577 seg->bsfs_octo_size = cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE);
3578}
3579
3580static void set_sig_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02003581 u32 size)
Sagi Grimberge6631812014-02-23 14:19:11 +02003582{
3583 memset(umr, 0, sizeof(*umr));
3584
3585 umr->flags = MLX5_FLAGS_INLINE | MLX5_FLAGS_CHECK_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02003586 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02003587 umr->bsf_octowords = cpu_to_be16(MLX5_MKEY_BSF_OCTO_SIZE);
3588 umr->mkey_mask = sig_mkey_mask();
3589}
3590
3591
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003592static int set_sig_umr_wr(struct ib_send_wr *send_wr, struct mlx5_ib_qp *qp,
Sagi Grimberge6631812014-02-23 14:19:11 +02003593 void **seg, int *size)
3594{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003595 struct ib_sig_handover_wr *wr = sig_handover_wr(send_wr);
3596 struct mlx5_ib_mr *sig_mr = to_mmr(wr->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02003597 u32 pdn = get_pd(qp)->pdn;
Artemy Kovalyov31616252017-01-02 11:37:42 +02003598 u32 xlt_size;
Sagi Grimberge6631812014-02-23 14:19:11 +02003599 int region_len, ret;
3600
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003601 if (unlikely(wr->wr.num_sge != 1) ||
3602 unlikely(wr->access_flags & IB_ACCESS_REMOTE_ATOMIC) ||
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003603 unlikely(!sig_mr->sig) || unlikely(!qp->signature_en) ||
3604 unlikely(!sig_mr->sig->sig_status_checked))
Sagi Grimberge6631812014-02-23 14:19:11 +02003605 return -EINVAL;
3606
3607 /* length of the protected region, data + protection */
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003608 region_len = wr->wr.sg_list->length;
3609 if (wr->prot &&
3610 (wr->prot->lkey != wr->wr.sg_list->lkey ||
3611 wr->prot->addr != wr->wr.sg_list->addr ||
3612 wr->prot->length != wr->wr.sg_list->length))
3613 region_len += wr->prot->length;
Sagi Grimberge6631812014-02-23 14:19:11 +02003614
3615 /**
3616 * KLM octoword size - if protection was provided
3617 * then we use strided block format (3 octowords),
3618 * else we use single KLM (1 octoword)
3619 **/
Artemy Kovalyov31616252017-01-02 11:37:42 +02003620 xlt_size = wr->prot ? 0x30 : sizeof(struct mlx5_klm);
Sagi Grimberge6631812014-02-23 14:19:11 +02003621
Artemy Kovalyov31616252017-01-02 11:37:42 +02003622 set_sig_umr_segment(*seg, xlt_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02003623 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
3624 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
3625 if (unlikely((*seg == qp->sq.qend)))
3626 *seg = mlx5_get_send_wqe(qp, 0);
3627
Artemy Kovalyov31616252017-01-02 11:37:42 +02003628 set_sig_mkey_segment(*seg, wr, xlt_size, region_len, pdn);
Sagi Grimberge6631812014-02-23 14:19:11 +02003629 *seg += sizeof(struct mlx5_mkey_seg);
3630 *size += sizeof(struct mlx5_mkey_seg) / 16;
3631 if (unlikely((*seg == qp->sq.qend)))
3632 *seg = mlx5_get_send_wqe(qp, 0);
3633
3634 ret = set_sig_data_segment(wr, qp, seg, size);
3635 if (ret)
3636 return ret;
3637
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003638 sig_mr->sig->sig_status_checked = false;
Sagi Grimberge6631812014-02-23 14:19:11 +02003639 return 0;
3640}
3641
3642static int set_psv_wr(struct ib_sig_domain *domain,
3643 u32 psv_idx, void **seg, int *size)
3644{
3645 struct mlx5_seg_set_psv *psv_seg = *seg;
3646
3647 memset(psv_seg, 0, sizeof(*psv_seg));
3648 psv_seg->psv_num = cpu_to_be32(psv_idx);
3649 switch (domain->sig_type) {
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003650 case IB_SIG_TYPE_NONE:
3651 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003652 case IB_SIG_TYPE_T10_DIF:
3653 psv_seg->transient_sig = cpu_to_be32(domain->sig.dif.bg << 16 |
3654 domain->sig.dif.app_tag);
3655 psv_seg->ref_tag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberge6631812014-02-23 14:19:11 +02003656 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003657 default:
Leon Romanovsky12bbf1e2017-01-18 14:10:31 +02003658 pr_err("Bad signature type (%d) is given.\n",
3659 domain->sig_type);
3660 return -EINVAL;
Sagi Grimberge6631812014-02-23 14:19:11 +02003661 }
3662
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003663 *seg += sizeof(*psv_seg);
3664 *size += sizeof(*psv_seg) / 16;
3665
Sagi Grimberge6631812014-02-23 14:19:11 +02003666 return 0;
3667}
3668
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003669static int set_reg_wr(struct mlx5_ib_qp *qp,
3670 struct ib_reg_wr *wr,
3671 void **seg, int *size)
3672{
3673 struct mlx5_ib_mr *mr = to_mmr(wr->mr);
3674 struct mlx5_ib_pd *pd = to_mpd(qp->ibqp.pd);
3675
3676 if (unlikely(wr->wr.send_flags & IB_SEND_INLINE)) {
3677 mlx5_ib_warn(to_mdev(qp->ibqp.device),
3678 "Invalid IB_SEND_INLINE send flag\n");
3679 return -EINVAL;
3680 }
3681
3682 set_reg_umr_seg(*seg, mr);
3683 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
3684 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
3685 if (unlikely((*seg == qp->sq.qend)))
3686 *seg = mlx5_get_send_wqe(qp, 0);
3687
3688 set_reg_mkey_seg(*seg, mr, wr->key, wr->access);
3689 *seg += sizeof(struct mlx5_mkey_seg);
3690 *size += sizeof(struct mlx5_mkey_seg) / 16;
3691 if (unlikely((*seg == qp->sq.qend)))
3692 *seg = mlx5_get_send_wqe(qp, 0);
3693
3694 set_reg_data_seg(*seg, mr, pd);
3695 *seg += sizeof(struct mlx5_wqe_data_seg);
3696 *size += (sizeof(struct mlx5_wqe_data_seg) / 16);
3697
3698 return 0;
3699}
3700
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003701static void set_linv_wr(struct mlx5_ib_qp *qp, void **seg, int *size)
Eli Cohene126ba92013-07-07 17:25:49 +03003702{
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003703 set_linv_umr_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03003704 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
3705 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
3706 if (unlikely((*seg == qp->sq.qend)))
3707 *seg = mlx5_get_send_wqe(qp, 0);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003708 set_linv_mkey_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03003709 *seg += sizeof(struct mlx5_mkey_seg);
3710 *size += sizeof(struct mlx5_mkey_seg) / 16;
3711 if (unlikely((*seg == qp->sq.qend)))
3712 *seg = mlx5_get_send_wqe(qp, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03003713}
3714
3715static void dump_wqe(struct mlx5_ib_qp *qp, int idx, int size_16)
3716{
3717 __be32 *p = NULL;
3718 int tidx = idx;
3719 int i, j;
3720
3721 pr_debug("dump wqe at %p\n", mlx5_get_send_wqe(qp, tidx));
3722 for (i = 0, j = 0; i < size_16 * 4; i += 4, j += 4) {
3723 if ((i & 0xf) == 0) {
3724 void *buf = mlx5_get_send_wqe(qp, tidx);
3725 tidx = (tidx + 1) & (qp->sq.wqe_cnt - 1);
3726 p = buf;
3727 j = 0;
3728 }
3729 pr_debug("%08x %08x %08x %08x\n", be32_to_cpu(p[j]),
3730 be32_to_cpu(p[j + 1]), be32_to_cpu(p[j + 2]),
3731 be32_to_cpu(p[j + 3]));
3732 }
3733}
3734
Eli Cohene126ba92013-07-07 17:25:49 +03003735static u8 get_fence(u8 fence, struct ib_send_wr *wr)
3736{
3737 if (unlikely(wr->opcode == IB_WR_LOCAL_INV &&
3738 wr->send_flags & IB_SEND_FENCE))
3739 return MLX5_FENCE_MODE_STRONG_ORDERING;
3740
3741 if (unlikely(fence)) {
3742 if (wr->send_flags & IB_SEND_FENCE)
3743 return MLX5_FENCE_MODE_SMALL_AND_FENCE;
3744 else
3745 return fence;
Eli Cohenc9b25492016-06-22 17:27:26 +03003746 } else if (unlikely(wr->send_flags & IB_SEND_FENCE)) {
3747 return MLX5_FENCE_MODE_FENCE;
Eli Cohene126ba92013-07-07 17:25:49 +03003748 }
Eli Cohenc9b25492016-06-22 17:27:26 +03003749
3750 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003751}
3752
Sagi Grimberg6e5eada2014-02-23 14:19:08 +02003753static int begin_wqe(struct mlx5_ib_qp *qp, void **seg,
3754 struct mlx5_wqe_ctrl_seg **ctrl,
Eli Cohen6a4f1392014-12-02 12:26:18 +02003755 struct ib_send_wr *wr, unsigned *idx,
Sagi Grimberg6e5eada2014-02-23 14:19:08 +02003756 int *size, int nreq)
3757{
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03003758 if (unlikely(mlx5_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)))
3759 return -ENOMEM;
Sagi Grimberg6e5eada2014-02-23 14:19:08 +02003760
3761 *idx = qp->sq.cur_post & (qp->sq.wqe_cnt - 1);
3762 *seg = mlx5_get_send_wqe(qp, *idx);
3763 *ctrl = *seg;
3764 *(uint32_t *)(*seg + 8) = 0;
3765 (*ctrl)->imm = send_ieth(wr);
3766 (*ctrl)->fm_ce_se = qp->sq_signal_bits |
3767 (wr->send_flags & IB_SEND_SIGNALED ?
3768 MLX5_WQE_CTRL_CQ_UPDATE : 0) |
3769 (wr->send_flags & IB_SEND_SOLICITED ?
3770 MLX5_WQE_CTRL_SOLICITED : 0);
3771
3772 *seg += sizeof(**ctrl);
3773 *size = sizeof(**ctrl) / 16;
3774
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03003775 return 0;
Sagi Grimberg6e5eada2014-02-23 14:19:08 +02003776}
3777
3778static void finish_wqe(struct mlx5_ib_qp *qp,
3779 struct mlx5_wqe_ctrl_seg *ctrl,
3780 u8 size, unsigned idx, u64 wr_id,
3781 int nreq, u8 fence, u8 next_fence,
3782 u32 mlx5_opcode)
3783{
3784 u8 opmod = 0;
3785
3786 ctrl->opmod_idx_opcode = cpu_to_be32(((u32)(qp->sq.cur_post) << 8) |
3787 mlx5_opcode | ((u32)opmod << 24));
majd@mellanox.com19098df2016-01-14 19:13:03 +02003788 ctrl->qpn_ds = cpu_to_be32(size | (qp->trans_qp.base.mqp.qpn << 8));
Sagi Grimberg6e5eada2014-02-23 14:19:08 +02003789 ctrl->fm_ce_se |= fence;
3790 qp->fm_cache = next_fence;
3791 if (unlikely(qp->wq_sig))
3792 ctrl->signature = wq_sig(ctrl);
3793
3794 qp->sq.wrid[idx] = wr_id;
3795 qp->sq.w_list[idx].opcode = mlx5_opcode;
3796 qp->sq.wqe_head[idx] = qp->sq.head + nreq;
3797 qp->sq.cur_post += DIV_ROUND_UP(size * 16, MLX5_SEND_WQE_BB);
3798 qp->sq.w_list[idx].next = qp->sq.cur_post;
3799}
3800
3801
Eli Cohene126ba92013-07-07 17:25:49 +03003802int mlx5_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
3803 struct ib_send_wr **bad_wr)
3804{
3805 struct mlx5_wqe_ctrl_seg *ctrl = NULL; /* compiler warning */
3806 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003807 struct mlx5_core_dev *mdev = dev->mdev;
Haggai Erand16e91d2016-02-29 15:45:05 +02003808 struct mlx5_ib_qp *qp;
Sagi Grimberge6631812014-02-23 14:19:11 +02003809 struct mlx5_ib_mr *mr;
Eli Cohene126ba92013-07-07 17:25:49 +03003810 struct mlx5_wqe_data_seg *dpseg;
3811 struct mlx5_wqe_xrc_seg *xrc;
Haggai Erand16e91d2016-02-29 15:45:05 +02003812 struct mlx5_bf *bf;
Eli Cohene126ba92013-07-07 17:25:49 +03003813 int uninitialized_var(size);
Haggai Erand16e91d2016-02-29 15:45:05 +02003814 void *qend;
Eli Cohene126ba92013-07-07 17:25:49 +03003815 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03003816 unsigned idx;
3817 int err = 0;
3818 int inl = 0;
3819 int num_sge;
3820 void *seg;
3821 int nreq;
3822 int i;
3823 u8 next_fence = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003824 u8 fence;
3825
Haggai Erand16e91d2016-02-29 15:45:05 +02003826 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
3827 return mlx5_ib_gsi_post_send(ibqp, wr, bad_wr);
3828
3829 qp = to_mqp(ibqp);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003830 bf = &qp->bf;
Haggai Erand16e91d2016-02-29 15:45:05 +02003831 qend = qp->sq.qend;
3832
Eli Cohene126ba92013-07-07 17:25:49 +03003833 spin_lock_irqsave(&qp->sq.lock, flags);
3834
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003835 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
3836 err = -EIO;
3837 *bad_wr = wr;
3838 nreq = 0;
3839 goto out;
3840 }
3841
Eli Cohene126ba92013-07-07 17:25:49 +03003842 for (nreq = 0; wr; nreq++, wr = wr->next) {
Fabian Fredericka8f731e2014-08-12 19:20:08 -04003843 if (unlikely(wr->opcode >= ARRAY_SIZE(mlx5_ib_opcode))) {
Eli Cohene126ba92013-07-07 17:25:49 +03003844 mlx5_ib_warn(dev, "\n");
3845 err = -EINVAL;
3846 *bad_wr = wr;
3847 goto out;
3848 }
3849
Eli Cohene126ba92013-07-07 17:25:49 +03003850 fence = qp->fm_cache;
3851 num_sge = wr->num_sge;
3852 if (unlikely(num_sge > qp->sq.max_gs)) {
3853 mlx5_ib_warn(dev, "\n");
Chuck Lever24be4092016-08-28 10:58:34 +03003854 err = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03003855 *bad_wr = wr;
3856 goto out;
3857 }
3858
Sagi Grimberg6e5eada2014-02-23 14:19:08 +02003859 err = begin_wqe(qp, &seg, &ctrl, wr, &idx, &size, nreq);
3860 if (err) {
3861 mlx5_ib_warn(dev, "\n");
3862 err = -ENOMEM;
3863 *bad_wr = wr;
3864 goto out;
3865 }
Eli Cohene126ba92013-07-07 17:25:49 +03003866
3867 switch (ibqp->qp_type) {
3868 case IB_QPT_XRC_INI:
3869 xrc = seg;
Eli Cohene126ba92013-07-07 17:25:49 +03003870 seg += sizeof(*xrc);
3871 size += sizeof(*xrc) / 16;
3872 /* fall through */
3873 case IB_QPT_RC:
3874 switch (wr->opcode) {
3875 case IB_WR_RDMA_READ:
3876 case IB_WR_RDMA_WRITE:
3877 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003878 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
3879 rdma_wr(wr)->rkey);
Jack Morgensteinf241e742014-07-28 23:30:23 +03003880 seg += sizeof(struct mlx5_wqe_raddr_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03003881 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
3882 break;
3883
3884 case IB_WR_ATOMIC_CMP_AND_SWP:
3885 case IB_WR_ATOMIC_FETCH_AND_ADD:
Eli Cohene126ba92013-07-07 17:25:49 +03003886 case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
Eli Cohen81bea282013-09-11 16:35:30 +03003887 mlx5_ib_warn(dev, "Atomic operations are not supported yet\n");
3888 err = -ENOSYS;
3889 *bad_wr = wr;
3890 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03003891
3892 case IB_WR_LOCAL_INV:
3893 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
3894 qp->sq.wr_data[idx] = IB_WR_LOCAL_INV;
3895 ctrl->imm = cpu_to_be32(wr->ex.invalidate_rkey);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003896 set_linv_wr(qp, &seg, &size);
Eli Cohene126ba92013-07-07 17:25:49 +03003897 num_sge = 0;
3898 break;
3899
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003900 case IB_WR_REG_MR:
3901 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
3902 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
3924 finish_wqe(qp, ctrl, size, idx, wr->wr_id,
3925 nreq, get_fence(fence, wr),
3926 next_fence, MLX5_OPCODE_UMR);
3927 /*
3928 * SET_PSV WQEs are not signaled and solicited
3929 * on error
3930 */
3931 wr->send_flags &= ~IB_SEND_SIGNALED;
3932 wr->send_flags |= IB_SEND_SOLICITED;
3933 err = begin_wqe(qp, &seg, &ctrl, wr,
3934 &idx, &size, nreq);
3935 if (err) {
3936 mlx5_ib_warn(dev, "\n");
3937 err = -ENOMEM;
3938 *bad_wr = wr;
3939 goto out;
3940 }
3941
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003942 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->mem,
Sagi Grimberge6631812014-02-23 14:19:11 +02003943 mr->sig->psv_memory.psv_idx, &seg,
3944 &size);
3945 if (err) {
3946 mlx5_ib_warn(dev, "\n");
3947 *bad_wr = wr;
3948 goto out;
3949 }
3950
3951 finish_wqe(qp, ctrl, size, idx, wr->wr_id,
3952 nreq, get_fence(fence, wr),
3953 next_fence, MLX5_OPCODE_SET_PSV);
3954 err = begin_wqe(qp, &seg, &ctrl, wr,
3955 &idx, &size, nreq);
3956 if (err) {
3957 mlx5_ib_warn(dev, "\n");
3958 err = -ENOMEM;
3959 *bad_wr = wr;
3960 goto out;
3961 }
3962
3963 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003964 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->wire,
Sagi Grimberge6631812014-02-23 14:19:11 +02003965 mr->sig->psv_wire.psv_idx, &seg,
3966 &size);
3967 if (err) {
3968 mlx5_ib_warn(dev, "\n");
3969 *bad_wr = wr;
3970 goto out;
3971 }
3972
3973 finish_wqe(qp, ctrl, size, idx, wr->wr_id,
3974 nreq, get_fence(fence, wr),
3975 next_fence, MLX5_OPCODE_SET_PSV);
3976 num_sge = 0;
3977 goto skip_psv;
3978
Eli Cohene126ba92013-07-07 17:25:49 +03003979 default:
3980 break;
3981 }
3982 break;
3983
3984 case IB_QPT_UC:
3985 switch (wr->opcode) {
3986 case IB_WR_RDMA_WRITE:
3987 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003988 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
3989 rdma_wr(wr)->rkey);
Eli Cohene126ba92013-07-07 17:25:49 +03003990 seg += sizeof(struct mlx5_wqe_raddr_seg);
3991 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
3992 break;
3993
3994 default:
3995 break;
3996 }
3997 break;
3998
Eli Cohene126ba92013-07-07 17:25:49 +03003999 case IB_QPT_SMI:
Maor Gottlieb1e0e50b2017-01-18 14:10:34 +02004000 if (unlikely(!mdev->port_caps[qp->port - 1].has_smi)) {
4001 mlx5_ib_warn(dev, "Send SMP MADs is not allowed\n");
4002 err = -EPERM;
4003 *bad_wr = wr;
4004 goto out;
4005 }
Haggai Erand16e91d2016-02-29 15:45:05 +02004006 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03004007 set_datagram_seg(seg, wr);
Jack Morgensteinf241e742014-07-28 23:30:23 +03004008 seg += sizeof(struct mlx5_wqe_datagram_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004009 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
4010 if (unlikely((seg == qend)))
4011 seg = mlx5_get_send_wqe(qp, 0);
4012 break;
Erez Shitritf0313962016-02-21 16:27:17 +02004013 case IB_QPT_UD:
4014 set_datagram_seg(seg, wr);
4015 seg += sizeof(struct mlx5_wqe_datagram_seg);
4016 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Eli Cohene126ba92013-07-07 17:25:49 +03004017
Erez Shitritf0313962016-02-21 16:27:17 +02004018 if (unlikely((seg == qend)))
4019 seg = mlx5_get_send_wqe(qp, 0);
4020
4021 /* handle qp that supports ud offload */
4022 if (qp->flags & IB_QP_CREATE_IPOIB_UD_LSO) {
4023 struct mlx5_wqe_eth_pad *pad;
4024
4025 pad = seg;
4026 memset(pad, 0, sizeof(struct mlx5_wqe_eth_pad));
4027 seg += sizeof(struct mlx5_wqe_eth_pad);
4028 size += sizeof(struct mlx5_wqe_eth_pad) / 16;
4029
4030 seg = set_eth_seg(seg, wr, qend, qp, &size);
4031
4032 if (unlikely((seg == qend)))
4033 seg = mlx5_get_send_wqe(qp, 0);
4034 }
4035 break;
Eli Cohene126ba92013-07-07 17:25:49 +03004036 case MLX5_IB_QPT_REG_UMR:
4037 if (wr->opcode != MLX5_IB_WR_UMR) {
4038 err = -EINVAL;
4039 mlx5_ib_warn(dev, "bad opcode\n");
4040 goto out;
4041 }
4042 qp->sq.wr_data[idx] = MLX5_IB_WR_UMR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004043 ctrl->imm = cpu_to_be32(umr_wr(wr)->mkey);
Maor Gottlieb578e7262016-10-27 16:36:37 +03004044 set_reg_umr_segment(seg, wr, !!(MLX5_CAP_GEN(mdev, atomic)));
Eli Cohene126ba92013-07-07 17:25:49 +03004045 seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4046 size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4047 if (unlikely((seg == qend)))
4048 seg = mlx5_get_send_wqe(qp, 0);
4049 set_reg_mkey_segment(seg, wr);
4050 seg += sizeof(struct mlx5_mkey_seg);
4051 size += sizeof(struct mlx5_mkey_seg) / 16;
4052 if (unlikely((seg == qend)))
4053 seg = mlx5_get_send_wqe(qp, 0);
4054 break;
4055
4056 default:
4057 break;
4058 }
4059
4060 if (wr->send_flags & IB_SEND_INLINE && num_sge) {
4061 int uninitialized_var(sz);
4062
4063 err = set_data_inl_seg(qp, wr, seg, &sz);
4064 if (unlikely(err)) {
4065 mlx5_ib_warn(dev, "\n");
4066 *bad_wr = wr;
4067 goto out;
4068 }
4069 inl = 1;
4070 size += sz;
4071 } else {
4072 dpseg = seg;
4073 for (i = 0; i < num_sge; i++) {
4074 if (unlikely(dpseg == qend)) {
4075 seg = mlx5_get_send_wqe(qp, 0);
4076 dpseg = seg;
4077 }
4078 if (likely(wr->sg_list[i].length)) {
4079 set_data_ptr_seg(dpseg, wr->sg_list + i);
4080 size += sizeof(struct mlx5_wqe_data_seg) / 16;
4081 dpseg++;
4082 }
4083 }
4084 }
4085
Sagi Grimberg6e5eada2014-02-23 14:19:08 +02004086 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
4087 get_fence(fence, wr), next_fence,
4088 mlx5_ib_opcode[wr->opcode]);
Sagi Grimberge6631812014-02-23 14:19:11 +02004089skip_psv:
Eli Cohene126ba92013-07-07 17:25:49 +03004090 if (0)
4091 dump_wqe(qp, idx, size);
4092 }
4093
4094out:
4095 if (likely(nreq)) {
4096 qp->sq.head += nreq;
4097
4098 /* Make sure that descriptors are written before
4099 * updating doorbell record and ringing the doorbell
4100 */
4101 wmb();
4102
4103 qp->db.db[MLX5_SND_DBR] = cpu_to_be32(qp->sq.cur_post);
4104
Eli Cohenada388f2014-01-14 17:45:16 +02004105 /* Make sure doorbell record is visible to the HCA before
4106 * we hit doorbell */
4107 wmb();
4108
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004109 /* currently we support only regular doorbells */
4110 mlx5_write64((__be32 *)ctrl, bf->bfreg->map + bf->offset, NULL);
4111 /* Make sure doorbells don't leak out of SQ spinlock
4112 * and reach the HCA out of order.
4113 */
4114 mmiowb();
Eli Cohene126ba92013-07-07 17:25:49 +03004115 bf->offset ^= bf->buf_size;
Eli Cohene126ba92013-07-07 17:25:49 +03004116 }
4117
4118 spin_unlock_irqrestore(&qp->sq.lock, flags);
4119
4120 return err;
4121}
4122
4123static void set_sig_seg(struct mlx5_rwqe_sig *sig, int size)
4124{
4125 sig->signature = calc_sig(sig, size);
4126}
4127
4128int mlx5_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
4129 struct ib_recv_wr **bad_wr)
4130{
4131 struct mlx5_ib_qp *qp = to_mqp(ibqp);
4132 struct mlx5_wqe_data_seg *scat;
4133 struct mlx5_rwqe_sig *sig;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004134 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4135 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004136 unsigned long flags;
4137 int err = 0;
4138 int nreq;
4139 int ind;
4140 int i;
4141
Haggai Erand16e91d2016-02-29 15:45:05 +02004142 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4143 return mlx5_ib_gsi_post_recv(ibqp, wr, bad_wr);
4144
Eli Cohene126ba92013-07-07 17:25:49 +03004145 spin_lock_irqsave(&qp->rq.lock, flags);
4146
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004147 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
4148 err = -EIO;
4149 *bad_wr = wr;
4150 nreq = 0;
4151 goto out;
4152 }
4153
Eli Cohene126ba92013-07-07 17:25:49 +03004154 ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
4155
4156 for (nreq = 0; wr; nreq++, wr = wr->next) {
4157 if (mlx5_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
4158 err = -ENOMEM;
4159 *bad_wr = wr;
4160 goto out;
4161 }
4162
4163 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
4164 err = -EINVAL;
4165 *bad_wr = wr;
4166 goto out;
4167 }
4168
4169 scat = get_recv_wqe(qp, ind);
4170 if (qp->wq_sig)
4171 scat++;
4172
4173 for (i = 0; i < wr->num_sge; i++)
4174 set_data_ptr_seg(scat + i, wr->sg_list + i);
4175
4176 if (i < qp->rq.max_gs) {
4177 scat[i].byte_count = 0;
4178 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
4179 scat[i].addr = 0;
4180 }
4181
4182 if (qp->wq_sig) {
4183 sig = (struct mlx5_rwqe_sig *)scat;
4184 set_sig_seg(sig, (qp->rq.max_gs + 1) << 2);
4185 }
4186
4187 qp->rq.wrid[ind] = wr->wr_id;
4188
4189 ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
4190 }
4191
4192out:
4193 if (likely(nreq)) {
4194 qp->rq.head += nreq;
4195
4196 /* Make sure that descriptors are written before
4197 * doorbell record.
4198 */
4199 wmb();
4200
4201 *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
4202 }
4203
4204 spin_unlock_irqrestore(&qp->rq.lock, flags);
4205
4206 return err;
4207}
4208
4209static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
4210{
4211 switch (mlx5_state) {
4212 case MLX5_QP_STATE_RST: return IB_QPS_RESET;
4213 case MLX5_QP_STATE_INIT: return IB_QPS_INIT;
4214 case MLX5_QP_STATE_RTR: return IB_QPS_RTR;
4215 case MLX5_QP_STATE_RTS: return IB_QPS_RTS;
4216 case MLX5_QP_STATE_SQ_DRAINING:
4217 case MLX5_QP_STATE_SQD: return IB_QPS_SQD;
4218 case MLX5_QP_STATE_SQER: return IB_QPS_SQE;
4219 case MLX5_QP_STATE_ERR: return IB_QPS_ERR;
4220 default: return -1;
4221 }
4222}
4223
4224static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
4225{
4226 switch (mlx5_mig_state) {
4227 case MLX5_QP_PM_ARMED: return IB_MIG_ARMED;
4228 case MLX5_QP_PM_REARM: return IB_MIG_REARM;
4229 case MLX5_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
4230 default: return -1;
4231 }
4232}
4233
4234static int to_ib_qp_access_flags(int mlx5_flags)
4235{
4236 int ib_flags = 0;
4237
4238 if (mlx5_flags & MLX5_QP_BIT_RRE)
4239 ib_flags |= IB_ACCESS_REMOTE_READ;
4240 if (mlx5_flags & MLX5_QP_BIT_RWE)
4241 ib_flags |= IB_ACCESS_REMOTE_WRITE;
4242 if (mlx5_flags & MLX5_QP_BIT_RAE)
4243 ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
4244
4245 return ib_flags;
4246}
4247
4248static void to_ib_ah_attr(struct mlx5_ib_dev *ibdev, struct ib_ah_attr *ib_ah_attr,
4249 struct mlx5_qp_path *path)
4250{
Jack Morgenstein9603b612014-07-28 23:30:22 +03004251 struct mlx5_core_dev *dev = ibdev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004252
4253 memset(ib_ah_attr, 0, sizeof(*ib_ah_attr));
4254 ib_ah_attr->port_num = path->port;
4255
Eli Cohenc7a08ac2014-10-02 12:19:42 +03004256 if (ib_ah_attr->port_num == 0 ||
Saeed Mahameed938fe832015-05-28 22:28:41 +03004257 ib_ah_attr->port_num > MLX5_CAP_GEN(dev, num_ports))
Eli Cohene126ba92013-07-07 17:25:49 +03004258 return;
4259
Achiad Shochat2811ba52015-12-23 18:47:24 +02004260 ib_ah_attr->sl = path->dci_cfi_prio_sl & 0xf;
Eli Cohene126ba92013-07-07 17:25:49 +03004261
4262 ib_ah_attr->dlid = be16_to_cpu(path->rlid);
4263 ib_ah_attr->src_path_bits = path->grh_mlid & 0x7f;
4264 ib_ah_attr->static_rate = path->static_rate ? path->static_rate - 5 : 0;
4265 ib_ah_attr->ah_flags = (path->grh_mlid & (1 << 7)) ? IB_AH_GRH : 0;
4266 if (ib_ah_attr->ah_flags) {
4267 ib_ah_attr->grh.sgid_index = path->mgid_index;
4268 ib_ah_attr->grh.hop_limit = path->hop_limit;
4269 ib_ah_attr->grh.traffic_class =
4270 (be32_to_cpu(path->tclass_flowlabel) >> 20) & 0xff;
4271 ib_ah_attr->grh.flow_label =
4272 be32_to_cpu(path->tclass_flowlabel) & 0xfffff;
4273 memcpy(ib_ah_attr->grh.dgid.raw,
4274 path->rgid, sizeof(ib_ah_attr->grh.dgid.raw));
4275 }
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);
4288 out = mlx5_vzalloc(inlen);
4289 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);
4315 out = mlx5_vzalloc(inlen);
4316 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) {
4439 to_ib_ah_attr(dev, &qp_attr->ah_attr, &context->pri_path);
4440 to_ib_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);
Eli Cohene126ba92013-07-07 17:25:49 +03004443 qp_attr->alt_port_num = qp_attr->alt_ah_attr.port_num;
4444 }
4445
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03004446 qp_attr->pkey_index = be16_to_cpu(context->pri_path.pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03004447 qp_attr->port_num = context->pri_path.port;
4448
4449 /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
4450 qp_attr->sq_draining = mlx5_state == MLX5_QP_STATE_SQ_DRAINING;
4451
4452 qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context->params1) >> 21) & 0x7);
4453
4454 qp_attr->max_dest_rd_atomic =
4455 1 << ((be32_to_cpu(context->params2) >> 21) & 0x7);
4456 qp_attr->min_rnr_timer =
4457 (be32_to_cpu(context->rnr_nextrecvpsn) >> 24) & 0x1f;
4458 qp_attr->timeout = context->pri_path.ackto_lt >> 3;
4459 qp_attr->retry_cnt = (be32_to_cpu(context->params1) >> 16) & 0x7;
4460 qp_attr->rnr_retry = (be32_to_cpu(context->params1) >> 13) & 0x7;
4461 qp_attr->alt_timeout = context->alt_path.ackto_lt >> 3;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004462
4463out:
4464 kfree(outb);
4465 return err;
4466}
4467
4468int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
4469 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
4470{
4471 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4472 struct mlx5_ib_qp *qp = to_mqp(ibqp);
4473 int err = 0;
4474 u8 raw_packet_qp_state;
4475
Yishai Hadas28d61372016-05-23 15:20:56 +03004476 if (ibqp->rwq_ind_tbl)
4477 return -ENOSYS;
4478
Haggai Erand16e91d2016-02-29 15:45:05 +02004479 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4480 return mlx5_ib_gsi_query_qp(ibqp, qp_attr, qp_attr_mask,
4481 qp_init_attr);
4482
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004483 mutex_lock(&qp->mutex);
4484
4485 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET) {
4486 err = query_raw_packet_qp_state(dev, qp, &raw_packet_qp_state);
4487 if (err)
4488 goto out;
4489 qp->state = raw_packet_qp_state;
4490 qp_attr->port_num = 1;
4491 } else {
4492 err = query_qp_attr(dev, qp, qp_attr);
4493 if (err)
4494 goto out;
4495 }
4496
4497 qp_attr->qp_state = qp->state;
Eli Cohene126ba92013-07-07 17:25:49 +03004498 qp_attr->cur_qp_state = qp_attr->qp_state;
4499 qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
4500 qp_attr->cap.max_recv_sge = qp->rq.max_gs;
4501
4502 if (!ibqp->uobject) {
Noa Osherovich0540d812016-06-04 15:15:32 +03004503 qp_attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +03004504 qp_attr->cap.max_send_sge = qp->sq.max_gs;
Noa Osherovich0540d812016-06-04 15:15:32 +03004505 qp_init_attr->qp_context = ibqp->qp_context;
Eli Cohene126ba92013-07-07 17:25:49 +03004506 } else {
4507 qp_attr->cap.max_send_wr = 0;
4508 qp_attr->cap.max_send_sge = 0;
4509 }
4510
Noa Osherovich0540d812016-06-04 15:15:32 +03004511 qp_init_attr->qp_type = ibqp->qp_type;
4512 qp_init_attr->recv_cq = ibqp->recv_cq;
4513 qp_init_attr->send_cq = ibqp->send_cq;
4514 qp_init_attr->srq = ibqp->srq;
4515 qp_attr->cap.max_inline_data = qp->max_inline_data;
Eli Cohene126ba92013-07-07 17:25:49 +03004516
4517 qp_init_attr->cap = qp_attr->cap;
4518
4519 qp_init_attr->create_flags = 0;
4520 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
4521 qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
4522
Leon Romanovsky051f2632015-12-20 12:16:11 +02004523 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
4524 qp_init_attr->create_flags |= IB_QP_CREATE_CROSS_CHANNEL;
4525 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
4526 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_SEND;
4527 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
4528 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_RECV;
Haggai Eranb11a4f92016-02-29 15:45:03 +02004529 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
4530 qp_init_attr->create_flags |= mlx5_ib_create_qp_sqpn_qp1();
Leon Romanovsky051f2632015-12-20 12:16:11 +02004531
Eli Cohene126ba92013-07-07 17:25:49 +03004532 qp_init_attr->sq_sig_type = qp->sq_signal_bits & MLX5_WQE_CTRL_CQ_UPDATE ?
4533 IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
4534
Eli Cohene126ba92013-07-07 17:25:49 +03004535out:
4536 mutex_unlock(&qp->mutex);
4537 return err;
4538}
4539
4540struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
4541 struct ib_ucontext *context,
4542 struct ib_udata *udata)
4543{
4544 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4545 struct mlx5_ib_xrcd *xrcd;
4546 int err;
4547
Saeed Mahameed938fe832015-05-28 22:28:41 +03004548 if (!MLX5_CAP_GEN(dev->mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +03004549 return ERR_PTR(-ENOSYS);
4550
4551 xrcd = kmalloc(sizeof(*xrcd), GFP_KERNEL);
4552 if (!xrcd)
4553 return ERR_PTR(-ENOMEM);
4554
Jack Morgenstein9603b612014-07-28 23:30:22 +03004555 err = mlx5_core_xrcd_alloc(dev->mdev, &xrcd->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03004556 if (err) {
4557 kfree(xrcd);
4558 return ERR_PTR(-ENOMEM);
4559 }
4560
4561 return &xrcd->ibxrcd;
4562}
4563
4564int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
4565{
4566 struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
4567 u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
4568 int err;
4569
Jack Morgenstein9603b612014-07-28 23:30:22 +03004570 err = mlx5_core_xrcd_dealloc(dev->mdev, xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03004571 if (err) {
4572 mlx5_ib_warn(dev, "failed to dealloc xrcdn 0x%x\n", xrcdn);
4573 return err;
4574 }
4575
4576 kfree(xrcd);
4577
4578 return 0;
4579}
Yishai Hadas79b20a62016-05-23 15:20:50 +03004580
Yishai Hadas350d0e42016-08-28 14:58:18 +03004581static void mlx5_ib_wq_event(struct mlx5_core_qp *core_qp, int type)
4582{
4583 struct mlx5_ib_rwq *rwq = to_mibrwq(core_qp);
4584 struct mlx5_ib_dev *dev = to_mdev(rwq->ibwq.device);
4585 struct ib_event event;
4586
4587 if (rwq->ibwq.event_handler) {
4588 event.device = rwq->ibwq.device;
4589 event.element.wq = &rwq->ibwq;
4590 switch (type) {
4591 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
4592 event.event = IB_EVENT_WQ_FATAL;
4593 break;
4594 default:
4595 mlx5_ib_warn(dev, "Unexpected event type %d on WQ %06x\n", type, core_qp->qpn);
4596 return;
4597 }
4598
4599 rwq->ibwq.event_handler(&event, rwq->ibwq.wq_context);
4600 }
4601}
4602
Yishai Hadas79b20a62016-05-23 15:20:50 +03004603static int create_rq(struct mlx5_ib_rwq *rwq, struct ib_pd *pd,
4604 struct ib_wq_init_attr *init_attr)
4605{
4606 struct mlx5_ib_dev *dev;
4607 __be64 *rq_pas0;
4608 void *in;
4609 void *rqc;
4610 void *wq;
4611 int inlen;
4612 int err;
4613
4614 dev = to_mdev(pd->device);
4615
4616 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + sizeof(u64) * rwq->rq_num_pas;
4617 in = mlx5_vzalloc(inlen);
4618 if (!in)
4619 return -ENOMEM;
4620
4621 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
4622 MLX5_SET(rqc, rqc, mem_rq_type,
4623 MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
4624 MLX5_SET(rqc, rqc, user_index, rwq->user_index);
4625 MLX5_SET(rqc, rqc, cqn, to_mcq(init_attr->cq)->mcq.cqn);
4626 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
4627 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
4628 wq = MLX5_ADDR_OF(rqc, rqc, wq);
4629 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
4630 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
4631 MLX5_SET(wq, wq, log_wq_stride, rwq->log_rq_stride);
4632 MLX5_SET(wq, wq, log_wq_sz, rwq->log_rq_size);
4633 MLX5_SET(wq, wq, pd, to_mpd(pd)->pdn);
4634 MLX5_SET(wq, wq, page_offset, rwq->rq_page_offset);
4635 MLX5_SET(wq, wq, log_wq_pg_sz, rwq->log_page_size);
4636 MLX5_SET(wq, wq, wq_signature, rwq->wq_sig);
4637 MLX5_SET64(wq, wq, dbr_addr, rwq->db.dma);
Noa Osherovichb1f74a82017-01-18 15:40:02 +02004638 if (init_attr->create_flags & IB_WQ_FLAGS_CVLAN_STRIPPING) {
4639 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
4640 MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
4641 mlx5_ib_dbg(dev, "VLAN offloads are not supported\n");
4642 err = -EOPNOTSUPP;
4643 goto out;
4644 }
4645 } else {
4646 MLX5_SET(rqc, rqc, vsd, 1);
4647 }
Yishai Hadas79b20a62016-05-23 15:20:50 +03004648 rq_pas0 = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
4649 mlx5_ib_populate_pas(dev, rwq->umem, rwq->page_shift, rq_pas0, 0);
Yishai Hadas350d0e42016-08-28 14:58:18 +03004650 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rwq->core_qp);
Noa Osherovichb1f74a82017-01-18 15:40:02 +02004651out:
Yishai Hadas79b20a62016-05-23 15:20:50 +03004652 kvfree(in);
4653 return err;
4654}
4655
4656static int set_user_rq_size(struct mlx5_ib_dev *dev,
4657 struct ib_wq_init_attr *wq_init_attr,
4658 struct mlx5_ib_create_wq *ucmd,
4659 struct mlx5_ib_rwq *rwq)
4660{
4661 /* Sanity check RQ size before proceeding */
4662 if (wq_init_attr->max_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_wq_sz)))
4663 return -EINVAL;
4664
4665 if (!ucmd->rq_wqe_count)
4666 return -EINVAL;
4667
4668 rwq->wqe_count = ucmd->rq_wqe_count;
4669 rwq->wqe_shift = ucmd->rq_wqe_shift;
4670 rwq->buf_size = (rwq->wqe_count << rwq->wqe_shift);
4671 rwq->log_rq_stride = rwq->wqe_shift;
4672 rwq->log_rq_size = ilog2(rwq->wqe_count);
4673 return 0;
4674}
4675
4676static int prepare_user_rq(struct ib_pd *pd,
4677 struct ib_wq_init_attr *init_attr,
4678 struct ib_udata *udata,
4679 struct mlx5_ib_rwq *rwq)
4680{
4681 struct mlx5_ib_dev *dev = to_mdev(pd->device);
4682 struct mlx5_ib_create_wq ucmd = {};
4683 int err;
4684 size_t required_cmd_sz;
4685
4686 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
4687 if (udata->inlen < required_cmd_sz) {
4688 mlx5_ib_dbg(dev, "invalid inlen\n");
4689 return -EINVAL;
4690 }
4691
4692 if (udata->inlen > sizeof(ucmd) &&
4693 !ib_is_udata_cleared(udata, sizeof(ucmd),
4694 udata->inlen - sizeof(ucmd))) {
4695 mlx5_ib_dbg(dev, "inlen is not supported\n");
4696 return -EOPNOTSUPP;
4697 }
4698
4699 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
4700 mlx5_ib_dbg(dev, "copy failed\n");
4701 return -EFAULT;
4702 }
4703
4704 if (ucmd.comp_mask) {
4705 mlx5_ib_dbg(dev, "invalid comp mask\n");
4706 return -EOPNOTSUPP;
4707 }
4708
4709 if (ucmd.reserved) {
4710 mlx5_ib_dbg(dev, "invalid reserved\n");
4711 return -EOPNOTSUPP;
4712 }
4713
4714 err = set_user_rq_size(dev, init_attr, &ucmd, rwq);
4715 if (err) {
4716 mlx5_ib_dbg(dev, "err %d\n", err);
4717 return err;
4718 }
4719
4720 err = create_user_rq(dev, pd, rwq, &ucmd);
4721 if (err) {
4722 mlx5_ib_dbg(dev, "err %d\n", err);
4723 if (err)
4724 return err;
4725 }
4726
4727 rwq->user_index = ucmd.user_index;
4728 return 0;
4729}
4730
4731struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
4732 struct ib_wq_init_attr *init_attr,
4733 struct ib_udata *udata)
4734{
4735 struct mlx5_ib_dev *dev;
4736 struct mlx5_ib_rwq *rwq;
4737 struct mlx5_ib_create_wq_resp resp = {};
4738 size_t min_resp_len;
4739 int err;
4740
4741 if (!udata)
4742 return ERR_PTR(-ENOSYS);
4743
4744 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
4745 if (udata->outlen && udata->outlen < min_resp_len)
4746 return ERR_PTR(-EINVAL);
4747
4748 dev = to_mdev(pd->device);
4749 switch (init_attr->wq_type) {
4750 case IB_WQT_RQ:
4751 rwq = kzalloc(sizeof(*rwq), GFP_KERNEL);
4752 if (!rwq)
4753 return ERR_PTR(-ENOMEM);
4754 err = prepare_user_rq(pd, init_attr, udata, rwq);
4755 if (err)
4756 goto err;
4757 err = create_rq(rwq, pd, init_attr);
4758 if (err)
4759 goto err_user_rq;
4760 break;
4761 default:
4762 mlx5_ib_dbg(dev, "unsupported wq type %d\n",
4763 init_attr->wq_type);
4764 return ERR_PTR(-EINVAL);
4765 }
4766
Yishai Hadas350d0e42016-08-28 14:58:18 +03004767 rwq->ibwq.wq_num = rwq->core_qp.qpn;
Yishai Hadas79b20a62016-05-23 15:20:50 +03004768 rwq->ibwq.state = IB_WQS_RESET;
4769 if (udata->outlen) {
4770 resp.response_length = offsetof(typeof(resp), response_length) +
4771 sizeof(resp.response_length);
4772 err = ib_copy_to_udata(udata, &resp, resp.response_length);
4773 if (err)
4774 goto err_copy;
4775 }
4776
Yishai Hadas350d0e42016-08-28 14:58:18 +03004777 rwq->core_qp.event = mlx5_ib_wq_event;
4778 rwq->ibwq.event_handler = init_attr->event_handler;
Yishai Hadas79b20a62016-05-23 15:20:50 +03004779 return &rwq->ibwq;
4780
4781err_copy:
Yishai Hadas350d0e42016-08-28 14:58:18 +03004782 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004783err_user_rq:
4784 destroy_user_rq(pd, rwq);
4785err:
4786 kfree(rwq);
4787 return ERR_PTR(err);
4788}
4789
4790int mlx5_ib_destroy_wq(struct ib_wq *wq)
4791{
4792 struct mlx5_ib_dev *dev = to_mdev(wq->device);
4793 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
4794
Yishai Hadas350d0e42016-08-28 14:58:18 +03004795 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004796 destroy_user_rq(wq->pd, rwq);
4797 kfree(rwq);
4798
4799 return 0;
4800}
4801
Yishai Hadasc5f90922016-05-23 15:20:53 +03004802struct ib_rwq_ind_table *mlx5_ib_create_rwq_ind_table(struct ib_device *device,
4803 struct ib_rwq_ind_table_init_attr *init_attr,
4804 struct ib_udata *udata)
4805{
4806 struct mlx5_ib_dev *dev = to_mdev(device);
4807 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl;
4808 int sz = 1 << init_attr->log_ind_tbl_size;
4809 struct mlx5_ib_create_rwq_ind_tbl_resp resp = {};
4810 size_t min_resp_len;
4811 int inlen;
4812 int err;
4813 int i;
4814 u32 *in;
4815 void *rqtc;
4816
4817 if (udata->inlen > 0 &&
4818 !ib_is_udata_cleared(udata, 0,
4819 udata->inlen))
4820 return ERR_PTR(-EOPNOTSUPP);
4821
Maor Gottliebefd7f402016-10-27 16:36:40 +03004822 if (init_attr->log_ind_tbl_size >
4823 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size)) {
4824 mlx5_ib_dbg(dev, "log_ind_tbl_size = %d is bigger than supported = %d\n",
4825 init_attr->log_ind_tbl_size,
4826 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size));
4827 return ERR_PTR(-EINVAL);
4828 }
4829
Yishai Hadasc5f90922016-05-23 15:20:53 +03004830 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
4831 if (udata->outlen && udata->outlen < min_resp_len)
4832 return ERR_PTR(-EINVAL);
4833
4834 rwq_ind_tbl = kzalloc(sizeof(*rwq_ind_tbl), GFP_KERNEL);
4835 if (!rwq_ind_tbl)
4836 return ERR_PTR(-ENOMEM);
4837
4838 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
4839 in = mlx5_vzalloc(inlen);
4840 if (!in) {
4841 err = -ENOMEM;
4842 goto err;
4843 }
4844
4845 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
4846
4847 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
4848 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
4849
4850 for (i = 0; i < sz; i++)
4851 MLX5_SET(rqtc, rqtc, rq_num[i], init_attr->ind_tbl[i]->wq_num);
4852
4853 err = mlx5_core_create_rqt(dev->mdev, in, inlen, &rwq_ind_tbl->rqtn);
4854 kvfree(in);
4855
4856 if (err)
4857 goto err;
4858
4859 rwq_ind_tbl->ib_rwq_ind_tbl.ind_tbl_num = rwq_ind_tbl->rqtn;
4860 if (udata->outlen) {
4861 resp.response_length = offsetof(typeof(resp), response_length) +
4862 sizeof(resp.response_length);
4863 err = ib_copy_to_udata(udata, &resp, resp.response_length);
4864 if (err)
4865 goto err_copy;
4866 }
4867
4868 return &rwq_ind_tbl->ib_rwq_ind_tbl;
4869
4870err_copy:
4871 mlx5_core_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn);
4872err:
4873 kfree(rwq_ind_tbl);
4874 return ERR_PTR(err);
4875}
4876
4877int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
4878{
4879 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl = to_mrwq_ind_table(ib_rwq_ind_tbl);
4880 struct mlx5_ib_dev *dev = to_mdev(ib_rwq_ind_tbl->device);
4881
4882 mlx5_core_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn);
4883
4884 kfree(rwq_ind_tbl);
4885 return 0;
4886}
4887
Yishai Hadas79b20a62016-05-23 15:20:50 +03004888int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
4889 u32 wq_attr_mask, struct ib_udata *udata)
4890{
4891 struct mlx5_ib_dev *dev = to_mdev(wq->device);
4892 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
4893 struct mlx5_ib_modify_wq ucmd = {};
4894 size_t required_cmd_sz;
4895 int curr_wq_state;
4896 int wq_state;
4897 int inlen;
4898 int err;
4899 void *rqc;
4900 void *in;
4901
4902 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
4903 if (udata->inlen < required_cmd_sz)
4904 return -EINVAL;
4905
4906 if (udata->inlen > sizeof(ucmd) &&
4907 !ib_is_udata_cleared(udata, sizeof(ucmd),
4908 udata->inlen - sizeof(ucmd)))
4909 return -EOPNOTSUPP;
4910
4911 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
4912 return -EFAULT;
4913
4914 if (ucmd.comp_mask || ucmd.reserved)
4915 return -EOPNOTSUPP;
4916
4917 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
4918 in = mlx5_vzalloc(inlen);
4919 if (!in)
4920 return -ENOMEM;
4921
4922 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
4923
4924 curr_wq_state = (wq_attr_mask & IB_WQ_CUR_STATE) ?
4925 wq_attr->curr_wq_state : wq->state;
4926 wq_state = (wq_attr_mask & IB_WQ_STATE) ?
4927 wq_attr->wq_state : curr_wq_state;
4928 if (curr_wq_state == IB_WQS_ERR)
4929 curr_wq_state = MLX5_RQC_STATE_ERR;
4930 if (wq_state == IB_WQS_ERR)
4931 wq_state = MLX5_RQC_STATE_ERR;
4932 MLX5_SET(modify_rq_in, in, rq_state, curr_wq_state);
4933 MLX5_SET(rqc, rqc, state, wq_state);
4934
Noa Osherovichb1f74a82017-01-18 15:40:02 +02004935 if (wq_attr_mask & IB_WQ_FLAGS) {
4936 if (wq_attr->flags_mask & IB_WQ_FLAGS_CVLAN_STRIPPING) {
4937 if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
4938 MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
4939 mlx5_ib_dbg(dev, "VLAN offloads are not "
4940 "supported\n");
4941 err = -EOPNOTSUPP;
4942 goto out;
4943 }
4944 MLX5_SET64(modify_rq_in, in, modify_bitmask,
4945 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
4946 MLX5_SET(rqc, rqc, vsd,
4947 (wq_attr->flags & IB_WQ_FLAGS_CVLAN_STRIPPING) ? 0 : 1);
4948 }
4949 }
4950
Majd Dibbiny23a69642017-01-18 15:25:10 +02004951 if (curr_wq_state == IB_WQS_RESET && wq_state == IB_WQS_RDY) {
4952 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
4953 MLX5_SET64(modify_rq_in, in, modify_bitmask,
4954 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
4955 MLX5_SET(rqc, rqc, counter_set_id, dev->port->q_cnts.set_id);
4956 } else
4957 pr_info_once("%s: Receive WQ counters are not supported on current FW\n",
4958 dev->ib_dev.name);
4959 }
4960
Yishai Hadas350d0e42016-08-28 14:58:18 +03004961 err = mlx5_core_modify_rq(dev->mdev, rwq->core_qp.qpn, in, inlen);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004962 if (!err)
4963 rwq->ibwq.state = (wq_state == MLX5_RQC_STATE_ERR) ? IB_WQS_ERR : wq_state;
4964
Noa Osherovichb1f74a82017-01-18 15:40:02 +02004965out:
4966 kvfree(in);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004967 return err;
4968}