blob: 240fbb0c63ba1ec59dce68f279d76ea29c16ace6 [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{
912 enum mlx5_ib_latency_class lc = MLX5_IB_LATENCY_CLASS_LOW;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200913 struct mlx5_bfreg_info *bfregi;
Eli Cohene126ba92013-07-07 17:25:49 +0300914 int uar_index;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300915 void *qpc;
Eli Cohen2f5ff262017-01-03 23:55:21 +0200916 int bfregn;
Eli Cohene126ba92013-07-07 17:25:49 +0300917 int err;
918
Eli Cohen2f5ff262017-01-03 23:55:21 +0200919 bfregi = &dev->mdev->priv.bfregi;
Erez Shitritf0313962016-02-21 16:27:17 +0200920 if (init_attr->create_flags & ~(IB_QP_CREATE_SIGNATURE_EN |
921 IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
Haggai Eranb11a4f92016-02-29 15:45:03 +0200922 IB_QP_CREATE_IPOIB_UD_LSO |
923 mlx5_ib_create_qp_sqpn_qp1()))
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200924 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300925
926 if (init_attr->qp_type == MLX5_IB_QPT_REG_UMR)
927 lc = MLX5_IB_LATENCY_CLASS_FAST_PATH;
928
Eli Cohen2f5ff262017-01-03 23:55:21 +0200929 bfregn = alloc_bfreg(bfregi, lc);
930 if (bfregn < 0) {
Eli Cohene126ba92013-07-07 17:25:49 +0300931 mlx5_ib_dbg(dev, "\n");
932 return -ENOMEM;
933 }
934
Eli Cohen2f5ff262017-01-03 23:55:21 +0200935 qp->bf = &bfregi->bfs[bfregn];
Eli Cohene126ba92013-07-07 17:25:49 +0300936 uar_index = qp->bf->uar->index;
937
938 err = calc_sq_size(dev, init_attr, qp);
939 if (err < 0) {
940 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200941 goto err_bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300942 }
943
944 qp->rq.offset = 0;
945 qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
majd@mellanox.com19098df2016-01-14 19:13:03 +0200946 base->ubuffer.buf_size = err + (qp->rq.wqe_cnt << qp->rq.wqe_shift);
Eli Cohene126ba92013-07-07 17:25:49 +0300947
majd@mellanox.com19098df2016-01-14 19:13:03 +0200948 err = mlx5_buf_alloc(dev->mdev, base->ubuffer.buf_size, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +0300949 if (err) {
950 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohen2f5ff262017-01-03 23:55:21 +0200951 goto err_bfreg;
Eli Cohene126ba92013-07-07 17:25:49 +0300952 }
953
954 qp->sq.qend = mlx5_get_send_wqe(qp, qp->sq.wqe_cnt);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300955 *inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
956 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * qp->buf.npages;
Eli Cohene126ba92013-07-07 17:25:49 +0300957 *in = mlx5_vzalloc(*inlen);
958 if (!*in) {
959 err = -ENOMEM;
960 goto err_buf;
961 }
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300962
963 qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
964 MLX5_SET(qpc, qpc, uar_page, uar_index);
965 MLX5_SET(qpc, qpc, log_page_size, qp->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
966
Eli Cohene126ba92013-07-07 17:25:49 +0300967 /* Set "fast registration enabled" for all kernel QPs */
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300968 MLX5_SET(qpc, qpc, fre, 1);
969 MLX5_SET(qpc, qpc, rlky, 1);
Eli Cohene126ba92013-07-07 17:25:49 +0300970
Haggai Eranb11a4f92016-02-29 15:45:03 +0200971 if (init_attr->create_flags & mlx5_ib_create_qp_sqpn_qp1()) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300972 MLX5_SET(qpc, qpc, deth_sqpn, 1);
Haggai Eranb11a4f92016-02-29 15:45:03 +0200973 qp->flags |= MLX5_IB_QP_SQPN_QP1;
974 }
975
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +0300976 mlx5_fill_page_array(&qp->buf,
977 (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas));
Eli Cohene126ba92013-07-07 17:25:49 +0300978
Jack Morgenstein9603b612014-07-28 23:30:22 +0300979 err = mlx5_db_alloc(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300980 if (err) {
981 mlx5_ib_dbg(dev, "err %d\n", err);
982 goto err_free;
983 }
984
Eli Cohene126ba92013-07-07 17:25:49 +0300985 qp->sq.wrid = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wrid), GFP_KERNEL);
986 qp->sq.wr_data = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wr_data), GFP_KERNEL);
987 qp->rq.wrid = kmalloc(qp->rq.wqe_cnt * sizeof(*qp->rq.wrid), GFP_KERNEL);
988 qp->sq.w_list = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.w_list), GFP_KERNEL);
989 qp->sq.wqe_head = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wqe_head), GFP_KERNEL);
990
991 if (!qp->sq.wrid || !qp->sq.wr_data || !qp->rq.wrid ||
992 !qp->sq.w_list || !qp->sq.wqe_head) {
993 err = -ENOMEM;
994 goto err_wrid;
995 }
996 qp->create_type = MLX5_QP_KERNEL;
997
998 return 0;
999
1000err_wrid:
Eli Cohene126ba92013-07-07 17:25:49 +03001001 kfree(qp->sq.wqe_head);
1002 kfree(qp->sq.w_list);
1003 kfree(qp->sq.wrid);
1004 kfree(qp->sq.wr_data);
1005 kfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001006 mlx5_db_free(dev->mdev, &qp->db);
Eli Cohene126ba92013-07-07 17:25:49 +03001007
1008err_free:
Al Viro479163f2014-11-20 08:13:57 +00001009 kvfree(*in);
Eli Cohene126ba92013-07-07 17:25:49 +03001010
1011err_buf:
Jack Morgenstein9603b612014-07-28 23:30:22 +03001012 mlx5_buf_free(dev->mdev, &qp->buf);
Eli Cohene126ba92013-07-07 17:25:49 +03001013
Eli Cohen2f5ff262017-01-03 23:55:21 +02001014err_bfreg:
1015 free_bfreg(&dev->mdev->priv.bfregi, bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +03001016 return err;
1017}
1018
1019static void destroy_qp_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1020{
Eli Cohene126ba92013-07-07 17:25:49 +03001021 kfree(qp->sq.wqe_head);
1022 kfree(qp->sq.w_list);
1023 kfree(qp->sq.wrid);
1024 kfree(qp->sq.wr_data);
1025 kfree(qp->rq.wrid);
Eli Cohenf4044da2017-01-03 23:55:20 +02001026 mlx5_db_free(dev->mdev, &qp->db);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001027 mlx5_buf_free(dev->mdev, &qp->buf);
Eli Cohen2f5ff262017-01-03 23:55:21 +02001028 free_bfreg(&dev->mdev->priv.bfregi, qp->bf->bfregn);
Eli Cohene126ba92013-07-07 17:25:49 +03001029}
1030
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001031static u32 get_rx_type(struct mlx5_ib_qp *qp, struct ib_qp_init_attr *attr)
Eli Cohene126ba92013-07-07 17:25:49 +03001032{
1033 if (attr->srq || (attr->qp_type == IB_QPT_XRC_TGT) ||
1034 (attr->qp_type == IB_QPT_XRC_INI))
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001035 return MLX5_SRQ_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001036 else if (!qp->has_rq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001037 return MLX5_ZERO_LEN_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001038 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001039 return MLX5_NON_ZERO_RQ;
Eli Cohene126ba92013-07-07 17:25:49 +03001040}
1041
1042static int is_connected(enum ib_qp_type qp_type)
1043{
1044 if (qp_type == IB_QPT_RC || qp_type == IB_QPT_UC)
1045 return 1;
1046
1047 return 0;
1048}
1049
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001050static int create_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
1051 struct mlx5_ib_sq *sq, u32 tdn)
1052{
Saeed Mahameedc4f287c2016-07-19 20:17:12 +03001053 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001054 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1055
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001056 MLX5_SET(tisc, tisc, transport_domain, tdn);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001057 return mlx5_core_create_tis(dev->mdev, in, sizeof(in), &sq->tisn);
1058}
1059
1060static void destroy_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
1061 struct mlx5_ib_sq *sq)
1062{
1063 mlx5_core_destroy_tis(dev->mdev, sq->tisn);
1064}
1065
1066static int create_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1067 struct mlx5_ib_sq *sq, void *qpin,
1068 struct ib_pd *pd)
1069{
1070 struct mlx5_ib_ubuffer *ubuffer = &sq->ubuffer;
1071 __be64 *pas;
1072 void *in;
1073 void *sqc;
1074 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1075 void *wq;
1076 int inlen;
1077 int err;
1078 int page_shift = 0;
1079 int npages;
1080 int ncont = 0;
1081 u32 offset = 0;
1082
1083 err = mlx5_ib_umem_get(dev, pd, ubuffer->buf_addr, ubuffer->buf_size,
1084 &sq->ubuffer.umem, &npages, &page_shift,
1085 &ncont, &offset);
1086 if (err)
1087 return err;
1088
1089 inlen = MLX5_ST_SZ_BYTES(create_sq_in) + sizeof(u64) * ncont;
1090 in = mlx5_vzalloc(inlen);
1091 if (!in) {
1092 err = -ENOMEM;
1093 goto err_umem;
1094 }
1095
1096 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1097 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
1098 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1099 MLX5_SET(sqc, sqc, user_index, MLX5_GET(qpc, qpc, user_index));
1100 MLX5_SET(sqc, sqc, cqn, MLX5_GET(qpc, qpc, cqn_snd));
1101 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1102 MLX5_SET(sqc, sqc, tis_num_0, sq->tisn);
1103
1104 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1105 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1106 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1107 MLX5_SET(wq, wq, uar_page, MLX5_GET(qpc, qpc, uar_page));
1108 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1109 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1110 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_sq_size));
1111 MLX5_SET(wq, wq, log_wq_pg_sz, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1112 MLX5_SET(wq, wq, page_offset, offset);
1113
1114 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1115 mlx5_ib_populate_pas(dev, sq->ubuffer.umem, page_shift, pas, 0);
1116
1117 err = mlx5_core_create_sq_tracked(dev->mdev, in, inlen, &sq->base.mqp);
1118
1119 kvfree(in);
1120
1121 if (err)
1122 goto err_umem;
1123
1124 return 0;
1125
1126err_umem:
1127 ib_umem_release(sq->ubuffer.umem);
1128 sq->ubuffer.umem = NULL;
1129
1130 return err;
1131}
1132
1133static void destroy_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1134 struct mlx5_ib_sq *sq)
1135{
1136 mlx5_core_destroy_sq_tracked(dev->mdev, &sq->base.mqp);
1137 ib_umem_release(sq->ubuffer.umem);
1138}
1139
1140static int get_rq_pas_size(void *qpc)
1141{
1142 u32 log_page_size = MLX5_GET(qpc, qpc, log_page_size) + 12;
1143 u32 log_rq_stride = MLX5_GET(qpc, qpc, log_rq_stride);
1144 u32 log_rq_size = MLX5_GET(qpc, qpc, log_rq_size);
1145 u32 page_offset = MLX5_GET(qpc, qpc, page_offset);
1146 u32 po_quanta = 1 << (log_page_size - 6);
1147 u32 rq_sz = 1 << (log_rq_size + 4 + log_rq_stride);
1148 u32 page_size = 1 << log_page_size;
1149 u32 rq_sz_po = rq_sz + (page_offset * po_quanta);
1150 u32 rq_num_pas = (rq_sz_po + page_size - 1) / page_size;
1151
1152 return rq_num_pas * sizeof(u64);
1153}
1154
1155static int create_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1156 struct mlx5_ib_rq *rq, void *qpin)
1157{
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001158 struct mlx5_ib_qp *mqp = rq->base.container_mibqp;
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001159 __be64 *pas;
1160 __be64 *qp_pas;
1161 void *in;
1162 void *rqc;
1163 void *wq;
1164 void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1165 int inlen;
1166 int err;
1167 u32 rq_pas_size = get_rq_pas_size(qpc);
1168
1169 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + rq_pas_size;
1170 in = mlx5_vzalloc(inlen);
1171 if (!in)
1172 return -ENOMEM;
1173
1174 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
1175 MLX5_SET(rqc, rqc, vsd, 1);
1176 MLX5_SET(rqc, rqc, mem_rq_type, MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
1177 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
1178 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
1179 MLX5_SET(rqc, rqc, user_index, MLX5_GET(qpc, qpc, user_index));
1180 MLX5_SET(rqc, rqc, cqn, MLX5_GET(qpc, qpc, cqn_rcv));
1181
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001182 if (mqp->flags & MLX5_IB_QP_CAP_SCATTER_FCS)
1183 MLX5_SET(rqc, rqc, scatter_fcs, 1);
1184
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001185 wq = MLX5_ADDR_OF(rqc, rqc, wq);
1186 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1187 MLX5_SET(wq, wq, end_padding_mode,
Maor Gottlieb01581fb2016-01-28 17:51:49 +02001188 MLX5_GET(qpc, qpc, end_padding_mode));
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001189 MLX5_SET(wq, wq, page_offset, MLX5_GET(qpc, qpc, page_offset));
1190 MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
1191 MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
1192 MLX5_SET(wq, wq, log_wq_stride, MLX5_GET(qpc, qpc, log_rq_stride) + 4);
1193 MLX5_SET(wq, wq, log_wq_pg_sz, MLX5_GET(qpc, qpc, log_page_size));
1194 MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_rq_size));
1195
1196 pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
1197 qp_pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, qpin, pas);
1198 memcpy(pas, qp_pas, rq_pas_size);
1199
1200 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rq->base.mqp);
1201
1202 kvfree(in);
1203
1204 return err;
1205}
1206
1207static void destroy_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1208 struct mlx5_ib_rq *rq)
1209{
1210 mlx5_core_destroy_rq_tracked(dev->mdev, &rq->base.mqp);
1211}
1212
1213static int create_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1214 struct mlx5_ib_rq *rq, u32 tdn)
1215{
1216 u32 *in;
1217 void *tirc;
1218 int inlen;
1219 int err;
1220
1221 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
1222 in = mlx5_vzalloc(inlen);
1223 if (!in)
1224 return -ENOMEM;
1225
1226 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1227 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
1228 MLX5_SET(tirc, tirc, inline_rqn, rq->base.mqp.qpn);
1229 MLX5_SET(tirc, tirc, transport_domain, tdn);
1230
1231 err = mlx5_core_create_tir(dev->mdev, in, inlen, &rq->tirn);
1232
1233 kvfree(in);
1234
1235 return err;
1236}
1237
1238static void destroy_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1239 struct mlx5_ib_rq *rq)
1240{
1241 mlx5_core_destroy_tir(dev->mdev, rq->tirn);
1242}
1243
1244static int create_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001245 u32 *in,
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001246 struct ib_pd *pd)
1247{
1248 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1249 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1250 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1251 struct ib_uobject *uobj = pd->uobject;
1252 struct ib_ucontext *ucontext = uobj->context;
1253 struct mlx5_ib_ucontext *mucontext = to_mucontext(ucontext);
1254 int err;
1255 u32 tdn = mucontext->tdn;
1256
1257 if (qp->sq.wqe_cnt) {
1258 err = create_raw_packet_qp_tis(dev, sq, tdn);
1259 if (err)
1260 return err;
1261
1262 err = create_raw_packet_qp_sq(dev, sq, in, pd);
1263 if (err)
1264 goto err_destroy_tis;
1265
1266 sq->base.container_mibqp = qp;
1267 }
1268
1269 if (qp->rq.wqe_cnt) {
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001270 rq->base.container_mibqp = qp;
1271
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001272 err = create_raw_packet_qp_rq(dev, rq, in);
1273 if (err)
1274 goto err_destroy_sq;
1275
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001276
1277 err = create_raw_packet_qp_tir(dev, rq, tdn);
1278 if (err)
1279 goto err_destroy_rq;
1280 }
1281
1282 qp->trans_qp.base.mqp.qpn = qp->sq.wqe_cnt ? sq->base.mqp.qpn :
1283 rq->base.mqp.qpn;
1284
1285 return 0;
1286
1287err_destroy_rq:
1288 destroy_raw_packet_qp_rq(dev, rq);
1289err_destroy_sq:
1290 if (!qp->sq.wqe_cnt)
1291 return err;
1292 destroy_raw_packet_qp_sq(dev, sq);
1293err_destroy_tis:
1294 destroy_raw_packet_qp_tis(dev, sq);
1295
1296 return err;
1297}
1298
1299static void destroy_raw_packet_qp(struct mlx5_ib_dev *dev,
1300 struct mlx5_ib_qp *qp)
1301{
1302 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
1303 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1304 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1305
1306 if (qp->rq.wqe_cnt) {
1307 destroy_raw_packet_qp_tir(dev, rq);
1308 destroy_raw_packet_qp_rq(dev, rq);
1309 }
1310
1311 if (qp->sq.wqe_cnt) {
1312 destroy_raw_packet_qp_sq(dev, sq);
1313 destroy_raw_packet_qp_tis(dev, sq);
1314 }
1315}
1316
1317static void raw_packet_qp_copy_info(struct mlx5_ib_qp *qp,
1318 struct mlx5_ib_raw_packet_qp *raw_packet_qp)
1319{
1320 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
1321 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1322
1323 sq->sq = &qp->sq;
1324 rq->rq = &qp->rq;
1325 sq->doorbell = &qp->db;
1326 rq->doorbell = &qp->db;
1327}
1328
Yishai Hadas28d61372016-05-23 15:20:56 +03001329static void destroy_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1330{
1331 mlx5_core_destroy_tir(dev->mdev, qp->rss_qp.tirn);
1332}
1333
1334static int create_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
1335 struct ib_pd *pd,
1336 struct ib_qp_init_attr *init_attr,
1337 struct ib_udata *udata)
1338{
1339 struct ib_uobject *uobj = pd->uobject;
1340 struct ib_ucontext *ucontext = uobj->context;
1341 struct mlx5_ib_ucontext *mucontext = to_mucontext(ucontext);
1342 struct mlx5_ib_create_qp_resp resp = {};
1343 int inlen;
1344 int err;
1345 u32 *in;
1346 void *tirc;
1347 void *hfso;
1348 u32 selected_fields = 0;
1349 size_t min_resp_len;
1350 u32 tdn = mucontext->tdn;
1351 struct mlx5_ib_create_qp_rss ucmd = {};
1352 size_t required_cmd_sz;
1353
1354 if (init_attr->qp_type != IB_QPT_RAW_PACKET)
1355 return -EOPNOTSUPP;
1356
1357 if (init_attr->create_flags || init_attr->send_cq)
1358 return -EINVAL;
1359
Eli Cohen2f5ff262017-01-03 23:55:21 +02001360 min_resp_len = offsetof(typeof(resp), bfreg_index) + sizeof(resp.bfreg_index);
Yishai Hadas28d61372016-05-23 15:20:56 +03001361 if (udata->outlen < min_resp_len)
1362 return -EINVAL;
1363
1364 required_cmd_sz = offsetof(typeof(ucmd), reserved1) + sizeof(ucmd.reserved1);
1365 if (udata->inlen < required_cmd_sz) {
1366 mlx5_ib_dbg(dev, "invalid inlen\n");
1367 return -EINVAL;
1368 }
1369
1370 if (udata->inlen > sizeof(ucmd) &&
1371 !ib_is_udata_cleared(udata, sizeof(ucmd),
1372 udata->inlen - sizeof(ucmd))) {
1373 mlx5_ib_dbg(dev, "inlen is not supported\n");
1374 return -EOPNOTSUPP;
1375 }
1376
1377 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
1378 mlx5_ib_dbg(dev, "copy failed\n");
1379 return -EFAULT;
1380 }
1381
1382 if (ucmd.comp_mask) {
1383 mlx5_ib_dbg(dev, "invalid comp mask\n");
1384 return -EOPNOTSUPP;
1385 }
1386
1387 if (memchr_inv(ucmd.reserved, 0, sizeof(ucmd.reserved)) || ucmd.reserved1) {
1388 mlx5_ib_dbg(dev, "invalid reserved\n");
1389 return -EOPNOTSUPP;
1390 }
1391
1392 err = ib_copy_to_udata(udata, &resp, min_resp_len);
1393 if (err) {
1394 mlx5_ib_dbg(dev, "copy failed\n");
1395 return -EINVAL;
1396 }
1397
1398 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
1399 in = mlx5_vzalloc(inlen);
1400 if (!in)
1401 return -ENOMEM;
1402
1403 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1404 MLX5_SET(tirc, tirc, disp_type,
1405 MLX5_TIRC_DISP_TYPE_INDIRECT);
1406 MLX5_SET(tirc, tirc, indirect_table,
1407 init_attr->rwq_ind_tbl->ind_tbl_num);
1408 MLX5_SET(tirc, tirc, transport_domain, tdn);
1409
1410 hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1411 switch (ucmd.rx_hash_function) {
1412 case MLX5_RX_HASH_FUNC_TOEPLITZ:
1413 {
1414 void *rss_key = MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
1415 size_t len = MLX5_FLD_SZ_BYTES(tirc, rx_hash_toeplitz_key);
1416
1417 if (len != ucmd.rx_key_len) {
1418 err = -EINVAL;
1419 goto err;
1420 }
1421
1422 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_TOEPLITZ);
1423 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1424 memcpy(rss_key, ucmd.rx_hash_key, len);
1425 break;
1426 }
1427 default:
1428 err = -EOPNOTSUPP;
1429 goto err;
1430 }
1431
1432 if (!ucmd.rx_hash_fields_mask) {
1433 /* special case when this TIR serves as steering entry without hashing */
1434 if (!init_attr->rwq_ind_tbl->log_ind_tbl_size)
1435 goto create_tir;
1436 err = -EINVAL;
1437 goto err;
1438 }
1439
1440 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1441 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4)) &&
1442 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1443 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))) {
1444 err = -EINVAL;
1445 goto err;
1446 }
1447
1448 /* If none of IPV4 & IPV6 SRC/DST was set - this bit field is ignored */
1449 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1450 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4))
1451 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1452 MLX5_L3_PROT_TYPE_IPV4);
1453 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
1454 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1455 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1456 MLX5_L3_PROT_TYPE_IPV6);
1457
1458 if (((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1459 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP)) &&
1460 ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1461 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))) {
1462 err = -EINVAL;
1463 goto err;
1464 }
1465
1466 /* If none of TCP & UDP SRC/DST was set - this bit field is ignored */
1467 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1468 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP))
1469 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1470 MLX5_L4_PROT_TYPE_TCP);
1471 else if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
1472 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1473 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1474 MLX5_L4_PROT_TYPE_UDP);
1475
1476 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
1477 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6))
1478 selected_fields |= MLX5_HASH_FIELD_SEL_SRC_IP;
1479
1480 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4) ||
1481 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
1482 selected_fields |= MLX5_HASH_FIELD_SEL_DST_IP;
1483
1484 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
1485 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP))
1486 selected_fields |= MLX5_HASH_FIELD_SEL_L4_SPORT;
1487
1488 if ((ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP) ||
1489 (ucmd.rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
1490 selected_fields |= MLX5_HASH_FIELD_SEL_L4_DPORT;
1491
1492 MLX5_SET(rx_hash_field_select, hfso, selected_fields, selected_fields);
1493
1494create_tir:
1495 err = mlx5_core_create_tir(dev->mdev, in, inlen, &qp->rss_qp.tirn);
1496
1497 if (err)
1498 goto err;
1499
1500 kvfree(in);
1501 /* qpn is reserved for that QP */
1502 qp->trans_qp.base.mqp.qpn = 0;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03001503 qp->flags |= MLX5_IB_QP_RSS;
Yishai Hadas28d61372016-05-23 15:20:56 +03001504 return 0;
1505
1506err:
1507 kvfree(in);
1508 return err;
1509}
1510
Eli Cohene126ba92013-07-07 17:25:49 +03001511static int create_qp_common(struct mlx5_ib_dev *dev, struct ib_pd *pd,
1512 struct ib_qp_init_attr *init_attr,
1513 struct ib_udata *udata, struct mlx5_ib_qp *qp)
1514{
1515 struct mlx5_ib_resources *devr = &dev->devr;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001516 int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
Saeed Mahameed938fe832015-05-28 22:28:41 +03001517 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001518 struct mlx5_ib_create_qp_resp resp;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001519 struct mlx5_ib_cq *send_cq;
1520 struct mlx5_ib_cq *recv_cq;
1521 unsigned long flags;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001522 u32 uidx = MLX5_IB_DEFAULT_UIDX;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001523 struct mlx5_ib_create_qp ucmd;
1524 struct mlx5_ib_qp_base *base;
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001525 void *qpc;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001526 u32 *in;
1527 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03001528
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001529 base = init_attr->qp_type == IB_QPT_RAW_PACKET ?
1530 &qp->raw_packet_qp.rq.base :
1531 &qp->trans_qp.base;
1532
Eli Cohene126ba92013-07-07 17:25:49 +03001533 mutex_init(&qp->mutex);
1534 spin_lock_init(&qp->sq.lock);
1535 spin_lock_init(&qp->rq.lock);
1536
Yishai Hadas28d61372016-05-23 15:20:56 +03001537 if (init_attr->rwq_ind_tbl) {
1538 if (!udata)
1539 return -ENOSYS;
1540
1541 err = create_rss_raw_qp_tir(dev, qp, pd, init_attr, udata);
1542 return err;
1543 }
1544
Eli Cohenf360d882014-04-02 00:10:16 +03001545 if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001546 if (!MLX5_CAP_GEN(mdev, block_lb_mc)) {
Eli Cohenf360d882014-04-02 00:10:16 +03001547 mlx5_ib_dbg(dev, "block multicast loopback isn't supported\n");
1548 return -EINVAL;
1549 } else {
1550 qp->flags |= MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK;
1551 }
1552 }
1553
Leon Romanovsky051f2632015-12-20 12:16:11 +02001554 if (init_attr->create_flags &
1555 (IB_QP_CREATE_CROSS_CHANNEL |
1556 IB_QP_CREATE_MANAGED_SEND |
1557 IB_QP_CREATE_MANAGED_RECV)) {
1558 if (!MLX5_CAP_GEN(mdev, cd)) {
1559 mlx5_ib_dbg(dev, "cross-channel isn't supported\n");
1560 return -EINVAL;
1561 }
1562 if (init_attr->create_flags & IB_QP_CREATE_CROSS_CHANNEL)
1563 qp->flags |= MLX5_IB_QP_CROSS_CHANNEL;
1564 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_SEND)
1565 qp->flags |= MLX5_IB_QP_MANAGED_SEND;
1566 if (init_attr->create_flags & IB_QP_CREATE_MANAGED_RECV)
1567 qp->flags |= MLX5_IB_QP_MANAGED_RECV;
1568 }
Erez Shitritf0313962016-02-21 16:27:17 +02001569
1570 if (init_attr->qp_type == IB_QPT_UD &&
1571 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO))
1572 if (!MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
1573 mlx5_ib_dbg(dev, "ipoib UD lso qp isn't supported\n");
1574 return -EOPNOTSUPP;
1575 }
1576
Majd Dibbiny358e42e2016-04-17 17:19:37 +03001577 if (init_attr->create_flags & IB_QP_CREATE_SCATTER_FCS) {
1578 if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
1579 mlx5_ib_dbg(dev, "Scatter FCS is supported only for Raw Packet QPs");
1580 return -EOPNOTSUPP;
1581 }
1582 if (!MLX5_CAP_GEN(dev->mdev, eth_net_offloads) ||
1583 !MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
1584 mlx5_ib_dbg(dev, "Scatter FCS isn't supported\n");
1585 return -EOPNOTSUPP;
1586 }
1587 qp->flags |= MLX5_IB_QP_CAP_SCATTER_FCS;
1588 }
1589
Eli Cohene126ba92013-07-07 17:25:49 +03001590 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
1591 qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;
1592
1593 if (pd && pd->uobject) {
1594 if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
1595 mlx5_ib_dbg(dev, "copy failed\n");
1596 return -EFAULT;
1597 }
1598
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001599 err = get_qp_user_index(to_mucontext(pd->uobject->context),
1600 &ucmd, udata->inlen, &uidx);
1601 if (err)
1602 return err;
1603
Eli Cohene126ba92013-07-07 17:25:49 +03001604 qp->wq_sig = !!(ucmd.flags & MLX5_QP_FLAG_SIGNATURE);
1605 qp->scat_cqe = !!(ucmd.flags & MLX5_QP_FLAG_SCATTER_CQE);
1606 } else {
1607 qp->wq_sig = !!wq_signature;
1608 }
1609
1610 qp->has_rq = qp_has_rq(init_attr);
1611 err = set_rq_size(dev, &init_attr->cap, qp->has_rq,
1612 qp, (pd && pd->uobject) ? &ucmd : NULL);
1613 if (err) {
1614 mlx5_ib_dbg(dev, "err %d\n", err);
1615 return err;
1616 }
1617
1618 if (pd) {
1619 if (pd->uobject) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001620 __u32 max_wqes =
1621 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Eli Cohene126ba92013-07-07 17:25:49 +03001622 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d)\n", ucmd.sq_wqe_count);
1623 if (ucmd.rq_wqe_shift != qp->rq.wqe_shift ||
1624 ucmd.rq_wqe_count != qp->rq.wqe_cnt) {
1625 mlx5_ib_dbg(dev, "invalid rq params\n");
1626 return -EINVAL;
1627 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03001628 if (ucmd.sq_wqe_count > max_wqes) {
Eli Cohene126ba92013-07-07 17:25:49 +03001629 mlx5_ib_dbg(dev, "requested sq_wqe_count (%d) > max allowed (%d)\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +03001630 ucmd.sq_wqe_count, max_wqes);
Eli Cohene126ba92013-07-07 17:25:49 +03001631 return -EINVAL;
1632 }
Haggai Eranb11a4f92016-02-29 15:45:03 +02001633 if (init_attr->create_flags &
1634 mlx5_ib_create_qp_sqpn_qp1()) {
1635 mlx5_ib_dbg(dev, "user-space is not allowed to create UD QPs spoofing as QP1\n");
1636 return -EINVAL;
1637 }
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001638 err = create_user_qp(dev, pd, qp, udata, init_attr, &in,
1639 &resp, &inlen, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001640 if (err)
1641 mlx5_ib_dbg(dev, "err %d\n", err);
1642 } else {
majd@mellanox.com19098df2016-01-14 19:13:03 +02001643 err = create_kernel_qp(dev, init_attr, qp, &in, &inlen,
1644 base);
Eli Cohene126ba92013-07-07 17:25:49 +03001645 if (err)
1646 mlx5_ib_dbg(dev, "err %d\n", err);
Eli Cohene126ba92013-07-07 17:25:49 +03001647 }
1648
1649 if (err)
1650 return err;
1651 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001652 in = mlx5_vzalloc(inlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001653 if (!in)
1654 return -ENOMEM;
1655
1656 qp->create_type = MLX5_QP_EMPTY;
1657 }
1658
1659 if (is_sqp(init_attr->qp_type))
1660 qp->port = init_attr->port_num;
1661
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001662 qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
1663
1664 MLX5_SET(qpc, qpc, st, to_mlx5_st(init_attr->qp_type));
1665 MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
Eli Cohene126ba92013-07-07 17:25:49 +03001666
1667 if (init_attr->qp_type != MLX5_IB_QPT_REG_UMR)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001668 MLX5_SET(qpc, qpc, pd, to_mpd(pd ? pd : devr->p0)->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001669 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001670 MLX5_SET(qpc, qpc, latency_sensitive, 1);
1671
Eli Cohene126ba92013-07-07 17:25:49 +03001672
1673 if (qp->wq_sig)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001674 MLX5_SET(qpc, qpc, wq_signature, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001675
Eli Cohenf360d882014-04-02 00:10:16 +03001676 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001677 MLX5_SET(qpc, qpc, block_lb_mc, 1);
Eli Cohenf360d882014-04-02 00:10:16 +03001678
Leon Romanovsky051f2632015-12-20 12:16:11 +02001679 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001680 MLX5_SET(qpc, qpc, cd_master, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001681 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001682 MLX5_SET(qpc, qpc, cd_slave_send, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001683 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001684 MLX5_SET(qpc, qpc, cd_slave_receive, 1);
Leon Romanovsky051f2632015-12-20 12:16:11 +02001685
Eli Cohene126ba92013-07-07 17:25:49 +03001686 if (qp->scat_cqe && is_connected(init_attr->qp_type)) {
1687 int rcqe_sz;
1688 int scqe_sz;
1689
1690 rcqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->recv_cq);
1691 scqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->send_cq);
1692
1693 if (rcqe_sz == 128)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001694 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA64_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001695 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001696 MLX5_SET(qpc, qpc, cs_res, MLX5_RES_SCAT_DATA32_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001697
1698 if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR) {
1699 if (scqe_sz == 128)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001700 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA64_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001701 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001702 MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA32_CQE);
Eli Cohene126ba92013-07-07 17:25:49 +03001703 }
1704 }
1705
1706 if (qp->rq.wqe_cnt) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001707 MLX5_SET(qpc, qpc, log_rq_stride, qp->rq.wqe_shift - 4);
1708 MLX5_SET(qpc, qpc, log_rq_size, ilog2(qp->rq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03001709 }
1710
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001711 MLX5_SET(qpc, qpc, rq_type, get_rx_type(qp, init_attr));
Eli Cohene126ba92013-07-07 17:25:49 +03001712
1713 if (qp->sq.wqe_cnt)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001714 MLX5_SET(qpc, qpc, log_sq_size, ilog2(qp->sq.wqe_cnt));
Eli Cohene126ba92013-07-07 17:25:49 +03001715 else
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001716 MLX5_SET(qpc, qpc, no_sq, 1);
Eli Cohene126ba92013-07-07 17:25:49 +03001717
1718 /* Set default resources */
1719 switch (init_attr->qp_type) {
1720 case IB_QPT_XRC_TGT:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001721 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
1722 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(devr->c0)->mcq.cqn);
1723 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
1724 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(init_attr->xrcd)->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001725 break;
1726 case IB_QPT_XRC_INI:
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001727 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
1728 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
1729 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001730 break;
1731 default:
1732 if (init_attr->srq) {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001733 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x0)->xrcdn);
1734 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(init_attr->srq)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001735 } else {
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001736 MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
1737 MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s1)->msrq.srqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001738 }
1739 }
1740
1741 if (init_attr->send_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001742 MLX5_SET(qpc, qpc, cqn_snd, to_mcq(init_attr->send_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001743
1744 if (init_attr->recv_cq)
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001745 MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(init_attr->recv_cq)->mcq.cqn);
Eli Cohene126ba92013-07-07 17:25:49 +03001746
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001747 MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
Eli Cohene126ba92013-07-07 17:25:49 +03001748
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001749 /* 0xffffff means we ask to work with cqe version 0 */
1750 if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1)
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001751 MLX5_SET(qpc, qpc, user_index, uidx);
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03001752
Erez Shitritf0313962016-02-21 16:27:17 +02001753 /* we use IB_QP_CREATE_IPOIB_UD_LSO to indicates ipoib qp */
1754 if (init_attr->qp_type == IB_QPT_UD &&
1755 (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)) {
Erez Shitritf0313962016-02-21 16:27:17 +02001756 MLX5_SET(qpc, qpc, ulp_stateless_offload_mode, 1);
1757 qp->flags |= MLX5_IB_QP_LSO;
1758 }
Haggai Abramovskycfb5e082016-01-14 19:12:57 +02001759
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001760 if (init_attr->qp_type == IB_QPT_RAW_PACKET) {
1761 qp->raw_packet_qp.sq.ubuffer.buf_addr = ucmd.sq_buf_addr;
1762 raw_packet_qp_copy_info(qp, &qp->raw_packet_qp);
1763 err = create_raw_packet_qp(dev, qp, in, pd);
1764 } else {
1765 err = mlx5_core_create_qp(dev->mdev, &base->mqp, in, inlen);
1766 }
1767
Eli Cohene126ba92013-07-07 17:25:49 +03001768 if (err) {
1769 mlx5_ib_dbg(dev, "create qp failed\n");
1770 goto err_create;
1771 }
1772
Al Viro479163f2014-11-20 08:13:57 +00001773 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001774
majd@mellanox.com19098df2016-01-14 19:13:03 +02001775 base->container_mibqp = qp;
1776 base->mqp.event = mlx5_ib_qp_event;
Eli Cohene126ba92013-07-07 17:25:49 +03001777
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001778 get_cqs(init_attr->qp_type, init_attr->send_cq, init_attr->recv_cq,
1779 &send_cq, &recv_cq);
1780 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
1781 mlx5_ib_lock_cqs(send_cq, recv_cq);
1782 /* Maintain device to QPs access, needed for further handling via reset
1783 * flow
1784 */
1785 list_add_tail(&qp->qps_list, &dev->qp_list);
1786 /* Maintain CQ to QPs access, needed for further handling via reset flow
1787 */
1788 if (send_cq)
1789 list_add_tail(&qp->cq_send_list, &send_cq->list_send_qp);
1790 if (recv_cq)
1791 list_add_tail(&qp->cq_recv_list, &recv_cq->list_recv_qp);
1792 mlx5_ib_unlock_cqs(send_cq, recv_cq);
1793 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
1794
Eli Cohene126ba92013-07-07 17:25:49 +03001795 return 0;
1796
1797err_create:
1798 if (qp->create_type == MLX5_QP_USER)
majd@mellanox.com19098df2016-01-14 19:13:03 +02001799 destroy_qp_user(pd, qp, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001800 else if (qp->create_type == MLX5_QP_KERNEL)
1801 destroy_qp_kernel(dev, qp);
1802
Al Viro479163f2014-11-20 08:13:57 +00001803 kvfree(in);
Eli Cohene126ba92013-07-07 17:25:49 +03001804 return err;
1805}
1806
1807static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
1808 __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
1809{
1810 if (send_cq) {
1811 if (recv_cq) {
1812 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001813 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001814 spin_lock_nested(&recv_cq->lock,
1815 SINGLE_DEPTH_NESTING);
1816 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001817 spin_lock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001818 __acquire(&recv_cq->lock);
1819 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001820 spin_lock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001821 spin_lock_nested(&send_cq->lock,
1822 SINGLE_DEPTH_NESTING);
1823 }
1824 } else {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001825 spin_lock(&send_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001826 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001827 }
1828 } else if (recv_cq) {
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001829 spin_lock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001830 __acquire(&send_cq->lock);
1831 } else {
1832 __acquire(&send_cq->lock);
1833 __acquire(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001834 }
1835}
1836
1837static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
1838 __releases(&send_cq->lock) __releases(&recv_cq->lock)
1839{
1840 if (send_cq) {
1841 if (recv_cq) {
1842 if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
1843 spin_unlock(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001844 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001845 } else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
1846 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001847 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001848 } else {
1849 spin_unlock(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001850 spin_unlock(&recv_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001851 }
1852 } else {
Eli Cohen6a4f1392014-12-02 12:26:18 +02001853 __release(&recv_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001854 spin_unlock(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001855 }
1856 } else if (recv_cq) {
Eli Cohen6a4f1392014-12-02 12:26:18 +02001857 __release(&send_cq->lock);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001858 spin_unlock(&recv_cq->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02001859 } else {
1860 __release(&recv_cq->lock);
1861 __release(&send_cq->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001862 }
1863}
1864
1865static struct mlx5_ib_pd *get_pd(struct mlx5_ib_qp *qp)
1866{
1867 return to_mpd(qp->ibqp.pd);
1868}
1869
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001870static void get_cqs(enum ib_qp_type qp_type,
1871 struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
Eli Cohene126ba92013-07-07 17:25:49 +03001872 struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq)
1873{
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001874 switch (qp_type) {
Eli Cohene126ba92013-07-07 17:25:49 +03001875 case IB_QPT_XRC_TGT:
1876 *send_cq = NULL;
1877 *recv_cq = NULL;
1878 break;
1879 case MLX5_IB_QPT_REG_UMR:
1880 case IB_QPT_XRC_INI:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001881 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03001882 *recv_cq = NULL;
1883 break;
1884
1885 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02001886 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03001887 case IB_QPT_RC:
1888 case IB_QPT_UC:
1889 case IB_QPT_UD:
1890 case IB_QPT_RAW_IPV6:
1891 case IB_QPT_RAW_ETHERTYPE:
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001892 case IB_QPT_RAW_PACKET:
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001893 *send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
1894 *recv_cq = ib_recv_cq ? to_mcq(ib_recv_cq) : NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03001895 break;
1896
Eli Cohene126ba92013-07-07 17:25:49 +03001897 case IB_QPT_MAX:
1898 default:
1899 *send_cq = NULL;
1900 *recv_cq = NULL;
1901 break;
1902 }
1903}
1904
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001905static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03001906 const struct mlx5_modify_raw_qp_param *raw_qp_param,
1907 u8 lag_tx_affinity);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001908
Eli Cohene126ba92013-07-07 17:25:49 +03001909static void destroy_qp_common(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1910{
1911 struct mlx5_ib_cq *send_cq, *recv_cq;
majd@mellanox.com19098df2016-01-14 19:13:03 +02001912 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001913 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03001914 int err;
1915
Yishai Hadas28d61372016-05-23 15:20:56 +03001916 if (qp->ibqp.rwq_ind_tbl) {
1917 destroy_rss_raw_qp_tir(dev, qp);
1918 return;
1919 }
1920
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001921 base = qp->ibqp.qp_type == IB_QPT_RAW_PACKET ?
1922 &qp->raw_packet_qp.rq.base :
1923 &qp->trans_qp.base;
1924
Haggai Eran6aec21f2014-12-11 17:04:23 +02001925 if (qp->state != IB_QPS_RESET) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001926 if (qp->ibqp.qp_type != IB_QPT_RAW_PACKET) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001927 err = mlx5_core_qp_modify(dev->mdev,
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03001928 MLX5_CMD_OP_2RST_QP, 0,
1929 NULL, &base->mqp);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001930 } else {
Alex Vesker0680efa2016-08-28 12:25:52 +03001931 struct mlx5_modify_raw_qp_param raw_qp_param = {
1932 .operation = MLX5_CMD_OP_2RST_QP
1933 };
1934
Aviv Heller13eab212016-09-18 20:48:04 +03001935 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, 0);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02001936 }
1937 if (err)
majd@mellanox.com427c1e72016-01-14 19:13:07 +02001938 mlx5_ib_warn(dev, "mlx5_ib: modify QP 0x%06x to RESET failed\n",
majd@mellanox.com19098df2016-01-14 19:13:03 +02001939 base->mqp.qpn);
Haggai Eran6aec21f2014-12-11 17:04:23 +02001940 }
Eli Cohene126ba92013-07-07 17:25:49 +03001941
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001942 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
1943 &send_cq, &recv_cq);
1944
1945 spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
1946 mlx5_ib_lock_cqs(send_cq, recv_cq);
1947 /* del from lists under both locks above to protect reset flow paths */
1948 list_del(&qp->qps_list);
1949 if (send_cq)
1950 list_del(&qp->cq_send_list);
1951
1952 if (recv_cq)
1953 list_del(&qp->cq_recv_list);
Eli Cohene126ba92013-07-07 17:25:49 +03001954
1955 if (qp->create_type == MLX5_QP_KERNEL) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02001956 __mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03001957 qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
1958 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02001959 __mlx5_ib_cq_clean(send_cq, base->mqp.qpn,
1960 NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03001961 }
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03001962 mlx5_ib_unlock_cqs(send_cq, recv_cq);
1963 spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
Eli Cohene126ba92013-07-07 17:25:49 +03001964
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02001965 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET) {
1966 destroy_raw_packet_qp(dev, qp);
1967 } else {
1968 err = mlx5_core_destroy_qp(dev->mdev, &base->mqp);
1969 if (err)
1970 mlx5_ib_warn(dev, "failed to destroy QP 0x%x\n",
1971 base->mqp.qpn);
1972 }
Eli Cohene126ba92013-07-07 17:25:49 +03001973
Eli Cohene126ba92013-07-07 17:25:49 +03001974 if (qp->create_type == MLX5_QP_KERNEL)
1975 destroy_qp_kernel(dev, qp);
1976 else if (qp->create_type == MLX5_QP_USER)
majd@mellanox.com19098df2016-01-14 19:13:03 +02001977 destroy_qp_user(&get_pd(qp)->ibpd, qp, base);
Eli Cohene126ba92013-07-07 17:25:49 +03001978}
1979
1980static const char *ib_qp_type_str(enum ib_qp_type type)
1981{
1982 switch (type) {
1983 case IB_QPT_SMI:
1984 return "IB_QPT_SMI";
1985 case IB_QPT_GSI:
1986 return "IB_QPT_GSI";
1987 case IB_QPT_RC:
1988 return "IB_QPT_RC";
1989 case IB_QPT_UC:
1990 return "IB_QPT_UC";
1991 case IB_QPT_UD:
1992 return "IB_QPT_UD";
1993 case IB_QPT_RAW_IPV6:
1994 return "IB_QPT_RAW_IPV6";
1995 case IB_QPT_RAW_ETHERTYPE:
1996 return "IB_QPT_RAW_ETHERTYPE";
1997 case IB_QPT_XRC_INI:
1998 return "IB_QPT_XRC_INI";
1999 case IB_QPT_XRC_TGT:
2000 return "IB_QPT_XRC_TGT";
2001 case IB_QPT_RAW_PACKET:
2002 return "IB_QPT_RAW_PACKET";
2003 case MLX5_IB_QPT_REG_UMR:
2004 return "MLX5_IB_QPT_REG_UMR";
2005 case IB_QPT_MAX:
2006 default:
2007 return "Invalid QP type";
2008 }
2009}
2010
2011struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd,
2012 struct ib_qp_init_attr *init_attr,
2013 struct ib_udata *udata)
2014{
2015 struct mlx5_ib_dev *dev;
2016 struct mlx5_ib_qp *qp;
2017 u16 xrcdn = 0;
2018 int err;
2019
2020 if (pd) {
2021 dev = to_mdev(pd->device);
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002022
2023 if (init_attr->qp_type == IB_QPT_RAW_PACKET) {
2024 if (!pd->uobject) {
2025 mlx5_ib_dbg(dev, "Raw Packet QP is not supported for kernel consumers\n");
2026 return ERR_PTR(-EINVAL);
2027 } else if (!to_mucontext(pd->uobject->context)->cqe_version) {
2028 mlx5_ib_dbg(dev, "Raw Packet QP is only supported for CQE version > 0\n");
2029 return ERR_PTR(-EINVAL);
2030 }
2031 }
Majd Dibbiny09f16cf2016-01-28 17:51:48 +02002032 } else {
2033 /* being cautious here */
2034 if (init_attr->qp_type != IB_QPT_XRC_TGT &&
2035 init_attr->qp_type != MLX5_IB_QPT_REG_UMR) {
2036 pr_warn("%s: no PD for transport %s\n", __func__,
2037 ib_qp_type_str(init_attr->qp_type));
2038 return ERR_PTR(-EINVAL);
2039 }
2040 dev = to_mdev(to_mxrcd(init_attr->xrcd)->ibxrcd.device);
Eli Cohene126ba92013-07-07 17:25:49 +03002041 }
2042
2043 switch (init_attr->qp_type) {
2044 case IB_QPT_XRC_TGT:
2045 case IB_QPT_XRC_INI:
Saeed Mahameed938fe832015-05-28 22:28:41 +03002046 if (!MLX5_CAP_GEN(dev->mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002047 mlx5_ib_dbg(dev, "XRC not supported\n");
2048 return ERR_PTR(-ENOSYS);
2049 }
2050 init_attr->recv_cq = NULL;
2051 if (init_attr->qp_type == IB_QPT_XRC_TGT) {
2052 xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
2053 init_attr->send_cq = NULL;
2054 }
2055
2056 /* fall through */
majd@mellanox.com0fb2ed62016-01-14 19:13:04 +02002057 case IB_QPT_RAW_PACKET:
Eli Cohene126ba92013-07-07 17:25:49 +03002058 case IB_QPT_RC:
2059 case IB_QPT_UC:
2060 case IB_QPT_UD:
2061 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02002062 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03002063 case MLX5_IB_QPT_REG_UMR:
2064 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2065 if (!qp)
2066 return ERR_PTR(-ENOMEM);
2067
2068 err = create_qp_common(dev, pd, init_attr, udata, qp);
2069 if (err) {
2070 mlx5_ib_dbg(dev, "create_qp_common failed\n");
2071 kfree(qp);
2072 return ERR_PTR(err);
2073 }
2074
2075 if (is_qp0(init_attr->qp_type))
2076 qp->ibqp.qp_num = 0;
2077 else if (is_qp1(init_attr->qp_type))
2078 qp->ibqp.qp_num = 1;
2079 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002080 qp->ibqp.qp_num = qp->trans_qp.base.mqp.qpn;
Eli Cohene126ba92013-07-07 17:25:49 +03002081
2082 mlx5_ib_dbg(dev, "ib qpnum 0x%x, mlx qpn 0x%x, rcqn 0x%x, scqn 0x%x\n",
majd@mellanox.com19098df2016-01-14 19:13:03 +02002083 qp->ibqp.qp_num, qp->trans_qp.base.mqp.qpn,
Eli Cohena1ab8402016-10-27 16:36:46 +03002084 init_attr->recv_cq ? to_mcq(init_attr->recv_cq)->mcq.cqn : -1,
2085 init_attr->send_cq ? to_mcq(init_attr->send_cq)->mcq.cqn : -1);
Eli Cohene126ba92013-07-07 17:25:49 +03002086
majd@mellanox.com19098df2016-01-14 19:13:03 +02002087 qp->trans_qp.xrcdn = xrcdn;
Eli Cohene126ba92013-07-07 17:25:49 +03002088
2089 break;
2090
Haggai Erand16e91d2016-02-29 15:45:05 +02002091 case IB_QPT_GSI:
2092 return mlx5_ib_gsi_create_qp(pd, init_attr);
2093
Eli Cohene126ba92013-07-07 17:25:49 +03002094 case IB_QPT_RAW_IPV6:
2095 case IB_QPT_RAW_ETHERTYPE:
Eli Cohene126ba92013-07-07 17:25:49 +03002096 case IB_QPT_MAX:
2097 default:
2098 mlx5_ib_dbg(dev, "unsupported qp type %d\n",
2099 init_attr->qp_type);
2100 /* Don't support raw QPs */
2101 return ERR_PTR(-EINVAL);
2102 }
2103
2104 return &qp->ibqp;
2105}
2106
2107int mlx5_ib_destroy_qp(struct ib_qp *qp)
2108{
2109 struct mlx5_ib_dev *dev = to_mdev(qp->device);
2110 struct mlx5_ib_qp *mqp = to_mqp(qp);
2111
Haggai Erand16e91d2016-02-29 15:45:05 +02002112 if (unlikely(qp->qp_type == IB_QPT_GSI))
2113 return mlx5_ib_gsi_destroy_qp(qp);
2114
Eli Cohene126ba92013-07-07 17:25:49 +03002115 destroy_qp_common(dev, mqp);
2116
2117 kfree(mqp);
2118
2119 return 0;
2120}
2121
2122static __be32 to_mlx5_access_flags(struct mlx5_ib_qp *qp, const struct ib_qp_attr *attr,
2123 int attr_mask)
2124{
2125 u32 hw_access_flags = 0;
2126 u8 dest_rd_atomic;
2127 u32 access_flags;
2128
2129 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
2130 dest_rd_atomic = attr->max_dest_rd_atomic;
2131 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002132 dest_rd_atomic = qp->trans_qp.resp_depth;
Eli Cohene126ba92013-07-07 17:25:49 +03002133
2134 if (attr_mask & IB_QP_ACCESS_FLAGS)
2135 access_flags = attr->qp_access_flags;
2136 else
majd@mellanox.com19098df2016-01-14 19:13:03 +02002137 access_flags = qp->trans_qp.atomic_rd_en;
Eli Cohene126ba92013-07-07 17:25:49 +03002138
2139 if (!dest_rd_atomic)
2140 access_flags &= IB_ACCESS_REMOTE_WRITE;
2141
2142 if (access_flags & IB_ACCESS_REMOTE_READ)
2143 hw_access_flags |= MLX5_QP_BIT_RRE;
2144 if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
2145 hw_access_flags |= (MLX5_QP_BIT_RAE | MLX5_ATOMIC_MODE_CX);
2146 if (access_flags & IB_ACCESS_REMOTE_WRITE)
2147 hw_access_flags |= MLX5_QP_BIT_RWE;
2148
2149 return cpu_to_be32(hw_access_flags);
2150}
2151
2152enum {
2153 MLX5_PATH_FLAG_FL = 1 << 0,
2154 MLX5_PATH_FLAG_FREE_AR = 1 << 1,
2155 MLX5_PATH_FLAG_COUNTER = 1 << 2,
2156};
2157
2158static int ib_rate_to_mlx5(struct mlx5_ib_dev *dev, u8 rate)
2159{
2160 if (rate == IB_RATE_PORT_CURRENT) {
2161 return 0;
2162 } else if (rate < IB_RATE_2_5_GBPS || rate > IB_RATE_300_GBPS) {
2163 return -EINVAL;
2164 } else {
2165 while (rate != IB_RATE_2_5_GBPS &&
2166 !(1 << (rate + MLX5_STAT_RATE_OFFSET) &
Saeed Mahameed938fe832015-05-28 22:28:41 +03002167 MLX5_CAP_GEN(dev->mdev, stat_rate_support)))
Eli Cohene126ba92013-07-07 17:25:49 +03002168 --rate;
2169 }
2170
2171 return rate + MLX5_STAT_RATE_OFFSET;
2172}
2173
majd@mellanox.com75850d02016-01-14 19:13:06 +02002174static int modify_raw_packet_eth_prio(struct mlx5_core_dev *dev,
2175 struct mlx5_ib_sq *sq, u8 sl)
2176{
2177 void *in;
2178 void *tisc;
2179 int inlen;
2180 int err;
2181
2182 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
2183 in = mlx5_vzalloc(inlen);
2184 if (!in)
2185 return -ENOMEM;
2186
2187 MLX5_SET(modify_tis_in, in, bitmask.prio, 1);
2188
2189 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2190 MLX5_SET(tisc, tisc, prio, ((sl & 0x7) << 1));
2191
2192 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2193
2194 kvfree(in);
2195
2196 return err;
2197}
2198
Aviv Heller13eab212016-09-18 20:48:04 +03002199static int modify_raw_packet_tx_affinity(struct mlx5_core_dev *dev,
2200 struct mlx5_ib_sq *sq, u8 tx_affinity)
2201{
2202 void *in;
2203 void *tisc;
2204 int inlen;
2205 int err;
2206
2207 inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
2208 in = mlx5_vzalloc(inlen);
2209 if (!in)
2210 return -ENOMEM;
2211
2212 MLX5_SET(modify_tis_in, in, bitmask.lag_tx_port_affinity, 1);
2213
2214 tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
2215 MLX5_SET(tisc, tisc, lag_tx_port_affinity, tx_affinity);
2216
2217 err = mlx5_core_modify_tis(dev, sq->tisn, in, inlen);
2218
2219 kvfree(in);
2220
2221 return err;
2222}
2223
majd@mellanox.com75850d02016-01-14 19:13:06 +02002224static int mlx5_set_path(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
2225 const struct ib_ah_attr *ah,
Eli Cohene126ba92013-07-07 17:25:49 +03002226 struct mlx5_qp_path *path, u8 port, int attr_mask,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002227 u32 path_flags, const struct ib_qp_attr *attr,
2228 bool alt)
Eli Cohene126ba92013-07-07 17:25:49 +03002229{
Achiad Shochat2811ba52015-12-23 18:47:24 +02002230 enum rdma_link_layer ll = rdma_port_get_link_layer(&dev->ib_dev, port);
Eli Cohene126ba92013-07-07 17:25:49 +03002231 int err;
2232
Eli Cohene126ba92013-07-07 17:25:49 +03002233 if (attr_mask & IB_QP_PKEY_INDEX)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002234 path->pkey_index = cpu_to_be16(alt ? attr->alt_pkey_index :
2235 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002236
Eli Cohene126ba92013-07-07 17:25:49 +03002237 if (ah->ah_flags & IB_AH_GRH) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03002238 if (ah->grh.sgid_index >=
2239 dev->mdev->port_caps[port - 1].gid_table_len) {
Joe Perchesf4f01b52015-05-08 15:58:07 -07002240 pr_err("sgid_index (%u) too large. max is %d\n",
Saeed Mahameed938fe832015-05-28 22:28:41 +03002241 ah->grh.sgid_index,
2242 dev->mdev->port_caps[port - 1].gid_table_len);
Eli Cohenf83b4262014-09-14 16:47:54 +03002243 return -EINVAL;
2244 }
Achiad Shochat2811ba52015-12-23 18:47:24 +02002245 }
2246
2247 if (ll == IB_LINK_LAYER_ETHERNET) {
2248 if (!(ah->ah_flags & IB_AH_GRH))
2249 return -EINVAL;
2250 memcpy(path->rmac, ah->dmac, sizeof(ah->dmac));
2251 path->udp_sport = mlx5_get_roce_udp_sport(dev, port,
2252 ah->grh.sgid_index);
2253 path->dci_cfi_prio_sl = (ah->sl & 0x7) << 4;
2254 } else {
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03002255 path->fl_free_ar = (path_flags & MLX5_PATH_FLAG_FL) ? 0x80 : 0;
2256 path->fl_free_ar |=
2257 (path_flags & MLX5_PATH_FLAG_FREE_AR) ? 0x40 : 0;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002258 path->rlid = cpu_to_be16(ah->dlid);
2259 path->grh_mlid = ah->src_path_bits & 0x7f;
2260 if (ah->ah_flags & IB_AH_GRH)
2261 path->grh_mlid |= 1 << 7;
2262 path->dci_cfi_prio_sl = ah->sl & 0xf;
2263 }
2264
2265 if (ah->ah_flags & IB_AH_GRH) {
Eli Cohene126ba92013-07-07 17:25:49 +03002266 path->mgid_index = ah->grh.sgid_index;
2267 path->hop_limit = ah->grh.hop_limit;
2268 path->tclass_flowlabel =
2269 cpu_to_be32((ah->grh.traffic_class << 20) |
2270 (ah->grh.flow_label));
2271 memcpy(path->rgid, ah->grh.dgid.raw, 16);
2272 }
2273
2274 err = ib_rate_to_mlx5(dev, ah->static_rate);
2275 if (err < 0)
2276 return err;
2277 path->static_rate = err;
2278 path->port = port;
2279
Eli Cohene126ba92013-07-07 17:25:49 +03002280 if (attr_mask & IB_QP_TIMEOUT)
Achiad Shochatf879ee82016-06-04 15:15:37 +03002281 path->ackto_lt = (alt ? attr->alt_timeout : attr->timeout) << 3;
Eli Cohene126ba92013-07-07 17:25:49 +03002282
majd@mellanox.com75850d02016-01-14 19:13:06 +02002283 if ((qp->ibqp.qp_type == IB_QPT_RAW_PACKET) && qp->sq.wqe_cnt)
2284 return modify_raw_packet_eth_prio(dev->mdev,
2285 &qp->raw_packet_qp.sq,
2286 ah->sl & 0xf);
2287
Eli Cohene126ba92013-07-07 17:25:49 +03002288 return 0;
2289}
2290
2291static enum mlx5_qp_optpar opt_mask[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE][MLX5_QP_ST_MAX] = {
2292 [MLX5_QP_STATE_INIT] = {
2293 [MLX5_QP_STATE_INIT] = {
2294 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2295 MLX5_QP_OPTPAR_RAE |
2296 MLX5_QP_OPTPAR_RWE |
2297 MLX5_QP_OPTPAR_PKEY_INDEX |
2298 MLX5_QP_OPTPAR_PRI_PORT,
2299 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
2300 MLX5_QP_OPTPAR_PKEY_INDEX |
2301 MLX5_QP_OPTPAR_PRI_PORT,
2302 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2303 MLX5_QP_OPTPAR_Q_KEY |
2304 MLX5_QP_OPTPAR_PRI_PORT,
2305 },
2306 [MLX5_QP_STATE_RTR] = {
2307 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2308 MLX5_QP_OPTPAR_RRE |
2309 MLX5_QP_OPTPAR_RAE |
2310 MLX5_QP_OPTPAR_RWE |
2311 MLX5_QP_OPTPAR_PKEY_INDEX,
2312 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2313 MLX5_QP_OPTPAR_RWE |
2314 MLX5_QP_OPTPAR_PKEY_INDEX,
2315 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX |
2316 MLX5_QP_OPTPAR_Q_KEY,
2317 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_PKEY_INDEX |
2318 MLX5_QP_OPTPAR_Q_KEY,
Eli Cohena4774e92013-09-11 16:35:32 +03002319 [MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2320 MLX5_QP_OPTPAR_RRE |
2321 MLX5_QP_OPTPAR_RAE |
2322 MLX5_QP_OPTPAR_RWE |
2323 MLX5_QP_OPTPAR_PKEY_INDEX,
Eli Cohene126ba92013-07-07 17:25:49 +03002324 },
2325 },
2326 [MLX5_QP_STATE_RTR] = {
2327 [MLX5_QP_STATE_RTS] = {
2328 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2329 MLX5_QP_OPTPAR_RRE |
2330 MLX5_QP_OPTPAR_RAE |
2331 MLX5_QP_OPTPAR_RWE |
2332 MLX5_QP_OPTPAR_PM_STATE |
2333 MLX5_QP_OPTPAR_RNR_TIMEOUT,
2334 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
2335 MLX5_QP_OPTPAR_RWE |
2336 MLX5_QP_OPTPAR_PM_STATE,
2337 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
2338 },
2339 },
2340 [MLX5_QP_STATE_RTS] = {
2341 [MLX5_QP_STATE_RTS] = {
2342 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE |
2343 MLX5_QP_OPTPAR_RAE |
2344 MLX5_QP_OPTPAR_RWE |
2345 MLX5_QP_OPTPAR_RNR_TIMEOUT |
Eli Cohenc2a34312013-10-24 12:01:02 +03002346 MLX5_QP_OPTPAR_PM_STATE |
2347 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03002348 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE |
Eli Cohenc2a34312013-10-24 12:01:02 +03002349 MLX5_QP_OPTPAR_PM_STATE |
2350 MLX5_QP_OPTPAR_ALT_ADDR_PATH,
Eli Cohene126ba92013-07-07 17:25:49 +03002351 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY |
2352 MLX5_QP_OPTPAR_SRQN |
2353 MLX5_QP_OPTPAR_CQN_RCV,
2354 },
2355 },
2356 [MLX5_QP_STATE_SQER] = {
2357 [MLX5_QP_STATE_RTS] = {
2358 [MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
2359 [MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_Q_KEY,
Eli Cohen75959f52013-09-11 16:35:31 +03002360 [MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE,
Eli Cohena4774e92013-09-11 16:35:32 +03002361 [MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RNR_TIMEOUT |
2362 MLX5_QP_OPTPAR_RWE |
2363 MLX5_QP_OPTPAR_RAE |
2364 MLX5_QP_OPTPAR_RRE,
Eli Cohene126ba92013-07-07 17:25:49 +03002365 },
2366 },
2367};
2368
2369static int ib_nr_to_mlx5_nr(int ib_mask)
2370{
2371 switch (ib_mask) {
2372 case IB_QP_STATE:
2373 return 0;
2374 case IB_QP_CUR_STATE:
2375 return 0;
2376 case IB_QP_EN_SQD_ASYNC_NOTIFY:
2377 return 0;
2378 case IB_QP_ACCESS_FLAGS:
2379 return MLX5_QP_OPTPAR_RWE | MLX5_QP_OPTPAR_RRE |
2380 MLX5_QP_OPTPAR_RAE;
2381 case IB_QP_PKEY_INDEX:
2382 return MLX5_QP_OPTPAR_PKEY_INDEX;
2383 case IB_QP_PORT:
2384 return MLX5_QP_OPTPAR_PRI_PORT;
2385 case IB_QP_QKEY:
2386 return MLX5_QP_OPTPAR_Q_KEY;
2387 case IB_QP_AV:
2388 return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH |
2389 MLX5_QP_OPTPAR_PRI_PORT;
2390 case IB_QP_PATH_MTU:
2391 return 0;
2392 case IB_QP_TIMEOUT:
2393 return MLX5_QP_OPTPAR_ACK_TIMEOUT;
2394 case IB_QP_RETRY_CNT:
2395 return MLX5_QP_OPTPAR_RETRY_COUNT;
2396 case IB_QP_RNR_RETRY:
2397 return MLX5_QP_OPTPAR_RNR_RETRY;
2398 case IB_QP_RQ_PSN:
2399 return 0;
2400 case IB_QP_MAX_QP_RD_ATOMIC:
2401 return MLX5_QP_OPTPAR_SRA_MAX;
2402 case IB_QP_ALT_PATH:
2403 return MLX5_QP_OPTPAR_ALT_ADDR_PATH;
2404 case IB_QP_MIN_RNR_TIMER:
2405 return MLX5_QP_OPTPAR_RNR_TIMEOUT;
2406 case IB_QP_SQ_PSN:
2407 return 0;
2408 case IB_QP_MAX_DEST_RD_ATOMIC:
2409 return MLX5_QP_OPTPAR_RRA_MAX | MLX5_QP_OPTPAR_RWE |
2410 MLX5_QP_OPTPAR_RRE | MLX5_QP_OPTPAR_RAE;
2411 case IB_QP_PATH_MIG_STATE:
2412 return MLX5_QP_OPTPAR_PM_STATE;
2413 case IB_QP_CAP:
2414 return 0;
2415 case IB_QP_DEST_QPN:
2416 return 0;
2417 }
2418 return 0;
2419}
2420
2421static int ib_mask_to_mlx5_opt(int ib_mask)
2422{
2423 int result = 0;
2424 int i;
2425
2426 for (i = 0; i < 8 * sizeof(int); i++) {
2427 if ((1 << i) & ib_mask)
2428 result |= ib_nr_to_mlx5_nr(1 << i);
2429 }
2430
2431 return result;
2432}
2433
Alex Veskereb49ab02016-08-28 12:25:53 +03002434static int modify_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
2435 struct mlx5_ib_rq *rq, int new_state,
2436 const struct mlx5_modify_raw_qp_param *raw_qp_param)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002437{
2438 void *in;
2439 void *rqc;
2440 int inlen;
2441 int err;
2442
2443 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
2444 in = mlx5_vzalloc(inlen);
2445 if (!in)
2446 return -ENOMEM;
2447
2448 MLX5_SET(modify_rq_in, in, rq_state, rq->state);
2449
2450 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
2451 MLX5_SET(rqc, rqc, state, new_state);
2452
Alex Veskereb49ab02016-08-28 12:25:53 +03002453 if (raw_qp_param->set_mask & MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID) {
2454 if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
2455 MLX5_SET64(modify_rq_in, in, modify_bitmask,
2456 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_MODIFY_RQ_COUNTER_SET_ID);
2457 MLX5_SET(rqc, rqc, counter_set_id, raw_qp_param->rq_q_ctr_id);
2458 } else
2459 pr_info_once("%s: RAW PACKET QP counters are not supported on current FW\n",
2460 dev->ib_dev.name);
2461 }
2462
2463 err = mlx5_core_modify_rq(dev->mdev, rq->base.mqp.qpn, in, inlen);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002464 if (err)
2465 goto out;
2466
2467 rq->state = new_state;
2468
2469out:
2470 kvfree(in);
2471 return err;
2472}
2473
2474static int modify_raw_packet_qp_sq(struct mlx5_core_dev *dev,
Bodong Wang7d29f342016-12-01 13:43:16 +02002475 struct mlx5_ib_sq *sq,
2476 int new_state,
2477 const struct mlx5_modify_raw_qp_param *raw_qp_param)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002478{
Bodong Wang7d29f342016-12-01 13:43:16 +02002479 struct mlx5_ib_qp *ibqp = sq->base.container_mibqp;
2480 u32 old_rate = ibqp->rate_limit;
2481 u32 new_rate = old_rate;
2482 u16 rl_index = 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002483 void *in;
2484 void *sqc;
2485 int inlen;
2486 int err;
2487
2488 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
2489 in = mlx5_vzalloc(inlen);
2490 if (!in)
2491 return -ENOMEM;
2492
2493 MLX5_SET(modify_sq_in, in, sq_state, sq->state);
2494
2495 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
2496 MLX5_SET(sqc, sqc, state, new_state);
2497
Bodong Wang7d29f342016-12-01 13:43:16 +02002498 if (raw_qp_param->set_mask & MLX5_RAW_QP_RATE_LIMIT) {
2499 if (new_state != MLX5_SQC_STATE_RDY)
2500 pr_warn("%s: Rate limit can only be changed when SQ is moving to RDY\n",
2501 __func__);
2502 else
2503 new_rate = raw_qp_param->rate_limit;
2504 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002505
Bodong Wang7d29f342016-12-01 13:43:16 +02002506 if (old_rate != new_rate) {
2507 if (new_rate) {
2508 err = mlx5_rl_add_rate(dev, new_rate, &rl_index);
2509 if (err) {
2510 pr_err("Failed configuring rate %u: %d\n",
2511 new_rate, err);
2512 goto out;
2513 }
2514 }
2515
2516 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
2517 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
2518 }
2519
2520 err = mlx5_core_modify_sq(dev, sq->base.mqp.qpn, in, inlen);
2521 if (err) {
2522 /* Remove new rate from table if failed */
2523 if (new_rate &&
2524 old_rate != new_rate)
2525 mlx5_rl_remove_rate(dev, new_rate);
2526 goto out;
2527 }
2528
2529 /* Only remove the old rate after new rate was set */
2530 if ((old_rate &&
2531 (old_rate != new_rate)) ||
2532 (new_state != MLX5_SQC_STATE_RDY))
2533 mlx5_rl_remove_rate(dev, old_rate);
2534
2535 ibqp->rate_limit = new_rate;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002536 sq->state = new_state;
2537
2538out:
2539 kvfree(in);
2540 return err;
2541}
2542
2543static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
Aviv Heller13eab212016-09-18 20:48:04 +03002544 const struct mlx5_modify_raw_qp_param *raw_qp_param,
2545 u8 tx_affinity)
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002546{
2547 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
2548 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
2549 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
Bodong Wang7d29f342016-12-01 13:43:16 +02002550 int modify_rq = !!qp->rq.wqe_cnt;
2551 int modify_sq = !!qp->sq.wqe_cnt;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002552 int rq_state;
2553 int sq_state;
2554 int err;
2555
Alex Vesker0680efa2016-08-28 12:25:52 +03002556 switch (raw_qp_param->operation) {
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002557 case MLX5_CMD_OP_RST2INIT_QP:
2558 rq_state = MLX5_RQC_STATE_RDY;
2559 sq_state = MLX5_SQC_STATE_RDY;
2560 break;
2561 case MLX5_CMD_OP_2ERR_QP:
2562 rq_state = MLX5_RQC_STATE_ERR;
2563 sq_state = MLX5_SQC_STATE_ERR;
2564 break;
2565 case MLX5_CMD_OP_2RST_QP:
2566 rq_state = MLX5_RQC_STATE_RST;
2567 sq_state = MLX5_SQC_STATE_RST;
2568 break;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002569 case MLX5_CMD_OP_RTR2RTS_QP:
2570 case MLX5_CMD_OP_RTS2RTS_QP:
Bodong Wang7d29f342016-12-01 13:43:16 +02002571 if (raw_qp_param->set_mask ==
2572 MLX5_RAW_QP_RATE_LIMIT) {
2573 modify_rq = 0;
2574 sq_state = sq->state;
2575 } else {
2576 return raw_qp_param->set_mask ? -EINVAL : 0;
2577 }
2578 break;
2579 case MLX5_CMD_OP_INIT2INIT_QP:
2580 case MLX5_CMD_OP_INIT2RTR_QP:
Alex Veskereb49ab02016-08-28 12:25:53 +03002581 if (raw_qp_param->set_mask)
2582 return -EINVAL;
2583 else
2584 return 0;
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002585 default:
2586 WARN_ON(1);
2587 return -EINVAL;
2588 }
2589
Bodong Wang7d29f342016-12-01 13:43:16 +02002590 if (modify_rq) {
2591 err = modify_raw_packet_qp_rq(dev, rq, rq_state, raw_qp_param);
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002592 if (err)
2593 return err;
2594 }
2595
Bodong Wang7d29f342016-12-01 13:43:16 +02002596 if (modify_sq) {
Aviv Heller13eab212016-09-18 20:48:04 +03002597 if (tx_affinity) {
2598 err = modify_raw_packet_tx_affinity(dev->mdev, sq,
2599 tx_affinity);
2600 if (err)
2601 return err;
2602 }
2603
Bodong Wang7d29f342016-12-01 13:43:16 +02002604 return modify_raw_packet_qp_sq(dev->mdev, sq, sq_state, raw_qp_param);
Aviv Heller13eab212016-09-18 20:48:04 +03002605 }
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002606
2607 return 0;
2608}
2609
Eli Cohene126ba92013-07-07 17:25:49 +03002610static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
2611 const struct ib_qp_attr *attr, int attr_mask,
2612 enum ib_qp_state cur_state, enum ib_qp_state new_state)
2613{
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002614 static const u16 optab[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE] = {
2615 [MLX5_QP_STATE_RST] = {
2616 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2617 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2618 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_RST2INIT_QP,
2619 },
2620 [MLX5_QP_STATE_INIT] = {
2621 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2622 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2623 [MLX5_QP_STATE_INIT] = MLX5_CMD_OP_INIT2INIT_QP,
2624 [MLX5_QP_STATE_RTR] = MLX5_CMD_OP_INIT2RTR_QP,
2625 },
2626 [MLX5_QP_STATE_RTR] = {
2627 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2628 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2629 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTR2RTS_QP,
2630 },
2631 [MLX5_QP_STATE_RTS] = {
2632 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2633 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2634 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_RTS2RTS_QP,
2635 },
2636 [MLX5_QP_STATE_SQD] = {
2637 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2638 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2639 },
2640 [MLX5_QP_STATE_SQER] = {
2641 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2642 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2643 [MLX5_QP_STATE_RTS] = MLX5_CMD_OP_SQERR2RTS_QP,
2644 },
2645 [MLX5_QP_STATE_ERR] = {
2646 [MLX5_QP_STATE_RST] = MLX5_CMD_OP_2RST_QP,
2647 [MLX5_QP_STATE_ERR] = MLX5_CMD_OP_2ERR_QP,
2648 }
2649 };
2650
Eli Cohene126ba92013-07-07 17:25:49 +03002651 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2652 struct mlx5_ib_qp *qp = to_mqp(ibqp);
majd@mellanox.com19098df2016-01-14 19:13:03 +02002653 struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
Eli Cohene126ba92013-07-07 17:25:49 +03002654 struct mlx5_ib_cq *send_cq, *recv_cq;
2655 struct mlx5_qp_context *context;
Eli Cohene126ba92013-07-07 17:25:49 +03002656 struct mlx5_ib_pd *pd;
Alex Veskereb49ab02016-08-28 12:25:53 +03002657 struct mlx5_ib_port *mibport = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002658 enum mlx5_qp_state mlx5_cur, mlx5_new;
2659 enum mlx5_qp_optpar optpar;
Eli Cohene126ba92013-07-07 17:25:49 +03002660 int mlx5_st;
2661 int err;
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002662 u16 op;
Aviv Heller13eab212016-09-18 20:48:04 +03002663 u8 tx_affinity = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002664
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002665 context = kzalloc(sizeof(*context), GFP_KERNEL);
2666 if (!context)
Eli Cohene126ba92013-07-07 17:25:49 +03002667 return -ENOMEM;
2668
Eli Cohene126ba92013-07-07 17:25:49 +03002669 err = to_mlx5_st(ibqp->qp_type);
Haggai Eran158abf82016-02-29 15:45:04 +02002670 if (err < 0) {
2671 mlx5_ib_dbg(dev, "unsupported qp type %d\n", ibqp->qp_type);
Eli Cohene126ba92013-07-07 17:25:49 +03002672 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002673 }
Eli Cohene126ba92013-07-07 17:25:49 +03002674
2675 context->flags = cpu_to_be32(err << 16);
2676
2677 if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
2678 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
2679 } else {
2680 switch (attr->path_mig_state) {
2681 case IB_MIG_MIGRATED:
2682 context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
2683 break;
2684 case IB_MIG_REARM:
2685 context->flags |= cpu_to_be32(MLX5_QP_PM_REARM << 11);
2686 break;
2687 case IB_MIG_ARMED:
2688 context->flags |= cpu_to_be32(MLX5_QP_PM_ARMED << 11);
2689 break;
2690 }
2691 }
2692
Aviv Heller13eab212016-09-18 20:48:04 +03002693 if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
2694 if ((ibqp->qp_type == IB_QPT_RC) ||
2695 (ibqp->qp_type == IB_QPT_UD &&
2696 !(qp->flags & MLX5_IB_QP_SQPN_QP1)) ||
2697 (ibqp->qp_type == IB_QPT_UC) ||
2698 (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
2699 (ibqp->qp_type == IB_QPT_XRC_INI) ||
2700 (ibqp->qp_type == IB_QPT_XRC_TGT)) {
2701 if (mlx5_lag_is_active(dev->mdev)) {
2702 tx_affinity = (unsigned int)atomic_add_return(1,
2703 &dev->roce.next_port) %
2704 MLX5_MAX_PORTS + 1;
2705 context->flags |= cpu_to_be32(tx_affinity << 24);
2706 }
2707 }
2708 }
2709
Haggai Erand16e91d2016-02-29 15:45:05 +02002710 if (is_sqp(ibqp->qp_type)) {
Eli Cohene126ba92013-07-07 17:25:49 +03002711 context->mtu_msgmax = (IB_MTU_256 << 5) | 8;
2712 } else if (ibqp->qp_type == IB_QPT_UD ||
2713 ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
2714 context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
2715 } else if (attr_mask & IB_QP_PATH_MTU) {
2716 if (attr->path_mtu < IB_MTU_256 ||
2717 attr->path_mtu > IB_MTU_4096) {
2718 mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
2719 err = -EINVAL;
2720 goto out;
2721 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03002722 context->mtu_msgmax = (attr->path_mtu << 5) |
2723 (u8)MLX5_CAP_GEN(dev->mdev, log_max_msg);
Eli Cohene126ba92013-07-07 17:25:49 +03002724 }
2725
2726 if (attr_mask & IB_QP_DEST_QPN)
2727 context->log_pg_sz_remote_qpn = cpu_to_be32(attr->dest_qp_num);
2728
2729 if (attr_mask & IB_QP_PKEY_INDEX)
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03002730 context->pri_path.pkey_index = cpu_to_be16(attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002731
2732 /* todo implement counter_index functionality */
2733
2734 if (is_sqp(ibqp->qp_type))
2735 context->pri_path.port = qp->port;
2736
2737 if (attr_mask & IB_QP_PORT)
2738 context->pri_path.port = attr->port_num;
2739
2740 if (attr_mask & IB_QP_AV) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02002741 err = mlx5_set_path(dev, qp, &attr->ah_attr, &context->pri_path,
Eli Cohene126ba92013-07-07 17:25:49 +03002742 attr_mask & IB_QP_PORT ? attr->port_num : qp->port,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002743 attr_mask, 0, attr, false);
Eli Cohene126ba92013-07-07 17:25:49 +03002744 if (err)
2745 goto out;
2746 }
2747
2748 if (attr_mask & IB_QP_TIMEOUT)
2749 context->pri_path.ackto_lt |= attr->timeout << 3;
2750
2751 if (attr_mask & IB_QP_ALT_PATH) {
majd@mellanox.com75850d02016-01-14 19:13:06 +02002752 err = mlx5_set_path(dev, qp, &attr->alt_ah_attr,
2753 &context->alt_path,
Achiad Shochatf879ee82016-06-04 15:15:37 +03002754 attr->alt_port_num,
2755 attr_mask | IB_QP_PKEY_INDEX | IB_QP_TIMEOUT,
2756 0, attr, true);
Eli Cohene126ba92013-07-07 17:25:49 +03002757 if (err)
2758 goto out;
2759 }
2760
2761 pd = get_pd(qp);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002762 get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
2763 &send_cq, &recv_cq);
Eli Cohene126ba92013-07-07 17:25:49 +03002764
2765 context->flags_pd = cpu_to_be32(pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
2766 context->cqn_send = send_cq ? cpu_to_be32(send_cq->mcq.cqn) : 0;
2767 context->cqn_recv = recv_cq ? cpu_to_be32(recv_cq->mcq.cqn) : 0;
2768 context->params1 = cpu_to_be32(MLX5_IB_ACK_REQ_FREQ << 28);
2769
2770 if (attr_mask & IB_QP_RNR_RETRY)
2771 context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
2772
2773 if (attr_mask & IB_QP_RETRY_CNT)
2774 context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
2775
2776 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
2777 if (attr->max_rd_atomic)
2778 context->params1 |=
2779 cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
2780 }
2781
2782 if (attr_mask & IB_QP_SQ_PSN)
2783 context->next_send_psn = cpu_to_be32(attr->sq_psn);
2784
2785 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
2786 if (attr->max_dest_rd_atomic)
2787 context->params2 |=
2788 cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
2789 }
2790
2791 if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC))
2792 context->params2 |= to_mlx5_access_flags(qp, attr, attr_mask);
2793
2794 if (attr_mask & IB_QP_MIN_RNR_TIMER)
2795 context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
2796
2797 if (attr_mask & IB_QP_RQ_PSN)
2798 context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
2799
2800 if (attr_mask & IB_QP_QKEY)
2801 context->qkey = cpu_to_be32(attr->qkey);
2802
2803 if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
2804 context->db_rec_addr = cpu_to_be64(qp->db.dma);
2805
Mark Bloch0837e862016-06-17 15:10:55 +03002806 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
2807 u8 port_num = (attr_mask & IB_QP_PORT ? attr->port_num :
2808 qp->port) - 1;
Alex Veskereb49ab02016-08-28 12:25:53 +03002809 mibport = &dev->port[port_num];
Mark Bloch0837e862016-06-17 15:10:55 +03002810 context->qp_counter_set_usr_page |=
Alex Vesker321a9e32016-07-13 16:25:11 +03002811 cpu_to_be32((u32)(mibport->q_cnt_id) << 24);
Mark Bloch0837e862016-06-17 15:10:55 +03002812 }
2813
Eli Cohene126ba92013-07-07 17:25:49 +03002814 if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
2815 context->sq_crq_size |= cpu_to_be16(1 << 4);
2816
Haggai Eranb11a4f92016-02-29 15:45:03 +02002817 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
2818 context->deth_sqpn = cpu_to_be32(1);
Eli Cohene126ba92013-07-07 17:25:49 +03002819
2820 mlx5_cur = to_mlx5_state(cur_state);
2821 mlx5_new = to_mlx5_state(new_state);
2822 mlx5_st = to_mlx5_st(ibqp->qp_type);
Eli Cohen07c91132013-10-24 12:01:01 +03002823 if (mlx5_st < 0)
Eli Cohene126ba92013-07-07 17:25:49 +03002824 goto out;
2825
majd@mellanox.com427c1e72016-01-14 19:13:07 +02002826 if (mlx5_cur >= MLX5_QP_NUM_STATE || mlx5_new >= MLX5_QP_NUM_STATE ||
2827 !optab[mlx5_cur][mlx5_new])
2828 goto out;
2829
2830 op = optab[mlx5_cur][mlx5_new];
Eli Cohene126ba92013-07-07 17:25:49 +03002831 optpar = ib_mask_to_mlx5_opt(attr_mask);
2832 optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002833
Alex Vesker0680efa2016-08-28 12:25:52 +03002834 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET) {
2835 struct mlx5_modify_raw_qp_param raw_qp_param = {};
2836
2837 raw_qp_param.operation = op;
Alex Veskereb49ab02016-08-28 12:25:53 +03002838 if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
2839 raw_qp_param.rq_q_ctr_id = mibport->q_cnt_id;
2840 raw_qp_param.set_mask |= MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID;
2841 }
Bodong Wang7d29f342016-12-01 13:43:16 +02002842
2843 if (attr_mask & IB_QP_RATE_LIMIT) {
2844 raw_qp_param.rate_limit = attr->rate_limit;
2845 raw_qp_param.set_mask |= MLX5_RAW_QP_RATE_LIMIT;
2846 }
2847
Aviv Heller13eab212016-09-18 20:48:04 +03002848 err = modify_raw_packet_qp(dev, qp, &raw_qp_param, tx_affinity);
Alex Vesker0680efa2016-08-28 12:25:52 +03002849 } else {
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002850 err = mlx5_core_qp_modify(dev->mdev, op, optpar, context,
majd@mellanox.comad5f8e92016-01-14 19:13:08 +02002851 &base->mqp);
Alex Vesker0680efa2016-08-28 12:25:52 +03002852 }
2853
Eli Cohene126ba92013-07-07 17:25:49 +03002854 if (err)
2855 goto out;
2856
2857 qp->state = new_state;
2858
2859 if (attr_mask & IB_QP_ACCESS_FLAGS)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002860 qp->trans_qp.atomic_rd_en = attr->qp_access_flags;
Eli Cohene126ba92013-07-07 17:25:49 +03002861 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002862 qp->trans_qp.resp_depth = attr->max_dest_rd_atomic;
Eli Cohene126ba92013-07-07 17:25:49 +03002863 if (attr_mask & IB_QP_PORT)
2864 qp->port = attr->port_num;
2865 if (attr_mask & IB_QP_ALT_PATH)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002866 qp->trans_qp.alt_port = attr->alt_port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03002867
2868 /*
2869 * If we moved a kernel QP to RESET, clean up all old CQ
2870 * entries and reinitialize the QP.
2871 */
2872 if (new_state == IB_QPS_RESET && !ibqp->uobject) {
majd@mellanox.com19098df2016-01-14 19:13:03 +02002873 mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
Eli Cohene126ba92013-07-07 17:25:49 +03002874 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
2875 if (send_cq != recv_cq)
majd@mellanox.com19098df2016-01-14 19:13:03 +02002876 mlx5_ib_cq_clean(send_cq, base->mqp.qpn, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002877
2878 qp->rq.head = 0;
2879 qp->rq.tail = 0;
2880 qp->sq.head = 0;
2881 qp->sq.tail = 0;
2882 qp->sq.cur_post = 0;
2883 qp->sq.last_poll = 0;
2884 qp->db.db[MLX5_RCV_DBR] = 0;
2885 qp->db.db[MLX5_SND_DBR] = 0;
2886 }
2887
2888out:
Saeed Mahameed1a412fb2016-07-19 18:03:21 +03002889 kfree(context);
Eli Cohene126ba92013-07-07 17:25:49 +03002890 return err;
2891}
2892
2893int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2894 int attr_mask, struct ib_udata *udata)
2895{
2896 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2897 struct mlx5_ib_qp *qp = to_mqp(ibqp);
Haggai Erand16e91d2016-02-29 15:45:05 +02002898 enum ib_qp_type qp_type;
Eli Cohene126ba92013-07-07 17:25:49 +03002899 enum ib_qp_state cur_state, new_state;
2900 int err = -EINVAL;
2901 int port;
Achiad Shochat2811ba52015-12-23 18:47:24 +02002902 enum rdma_link_layer ll = IB_LINK_LAYER_UNSPECIFIED;
Eli Cohene126ba92013-07-07 17:25:49 +03002903
Yishai Hadas28d61372016-05-23 15:20:56 +03002904 if (ibqp->rwq_ind_tbl)
2905 return -ENOSYS;
2906
Haggai Erand16e91d2016-02-29 15:45:05 +02002907 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
2908 return mlx5_ib_gsi_modify_qp(ibqp, attr, attr_mask);
2909
2910 qp_type = (unlikely(ibqp->qp_type == MLX5_IB_QPT_HW_GSI)) ?
2911 IB_QPT_GSI : ibqp->qp_type;
2912
Eli Cohene126ba92013-07-07 17:25:49 +03002913 mutex_lock(&qp->mutex);
2914
2915 cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
2916 new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
2917
Achiad Shochat2811ba52015-12-23 18:47:24 +02002918 if (!(cur_state == new_state && cur_state == IB_QPS_RESET)) {
2919 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
2920 ll = dev->ib_dev.get_link_layer(&dev->ib_dev, port);
2921 }
2922
Haggai Erand16e91d2016-02-29 15:45:05 +02002923 if (qp_type != MLX5_IB_QPT_REG_UMR &&
2924 !ib_modify_qp_is_ok(cur_state, new_state, qp_type, attr_mask, ll)) {
Haggai Eran158abf82016-02-29 15:45:04 +02002925 mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
2926 cur_state, new_state, ibqp->qp_type, attr_mask);
Eli Cohene126ba92013-07-07 17:25:49 +03002927 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002928 }
Eli Cohene126ba92013-07-07 17:25:49 +03002929
2930 if ((attr_mask & IB_QP_PORT) &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03002931 (attr->port_num == 0 ||
Haggai Eran158abf82016-02-29 15:45:04 +02002932 attr->port_num > MLX5_CAP_GEN(dev->mdev, num_ports))) {
2933 mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
2934 attr->port_num, dev->num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03002935 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002936 }
Eli Cohene126ba92013-07-07 17:25:49 +03002937
2938 if (attr_mask & IB_QP_PKEY_INDEX) {
2939 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
Saeed Mahameed938fe832015-05-28 22:28:41 +03002940 if (attr->pkey_index >=
Haggai Eran158abf82016-02-29 15:45:04 +02002941 dev->mdev->port_caps[port - 1].pkey_table_len) {
2942 mlx5_ib_dbg(dev, "invalid pkey index %d\n",
2943 attr->pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03002944 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002945 }
Eli Cohene126ba92013-07-07 17:25:49 +03002946 }
2947
2948 if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03002949 attr->max_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02002950 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_res_qp))) {
2951 mlx5_ib_dbg(dev, "invalid max_rd_atomic value %d\n",
2952 attr->max_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03002953 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002954 }
Eli Cohene126ba92013-07-07 17:25:49 +03002955
2956 if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
Saeed Mahameed938fe832015-05-28 22:28:41 +03002957 attr->max_dest_rd_atomic >
Haggai Eran158abf82016-02-29 15:45:04 +02002958 (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_req_qp))) {
2959 mlx5_ib_dbg(dev, "invalid max_dest_rd_atomic value %d\n",
2960 attr->max_dest_rd_atomic);
Eli Cohene126ba92013-07-07 17:25:49 +03002961 goto out;
Haggai Eran158abf82016-02-29 15:45:04 +02002962 }
Eli Cohene126ba92013-07-07 17:25:49 +03002963
2964 if (cur_state == new_state && cur_state == IB_QPS_RESET) {
2965 err = 0;
2966 goto out;
2967 }
2968
2969 err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
2970
2971out:
2972 mutex_unlock(&qp->mutex);
2973 return err;
2974}
2975
2976static int mlx5_wq_overflow(struct mlx5_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
2977{
2978 struct mlx5_ib_cq *cq;
2979 unsigned cur;
2980
2981 cur = wq->head - wq->tail;
2982 if (likely(cur + nreq < wq->max_post))
2983 return 0;
2984
2985 cq = to_mcq(ib_cq);
2986 spin_lock(&cq->lock);
2987 cur = wq->head - wq->tail;
2988 spin_unlock(&cq->lock);
2989
2990 return cur + nreq >= wq->max_post;
2991}
2992
2993static __always_inline void set_raddr_seg(struct mlx5_wqe_raddr_seg *rseg,
2994 u64 remote_addr, u32 rkey)
2995{
2996 rseg->raddr = cpu_to_be64(remote_addr);
2997 rseg->rkey = cpu_to_be32(rkey);
2998 rseg->reserved = 0;
2999}
3000
Erez Shitritf0313962016-02-21 16:27:17 +02003001static void *set_eth_seg(struct mlx5_wqe_eth_seg *eseg,
3002 struct ib_send_wr *wr, void *qend,
3003 struct mlx5_ib_qp *qp, int *size)
3004{
3005 void *seg = eseg;
3006
3007 memset(eseg, 0, sizeof(struct mlx5_wqe_eth_seg));
3008
3009 if (wr->send_flags & IB_SEND_IP_CSUM)
3010 eseg->cs_flags = MLX5_ETH_WQE_L3_CSUM |
3011 MLX5_ETH_WQE_L4_CSUM;
3012
3013 seg += sizeof(struct mlx5_wqe_eth_seg);
3014 *size += sizeof(struct mlx5_wqe_eth_seg) / 16;
3015
3016 if (wr->opcode == IB_WR_LSO) {
3017 struct ib_ud_wr *ud_wr = container_of(wr, struct ib_ud_wr, wr);
3018 int size_of_inl_hdr_start = sizeof(eseg->inline_hdr_start);
3019 u64 left, leftlen, copysz;
3020 void *pdata = ud_wr->header;
3021
3022 left = ud_wr->hlen;
3023 eseg->mss = cpu_to_be16(ud_wr->mss);
3024 eseg->inline_hdr_sz = cpu_to_be16(left);
3025
3026 /*
3027 * check if there is space till the end of queue, if yes,
3028 * copy all in one shot, otherwise copy till the end of queue,
3029 * rollback and than the copy the left
3030 */
3031 leftlen = qend - (void *)eseg->inline_hdr_start;
3032 copysz = min_t(u64, leftlen, left);
3033
3034 memcpy(seg - size_of_inl_hdr_start, pdata, copysz);
3035
3036 if (likely(copysz > size_of_inl_hdr_start)) {
3037 seg += ALIGN(copysz - size_of_inl_hdr_start, 16);
3038 *size += ALIGN(copysz - size_of_inl_hdr_start, 16) / 16;
3039 }
3040
3041 if (unlikely(copysz < left)) { /* the last wqe in the queue */
3042 seg = mlx5_get_send_wqe(qp, 0);
3043 left -= copysz;
3044 pdata += copysz;
3045 memcpy(seg, pdata, left);
3046 seg += ALIGN(left, 16);
3047 *size += ALIGN(left, 16) / 16;
3048 }
3049 }
3050
3051 return seg;
3052}
3053
Eli Cohene126ba92013-07-07 17:25:49 +03003054static void set_datagram_seg(struct mlx5_wqe_datagram_seg *dseg,
3055 struct ib_send_wr *wr)
3056{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003057 memcpy(&dseg->av, &to_mah(ud_wr(wr)->ah)->av, sizeof(struct mlx5_av));
3058 dseg->av.dqp_dct = cpu_to_be32(ud_wr(wr)->remote_qpn | MLX5_EXTENDED_UD_AV);
3059 dseg->av.key.qkey.qkey = cpu_to_be32(ud_wr(wr)->remote_qkey);
Eli Cohene126ba92013-07-07 17:25:49 +03003060}
3061
3062static void set_data_ptr_seg(struct mlx5_wqe_data_seg *dseg, struct ib_sge *sg)
3063{
3064 dseg->byte_count = cpu_to_be32(sg->length);
3065 dseg->lkey = cpu_to_be32(sg->lkey);
3066 dseg->addr = cpu_to_be64(sg->addr);
3067}
3068
Artemy Kovalyov31616252017-01-02 11:37:42 +02003069static u64 get_xlt_octo(u64 bytes)
Eli Cohene126ba92013-07-07 17:25:49 +03003070{
Artemy Kovalyov31616252017-01-02 11:37:42 +02003071 return ALIGN(bytes, MLX5_IB_UMR_XLT_ALIGNMENT) /
3072 MLX5_IB_UMR_OCTOWORD;
Eli Cohene126ba92013-07-07 17:25:49 +03003073}
3074
3075static __be64 frwr_mkey_mask(void)
3076{
3077 u64 result;
3078
3079 result = MLX5_MKEY_MASK_LEN |
3080 MLX5_MKEY_MASK_PAGE_SIZE |
3081 MLX5_MKEY_MASK_START_ADDR |
3082 MLX5_MKEY_MASK_EN_RINVAL |
3083 MLX5_MKEY_MASK_KEY |
3084 MLX5_MKEY_MASK_LR |
3085 MLX5_MKEY_MASK_LW |
3086 MLX5_MKEY_MASK_RR |
3087 MLX5_MKEY_MASK_RW |
3088 MLX5_MKEY_MASK_A |
3089 MLX5_MKEY_MASK_SMALL_FENCE |
3090 MLX5_MKEY_MASK_FREE;
3091
3092 return cpu_to_be64(result);
3093}
3094
Sagi Grimberge6631812014-02-23 14:19:11 +02003095static __be64 sig_mkey_mask(void)
3096{
3097 u64 result;
3098
3099 result = MLX5_MKEY_MASK_LEN |
3100 MLX5_MKEY_MASK_PAGE_SIZE |
3101 MLX5_MKEY_MASK_START_ADDR |
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003102 MLX5_MKEY_MASK_EN_SIGERR |
Sagi Grimberge6631812014-02-23 14:19:11 +02003103 MLX5_MKEY_MASK_EN_RINVAL |
3104 MLX5_MKEY_MASK_KEY |
3105 MLX5_MKEY_MASK_LR |
3106 MLX5_MKEY_MASK_LW |
3107 MLX5_MKEY_MASK_RR |
3108 MLX5_MKEY_MASK_RW |
3109 MLX5_MKEY_MASK_SMALL_FENCE |
3110 MLX5_MKEY_MASK_FREE |
3111 MLX5_MKEY_MASK_BSF_EN;
3112
3113 return cpu_to_be64(result);
3114}
3115
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003116static void set_reg_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02003117 struct mlx5_ib_mr *mr)
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003118{
Artemy Kovalyov31616252017-01-02 11:37:42 +02003119 int size = mr->ndescs * mr->desc_size;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003120
3121 memset(umr, 0, sizeof(*umr));
Sagi Grimbergb005d312016-02-29 19:07:33 +02003122
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003123 umr->flags = MLX5_UMR_CHECK_NOT_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02003124 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003125 umr->mkey_mask = frwr_mkey_mask();
3126}
3127
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003128static void set_linv_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr)
Eli Cohene126ba92013-07-07 17:25:49 +03003129{
3130 memset(umr, 0, sizeof(*umr));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003131 umr->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
Max Gurtovoy2d221582016-10-27 16:36:36 +03003132 umr->flags = MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03003133}
3134
Artemy Kovalyov31616252017-01-02 11:37:42 +02003135static __be64 get_umr_enable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02003136{
3137 u64 result;
3138
Artemy Kovalyov31616252017-01-02 11:37:42 +02003139 result = MLX5_MKEY_MASK_KEY |
Haggai Eran968e78d2014-12-11 17:04:11 +02003140 MLX5_MKEY_MASK_FREE;
3141
3142 return cpu_to_be64(result);
3143}
3144
Artemy Kovalyov31616252017-01-02 11:37:42 +02003145static __be64 get_umr_disable_mr_mask(void)
Haggai Eran968e78d2014-12-11 17:04:11 +02003146{
3147 u64 result;
3148
3149 result = MLX5_MKEY_MASK_FREE;
3150
3151 return cpu_to_be64(result);
3152}
3153
Noa Osherovich56e11d62016-02-29 16:46:51 +02003154static __be64 get_umr_update_translation_mask(void)
3155{
3156 u64 result;
3157
3158 result = MLX5_MKEY_MASK_LEN |
3159 MLX5_MKEY_MASK_PAGE_SIZE |
Artemy Kovalyov31616252017-01-02 11:37:42 +02003160 MLX5_MKEY_MASK_START_ADDR;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003161
3162 return cpu_to_be64(result);
3163}
3164
Artemy Kovalyov31616252017-01-02 11:37:42 +02003165static __be64 get_umr_update_access_mask(int atomic)
Noa Osherovich56e11d62016-02-29 16:46:51 +02003166{
3167 u64 result;
3168
Artemy Kovalyov31616252017-01-02 11:37:42 +02003169 result = MLX5_MKEY_MASK_LR |
3170 MLX5_MKEY_MASK_LW |
Noa Osherovich56e11d62016-02-29 16:46:51 +02003171 MLX5_MKEY_MASK_RR |
Artemy Kovalyov31616252017-01-02 11:37:42 +02003172 MLX5_MKEY_MASK_RW;
3173
3174 if (atomic)
3175 result |= MLX5_MKEY_MASK_A;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003176
3177 return cpu_to_be64(result);
3178}
3179
3180static __be64 get_umr_update_pd_mask(void)
3181{
3182 u64 result;
3183
Artemy Kovalyov31616252017-01-02 11:37:42 +02003184 result = MLX5_MKEY_MASK_PD;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003185
3186 return cpu_to_be64(result);
3187}
3188
Eli Cohene126ba92013-07-07 17:25:49 +03003189static void set_reg_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Maor Gottlieb578e7262016-10-27 16:36:37 +03003190 struct ib_send_wr *wr, int atomic)
Eli Cohene126ba92013-07-07 17:25:49 +03003191{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003192 struct mlx5_umr_wr *umrwr = umr_wr(wr);
Eli Cohene126ba92013-07-07 17:25:49 +03003193
3194 memset(umr, 0, sizeof(*umr));
3195
Haggai Eran968e78d2014-12-11 17:04:11 +02003196 if (wr->send_flags & MLX5_IB_SEND_UMR_FAIL_IF_FREE)
3197 umr->flags = MLX5_UMR_CHECK_FREE; /* fail if free */
3198 else
3199 umr->flags = MLX5_UMR_CHECK_NOT_FREE; /* fail if not free */
3200
Artemy Kovalyov31616252017-01-02 11:37:42 +02003201 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(umrwr->xlt_size));
3202 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_XLT) {
3203 u64 offset = get_xlt_octo(umrwr->offset);
3204
3205 umr->xlt_offset = cpu_to_be16(offset & 0xffff);
3206 umr->xlt_offset_47_16 = cpu_to_be32(offset >> 16);
3207 umr->flags |= MLX5_UMR_TRANSLATION_OFFSET_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03003208 }
Artemy Kovalyov31616252017-01-02 11:37:42 +02003209 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION)
3210 umr->mkey_mask |= get_umr_update_translation_mask();
3211 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS) {
3212 umr->mkey_mask |= get_umr_update_access_mask(atomic);
3213 umr->mkey_mask |= get_umr_update_pd_mask();
3214 }
3215 if (wr->send_flags & MLX5_IB_SEND_UMR_ENABLE_MR)
3216 umr->mkey_mask |= get_umr_enable_mr_mask();
3217 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
3218 umr->mkey_mask |= get_umr_disable_mr_mask();
Eli Cohene126ba92013-07-07 17:25:49 +03003219
3220 if (!wr->num_sge)
Haggai Eran968e78d2014-12-11 17:04:11 +02003221 umr->flags |= MLX5_UMR_INLINE;
Eli Cohene126ba92013-07-07 17:25:49 +03003222}
3223
3224static u8 get_umr_flags(int acc)
3225{
3226 return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC : 0) |
3227 (acc & IB_ACCESS_REMOTE_WRITE ? MLX5_PERM_REMOTE_WRITE : 0) |
3228 (acc & IB_ACCESS_REMOTE_READ ? MLX5_PERM_REMOTE_READ : 0) |
3229 (acc & IB_ACCESS_LOCAL_WRITE ? MLX5_PERM_LOCAL_WRITE : 0) |
Sagi Grimberg2ac45932014-02-23 14:19:09 +02003230 MLX5_PERM_LOCAL_READ | MLX5_PERM_UMR_EN;
Eli Cohene126ba92013-07-07 17:25:49 +03003231}
3232
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003233static void set_reg_mkey_seg(struct mlx5_mkey_seg *seg,
3234 struct mlx5_ib_mr *mr,
3235 u32 key, int access)
3236{
3237 int ndescs = ALIGN(mr->ndescs, 8) >> 1;
3238
3239 memset(seg, 0, sizeof(*seg));
Sagi Grimbergb005d312016-02-29 19:07:33 +02003240
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003241 if (mr->access_mode == MLX5_MKC_ACCESS_MODE_MTT)
Sagi Grimbergb005d312016-02-29 19:07:33 +02003242 seg->log2_page_size = ilog2(mr->ibmr.page_size);
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003243 else if (mr->access_mode == MLX5_MKC_ACCESS_MODE_KLMS)
Sagi Grimbergb005d312016-02-29 19:07:33 +02003244 /* KLMs take twice the size of MTTs */
3245 ndescs *= 2;
3246
3247 seg->flags = get_umr_flags(access) | mr->access_mode;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003248 seg->qpn_mkey7_0 = cpu_to_be32((key & 0xff) | 0xffffff00);
3249 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL);
3250 seg->start_addr = cpu_to_be64(mr->ibmr.iova);
3251 seg->len = cpu_to_be64(mr->ibmr.length);
3252 seg->xlt_oct_size = cpu_to_be32(ndescs);
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003253}
3254
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003255static void set_linv_mkey_seg(struct mlx5_mkey_seg *seg)
Eli Cohene126ba92013-07-07 17:25:49 +03003256{
3257 memset(seg, 0, sizeof(*seg));
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003258 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03003259}
3260
3261static void set_reg_mkey_segment(struct mlx5_mkey_seg *seg, struct ib_send_wr *wr)
3262{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003263 struct mlx5_umr_wr *umrwr = umr_wr(wr);
Haggai Eran968e78d2014-12-11 17:04:11 +02003264
Eli Cohene126ba92013-07-07 17:25:49 +03003265 memset(seg, 0, sizeof(*seg));
Artemy Kovalyov31616252017-01-02 11:37:42 +02003266 if (wr->send_flags & MLX5_IB_SEND_UMR_DISABLE_MR)
Haggai Eran968e78d2014-12-11 17:04:11 +02003267 seg->status = MLX5_MKEY_STATUS_FREE;
Eli Cohene126ba92013-07-07 17:25:49 +03003268
Haggai Eran968e78d2014-12-11 17:04:11 +02003269 seg->flags = convert_access(umrwr->access_flags);
Artemy Kovalyov31616252017-01-02 11:37:42 +02003270 if (umrwr->pd)
3271 seg->flags_pd = cpu_to_be32(to_mpd(umrwr->pd)->pdn);
3272 if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_TRANSLATION &&
3273 !umrwr->length)
3274 seg->flags_pd |= cpu_to_be32(MLX5_MKEY_LEN64);
3275
3276 seg->start_addr = cpu_to_be64(umrwr->virt_addr);
Haggai Eran968e78d2014-12-11 17:04:11 +02003277 seg->len = cpu_to_be64(umrwr->length);
3278 seg->log2_page_size = umrwr->page_shift;
Eli Cohen746b5582013-10-23 09:53:14 +03003279 seg->qpn_mkey7_0 = cpu_to_be32(0xffffff00 |
Haggai Eran968e78d2014-12-11 17:04:11 +02003280 mlx5_mkey_variant(umrwr->mkey));
Eli Cohene126ba92013-07-07 17:25:49 +03003281}
3282
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003283static void set_reg_data_seg(struct mlx5_wqe_data_seg *dseg,
3284 struct mlx5_ib_mr *mr,
3285 struct mlx5_ib_pd *pd)
3286{
3287 int bcount = mr->desc_size * mr->ndescs;
3288
3289 dseg->addr = cpu_to_be64(mr->desc_map);
3290 dseg->byte_count = cpu_to_be32(ALIGN(bcount, 64));
3291 dseg->lkey = cpu_to_be32(pd->ibpd.local_dma_lkey);
3292}
3293
Eli Cohene126ba92013-07-07 17:25:49 +03003294static __be32 send_ieth(struct ib_send_wr *wr)
3295{
3296 switch (wr->opcode) {
3297 case IB_WR_SEND_WITH_IMM:
3298 case IB_WR_RDMA_WRITE_WITH_IMM:
3299 return wr->ex.imm_data;
3300
3301 case IB_WR_SEND_WITH_INV:
3302 return cpu_to_be32(wr->ex.invalidate_rkey);
3303
3304 default:
3305 return 0;
3306 }
3307}
3308
3309static u8 calc_sig(void *wqe, int size)
3310{
3311 u8 *p = wqe;
3312 u8 res = 0;
3313 int i;
3314
3315 for (i = 0; i < size; i++)
3316 res ^= p[i];
3317
3318 return ~res;
3319}
3320
3321static u8 wq_sig(void *wqe)
3322{
3323 return calc_sig(wqe, (*((u8 *)wqe + 8) & 0x3f) << 4);
3324}
3325
3326static int set_data_inl_seg(struct mlx5_ib_qp *qp, struct ib_send_wr *wr,
3327 void *wqe, int *sz)
3328{
3329 struct mlx5_wqe_inline_seg *seg;
3330 void *qend = qp->sq.qend;
3331 void *addr;
3332 int inl = 0;
3333 int copy;
3334 int len;
3335 int i;
3336
3337 seg = wqe;
3338 wqe += sizeof(*seg);
3339 for (i = 0; i < wr->num_sge; i++) {
3340 addr = (void *)(unsigned long)(wr->sg_list[i].addr);
3341 len = wr->sg_list[i].length;
3342 inl += len;
3343
3344 if (unlikely(inl > qp->max_inline_data))
3345 return -ENOMEM;
3346
3347 if (unlikely(wqe + len > qend)) {
3348 copy = qend - wqe;
3349 memcpy(wqe, addr, copy);
3350 addr += copy;
3351 len -= copy;
3352 wqe = mlx5_get_send_wqe(qp, 0);
3353 }
3354 memcpy(wqe, addr, len);
3355 wqe += len;
3356 }
3357
3358 seg->byte_count = cpu_to_be32(inl | MLX5_INLINE_SEG);
3359
3360 *sz = ALIGN(inl + sizeof(seg->byte_count), 16) / 16;
3361
3362 return 0;
3363}
3364
Sagi Grimberge6631812014-02-23 14:19:11 +02003365static u16 prot_field_size(enum ib_signature_type type)
3366{
3367 switch (type) {
3368 case IB_SIG_TYPE_T10_DIF:
3369 return MLX5_DIF_SIZE;
3370 default:
3371 return 0;
3372 }
3373}
3374
3375static u8 bs_selector(int block_size)
3376{
3377 switch (block_size) {
3378 case 512: return 0x1;
3379 case 520: return 0x2;
3380 case 4096: return 0x3;
3381 case 4160: return 0x4;
3382 case 1073741824: return 0x5;
3383 default: return 0;
3384 }
3385}
3386
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003387static void mlx5_fill_inl_bsf(struct ib_sig_domain *domain,
3388 struct mlx5_bsf_inl *inl)
Sagi Grimberge6631812014-02-23 14:19:11 +02003389{
Sagi Grimberg142537f2014-08-13 19:54:32 +03003390 /* Valid inline section and allow BSF refresh */
3391 inl->vld_refresh = cpu_to_be16(MLX5_BSF_INL_VALID |
3392 MLX5_BSF_REFRESH_DIF);
3393 inl->dif_apptag = cpu_to_be16(domain->sig.dif.app_tag);
3394 inl->dif_reftag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003395 /* repeating block */
3396 inl->rp_inv_seed = MLX5_BSF_REPEAT_BLOCK;
3397 inl->sig_type = domain->sig.dif.bg_type == IB_T10DIF_CRC ?
3398 MLX5_DIF_CRC : MLX5_DIF_IPCS;
Sagi Grimberge6631812014-02-23 14:19:11 +02003399
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003400 if (domain->sig.dif.ref_remap)
3401 inl->dif_inc_ref_guard_check |= MLX5_BSF_INC_REFTAG;
Sagi Grimberge6631812014-02-23 14:19:11 +02003402
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003403 if (domain->sig.dif.app_escape) {
3404 if (domain->sig.dif.ref_escape)
3405 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPREF_ESCAPE;
3406 else
3407 inl->dif_inc_ref_guard_check |= MLX5_BSF_APPTAG_ESCAPE;
Sagi Grimberge6631812014-02-23 14:19:11 +02003408 }
3409
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003410 inl->dif_app_bitmask_check =
3411 cpu_to_be16(domain->sig.dif.apptag_check_mask);
Sagi Grimberge6631812014-02-23 14:19:11 +02003412}
3413
3414static int mlx5_set_bsf(struct ib_mr *sig_mr,
3415 struct ib_sig_attrs *sig_attrs,
3416 struct mlx5_bsf *bsf, u32 data_size)
3417{
3418 struct mlx5_core_sig_ctx *msig = to_mmr(sig_mr)->sig;
3419 struct mlx5_bsf_basic *basic = &bsf->basic;
3420 struct ib_sig_domain *mem = &sig_attrs->mem;
3421 struct ib_sig_domain *wire = &sig_attrs->wire;
Sagi Grimberge6631812014-02-23 14:19:11 +02003422
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003423 memset(bsf, 0, sizeof(*bsf));
Sagi Grimberge6631812014-02-23 14:19:11 +02003424
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003425 /* Basic + Extended + Inline */
3426 basic->bsf_size_sbs = 1 << 7;
3427 /* Input domain check byte mask */
3428 basic->check_byte_mask = sig_attrs->check_mask;
3429 basic->raw_data_size = cpu_to_be32(data_size);
3430
3431 /* Memory domain */
3432 switch (sig_attrs->mem.sig_type) {
3433 case IB_SIG_TYPE_NONE:
3434 break;
3435 case IB_SIG_TYPE_T10_DIF:
3436 basic->mem.bs_selector = bs_selector(mem->sig.dif.pi_interval);
3437 basic->m_bfs_psv = cpu_to_be32(msig->psv_memory.psv_idx);
3438 mlx5_fill_inl_bsf(mem, &bsf->m_inl);
3439 break;
3440 default:
3441 return -EINVAL;
3442 }
3443
3444 /* Wire domain */
3445 switch (sig_attrs->wire.sig_type) {
3446 case IB_SIG_TYPE_NONE:
3447 break;
3448 case IB_SIG_TYPE_T10_DIF:
Sagi Grimberge6631812014-02-23 14:19:11 +02003449 if (mem->sig.dif.pi_interval == wire->sig.dif.pi_interval &&
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003450 mem->sig_type == wire->sig_type) {
Sagi Grimberge6631812014-02-23 14:19:11 +02003451 /* Same block structure */
Sagi Grimberg142537f2014-08-13 19:54:32 +03003452 basic->bsf_size_sbs |= 1 << 4;
Sagi Grimberge6631812014-02-23 14:19:11 +02003453 if (mem->sig.dif.bg_type == wire->sig.dif.bg_type)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003454 basic->wire.copy_byte_mask |= MLX5_CPY_GRD_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003455 if (mem->sig.dif.app_tag == wire->sig.dif.app_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003456 basic->wire.copy_byte_mask |= MLX5_CPY_APP_MASK;
Sagi Grimbergc7f44fb2014-05-18 18:32:40 +03003457 if (mem->sig.dif.ref_tag == wire->sig.dif.ref_tag)
Sagi Grimbergfd22f782014-08-13 19:54:29 +03003458 basic->wire.copy_byte_mask |= MLX5_CPY_REF_MASK;
Sagi Grimberge6631812014-02-23 14:19:11 +02003459 } else
3460 basic->wire.bs_selector = bs_selector(wire->sig.dif.pi_interval);
3461
Sagi Grimberg142537f2014-08-13 19:54:32 +03003462 basic->w_bfs_psv = cpu_to_be32(msig->psv_wire.psv_idx);
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003463 mlx5_fill_inl_bsf(wire, &bsf->w_inl);
Sagi Grimberge6631812014-02-23 14:19:11 +02003464 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003465 default:
3466 return -EINVAL;
3467 }
3468
3469 return 0;
3470}
3471
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003472static int set_sig_data_segment(struct ib_sig_handover_wr *wr,
3473 struct mlx5_ib_qp *qp, void **seg, int *size)
Sagi Grimberge6631812014-02-23 14:19:11 +02003474{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003475 struct ib_sig_attrs *sig_attrs = wr->sig_attrs;
3476 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003477 struct mlx5_bsf *bsf;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003478 u32 data_len = wr->wr.sg_list->length;
3479 u32 data_key = wr->wr.sg_list->lkey;
3480 u64 data_va = wr->wr.sg_list->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003481 int ret;
3482 int wqe_size;
3483
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003484 if (!wr->prot ||
3485 (data_key == wr->prot->lkey &&
3486 data_va == wr->prot->addr &&
3487 data_len == wr->prot->length)) {
Sagi Grimberge6631812014-02-23 14:19:11 +02003488 /**
3489 * Source domain doesn't contain signature information
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003490 * or data and protection are interleaved in memory.
Sagi Grimberge6631812014-02-23 14:19:11 +02003491 * So need construct:
3492 * ------------------
3493 * | data_klm |
3494 * ------------------
3495 * | BSF |
3496 * ------------------
3497 **/
3498 struct mlx5_klm *data_klm = *seg;
3499
3500 data_klm->bcount = cpu_to_be32(data_len);
3501 data_klm->key = cpu_to_be32(data_key);
3502 data_klm->va = cpu_to_be64(data_va);
3503 wqe_size = ALIGN(sizeof(*data_klm), 64);
3504 } else {
3505 /**
3506 * Source domain contains signature information
3507 * So need construct a strided block format:
3508 * ---------------------------
3509 * | stride_block_ctrl |
3510 * ---------------------------
3511 * | data_klm |
3512 * ---------------------------
3513 * | prot_klm |
3514 * ---------------------------
3515 * | BSF |
3516 * ---------------------------
3517 **/
3518 struct mlx5_stride_block_ctrl_seg *sblock_ctrl;
3519 struct mlx5_stride_block_entry *data_sentry;
3520 struct mlx5_stride_block_entry *prot_sentry;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003521 u32 prot_key = wr->prot->lkey;
3522 u64 prot_va = wr->prot->addr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003523 u16 block_size = sig_attrs->mem.sig.dif.pi_interval;
3524 int prot_size;
3525
3526 sblock_ctrl = *seg;
3527 data_sentry = (void *)sblock_ctrl + sizeof(*sblock_ctrl);
3528 prot_sentry = (void *)data_sentry + sizeof(*data_sentry);
3529
3530 prot_size = prot_field_size(sig_attrs->mem.sig_type);
3531 if (!prot_size) {
3532 pr_err("Bad block size given: %u\n", block_size);
3533 return -EINVAL;
3534 }
3535 sblock_ctrl->bcount_per_cycle = cpu_to_be32(block_size +
3536 prot_size);
3537 sblock_ctrl->op = cpu_to_be32(MLX5_STRIDE_BLOCK_OP);
3538 sblock_ctrl->repeat_count = cpu_to_be32(data_len / block_size);
3539 sblock_ctrl->num_entries = cpu_to_be16(2);
3540
3541 data_sentry->bcount = cpu_to_be16(block_size);
3542 data_sentry->key = cpu_to_be32(data_key);
3543 data_sentry->va = cpu_to_be64(data_va);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003544 data_sentry->stride = cpu_to_be16(block_size);
3545
Sagi Grimberge6631812014-02-23 14:19:11 +02003546 prot_sentry->bcount = cpu_to_be16(prot_size);
3547 prot_sentry->key = cpu_to_be32(prot_key);
Sagi Grimberg5c273b12014-05-18 18:32:39 +03003548 prot_sentry->va = cpu_to_be64(prot_va);
3549 prot_sentry->stride = cpu_to_be16(prot_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02003550
Sagi Grimberge6631812014-02-23 14:19:11 +02003551 wqe_size = ALIGN(sizeof(*sblock_ctrl) + sizeof(*data_sentry) +
3552 sizeof(*prot_sentry), 64);
3553 }
3554
3555 *seg += wqe_size;
3556 *size += wqe_size / 16;
3557 if (unlikely((*seg == qp->sq.qend)))
3558 *seg = mlx5_get_send_wqe(qp, 0);
3559
3560 bsf = *seg;
3561 ret = mlx5_set_bsf(sig_mr, sig_attrs, bsf, data_len);
3562 if (ret)
3563 return -EINVAL;
3564
3565 *seg += sizeof(*bsf);
3566 *size += sizeof(*bsf) / 16;
3567 if (unlikely((*seg == qp->sq.qend)))
3568 *seg = mlx5_get_send_wqe(qp, 0);
3569
3570 return 0;
3571}
3572
3573static void set_sig_mkey_segment(struct mlx5_mkey_seg *seg,
Artemy Kovalyov31616252017-01-02 11:37:42 +02003574 struct ib_sig_handover_wr *wr, u32 size,
Sagi Grimberge6631812014-02-23 14:19:11 +02003575 u32 length, u32 pdn)
3576{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003577 struct ib_mr *sig_mr = wr->sig_mr;
Sagi Grimberge6631812014-02-23 14:19:11 +02003578 u32 sig_key = sig_mr->rkey;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003579 u8 sigerr = to_mmr(sig_mr)->sig->sigerr_count & 1;
Sagi Grimberge6631812014-02-23 14:19:11 +02003580
3581 memset(seg, 0, sizeof(*seg));
3582
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003583 seg->flags = get_umr_flags(wr->access_flags) |
Saeed Mahameedec22eb52016-07-16 06:28:36 +03003584 MLX5_MKC_ACCESS_MODE_KLMS;
Sagi Grimberge6631812014-02-23 14:19:11 +02003585 seg->qpn_mkey7_0 = cpu_to_be32((sig_key & 0xff) | 0xffffff00);
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003586 seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL | sigerr << 26 |
Sagi Grimberge6631812014-02-23 14:19:11 +02003587 MLX5_MKEY_BSF_EN | pdn);
3588 seg->len = cpu_to_be64(length);
Artemy Kovalyov31616252017-01-02 11:37:42 +02003589 seg->xlt_oct_size = cpu_to_be32(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02003590 seg->bsfs_octo_size = cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE);
3591}
3592
3593static void set_sig_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
Artemy Kovalyov31616252017-01-02 11:37:42 +02003594 u32 size)
Sagi Grimberge6631812014-02-23 14:19:11 +02003595{
3596 memset(umr, 0, sizeof(*umr));
3597
3598 umr->flags = MLX5_FLAGS_INLINE | MLX5_FLAGS_CHECK_FREE;
Artemy Kovalyov31616252017-01-02 11:37:42 +02003599 umr->xlt_octowords = cpu_to_be16(get_xlt_octo(size));
Sagi Grimberge6631812014-02-23 14:19:11 +02003600 umr->bsf_octowords = cpu_to_be16(MLX5_MKEY_BSF_OCTO_SIZE);
3601 umr->mkey_mask = sig_mkey_mask();
3602}
3603
3604
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003605static int set_sig_umr_wr(struct ib_send_wr *send_wr, struct mlx5_ib_qp *qp,
Sagi Grimberge6631812014-02-23 14:19:11 +02003606 void **seg, int *size)
3607{
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003608 struct ib_sig_handover_wr *wr = sig_handover_wr(send_wr);
3609 struct mlx5_ib_mr *sig_mr = to_mmr(wr->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02003610 u32 pdn = get_pd(qp)->pdn;
Artemy Kovalyov31616252017-01-02 11:37:42 +02003611 u32 xlt_size;
Sagi Grimberge6631812014-02-23 14:19:11 +02003612 int region_len, ret;
3613
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003614 if (unlikely(wr->wr.num_sge != 1) ||
3615 unlikely(wr->access_flags & IB_ACCESS_REMOTE_ATOMIC) ||
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003616 unlikely(!sig_mr->sig) || unlikely(!qp->signature_en) ||
3617 unlikely(!sig_mr->sig->sig_status_checked))
Sagi Grimberge6631812014-02-23 14:19:11 +02003618 return -EINVAL;
3619
3620 /* length of the protected region, data + protection */
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003621 region_len = wr->wr.sg_list->length;
3622 if (wr->prot &&
3623 (wr->prot->lkey != wr->wr.sg_list->lkey ||
3624 wr->prot->addr != wr->wr.sg_list->addr ||
3625 wr->prot->length != wr->wr.sg_list->length))
3626 region_len += wr->prot->length;
Sagi Grimberge6631812014-02-23 14:19:11 +02003627
3628 /**
3629 * KLM octoword size - if protection was provided
3630 * then we use strided block format (3 octowords),
3631 * else we use single KLM (1 octoword)
3632 **/
Artemy Kovalyov31616252017-01-02 11:37:42 +02003633 xlt_size = wr->prot ? 0x30 : sizeof(struct mlx5_klm);
Sagi Grimberge6631812014-02-23 14:19:11 +02003634
Artemy Kovalyov31616252017-01-02 11:37:42 +02003635 set_sig_umr_segment(*seg, xlt_size);
Sagi Grimberge6631812014-02-23 14:19:11 +02003636 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
3637 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
3638 if (unlikely((*seg == qp->sq.qend)))
3639 *seg = mlx5_get_send_wqe(qp, 0);
3640
Artemy Kovalyov31616252017-01-02 11:37:42 +02003641 set_sig_mkey_segment(*seg, wr, xlt_size, region_len, pdn);
Sagi Grimberge6631812014-02-23 14:19:11 +02003642 *seg += sizeof(struct mlx5_mkey_seg);
3643 *size += sizeof(struct mlx5_mkey_seg) / 16;
3644 if (unlikely((*seg == qp->sq.qend)))
3645 *seg = mlx5_get_send_wqe(qp, 0);
3646
3647 ret = set_sig_data_segment(wr, qp, seg, size);
3648 if (ret)
3649 return ret;
3650
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003651 sig_mr->sig->sig_status_checked = false;
Sagi Grimberge6631812014-02-23 14:19:11 +02003652 return 0;
3653}
3654
3655static int set_psv_wr(struct ib_sig_domain *domain,
3656 u32 psv_idx, void **seg, int *size)
3657{
3658 struct mlx5_seg_set_psv *psv_seg = *seg;
3659
3660 memset(psv_seg, 0, sizeof(*psv_seg));
3661 psv_seg->psv_num = cpu_to_be32(psv_idx);
3662 switch (domain->sig_type) {
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003663 case IB_SIG_TYPE_NONE:
3664 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003665 case IB_SIG_TYPE_T10_DIF:
3666 psv_seg->transient_sig = cpu_to_be32(domain->sig.dif.bg << 16 |
3667 domain->sig.dif.app_tag);
3668 psv_seg->ref_tag = cpu_to_be32(domain->sig.dif.ref_tag);
Sagi Grimberge6631812014-02-23 14:19:11 +02003669 break;
Sagi Grimberge6631812014-02-23 14:19:11 +02003670 default:
3671 pr_err("Bad signature type given.\n");
3672 return 1;
3673 }
3674
Sagi Grimberg78eda2b2014-08-13 19:54:35 +03003675 *seg += sizeof(*psv_seg);
3676 *size += sizeof(*psv_seg) / 16;
3677
Sagi Grimberge6631812014-02-23 14:19:11 +02003678 return 0;
3679}
3680
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003681static int set_reg_wr(struct mlx5_ib_qp *qp,
3682 struct ib_reg_wr *wr,
3683 void **seg, int *size)
3684{
3685 struct mlx5_ib_mr *mr = to_mmr(wr->mr);
3686 struct mlx5_ib_pd *pd = to_mpd(qp->ibqp.pd);
3687
3688 if (unlikely(wr->wr.send_flags & IB_SEND_INLINE)) {
3689 mlx5_ib_warn(to_mdev(qp->ibqp.device),
3690 "Invalid IB_SEND_INLINE send flag\n");
3691 return -EINVAL;
3692 }
3693
3694 set_reg_umr_seg(*seg, mr);
3695 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
3696 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
3697 if (unlikely((*seg == qp->sq.qend)))
3698 *seg = mlx5_get_send_wqe(qp, 0);
3699
3700 set_reg_mkey_seg(*seg, mr, wr->key, wr->access);
3701 *seg += sizeof(struct mlx5_mkey_seg);
3702 *size += sizeof(struct mlx5_mkey_seg) / 16;
3703 if (unlikely((*seg == qp->sq.qend)))
3704 *seg = mlx5_get_send_wqe(qp, 0);
3705
3706 set_reg_data_seg(*seg, mr, pd);
3707 *seg += sizeof(struct mlx5_wqe_data_seg);
3708 *size += (sizeof(struct mlx5_wqe_data_seg) / 16);
3709
3710 return 0;
3711}
3712
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003713static void set_linv_wr(struct mlx5_ib_qp *qp, void **seg, int *size)
Eli Cohene126ba92013-07-07 17:25:49 +03003714{
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003715 set_linv_umr_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03003716 *seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
3717 *size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
3718 if (unlikely((*seg == qp->sq.qend)))
3719 *seg = mlx5_get_send_wqe(qp, 0);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003720 set_linv_mkey_seg(*seg);
Eli Cohene126ba92013-07-07 17:25:49 +03003721 *seg += sizeof(struct mlx5_mkey_seg);
3722 *size += sizeof(struct mlx5_mkey_seg) / 16;
3723 if (unlikely((*seg == qp->sq.qend)))
3724 *seg = mlx5_get_send_wqe(qp, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03003725}
3726
3727static void dump_wqe(struct mlx5_ib_qp *qp, int idx, int size_16)
3728{
3729 __be32 *p = NULL;
3730 int tidx = idx;
3731 int i, j;
3732
3733 pr_debug("dump wqe at %p\n", mlx5_get_send_wqe(qp, tidx));
3734 for (i = 0, j = 0; i < size_16 * 4; i += 4, j += 4) {
3735 if ((i & 0xf) == 0) {
3736 void *buf = mlx5_get_send_wqe(qp, tidx);
3737 tidx = (tidx + 1) & (qp->sq.wqe_cnt - 1);
3738 p = buf;
3739 j = 0;
3740 }
3741 pr_debug("%08x %08x %08x %08x\n", be32_to_cpu(p[j]),
3742 be32_to_cpu(p[j + 1]), be32_to_cpu(p[j + 2]),
3743 be32_to_cpu(p[j + 3]));
3744 }
3745}
3746
3747static void mlx5_bf_copy(u64 __iomem *dst, u64 *src,
3748 unsigned bytecnt, struct mlx5_ib_qp *qp)
3749{
3750 while (bytecnt > 0) {
3751 __iowrite64_copy(dst++, src++, 8);
3752 __iowrite64_copy(dst++, src++, 8);
3753 __iowrite64_copy(dst++, src++, 8);
3754 __iowrite64_copy(dst++, src++, 8);
3755 __iowrite64_copy(dst++, src++, 8);
3756 __iowrite64_copy(dst++, src++, 8);
3757 __iowrite64_copy(dst++, src++, 8);
3758 __iowrite64_copy(dst++, src++, 8);
3759 bytecnt -= 64;
3760 if (unlikely(src == qp->sq.qend))
3761 src = mlx5_get_send_wqe(qp, 0);
3762 }
3763}
3764
3765static u8 get_fence(u8 fence, struct ib_send_wr *wr)
3766{
3767 if (unlikely(wr->opcode == IB_WR_LOCAL_INV &&
3768 wr->send_flags & IB_SEND_FENCE))
3769 return MLX5_FENCE_MODE_STRONG_ORDERING;
3770
3771 if (unlikely(fence)) {
3772 if (wr->send_flags & IB_SEND_FENCE)
3773 return MLX5_FENCE_MODE_SMALL_AND_FENCE;
3774 else
3775 return fence;
Eli Cohenc9b25492016-06-22 17:27:26 +03003776 } else if (unlikely(wr->send_flags & IB_SEND_FENCE)) {
3777 return MLX5_FENCE_MODE_FENCE;
Eli Cohene126ba92013-07-07 17:25:49 +03003778 }
Eli Cohenc9b25492016-06-22 17:27:26 +03003779
3780 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003781}
3782
Sagi Grimberg6e5eada2014-02-23 14:19:08 +02003783static int begin_wqe(struct mlx5_ib_qp *qp, void **seg,
3784 struct mlx5_wqe_ctrl_seg **ctrl,
Eli Cohen6a4f1392014-12-02 12:26:18 +02003785 struct ib_send_wr *wr, unsigned *idx,
Sagi Grimberg6e5eada2014-02-23 14:19:08 +02003786 int *size, int nreq)
3787{
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03003788 if (unlikely(mlx5_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)))
3789 return -ENOMEM;
Sagi Grimberg6e5eada2014-02-23 14:19:08 +02003790
3791 *idx = qp->sq.cur_post & (qp->sq.wqe_cnt - 1);
3792 *seg = mlx5_get_send_wqe(qp, *idx);
3793 *ctrl = *seg;
3794 *(uint32_t *)(*seg + 8) = 0;
3795 (*ctrl)->imm = send_ieth(wr);
3796 (*ctrl)->fm_ce_se = qp->sq_signal_bits |
3797 (wr->send_flags & IB_SEND_SIGNALED ?
3798 MLX5_WQE_CTRL_CQ_UPDATE : 0) |
3799 (wr->send_flags & IB_SEND_SOLICITED ?
3800 MLX5_WQE_CTRL_SOLICITED : 0);
3801
3802 *seg += sizeof(**ctrl);
3803 *size = sizeof(**ctrl) / 16;
3804
Leon Romanovskyb2a232d2016-08-28 10:58:35 +03003805 return 0;
Sagi Grimberg6e5eada2014-02-23 14:19:08 +02003806}
3807
3808static void finish_wqe(struct mlx5_ib_qp *qp,
3809 struct mlx5_wqe_ctrl_seg *ctrl,
3810 u8 size, unsigned idx, u64 wr_id,
3811 int nreq, u8 fence, u8 next_fence,
3812 u32 mlx5_opcode)
3813{
3814 u8 opmod = 0;
3815
3816 ctrl->opmod_idx_opcode = cpu_to_be32(((u32)(qp->sq.cur_post) << 8) |
3817 mlx5_opcode | ((u32)opmod << 24));
majd@mellanox.com19098df2016-01-14 19:13:03 +02003818 ctrl->qpn_ds = cpu_to_be32(size | (qp->trans_qp.base.mqp.qpn << 8));
Sagi Grimberg6e5eada2014-02-23 14:19:08 +02003819 ctrl->fm_ce_se |= fence;
3820 qp->fm_cache = next_fence;
3821 if (unlikely(qp->wq_sig))
3822 ctrl->signature = wq_sig(ctrl);
3823
3824 qp->sq.wrid[idx] = wr_id;
3825 qp->sq.w_list[idx].opcode = mlx5_opcode;
3826 qp->sq.wqe_head[idx] = qp->sq.head + nreq;
3827 qp->sq.cur_post += DIV_ROUND_UP(size * 16, MLX5_SEND_WQE_BB);
3828 qp->sq.w_list[idx].next = qp->sq.cur_post;
3829}
3830
3831
Eli Cohene126ba92013-07-07 17:25:49 +03003832int mlx5_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
3833 struct ib_send_wr **bad_wr)
3834{
3835 struct mlx5_wqe_ctrl_seg *ctrl = NULL; /* compiler warning */
3836 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003837 struct mlx5_core_dev *mdev = dev->mdev;
Haggai Erand16e91d2016-02-29 15:45:05 +02003838 struct mlx5_ib_qp *qp;
Sagi Grimberge6631812014-02-23 14:19:11 +02003839 struct mlx5_ib_mr *mr;
Eli Cohene126ba92013-07-07 17:25:49 +03003840 struct mlx5_wqe_data_seg *dpseg;
3841 struct mlx5_wqe_xrc_seg *xrc;
Haggai Erand16e91d2016-02-29 15:45:05 +02003842 struct mlx5_bf *bf;
Eli Cohene126ba92013-07-07 17:25:49 +03003843 int uninitialized_var(size);
Haggai Erand16e91d2016-02-29 15:45:05 +02003844 void *qend;
Eli Cohene126ba92013-07-07 17:25:49 +03003845 unsigned long flags;
Eli Cohene126ba92013-07-07 17:25:49 +03003846 unsigned idx;
3847 int err = 0;
3848 int inl = 0;
3849 int num_sge;
3850 void *seg;
3851 int nreq;
3852 int i;
3853 u8 next_fence = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03003854 u8 fence;
3855
Haggai Erand16e91d2016-02-29 15:45:05 +02003856 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
3857 return mlx5_ib_gsi_post_send(ibqp, wr, bad_wr);
3858
3859 qp = to_mqp(ibqp);
3860 bf = qp->bf;
3861 qend = qp->sq.qend;
3862
Eli Cohene126ba92013-07-07 17:25:49 +03003863 spin_lock_irqsave(&qp->sq.lock, flags);
3864
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003865 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
3866 err = -EIO;
3867 *bad_wr = wr;
3868 nreq = 0;
3869 goto out;
3870 }
3871
Eli Cohene126ba92013-07-07 17:25:49 +03003872 for (nreq = 0; wr; nreq++, wr = wr->next) {
Fabian Fredericka8f731e2014-08-12 19:20:08 -04003873 if (unlikely(wr->opcode >= ARRAY_SIZE(mlx5_ib_opcode))) {
Eli Cohene126ba92013-07-07 17:25:49 +03003874 mlx5_ib_warn(dev, "\n");
3875 err = -EINVAL;
3876 *bad_wr = wr;
3877 goto out;
3878 }
3879
Eli Cohene126ba92013-07-07 17:25:49 +03003880 fence = qp->fm_cache;
3881 num_sge = wr->num_sge;
3882 if (unlikely(num_sge > qp->sq.max_gs)) {
3883 mlx5_ib_warn(dev, "\n");
Chuck Lever24be4092016-08-28 10:58:34 +03003884 err = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03003885 *bad_wr = wr;
3886 goto out;
3887 }
3888
Sagi Grimberg6e5eada2014-02-23 14:19:08 +02003889 err = begin_wqe(qp, &seg, &ctrl, wr, &idx, &size, nreq);
3890 if (err) {
3891 mlx5_ib_warn(dev, "\n");
3892 err = -ENOMEM;
3893 *bad_wr = wr;
3894 goto out;
3895 }
Eli Cohene126ba92013-07-07 17:25:49 +03003896
3897 switch (ibqp->qp_type) {
3898 case IB_QPT_XRC_INI:
3899 xrc = seg;
Eli Cohene126ba92013-07-07 17:25:49 +03003900 seg += sizeof(*xrc);
3901 size += sizeof(*xrc) / 16;
3902 /* fall through */
3903 case IB_QPT_RC:
3904 switch (wr->opcode) {
3905 case IB_WR_RDMA_READ:
3906 case IB_WR_RDMA_WRITE:
3907 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003908 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
3909 rdma_wr(wr)->rkey);
Jack Morgensteinf241e742014-07-28 23:30:23 +03003910 seg += sizeof(struct mlx5_wqe_raddr_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03003911 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
3912 break;
3913
3914 case IB_WR_ATOMIC_CMP_AND_SWP:
3915 case IB_WR_ATOMIC_FETCH_AND_ADD:
Eli Cohene126ba92013-07-07 17:25:49 +03003916 case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
Eli Cohen81bea282013-09-11 16:35:30 +03003917 mlx5_ib_warn(dev, "Atomic operations are not supported yet\n");
3918 err = -ENOSYS;
3919 *bad_wr = wr;
3920 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03003921
3922 case IB_WR_LOCAL_INV:
3923 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
3924 qp->sq.wr_data[idx] = IB_WR_LOCAL_INV;
3925 ctrl->imm = cpu_to_be32(wr->ex.invalidate_rkey);
Sagi Grimbergdd01e662015-10-13 19:11:42 +03003926 set_linv_wr(qp, &seg, &size);
Eli Cohene126ba92013-07-07 17:25:49 +03003927 num_sge = 0;
3928 break;
3929
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003930 case IB_WR_REG_MR:
3931 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
3932 qp->sq.wr_data[idx] = IB_WR_REG_MR;
3933 ctrl->imm = cpu_to_be32(reg_wr(wr)->key);
3934 err = set_reg_wr(qp, reg_wr(wr), &seg, &size);
3935 if (err) {
3936 *bad_wr = wr;
3937 goto out;
3938 }
3939 num_sge = 0;
3940 break;
3941
Sagi Grimberge6631812014-02-23 14:19:11 +02003942 case IB_WR_REG_SIG_MR:
3943 qp->sq.wr_data[idx] = IB_WR_REG_SIG_MR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003944 mr = to_mmr(sig_handover_wr(wr)->sig_mr);
Sagi Grimberge6631812014-02-23 14:19:11 +02003945
3946 ctrl->imm = cpu_to_be32(mr->ibmr.rkey);
3947 err = set_sig_umr_wr(wr, qp, &seg, &size);
3948 if (err) {
3949 mlx5_ib_warn(dev, "\n");
3950 *bad_wr = wr;
3951 goto out;
3952 }
3953
3954 finish_wqe(qp, ctrl, size, idx, wr->wr_id,
3955 nreq, get_fence(fence, wr),
3956 next_fence, MLX5_OPCODE_UMR);
3957 /*
3958 * SET_PSV WQEs are not signaled and solicited
3959 * on error
3960 */
3961 wr->send_flags &= ~IB_SEND_SIGNALED;
3962 wr->send_flags |= IB_SEND_SOLICITED;
3963 err = begin_wqe(qp, &seg, &ctrl, wr,
3964 &idx, &size, nreq);
3965 if (err) {
3966 mlx5_ib_warn(dev, "\n");
3967 err = -ENOMEM;
3968 *bad_wr = wr;
3969 goto out;
3970 }
3971
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003972 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->mem,
Sagi Grimberge6631812014-02-23 14:19:11 +02003973 mr->sig->psv_memory.psv_idx, &seg,
3974 &size);
3975 if (err) {
3976 mlx5_ib_warn(dev, "\n");
3977 *bad_wr = wr;
3978 goto out;
3979 }
3980
3981 finish_wqe(qp, ctrl, size, idx, wr->wr_id,
3982 nreq, get_fence(fence, wr),
3983 next_fence, MLX5_OPCODE_SET_PSV);
3984 err = begin_wqe(qp, &seg, &ctrl, wr,
3985 &idx, &size, nreq);
3986 if (err) {
3987 mlx5_ib_warn(dev, "\n");
3988 err = -ENOMEM;
3989 *bad_wr = wr;
3990 goto out;
3991 }
3992
3993 next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01003994 err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->wire,
Sagi Grimberge6631812014-02-23 14:19:11 +02003995 mr->sig->psv_wire.psv_idx, &seg,
3996 &size);
3997 if (err) {
3998 mlx5_ib_warn(dev, "\n");
3999 *bad_wr = wr;
4000 goto out;
4001 }
4002
4003 finish_wqe(qp, ctrl, size, idx, wr->wr_id,
4004 nreq, get_fence(fence, wr),
4005 next_fence, MLX5_OPCODE_SET_PSV);
4006 num_sge = 0;
4007 goto skip_psv;
4008
Eli Cohene126ba92013-07-07 17:25:49 +03004009 default:
4010 break;
4011 }
4012 break;
4013
4014 case IB_QPT_UC:
4015 switch (wr->opcode) {
4016 case IB_WR_RDMA_WRITE:
4017 case IB_WR_RDMA_WRITE_WITH_IMM:
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004018 set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
4019 rdma_wr(wr)->rkey);
Eli Cohene126ba92013-07-07 17:25:49 +03004020 seg += sizeof(struct mlx5_wqe_raddr_seg);
4021 size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
4022 break;
4023
4024 default:
4025 break;
4026 }
4027 break;
4028
Eli Cohene126ba92013-07-07 17:25:49 +03004029 case IB_QPT_SMI:
Haggai Erand16e91d2016-02-29 15:45:05 +02004030 case MLX5_IB_QPT_HW_GSI:
Eli Cohene126ba92013-07-07 17:25:49 +03004031 set_datagram_seg(seg, wr);
Jack Morgensteinf241e742014-07-28 23:30:23 +03004032 seg += sizeof(struct mlx5_wqe_datagram_seg);
Eli Cohene126ba92013-07-07 17:25:49 +03004033 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
4034 if (unlikely((seg == qend)))
4035 seg = mlx5_get_send_wqe(qp, 0);
4036 break;
Erez Shitritf0313962016-02-21 16:27:17 +02004037 case IB_QPT_UD:
4038 set_datagram_seg(seg, wr);
4039 seg += sizeof(struct mlx5_wqe_datagram_seg);
4040 size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
Eli Cohene126ba92013-07-07 17:25:49 +03004041
Erez Shitritf0313962016-02-21 16:27:17 +02004042 if (unlikely((seg == qend)))
4043 seg = mlx5_get_send_wqe(qp, 0);
4044
4045 /* handle qp that supports ud offload */
4046 if (qp->flags & IB_QP_CREATE_IPOIB_UD_LSO) {
4047 struct mlx5_wqe_eth_pad *pad;
4048
4049 pad = seg;
4050 memset(pad, 0, sizeof(struct mlx5_wqe_eth_pad));
4051 seg += sizeof(struct mlx5_wqe_eth_pad);
4052 size += sizeof(struct mlx5_wqe_eth_pad) / 16;
4053
4054 seg = set_eth_seg(seg, wr, qend, qp, &size);
4055
4056 if (unlikely((seg == qend)))
4057 seg = mlx5_get_send_wqe(qp, 0);
4058 }
4059 break;
Eli Cohene126ba92013-07-07 17:25:49 +03004060 case MLX5_IB_QPT_REG_UMR:
4061 if (wr->opcode != MLX5_IB_WR_UMR) {
4062 err = -EINVAL;
4063 mlx5_ib_warn(dev, "bad opcode\n");
4064 goto out;
4065 }
4066 qp->sq.wr_data[idx] = MLX5_IB_WR_UMR;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01004067 ctrl->imm = cpu_to_be32(umr_wr(wr)->mkey);
Maor Gottlieb578e7262016-10-27 16:36:37 +03004068 set_reg_umr_segment(seg, wr, !!(MLX5_CAP_GEN(mdev, atomic)));
Eli Cohene126ba92013-07-07 17:25:49 +03004069 seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
4070 size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
4071 if (unlikely((seg == qend)))
4072 seg = mlx5_get_send_wqe(qp, 0);
4073 set_reg_mkey_segment(seg, wr);
4074 seg += sizeof(struct mlx5_mkey_seg);
4075 size += sizeof(struct mlx5_mkey_seg) / 16;
4076 if (unlikely((seg == qend)))
4077 seg = mlx5_get_send_wqe(qp, 0);
4078 break;
4079
4080 default:
4081 break;
4082 }
4083
4084 if (wr->send_flags & IB_SEND_INLINE && num_sge) {
4085 int uninitialized_var(sz);
4086
4087 err = set_data_inl_seg(qp, wr, seg, &sz);
4088 if (unlikely(err)) {
4089 mlx5_ib_warn(dev, "\n");
4090 *bad_wr = wr;
4091 goto out;
4092 }
4093 inl = 1;
4094 size += sz;
4095 } else {
4096 dpseg = seg;
4097 for (i = 0; i < num_sge; i++) {
4098 if (unlikely(dpseg == qend)) {
4099 seg = mlx5_get_send_wqe(qp, 0);
4100 dpseg = seg;
4101 }
4102 if (likely(wr->sg_list[i].length)) {
4103 set_data_ptr_seg(dpseg, wr->sg_list + i);
4104 size += sizeof(struct mlx5_wqe_data_seg) / 16;
4105 dpseg++;
4106 }
4107 }
4108 }
4109
Sagi Grimberg6e5eada2014-02-23 14:19:08 +02004110 finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
4111 get_fence(fence, wr), next_fence,
4112 mlx5_ib_opcode[wr->opcode]);
Sagi Grimberge6631812014-02-23 14:19:11 +02004113skip_psv:
Eli Cohene126ba92013-07-07 17:25:49 +03004114 if (0)
4115 dump_wqe(qp, idx, size);
4116 }
4117
4118out:
4119 if (likely(nreq)) {
4120 qp->sq.head += nreq;
4121
4122 /* Make sure that descriptors are written before
4123 * updating doorbell record and ringing the doorbell
4124 */
4125 wmb();
4126
4127 qp->db.db[MLX5_SND_DBR] = cpu_to_be32(qp->sq.cur_post);
4128
Eli Cohenada388f2014-01-14 17:45:16 +02004129 /* Make sure doorbell record is visible to the HCA before
4130 * we hit doorbell */
4131 wmb();
4132
Eli Cohene126ba92013-07-07 17:25:49 +03004133 if (bf->need_lock)
4134 spin_lock(&bf->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02004135 else
4136 __acquire(&bf->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03004137
4138 /* TBD enable WC */
Eli Cohen2f5ff262017-01-03 23:55:21 +02004139 if (0 && nreq == 1 && bf->bfregn && inl && size > 1 && size <= bf->buf_size / 16) {
Eli Cohene126ba92013-07-07 17:25:49 +03004140 mlx5_bf_copy(bf->reg + bf->offset, (u64 *)ctrl, ALIGN(size * 16, 64), qp);
4141 /* wc_wmb(); */
4142 } else {
4143 mlx5_write64((__be32 *)ctrl, bf->regreg + bf->offset,
4144 MLX5_GET_DOORBELL_LOCK(&bf->lock32));
4145 /* Make sure doorbells don't leak out of SQ spinlock
4146 * and reach the HCA out of order.
4147 */
4148 mmiowb();
4149 }
4150 bf->offset ^= bf->buf_size;
4151 if (bf->need_lock)
4152 spin_unlock(&bf->lock);
Eli Cohen6a4f1392014-12-02 12:26:18 +02004153 else
4154 __release(&bf->lock);
Eli Cohene126ba92013-07-07 17:25:49 +03004155 }
4156
4157 spin_unlock_irqrestore(&qp->sq.lock, flags);
4158
4159 return err;
4160}
4161
4162static void set_sig_seg(struct mlx5_rwqe_sig *sig, int size)
4163{
4164 sig->signature = calc_sig(sig, size);
4165}
4166
4167int mlx5_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
4168 struct ib_recv_wr **bad_wr)
4169{
4170 struct mlx5_ib_qp *qp = to_mqp(ibqp);
4171 struct mlx5_wqe_data_seg *scat;
4172 struct mlx5_rwqe_sig *sig;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004173 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4174 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004175 unsigned long flags;
4176 int err = 0;
4177 int nreq;
4178 int ind;
4179 int i;
4180
Haggai Erand16e91d2016-02-29 15:45:05 +02004181 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4182 return mlx5_ib_gsi_post_recv(ibqp, wr, bad_wr);
4183
Eli Cohene126ba92013-07-07 17:25:49 +03004184 spin_lock_irqsave(&qp->rq.lock, flags);
4185
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004186 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
4187 err = -EIO;
4188 *bad_wr = wr;
4189 nreq = 0;
4190 goto out;
4191 }
4192
Eli Cohene126ba92013-07-07 17:25:49 +03004193 ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
4194
4195 for (nreq = 0; wr; nreq++, wr = wr->next) {
4196 if (mlx5_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
4197 err = -ENOMEM;
4198 *bad_wr = wr;
4199 goto out;
4200 }
4201
4202 if (unlikely(wr->num_sge > qp->rq.max_gs)) {
4203 err = -EINVAL;
4204 *bad_wr = wr;
4205 goto out;
4206 }
4207
4208 scat = get_recv_wqe(qp, ind);
4209 if (qp->wq_sig)
4210 scat++;
4211
4212 for (i = 0; i < wr->num_sge; i++)
4213 set_data_ptr_seg(scat + i, wr->sg_list + i);
4214
4215 if (i < qp->rq.max_gs) {
4216 scat[i].byte_count = 0;
4217 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
4218 scat[i].addr = 0;
4219 }
4220
4221 if (qp->wq_sig) {
4222 sig = (struct mlx5_rwqe_sig *)scat;
4223 set_sig_seg(sig, (qp->rq.max_gs + 1) << 2);
4224 }
4225
4226 qp->rq.wrid[ind] = wr->wr_id;
4227
4228 ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
4229 }
4230
4231out:
4232 if (likely(nreq)) {
4233 qp->rq.head += nreq;
4234
4235 /* Make sure that descriptors are written before
4236 * doorbell record.
4237 */
4238 wmb();
4239
4240 *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
4241 }
4242
4243 spin_unlock_irqrestore(&qp->rq.lock, flags);
4244
4245 return err;
4246}
4247
4248static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
4249{
4250 switch (mlx5_state) {
4251 case MLX5_QP_STATE_RST: return IB_QPS_RESET;
4252 case MLX5_QP_STATE_INIT: return IB_QPS_INIT;
4253 case MLX5_QP_STATE_RTR: return IB_QPS_RTR;
4254 case MLX5_QP_STATE_RTS: return IB_QPS_RTS;
4255 case MLX5_QP_STATE_SQ_DRAINING:
4256 case MLX5_QP_STATE_SQD: return IB_QPS_SQD;
4257 case MLX5_QP_STATE_SQER: return IB_QPS_SQE;
4258 case MLX5_QP_STATE_ERR: return IB_QPS_ERR;
4259 default: return -1;
4260 }
4261}
4262
4263static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
4264{
4265 switch (mlx5_mig_state) {
4266 case MLX5_QP_PM_ARMED: return IB_MIG_ARMED;
4267 case MLX5_QP_PM_REARM: return IB_MIG_REARM;
4268 case MLX5_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
4269 default: return -1;
4270 }
4271}
4272
4273static int to_ib_qp_access_flags(int mlx5_flags)
4274{
4275 int ib_flags = 0;
4276
4277 if (mlx5_flags & MLX5_QP_BIT_RRE)
4278 ib_flags |= IB_ACCESS_REMOTE_READ;
4279 if (mlx5_flags & MLX5_QP_BIT_RWE)
4280 ib_flags |= IB_ACCESS_REMOTE_WRITE;
4281 if (mlx5_flags & MLX5_QP_BIT_RAE)
4282 ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
4283
4284 return ib_flags;
4285}
4286
4287static void to_ib_ah_attr(struct mlx5_ib_dev *ibdev, struct ib_ah_attr *ib_ah_attr,
4288 struct mlx5_qp_path *path)
4289{
Jack Morgenstein9603b612014-07-28 23:30:22 +03004290 struct mlx5_core_dev *dev = ibdev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004291
4292 memset(ib_ah_attr, 0, sizeof(*ib_ah_attr));
4293 ib_ah_attr->port_num = path->port;
4294
Eli Cohenc7a08ac2014-10-02 12:19:42 +03004295 if (ib_ah_attr->port_num == 0 ||
Saeed Mahameed938fe832015-05-28 22:28:41 +03004296 ib_ah_attr->port_num > MLX5_CAP_GEN(dev, num_ports))
Eli Cohene126ba92013-07-07 17:25:49 +03004297 return;
4298
Achiad Shochat2811ba52015-12-23 18:47:24 +02004299 ib_ah_attr->sl = path->dci_cfi_prio_sl & 0xf;
Eli Cohene126ba92013-07-07 17:25:49 +03004300
4301 ib_ah_attr->dlid = be16_to_cpu(path->rlid);
4302 ib_ah_attr->src_path_bits = path->grh_mlid & 0x7f;
4303 ib_ah_attr->static_rate = path->static_rate ? path->static_rate - 5 : 0;
4304 ib_ah_attr->ah_flags = (path->grh_mlid & (1 << 7)) ? IB_AH_GRH : 0;
4305 if (ib_ah_attr->ah_flags) {
4306 ib_ah_attr->grh.sgid_index = path->mgid_index;
4307 ib_ah_attr->grh.hop_limit = path->hop_limit;
4308 ib_ah_attr->grh.traffic_class =
4309 (be32_to_cpu(path->tclass_flowlabel) >> 20) & 0xff;
4310 ib_ah_attr->grh.flow_label =
4311 be32_to_cpu(path->tclass_flowlabel) & 0xfffff;
4312 memcpy(ib_ah_attr->grh.dgid.raw,
4313 path->rgid, sizeof(ib_ah_attr->grh.dgid.raw));
4314 }
4315}
4316
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004317static int query_raw_packet_qp_sq_state(struct mlx5_ib_dev *dev,
4318 struct mlx5_ib_sq *sq,
4319 u8 *sq_state)
Eli Cohene126ba92013-07-07 17:25:49 +03004320{
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004321 void *out;
4322 void *sqc;
4323 int inlen;
4324 int err;
4325
4326 inlen = MLX5_ST_SZ_BYTES(query_sq_out);
4327 out = mlx5_vzalloc(inlen);
4328 if (!out)
4329 return -ENOMEM;
4330
4331 err = mlx5_core_query_sq(dev->mdev, sq->base.mqp.qpn, out);
4332 if (err)
4333 goto out;
4334
4335 sqc = MLX5_ADDR_OF(query_sq_out, out, sq_context);
4336 *sq_state = MLX5_GET(sqc, sqc, state);
4337 sq->state = *sq_state;
4338
4339out:
4340 kvfree(out);
4341 return err;
4342}
4343
4344static int query_raw_packet_qp_rq_state(struct mlx5_ib_dev *dev,
4345 struct mlx5_ib_rq *rq,
4346 u8 *rq_state)
4347{
4348 void *out;
4349 void *rqc;
4350 int inlen;
4351 int err;
4352
4353 inlen = MLX5_ST_SZ_BYTES(query_rq_out);
4354 out = mlx5_vzalloc(inlen);
4355 if (!out)
4356 return -ENOMEM;
4357
4358 err = mlx5_core_query_rq(dev->mdev, rq->base.mqp.qpn, out);
4359 if (err)
4360 goto out;
4361
4362 rqc = MLX5_ADDR_OF(query_rq_out, out, rq_context);
4363 *rq_state = MLX5_GET(rqc, rqc, state);
4364 rq->state = *rq_state;
4365
4366out:
4367 kvfree(out);
4368 return err;
4369}
4370
4371static int sqrq_state_to_qp_state(u8 sq_state, u8 rq_state,
4372 struct mlx5_ib_qp *qp, u8 *qp_state)
4373{
4374 static const u8 sqrq_trans[MLX5_RQ_NUM_STATE][MLX5_SQ_NUM_STATE] = {
4375 [MLX5_RQC_STATE_RST] = {
4376 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
4377 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
4378 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE_BAD,
4379 [MLX5_SQ_STATE_NA] = IB_QPS_RESET,
4380 },
4381 [MLX5_RQC_STATE_RDY] = {
4382 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
4383 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
4384 [MLX5_SQC_STATE_ERR] = IB_QPS_SQE,
4385 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE,
4386 },
4387 [MLX5_RQC_STATE_ERR] = {
4388 [MLX5_SQC_STATE_RST] = MLX5_QP_STATE_BAD,
4389 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE_BAD,
4390 [MLX5_SQC_STATE_ERR] = IB_QPS_ERR,
4391 [MLX5_SQ_STATE_NA] = IB_QPS_ERR,
4392 },
4393 [MLX5_RQ_STATE_NA] = {
4394 [MLX5_SQC_STATE_RST] = IB_QPS_RESET,
4395 [MLX5_SQC_STATE_RDY] = MLX5_QP_STATE,
4396 [MLX5_SQC_STATE_ERR] = MLX5_QP_STATE,
4397 [MLX5_SQ_STATE_NA] = MLX5_QP_STATE_BAD,
4398 },
4399 };
4400
4401 *qp_state = sqrq_trans[rq_state][sq_state];
4402
4403 if (*qp_state == MLX5_QP_STATE_BAD) {
4404 WARN(1, "Buggy Raw Packet QP state, SQ 0x%x state: 0x%x, RQ 0x%x state: 0x%x",
4405 qp->raw_packet_qp.sq.base.mqp.qpn, sq_state,
4406 qp->raw_packet_qp.rq.base.mqp.qpn, rq_state);
4407 return -EINVAL;
4408 }
4409
4410 if (*qp_state == MLX5_QP_STATE)
4411 *qp_state = qp->state;
4412
4413 return 0;
4414}
4415
4416static int query_raw_packet_qp_state(struct mlx5_ib_dev *dev,
4417 struct mlx5_ib_qp *qp,
4418 u8 *raw_packet_qp_state)
4419{
4420 struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
4421 struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
4422 struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
4423 int err;
4424 u8 sq_state = MLX5_SQ_STATE_NA;
4425 u8 rq_state = MLX5_RQ_STATE_NA;
4426
4427 if (qp->sq.wqe_cnt) {
4428 err = query_raw_packet_qp_sq_state(dev, sq, &sq_state);
4429 if (err)
4430 return err;
4431 }
4432
4433 if (qp->rq.wqe_cnt) {
4434 err = query_raw_packet_qp_rq_state(dev, rq, &rq_state);
4435 if (err)
4436 return err;
4437 }
4438
4439 return sqrq_state_to_qp_state(sq_state, rq_state, qp,
4440 raw_packet_qp_state);
4441}
4442
4443static int query_qp_attr(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
4444 struct ib_qp_attr *qp_attr)
4445{
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004446 int outlen = MLX5_ST_SZ_BYTES(query_qp_out);
Eli Cohene126ba92013-07-07 17:25:49 +03004447 struct mlx5_qp_context *context;
4448 int mlx5_state;
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004449 u32 *outb;
Eli Cohene126ba92013-07-07 17:25:49 +03004450 int err = 0;
4451
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004452 outb = kzalloc(outlen, GFP_KERNEL);
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004453 if (!outb)
4454 return -ENOMEM;
4455
majd@mellanox.com19098df2016-01-14 19:13:03 +02004456 err = mlx5_core_qp_query(dev->mdev, &qp->trans_qp.base.mqp, outb,
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004457 outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03004458 if (err)
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004459 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004460
Saeed Mahameed09a7d9e2016-07-19 01:17:59 +03004461 /* FIXME: use MLX5_GET rather than mlx5_qp_context manual struct */
4462 context = (struct mlx5_qp_context *)MLX5_ADDR_OF(query_qp_out, outb, qpc);
4463
Eli Cohene126ba92013-07-07 17:25:49 +03004464 mlx5_state = be32_to_cpu(context->flags) >> 28;
4465
4466 qp->state = to_ib_qp_state(mlx5_state);
Eli Cohene126ba92013-07-07 17:25:49 +03004467 qp_attr->path_mtu = context->mtu_msgmax >> 5;
4468 qp_attr->path_mig_state =
4469 to_ib_mig_state((be32_to_cpu(context->flags) >> 11) & 0x3);
4470 qp_attr->qkey = be32_to_cpu(context->qkey);
4471 qp_attr->rq_psn = be32_to_cpu(context->rnr_nextrecvpsn) & 0xffffff;
4472 qp_attr->sq_psn = be32_to_cpu(context->next_send_psn) & 0xffffff;
4473 qp_attr->dest_qp_num = be32_to_cpu(context->log_pg_sz_remote_qpn) & 0xffffff;
4474 qp_attr->qp_access_flags =
4475 to_ib_qp_access_flags(be32_to_cpu(context->params2));
4476
4477 if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
4478 to_ib_ah_attr(dev, &qp_attr->ah_attr, &context->pri_path);
4479 to_ib_ah_attr(dev, &qp_attr->alt_ah_attr, &context->alt_path);
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03004480 qp_attr->alt_pkey_index =
4481 be16_to_cpu(context->alt_path.pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03004482 qp_attr->alt_port_num = qp_attr->alt_ah_attr.port_num;
4483 }
4484
Noa Osherovichd3ae2bd2016-06-04 15:15:36 +03004485 qp_attr->pkey_index = be16_to_cpu(context->pri_path.pkey_index);
Eli Cohene126ba92013-07-07 17:25:49 +03004486 qp_attr->port_num = context->pri_path.port;
4487
4488 /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
4489 qp_attr->sq_draining = mlx5_state == MLX5_QP_STATE_SQ_DRAINING;
4490
4491 qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context->params1) >> 21) & 0x7);
4492
4493 qp_attr->max_dest_rd_atomic =
4494 1 << ((be32_to_cpu(context->params2) >> 21) & 0x7);
4495 qp_attr->min_rnr_timer =
4496 (be32_to_cpu(context->rnr_nextrecvpsn) >> 24) & 0x1f;
4497 qp_attr->timeout = context->pri_path.ackto_lt >> 3;
4498 qp_attr->retry_cnt = (be32_to_cpu(context->params1) >> 16) & 0x7;
4499 qp_attr->rnr_retry = (be32_to_cpu(context->params1) >> 13) & 0x7;
4500 qp_attr->alt_timeout = context->alt_path.ackto_lt >> 3;
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004501
4502out:
4503 kfree(outb);
4504 return err;
4505}
4506
4507int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
4508 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
4509{
4510 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4511 struct mlx5_ib_qp *qp = to_mqp(ibqp);
4512 int err = 0;
4513 u8 raw_packet_qp_state;
4514
Yishai Hadas28d61372016-05-23 15:20:56 +03004515 if (ibqp->rwq_ind_tbl)
4516 return -ENOSYS;
4517
Haggai Erand16e91d2016-02-29 15:45:05 +02004518 if (unlikely(ibqp->qp_type == IB_QPT_GSI))
4519 return mlx5_ib_gsi_query_qp(ibqp, qp_attr, qp_attr_mask,
4520 qp_init_attr);
4521
majd@mellanox.com6d2f89df2016-01-14 19:13:05 +02004522 mutex_lock(&qp->mutex);
4523
4524 if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET) {
4525 err = query_raw_packet_qp_state(dev, qp, &raw_packet_qp_state);
4526 if (err)
4527 goto out;
4528 qp->state = raw_packet_qp_state;
4529 qp_attr->port_num = 1;
4530 } else {
4531 err = query_qp_attr(dev, qp, qp_attr);
4532 if (err)
4533 goto out;
4534 }
4535
4536 qp_attr->qp_state = qp->state;
Eli Cohene126ba92013-07-07 17:25:49 +03004537 qp_attr->cur_qp_state = qp_attr->qp_state;
4538 qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
4539 qp_attr->cap.max_recv_sge = qp->rq.max_gs;
4540
4541 if (!ibqp->uobject) {
Noa Osherovich0540d812016-06-04 15:15:32 +03004542 qp_attr->cap.max_send_wr = qp->sq.max_post;
Eli Cohene126ba92013-07-07 17:25:49 +03004543 qp_attr->cap.max_send_sge = qp->sq.max_gs;
Noa Osherovich0540d812016-06-04 15:15:32 +03004544 qp_init_attr->qp_context = ibqp->qp_context;
Eli Cohene126ba92013-07-07 17:25:49 +03004545 } else {
4546 qp_attr->cap.max_send_wr = 0;
4547 qp_attr->cap.max_send_sge = 0;
4548 }
4549
Noa Osherovich0540d812016-06-04 15:15:32 +03004550 qp_init_attr->qp_type = ibqp->qp_type;
4551 qp_init_attr->recv_cq = ibqp->recv_cq;
4552 qp_init_attr->send_cq = ibqp->send_cq;
4553 qp_init_attr->srq = ibqp->srq;
4554 qp_attr->cap.max_inline_data = qp->max_inline_data;
Eli Cohene126ba92013-07-07 17:25:49 +03004555
4556 qp_init_attr->cap = qp_attr->cap;
4557
4558 qp_init_attr->create_flags = 0;
4559 if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
4560 qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
4561
Leon Romanovsky051f2632015-12-20 12:16:11 +02004562 if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
4563 qp_init_attr->create_flags |= IB_QP_CREATE_CROSS_CHANNEL;
4564 if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
4565 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_SEND;
4566 if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
4567 qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_RECV;
Haggai Eranb11a4f92016-02-29 15:45:03 +02004568 if (qp->flags & MLX5_IB_QP_SQPN_QP1)
4569 qp_init_attr->create_flags |= mlx5_ib_create_qp_sqpn_qp1();
Leon Romanovsky051f2632015-12-20 12:16:11 +02004570
Eli Cohene126ba92013-07-07 17:25:49 +03004571 qp_init_attr->sq_sig_type = qp->sq_signal_bits & MLX5_WQE_CTRL_CQ_UPDATE ?
4572 IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
4573
Eli Cohene126ba92013-07-07 17:25:49 +03004574out:
4575 mutex_unlock(&qp->mutex);
4576 return err;
4577}
4578
4579struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
4580 struct ib_ucontext *context,
4581 struct ib_udata *udata)
4582{
4583 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4584 struct mlx5_ib_xrcd *xrcd;
4585 int err;
4586
Saeed Mahameed938fe832015-05-28 22:28:41 +03004587 if (!MLX5_CAP_GEN(dev->mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +03004588 return ERR_PTR(-ENOSYS);
4589
4590 xrcd = kmalloc(sizeof(*xrcd), GFP_KERNEL);
4591 if (!xrcd)
4592 return ERR_PTR(-ENOMEM);
4593
Jack Morgenstein9603b612014-07-28 23:30:22 +03004594 err = mlx5_core_xrcd_alloc(dev->mdev, &xrcd->xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03004595 if (err) {
4596 kfree(xrcd);
4597 return ERR_PTR(-ENOMEM);
4598 }
4599
4600 return &xrcd->ibxrcd;
4601}
4602
4603int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
4604{
4605 struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
4606 u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
4607 int err;
4608
Jack Morgenstein9603b612014-07-28 23:30:22 +03004609 err = mlx5_core_xrcd_dealloc(dev->mdev, xrcdn);
Eli Cohene126ba92013-07-07 17:25:49 +03004610 if (err) {
4611 mlx5_ib_warn(dev, "failed to dealloc xrcdn 0x%x\n", xrcdn);
4612 return err;
4613 }
4614
4615 kfree(xrcd);
4616
4617 return 0;
4618}
Yishai Hadas79b20a62016-05-23 15:20:50 +03004619
Yishai Hadas350d0e42016-08-28 14:58:18 +03004620static void mlx5_ib_wq_event(struct mlx5_core_qp *core_qp, int type)
4621{
4622 struct mlx5_ib_rwq *rwq = to_mibrwq(core_qp);
4623 struct mlx5_ib_dev *dev = to_mdev(rwq->ibwq.device);
4624 struct ib_event event;
4625
4626 if (rwq->ibwq.event_handler) {
4627 event.device = rwq->ibwq.device;
4628 event.element.wq = &rwq->ibwq;
4629 switch (type) {
4630 case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
4631 event.event = IB_EVENT_WQ_FATAL;
4632 break;
4633 default:
4634 mlx5_ib_warn(dev, "Unexpected event type %d on WQ %06x\n", type, core_qp->qpn);
4635 return;
4636 }
4637
4638 rwq->ibwq.event_handler(&event, rwq->ibwq.wq_context);
4639 }
4640}
4641
Yishai Hadas79b20a62016-05-23 15:20:50 +03004642static int create_rq(struct mlx5_ib_rwq *rwq, struct ib_pd *pd,
4643 struct ib_wq_init_attr *init_attr)
4644{
4645 struct mlx5_ib_dev *dev;
4646 __be64 *rq_pas0;
4647 void *in;
4648 void *rqc;
4649 void *wq;
4650 int inlen;
4651 int err;
4652
4653 dev = to_mdev(pd->device);
4654
4655 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + sizeof(u64) * rwq->rq_num_pas;
4656 in = mlx5_vzalloc(inlen);
4657 if (!in)
4658 return -ENOMEM;
4659
4660 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
4661 MLX5_SET(rqc, rqc, mem_rq_type,
4662 MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
4663 MLX5_SET(rqc, rqc, user_index, rwq->user_index);
4664 MLX5_SET(rqc, rqc, cqn, to_mcq(init_attr->cq)->mcq.cqn);
4665 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
4666 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
4667 wq = MLX5_ADDR_OF(rqc, rqc, wq);
4668 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
4669 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
4670 MLX5_SET(wq, wq, log_wq_stride, rwq->log_rq_stride);
4671 MLX5_SET(wq, wq, log_wq_sz, rwq->log_rq_size);
4672 MLX5_SET(wq, wq, pd, to_mpd(pd)->pdn);
4673 MLX5_SET(wq, wq, page_offset, rwq->rq_page_offset);
4674 MLX5_SET(wq, wq, log_wq_pg_sz, rwq->log_page_size);
4675 MLX5_SET(wq, wq, wq_signature, rwq->wq_sig);
4676 MLX5_SET64(wq, wq, dbr_addr, rwq->db.dma);
4677 rq_pas0 = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
4678 mlx5_ib_populate_pas(dev, rwq->umem, rwq->page_shift, rq_pas0, 0);
Yishai Hadas350d0e42016-08-28 14:58:18 +03004679 err = mlx5_core_create_rq_tracked(dev->mdev, in, inlen, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004680 kvfree(in);
4681 return err;
4682}
4683
4684static int set_user_rq_size(struct mlx5_ib_dev *dev,
4685 struct ib_wq_init_attr *wq_init_attr,
4686 struct mlx5_ib_create_wq *ucmd,
4687 struct mlx5_ib_rwq *rwq)
4688{
4689 /* Sanity check RQ size before proceeding */
4690 if (wq_init_attr->max_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_wq_sz)))
4691 return -EINVAL;
4692
4693 if (!ucmd->rq_wqe_count)
4694 return -EINVAL;
4695
4696 rwq->wqe_count = ucmd->rq_wqe_count;
4697 rwq->wqe_shift = ucmd->rq_wqe_shift;
4698 rwq->buf_size = (rwq->wqe_count << rwq->wqe_shift);
4699 rwq->log_rq_stride = rwq->wqe_shift;
4700 rwq->log_rq_size = ilog2(rwq->wqe_count);
4701 return 0;
4702}
4703
4704static int prepare_user_rq(struct ib_pd *pd,
4705 struct ib_wq_init_attr *init_attr,
4706 struct ib_udata *udata,
4707 struct mlx5_ib_rwq *rwq)
4708{
4709 struct mlx5_ib_dev *dev = to_mdev(pd->device);
4710 struct mlx5_ib_create_wq ucmd = {};
4711 int err;
4712 size_t required_cmd_sz;
4713
4714 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
4715 if (udata->inlen < required_cmd_sz) {
4716 mlx5_ib_dbg(dev, "invalid inlen\n");
4717 return -EINVAL;
4718 }
4719
4720 if (udata->inlen > sizeof(ucmd) &&
4721 !ib_is_udata_cleared(udata, sizeof(ucmd),
4722 udata->inlen - sizeof(ucmd))) {
4723 mlx5_ib_dbg(dev, "inlen is not supported\n");
4724 return -EOPNOTSUPP;
4725 }
4726
4727 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
4728 mlx5_ib_dbg(dev, "copy failed\n");
4729 return -EFAULT;
4730 }
4731
4732 if (ucmd.comp_mask) {
4733 mlx5_ib_dbg(dev, "invalid comp mask\n");
4734 return -EOPNOTSUPP;
4735 }
4736
4737 if (ucmd.reserved) {
4738 mlx5_ib_dbg(dev, "invalid reserved\n");
4739 return -EOPNOTSUPP;
4740 }
4741
4742 err = set_user_rq_size(dev, init_attr, &ucmd, rwq);
4743 if (err) {
4744 mlx5_ib_dbg(dev, "err %d\n", err);
4745 return err;
4746 }
4747
4748 err = create_user_rq(dev, pd, rwq, &ucmd);
4749 if (err) {
4750 mlx5_ib_dbg(dev, "err %d\n", err);
4751 if (err)
4752 return err;
4753 }
4754
4755 rwq->user_index = ucmd.user_index;
4756 return 0;
4757}
4758
4759struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
4760 struct ib_wq_init_attr *init_attr,
4761 struct ib_udata *udata)
4762{
4763 struct mlx5_ib_dev *dev;
4764 struct mlx5_ib_rwq *rwq;
4765 struct mlx5_ib_create_wq_resp resp = {};
4766 size_t min_resp_len;
4767 int err;
4768
4769 if (!udata)
4770 return ERR_PTR(-ENOSYS);
4771
4772 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
4773 if (udata->outlen && udata->outlen < min_resp_len)
4774 return ERR_PTR(-EINVAL);
4775
4776 dev = to_mdev(pd->device);
4777 switch (init_attr->wq_type) {
4778 case IB_WQT_RQ:
4779 rwq = kzalloc(sizeof(*rwq), GFP_KERNEL);
4780 if (!rwq)
4781 return ERR_PTR(-ENOMEM);
4782 err = prepare_user_rq(pd, init_attr, udata, rwq);
4783 if (err)
4784 goto err;
4785 err = create_rq(rwq, pd, init_attr);
4786 if (err)
4787 goto err_user_rq;
4788 break;
4789 default:
4790 mlx5_ib_dbg(dev, "unsupported wq type %d\n",
4791 init_attr->wq_type);
4792 return ERR_PTR(-EINVAL);
4793 }
4794
Yishai Hadas350d0e42016-08-28 14:58:18 +03004795 rwq->ibwq.wq_num = rwq->core_qp.qpn;
Yishai Hadas79b20a62016-05-23 15:20:50 +03004796 rwq->ibwq.state = IB_WQS_RESET;
4797 if (udata->outlen) {
4798 resp.response_length = offsetof(typeof(resp), response_length) +
4799 sizeof(resp.response_length);
4800 err = ib_copy_to_udata(udata, &resp, resp.response_length);
4801 if (err)
4802 goto err_copy;
4803 }
4804
Yishai Hadas350d0e42016-08-28 14:58:18 +03004805 rwq->core_qp.event = mlx5_ib_wq_event;
4806 rwq->ibwq.event_handler = init_attr->event_handler;
Yishai Hadas79b20a62016-05-23 15:20:50 +03004807 return &rwq->ibwq;
4808
4809err_copy:
Yishai Hadas350d0e42016-08-28 14:58:18 +03004810 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004811err_user_rq:
4812 destroy_user_rq(pd, rwq);
4813err:
4814 kfree(rwq);
4815 return ERR_PTR(err);
4816}
4817
4818int mlx5_ib_destroy_wq(struct ib_wq *wq)
4819{
4820 struct mlx5_ib_dev *dev = to_mdev(wq->device);
4821 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
4822
Yishai Hadas350d0e42016-08-28 14:58:18 +03004823 mlx5_core_destroy_rq_tracked(dev->mdev, &rwq->core_qp);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004824 destroy_user_rq(wq->pd, rwq);
4825 kfree(rwq);
4826
4827 return 0;
4828}
4829
Yishai Hadasc5f90922016-05-23 15:20:53 +03004830struct ib_rwq_ind_table *mlx5_ib_create_rwq_ind_table(struct ib_device *device,
4831 struct ib_rwq_ind_table_init_attr *init_attr,
4832 struct ib_udata *udata)
4833{
4834 struct mlx5_ib_dev *dev = to_mdev(device);
4835 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl;
4836 int sz = 1 << init_attr->log_ind_tbl_size;
4837 struct mlx5_ib_create_rwq_ind_tbl_resp resp = {};
4838 size_t min_resp_len;
4839 int inlen;
4840 int err;
4841 int i;
4842 u32 *in;
4843 void *rqtc;
4844
4845 if (udata->inlen > 0 &&
4846 !ib_is_udata_cleared(udata, 0,
4847 udata->inlen))
4848 return ERR_PTR(-EOPNOTSUPP);
4849
Maor Gottliebefd7f402016-10-27 16:36:40 +03004850 if (init_attr->log_ind_tbl_size >
4851 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size)) {
4852 mlx5_ib_dbg(dev, "log_ind_tbl_size = %d is bigger than supported = %d\n",
4853 init_attr->log_ind_tbl_size,
4854 MLX5_CAP_GEN(dev->mdev, log_max_rqt_size));
4855 return ERR_PTR(-EINVAL);
4856 }
4857
Yishai Hadasc5f90922016-05-23 15:20:53 +03004858 min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
4859 if (udata->outlen && udata->outlen < min_resp_len)
4860 return ERR_PTR(-EINVAL);
4861
4862 rwq_ind_tbl = kzalloc(sizeof(*rwq_ind_tbl), GFP_KERNEL);
4863 if (!rwq_ind_tbl)
4864 return ERR_PTR(-ENOMEM);
4865
4866 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
4867 in = mlx5_vzalloc(inlen);
4868 if (!in) {
4869 err = -ENOMEM;
4870 goto err;
4871 }
4872
4873 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
4874
4875 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
4876 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
4877
4878 for (i = 0; i < sz; i++)
4879 MLX5_SET(rqtc, rqtc, rq_num[i], init_attr->ind_tbl[i]->wq_num);
4880
4881 err = mlx5_core_create_rqt(dev->mdev, in, inlen, &rwq_ind_tbl->rqtn);
4882 kvfree(in);
4883
4884 if (err)
4885 goto err;
4886
4887 rwq_ind_tbl->ib_rwq_ind_tbl.ind_tbl_num = rwq_ind_tbl->rqtn;
4888 if (udata->outlen) {
4889 resp.response_length = offsetof(typeof(resp), response_length) +
4890 sizeof(resp.response_length);
4891 err = ib_copy_to_udata(udata, &resp, resp.response_length);
4892 if (err)
4893 goto err_copy;
4894 }
4895
4896 return &rwq_ind_tbl->ib_rwq_ind_tbl;
4897
4898err_copy:
4899 mlx5_core_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn);
4900err:
4901 kfree(rwq_ind_tbl);
4902 return ERR_PTR(err);
4903}
4904
4905int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
4906{
4907 struct mlx5_ib_rwq_ind_table *rwq_ind_tbl = to_mrwq_ind_table(ib_rwq_ind_tbl);
4908 struct mlx5_ib_dev *dev = to_mdev(ib_rwq_ind_tbl->device);
4909
4910 mlx5_core_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn);
4911
4912 kfree(rwq_ind_tbl);
4913 return 0;
4914}
4915
Yishai Hadas79b20a62016-05-23 15:20:50 +03004916int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
4917 u32 wq_attr_mask, struct ib_udata *udata)
4918{
4919 struct mlx5_ib_dev *dev = to_mdev(wq->device);
4920 struct mlx5_ib_rwq *rwq = to_mrwq(wq);
4921 struct mlx5_ib_modify_wq ucmd = {};
4922 size_t required_cmd_sz;
4923 int curr_wq_state;
4924 int wq_state;
4925 int inlen;
4926 int err;
4927 void *rqc;
4928 void *in;
4929
4930 required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
4931 if (udata->inlen < required_cmd_sz)
4932 return -EINVAL;
4933
4934 if (udata->inlen > sizeof(ucmd) &&
4935 !ib_is_udata_cleared(udata, sizeof(ucmd),
4936 udata->inlen - sizeof(ucmd)))
4937 return -EOPNOTSUPP;
4938
4939 if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
4940 return -EFAULT;
4941
4942 if (ucmd.comp_mask || ucmd.reserved)
4943 return -EOPNOTSUPP;
4944
4945 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
4946 in = mlx5_vzalloc(inlen);
4947 if (!in)
4948 return -ENOMEM;
4949
4950 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
4951
4952 curr_wq_state = (wq_attr_mask & IB_WQ_CUR_STATE) ?
4953 wq_attr->curr_wq_state : wq->state;
4954 wq_state = (wq_attr_mask & IB_WQ_STATE) ?
4955 wq_attr->wq_state : curr_wq_state;
4956 if (curr_wq_state == IB_WQS_ERR)
4957 curr_wq_state = MLX5_RQC_STATE_ERR;
4958 if (wq_state == IB_WQS_ERR)
4959 wq_state = MLX5_RQC_STATE_ERR;
4960 MLX5_SET(modify_rq_in, in, rq_state, curr_wq_state);
4961 MLX5_SET(rqc, rqc, state, wq_state);
4962
Yishai Hadas350d0e42016-08-28 14:58:18 +03004963 err = mlx5_core_modify_rq(dev->mdev, rwq->core_qp.qpn, in, inlen);
Yishai Hadas79b20a62016-05-23 15:20:50 +03004964 kvfree(in);
4965 if (!err)
4966 rwq->ibwq.state = (wq_state == MLX5_RQC_STATE_ERR) ? IB_WQS_ERR : wq_state;
4967
4968 return err;
4969}