blob: fce1c6db393b121b2c836ff632f3fa973062cbfc [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 Cohen2f5ff262017-01-03 23:55:21 +0200483static int next_bfreg(int n)
Eli Cohenc1be5232014-01-14 17:45:12 +0200484{
485 n++;
486
487 while (((n % 4) & 2))
488 n++;
489
490 return n;
491}
492
Eli Cohen0b80c142017-01-03 23:55:22 +0200493enum {
494 /* this is the first blue flame register in the array of bfregs assigned
495 * to a processes. Since we do not use it for blue flame but rather
496 * regular 64 bit doorbells, we do not need a lock for maintaiing
497 * "odd/even" order
498 */
499 NUM_NON_BLUE_FLAME_BFREGS = 1,
500};
501
Eli Cohen2f5ff262017-01-03 23:55:21 +0200502static int num_med_bfreg(struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200503{
504 int n;
505
Eli Cohen2f5ff262017-01-03 23:55:21 +0200506 n = bfregi->num_uars * MLX5_NON_FP_BFREGS_PER_UAR -
Eli Cohen0b80c142017-01-03 23:55:22 +0200507 bfregi->num_low_latency_bfregs - NUM_NON_BLUE_FLAME_BFREGS;
Eli Cohenc1be5232014-01-14 17:45:12 +0200508
509 return n >= 0 ? n : 0;
510}
511
Eli Cohen2f5ff262017-01-03 23:55:21 +0200512static int max_bfregi(struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200513{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200514 return bfregi->num_uars * 4;
Eli Cohenc1be5232014-01-14 17:45:12 +0200515}
516
Eli Cohen2f5ff262017-01-03 23:55:21 +0200517static int first_hi_bfreg(struct mlx5_bfreg_info *bfregi)
Eli Cohenc1be5232014-01-14 17:45:12 +0200518{
519 int med;
Eli Cohenc1be5232014-01-14 17:45:12 +0200520
Eli Cohen2f5ff262017-01-03 23:55:21 +0200521 med = num_med_bfreg(bfregi);
Eli Cohen0b80c142017-01-03 23:55:22 +0200522 return next_bfreg(med);
Eli Cohenc1be5232014-01-14 17:45:12 +0200523}
524
Eli Cohen2f5ff262017-01-03 23:55:21 +0200525static int alloc_high_class_bfreg(struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300526{
Eli Cohene126ba92013-07-07 17:25:49 +0300527 int i;
528
Eli Cohen2f5ff262017-01-03 23:55:21 +0200529 for (i = first_hi_bfreg(bfregi); i < max_bfregi(bfregi); i = next_bfreg(i)) {
530 if (!test_bit(i, bfregi->bitmap)) {
531 set_bit(i, bfregi->bitmap);
532 bfregi->count[i]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300533 return i;
534 }
535 }
536
537 return -ENOMEM;
538}
539
Eli Cohen2f5ff262017-01-03 23:55:21 +0200540static int alloc_med_class_bfreg(struct mlx5_bfreg_info *bfregi)
Eli Cohene126ba92013-07-07 17:25:49 +0300541{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200542 int minidx = first_med_bfreg();
Eli Cohene126ba92013-07-07 17:25:49 +0300543 int i;
544
Eli Cohen2f5ff262017-01-03 23:55:21 +0200545 for (i = first_med_bfreg(); i < first_hi_bfreg(bfregi); i = next_bfreg(i)) {
546 if (bfregi->count[i] < bfregi->count[minidx])
Eli Cohene126ba92013-07-07 17:25:49 +0300547 minidx = i;
Eli Cohen0b80c142017-01-03 23:55:22 +0200548 if (!bfregi->count[minidx])
549 break;
Eli Cohene126ba92013-07-07 17:25:49 +0300550 }
551
Eli Cohen2f5ff262017-01-03 23:55:21 +0200552 bfregi->count[minidx]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300553 return minidx;
554}
555
Eli Cohen2f5ff262017-01-03 23:55:21 +0200556static int alloc_bfreg(struct mlx5_bfreg_info *bfregi,
557 enum mlx5_ib_latency_class lat)
Eli Cohene126ba92013-07-07 17:25:49 +0300558{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200559 int bfregn = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300560
Eli Cohen2f5ff262017-01-03 23:55:21 +0200561 mutex_lock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300562 switch (lat) {
563 case MLX5_IB_LATENCY_CLASS_LOW:
Eli Cohen0b80c142017-01-03 23:55:22 +0200564 BUILD_BUG_ON(NUM_NON_BLUE_FLAME_BFREGS != 1);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200565 bfregn = 0;
566 bfregi->count[bfregn]++;
Eli Cohene126ba92013-07-07 17:25:49 +0300567 break;
568
569 case MLX5_IB_LATENCY_CLASS_MEDIUM:
Eli Cohen2f5ff262017-01-03 23:55:21 +0200570 if (bfregi->ver < 2)
571 bfregn = -ENOMEM;
Eli Cohen78c0f982014-01-30 13:49:48 +0200572 else
Eli Cohen2f5ff262017-01-03 23:55:21 +0200573 bfregn = alloc_med_class_bfreg(bfregi);
Eli Cohene126ba92013-07-07 17:25:49 +0300574 break;
575
576 case MLX5_IB_LATENCY_CLASS_HIGH:
Eli Cohen2f5ff262017-01-03 23:55:21 +0200577 if (bfregi->ver < 2)
578 bfregn = -ENOMEM;
Eli Cohen78c0f982014-01-30 13:49:48 +0200579 else
Eli Cohen2f5ff262017-01-03 23:55:21 +0200580 bfregn = alloc_high_class_bfreg(bfregi);
Eli Cohene126ba92013-07-07 17:25:49 +0300581 break;
582
583 case MLX5_IB_LATENCY_CLASS_FAST_PATH:
Eli Cohen2f5ff262017-01-03 23:55:21 +0200584 bfregn = 2;
Eli Cohene126ba92013-07-07 17:25:49 +0300585 break;
586 }
Eli Cohen2f5ff262017-01-03 23:55:21 +0200587 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300588
Eli Cohen2f5ff262017-01-03 23:55:21 +0200589 return bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300590}
591
Eli Cohen2f5ff262017-01-03 23:55:21 +0200592static void free_med_class_bfreg(struct mlx5_bfreg_info *bfregi, int bfregn)
Eli Cohene126ba92013-07-07 17:25:49 +0300593{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200594 clear_bit(bfregn, bfregi->bitmap);
595 --bfregi->count[bfregn];
Eli Cohene126ba92013-07-07 17:25:49 +0300596}
597
Eli Cohen2f5ff262017-01-03 23:55:21 +0200598static void free_high_class_bfreg(struct mlx5_bfreg_info *bfregi, int bfregn)
Eli Cohene126ba92013-07-07 17:25:49 +0300599{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200600 clear_bit(bfregn, bfregi->bitmap);
601 --bfregi->count[bfregn];
Eli Cohene126ba92013-07-07 17:25:49 +0300602}
603
Eli Cohen2f5ff262017-01-03 23:55:21 +0200604static void free_bfreg(struct mlx5_bfreg_info *bfregi, int bfregn)
Eli Cohene126ba92013-07-07 17:25:49 +0300605{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200606 int nbfregs = bfregi->num_uars * MLX5_BFREGS_PER_UAR;
607 int high_bfreg = nbfregs - bfregi->num_low_latency_bfregs;
Eli Cohene126ba92013-07-07 17:25:49 +0300608
Eli Cohen2f5ff262017-01-03 23:55:21 +0200609 mutex_lock(&bfregi->lock);
610 if (bfregn == 0) {
611 --bfregi->count[bfregn];
Eli Cohene126ba92013-07-07 17:25:49 +0300612 goto out;
613 }
614
Eli Cohen2f5ff262017-01-03 23:55:21 +0200615 if (bfregn < high_bfreg) {
616 free_med_class_bfreg(bfregi, bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300617 goto out;
618 }
619
Eli Cohen2f5ff262017-01-03 23:55:21 +0200620 free_high_class_bfreg(bfregi, bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300621
622out:
Eli Cohen2f5ff262017-01-03 23:55:21 +0200623 mutex_unlock(&bfregi->lock);
Eli Cohene126ba92013-07-07 17:25:49 +0300624}
625
626static enum mlx5_qp_state to_mlx5_state(enum ib_qp_state state)
627{
628 switch (state) {
629 case IB_QPS_RESET: return MLX5_QP_STATE_RST;
630 case IB_QPS_INIT: return MLX5_QP_STATE_INIT;
631 case IB_QPS_RTR: return MLX5_QP_STATE_RTR;
632 case IB_QPS_RTS: return MLX5_QP_STATE_RTS;
633 case IB_QPS_SQD: return MLX5_QP_STATE_SQD;
634 case IB_QPS_SQE: return MLX5_QP_STATE_SQER;
635 case IB_QPS_ERR: return MLX5_QP_STATE_ERR;
636 default: return -1;
637 }
638}
639
640static int to_mlx5_st(enum ib_qp_type type)
641{
642 switch (type) {
643 case IB_QPT_RC: return MLX5_QP_ST_RC;
644 case IB_QPT_UC: return MLX5_QP_ST_UC;
645 case IB_QPT_UD: return MLX5_QP_ST_UD;
646 case MLX5_IB_QPT_REG_UMR: return MLX5_QP_ST_REG_UMR;
647 case IB_QPT_XRC_INI:
648 case IB_QPT_XRC_TGT: return MLX5_QP_ST_XRC;
649 case IB_QPT_SMI: return MLX5_QP_ST_QP0;
Haggai Erand16e91d2016-02-29 15:45:05 +0200650 case MLX5_IB_QPT_HW_GSI: return MLX5_QP_ST_QP1;
Eli Cohene126ba92013-07-07 17:25:49 +0300651 case IB_QPT_RAW_IPV6: return MLX5_QP_ST_RAW_IPV6;
Eli Cohene126ba92013-07-07 17:25:49 +0300652 case IB_QPT_RAW_PACKET:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200653 case IB_QPT_RAW_ETHERTYPE: return MLX5_QP_ST_RAW_ETHERTYPE;
Eli Cohene126ba92013-07-07 17:25:49 +0300654 case IB_QPT_MAX:
655 default: return -EINVAL;
656 }
657}
658
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300659static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq,
660 struct mlx5_ib_cq *recv_cq);
661static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq,
662 struct mlx5_ib_cq *recv_cq);
663
Eli Cohen2f5ff262017-01-03 23:55:21 +0200664static int bfregn_to_uar_index(struct mlx5_bfreg_info *bfregi, int bfregn)
Eli Cohene126ba92013-07-07 17:25:49 +0300665{
Eli Cohen2f5ff262017-01-03 23:55:21 +0200666 return bfregi->uars[bfregn / MLX5_BFREGS_PER_UAR].index;
Eli Cohene126ba92013-07-07 17:25:49 +0300667}
668
majd@mellanox.com19098df2016-01-14 19:13:03 +0200669static int mlx5_ib_umem_get(struct mlx5_ib_dev *dev,
670 struct ib_pd *pd,
671 unsigned long addr, size_t size,
672 struct ib_umem **umem,
673 int *npages, int *page_shift, int *ncont,
674 u32 *offset)
675{
676 int err;
677
678 *umem = ib_umem_get(pd->uobject->context, addr, size, 0, 0);
679 if (IS_ERR(*umem)) {
680 mlx5_ib_dbg(dev, "umem_get failed\n");
681 return PTR_ERR(*umem);
682 }
683
Majd Dibbiny762f8992016-10-27 16:36:47 +0300684 mlx5_ib_cont_pages(*umem, addr, 0, npages, page_shift, ncont, NULL);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200685
686 err = mlx5_ib_get_buf_offset(addr, *page_shift, offset);
687 if (err) {
688 mlx5_ib_warn(dev, "bad offset\n");
689 goto err_umem;
690 }
691
692 mlx5_ib_dbg(dev, "addr 0x%lx, size %zu, npages %d, page_shift %d, ncont %d, offset %d\n",
693 addr, size, *npages, *page_shift, *ncont, *offset);
694
695 return 0;
696
697err_umem:
698 ib_umem_release(*umem);
699 *umem = NULL;
700
701 return err;
702}
703
Yishai Hadas79b20a62016-05-23 15:20:50 +0300704static void destroy_user_rq(struct ib_pd *pd, struct mlx5_ib_rwq *rwq)
705{
706 struct mlx5_ib_ucontext *context;
707
708 context = to_mucontext(pd->uobject->context);
709 mlx5_ib_db_unmap_user(context, &rwq->db);
710 if (rwq->umem)
711 ib_umem_release(rwq->umem);
712}
713
714static int create_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
715 struct mlx5_ib_rwq *rwq,
716 struct mlx5_ib_create_wq *ucmd)
717{
718 struct mlx5_ib_ucontext *context;
719 int page_shift = 0;
720 int npages;
721 u32 offset = 0;
722 int ncont = 0;
723 int err;
724
725 if (!ucmd->buf_addr)
726 return -EINVAL;
727
728 context = to_mucontext(pd->uobject->context);
729 rwq->umem = ib_umem_get(pd->uobject->context, ucmd->buf_addr,
730 rwq->buf_size, 0, 0);
731 if (IS_ERR(rwq->umem)) {
732 mlx5_ib_dbg(dev, "umem_get failed\n");
733 err = PTR_ERR(rwq->umem);
734 return err;
735 }
736
Majd Dibbiny762f8992016-10-27 16:36:47 +0300737 mlx5_ib_cont_pages(rwq->umem, ucmd->buf_addr, 0, &npages, &page_shift,
Yishai Hadas79b20a62016-05-23 15:20:50 +0300738 &ncont, NULL);
739 err = mlx5_ib_get_buf_offset(ucmd->buf_addr, page_shift,
740 &rwq->rq_page_offset);
741 if (err) {
742 mlx5_ib_warn(dev, "bad offset\n");
743 goto err_umem;
744 }
745
746 rwq->rq_num_pas = ncont;
747 rwq->page_shift = page_shift;
748 rwq->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
749 rwq->wq_sig = !!(ucmd->flags & MLX5_WQ_FLAG_SIGNATURE);
750
751 mlx5_ib_dbg(dev, "addr 0x%llx, size %zd, npages %d, page_shift %d, ncont %d, offset %d\n",
752 (unsigned long long)ucmd->buf_addr, rwq->buf_size,
753 npages, page_shift, ncont, offset);
754
755 err = mlx5_ib_db_map_user(context, ucmd->db_addr, &rwq->db);
756 if (err) {
757 mlx5_ib_dbg(dev, "map failed\n");
758 goto err_umem;
759 }
760
761 rwq->create_type = MLX5_WQ_USER;
762 return 0;
763
764err_umem:
765 ib_umem_release(rwq->umem);
766 return err;
767}
768
Eli Cohene126ba92013-07-07 17:25:49 +0300769static int create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
770 struct mlx5_ib_qp *qp, struct ib_udata *udata,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200771 struct ib_qp_init_attr *attr,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300772 u32 **in,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200773 struct mlx5_ib_create_qp_resp *resp, int *inlen,
774 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300775{
776 struct mlx5_ib_ucontext *context;
777 struct mlx5_ib_create_qp ucmd;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200778 struct mlx5_ib_ubuffer *ubuffer = &base->ubuffer;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200779 int page_shift = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300780 int uar_index;
781 int npages;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200782 u32 offset = 0;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200783 int bfregn;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200784 int ncont = 0;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300785 __be64 *pas;
786 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300787 int err;
788
789 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
790 if (err) {
791 mlx5_ib_dbg(dev, "copy failed\n");
792 return err;
793 }
794
795 context = to_mucontext(pd->uobject->context);
796 /*
797 * TBD: should come from the verbs when we have the API
798 */
Leon Romanovsky051f2632015-12-20 12:16:11 +0200799 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
800 /* In CROSS_CHANNEL CQ and QP must use the same UAR */
Eli Cohen2f5ff262017-01-03 23:55:21 +0200801 bfregn = MLX5_CROSS_CHANNEL_BFREG;
Leon Romanovsky051f2632015-12-20 12:16:11 +0200802 else {
Eli Cohen2f5ff262017-01-03 23:55:21 +0200803 bfregn = alloc_bfreg(&context->bfregi, MLX5_IB_LATENCY_CLASS_HIGH);
804 if (bfregn < 0) {
805 mlx5_ib_dbg(dev, "failed to allocate low latency BFREG\n");
Leon Romanovsky051f2632015-12-20 12:16:11 +0200806 mlx5_ib_dbg(dev, "reverting to medium latency\n");
Eli Cohen2f5ff262017-01-03 23:55:21 +0200807 bfregn = alloc_bfreg(&context->bfregi, MLX5_IB_LATENCY_CLASS_MEDIUM);
808 if (bfregn < 0) {
809 mlx5_ib_dbg(dev, "failed to allocate medium latency BFREG\n");
Leon Romanovsky051f2632015-12-20 12:16:11 +0200810 mlx5_ib_dbg(dev, "reverting to high latency\n");
Eli Cohen2f5ff262017-01-03 23:55:21 +0200811 bfregn = alloc_bfreg(&context->bfregi, MLX5_IB_LATENCY_CLASS_LOW);
812 if (bfregn < 0) {
813 mlx5_ib_warn(dev, "bfreg allocation failed\n");
814 return bfregn;
Leon Romanovsky051f2632015-12-20 12:16:11 +0200815 }
Eli Cohenc1be5232014-01-14 17:45:12 +0200816 }
Eli Cohene126ba92013-07-07 17:25:49 +0300817 }
818 }
819
Eli Cohen2f5ff262017-01-03 23:55:21 +0200820 uar_index = bfregn_to_uar_index(&context->bfregi, bfregn);
821 mlx5_ib_dbg(dev, "bfregn 0x%x, uar_index 0x%x\n", bfregn, uar_index);
Eli Cohene126ba92013-07-07 17:25:49 +0300822
Haggai Eran48fea832014-05-22 14:50:11 +0300823 qp->rq.offset = 0;
824 qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
825 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
826
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +0200827 err = set_user_buf_size(dev, qp, &ucmd, base, attr);
Eli Cohene126ba92013-07-07 17:25:49 +0300828 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200829 goto err_bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300830
majd@mellanox.com19098df2016-01-14 19:13:03 +0200831 if (ucmd.buf_addr && ubuffer->buf_size) {
832 ubuffer->buf_addr = ucmd.buf_addr;
833 err = mlx5_ib_umem_get(dev, pd, ubuffer->buf_addr,
834 ubuffer->buf_size,
835 &ubuffer->umem, &npages, &page_shift,
836 &ncont, &offset);
837 if (err)
Eli Cohen2f5ff262017-01-03 23:55:21 +0200838 goto err_bfreg;
Eli Cohen9e9c47d2014-01-14 17:45:21 +0200839 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +0200840 ubuffer->umem = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300841 }
Eli Cohene126ba92013-07-07 17:25:49 +0300842
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300843 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
844 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * ncont;
Eli Cohene126ba92013-07-07 17:25:49 +0300845 *in = mlx5_vzalloc(*inlen);
846 if (!*in) {
847 err = -ENOMEM;
848 goto err_umem;
849 }
Eli Cohene126ba92013-07-07 17:25:49 +0300850
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300851 pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas);
852 if (ubuffer->umem)
853 mlx5_ib_populate_pas(dev, ubuffer->umem, page_shift, pas, 0);
854
855 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
856
857 MLX5_SET(qpc, qpc, log_page_size, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
858 MLX5_SET(qpc, qpc, page_offset, offset);
859
860 MLX5_SET(qpc, qpc, uar_page, uar_index);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200861 resp->bfreg_index = bfregn;
862 qp->bfregn = bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300863
864 err = mlx5_ib_db_map_user(context, ucmd.db_addr, &qp->db);
865 if (err) {
866 mlx5_ib_dbg(dev, "map failed\n");
867 goto err_free;
868 }
869
870 err = ib_copy_to_udata(udata, resp, sizeof(*resp));
871 if (err) {
872 mlx5_ib_dbg(dev, "copy failed\n");
873 goto err_unmap;
874 }
875 qp->create_type = MLX5_QP_USER;
876
877 return 0;
878
879err_unmap:
880 mlx5_ib_db_unmap_user(context, &qp->db);
881
882err_free:
Al Viro479163f2014-11-20 08:13:57 +0000883 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +0300884
885err_umem:
majd@mellanox.com19098df2016-01-14 19:13:03 +0200886 if (ubuffer->umem)
887 ib_umem_release(ubuffer->umem);
Eli Cohene126ba92013-07-07 17:25:49 +0300888
Eli Cohen2f5ff262017-01-03 23:55:21 +0200889err_bfreg:
890 free_bfreg(&context->bfregi, bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300891 return err;
892}
893
majd@mellanox.com19098df2016-01-14 19:13:03 +0200894static void destroy_qp_user(struct ib_pd *pd, struct mlx5_ib_qp *qp,
895 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300896{
897 struct mlx5_ib_ucontext *context;
898
899 context = to_mucontext(pd->uobject->context);
900 mlx5_ib_db_unmap_user(context, &qp->db);
majd@mellanox.com19098df2016-01-14 19:13:03 +0200901 if (base->ubuffer.umem)
902 ib_umem_release(base->ubuffer.umem);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200903 free_bfreg(&context->bfregi, qp->bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +0300904}
905
906static int create_kernel_qp(struct mlx5_ib_dev *dev,
907 struct ib_qp_init_attr *init_attr,
908 struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300909 u32 **in, int *inlen,
majd@mellanox.com19098df2016-01-14 19:13:03 +0200910 struct mlx5_ib_qp_base *base)
Eli Cohene126ba92013-07-07 17:25:49 +0300911{
Eli Cohene126ba92013-07-07 17:25:49 +0300912 int uar_index;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300913 void *qpc;
Eli Cohene126ba92013-07-07 17:25:49 +0300914 int err;
915
Erez Shitritf0313962016-02-21 16:27:17 +0200916 if (init_attr->create_flags & ~(IB_QP_CREATE_SIGNATURE_EN |
917 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
Haggai Eranb11a4f92016-02-29 15:45:03 +0200918 IB_QP_CREATE_IPOIB_UD_LSO |
919 mlx5_ib_create_qp_sqpn_qp1()))
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200920 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300921
922 if (init_attr->qp_type == MLX5_IB_QPT_REG_UMR)
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200923 qp->bf.bfreg = &dev->fp_bfreg;
924 else
925 qp->bf.bfreg = &dev->bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300926
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200927 qp->bf.buf_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
928 uar_index = qp->bf.bfreg->index;
Eli Cohene126ba92013-07-07 17:25:49 +0300929
930 err = calc_sq_size(dev, init_attr, qp);
931 if (err < 0) {
932 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200933 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300934 }
935
936 qp->rq.offset = 0;
937 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200938 base->ubuffer.buf_size = err + (qp->rq.wqe_cnt << qp->rq.wqe_shift);
Eli Cohene126ba92013-07-07 17:25:49 +0300939
majd@mellanox.com19098df2016-01-14 19:13:03 +0200940 err = mlx5_buf_alloc(dev->mdev, base->ubuffer.buf_size, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +0300941 if (err) {
942 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen5fe9dec2017-01-03 23:55:25 +0200943 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300944 }
945
946 qp->sq.qend = mlx5_get_send_wqe(qp, qp->sq.wqe_cnt);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300947 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
948 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * qp->buf.npages;
Eli Cohene126ba92013-07-07 17:25:49 +0300949 *in = mlx5_vzalloc(*inlen);
950 if (!*in) {
951 err = -ENOMEM;
952 goto err_buf;
953 }
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300954
955 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
956 MLX5_SET(qpc, qpc, uar_page, uar_index);
957 MLX5_SET(qpc, qpc, log_page_size, qp->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
958
Eli Cohene126ba92013-07-07 17:25:49 +0300959 /* Set "fast registration enabled" for all kernel QPs */
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300960 MLX5_SET(qpc, qpc, fre, 1);
961 MLX5_SET(qpc, qpc, rlky, 1);
Eli Cohene126ba92013-07-07 17:25:49 +0300962
Haggai Eranb11a4f92016-02-29 15:45:03 +0200963 if (init_attr->create_flags & mlx5_ib_create_qp_sqpn_qp1()) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300964 MLX5_SET(qpc, qpc, deth_sqpn, 1);
Haggai Eranb11a4f92016-02-29 15:45:03 +0200965 qp->flags |= MLX5_IB_QP_SQPN_QP1;
966 }
967
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300968 mlx5_fill_page_array(&qp->buf,
969 (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas));
Eli Cohene126ba92013-07-07 17:25:49 +0300970
Jack Morgenstein9603b612014-07-28 23:30:22 +0300971 err = mlx5_db_alloc(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300972 if (err) {
973 mlx5_ib_dbg(dev, "err %d\n", err);
974 goto err_free;
975 }
976
Eli Cohene126ba92013-07-07 17:25:49 +0300977 qp->sq.wrid = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wrid), GFP_KERNEL);
978 qp->sq.wr_data = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wr_data), GFP_KERNEL);
979 qp->rq.wrid = kmalloc(qp->rq.wqe_cnt * sizeof(*qp->rq.wrid), GFP_KERNEL);
980 qp->sq.w_list = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.w_list), GFP_KERNEL);
981 qp->sq.wqe_head = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wqe_head), GFP_KERNEL);
982
983 if (!qp->sq.wrid || !qp->sq.wr_data || !qp->rq.wrid ||
984 !qp->sq.w_list || !qp->sq.wqe_head) {
985 err = -ENOMEM;
986 goto err_wrid;
987 }
988 qp->create_type = MLX5_QP_KERNEL;
989
990 return 0;
991
992err_wrid:
Eli Cohene126ba92013-07-07 17:25:49 +0300993 kfree(qp->sq.wqe_head);
994 kfree(qp->sq.w_list);
995 kfree(qp->sq.wrid);
996 kfree(qp->sq.wr_data);
997 kfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +0200998 mlx5_db_free(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300999
1000err_free:
Al Viro479163f2014-11-20 08:13:57 +00001001 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +03001002
1003err_buf:
Jack Morgenstein9603b612014-07-28 23:30:22 +03001004 mlx5_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001005 return err;
1006}
1007
1008static void destroy_qp_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1009{
Eli Cohene126ba92013-07-07 17:25:49 +03001010 kfree(qp->sq.wqe_head);
1011 kfree(qp->sq.w_list);
1012 kfree(qp->sq.wrid);
1013 kfree(qp->sq.wr_data);
1014 kfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001015 mlx5_db_free(dev->mdev, &qp->db);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001016 mlx5_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001017}
1018
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001019static u32 get_rx_type(struct mlx5_ib_qp *qp, struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +03001020{
1021 if (attr->srq || (attr->qp_type == IB_QPT_XRC_TGT) ||
1022 (attr->qp_type == IB_QPT_XRC_INI))
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001023 return MLX5_SRQ_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001024 else if (!qp->has_rq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001025 return MLX5_ZERO_LEN_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001026 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001027 return MLX5_NON_ZERO_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001028}
1029
1030static int is_connected(enum ib_qp_type qp_type)
1031{
1032 if (qp_type == IB_QPT_RC || qp_type == IB_QPT_UC)
1033 return 1;
1034
1035 return 0;
1036}
1037
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001038static int create_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
1039 struct mlx5_ib_sq *sq, u32 tdn)
1040{
Saeed Mahameedc4f287c2016-07-19 20:17:12 +03001041 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001042 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1043
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001044 MLX5_SET(tisc, tisc, transport_domain, tdn);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001045 return mlx5_core_create_tis(dev->mdev, in, sizeof(in), &sq->tisn);
1046}
1047
1048static void destroy_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
1049 struct mlx5_ib_sq *sq)
1050{
1051 mlx5_core_destroy_tis(dev->mdev, sq->tisn);
1052}
1053
1054static int create_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1055 struct mlx5_ib_sq *sq, void *qpin,
1056 struct ib_pd *pd)
1057{
1058 struct mlx5_ib_ubuffer *ubuffer = &sq->ubuffer;
1059 __be64 *pas;
1060 void *in;
1061 void *sqc;
1062 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1063 void *wq;
1064 int inlen;
1065 int err;
1066 int page_shift = 0;
1067 int npages;
1068 int ncont = 0;
1069 u32 offset = 0;
1070
1071 err = mlx5_ib_umem_get(dev, pd, ubuffer->buf_addr, ubuffer->buf_size,
1072 &sq->ubuffer.umem, &npages, &page_shift,
1073 &ncont, &offset);
1074 if (err)
1075 return err;
1076
1077 inlen = MLX5_ST_SZ_BYTES(create_sq_in) + sizeof(u64) * ncont;
1078 in = mlx5_vzalloc(inlen);
1079 if (!in) {
1080 err = -ENOMEM;
1081 goto err_umem;
1082 }
1083
1084 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1085 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
1086 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1087 MLX5_SET(sqc, sqc, user_index, MLX5_GET(qpc, qpc, user_index));
1088 MLX5_SET(sqc, sqc, cqn, MLX5_GET(qpc, qpc, cqn_snd));
1089 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1090 MLX5_SET(sqc, sqc, tis_num_0, sq->tisn);
1091
1092 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1093 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1094 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1095 MLX5_SET(wq, wq, uar_page, MLX5_GET(qpc, qpc, uar_page));
1096 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1097 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1098 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_sq_size));
1099 MLX5_SET(wq, wq, log_wq_pg_sz, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1100 MLX5_SET(wq, wq, page_offset, offset);
1101
1102 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1103 mlx5_ib_populate_pas(dev, sq->ubuffer.umem, page_shift, pas, 0);
1104
1105 err = mlx5_core_create_sq_tracked(dev->mdev, in, inlen, &sq->base.mqp);
1106
1107 kvfree(in);
1108
1109 if (err)
1110 goto err_umem;
1111
1112 return 0;
1113
1114err_umem:
1115 ib_umem_release(sq->ubuffer.umem);
1116 sq->ubuffer.umem = NULL;
1117
1118 return err;
1119}
1120
1121static void destroy_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1122 struct mlx5_ib_sq *sq)
1123{
1124 mlx5_core_destroy_sq_tracked(dev->mdev, &sq->base.mqp);
1125 ib_umem_release(sq->ubuffer.umem);
1126}
1127
1128static int get_rq_pas_size(void *qpc)
1129{
1130 u32 log_page_size = MLX5_GET(qpc, qpc, log_page_size) + 12;
1131 u32 log_rq_stride = MLX5_GET(qpc, qpc, log_rq_stride);
1132 u32 log_rq_size = MLX5_GET(qpc, qpc, log_rq_size);
1133 u32 page_offset = MLX5_GET(qpc, qpc, page_offset);
1134 u32 po_quanta = 1 << (log_page_size - 6);
1135 u32 rq_sz = 1 << (log_rq_size + 4 + log_rq_stride);
1136 u32 page_size = 1 << log_page_size;
1137 u32 rq_sz_po = rq_sz + (page_offset * po_quanta);
1138 u32 rq_num_pas = (rq_sz_po + page_size - 1) / page_size;
1139
1140 return rq_num_pas * sizeof(u64);
1141}
1142
1143static int create_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1144 struct mlx5_ib_rq *rq, void *qpin)
1145{
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001146 struct mlx5_ib_qp *mqp = rq->base.container_mibqp;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001147 __be64 *pas;
1148 __be64 *qp_pas;
1149 void *in;
1150 void *rqc;
1151 void *wq;
1152 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1153 int inlen;
1154 int err;
1155 u32 rq_pas_size = get_rq_pas_size(qpc);
1156
1157 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + rq_pas_size;
1158 in = mlx5_vzalloc(inlen);
1159 if (!in)
1160 return -ENOMEM;
1161
1162 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
1163 MLX5_SET(rqc, rqc, vsd, 1);
1164 MLX5_SET(rqc, rqc, mem_rq_type, MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
1165 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
1166 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
1167 MLX5_SET(rqc, rqc, user_index, MLX5_GET(qpc, qpc, user_index));
1168 MLX5_SET(rqc, rqc, cqn, MLX5_GET(qpc, qpc, cqn_rcv));
1169
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001170 if (mqp->flags & MLX5_IB_QP_CAP_SCATTER_FCS)
1171 MLX5_SET(rqc, rqc, scatter_fcs, 1);
1172
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001173 wq = MLX5_ADDR_OF(rqc, rqc, wq);
1174 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1175 MLX5_SET(wq, wq, end_padding_mode,
Maor Gottlieb01581fb2016-01-28 17:51:49 +02001176 MLX5_GET(qpc, qpc, end_padding_mode));
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001177 MLX5_SET(wq, wq, page_offset, MLX5_GET(qpc, qpc, page_offset));
1178 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1179 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1180 MLX5_SET(wq, wq, log_wq_stride, MLX5_GET(qpc, qpc, log_rq_stride) + 4);
1181 MLX5_SET(wq, wq, log_wq_pg_sz, MLX5_GET(qpc, qpc, log_page_size));
1182 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_rq_size));
1183
1184 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1185 qp_pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, qpin, pas);
1186 memcpy(pas, qp_pas, rq_pas_size);
1187
1188 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rq->base.mqp);
1189
1190 kvfree(in);
1191
1192 return err;
1193}
1194
1195static void destroy_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1196 struct mlx5_ib_rq *rq)
1197{
1198 mlx5_core_destroy_rq_tracked(dev->mdev, &rq->base.mqp);
1199}
1200
1201static int create_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1202 struct mlx5_ib_rq *rq, u32 tdn)
1203{
1204 u32 *in;
1205 void *tirc;
1206 int inlen;
1207 int err;
1208
1209 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
1210 in = mlx5_vzalloc(inlen);
1211 if (!in)
1212 return -ENOMEM;
1213
1214 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1215 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
1216 MLX5_SET(tirc, tirc, inline_rqn, rq->base.mqp.qpn);
1217 MLX5_SET(tirc, tirc, transport_domain, tdn);
1218
1219 err = mlx5_core_create_tir(dev->mdev, in, inlen, &rq->tirn);
1220
1221 kvfree(in);
1222
1223 return err;
1224}
1225
1226static void destroy_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1227 struct mlx5_ib_rq *rq)
1228{
1229 mlx5_core_destroy_tir(dev->mdev, rq->tirn);
1230}
1231
1232static int create_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001233 u32 *in,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001234 struct ib_pd *pd)
1235{
1236 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1237 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1238 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1239 struct ib_uobject *uobj = pd->uobject;
1240 struct ib_ucontext *ucontext = uobj->context;
1241 struct mlx5_ib_ucontext *mucontext = to_mucontext(ucontext);
1242 int err;
1243 u32 tdn = mucontext->tdn;
1244
1245 if (qp->sq.wqe_cnt) {
1246 err = create_raw_packet_qp_tis(dev, sq, tdn);
1247 if (err)
1248 return err;
1249
1250 err = create_raw_packet_qp_sq(dev, sq, in, pd);
1251 if (err)
1252 goto err_destroy_tis;
1253
1254 sq->base.container_mibqp = qp;
1255 }
1256
1257 if (qp->rq.wqe_cnt) {
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001258 rq->base.container_mibqp = qp;
1259
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001260 err = create_raw_packet_qp_rq(dev, rq, in);
1261 if (err)
1262 goto err_destroy_sq;
1263
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001264
1265 err = create_raw_packet_qp_tir(dev, rq, tdn);
1266 if (err)
1267 goto err_destroy_rq;
1268 }
1269
1270 qp->trans_qp.base.mqp.qpn = qp->sq.wqe_cnt ? sq->base.mqp.qpn :
1271 rq->base.mqp.qpn;
1272
1273 return 0;
1274
1275err_destroy_rq:
1276 destroy_raw_packet_qp_rq(dev, rq);
1277err_destroy_sq:
1278 if (!qp->sq.wqe_cnt)
1279 return err;
1280 destroy_raw_packet_qp_sq(dev, sq);
1281err_destroy_tis:
1282 destroy_raw_packet_qp_tis(dev, sq);
1283
1284 return err;
1285}
1286
1287static void destroy_raw_packet_qp(struct mlx5_ib_dev *dev,
1288 struct mlx5_ib_qp *qp)
1289{
1290 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1291 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1292 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1293
1294 if (qp->rq.wqe_cnt) {
1295 destroy_raw_packet_qp_tir(dev, rq);
1296 destroy_raw_packet_qp_rq(dev, rq);
1297 }
1298
1299 if (qp->sq.wqe_cnt) {
1300 destroy_raw_packet_qp_sq(dev, sq);
1301 destroy_raw_packet_qp_tis(dev, sq);
1302 }
1303}
1304
1305static void raw_packet_qp_copy_info(struct mlx5_ib_qp *qp,
1306 struct mlx5_ib_raw_packet_qp *raw_packet_qp)
1307{
1308 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1309 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1310
1311 sq->sq = &qp->sq;
1312 rq->rq = &qp->rq;
1313 sq->doorbell = &qp->db;
1314 rq->doorbell = &qp->db;
1315}
1316
Yishai Hadas28d61372016-05-23 15:20:56 +03001317static void destroy_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1318{
1319 mlx5_core_destroy_tir(dev->mdev, qp->rss_qp.tirn);
1320}
1321
1322static int create_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
1323 struct ib_pd *pd,
1324 struct ib_qp_init_attr *init_attr,
1325 struct ib_udata *udata)
1326{
1327 struct ib_uobject *uobj = pd->uobject;
1328 struct ib_ucontext *ucontext = uobj->context;
1329 struct mlx5_ib_ucontext *mucontext = to_mucontext(ucontext);
1330 struct mlx5_ib_create_qp_resp resp = {};
1331 int inlen;
1332 int err;
1333 u32 *in;
1334 void *tirc;
1335 void *hfso;
1336 u32 selected_fields = 0;
1337 size_t min_resp_len;
1338 u32 tdn = mucontext->tdn;
1339 struct mlx5_ib_create_qp_rss ucmd = {};
1340 size_t required_cmd_sz;
1341
1342 if (init_attr->qp_type != IB_QPT_RAW_PACKET)
1343 return -EOPNOTSUPP;
1344
1345 if (init_attr->create_flags || init_attr->send_cq)
1346 return -EINVAL;
1347
Eli Cohen2f5ff262017-01-03 23:55:21 +02001348 min_resp_len = offsetof(typeof(resp), bfreg_index) + sizeof(resp.bfreg_index);
Yishai Hadas28d61372016-05-23 15:20:56 +03001349 if (udata->outlen < min_resp_len)
1350 return -EINVAL;
1351
1352 required_cmd_sz = offsetof(typeof(ucmd), reserved1) + sizeof(ucmd.reserved1);
1353 if (udata->inlen < required_cmd_sz) {
1354 mlx5_ib_dbg(dev, "invalid inlen\n");
1355 return -EINVAL;
1356 }
1357
1358 if (udata->inlen > sizeof(ucmd) &&
1359 !ib_is_udata_cleared(udata, sizeof(ucmd),
1360 udata->inlen - sizeof(ucmd))) {
1361 mlx5_ib_dbg(dev, "inlen is not supported\n");
1362 return -EOPNOTSUPP;
1363 }
1364
1365 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
1366 mlx5_ib_dbg(dev, "copy failed\n");
1367 return -EFAULT;
1368 }
1369
1370 if (ucmd.comp_mask) {
1371 mlx5_ib_dbg(dev, "invalid comp mask\n");
1372 return -EOPNOTSUPP;
1373 }
1374
1375 if (memchr_inv(ucmd.reserved, 0, sizeof(ucmd.reserved)) || ucmd.reserved1) {
1376 mlx5_ib_dbg(dev, "invalid reserved\n");
1377 return -EOPNOTSUPP;
1378 }
1379
1380 err = ib_copy_to_udata(udata, &resp, min_resp_len);
1381 if (err) {
1382 mlx5_ib_dbg(dev, "copy failed\n");
1383 return -EINVAL;
1384 }
1385
1386 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
1387 in = mlx5_vzalloc(inlen);
1388 if (!in)
1389 return -ENOMEM;
1390
1391 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1392 MLX5_SET(tirc, tirc, disp_type,
1393 MLX5_TIRC_DISP_TYPE_INDIRECT);
1394 MLX5_SET(tirc, tirc, indirect_table,
1395 init_attr->rwq_ind_tbl->ind_tbl_num);
1396 MLX5_SET(tirc, tirc, transport_domain, tdn);
1397
1398 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1399 switch (ucmd.rx_hash_function) {
1400 case MLX5_RX_HASH_FUNC_TOEPLITZ:
1401 {
1402 void *rss_key = MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
1403 size_t len = MLX5_FLD_SZ_BYTES(tirc, rx_hash_toeplitz_key);
1404
1405 if (len != ucmd.rx_key_len) {
1406 err = -EINVAL;
1407 goto err;
1408 }
1409
1410 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_TOEPLITZ);
1411 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1412 memcpy(rss_key, ucmd.rx_hash_key, len);
1413 break;
1414 }
1415 default:
1416 err = -EOPNOTSUPP;
1417 goto err;
1418 }
1419
1420 if (!ucmd.rx_hash_fields_mask) {
1421 /* special case when this TIR serves as steering entry without hashing */
1422 if (!init_attr->rwq_ind_tbl->log_ind_tbl_size)
1423 goto create_tir;
1424 err = -EINVAL;
1425 goto err;
1426 }
1427
1428 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1429 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4)) &&
1430 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1431 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))) {
1432 err = -EINVAL;
1433 goto err;
1434 }
1435
1436 /* If none of IPV4 & IPV6 SRC/DST was set - this bit field is ignored */
1437 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1438 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4))
1439 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1440 MLX5_L3_PROT_TYPE_IPV4);
1441 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1442 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1443 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1444 MLX5_L3_PROT_TYPE_IPV6);
1445
1446 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1447 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP)) &&
1448 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1449 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))) {
1450 err = -EINVAL;
1451 goto err;
1452 }
1453
1454 /* If none of TCP & UDP SRC/DST was set - this bit field is ignored */
1455 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1456 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP))
1457 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1458 MLX5_L4_PROT_TYPE_TCP);
1459 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1460 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1461 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1462 MLX5_L4_PROT_TYPE_UDP);
1463
1464 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1465 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6))
1466 selected_fields |= MLX5_HASH_FIELD_SEL_SRC_IP;
1467
1468 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4) ||
1469 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1470 selected_fields |= MLX5_HASH_FIELD_SEL_DST_IP;
1471
1472 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1473 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP))
1474 selected_fields |= MLX5_HASH_FIELD_SEL_L4_SPORT;
1475
1476 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP) ||
1477 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1478 selected_fields |= MLX5_HASH_FIELD_SEL_L4_DPORT;
1479
1480 MLX5_SET(rx_hash_field_select, hfso, selected_fields, selected_fields);
1481
1482create_tir:
1483 err = mlx5_core_create_tir(dev->mdev, in, inlen, &qp->rss_qp.tirn);
1484
1485 if (err)
1486 goto err;
1487
1488 kvfree(in);
1489 /* qpn is reserved for that QP */
1490 qp->trans_qp.base.mqp.qpn = 0;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03001491 qp->flags |= MLX5_IB_QP_RSS;
Yishai Hadas28d61372016-05-23 15:20:56 +03001492 return 0;
1493
1494err:
1495 kvfree(in);
1496 return err;
1497}
1498
Eli Cohene126ba92013-07-07 17:25:49 +03001499static int create_qp_common(struct mlx5_ib_dev *dev, struct ib_pd *pd,
1500 struct ib_qp_init_attr *init_attr,
1501 struct ib_udata *udata, struct mlx5_ib_qp *qp)
1502{
1503 struct mlx5_ib_resources *devr = &dev->devr;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001504 int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
Saeed Mahameed938fe832015-05-28 22:28:41 +03001505 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001506 struct mlx5_ib_create_qp_resp resp;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001507 struct mlx5_ib_cq *send_cq;
1508 struct mlx5_ib_cq *recv_cq;
1509 unsigned long flags;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001510 u32 uidx = MLX5_IB_DEFAULT_UIDX;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001511 struct mlx5_ib_create_qp ucmd;
1512 struct mlx5_ib_qp_base *base;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001513 void *qpc;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001514 u32 *in;
1515 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03001516
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001517 base = init_attr->qp_type == IB_QPT_RAW_PACKET ?
1518 &qp->raw_packet_qp.rq.base :
1519 &qp->trans_qp.base;
1520
Eli Cohene126ba92013-07-07 17:25:49 +03001521 mutex_init(&qp->mutex);
1522 spin_lock_init(&qp->sq.lock);
1523 spin_lock_init(&qp->rq.lock);
1524
Yishai Hadas28d61372016-05-23 15:20:56 +03001525 if (init_attr->rwq_ind_tbl) {
1526 if (!udata)
1527 return -ENOSYS;
1528
1529 err = create_rss_raw_qp_tir(dev, qp, pd, init_attr, udata);
1530 return err;
1531 }
1532
Eli Cohenf360d882014-04-02 00:10:16 +03001533 if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001534 if (!MLX5_CAP_GEN(mdev, block_lb_mc)) {
Eli Cohenf360d882014-04-02 00:10:16 +03001535 mlx5_ib_dbg(dev, "block multicast loopback isn't supported\n");
1536 return -EINVAL;
1537 } else {
1538 qp->flags |= MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK;
1539 }
1540 }
1541
Leon Romanovsky051f2632015-12-20 12:16:11 +02001542 if (init_attr->create_flags &
1543 (IB_QP_CREATE_CROSS_CHANNEL |
1544 IB_QP_CREATE_MANAGED_SEND |
1545 IB_QP_CREATE_MANAGED_RECV)) {
1546 if (!MLX5_CAP_GEN(mdev, cd)) {
1547 mlx5_ib_dbg(dev, "cross-channel isn't supported\n");
1548 return -EINVAL;
1549 }
1550 if (init_attr->create_flags & IB_QP_CREATE_CROSS_CHANNEL)
1551 qp->flags |= MLX5_IB_QP_CROSS_CHANNEL;
1552 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_SEND)
1553 qp->flags |= MLX5_IB_QP_MANAGED_SEND;
1554 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_RECV)
1555 qp->flags |= MLX5_IB_QP_MANAGED_RECV;
1556 }
Erez Shitritf0313962016-02-21 16:27:17 +02001557
1558 if (init_attr->qp_type == IB_QPT_UD &&
1559 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO))
1560 if (!MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
1561 mlx5_ib_dbg(dev, "ipoib UD lso qp isn't supported\n");
1562 return -EOPNOTSUPP;
1563 }
1564
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001565 if (init_attr->create_flags & IB_QP_CREATE_SCATTER_FCS) {
1566 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
1567 mlx5_ib_dbg(dev, "Scatter FCS is supported only for Raw Packet QPs");
1568 return -EOPNOTSUPP;
1569 }
1570 if (!MLX5_CAP_GEN(dev->mdev, eth_net_offloads) ||
1571 !MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
1572 mlx5_ib_dbg(dev, "Scatter FCS isn't supported\n");
1573 return -EOPNOTSUPP;
1574 }
1575 qp->flags |= MLX5_IB_QP_CAP_SCATTER_FCS;
1576 }
1577
Eli Cohene126ba92013-07-07 17:25:49 +03001578 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
1579 qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;
1580
1581 if (pd && pd->uobject) {
1582 if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
1583 mlx5_ib_dbg(dev, "copy failed\n");
1584 return -EFAULT;
1585 }
1586
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001587 err = get_qp_user_index(to_mucontext(pd->uobject->context),
1588 &ucmd, udata->inlen, &uidx);
1589 if (err)
1590 return err;
1591
Eli Cohene126ba92013-07-07 17:25:49 +03001592 qp->wq_sig = !!(ucmd.flags & MLX5_QP_FLAG_SIGNATURE);
1593 qp->scat_cqe = !!(ucmd.flags & MLX5_QP_FLAG_SCATTER_CQE);
1594 } else {
1595 qp->wq_sig = !!wq_signature;
1596 }
1597
1598 qp->has_rq = qp_has_rq(init_attr);
1599 err = set_rq_size(dev, &init_attr->cap, qp->has_rq,
1600 qp, (pd && pd->uobject) ? &ucmd : NULL);
1601 if (err) {
1602 mlx5_ib_dbg(dev, "err %d\n", err);
1603 return err;
1604 }
1605
1606 if (pd) {
1607 if (pd->uobject) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001608 __u32 max_wqes =
1609 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Eli Cohene126ba92013-07-07 17:25:49 +03001610 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d)\n", ucmd.sq_wqe_count);
1611 if (ucmd.rq_wqe_shift != qp->rq.wqe_shift ||
1612 ucmd.rq_wqe_count != qp->rq.wqe_cnt) {
1613 mlx5_ib_dbg(dev, "invalid rq params\n");
1614 return -EINVAL;
1615 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03001616 if (ucmd.sq_wqe_count > max_wqes) {
Eli Cohene126ba92013-07-07 17:25:49 +03001617 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d) > max allowed (%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +03001618 ucmd.sq_wqe_count, max_wqes);
Eli Cohene126ba92013-07-07 17:25:49 +03001619 return -EINVAL;
1620 }
Haggai Eranb11a4f92016-02-29 15:45:03 +02001621 if (init_attr->create_flags &
1622 mlx5_ib_create_qp_sqpn_qp1()) {
1623 mlx5_ib_dbg(dev, "user-space is not allowed to create UD QPs spoofing as QP1\n");
1624 return -EINVAL;
1625 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001626 err = create_user_qp(dev, pd, qp, udata, init_attr, &in,
1627 &resp, &inlen, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001628 if (err)
1629 mlx5_ib_dbg(dev, "err %d\n", err);
1630 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +02001631 err = create_kernel_qp(dev, init_attr, qp, &in, &inlen,
1632 base);
Eli Cohene126ba92013-07-07 17:25:49 +03001633 if (err)
1634 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohene126ba92013-07-07 17:25:49 +03001635 }
1636
1637 if (err)
1638 return err;
1639 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001640 in = mlx5_vzalloc(inlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001641 if (!in)
1642 return -ENOMEM;
1643
1644 qp->create_type = MLX5_QP_EMPTY;
1645 }
1646
1647 if (is_sqp(init_attr->qp_type))
1648 qp->port = init_attr->port_num;
1649
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001650 qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
1651
1652 MLX5_SET(qpc, qpc, st, to_mlx5_st(init_attr->qp_type));
1653 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
Eli Cohene126ba92013-07-07 17:25:49 +03001654
1655 if (init_attr->qp_type != MLX5_IB_QPT_REG_UMR)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001656 MLX5_SET(qpc, qpc, pd, to_mpd(pd ? pd : devr->p0)->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001657 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001658 MLX5_SET(qpc, qpc, latency_sensitive, 1);
1659
Eli Cohene126ba92013-07-07 17:25:49 +03001660
1661 if (qp->wq_sig)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001662 MLX5_SET(qpc, qpc, wq_signature, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001663
Eli Cohenf360d882014-04-02 00:10:16 +03001664 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001665 MLX5_SET(qpc, qpc, block_lb_mc, 1);
Eli Cohenf360d882014-04-02 00:10:16 +03001666
Leon Romanovsky051f2632015-12-20 12:16:11 +02001667 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001668 MLX5_SET(qpc, qpc, cd_master, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001669 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001670 MLX5_SET(qpc, qpc, cd_slave_send, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001671 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001672 MLX5_SET(qpc, qpc, cd_slave_receive, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001673
Eli Cohene126ba92013-07-07 17:25:49 +03001674 if (qp->scat_cqe && is_connected(init_attr->qp_type)) {
1675 int rcqe_sz;
1676 int scqe_sz;
1677
1678 rcqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->recv_cq);
1679 scqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->send_cq);
1680
1681 if (rcqe_sz == 128)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001682 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA64_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001683 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001684 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA32_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001685
1686 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR) {
1687 if (scqe_sz == 128)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001688 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA64_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001689 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001690 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA32_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001691 }
1692 }
1693
1694 if (qp->rq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001695 MLX5_SET(qpc, qpc, log_rq_stride, qp->rq.wqe_shift - 4);
1696 MLX5_SET(qpc, qpc, log_rq_size, ilog2(qp->rq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03001697 }
1698
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001699 MLX5_SET(qpc, qpc, rq_type, get_rx_type(qp, init_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03001700
1701 if (qp->sq.wqe_cnt)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001702 MLX5_SET(qpc, qpc, log_sq_size, ilog2(qp->sq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03001703 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001704 MLX5_SET(qpc, qpc, no_sq, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001705
1706 /* Set default resources */
1707 switch (init_attr->qp_type) {
1708 case IB_QPT_XRC_TGT:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001709 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
1710 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(devr->c0)->mcq.cqn);
1711 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
1712 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(init_attr->xrcd)->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001713 break;
1714 case IB_QPT_XRC_INI:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001715 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
1716 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
1717 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001718 break;
1719 default:
1720 if (init_attr->srq) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001721 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x0)->xrcdn);
1722 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(init_attr->srq)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001723 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001724 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
1725 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s1)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001726 }
1727 }
1728
1729 if (init_attr->send_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001730 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(init_attr->send_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001731
1732 if (init_attr->recv_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001733 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(init_attr->recv_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001734
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001735 MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
Eli Cohene126ba92013-07-07 17:25:49 +03001736
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001737 /* 0xffffff means we ask to work with cqe version 0 */
1738 if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1)
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001739 MLX5_SET(qpc, qpc, user_index, uidx);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001740
Erez Shitritf0313962016-02-21 16:27:17 +02001741 /* we use IB_QP_CREATE_IPOIB_UD_LSO to indicates ipoib qp */
1742 if (init_attr->qp_type == IB_QPT_UD &&
1743 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)) {
Erez Shitritf0313962016-02-21 16:27:17 +02001744 MLX5_SET(qpc, qpc, ulp_stateless_offload_mode, 1);
1745 qp->flags |= MLX5_IB_QP_LSO;
1746 }
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001747
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001748 if (init_attr->qp_type == IB_QPT_RAW_PACKET) {
1749 qp->raw_packet_qp.sq.ubuffer.buf_addr = ucmd.sq_buf_addr;
1750 raw_packet_qp_copy_info(qp, &qp->raw_packet_qp);
1751 err = create_raw_packet_qp(dev, qp, in, pd);
1752 } else {
1753 err = mlx5_core_create_qp(dev->mdev, &base->mqp, in, inlen);
1754 }
1755
Eli Cohene126ba92013-07-07 17:25:49 +03001756 if (err) {
1757 mlx5_ib_dbg(dev, "create qp failed\n");
1758 goto err_create;
1759 }
1760
Al Viro479163f2014-11-20 08:13:57 +00001761 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001762
majd@mellanox.com19098df2016-01-14 19:13:03 +02001763 base->container_mibqp = qp;
1764 base->mqp.event = mlx5_ib_qp_event;
Eli Cohene126ba92013-07-07 17:25:49 +03001765
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001766 get_cqs(init_attr->qp_type, init_attr->send_cq, init_attr->recv_cq,
1767 &send_cq, &recv_cq);
1768 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
1769 mlx5_ib_lock_cqs(send_cq, recv_cq);
1770 /* Maintain device to QPs access, needed for further handling via reset
1771 * flow
1772 */
1773 list_add_tail(&qp->qps_list, &dev->qp_list);
1774 /* Maintain CQ to QPs access, needed for further handling via reset flow
1775 */
1776 if (send_cq)
1777 list_add_tail(&qp->cq_send_list, &send_cq->list_send_qp);
1778 if (recv_cq)
1779 list_add_tail(&qp->cq_recv_list, &recv_cq->list_recv_qp);
1780 mlx5_ib_unlock_cqs(send_cq, recv_cq);
1781 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
1782
Eli Cohene126ba92013-07-07 17:25:49 +03001783 return 0;
1784
1785err_create:
1786 if (qp->create_type == MLX5_QP_USER)
majd@mellanox.com19098df2016-01-14 19:13:03 +02001787 destroy_qp_user(pd, qp, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001788 else if (qp->create_type == MLX5_QP_KERNEL)
1789 destroy_qp_kernel(dev, qp);
1790
Al Viro479163f2014-11-20 08:13:57 +00001791 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001792 return err;
1793}
1794
1795static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
1796 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
1797{
1798 if (send_cq) {
1799 if (recv_cq) {
1800 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001801 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001802 spin_lock_nested(&recv_cq->lock,
1803 SINGLE_DEPTH_NESTING);
1804 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001805 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001806 __acquire(&recv_cq->lock);
1807 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001808 spin_lock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001809 spin_lock_nested(&send_cq->lock,
1810 SINGLE_DEPTH_NESTING);
1811 }
1812 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001813 spin_lock(&send_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001814 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001815 }
1816 } else if (recv_cq) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001817 spin_lock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001818 __acquire(&send_cq->lock);
1819 } else {
1820 __acquire(&send_cq->lock);
1821 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001822 }
1823}
1824
1825static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
1826 __releases(&send_cq->lock) __releases(&recv_cq->lock)
1827{
1828 if (send_cq) {
1829 if (recv_cq) {
1830 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
1831 spin_unlock(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001832 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001833 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
1834 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001835 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001836 } else {
1837 spin_unlock(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001838 spin_unlock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001839 }
1840 } else {
Eli Cohen6a4f1392014-12-02 12:26:18 +02001841 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001842 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001843 }
1844 } else if (recv_cq) {
Eli Cohen6a4f1392014-12-02 12:26:18 +02001845 __release(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001846 spin_unlock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001847 } else {
1848 __release(&recv_cq->lock);
1849 __release(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001850 }
1851}
1852
1853static struct mlx5_ib_pd *get_pd(struct mlx5_ib_qp *qp)
1854{
1855 return to_mpd(qp->ibqp.pd);
1856}
1857
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001858static void get_cqs(enum ib_qp_type qp_type,
1859 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
Eli Cohene126ba92013-07-07 17:25:49 +03001860 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq)
1861{
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001862 switch (qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +03001863 case IB_QPT_XRC_TGT:
1864 *send_cq = NULL;
1865 *recv_cq = NULL;
1866 break;
1867 case MLX5_IB_QPT_REG_UMR:
1868 case IB_QPT_XRC_INI:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001869 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03001870 *recv_cq = NULL;
1871 break;
1872
1873 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02001874 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03001875 case IB_QPT_RC:
1876 case IB_QPT_UC:
1877 case IB_QPT_UD:
1878 case IB_QPT_RAW_IPV6:
1879 case IB_QPT_RAW_ETHERTYPE:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001880 case IB_QPT_RAW_PACKET:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001881 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
1882 *recv_cq = ib_recv_cq ? to_mcq(ib_recv_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03001883 break;
1884
Eli Cohene126ba92013-07-07 17:25:49 +03001885 case IB_QPT_MAX:
1886 default:
1887 *send_cq = NULL;
1888 *recv_cq = NULL;
1889 break;
1890 }
1891}
1892
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001893static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03001894 const struct mlx5_modify_raw_qp_param *raw_qp_param,
1895 u8 lag_tx_affinity);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001896
Eli Cohene126ba92013-07-07 17:25:49 +03001897static void destroy_qp_common(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1898{
1899 struct mlx5_ib_cq *send_cq, *recv_cq;
majd@mellanox.com19098df2016-01-14 19:13:03 +02001900 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001901 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03001902 int err;
1903
Yishai Hadas28d61372016-05-23 15:20:56 +03001904 if (qp->ibqp.rwq_ind_tbl) {
1905 destroy_rss_raw_qp_tir(dev, qp);
1906 return;
1907 }
1908
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001909 base = qp->ibqp.qp_type == IB_QPT_RAW_PACKET ?
1910 &qp->raw_packet_qp.rq.base :
1911 &qp->trans_qp.base;
1912
Haggai Eran6aec21f2014-12-11 17:04:23 +02001913 if (qp->state != IB_QPS_RESET) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001914 if (qp->ibqp.qp_type != IB_QPT_RAW_PACKET) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001915 err = mlx5_core_qp_modify(dev->mdev,
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03001916 MLX5_CMD_OP_2RST_QP, 0,
1917 NULL, &base->mqp);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001918 } else {
Alex Vesker0680efa2016-08-28 12:25:52 +03001919 struct mlx5_modify_raw_qp_param raw_qp_param = {
1920 .operation = MLX5_CMD_OP_2RST_QP
1921 };
1922
Aviv Heller13eab212016-09-18 20:48:04 +03001923 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, 0);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001924 }
1925 if (err)
majd@mellanox.com427c1e72016-01-14 19:13:07 +02001926 mlx5_ib_warn(dev, "mlx5_ib: modify QP 0x%06x to RESET failed\n",
majd@mellanox.com19098df2016-01-14 19:13:03 +02001927 base->mqp.qpn);
Haggai Eran6aec21f2014-12-11 17:04:23 +02001928 }
Eli Cohene126ba92013-07-07 17:25:49 +03001929
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001930 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
1931 &send_cq, &recv_cq);
1932
1933 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
1934 mlx5_ib_lock_cqs(send_cq, recv_cq);
1935 /* del from lists under both locks above to protect reset flow paths */
1936 list_del(&qp->qps_list);
1937 if (send_cq)
1938 list_del(&qp->cq_send_list);
1939
1940 if (recv_cq)
1941 list_del(&qp->cq_recv_list);
Eli Cohene126ba92013-07-07 17:25:49 +03001942
1943 if (qp->create_type == MLX5_QP_KERNEL) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02001944 __mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03001945 qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
1946 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02001947 __mlx5_ib_cq_clean(send_cq, base->mqp.qpn,
1948 NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03001949 }
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001950 mlx5_ib_unlock_cqs(send_cq, recv_cq);
1951 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
Eli Cohene126ba92013-07-07 17:25:49 +03001952
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001953 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET) {
1954 destroy_raw_packet_qp(dev, qp);
1955 } else {
1956 err = mlx5_core_destroy_qp(dev->mdev, &base->mqp);
1957 if (err)
1958 mlx5_ib_warn(dev, "failed to destroy QP 0x%x\n",
1959 base->mqp.qpn);
1960 }
Eli Cohene126ba92013-07-07 17:25:49 +03001961
Eli Cohene126ba92013-07-07 17:25:49 +03001962 if (qp->create_type == MLX5_QP_KERNEL)
1963 destroy_qp_kernel(dev, qp);
1964 else if (qp->create_type == MLX5_QP_USER)
majd@mellanox.com19098df2016-01-14 19:13:03 +02001965 destroy_qp_user(&get_pd(qp)->ibpd, qp, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001966}
1967
1968static const char *ib_qp_type_str(enum ib_qp_type type)
1969{
1970 switch (type) {
1971 case IB_QPT_SMI:
1972 return "IB_QPT_SMI";
1973 case IB_QPT_GSI:
1974 return "IB_QPT_GSI";
1975 case IB_QPT_RC:
1976 return "IB_QPT_RC";
1977 case IB_QPT_UC:
1978 return "IB_QPT_UC";
1979 case IB_QPT_UD:
1980 return "IB_QPT_UD";
1981 case IB_QPT_RAW_IPV6:
1982 return "IB_QPT_RAW_IPV6";
1983 case IB_QPT_RAW_ETHERTYPE:
1984 return "IB_QPT_RAW_ETHERTYPE";
1985 case IB_QPT_XRC_INI:
1986 return "IB_QPT_XRC_INI";
1987 case IB_QPT_XRC_TGT:
1988 return "IB_QPT_XRC_TGT";
1989 case IB_QPT_RAW_PACKET:
1990 return "IB_QPT_RAW_PACKET";
1991 case MLX5_IB_QPT_REG_UMR:
1992 return "MLX5_IB_QPT_REG_UMR";
1993 case IB_QPT_MAX:
1994 default:
1995 return "Invalid QP type";
1996 }
1997}
1998
1999struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd,
2000 struct ib_qp_init_attr *init_attr,
2001 struct ib_udata *udata)
2002{
2003 struct mlx5_ib_dev *dev;
2004 struct mlx5_ib_qp *qp;
2005 u16 xrcdn = 0;
2006 int err;
2007
2008 if (pd) {
2009 dev = to_mdev(pd->device);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002010
2011 if (init_attr->qp_type == IB_QPT_RAW_PACKET) {
2012 if (!pd->uobject) {
2013 mlx5_ib_dbg(dev, "Raw Packet QP is not supported for kernel consumers\n");
2014 return ERR_PTR(-EINVAL);
2015 } else if (!to_mucontext(pd->uobject->context)->cqe_version) {
2016 mlx5_ib_dbg(dev, "Raw Packet QP is only supported for CQE version > 0\n");
2017 return ERR_PTR(-EINVAL);
2018 }
2019 }
Majd Dibbiny09f16cf2016-01-28 17:51:48 +02002020 } else {
2021 /* being cautious here */
2022 if (init_attr->qp_type != IB_QPT_XRC_TGT &&
2023 init_attr->qp_type != MLX5_IB_QPT_REG_UMR) {
2024 pr_warn("%s: no PD for transport %s\n", __func__,
2025 ib_qp_type_str(init_attr->qp_type));
2026 return ERR_PTR(-EINVAL);
2027 }
2028 dev = to_mdev(to_mxrcd(init_attr->xrcd)->ibxrcd.device);
Eli Cohene126ba92013-07-07 17:25:49 +03002029 }
2030
2031 switch (init_attr->qp_type) {
2032 case IB_QPT_XRC_TGT:
2033 case IB_QPT_XRC_INI:
Saeed Mahameed938fe832015-05-28 22:28:41 +03002034 if (!MLX5_CAP_GEN(dev->mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002035 mlx5_ib_dbg(dev, "XRC not supported\n");
2036 return ERR_PTR(-ENOSYS);
2037 }
2038 init_attr->recv_cq = NULL;
2039 if (init_attr->qp_type == IB_QPT_XRC_TGT) {
2040 xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
2041 init_attr->send_cq = NULL;
2042 }
2043
2044 /* fall through */
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002045 case IB_QPT_RAW_PACKET:
Eli Cohene126ba92013-07-07 17:25:49 +03002046 case IB_QPT_RC:
2047 case IB_QPT_UC:
2048 case IB_QPT_UD:
2049 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002050 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002051 case MLX5_IB_QPT_REG_UMR:
2052 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2053 if (!qp)
2054 return ERR_PTR(-ENOMEM);
2055
2056 err = create_qp_common(dev, pd, init_attr, udata, qp);
2057 if (err) {
2058 mlx5_ib_dbg(dev, "create_qp_common failed\n");
2059 kfree(qp);
2060 return ERR_PTR(err);
2061 }
2062
2063 if (is_qp0(init_attr->qp_type))
2064 qp->ibqp.qp_num = 0;
2065 else if (is_qp1(init_attr->qp_type))
2066 qp->ibqp.qp_num = 1;
2067 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002068 qp->ibqp.qp_num = qp->trans_qp.base.mqp.qpn;
Eli Cohene126ba92013-07-07 17:25:49 +03002069
2070 mlx5_ib_dbg(dev, "ib qpnum 0x%x, mlx qpn 0x%x, rcqn 0x%x, scqn 0x%x\n",
majd@mellanox.com19098df2016-01-14 19:13:03 +02002071 qp->ibqp.qp_num, qp->trans_qp.base.mqp.qpn,
Eli Cohena1ab8402016-10-27 16:36:46 +03002072 init_attr->recv_cq ? to_mcq(init_attr->recv_cq)->mcq.cqn : -1,
2073 init_attr->send_cq ? to_mcq(init_attr->send_cq)->mcq.cqn : -1);
Eli Cohene126ba92013-07-07 17:25:49 +03002074
majd@mellanox.com19098df2016-01-14 19:13:03 +02002075 qp->trans_qp.xrcdn = xrcdn;
Eli Cohene126ba92013-07-07 17:25:49 +03002076
2077 break;
2078
Haggai Erand16e91d2016-02-29 15:45:05 +02002079 case IB_QPT_GSI:
2080 return mlx5_ib_gsi_create_qp(pd, init_attr);
2081
Eli Cohene126ba92013-07-07 17:25:49 +03002082 case IB_QPT_RAW_IPV6:
2083 case IB_QPT_RAW_ETHERTYPE:
Eli Cohene126ba92013-07-07 17:25:49 +03002084 case IB_QPT_MAX:
2085 default:
2086 mlx5_ib_dbg(dev, "unsupported qp type %d\n",
2087 init_attr->qp_type);
2088 /* Don't support raw QPs */
2089 return ERR_PTR(-EINVAL);
2090 }
2091
2092 return &qp->ibqp;
2093}
2094
2095int mlx5_ib_destroy_qp(struct ib_qp *qp)
2096{
2097 struct mlx5_ib_dev *dev = to_mdev(qp->device);
2098 struct mlx5_ib_qp *mqp = to_mqp(qp);
2099
Haggai Erand16e91d2016-02-29 15:45:05 +02002100 if (unlikely(qp->qp_type == IB_QPT_GSI))
2101 return mlx5_ib_gsi_destroy_qp(qp);
2102
Eli Cohene126ba92013-07-07 17:25:49 +03002103 destroy_qp_common(dev, mqp);
2104
2105 kfree(mqp);
2106
2107 return 0;
2108}
2109
2110static __be32 to_mlx5_access_flags(struct mlx5_ib_qp *qp, const struct ib_qp_attr *attr,
2111 int attr_mask)
2112{
2113 u32 hw_access_flags = 0;
2114 u8 dest_rd_atomic;
2115 u32 access_flags;
2116
2117 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
2118 dest_rd_atomic = attr->max_dest_rd_atomic;
2119 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002120 dest_rd_atomic = qp->trans_qp.resp_depth;
Eli Cohene126ba92013-07-07 17:25:49 +03002121
2122 if (attr_mask & IB_QP_ACCESS_FLAGS)
2123 access_flags = attr->qp_access_flags;
2124 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002125 access_flags = qp->trans_qp.atomic_rd_en;
Eli Cohene126ba92013-07-07 17:25:49 +03002126
2127 if (!dest_rd_atomic)
2128 access_flags &= IB_ACCESS_REMOTE_WRITE;
2129
2130 if (access_flags & IB_ACCESS_REMOTE_READ)
2131 hw_access_flags |= MLX5_QP_BIT_RRE;
2132 if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
2133 hw_access_flags |= (MLX5_QP_BIT_RAE | MLX5_ATOMIC_MODE_CX);
2134 if (access_flags & IB_ACCESS_REMOTE_WRITE)
2135 hw_access_flags |= MLX5_QP_BIT_RWE;
2136
2137 return cpu_to_be32(hw_access_flags);
2138}
2139
2140enum {
2141 MLX5_PATH_FLAG_FL = 1 << 0,
2142 MLX5_PATH_FLAG_FREE_AR = 1 << 1,
2143 MLX5_PATH_FLAG_COUNTER = 1 << 2,
2144};
2145
2146static int ib_rate_to_mlx5(struct mlx5_ib_dev *dev, u8 rate)
2147{
2148 if (rate == IB_RATE_PORT_CURRENT) {
2149 return 0;
2150 } else if (rate < IB_RATE_2_5_GBPS || rate > IB_RATE_300_GBPS) {
2151 return -EINVAL;
2152 } else {
2153 while (rate != IB_RATE_2_5_GBPS &&
2154 !(1 << (rate + MLX5_STAT_RATE_OFFSET) &
Saeed Mahameed938fe832015-05-28 22:28:41 +03002155 MLX5_CAP_GEN(dev->mdev, stat_rate_support)))
Eli Cohene126ba92013-07-07 17:25:49 +03002156 --rate;
2157 }
2158
2159 return rate + MLX5_STAT_RATE_OFFSET;
2160}
2161
majd@mellanox.com75850d02016-01-14 19:13:06 +02002162static int modify_raw_packet_eth_prio(struct mlx5_core_dev *dev,
2163 struct mlx5_ib_sq *sq, u8 sl)
2164{
2165 void *in;
2166 void *tisc;
2167 int inlen;
2168 int err;
2169
2170 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
2171 in = mlx5_vzalloc(inlen);
2172 if (!in)
2173 return -ENOMEM;
2174
2175 MLX5_SET(modify_tis_in, in, bitmask.prio, 1);
2176
2177 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2178 MLX5_SET(tisc, tisc, prio, ((sl & 0x7) << 1));
2179
2180 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2181
2182 kvfree(in);
2183
2184 return err;
2185}
2186
Aviv Heller13eab212016-09-18 20:48:04 +03002187static int modify_raw_packet_tx_affinity(struct mlx5_core_dev *dev,
2188 struct mlx5_ib_sq *sq, u8 tx_affinity)
2189{
2190 void *in;
2191 void *tisc;
2192 int inlen;
2193 int err;
2194
2195 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
2196 in = mlx5_vzalloc(inlen);
2197 if (!in)
2198 return -ENOMEM;
2199
2200 MLX5_SET(modify_tis_in, in, bitmask.lag_tx_port_affinity, 1);
2201
2202 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2203 MLX5_SET(tisc, tisc, lag_tx_port_affinity, tx_affinity);
2204
2205 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2206
2207 kvfree(in);
2208
2209 return err;
2210}
2211
majd@mellanox.com75850d02016-01-14 19:13:06 +02002212static int mlx5_set_path(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
2213 const struct ib_ah_attr *ah,
Eli Cohene126ba92013-07-07 17:25:49 +03002214 struct mlx5_qp_path *path, u8 port, int attr_mask,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002215 u32 path_flags, const struct ib_qp_attr *attr,
2216 bool alt)
Eli Cohene126ba92013-07-07 17:25:49 +03002217{
Achiad Shochat2811ba52015-12-23 18:47:24 +02002218 enum rdma_link_layer ll = rdma_port_get_link_layer(&dev->ib_dev, port);
Eli Cohene126ba92013-07-07 17:25:49 +03002219 int err;
2220
Eli Cohene126ba92013-07-07 17:25:49 +03002221 if (attr_mask & IB_QP_PKEY_INDEX)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002222 path->pkey_index = cpu_to_be16(alt ? attr->alt_pkey_index :
2223 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002224
Eli Cohene126ba92013-07-07 17:25:49 +03002225 if (ah->ah_flags & IB_AH_GRH) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03002226 if (ah->grh.sgid_index >=
2227 dev->mdev->port_caps[port - 1].gid_table_len) {
Joe Perchesf4f01b52015-05-08 15:58:07 -07002228 pr_err("sgid_index (%u) too large. max is %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +03002229 ah->grh.sgid_index,
2230 dev->mdev->port_caps[port - 1].gid_table_len);
Eli Cohenf83b4262014-09-14 16:47:54 +03002231 return -EINVAL;
2232 }
Achiad Shochat2811ba52015-12-23 18:47:24 +02002233 }
2234
2235 if (ll == IB_LINK_LAYER_ETHERNET) {
2236 if (!(ah->ah_flags & IB_AH_GRH))
2237 return -EINVAL;
2238 memcpy(path->rmac, ah->dmac, sizeof(ah->dmac));
2239 path->udp_sport = mlx5_get_roce_udp_sport(dev, port,
2240 ah->grh.sgid_index);
2241 path->dci_cfi_prio_sl = (ah->sl & 0x7) << 4;
2242 } else {
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03002243 path->fl_free_ar = (path_flags & MLX5_PATH_FLAG_FL) ? 0x80 : 0;
2244 path->fl_free_ar |=
2245 (path_flags & MLX5_PATH_FLAG_FREE_AR) ? 0x40 : 0;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002246 path->rlid = cpu_to_be16(ah->dlid);
2247 path->grh_mlid = ah->src_path_bits & 0x7f;
2248 if (ah->ah_flags & IB_AH_GRH)
2249 path->grh_mlid |= 1 << 7;
2250 path->dci_cfi_prio_sl = ah->sl & 0xf;
2251 }
2252
2253 if (ah->ah_flags & IB_AH_GRH) {
Eli Cohene126ba92013-07-07 17:25:49 +03002254 path->mgid_index = ah->grh.sgid_index;
2255 path->hop_limit = ah->grh.hop_limit;
2256 path->tclass_flowlabel =
2257 cpu_to_be32((ah->grh.traffic_class << 20) |
2258 (ah->grh.flow_label));
2259 memcpy(path->rgid, ah->grh.dgid.raw, 16);
2260 }
2261
2262 err = ib_rate_to_mlx5(dev, ah->static_rate);
2263 if (err < 0)
2264 return err;
2265 path->static_rate = err;
2266 path->port = port;
2267
Eli Cohene126ba92013-07-07 17:25:49 +03002268 if (attr_mask & IB_QP_TIMEOUT)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002269 path->ackto_lt = (alt ? attr->alt_timeout : attr->timeout) << 3;
Eli Cohene126ba92013-07-07 17:25:49 +03002270
majd@mellanox.com75850d02016-01-14 19:13:06 +02002271 if ((qp->ibqp.qp_type == IB_QPT_RAW_PACKET) && qp->sq.wqe_cnt)
2272 return modify_raw_packet_eth_prio(dev->mdev,
2273 &qp->raw_packet_qp.sq,
2274 ah->sl & 0xf);
2275
Eli Cohene126ba92013-07-07 17:25:49 +03002276 return 0;
2277}
2278
2279static enum mlx5_qp_optpar opt_mask[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE][MLX5_QP_ST_MAX] = {
2280 [MLX5_QP_STATE_INIT] = {
2281 [MLX5_QP_STATE_INIT] = {
2282 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2283 MLX5_QP_OPTPAR_RAE |
2284 MLX5_QP_OPTPAR_RWE |
2285 MLX5_QP_OPTPAR_PKEY_INDEX |
2286 MLX5_QP_OPTPAR_PRI_PORT,
2287 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
2288 MLX5_QP_OPTPAR_PKEY_INDEX |
2289 MLX5_QP_OPTPAR_PRI_PORT,
2290 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2291 MLX5_QP_OPTPAR_Q_KEY |
2292 MLX5_QP_OPTPAR_PRI_PORT,
2293 },
2294 [MLX5_QP_STATE_RTR] = {
2295 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2296 MLX5_QP_OPTPAR_RRE |
2297 MLX5_QP_OPTPAR_RAE |
2298 MLX5_QP_OPTPAR_RWE |
2299 MLX5_QP_OPTPAR_PKEY_INDEX,
2300 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2301 MLX5_QP_OPTPAR_RWE |
2302 MLX5_QP_OPTPAR_PKEY_INDEX,
2303 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2304 MLX5_QP_OPTPAR_Q_KEY,
2305 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_PKEY_INDEX |
2306 MLX5_QP_OPTPAR_Q_KEY,
Eli Cohena4774e92013-09-11 16:35:32 +03002307 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2308 MLX5_QP_OPTPAR_RRE |
2309 MLX5_QP_OPTPAR_RAE |
2310 MLX5_QP_OPTPAR_RWE |
2311 MLX5_QP_OPTPAR_PKEY_INDEX,
Eli Cohene126ba92013-07-07 17:25:49 +03002312 },
2313 },
2314 [MLX5_QP_STATE_RTR] = {
2315 [MLX5_QP_STATE_RTS] = {
2316 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2317 MLX5_QP_OPTPAR_RRE |
2318 MLX5_QP_OPTPAR_RAE |
2319 MLX5_QP_OPTPAR_RWE |
2320 MLX5_QP_OPTPAR_PM_STATE |
2321 MLX5_QP_OPTPAR_RNR_TIMEOUT,
2322 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2323 MLX5_QP_OPTPAR_RWE |
2324 MLX5_QP_OPTPAR_PM_STATE,
2325 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
2326 },
2327 },
2328 [MLX5_QP_STATE_RTS] = {
2329 [MLX5_QP_STATE_RTS] = {
2330 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2331 MLX5_QP_OPTPAR_RAE |
2332 MLX5_QP_OPTPAR_RWE |
2333 MLX5_QP_OPTPAR_RNR_TIMEOUT |
Eli Cohenc2a34312013-10-24 12:01:02 +03002334 MLX5_QP_OPTPAR_PM_STATE |
2335 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03002336 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
Eli Cohenc2a34312013-10-24 12:01:02 +03002337 MLX5_QP_OPTPAR_PM_STATE |
2338 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03002339 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY |
2340 MLX5_QP_OPTPAR_SRQN |
2341 MLX5_QP_OPTPAR_CQN_RCV,
2342 },
2343 },
2344 [MLX5_QP_STATE_SQER] = {
2345 [MLX5_QP_STATE_RTS] = {
2346 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
2347 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_Q_KEY,
Eli Cohen75959f52013-09-11 16:35:31 +03002348 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE,
Eli Cohena4774e92013-09-11 16:35:32 +03002349 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
2350 MLX5_QP_OPTPAR_RWE |
2351 MLX5_QP_OPTPAR_RAE |
2352 MLX5_QP_OPTPAR_RRE,
Eli Cohene126ba92013-07-07 17:25:49 +03002353 },
2354 },
2355};
2356
2357static int ib_nr_to_mlx5_nr(int ib_mask)
2358{
2359 switch (ib_mask) {
2360 case IB_QP_STATE:
2361 return 0;
2362 case IB_QP_CUR_STATE:
2363 return 0;
2364 case IB_QP_EN_SQD_ASYNC_NOTIFY:
2365 return 0;
2366 case IB_QP_ACCESS_FLAGS:
2367 return MLX5_QP_OPTPAR_RWE | MLX5_QP_OPTPAR_RRE |
2368 MLX5_QP_OPTPAR_RAE;
2369 case IB_QP_PKEY_INDEX:
2370 return MLX5_QP_OPTPAR_PKEY_INDEX;
2371 case IB_QP_PORT:
2372 return MLX5_QP_OPTPAR_PRI_PORT;
2373 case IB_QP_QKEY:
2374 return MLX5_QP_OPTPAR_Q_KEY;
2375 case IB_QP_AV:
2376 return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH |
2377 MLX5_QP_OPTPAR_PRI_PORT;
2378 case IB_QP_PATH_MTU:
2379 return 0;
2380 case IB_QP_TIMEOUT:
2381 return MLX5_QP_OPTPAR_ACK_TIMEOUT;
2382 case IB_QP_RETRY_CNT:
2383 return MLX5_QP_OPTPAR_RETRY_COUNT;
2384 case IB_QP_RNR_RETRY:
2385 return MLX5_QP_OPTPAR_RNR_RETRY;
2386 case IB_QP_RQ_PSN:
2387 return 0;
2388 case IB_QP_MAX_QP_RD_ATOMIC:
2389 return MLX5_QP_OPTPAR_SRA_MAX;
2390 case IB_QP_ALT_PATH:
2391 return MLX5_QP_OPTPAR_ALT_ADDR_PATH;
2392 case IB_QP_MIN_RNR_TIMER:
2393 return MLX5_QP_OPTPAR_RNR_TIMEOUT;
2394 case IB_QP_SQ_PSN:
2395 return 0;
2396 case IB_QP_MAX_DEST_RD_ATOMIC:
2397 return MLX5_QP_OPTPAR_RRA_MAX | MLX5_QP_OPTPAR_RWE |
2398 MLX5_QP_OPTPAR_RRE | MLX5_QP_OPTPAR_RAE;
2399 case IB_QP_PATH_MIG_STATE:
2400 return MLX5_QP_OPTPAR_PM_STATE;
2401 case IB_QP_CAP:
2402 return 0;
2403 case IB_QP_DEST_QPN:
2404 return 0;
2405 }
2406 return 0;
2407}
2408
2409static int ib_mask_to_mlx5_opt(int ib_mask)
2410{
2411 int result = 0;
2412 int i;
2413
2414 for (i = 0; i < 8 * sizeof(int); i++) {
2415 if ((1 << i) & ib_mask)
2416 result |= ib_nr_to_mlx5_nr(1 << i);
2417 }
2418
2419 return result;
2420}
2421
Alex Veskereb49ab02016-08-28 12:25:53 +03002422static int modify_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
2423 struct mlx5_ib_rq *rq, int new_state,
2424 const struct mlx5_modify_raw_qp_param *raw_qp_param)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002425{
2426 void *in;
2427 void *rqc;
2428 int inlen;
2429 int err;
2430
2431 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
2432 in = mlx5_vzalloc(inlen);
2433 if (!in)
2434 return -ENOMEM;
2435
2436 MLX5_SET(modify_rq_in, in, rq_state, rq->state);
2437
2438 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
2439 MLX5_SET(rqc, rqc, state, new_state);
2440
Alex Veskereb49ab02016-08-28 12:25:53 +03002441 if (raw_qp_param->set_mask & MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID) {
2442 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
2443 MLX5_SET64(modify_rq_in, in, modify_bitmask,
2444 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_MODIFY_RQ_COUNTER_SET_ID);
2445 MLX5_SET(rqc, rqc, counter_set_id, raw_qp_param->rq_q_ctr_id);
2446 } else
2447 pr_info_once("%s: RAW PACKET QP counters are not supported on current FW\n",
2448 dev->ib_dev.name);
2449 }
2450
2451 err = mlx5_core_modify_rq(dev->mdev, rq->base.mqp.qpn, in, inlen);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002452 if (err)
2453 goto out;
2454
2455 rq->state = new_state;
2456
2457out:
2458 kvfree(in);
2459 return err;
2460}
2461
2462static int modify_raw_packet_qp_sq(struct mlx5_core_dev *dev,
Bodong Wang7d29f342016-12-01 13:43:16 +02002463 struct mlx5_ib_sq *sq,
2464 int new_state,
2465 const struct mlx5_modify_raw_qp_param *raw_qp_param)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002466{
Bodong Wang7d29f342016-12-01 13:43:16 +02002467 struct mlx5_ib_qp *ibqp = sq->base.container_mibqp;
2468 u32 old_rate = ibqp->rate_limit;
2469 u32 new_rate = old_rate;
2470 u16 rl_index = 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002471 void *in;
2472 void *sqc;
2473 int inlen;
2474 int err;
2475
2476 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
2477 in = mlx5_vzalloc(inlen);
2478 if (!in)
2479 return -ENOMEM;
2480
2481 MLX5_SET(modify_sq_in, in, sq_state, sq->state);
2482
2483 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
2484 MLX5_SET(sqc, sqc, state, new_state);
2485
Bodong Wang7d29f342016-12-01 13:43:16 +02002486 if (raw_qp_param->set_mask & MLX5_RAW_QP_RATE_LIMIT) {
2487 if (new_state != MLX5_SQC_STATE_RDY)
2488 pr_warn("%s: Rate limit can only be changed when SQ is moving to RDY\n",
2489 __func__);
2490 else
2491 new_rate = raw_qp_param->rate_limit;
2492 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002493
Bodong Wang7d29f342016-12-01 13:43:16 +02002494 if (old_rate != new_rate) {
2495 if (new_rate) {
2496 err = mlx5_rl_add_rate(dev, new_rate, &rl_index);
2497 if (err) {
2498 pr_err("Failed configuring rate %u: %d\n",
2499 new_rate, err);
2500 goto out;
2501 }
2502 }
2503
2504 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
2505 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
2506 }
2507
2508 err = mlx5_core_modify_sq(dev, sq->base.mqp.qpn, in, inlen);
2509 if (err) {
2510 /* Remove new rate from table if failed */
2511 if (new_rate &&
2512 old_rate != new_rate)
2513 mlx5_rl_remove_rate(dev, new_rate);
2514 goto out;
2515 }
2516
2517 /* Only remove the old rate after new rate was set */
2518 if ((old_rate &&
2519 (old_rate != new_rate)) ||
2520 (new_state != MLX5_SQC_STATE_RDY))
2521 mlx5_rl_remove_rate(dev, old_rate);
2522
2523 ibqp->rate_limit = new_rate;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002524 sq->state = new_state;
2525
2526out:
2527 kvfree(in);
2528 return err;
2529}
2530
2531static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03002532 const struct mlx5_modify_raw_qp_param *raw_qp_param,
2533 u8 tx_affinity)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002534{
2535 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
2536 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
2537 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
Bodong Wang7d29f342016-12-01 13:43:16 +02002538 int modify_rq = !!qp->rq.wqe_cnt;
2539 int modify_sq = !!qp->sq.wqe_cnt;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002540 int rq_state;
2541 int sq_state;
2542 int err;
2543
Alex Vesker0680efa2016-08-28 12:25:52 +03002544 switch (raw_qp_param->operation) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002545 case MLX5_CMD_OP_RST2INIT_QP:
2546 rq_state = MLX5_RQC_STATE_RDY;
2547 sq_state = MLX5_SQC_STATE_RDY;
2548 break;
2549 case MLX5_CMD_OP_2ERR_QP:
2550 rq_state = MLX5_RQC_STATE_ERR;
2551 sq_state = MLX5_SQC_STATE_ERR;
2552 break;
2553 case MLX5_CMD_OP_2RST_QP:
2554 rq_state = MLX5_RQC_STATE_RST;
2555 sq_state = MLX5_SQC_STATE_RST;
2556 break;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002557 case MLX5_CMD_OP_RTR2RTS_QP:
2558 case MLX5_CMD_OP_RTS2RTS_QP:
Bodong Wang7d29f342016-12-01 13:43:16 +02002559 if (raw_qp_param->set_mask ==
2560 MLX5_RAW_QP_RATE_LIMIT) {
2561 modify_rq = 0;
2562 sq_state = sq->state;
2563 } else {
2564 return raw_qp_param->set_mask ? -EINVAL : 0;
2565 }
2566 break;
2567 case MLX5_CMD_OP_INIT2INIT_QP:
2568 case MLX5_CMD_OP_INIT2RTR_QP:
Alex Veskereb49ab02016-08-28 12:25:53 +03002569 if (raw_qp_param->set_mask)
2570 return -EINVAL;
2571 else
2572 return 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002573 default:
2574 WARN_ON(1);
2575 return -EINVAL;
2576 }
2577
Bodong Wang7d29f342016-12-01 13:43:16 +02002578 if (modify_rq) {
2579 err = modify_raw_packet_qp_rq(dev, rq, rq_state, raw_qp_param);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002580 if (err)
2581 return err;
2582 }
2583
Bodong Wang7d29f342016-12-01 13:43:16 +02002584 if (modify_sq) {
Aviv Heller13eab212016-09-18 20:48:04 +03002585 if (tx_affinity) {
2586 err = modify_raw_packet_tx_affinity(dev->mdev, sq,
2587 tx_affinity);
2588 if (err)
2589 return err;
2590 }
2591
Bodong Wang7d29f342016-12-01 13:43:16 +02002592 return modify_raw_packet_qp_sq(dev->mdev, sq, sq_state, raw_qp_param);
Aviv Heller13eab212016-09-18 20:48:04 +03002593 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002594
2595 return 0;
2596}
2597
Eli Cohene126ba92013-07-07 17:25:49 +03002598static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
2599 const struct ib_qp_attr *attr, int attr_mask,
2600 enum ib_qp_state cur_state, enum ib_qp_state new_state)
2601{
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002602 static const u16 optab[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE] = {
2603 [MLX5_QP_STATE_RST] = {
2604 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2605 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2606 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_RST2INIT_QP,
2607 },
2608 [MLX5_QP_STATE_INIT] = {
2609 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2610 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2611 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_INIT2INIT_QP,
2612 [MLX5_QP_STATE_RTR] = MLX5_CMD_OP_INIT2RTR_QP,
2613 },
2614 [MLX5_QP_STATE_RTR] = {
2615 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2616 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2617 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTR2RTS_QP,
2618 },
2619 [MLX5_QP_STATE_RTS] = {
2620 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2621 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2622 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTS2RTS_QP,
2623 },
2624 [MLX5_QP_STATE_SQD] = {
2625 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2626 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2627 },
2628 [MLX5_QP_STATE_SQER] = {
2629 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2630 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2631 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_SQERR2RTS_QP,
2632 },
2633 [MLX5_QP_STATE_ERR] = {
2634 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2635 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2636 }
2637 };
2638
Eli Cohene126ba92013-07-07 17:25:49 +03002639 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2640 struct mlx5_ib_qp *qp = to_mqp(ibqp);
majd@mellanox.com19098df2016-01-14 19:13:03 +02002641 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Eli Cohene126ba92013-07-07 17:25:49 +03002642 struct mlx5_ib_cq *send_cq, *recv_cq;
2643 struct mlx5_qp_context *context;
Eli Cohene126ba92013-07-07 17:25:49 +03002644 struct mlx5_ib_pd *pd;
Alex Veskereb49ab02016-08-28 12:25:53 +03002645 struct mlx5_ib_port *mibport = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002646 enum mlx5_qp_state mlx5_cur, mlx5_new;
2647 enum mlx5_qp_optpar optpar;
Eli Cohene126ba92013-07-07 17:25:49 +03002648 int mlx5_st;
2649 int err;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002650 u16 op;
Aviv Heller13eab212016-09-18 20:48:04 +03002651 u8 tx_affinity = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002652
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002653 context = kzalloc(sizeof(*context), GFP_KERNEL);
2654 if (!context)
Eli Cohene126ba92013-07-07 17:25:49 +03002655 return -ENOMEM;
2656
Eli Cohene126ba92013-07-07 17:25:49 +03002657 err = to_mlx5_st(ibqp->qp_type);
Haggai Eran158abf82016-02-29 15:45:04 +02002658 if (err < 0) {
2659 mlx5_ib_dbg(dev, "unsupported qp type %d\n", ibqp->qp_type);
Eli Cohene126ba92013-07-07 17:25:49 +03002660 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002661 }
Eli Cohene126ba92013-07-07 17:25:49 +03002662
2663 context->flags = cpu_to_be32(err << 16);
2664
2665 if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
2666 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
2667 } else {
2668 switch (attr->path_mig_state) {
2669 case IB_MIG_MIGRATED:
2670 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
2671 break;
2672 case IB_MIG_REARM:
2673 context->flags |= cpu_to_be32(MLX5_QP_PM_REARM << 11);
2674 break;
2675 case IB_MIG_ARMED:
2676 context->flags |= cpu_to_be32(MLX5_QP_PM_ARMED << 11);
2677 break;
2678 }
2679 }
2680
Aviv Heller13eab212016-09-18 20:48:04 +03002681 if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
2682 if ((ibqp->qp_type == IB_QPT_RC) ||
2683 (ibqp->qp_type == IB_QPT_UD &&
2684 !(qp->flags & MLX5_IB_QP_SQPN_QP1)) ||
2685 (ibqp->qp_type == IB_QPT_UC) ||
2686 (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
2687 (ibqp->qp_type == IB_QPT_XRC_INI) ||
2688 (ibqp->qp_type == IB_QPT_XRC_TGT)) {
2689 if (mlx5_lag_is_active(dev->mdev)) {
2690 tx_affinity = (unsigned int)atomic_add_return(1,
2691 &dev->roce.next_port) %
2692 MLX5_MAX_PORTS + 1;
2693 context->flags |= cpu_to_be32(tx_affinity << 24);
2694 }
2695 }
2696 }
2697
Haggai Erand16e91d2016-02-29 15:45:05 +02002698 if (is_sqp(ibqp->qp_type)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002699 context->mtu_msgmax = (IB_MTU_256 << 5) | 8;
2700 } else if (ibqp->qp_type == IB_QPT_UD ||
2701 ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
2702 context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
2703 } else if (attr_mask & IB_QP_PATH_MTU) {
2704 if (attr->path_mtu < IB_MTU_256 ||
2705 attr->path_mtu > IB_MTU_4096) {
2706 mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
2707 err = -EINVAL;
2708 goto out;
2709 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03002710 context->mtu_msgmax = (attr->path_mtu << 5) |
2711 (u8)MLX5_CAP_GEN(dev->mdev, log_max_msg);
Eli Cohene126ba92013-07-07 17:25:49 +03002712 }
2713
2714 if (attr_mask & IB_QP_DEST_QPN)
2715 context->log_pg_sz_remote_qpn = cpu_to_be32(attr->dest_qp_num);
2716
2717 if (attr_mask & IB_QP_PKEY_INDEX)
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03002718 context->pri_path.pkey_index = cpu_to_be16(attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002719
2720 /* todo implement counter_index functionality */
2721
2722 if (is_sqp(ibqp->qp_type))
2723 context->pri_path.port = qp->port;
2724
2725 if (attr_mask & IB_QP_PORT)
2726 context->pri_path.port = attr->port_num;
2727
2728 if (attr_mask & IB_QP_AV) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02002729 err = mlx5_set_path(dev, qp, &attr->ah_attr, &context->pri_path,
Eli Cohene126ba92013-07-07 17:25:49 +03002730 attr_mask & IB_QP_PORT ? attr->port_num : qp->port,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002731 attr_mask, 0, attr, false);
Eli Cohene126ba92013-07-07 17:25:49 +03002732 if (err)
2733 goto out;
2734 }
2735
2736 if (attr_mask & IB_QP_TIMEOUT)
2737 context->pri_path.ackto_lt |= attr->timeout << 3;
2738
2739 if (attr_mask & IB_QP_ALT_PATH) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02002740 err = mlx5_set_path(dev, qp, &attr->alt_ah_attr,
2741 &context->alt_path,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002742 attr->alt_port_num,
2743 attr_mask | IB_QP_PKEY_INDEX | IB_QP_TIMEOUT,
2744 0, attr, true);
Eli Cohene126ba92013-07-07 17:25:49 +03002745 if (err)
2746 goto out;
2747 }
2748
2749 pd = get_pd(qp);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002750 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
2751 &send_cq, &recv_cq);
Eli Cohene126ba92013-07-07 17:25:49 +03002752
2753 context->flags_pd = cpu_to_be32(pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
2754 context->cqn_send = send_cq ? cpu_to_be32(send_cq->mcq.cqn) : 0;
2755 context->cqn_recv = recv_cq ? cpu_to_be32(recv_cq->mcq.cqn) : 0;
2756 context->params1 = cpu_to_be32(MLX5_IB_ACK_REQ_FREQ << 28);
2757
2758 if (attr_mask & IB_QP_RNR_RETRY)
2759 context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
2760
2761 if (attr_mask & IB_QP_RETRY_CNT)
2762 context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
2763
2764 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
2765 if (attr->max_rd_atomic)
2766 context->params1 |=
2767 cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
2768 }
2769
2770 if (attr_mask & IB_QP_SQ_PSN)
2771 context->next_send_psn = cpu_to_be32(attr->sq_psn);
2772
2773 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
2774 if (attr->max_dest_rd_atomic)
2775 context->params2 |=
2776 cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
2777 }
2778
2779 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC))
2780 context->params2 |= to_mlx5_access_flags(qp, attr, attr_mask);
2781
2782 if (attr_mask & IB_QP_MIN_RNR_TIMER)
2783 context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
2784
2785 if (attr_mask & IB_QP_RQ_PSN)
2786 context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
2787
2788 if (attr_mask & IB_QP_QKEY)
2789 context->qkey = cpu_to_be32(attr->qkey);
2790
2791 if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
2792 context->db_rec_addr = cpu_to_be64(qp->db.dma);
2793
Mark Bloch0837e862016-06-17 15:10:55 +03002794 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
2795 u8 port_num = (attr_mask & IB_QP_PORT ? attr->port_num :
2796 qp->port) - 1;
Alex Veskereb49ab02016-08-28 12:25:53 +03002797 mibport = &dev->port[port_num];
Mark Bloch0837e862016-06-17 15:10:55 +03002798 context->qp_counter_set_usr_page |=
Alex Vesker321a9e32016-07-13 16:25:11 +03002799 cpu_to_be32((u32)(mibport->q_cnt_id) << 24);
Mark Bloch0837e862016-06-17 15:10:55 +03002800 }
2801
Eli Cohene126ba92013-07-07 17:25:49 +03002802 if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
2803 context->sq_crq_size |= cpu_to_be16(1 << 4);
2804
Haggai Eranb11a4f92016-02-29 15:45:03 +02002805 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
2806 context->deth_sqpn = cpu_to_be32(1);
Eli Cohene126ba92013-07-07 17:25:49 +03002807
2808 mlx5_cur = to_mlx5_state(cur_state);
2809 mlx5_new = to_mlx5_state(new_state);
2810 mlx5_st = to_mlx5_st(ibqp->qp_type);
Eli Cohen07c91132013-10-24 12:01:01 +03002811 if (mlx5_st < 0)
Eli Cohene126ba92013-07-07 17:25:49 +03002812 goto out;
2813
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002814 if (mlx5_cur >= MLX5_QP_NUM_STATE || mlx5_new >= MLX5_QP_NUM_STATE ||
2815 !optab[mlx5_cur][mlx5_new])
2816 goto out;
2817
2818 op = optab[mlx5_cur][mlx5_new];
Eli Cohene126ba92013-07-07 17:25:49 +03002819 optpar = ib_mask_to_mlx5_opt(attr_mask);
2820 optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002821
Alex Vesker0680efa2016-08-28 12:25:52 +03002822 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET) {
2823 struct mlx5_modify_raw_qp_param raw_qp_param = {};
2824
2825 raw_qp_param.operation = op;
Alex Veskereb49ab02016-08-28 12:25:53 +03002826 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
2827 raw_qp_param.rq_q_ctr_id = mibport->q_cnt_id;
2828 raw_qp_param.set_mask |= MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID;
2829 }
Bodong Wang7d29f342016-12-01 13:43:16 +02002830
2831 if (attr_mask & IB_QP_RATE_LIMIT) {
2832 raw_qp_param.rate_limit = attr->rate_limit;
2833 raw_qp_param.set_mask |= MLX5_RAW_QP_RATE_LIMIT;
2834 }
2835
Aviv Heller13eab212016-09-18 20:48:04 +03002836 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, tx_affinity);
Alex Vesker0680efa2016-08-28 12:25:52 +03002837 } else {
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002838 err = mlx5_core_qp_modify(dev->mdev, op, optpar, context,
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002839 &base->mqp);
Alex Vesker0680efa2016-08-28 12:25:52 +03002840 }
2841
Eli Cohene126ba92013-07-07 17:25:49 +03002842 if (err)
2843 goto out;
2844
2845 qp->state = new_state;
2846
2847 if (attr_mask & IB_QP_ACCESS_FLAGS)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002848 qp->trans_qp.atomic_rd_en = attr->qp_access_flags;
Eli Cohene126ba92013-07-07 17:25:49 +03002849 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002850 qp->trans_qp.resp_depth = attr->max_dest_rd_atomic;
Eli Cohene126ba92013-07-07 17:25:49 +03002851 if (attr_mask & IB_QP_PORT)
2852 qp->port = attr->port_num;
2853 if (attr_mask & IB_QP_ALT_PATH)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002854 qp->trans_qp.alt_port = attr->alt_port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03002855
2856 /*
2857 * If we moved a kernel QP to RESET, clean up all old CQ
2858 * entries and reinitialize the QP.
2859 */
2860 if (new_state == IB_QPS_RESET && !ibqp->uobject) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002861 mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03002862 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
2863 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002864 mlx5_ib_cq_clean(send_cq, base->mqp.qpn, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002865
2866 qp->rq.head = 0;
2867 qp->rq.tail = 0;
2868 qp->sq.head = 0;
2869 qp->sq.tail = 0;
2870 qp->sq.cur_post = 0;
2871 qp->sq.last_poll = 0;
2872 qp->db.db[MLX5_RCV_DBR] = 0;
2873 qp->db.db[MLX5_SND_DBR] = 0;
2874 }
2875
2876out:
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002877 kfree(context);
Eli Cohene126ba92013-07-07 17:25:49 +03002878 return err;
2879}
2880
2881int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2882 int attr_mask, struct ib_udata *udata)
2883{
2884 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2885 struct mlx5_ib_qp *qp = to_mqp(ibqp);
Haggai Erand16e91d2016-02-29 15:45:05 +02002886 enum ib_qp_type qp_type;
Eli Cohene126ba92013-07-07 17:25:49 +03002887 enum ib_qp_state cur_state, new_state;
2888 int err = -EINVAL;
2889 int port;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002890 enum rdma_link_layer ll = IB_LINK_LAYER_UNSPECIFIED;
Eli Cohene126ba92013-07-07 17:25:49 +03002891
Yishai Hadas28d61372016-05-23 15:20:56 +03002892 if (ibqp->rwq_ind_tbl)
2893 return -ENOSYS;
2894
Haggai Erand16e91d2016-02-29 15:45:05 +02002895 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
2896 return mlx5_ib_gsi_modify_qp(ibqp, attr, attr_mask);
2897
2898 qp_type = (unlikely(ibqp->qp_type == MLX5_IB_QPT_HW_GSI)) ?
2899 IB_QPT_GSI : ibqp->qp_type;
2900
Eli Cohene126ba92013-07-07 17:25:49 +03002901 mutex_lock(&qp->mutex);
2902
2903 cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
2904 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
2905
Achiad Shochat2811ba52015-12-23 18:47:24 +02002906 if (!(cur_state == new_state && cur_state == IB_QPS_RESET)) {
2907 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
2908 ll = dev->ib_dev.get_link_layer(&dev->ib_dev, port);
2909 }
2910
Haggai Erand16e91d2016-02-29 15:45:05 +02002911 if (qp_type != MLX5_IB_QPT_REG_UMR &&
2912 !ib_modify_qp_is_ok(cur_state, new_state, qp_type, attr_mask, ll)) {
Haggai Eran158abf82016-02-29 15:45:04 +02002913 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
2914 cur_state, new_state, ibqp->qp_type, attr_mask);
Eli Cohene126ba92013-07-07 17:25:49 +03002915 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002916 }
Eli Cohene126ba92013-07-07 17:25:49 +03002917
2918 if ((attr_mask & IB_QP_PORT) &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03002919 (attr->port_num == 0 ||
Haggai Eran158abf82016-02-29 15:45:04 +02002920 attr->port_num > MLX5_CAP_GEN(dev->mdev, num_ports))) {
2921 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
2922 attr->port_num, dev->num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03002923 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002924 }
Eli Cohene126ba92013-07-07 17:25:49 +03002925
2926 if (attr_mask & IB_QP_PKEY_INDEX) {
2927 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Saeed Mahameed938fe832015-05-28 22:28:41 +03002928 if (attr->pkey_index >=
Haggai Eran158abf82016-02-29 15:45:04 +02002929 dev->mdev->port_caps[port - 1].pkey_table_len) {
2930 mlx5_ib_dbg(dev, "invalid pkey index %d\n",
2931 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002932 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002933 }
Eli Cohene126ba92013-07-07 17:25:49 +03002934 }
2935
2936 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03002937 attr->max_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02002938 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_res_qp))) {
2939 mlx5_ib_dbg(dev, "invalid max_rd_atomic value %d\n",
2940 attr->max_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03002941 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002942 }
Eli Cohene126ba92013-07-07 17:25:49 +03002943
2944 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03002945 attr->max_dest_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02002946 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_req_qp))) {
2947 mlx5_ib_dbg(dev, "invalid max_dest_rd_atomic value %d\n",
2948 attr->max_dest_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03002949 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002950 }
Eli Cohene126ba92013-07-07 17:25:49 +03002951
2952 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
2953 err = 0;
2954 goto out;
2955 }
2956
2957 err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
2958
2959out:
2960 mutex_unlock(&qp->mutex);
2961 return err;
2962}
2963
2964static int mlx5_wq_overflow(struct mlx5_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
2965{
2966 struct mlx5_ib_cq *cq;
2967 unsigned cur;
2968
2969 cur = wq->head - wq->tail;
2970 if (likely(cur + nreq < wq->max_post))
2971 return 0;
2972
2973 cq = to_mcq(ib_cq);
2974 spin_lock(&cq->lock);
2975 cur = wq->head - wq->tail;
2976 spin_unlock(&cq->lock);
2977
2978 return cur + nreq >= wq->max_post;
2979}
2980
2981static __always_inline void set_raddr_seg(struct mlx5_wqe_raddr_seg *rseg,
2982 u64 remote_addr, u32 rkey)
2983{
2984 rseg->raddr = cpu_to_be64(remote_addr);
2985 rseg->rkey = cpu_to_be32(rkey);
2986 rseg->reserved = 0;
2987}
2988
Erez Shitritf0313962016-02-21 16:27:17 +02002989static void *set_eth_seg(struct mlx5_wqe_eth_seg *eseg,
2990 struct ib_send_wr *wr, void *qend,
2991 struct mlx5_ib_qp *qp, int *size)
2992{
2993 void *seg = eseg;
2994
2995 memset(eseg, 0, sizeof(struct mlx5_wqe_eth_seg));
2996
2997 if (wr->send_flags & IB_SEND_IP_CSUM)
2998 eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM |
2999 MLX5_ETH_WQE_L4_CSUM;
3000
3001 seg += sizeof(struct mlx5_wqe_eth_seg);
3002 *size += sizeof(struct mlx5_wqe_eth_seg) / 16;
3003
3004 if (wr->opcode == IB_WR_LSO) {
3005 struct ib_ud_wr *ud_wr = container_of(wr, struct ib_ud_wr, wr);
3006 int size_of_inl_hdr_start = sizeof(eseg->inline_hdr_start);
3007 u64 left, leftlen, copysz;
3008 void *pdata = ud_wr->header;
3009
3010 left = ud_wr->hlen;
3011 eseg->mss = cpu_to_be16(ud_wr->mss);
3012 eseg->inline_hdr_sz = cpu_to_be16(left);
3013
3014 /*
3015 * check if there is space till the end of queue, if yes,
3016 * copy all in one shot, otherwise copy till the end of queue,
3017 * rollback and than the copy the left
3018 */
3019 leftlen = qend - (void *)eseg->inline_hdr_start;
3020 copysz = min_t(u64, leftlen, left);
3021
3022 memcpy(seg - size_of_inl_hdr_start, pdata, copysz);
3023
3024 if (likely(copysz > size_of_inl_hdr_start)) {
3025 seg += ALIGN(copysz - size_of_inl_hdr_start, 16);
3026 *size += ALIGN(copysz - size_of_inl_hdr_start, 16) / 16;
3027 }
3028
3029 if (unlikely(copysz < left)) { /* the last wqe in the queue */
3030 seg = mlx5_get_send_wqe(qp, 0);
3031 left -= copysz;
3032 pdata += copysz;
3033 memcpy(seg, pdata, left);
3034 seg += ALIGN(left, 16);
3035 *size += ALIGN(left, 16) / 16;
3036 }
3037 }
3038
3039 return seg;
3040}
3041
Eli Cohene126ba92013-07-07 17:25:49 +03003042static void set_datagram_seg(struct mlx5_wqe_datagram_seg *dseg,
3043 struct ib_send_wr *wr)
3044{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003045 memcpy(&dseg->av, &to_mah(ud_wr(wr)->ah)->av, sizeof(struct mlx5_av));
3046 dseg->av.dqp_dct = cpu_to_be32(ud_wr(wr)->remote_qpn | MLX5_EXTENDED_UD_AV);
3047 dseg->av.key.qkey.qkey = cpu_to_be32(ud_wr(wr)->remote_qkey);
Eli Cohene126ba92013-07-07 17:25:49 +03003048}
3049
3050static void set_data_ptr_seg(struct mlx5_wqe_data_seg *dseg, struct ib_sge *sg)
3051{
3052 dseg->byte_count = cpu_to_be32(sg->length);
3053 dseg->lkey = cpu_to_be32(sg->lkey);
3054 dseg->addr = cpu_to_be64(sg->addr);
3055}
3056
Artemy Kovalyov31616252017-01-02 11:37:42 +02003057static u64 get_xlt_octo(u64 bytes)
Eli Cohene126ba92013-07-07 17:25:49 +03003058{
Artemy Kovalyov31616252017-01-02 11:37:42 +02003059 return ALIGN(bytes, MLX5_IB_UMR_XLT_ALIGNMENT) /
3060 MLX5_IB_UMR_OCTOWORD;
Eli Cohene126ba92013-07-07 17:25:49 +03003061}
3062
3063static __be64 frwr_mkey_mask(void)
3064{
3065 u64 result;
3066
3067 result = MLX5_MKEY_MASK_LEN |
3068 MLX5_MKEY_MASK_PAGE_SIZE |
3069 MLX5_MKEY_MASK_START_ADDR |
3070 MLX5_MKEY_MASK_EN_RINVAL |
3071 MLX5_MKEY_MASK_KEY |
3072 MLX5_MKEY_MASK_LR |
3073 MLX5_MKEY_MASK_LW |
3074 MLX5_MKEY_MASK_RR |
3075 MLX5_MKEY_MASK_RW |
3076 MLX5_MKEY_MASK_A |
3077 MLX5_MKEY_MASK_SMALL_FENCE |
3078 MLX5_MKEY_MASK_FREE;
3079
3080 return cpu_to_be64(result);
3081}
3082
Sagi Grimberge6631812014-02-23 14:19:11 +02003083static __be64 sig_mkey_mask(void)
3084{
3085 u64 result;
3086
3087 result = MLX5_MKEY_MASK_LEN |
3088 MLX5_MKEY_MASK_PAGE_SIZE |
3089 MLX5_MKEY_MASK_START_ADDR |
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003090 MLX5_MKEY_MASK_EN_SIGERR |
Sagi Grimberge6631812014-02-23 14:19:11 +02003091 MLX5_MKEY_MASK_EN_RINVAL |
3092 MLX5_MKEY_MASK_KEY |
3093 MLX5_MKEY_MASK_LR |
3094 MLX5_MKEY_MASK_LW |
3095 MLX5_MKEY_MASK_RR |
3096 MLX5_MKEY_MASK_RW |
3097 MLX5_MKEY_MASK_SMALL_FENCE |
3098 MLX5_MKEY_MASK_FREE |
3099 MLX5_MKEY_MASK_BSF_EN;
3100
3101 return cpu_to_be64(result);
3102}
3103
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003104static void set_reg_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02003105 struct mlx5_ib_mr *mr)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003106{
Artemy Kovalyov31616252017-01-02 11:37:42 +02003107 int size = mr->ndescs * mr->desc_size;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003108
3109 memset(umr, 0, sizeof(*umr));
Sagi Grimbergb005d312016-02-29 19:07:33 +02003110
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003111 umr->flags = MLX5_UMR_CHECK_NOT_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02003112 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003113 umr->mkey_mask = frwr_mkey_mask();
3114}
3115
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003116static void set_linv_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr)
Eli Cohene126ba92013-07-07 17:25:49 +03003117{
3118 memset(umr, 0, sizeof(*umr));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003119 umr->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
Max Gurtovoy2d221582016-10-27 16:36:36 +03003120 umr->flags = MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03003121}
3122
Artemy Kovalyov31616252017-01-02 11:37:42 +02003123static __be64 get_umr_enable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02003124{
3125 u64 result;
3126
Artemy Kovalyov31616252017-01-02 11:37:42 +02003127 result = MLX5_MKEY_MASK_KEY |
Haggai Eran968e78d2014-12-11 17:04:11 +02003128 MLX5_MKEY_MASK_FREE;
3129
3130 return cpu_to_be64(result);
3131}
3132
Artemy Kovalyov31616252017-01-02 11:37:42 +02003133static __be64 get_umr_disable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02003134{
3135 u64 result;
3136
3137 result = MLX5_MKEY_MASK_FREE;
3138
3139 return cpu_to_be64(result);
3140}
3141
Noa Osherovich56e11d62016-02-29 16:46:51 +02003142static __be64 get_umr_update_translation_mask(void)
3143{
3144 u64 result;
3145
3146 result = MLX5_MKEY_MASK_LEN |
3147 MLX5_MKEY_MASK_PAGE_SIZE |
Artemy Kovalyov31616252017-01-02 11:37:42 +02003148 MLX5_MKEY_MASK_START_ADDR;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003149
3150 return cpu_to_be64(result);
3151}
3152
Artemy Kovalyov31616252017-01-02 11:37:42 +02003153static __be64 get_umr_update_access_mask(int atomic)
Noa Osherovich56e11d62016-02-29 16:46:51 +02003154{
3155 u64 result;
3156
Artemy Kovalyov31616252017-01-02 11:37:42 +02003157 result = MLX5_MKEY_MASK_LR |
3158 MLX5_MKEY_MASK_LW |
Noa Osherovich56e11d62016-02-29 16:46:51 +02003159 MLX5_MKEY_MASK_RR |
Artemy Kovalyov31616252017-01-02 11:37:42 +02003160 MLX5_MKEY_MASK_RW;
3161
3162 if (atomic)
3163 result |= MLX5_MKEY_MASK_A;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003164
3165 return cpu_to_be64(result);
3166}
3167
3168static __be64 get_umr_update_pd_mask(void)
3169{
3170 u64 result;
3171
Artemy Kovalyov31616252017-01-02 11:37:42 +02003172 result = MLX5_MKEY_MASK_PD;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003173
3174 return cpu_to_be64(result);
3175}
3176
Eli Cohene126ba92013-07-07 17:25:49 +03003177static void set_reg_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Maor Gottlieb578e7262016-10-27 16:36:37 +03003178 struct ib_send_wr *wr, int atomic)
Eli Cohene126ba92013-07-07 17:25:49 +03003179{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003180 struct mlx5_umr_wr *umrwr = umr_wr(wr);
Eli Cohene126ba92013-07-07 17:25:49 +03003181
3182 memset(umr, 0, sizeof(*umr));
3183
Haggai Eran968e78d2014-12-11 17:04:11 +02003184 if (wr->send_flags & MLX5_IB_SEND_UMR_FAIL_IF_FREE)
3185 umr->flags = MLX5_UMR_CHECK_FREE; /* fail if free */
3186 else
3187 umr->flags = MLX5_UMR_CHECK_NOT_FREE; /* fail if not free */
3188
Artemy Kovalyov31616252017-01-02 11:37:42 +02003189 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(umrwr->xlt_size));
3190 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_XLT) {
3191 u64 offset = get_xlt_octo(umrwr->offset);
3192
3193 umr->xlt_offset = cpu_to_be16(offset & 0xffff);
3194 umr->xlt_offset_47_16 = cpu_to_be32(offset >> 16);
3195 umr->flags |= MLX5_UMR_TRANSLATION_OFFSET_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03003196 }
Artemy Kovalyov31616252017-01-02 11:37:42 +02003197 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION)
3198 umr->mkey_mask |= get_umr_update_translation_mask();
3199 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS) {
3200 umr->mkey_mask |= get_umr_update_access_mask(atomic);
3201 umr->mkey_mask |= get_umr_update_pd_mask();
3202 }
3203 if (wr->send_flags & MLX5_IB_SEND_UMR_ENABLE_MR)
3204 umr->mkey_mask |= get_umr_enable_mr_mask();
3205 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
3206 umr->mkey_mask |= get_umr_disable_mr_mask();
Eli Cohene126ba92013-07-07 17:25:49 +03003207
3208 if (!wr->num_sge)
Haggai Eran968e78d2014-12-11 17:04:11 +02003209 umr->flags |= MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03003210}
3211
3212static u8 get_umr_flags(int acc)
3213{
3214 return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC : 0) |
3215 (acc & IB_ACCESS_REMOTE_WRITE ? MLX5_PERM_REMOTE_WRITE : 0) |
3216 (acc & IB_ACCESS_REMOTE_READ ? MLX5_PERM_REMOTE_READ : 0) |
3217 (acc & IB_ACCESS_LOCAL_WRITE ? MLX5_PERM_LOCAL_WRITE : 0) |
Sagi Grimberg2ac45932014-02-23 14:19:09 +02003218 MLX5_PERM_LOCAL_READ | MLX5_PERM_UMR_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03003219}
3220
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003221static void set_reg_mkey_seg(struct mlx5_mkey_seg *seg,
3222 struct mlx5_ib_mr *mr,
3223 u32 key, int access)
3224{
3225 int ndescs = ALIGN(mr->ndescs, 8) >> 1;
3226
3227 memset(seg, 0, sizeof(*seg));
Sagi Grimbergb005d312016-02-29 19:07:33 +02003228
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003229 if (mr->access_mode == MLX5_MKC_ACCESS_MODE_MTT)
Sagi Grimbergb005d312016-02-29 19:07:33 +02003230 seg->log2_page_size = ilog2(mr->ibmr.page_size);
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003231 else if (mr->access_mode == MLX5_MKC_ACCESS_MODE_KLMS)
Sagi Grimbergb005d312016-02-29 19:07:33 +02003232 /* KLMs take twice the size of MTTs */
3233 ndescs *= 2;
3234
3235 seg->flags = get_umr_flags(access) | mr->access_mode;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003236 seg->qpn_mkey7_0 = cpu_to_be32((key & 0xff) | 0xffffff00);
3237 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL);
3238 seg->start_addr = cpu_to_be64(mr->ibmr.iova);
3239 seg->len = cpu_to_be64(mr->ibmr.length);
3240 seg->xlt_oct_size = cpu_to_be32(ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003241}
3242
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003243static void set_linv_mkey_seg(struct mlx5_mkey_seg *seg)
Eli Cohene126ba92013-07-07 17:25:49 +03003244{
3245 memset(seg, 0, sizeof(*seg));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003246 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03003247}
3248
3249static void set_reg_mkey_segment(struct mlx5_mkey_seg *seg, struct ib_send_wr *wr)
3250{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003251 struct mlx5_umr_wr *umrwr = umr_wr(wr);
Haggai Eran968e78d2014-12-11 17:04:11 +02003252
Eli Cohene126ba92013-07-07 17:25:49 +03003253 memset(seg, 0, sizeof(*seg));
Artemy Kovalyov31616252017-01-02 11:37:42 +02003254 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
Haggai Eran968e78d2014-12-11 17:04:11 +02003255 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03003256
Haggai Eran968e78d2014-12-11 17:04:11 +02003257 seg->flags = convert_access(umrwr->access_flags);
Artemy Kovalyov31616252017-01-02 11:37:42 +02003258 if (umrwr->pd)
3259 seg->flags_pd = cpu_to_be32(to_mpd(umrwr->pd)->pdn);
3260 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION &&
3261 !umrwr->length)
3262 seg->flags_pd |= cpu_to_be32(MLX5_MKEY_LEN64);
3263
3264 seg->start_addr = cpu_to_be64(umrwr->virt_addr);
Haggai Eran968e78d2014-12-11 17:04:11 +02003265 seg->len = cpu_to_be64(umrwr->length);
3266 seg->log2_page_size = umrwr->page_shift;
Eli Cohen746b5582013-10-23 09:53:14 +03003267 seg->qpn_mkey7_0 = cpu_to_be32(0xffffff00 |
Haggai Eran968e78d2014-12-11 17:04:11 +02003268 mlx5_mkey_variant(umrwr->mkey));
Eli Cohene126ba92013-07-07 17:25:49 +03003269}
3270
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003271static void set_reg_data_seg(struct mlx5_wqe_data_seg *dseg,
3272 struct mlx5_ib_mr *mr,
3273 struct mlx5_ib_pd *pd)
3274{
3275 int bcount = mr->desc_size * mr->ndescs;
3276
3277 dseg->addr = cpu_to_be64(mr->desc_map);
3278 dseg->byte_count = cpu_to_be32(ALIGN(bcount, 64));
3279 dseg->lkey = cpu_to_be32(pd->ibpd.local_dma_lkey);
3280}
3281
Eli Cohene126ba92013-07-07 17:25:49 +03003282static __be32 send_ieth(struct ib_send_wr *wr)
3283{
3284 switch (wr->opcode) {
3285 case IB_WR_SEND_WITH_IMM:
3286 case IB_WR_RDMA_WRITE_WITH_IMM:
3287 return wr->ex.imm_data;
3288
3289 case IB_WR_SEND_WITH_INV:
3290 return cpu_to_be32(wr->ex.invalidate_rkey);
3291
3292 default:
3293 return 0;
3294 }
3295}
3296
3297static u8 calc_sig(void *wqe, int size)
3298{
3299 u8 *p = wqe;
3300 u8 res = 0;
3301 int i;
3302
3303 for (i = 0; i < size; i++)
3304 res ^= p[i];
3305
3306 return ~res;
3307}
3308
3309static u8 wq_sig(void *wqe)
3310{
3311 return calc_sig(wqe, (*((u8 *)wqe + 8) & 0x3f) << 4);
3312}
3313
3314static int set_data_inl_seg(struct mlx5_ib_qp *qp, struct ib_send_wr *wr,
3315 void *wqe, int *sz)
3316{
3317 struct mlx5_wqe_inline_seg *seg;
3318 void *qend = qp->sq.qend;
3319 void *addr;
3320 int inl = 0;
3321 int copy;
3322 int len;
3323 int i;
3324
3325 seg = wqe;
3326 wqe += sizeof(*seg);
3327 for (i = 0; i < wr->num_sge; i++) {
3328 addr = (void *)(unsigned long)(wr->sg_list[i].addr);
3329 len = wr->sg_list[i].length;
3330 inl += len;
3331
3332 if (unlikely(inl > qp->max_inline_data))
3333 return -ENOMEM;
3334
3335 if (unlikely(wqe + len > qend)) {
3336 copy = qend - wqe;
3337 memcpy(wqe, addr, copy);
3338 addr += copy;
3339 len -= copy;
3340 wqe = mlx5_get_send_wqe(qp, 0);
3341 }
3342 memcpy(wqe, addr, len);
3343 wqe += len;
3344 }
3345
3346 seg->byte_count = cpu_to_be32(inl | MLX5_INLINE_SEG);
3347
3348 *sz = ALIGN(inl + sizeof(seg->byte_count), 16) / 16;
3349
3350 return 0;
3351}
3352
Sagi Grimberge6631812014-02-23 14:19:11 +02003353static u16 prot_field_size(enum ib_signature_type type)
3354{
3355 switch (type) {
3356 case IB_SIG_TYPE_T10_DIF:
3357 return MLX5_DIF_SIZE;
3358 default:
3359 return 0;
3360 }
3361}
3362
3363static u8 bs_selector(int block_size)
3364{
3365 switch (block_size) {
3366 case 512: return 0x1;
3367 case 520: return 0x2;
3368 case 4096: return 0x3;
3369 case 4160: return 0x4;
3370 case 1073741824: return 0x5;
3371 default: return 0;
3372 }
3373}
3374
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003375static void mlx5_fill_inl_bsf(struct ib_sig_domain *domain,
3376 struct mlx5_bsf_inl *inl)
Sagi Grimberge6631812014-02-23 14:19:11 +02003377{
Sagi Grimberg142537f2014-08-13 19:54:32 +03003378 /* Valid inline section and allow BSF refresh */
3379 inl->vld_refresh = cpu_to_be16(MLX5_BSF_INL_VALID |
3380 MLX5_BSF_REFRESH_DIF);
3381 inl->dif_apptag = cpu_to_be16(domain->sig.dif.app_tag);
3382 inl->dif_reftag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003383 /* repeating block */
3384 inl->rp_inv_seed = MLX5_BSF_REPEAT_BLOCK;
3385 inl->sig_type = domain->sig.dif.bg_type == IB_T10DIF_CRC ?
3386 MLX5_DIF_CRC : MLX5_DIF_IPCS;
Sagi Grimberge6631812014-02-23 14:19:11 +02003387
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003388 if (domain->sig.dif.ref_remap)
3389 inl->dif_inc_ref_guard_check |= MLX5_BSF_INC_REFTAG;
Sagi Grimberge6631812014-02-23 14:19:11 +02003390
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003391 if (domain->sig.dif.app_escape) {
3392 if (domain->sig.dif.ref_escape)
3393 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPREF_ESCAPE;
3394 else
3395 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPTAG_ESCAPE;
Sagi Grimberge6631812014-02-23 14:19:11 +02003396 }
3397
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003398 inl->dif_app_bitmask_check =
3399 cpu_to_be16(domain->sig.dif.apptag_check_mask);
Sagi Grimberge6631812014-02-23 14:19:11 +02003400}
3401
3402static int mlx5_set_bsf(struct ib_mr *sig_mr,
3403 struct ib_sig_attrs *sig_attrs,
3404 struct mlx5_bsf *bsf, u32 data_size)
3405{
3406 struct mlx5_core_sig_ctx *msig = to_mmr(sig_mr)->sig;
3407 struct mlx5_bsf_basic *basic = &bsf->basic;
3408 struct ib_sig_domain *mem = &sig_attrs->mem;
3409 struct ib_sig_domain *wire = &sig_attrs->wire;
Sagi Grimberge6631812014-02-23 14:19:11 +02003410
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003411 memset(bsf, 0, sizeof(*bsf));
Sagi Grimberge6631812014-02-23 14:19:11 +02003412
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003413 /* Basic + Extended + Inline */
3414 basic->bsf_size_sbs = 1 << 7;
3415 /* Input domain check byte mask */
3416 basic->check_byte_mask = sig_attrs->check_mask;
3417 basic->raw_data_size = cpu_to_be32(data_size);
3418
3419 /* Memory domain */
3420 switch (sig_attrs->mem.sig_type) {
3421 case IB_SIG_TYPE_NONE:
3422 break;
3423 case IB_SIG_TYPE_T10_DIF:
3424 basic->mem.bs_selector = bs_selector(mem->sig.dif.pi_interval);
3425 basic->m_bfs_psv = cpu_to_be32(msig->psv_memory.psv_idx);
3426 mlx5_fill_inl_bsf(mem, &bsf->m_inl);
3427 break;
3428 default:
3429 return -EINVAL;
3430 }
3431
3432 /* Wire domain */
3433 switch (sig_attrs->wire.sig_type) {
3434 case IB_SIG_TYPE_NONE:
3435 break;
3436 case IB_SIG_TYPE_T10_DIF:
Sagi Grimberge6631812014-02-23 14:19:11 +02003437 if (mem->sig.dif.pi_interval == wire->sig.dif.pi_interval &&
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003438 mem->sig_type == wire->sig_type) {
Sagi Grimberge6631812014-02-23 14:19:11 +02003439 /* Same block structure */
Sagi Grimberg142537f2014-08-13 19:54:32 +03003440 basic->bsf_size_sbs |= 1 << 4;
Sagi Grimberge6631812014-02-23 14:19:11 +02003441 if (mem->sig.dif.bg_type == wire->sig.dif.bg_type)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003442 basic->wire.copy_byte_mask |= MLX5_CPY_GRD_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003443 if (mem->sig.dif.app_tag == wire->sig.dif.app_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003444 basic->wire.copy_byte_mask |= MLX5_CPY_APP_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003445 if (mem->sig.dif.ref_tag == wire->sig.dif.ref_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003446 basic->wire.copy_byte_mask |= MLX5_CPY_REF_MASK;
Sagi Grimberge6631812014-02-23 14:19:11 +02003447 } else
3448 basic->wire.bs_selector = bs_selector(wire->sig.dif.pi_interval);
3449
Sagi Grimberg142537f2014-08-13 19:54:32 +03003450 basic->w_bfs_psv = cpu_to_be32(msig->psv_wire.psv_idx);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003451 mlx5_fill_inl_bsf(wire, &bsf->w_inl);
Sagi Grimberge6631812014-02-23 14:19:11 +02003452 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003453 default:
3454 return -EINVAL;
3455 }
3456
3457 return 0;
3458}
3459
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003460static int set_sig_data_segment(struct ib_sig_handover_wr *wr,
3461 struct mlx5_ib_qp *qp, void **seg, int *size)
Sagi Grimberge6631812014-02-23 14:19:11 +02003462{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003463 struct ib_sig_attrs *sig_attrs = wr->sig_attrs;
3464 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003465 struct mlx5_bsf *bsf;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003466 u32 data_len = wr->wr.sg_list->length;
3467 u32 data_key = wr->wr.sg_list->lkey;
3468 u64 data_va = wr->wr.sg_list->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003469 int ret;
3470 int wqe_size;
3471
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003472 if (!wr->prot ||
3473 (data_key == wr->prot->lkey &&
3474 data_va == wr->prot->addr &&
3475 data_len == wr->prot->length)) {
Sagi Grimberge6631812014-02-23 14:19:11 +02003476 /**
3477 * Source domain doesn't contain signature information
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003478 * or data and protection are interleaved in memory.
Sagi Grimberge6631812014-02-23 14:19:11 +02003479 * So need construct:
3480 * ------------------
3481 * | data_klm |
3482 * ------------------
3483 * | BSF |
3484 * ------------------
3485 **/
3486 struct mlx5_klm *data_klm = *seg;
3487
3488 data_klm->bcount = cpu_to_be32(data_len);
3489 data_klm->key = cpu_to_be32(data_key);
3490 data_klm->va = cpu_to_be64(data_va);
3491 wqe_size = ALIGN(sizeof(*data_klm), 64);
3492 } else {
3493 /**
3494 * Source domain contains signature information
3495 * So need construct a strided block format:
3496 * ---------------------------
3497 * | stride_block_ctrl |
3498 * ---------------------------
3499 * | data_klm |
3500 * ---------------------------
3501 * | prot_klm |
3502 * ---------------------------
3503 * | BSF |
3504 * ---------------------------
3505 **/
3506 struct mlx5_stride_block_ctrl_seg *sblock_ctrl;
3507 struct mlx5_stride_block_entry *data_sentry;
3508 struct mlx5_stride_block_entry *prot_sentry;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003509 u32 prot_key = wr->prot->lkey;
3510 u64 prot_va = wr->prot->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003511 u16 block_size = sig_attrs->mem.sig.dif.pi_interval;
3512 int prot_size;
3513
3514 sblock_ctrl = *seg;
3515 data_sentry = (void *)sblock_ctrl + sizeof(*sblock_ctrl);
3516 prot_sentry = (void *)data_sentry + sizeof(*data_sentry);
3517
3518 prot_size = prot_field_size(sig_attrs->mem.sig_type);
3519 if (!prot_size) {
3520 pr_err("Bad block size given: %u\n", block_size);
3521 return -EINVAL;
3522 }
3523 sblock_ctrl->bcount_per_cycle = cpu_to_be32(block_size +
3524 prot_size);
3525 sblock_ctrl->op = cpu_to_be32(MLX5_STRIDE_BLOCK_OP);
3526 sblock_ctrl->repeat_count = cpu_to_be32(data_len / block_size);
3527 sblock_ctrl->num_entries = cpu_to_be16(2);
3528
3529 data_sentry->bcount = cpu_to_be16(block_size);
3530 data_sentry->key = cpu_to_be32(data_key);
3531 data_sentry->va = cpu_to_be64(data_va);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003532 data_sentry->stride = cpu_to_be16(block_size);
3533
Sagi Grimberge6631812014-02-23 14:19:11 +02003534 prot_sentry->bcount = cpu_to_be16(prot_size);
3535 prot_sentry->key = cpu_to_be32(prot_key);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003536 prot_sentry->va = cpu_to_be64(prot_va);
3537 prot_sentry->stride = cpu_to_be16(prot_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02003538
Sagi Grimberge6631812014-02-23 14:19:11 +02003539 wqe_size = ALIGN(sizeof(*sblock_ctrl) + sizeof(*data_sentry) +
3540 sizeof(*prot_sentry), 64);
3541 }
3542
3543 *seg += wqe_size;
3544 *size += wqe_size / 16;
3545 if (unlikely((*seg == qp->sq.qend)))
3546 *seg = mlx5_get_send_wqe(qp, 0);
3547
3548 bsf = *seg;
3549 ret = mlx5_set_bsf(sig_mr, sig_attrs, bsf, data_len);
3550 if (ret)
3551 return -EINVAL;
3552
3553 *seg += sizeof(*bsf);
3554 *size += sizeof(*bsf) / 16;
3555 if (unlikely((*seg == qp->sq.qend)))
3556 *seg = mlx5_get_send_wqe(qp, 0);
3557
3558 return 0;
3559}
3560
3561static void set_sig_mkey_segment(struct mlx5_mkey_seg *seg,
Artemy Kovalyov31616252017-01-02 11:37:42 +02003562 struct ib_sig_handover_wr *wr, u32 size,
Sagi Grimberge6631812014-02-23 14:19:11 +02003563 u32 length, u32 pdn)
3564{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003565 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003566 u32 sig_key = sig_mr->rkey;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003567 u8 sigerr = to_mmr(sig_mr)->sig->sigerr_count & 1;
Sagi Grimberge6631812014-02-23 14:19:11 +02003568
3569 memset(seg, 0, sizeof(*seg));
3570
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003571 seg->flags = get_umr_flags(wr->access_flags) |
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003572 MLX5_MKC_ACCESS_MODE_KLMS;
Sagi Grimberge6631812014-02-23 14:19:11 +02003573 seg->qpn_mkey7_0 = cpu_to_be32((sig_key & 0xff) | 0xffffff00);
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003574 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL | sigerr << 26 |
Sagi Grimberge6631812014-02-23 14:19:11 +02003575 MLX5_MKEY_BSF_EN | pdn);
3576 seg->len = cpu_to_be64(length);
Artemy Kovalyov31616252017-01-02 11:37:42 +02003577 seg->xlt_oct_size = cpu_to_be32(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02003578 seg->bsfs_octo_size = cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE);
3579}
3580
3581static void set_sig_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02003582 u32 size)
Sagi Grimberge6631812014-02-23 14:19:11 +02003583{
3584 memset(umr, 0, sizeof(*umr));
3585
3586 umr->flags = MLX5_FLAGS_INLINE | MLX5_FLAGS_CHECK_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02003587 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02003588 umr->bsf_octowords = cpu_to_be16(MLX5_MKEY_BSF_OCTO_SIZE);
3589 umr->mkey_mask = sig_mkey_mask();
3590}
3591
3592
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003593static int set_sig_umr_wr(struct ib_send_wr *send_wr, struct mlx5_ib_qp *qp,
Sagi Grimberge6631812014-02-23 14:19:11 +02003594 void **seg, int *size)
3595{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003596 struct ib_sig_handover_wr *wr = sig_handover_wr(send_wr);
3597 struct mlx5_ib_mr *sig_mr = to_mmr(wr->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02003598 u32 pdn = get_pd(qp)->pdn;
Artemy Kovalyov31616252017-01-02 11:37:42 +02003599 u32 xlt_size;
Sagi Grimberge6631812014-02-23 14:19:11 +02003600 int region_len, ret;
3601
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003602 if (unlikely(wr->wr.num_sge != 1) ||
3603 unlikely(wr->access_flags & IB_ACCESS_REMOTE_ATOMIC) ||
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003604 unlikely(!sig_mr->sig) || unlikely(!qp->signature_en) ||
3605 unlikely(!sig_mr->sig->sig_status_checked))
Sagi Grimberge6631812014-02-23 14:19:11 +02003606 return -EINVAL;
3607
3608 /* length of the protected region, data + protection */
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003609 region_len = wr->wr.sg_list->length;
3610 if (wr->prot &&
3611 (wr->prot->lkey != wr->wr.sg_list->lkey ||
3612 wr->prot->addr != wr->wr.sg_list->addr ||
3613 wr->prot->length != wr->wr.sg_list->length))
3614 region_len += wr->prot->length;
Sagi Grimberge6631812014-02-23 14:19:11 +02003615
3616 /**
3617 * KLM octoword size - if protection was provided
3618 * then we use strided block format (3 octowords),
3619 * else we use single KLM (1 octoword)
3620 **/
Artemy Kovalyov31616252017-01-02 11:37:42 +02003621 xlt_size = wr->prot ? 0x30 : sizeof(struct mlx5_klm);
Sagi Grimberge6631812014-02-23 14:19:11 +02003622
Artemy Kovalyov31616252017-01-02 11:37:42 +02003623 set_sig_umr_segment(*seg, xlt_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02003624 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
3625 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
3626 if (unlikely((*seg == qp->sq.qend)))
3627 *seg = mlx5_get_send_wqe(qp, 0);
3628
Artemy Kovalyov31616252017-01-02 11:37:42 +02003629 set_sig_mkey_segment(*seg, wr, xlt_size, region_len, pdn);
Sagi Grimberge6631812014-02-23 14:19:11 +02003630 *seg += sizeof(struct mlx5_mkey_seg);
3631 *size += sizeof(struct mlx5_mkey_seg) / 16;
3632 if (unlikely((*seg == qp->sq.qend)))
3633 *seg = mlx5_get_send_wqe(qp, 0);
3634
3635 ret = set_sig_data_segment(wr, qp, seg, size);
3636 if (ret)
3637 return ret;
3638
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003639 sig_mr->sig->sig_status_checked = false;
Sagi Grimberge6631812014-02-23 14:19:11 +02003640 return 0;
3641}
3642
3643static int set_psv_wr(struct ib_sig_domain *domain,
3644 u32 psv_idx, void **seg, int *size)
3645{
3646 struct mlx5_seg_set_psv *psv_seg = *seg;
3647
3648 memset(psv_seg, 0, sizeof(*psv_seg));
3649 psv_seg->psv_num = cpu_to_be32(psv_idx);
3650 switch (domain->sig_type) {
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003651 case IB_SIG_TYPE_NONE:
3652 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003653 case IB_SIG_TYPE_T10_DIF:
3654 psv_seg->transient_sig = cpu_to_be32(domain->sig.dif.bg << 16 |
3655 domain->sig.dif.app_tag);
3656 psv_seg->ref_tag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberge6631812014-02-23 14:19:11 +02003657 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003658 default:
3659 pr_err("Bad signature type given.\n");
3660 return 1;
3661 }
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:
Haggai Erand16e91d2016-02-29 15:45:05 +02004000 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03004001 set_datagram_seg(seg, wr);
Jack Morgensteinf241e742014-07-28 23:30:23 +03004002 seg += sizeof(struct mlx5_wqe_datagram_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004003 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
4004 if (unlikely((seg == qend)))
4005 seg = mlx5_get_send_wqe(qp, 0);
4006 break;
Erez Shitritf0313962016-02-21 16:27:17 +02004007 case IB_QPT_UD:
4008 set_datagram_seg(seg, wr);
4009 seg += sizeof(struct mlx5_wqe_datagram_seg);
4010 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Eli Cohene126ba92013-07-07 17:25:49 +03004011
Erez Shitritf0313962016-02-21 16:27:17 +02004012 if (unlikely((seg == qend)))
4013 seg = mlx5_get_send_wqe(qp, 0);
4014
4015 /* handle qp that supports ud offload */
4016 if (qp->flags & IB_QP_CREATE_IPOIB_UD_LSO) {
4017 struct mlx5_wqe_eth_pad *pad;
4018
4019 pad = seg;
4020 memset(pad, 0, sizeof(struct mlx5_wqe_eth_pad));
4021 seg += sizeof(struct mlx5_wqe_eth_pad);
4022 size += sizeof(struct mlx5_wqe_eth_pad) / 16;
4023
4024 seg = set_eth_seg(seg, wr, qend, qp, &size);
4025
4026 if (unlikely((seg == qend)))
4027 seg = mlx5_get_send_wqe(qp, 0);
4028 }
4029 break;
Eli Cohene126ba92013-07-07 17:25:49 +03004030 case MLX5_IB_QPT_REG_UMR:
4031 if (wr->opcode != MLX5_IB_WR_UMR) {
4032 err = -EINVAL;
4033 mlx5_ib_warn(dev, "bad opcode\n");
4034 goto out;
4035 }
4036 qp->sq.wr_data[idx] = MLX5_IB_WR_UMR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004037 ctrl->imm = cpu_to_be32(umr_wr(wr)->mkey);
Maor Gottlieb578e7262016-10-27 16:36:37 +03004038 set_reg_umr_segment(seg, wr, !!(MLX5_CAP_GEN(mdev, atomic)));
Eli Cohene126ba92013-07-07 17:25:49 +03004039 seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4040 size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4041 if (unlikely((seg == qend)))
4042 seg = mlx5_get_send_wqe(qp, 0);
4043 set_reg_mkey_segment(seg, wr);
4044 seg += sizeof(struct mlx5_mkey_seg);
4045 size += sizeof(struct mlx5_mkey_seg) / 16;
4046 if (unlikely((seg == qend)))
4047 seg = mlx5_get_send_wqe(qp, 0);
4048 break;
4049
4050 default:
4051 break;
4052 }
4053
4054 if (wr->send_flags & IB_SEND_INLINE && num_sge) {
4055 int uninitialized_var(sz);
4056
4057 err = set_data_inl_seg(qp, wr, seg, &sz);
4058 if (unlikely(err)) {
4059 mlx5_ib_warn(dev, "\n");
4060 *bad_wr = wr;
4061 goto out;
4062 }
4063 inl = 1;
4064 size += sz;
4065 } else {
4066 dpseg = seg;
4067 for (i = 0; i < num_sge; i++) {
4068 if (unlikely(dpseg == qend)) {
4069 seg = mlx5_get_send_wqe(qp, 0);
4070 dpseg = seg;
4071 }
4072 if (likely(wr->sg_list[i].length)) {
4073 set_data_ptr_seg(dpseg, wr->sg_list + i);
4074 size += sizeof(struct mlx5_wqe_data_seg) / 16;
4075 dpseg++;
4076 }
4077 }
4078 }
4079
Sagi Grimberg6e5eada2014-02-23 14:19:08 +02004080 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
4081 get_fence(fence, wr), next_fence,
4082 mlx5_ib_opcode[wr->opcode]);
Sagi Grimberge6631812014-02-23 14:19:11 +02004083skip_psv:
Eli Cohene126ba92013-07-07 17:25:49 +03004084 if (0)
4085 dump_wqe(qp, idx, size);
4086 }
4087
4088out:
4089 if (likely(nreq)) {
4090 qp->sq.head += nreq;
4091
4092 /* Make sure that descriptors are written before
4093 * updating doorbell record and ringing the doorbell
4094 */
4095 wmb();
4096
4097 qp->db.db[MLX5_SND_DBR] = cpu_to_be32(qp->sq.cur_post);
4098
Eli Cohenada388f2014-01-14 17:45:16 +02004099 /* Make sure doorbell record is visible to the HCA before
4100 * we hit doorbell */
4101 wmb();
4102
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004103 /* currently we support only regular doorbells */
4104 mlx5_write64((__be32 *)ctrl, bf->bfreg->map + bf->offset, NULL);
4105 /* Make sure doorbells don't leak out of SQ spinlock
4106 * and reach the HCA out of order.
4107 */
4108 mmiowb();
Eli Cohene126ba92013-07-07 17:25:49 +03004109 bf->offset ^= bf->buf_size;
Eli Cohene126ba92013-07-07 17:25:49 +03004110 }
4111
4112 spin_unlock_irqrestore(&qp->sq.lock, flags);
4113
4114 return err;
4115}
4116
4117static void set_sig_seg(struct mlx5_rwqe_sig *sig, int size)
4118{
4119 sig->signature = calc_sig(sig, size);
4120}
4121
4122int mlx5_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
4123 struct ib_recv_wr **bad_wr)
4124{
4125 struct mlx5_ib_qp *qp = to_mqp(ibqp);
4126 struct mlx5_wqe_data_seg *scat;
4127 struct mlx5_rwqe_sig *sig;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004128 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4129 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004130 unsigned long flags;
4131 int err = 0;
4132 int nreq;
4133 int ind;
4134 int i;
4135
Haggai Erand16e91d2016-02-29 15:45:05 +02004136 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4137 return mlx5_ib_gsi_post_recv(ibqp, wr, bad_wr);
4138
Eli Cohene126ba92013-07-07 17:25:49 +03004139 spin_lock_irqsave(&qp->rq.lock, flags);
4140
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004141 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
4142 err = -EIO;
4143 *bad_wr = wr;
4144 nreq = 0;
4145 goto out;
4146 }
4147
Eli Cohene126ba92013-07-07 17:25:49 +03004148 ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
4149
4150 for (nreq = 0; wr; nreq++, wr = wr->next) {
4151 if (mlx5_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
4152 err = -ENOMEM;
4153 *bad_wr = wr;
4154 goto out;
4155 }
4156
4157 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
4158 err = -EINVAL;
4159 *bad_wr = wr;
4160 goto out;
4161 }
4162
4163 scat = get_recv_wqe(qp, ind);
4164 if (qp->wq_sig)
4165 scat++;
4166
4167 for (i = 0; i < wr->num_sge; i++)
4168 set_data_ptr_seg(scat + i, wr->sg_list + i);
4169
4170 if (i < qp->rq.max_gs) {
4171 scat[i].byte_count = 0;
4172 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
4173 scat[i].addr = 0;
4174 }
4175
4176 if (qp->wq_sig) {
4177 sig = (struct mlx5_rwqe_sig *)scat;
4178 set_sig_seg(sig, (qp->rq.max_gs + 1) << 2);
4179 }
4180
4181 qp->rq.wrid[ind] = wr->wr_id;
4182
4183 ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
4184 }
4185
4186out:
4187 if (likely(nreq)) {
4188 qp->rq.head += nreq;
4189
4190 /* Make sure that descriptors are written before
4191 * doorbell record.
4192 */
4193 wmb();
4194
4195 *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
4196 }
4197
4198 spin_unlock_irqrestore(&qp->rq.lock, flags);
4199
4200 return err;
4201}
4202
4203static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
4204{
4205 switch (mlx5_state) {
4206 case MLX5_QP_STATE_RST: return IB_QPS_RESET;
4207 case MLX5_QP_STATE_INIT: return IB_QPS_INIT;
4208 case MLX5_QP_STATE_RTR: return IB_QPS_RTR;
4209 case MLX5_QP_STATE_RTS: return IB_QPS_RTS;
4210 case MLX5_QP_STATE_SQ_DRAINING:
4211 case MLX5_QP_STATE_SQD: return IB_QPS_SQD;
4212 case MLX5_QP_STATE_SQER: return IB_QPS_SQE;
4213 case MLX5_QP_STATE_ERR: return IB_QPS_ERR;
4214 default: return -1;
4215 }
4216}
4217
4218static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
4219{
4220 switch (mlx5_mig_state) {
4221 case MLX5_QP_PM_ARMED: return IB_MIG_ARMED;
4222 case MLX5_QP_PM_REARM: return IB_MIG_REARM;
4223 case MLX5_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
4224 default: return -1;
4225 }
4226}
4227
4228static int to_ib_qp_access_flags(int mlx5_flags)
4229{
4230 int ib_flags = 0;
4231
4232 if (mlx5_flags & MLX5_QP_BIT_RRE)
4233 ib_flags |= IB_ACCESS_REMOTE_READ;
4234 if (mlx5_flags & MLX5_QP_BIT_RWE)
4235 ib_flags |= IB_ACCESS_REMOTE_WRITE;
4236 if (mlx5_flags & MLX5_QP_BIT_RAE)
4237 ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
4238
4239 return ib_flags;
4240}
4241
4242static void to_ib_ah_attr(struct mlx5_ib_dev *ibdev, struct ib_ah_attr *ib_ah_attr,
4243 struct mlx5_qp_path *path)
4244{
Jack Morgenstein9603b612014-07-28 23:30:22 +03004245 struct mlx5_core_dev *dev = ibdev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004246
4247 memset(ib_ah_attr, 0, sizeof(*ib_ah_attr));
4248 ib_ah_attr->port_num = path->port;
4249
Eli Cohenc7a08ac2014-10-02 12:19:42 +03004250 if (ib_ah_attr->port_num == 0 ||
Saeed Mahameed938fe832015-05-28 22:28:41 +03004251 ib_ah_attr->port_num > MLX5_CAP_GEN(dev, num_ports))
Eli Cohene126ba92013-07-07 17:25:49 +03004252 return;
4253
Achiad Shochat2811ba52015-12-23 18:47:24 +02004254 ib_ah_attr->sl = path->dci_cfi_prio_sl & 0xf;
Eli Cohene126ba92013-07-07 17:25:49 +03004255
4256 ib_ah_attr->dlid = be16_to_cpu(path->rlid);
4257 ib_ah_attr->src_path_bits = path->grh_mlid & 0x7f;
4258 ib_ah_attr->static_rate = path->static_rate ? path->static_rate - 5 : 0;
4259 ib_ah_attr->ah_flags = (path->grh_mlid & (1 << 7)) ? IB_AH_GRH : 0;
4260 if (ib_ah_attr->ah_flags) {
4261 ib_ah_attr->grh.sgid_index = path->mgid_index;
4262 ib_ah_attr->grh.hop_limit = path->hop_limit;
4263 ib_ah_attr->grh.traffic_class =
4264 (be32_to_cpu(path->tclass_flowlabel) >> 20) & 0xff;
4265 ib_ah_attr->grh.flow_label =
4266 be32_to_cpu(path->tclass_flowlabel) & 0xfffff;
4267 memcpy(ib_ah_attr->grh.dgid.raw,
4268 path->rgid, sizeof(ib_ah_attr->grh.dgid.raw));
4269 }
4270}
4271
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004272static int query_raw_packet_qp_sq_state(struct mlx5_ib_dev *dev,
4273 struct mlx5_ib_sq *sq,
4274 u8 *sq_state)
Eli Cohene126ba92013-07-07 17:25:49 +03004275{
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004276 void *out;
4277 void *sqc;
4278 int inlen;
4279 int err;
4280
4281 inlen = MLX5_ST_SZ_BYTES(query_sq_out);
4282 out = mlx5_vzalloc(inlen);
4283 if (!out)
4284 return -ENOMEM;
4285
4286 err = mlx5_core_query_sq(dev->mdev, sq->base.mqp.qpn, out);
4287 if (err)
4288 goto out;
4289
4290 sqc = MLX5_ADDR_OF(query_sq_out, out, sq_context);
4291 *sq_state = MLX5_GET(sqc, sqc, state);
4292 sq->state = *sq_state;
4293
4294out:
4295 kvfree(out);
4296 return err;
4297}
4298
4299static int query_raw_packet_qp_rq_state(struct mlx5_ib_dev *dev,
4300 struct mlx5_ib_rq *rq,
4301 u8 *rq_state)
4302{
4303 void *out;
4304 void *rqc;
4305 int inlen;
4306 int err;
4307
4308 inlen = MLX5_ST_SZ_BYTES(query_rq_out);
4309 out = mlx5_vzalloc(inlen);
4310 if (!out)
4311 return -ENOMEM;
4312
4313 err = mlx5_core_query_rq(dev->mdev, rq->base.mqp.qpn, out);
4314 if (err)
4315 goto out;
4316
4317 rqc = MLX5_ADDR_OF(query_rq_out, out, rq_context);
4318 *rq_state = MLX5_GET(rqc, rqc, state);
4319 rq->state = *rq_state;
4320
4321out:
4322 kvfree(out);
4323 return err;
4324}
4325
4326static int sqrq_state_to_qp_state(u8 sq_state, u8 rq_state,
4327 struct mlx5_ib_qp *qp, u8 *qp_state)
4328{
4329 static const u8 sqrq_trans[MLX5_RQ_NUM_STATE][MLX5_SQ_NUM_STATE] = {
4330 [MLX5_RQC_STATE_RST] = {
4331 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
4332 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
4333 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE_BAD,
4334 [MLX5_SQ_STATE_NA] = IB_QPS_RESET,
4335 },
4336 [MLX5_RQC_STATE_RDY] = {
4337 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
4338 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
4339 [MLX5_SQC_STATE_ERR] = IB_QPS_SQE,
4340 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE,
4341 },
4342 [MLX5_RQC_STATE_ERR] = {
4343 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
4344 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
4345 [MLX5_SQC_STATE_ERR] = IB_QPS_ERR,
4346 [MLX5_SQ_STATE_NA] = IB_QPS_ERR,
4347 },
4348 [MLX5_RQ_STATE_NA] = {
4349 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
4350 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
4351 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE,
4352 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE_BAD,
4353 },
4354 };
4355
4356 *qp_state = sqrq_trans[rq_state][sq_state];
4357
4358 if (*qp_state == MLX5_QP_STATE_BAD) {
4359 WARN(1, "Buggy Raw Packet QP state, SQ 0x%x state: 0x%x, RQ 0x%x state: 0x%x",
4360 qp->raw_packet_qp.sq.base.mqp.qpn, sq_state,
4361 qp->raw_packet_qp.rq.base.mqp.qpn, rq_state);
4362 return -EINVAL;
4363 }
4364
4365 if (*qp_state == MLX5_QP_STATE)
4366 *qp_state = qp->state;
4367
4368 return 0;
4369}
4370
4371static int query_raw_packet_qp_state(struct mlx5_ib_dev *dev,
4372 struct mlx5_ib_qp *qp,
4373 u8 *raw_packet_qp_state)
4374{
4375 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
4376 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
4377 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
4378 int err;
4379 u8 sq_state = MLX5_SQ_STATE_NA;
4380 u8 rq_state = MLX5_RQ_STATE_NA;
4381
4382 if (qp->sq.wqe_cnt) {
4383 err = query_raw_packet_qp_sq_state(dev, sq, &sq_state);
4384 if (err)
4385 return err;
4386 }
4387
4388 if (qp->rq.wqe_cnt) {
4389 err = query_raw_packet_qp_rq_state(dev, rq, &rq_state);
4390 if (err)
4391 return err;
4392 }
4393
4394 return sqrq_state_to_qp_state(sq_state, rq_state, qp,
4395 raw_packet_qp_state);
4396}
4397
4398static int query_qp_attr(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
4399 struct ib_qp_attr *qp_attr)
4400{
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004401 int outlen = MLX5_ST_SZ_BYTES(query_qp_out);
Eli Cohene126ba92013-07-07 17:25:49 +03004402 struct mlx5_qp_context *context;
4403 int mlx5_state;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004404 u32 *outb;
Eli Cohene126ba92013-07-07 17:25:49 +03004405 int err = 0;
4406
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004407 outb = kzalloc(outlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004408 if (!outb)
4409 return -ENOMEM;
4410
majd@mellanox.com19098df2016-01-14 19:13:03 +02004411 err = mlx5_core_qp_query(dev->mdev, &qp->trans_qp.base.mqp, outb,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004412 outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03004413 if (err)
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004414 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004415
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004416 /* FIXME: use MLX5_GET rather than mlx5_qp_context manual struct */
4417 context = (struct mlx5_qp_context *)MLX5_ADDR_OF(query_qp_out, outb, qpc);
4418
Eli Cohene126ba92013-07-07 17:25:49 +03004419 mlx5_state = be32_to_cpu(context->flags) >> 28;
4420
4421 qp->state = to_ib_qp_state(mlx5_state);
Eli Cohene126ba92013-07-07 17:25:49 +03004422 qp_attr->path_mtu = context->mtu_msgmax >> 5;
4423 qp_attr->path_mig_state =
4424 to_ib_mig_state((be32_to_cpu(context->flags) >> 11) & 0x3);
4425 qp_attr->qkey = be32_to_cpu(context->qkey);
4426 qp_attr->rq_psn = be32_to_cpu(context->rnr_nextrecvpsn) & 0xffffff;
4427 qp_attr->sq_psn = be32_to_cpu(context->next_send_psn) & 0xffffff;
4428 qp_attr->dest_qp_num = be32_to_cpu(context->log_pg_sz_remote_qpn) & 0xffffff;
4429 qp_attr->qp_access_flags =
4430 to_ib_qp_access_flags(be32_to_cpu(context->params2));
4431
4432 if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
4433 to_ib_ah_attr(dev, &qp_attr->ah_attr, &context->pri_path);
4434 to_ib_ah_attr(dev, &qp_attr->alt_ah_attr, &context->alt_path);
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03004435 qp_attr->alt_pkey_index =
4436 be16_to_cpu(context->alt_path.pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03004437 qp_attr->alt_port_num = qp_attr->alt_ah_attr.port_num;
4438 }
4439
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03004440 qp_attr->pkey_index = be16_to_cpu(context->pri_path.pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03004441 qp_attr->port_num = context->pri_path.port;
4442
4443 /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
4444 qp_attr->sq_draining = mlx5_state == MLX5_QP_STATE_SQ_DRAINING;
4445
4446 qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context->params1) >> 21) & 0x7);
4447
4448 qp_attr->max_dest_rd_atomic =
4449 1 << ((be32_to_cpu(context->params2) >> 21) & 0x7);
4450 qp_attr->min_rnr_timer =
4451 (be32_to_cpu(context->rnr_nextrecvpsn) >> 24) & 0x1f;
4452 qp_attr->timeout = context->pri_path.ackto_lt >> 3;
4453 qp_attr->retry_cnt = (be32_to_cpu(context->params1) >> 16) & 0x7;
4454 qp_attr->rnr_retry = (be32_to_cpu(context->params1) >> 13) & 0x7;
4455 qp_attr->alt_timeout = context->alt_path.ackto_lt >> 3;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004456
4457out:
4458 kfree(outb);
4459 return err;
4460}
4461
4462int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
4463 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
4464{
4465 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4466 struct mlx5_ib_qp *qp = to_mqp(ibqp);
4467 int err = 0;
4468 u8 raw_packet_qp_state;
4469
Yishai Hadas28d61372016-05-23 15:20:56 +03004470 if (ibqp->rwq_ind_tbl)
4471 return -ENOSYS;
4472
Haggai Erand16e91d2016-02-29 15:45:05 +02004473 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4474 return mlx5_ib_gsi_query_qp(ibqp, qp_attr, qp_attr_mask,
4475 qp_init_attr);
4476
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004477 mutex_lock(&qp->mutex);
4478
4479 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET) {
4480 err = query_raw_packet_qp_state(dev, qp, &raw_packet_qp_state);
4481 if (err)
4482 goto out;
4483 qp->state = raw_packet_qp_state;
4484 qp_attr->port_num = 1;
4485 } else {
4486 err = query_qp_attr(dev, qp, qp_attr);
4487 if (err)
4488 goto out;
4489 }
4490
4491 qp_attr->qp_state = qp->state;
Eli Cohene126ba92013-07-07 17:25:49 +03004492 qp_attr->cur_qp_state = qp_attr->qp_state;
4493 qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
4494 qp_attr->cap.max_recv_sge = qp->rq.max_gs;
4495
4496 if (!ibqp->uobject) {
Noa Osherovich0540d812016-06-04 15:15:32 +03004497 qp_attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +03004498 qp_attr->cap.max_send_sge = qp->sq.max_gs;
Noa Osherovich0540d812016-06-04 15:15:32 +03004499 qp_init_attr->qp_context = ibqp->qp_context;
Eli Cohene126ba92013-07-07 17:25:49 +03004500 } else {
4501 qp_attr->cap.max_send_wr = 0;
4502 qp_attr->cap.max_send_sge = 0;
4503 }
4504
Noa Osherovich0540d812016-06-04 15:15:32 +03004505 qp_init_attr->qp_type = ibqp->qp_type;
4506 qp_init_attr->recv_cq = ibqp->recv_cq;
4507 qp_init_attr->send_cq = ibqp->send_cq;
4508 qp_init_attr->srq = ibqp->srq;
4509 qp_attr->cap.max_inline_data = qp->max_inline_data;
Eli Cohene126ba92013-07-07 17:25:49 +03004510
4511 qp_init_attr->cap = qp_attr->cap;
4512
4513 qp_init_attr->create_flags = 0;
4514 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
4515 qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
4516
Leon Romanovsky051f2632015-12-20 12:16:11 +02004517 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
4518 qp_init_attr->create_flags |= IB_QP_CREATE_CROSS_CHANNEL;
4519 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
4520 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_SEND;
4521 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
4522 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_RECV;
Haggai Eranb11a4f92016-02-29 15:45:03 +02004523 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
4524 qp_init_attr->create_flags |= mlx5_ib_create_qp_sqpn_qp1();
Leon Romanovsky051f2632015-12-20 12:16:11 +02004525
Eli Cohene126ba92013-07-07 17:25:49 +03004526 qp_init_attr->sq_sig_type = qp->sq_signal_bits & MLX5_WQE_CTRL_CQ_UPDATE ?
4527 IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
4528
Eli Cohene126ba92013-07-07 17:25:49 +03004529out:
4530 mutex_unlock(&qp->mutex);
4531 return err;
4532}
4533
4534struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
4535 struct ib_ucontext *context,
4536 struct ib_udata *udata)
4537{
4538 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4539 struct mlx5_ib_xrcd *xrcd;
4540 int err;
4541
Saeed Mahameed938fe832015-05-28 22:28:41 +03004542 if (!MLX5_CAP_GEN(dev->mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +03004543 return ERR_PTR(-ENOSYS);
4544
4545 xrcd = kmalloc(sizeof(*xrcd), GFP_KERNEL);
4546 if (!xrcd)
4547 return ERR_PTR(-ENOMEM);
4548
Jack Morgenstein9603b612014-07-28 23:30:22 +03004549 err = mlx5_core_xrcd_alloc(dev->mdev, &xrcd->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03004550 if (err) {
4551 kfree(xrcd);
4552 return ERR_PTR(-ENOMEM);
4553 }
4554
4555 return &xrcd->ibxrcd;
4556}
4557
4558int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
4559{
4560 struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
4561 u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
4562 int err;
4563
Jack Morgenstein9603b612014-07-28 23:30:22 +03004564 err = mlx5_core_xrcd_dealloc(dev->mdev, xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03004565 if (err) {
4566 mlx5_ib_warn(dev, "failed to dealloc xrcdn 0x%x\n", xrcdn);
4567 return err;
4568 }
4569
4570 kfree(xrcd);
4571
4572 return 0;
4573}
Yishai Hadas79b20a62016-05-23 15:20:50 +03004574
Yishai Hadas350d0e42016-08-28 14:58:18 +03004575static void mlx5_ib_wq_event(struct mlx5_core_qp *core_qp, int type)
4576{
4577 struct mlx5_ib_rwq *rwq = to_mibrwq(core_qp);
4578 struct mlx5_ib_dev *dev = to_mdev(rwq->ibwq.device);
4579 struct ib_event event;
4580
4581 if (rwq->ibwq.event_handler) {
4582 event.device = rwq->ibwq.device;
4583 event.element.wq = &rwq->ibwq;
4584 switch (type) {
4585 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
4586 event.event = IB_EVENT_WQ_FATAL;
4587 break;
4588 default:
4589 mlx5_ib_warn(dev, "Unexpected event type %d on WQ %06x\n", type, core_qp->qpn);
4590 return;
4591 }
4592
4593 rwq->ibwq.event_handler(&event, rwq->ibwq.wq_context);
4594 }
4595}
4596
Yishai Hadas79b20a62016-05-23 15:20:50 +03004597static int create_rq(struct mlx5_ib_rwq *rwq, struct ib_pd *pd,
4598 struct ib_wq_init_attr *init_attr)
4599{
4600 struct mlx5_ib_dev *dev;
4601 __be64 *rq_pas0;
4602 void *in;
4603 void *rqc;
4604 void *wq;
4605 int inlen;
4606 int err;
4607
4608 dev = to_mdev(pd->device);
4609
4610 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + sizeof(u64) * rwq->rq_num_pas;
4611 in = mlx5_vzalloc(inlen);
4612 if (!in)
4613 return -ENOMEM;
4614
4615 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
4616 MLX5_SET(rqc, rqc, mem_rq_type,
4617 MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
4618 MLX5_SET(rqc, rqc, user_index, rwq->user_index);
4619 MLX5_SET(rqc, rqc, cqn, to_mcq(init_attr->cq)->mcq.cqn);
4620 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
4621 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
4622 wq = MLX5_ADDR_OF(rqc, rqc, wq);
4623 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
4624 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
4625 MLX5_SET(wq, wq, log_wq_stride, rwq->log_rq_stride);
4626 MLX5_SET(wq, wq, log_wq_sz, rwq->log_rq_size);
4627 MLX5_SET(wq, wq, pd, to_mpd(pd)->pdn);
4628 MLX5_SET(wq, wq, page_offset, rwq->rq_page_offset);
4629 MLX5_SET(wq, wq, log_wq_pg_sz, rwq->log_page_size);
4630 MLX5_SET(wq, wq, wq_signature, rwq->wq_sig);
4631 MLX5_SET64(wq, wq, dbr_addr, rwq->db.dma);
4632 rq_pas0 = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
4633 mlx5_ib_populate_pas(dev, rwq->umem, rwq->page_shift, rq_pas0, 0);
Yishai Hadas350d0e42016-08-28 14:58:18 +03004634 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004635 kvfree(in);
4636 return err;
4637}
4638
4639static int set_user_rq_size(struct mlx5_ib_dev *dev,
4640 struct ib_wq_init_attr *wq_init_attr,
4641 struct mlx5_ib_create_wq *ucmd,
4642 struct mlx5_ib_rwq *rwq)
4643{
4644 /* Sanity check RQ size before proceeding */
4645 if (wq_init_attr->max_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_wq_sz)))
4646 return -EINVAL;
4647
4648 if (!ucmd->rq_wqe_count)
4649 return -EINVAL;
4650
4651 rwq->wqe_count = ucmd->rq_wqe_count;
4652 rwq->wqe_shift = ucmd->rq_wqe_shift;
4653 rwq->buf_size = (rwq->wqe_count << rwq->wqe_shift);
4654 rwq->log_rq_stride = rwq->wqe_shift;
4655 rwq->log_rq_size = ilog2(rwq->wqe_count);
4656 return 0;
4657}
4658
4659static int prepare_user_rq(struct ib_pd *pd,
4660 struct ib_wq_init_attr *init_attr,
4661 struct ib_udata *udata,
4662 struct mlx5_ib_rwq *rwq)
4663{
4664 struct mlx5_ib_dev *dev = to_mdev(pd->device);
4665 struct mlx5_ib_create_wq ucmd = {};
4666 int err;
4667 size_t required_cmd_sz;
4668
4669 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
4670 if (udata->inlen < required_cmd_sz) {
4671 mlx5_ib_dbg(dev, "invalid inlen\n");
4672 return -EINVAL;
4673 }
4674
4675 if (udata->inlen > sizeof(ucmd) &&
4676 !ib_is_udata_cleared(udata, sizeof(ucmd),
4677 udata->inlen - sizeof(ucmd))) {
4678 mlx5_ib_dbg(dev, "inlen is not supported\n");
4679 return -EOPNOTSUPP;
4680 }
4681
4682 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
4683 mlx5_ib_dbg(dev, "copy failed\n");
4684 return -EFAULT;
4685 }
4686
4687 if (ucmd.comp_mask) {
4688 mlx5_ib_dbg(dev, "invalid comp mask\n");
4689 return -EOPNOTSUPP;
4690 }
4691
4692 if (ucmd.reserved) {
4693 mlx5_ib_dbg(dev, "invalid reserved\n");
4694 return -EOPNOTSUPP;
4695 }
4696
4697 err = set_user_rq_size(dev, init_attr, &ucmd, rwq);
4698 if (err) {
4699 mlx5_ib_dbg(dev, "err %d\n", err);
4700 return err;
4701 }
4702
4703 err = create_user_rq(dev, pd, rwq, &ucmd);
4704 if (err) {
4705 mlx5_ib_dbg(dev, "err %d\n", err);
4706 if (err)
4707 return err;
4708 }
4709
4710 rwq->user_index = ucmd.user_index;
4711 return 0;
4712}
4713
4714struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
4715 struct ib_wq_init_attr *init_attr,
4716 struct ib_udata *udata)
4717{
4718 struct mlx5_ib_dev *dev;
4719 struct mlx5_ib_rwq *rwq;
4720 struct mlx5_ib_create_wq_resp resp = {};
4721 size_t min_resp_len;
4722 int err;
4723
4724 if (!udata)
4725 return ERR_PTR(-ENOSYS);
4726
4727 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
4728 if (udata->outlen && udata->outlen < min_resp_len)
4729 return ERR_PTR(-EINVAL);
4730
4731 dev = to_mdev(pd->device);
4732 switch (init_attr->wq_type) {
4733 case IB_WQT_RQ:
4734 rwq = kzalloc(sizeof(*rwq), GFP_KERNEL);
4735 if (!rwq)
4736 return ERR_PTR(-ENOMEM);
4737 err = prepare_user_rq(pd, init_attr, udata, rwq);
4738 if (err)
4739 goto err;
4740 err = create_rq(rwq, pd, init_attr);
4741 if (err)
4742 goto err_user_rq;
4743 break;
4744 default:
4745 mlx5_ib_dbg(dev, "unsupported wq type %d\n",
4746 init_attr->wq_type);
4747 return ERR_PTR(-EINVAL);
4748 }
4749
Yishai Hadas350d0e42016-08-28 14:58:18 +03004750 rwq->ibwq.wq_num = rwq->core_qp.qpn;
Yishai Hadas79b20a62016-05-23 15:20:50 +03004751 rwq->ibwq.state = IB_WQS_RESET;
4752 if (udata->outlen) {
4753 resp.response_length = offsetof(typeof(resp), response_length) +
4754 sizeof(resp.response_length);
4755 err = ib_copy_to_udata(udata, &resp, resp.response_length);
4756 if (err)
4757 goto err_copy;
4758 }
4759
Yishai Hadas350d0e42016-08-28 14:58:18 +03004760 rwq->core_qp.event = mlx5_ib_wq_event;
4761 rwq->ibwq.event_handler = init_attr->event_handler;
Yishai Hadas79b20a62016-05-23 15:20:50 +03004762 return &rwq->ibwq;
4763
4764err_copy:
Yishai Hadas350d0e42016-08-28 14:58:18 +03004765 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004766err_user_rq:
4767 destroy_user_rq(pd, rwq);
4768err:
4769 kfree(rwq);
4770 return ERR_PTR(err);
4771}
4772
4773int mlx5_ib_destroy_wq(struct ib_wq *wq)
4774{
4775 struct mlx5_ib_dev *dev = to_mdev(wq->device);
4776 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
4777
Yishai Hadas350d0e42016-08-28 14:58:18 +03004778 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004779 destroy_user_rq(wq->pd, rwq);
4780 kfree(rwq);
4781
4782 return 0;
4783}
4784
Yishai Hadasc5f90922016-05-23 15:20:53 +03004785struct ib_rwq_ind_table *mlx5_ib_create_rwq_ind_table(struct ib_device *device,
4786 struct ib_rwq_ind_table_init_attr *init_attr,
4787 struct ib_udata *udata)
4788{
4789 struct mlx5_ib_dev *dev = to_mdev(device);
4790 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl;
4791 int sz = 1 << init_attr->log_ind_tbl_size;
4792 struct mlx5_ib_create_rwq_ind_tbl_resp resp = {};
4793 size_t min_resp_len;
4794 int inlen;
4795 int err;
4796 int i;
4797 u32 *in;
4798 void *rqtc;
4799
4800 if (udata->inlen > 0 &&
4801 !ib_is_udata_cleared(udata, 0,
4802 udata->inlen))
4803 return ERR_PTR(-EOPNOTSUPP);
4804
Maor Gottliebefd7f402016-10-27 16:36:40 +03004805 if (init_attr->log_ind_tbl_size >
4806 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size)) {
4807 mlx5_ib_dbg(dev, "log_ind_tbl_size = %d is bigger than supported = %d\n",
4808 init_attr->log_ind_tbl_size,
4809 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size));
4810 return ERR_PTR(-EINVAL);
4811 }
4812
Yishai Hadasc5f90922016-05-23 15:20:53 +03004813 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
4814 if (udata->outlen && udata->outlen < min_resp_len)
4815 return ERR_PTR(-EINVAL);
4816
4817 rwq_ind_tbl = kzalloc(sizeof(*rwq_ind_tbl), GFP_KERNEL);
4818 if (!rwq_ind_tbl)
4819 return ERR_PTR(-ENOMEM);
4820
4821 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
4822 in = mlx5_vzalloc(inlen);
4823 if (!in) {
4824 err = -ENOMEM;
4825 goto err;
4826 }
4827
4828 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
4829
4830 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
4831 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
4832
4833 for (i = 0; i < sz; i++)
4834 MLX5_SET(rqtc, rqtc, rq_num[i], init_attr->ind_tbl[i]->wq_num);
4835
4836 err = mlx5_core_create_rqt(dev->mdev, in, inlen, &rwq_ind_tbl->rqtn);
4837 kvfree(in);
4838
4839 if (err)
4840 goto err;
4841
4842 rwq_ind_tbl->ib_rwq_ind_tbl.ind_tbl_num = rwq_ind_tbl->rqtn;
4843 if (udata->outlen) {
4844 resp.response_length = offsetof(typeof(resp), response_length) +
4845 sizeof(resp.response_length);
4846 err = ib_copy_to_udata(udata, &resp, resp.response_length);
4847 if (err)
4848 goto err_copy;
4849 }
4850
4851 return &rwq_ind_tbl->ib_rwq_ind_tbl;
4852
4853err_copy:
4854 mlx5_core_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn);
4855err:
4856 kfree(rwq_ind_tbl);
4857 return ERR_PTR(err);
4858}
4859
4860int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
4861{
4862 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl = to_mrwq_ind_table(ib_rwq_ind_tbl);
4863 struct mlx5_ib_dev *dev = to_mdev(ib_rwq_ind_tbl->device);
4864
4865 mlx5_core_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn);
4866
4867 kfree(rwq_ind_tbl);
4868 return 0;
4869}
4870
Yishai Hadas79b20a62016-05-23 15:20:50 +03004871int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
4872 u32 wq_attr_mask, struct ib_udata *udata)
4873{
4874 struct mlx5_ib_dev *dev = to_mdev(wq->device);
4875 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
4876 struct mlx5_ib_modify_wq ucmd = {};
4877 size_t required_cmd_sz;
4878 int curr_wq_state;
4879 int wq_state;
4880 int inlen;
4881 int err;
4882 void *rqc;
4883 void *in;
4884
4885 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
4886 if (udata->inlen < required_cmd_sz)
4887 return -EINVAL;
4888
4889 if (udata->inlen > sizeof(ucmd) &&
4890 !ib_is_udata_cleared(udata, sizeof(ucmd),
4891 udata->inlen - sizeof(ucmd)))
4892 return -EOPNOTSUPP;
4893
4894 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
4895 return -EFAULT;
4896
4897 if (ucmd.comp_mask || ucmd.reserved)
4898 return -EOPNOTSUPP;
4899
4900 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
4901 in = mlx5_vzalloc(inlen);
4902 if (!in)
4903 return -ENOMEM;
4904
4905 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
4906
4907 curr_wq_state = (wq_attr_mask & IB_WQ_CUR_STATE) ?
4908 wq_attr->curr_wq_state : wq->state;
4909 wq_state = (wq_attr_mask & IB_WQ_STATE) ?
4910 wq_attr->wq_state : curr_wq_state;
4911 if (curr_wq_state == IB_WQS_ERR)
4912 curr_wq_state = MLX5_RQC_STATE_ERR;
4913 if (wq_state == IB_WQS_ERR)
4914 wq_state = MLX5_RQC_STATE_ERR;
4915 MLX5_SET(modify_rq_in, in, rq_state, curr_wq_state);
4916 MLX5_SET(rqc, rqc, state, wq_state);
4917
Yishai Hadas350d0e42016-08-28 14:58:18 +03004918 err = mlx5_core_modify_rq(dev->mdev, rwq->core_qp.qpn, in, inlen);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004919 kvfree(in);
4920 if (!err)
4921 rwq->ibwq.state = (wq_state == MLX5_RQC_STATE_ERR) ? IB_WQS_ERR : wq_state;
4922
4923 return err;
4924}