xref: /openbmc/linux/drivers/infiniband/hw/mlx5/qp.c (revision cfb5e088)
1e126ba97SEli Cohen /*
26cf0a15fSSaeed Mahameed  * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
3e126ba97SEli Cohen  *
4e126ba97SEli Cohen  * This software is available to you under a choice of one of two
5e126ba97SEli Cohen  * licenses.  You may choose to be licensed under the terms of the GNU
6e126ba97SEli Cohen  * General Public License (GPL) Version 2, available from the file
7e126ba97SEli Cohen  * COPYING in the main directory of this source tree, or the
8e126ba97SEli Cohen  * OpenIB.org BSD license below:
9e126ba97SEli Cohen  *
10e126ba97SEli Cohen  *     Redistribution and use in source and binary forms, with or
11e126ba97SEli Cohen  *     without modification, are permitted provided that the following
12e126ba97SEli Cohen  *     conditions are met:
13e126ba97SEli Cohen  *
14e126ba97SEli Cohen  *      - Redistributions of source code must retain the above
15e126ba97SEli Cohen  *        copyright notice, this list of conditions and the following
16e126ba97SEli Cohen  *        disclaimer.
17e126ba97SEli Cohen  *
18e126ba97SEli Cohen  *      - Redistributions in binary form must reproduce the above
19e126ba97SEli Cohen  *        copyright notice, this list of conditions and the following
20e126ba97SEli Cohen  *        disclaimer in the documentation and/or other materials
21e126ba97SEli Cohen  *        provided with the distribution.
22e126ba97SEli Cohen  *
23e126ba97SEli Cohen  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24e126ba97SEli Cohen  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25e126ba97SEli Cohen  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26e126ba97SEli Cohen  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27e126ba97SEli Cohen  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28e126ba97SEli Cohen  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29e126ba97SEli Cohen  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30e126ba97SEli Cohen  * SOFTWARE.
31e126ba97SEli Cohen  */
32e126ba97SEli Cohen 
33e126ba97SEli Cohen #include <linux/module.h>
34e126ba97SEli Cohen #include <rdma/ib_umem.h>
352811ba51SAchiad Shochat #include <rdma/ib_cache.h>
36cfb5e088SHaggai Abramovsky #include <rdma/ib_user_verbs.h>
37e126ba97SEli Cohen #include "mlx5_ib.h"
38e126ba97SEli Cohen #include "user.h"
39e126ba97SEli Cohen 
40e126ba97SEli Cohen /* not supported currently */
41e126ba97SEli Cohen static int wq_signature;
42e126ba97SEli Cohen 
43e126ba97SEli Cohen enum {
44e126ba97SEli Cohen 	MLX5_IB_ACK_REQ_FREQ	= 8,
45e126ba97SEli Cohen };
46e126ba97SEli Cohen 
47e126ba97SEli Cohen enum {
48e126ba97SEli Cohen 	MLX5_IB_DEFAULT_SCHED_QUEUE	= 0x83,
49e126ba97SEli Cohen 	MLX5_IB_DEFAULT_QP0_SCHED_QUEUE	= 0x3f,
50e126ba97SEli Cohen 	MLX5_IB_LINK_TYPE_IB		= 0,
51e126ba97SEli Cohen 	MLX5_IB_LINK_TYPE_ETH		= 1
52e126ba97SEli Cohen };
53e126ba97SEli Cohen 
54e126ba97SEli Cohen enum {
55e126ba97SEli Cohen 	MLX5_IB_SQ_STRIDE	= 6,
56e126ba97SEli Cohen 	MLX5_IB_CACHE_LINE_SIZE	= 64,
57e126ba97SEli Cohen };
58e126ba97SEli Cohen 
59e126ba97SEli Cohen static const u32 mlx5_ib_opcode[] = {
60e126ba97SEli Cohen 	[IB_WR_SEND]				= MLX5_OPCODE_SEND,
61e126ba97SEli Cohen 	[IB_WR_SEND_WITH_IMM]			= MLX5_OPCODE_SEND_IMM,
62e126ba97SEli Cohen 	[IB_WR_RDMA_WRITE]			= MLX5_OPCODE_RDMA_WRITE,
63e126ba97SEli Cohen 	[IB_WR_RDMA_WRITE_WITH_IMM]		= MLX5_OPCODE_RDMA_WRITE_IMM,
64e126ba97SEli Cohen 	[IB_WR_RDMA_READ]			= MLX5_OPCODE_RDMA_READ,
65e126ba97SEli Cohen 	[IB_WR_ATOMIC_CMP_AND_SWP]		= MLX5_OPCODE_ATOMIC_CS,
66e126ba97SEli Cohen 	[IB_WR_ATOMIC_FETCH_AND_ADD]		= MLX5_OPCODE_ATOMIC_FA,
67e126ba97SEli Cohen 	[IB_WR_SEND_WITH_INV]			= MLX5_OPCODE_SEND_INVAL,
68e126ba97SEli Cohen 	[IB_WR_LOCAL_INV]			= MLX5_OPCODE_UMR,
698a187ee5SSagi Grimberg 	[IB_WR_REG_MR]				= MLX5_OPCODE_UMR,
70e126ba97SEli Cohen 	[IB_WR_MASKED_ATOMIC_CMP_AND_SWP]	= MLX5_OPCODE_ATOMIC_MASKED_CS,
71e126ba97SEli Cohen 	[IB_WR_MASKED_ATOMIC_FETCH_AND_ADD]	= MLX5_OPCODE_ATOMIC_MASKED_FA,
72e126ba97SEli Cohen 	[MLX5_IB_WR_UMR]			= MLX5_OPCODE_UMR,
73e126ba97SEli Cohen };
74e126ba97SEli Cohen 
75e126ba97SEli Cohen 
76e126ba97SEli Cohen static int is_qp0(enum ib_qp_type qp_type)
77e126ba97SEli Cohen {
78e126ba97SEli Cohen 	return qp_type == IB_QPT_SMI;
79e126ba97SEli Cohen }
80e126ba97SEli Cohen 
81e126ba97SEli Cohen static int is_sqp(enum ib_qp_type qp_type)
82e126ba97SEli Cohen {
83e126ba97SEli Cohen 	return is_qp0(qp_type) || is_qp1(qp_type);
84e126ba97SEli Cohen }
85e126ba97SEli Cohen 
86e126ba97SEli Cohen static void *get_wqe(struct mlx5_ib_qp *qp, int offset)
87e126ba97SEli Cohen {
88e126ba97SEli Cohen 	return mlx5_buf_offset(&qp->buf, offset);
89e126ba97SEli Cohen }
90e126ba97SEli Cohen 
91e126ba97SEli Cohen static void *get_recv_wqe(struct mlx5_ib_qp *qp, int n)
92e126ba97SEli Cohen {
93e126ba97SEli Cohen 	return get_wqe(qp, qp->rq.offset + (n << qp->rq.wqe_shift));
94e126ba97SEli Cohen }
95e126ba97SEli Cohen 
96e126ba97SEli Cohen void *mlx5_get_send_wqe(struct mlx5_ib_qp *qp, int n)
97e126ba97SEli Cohen {
98e126ba97SEli Cohen 	return get_wqe(qp, qp->sq.offset + (n << MLX5_IB_SQ_STRIDE));
99e126ba97SEli Cohen }
100e126ba97SEli Cohen 
101c1395a2aSHaggai Eran /**
102c1395a2aSHaggai Eran  * mlx5_ib_read_user_wqe() - Copy a user-space WQE to kernel space.
103c1395a2aSHaggai Eran  *
104c1395a2aSHaggai Eran  * @qp: QP to copy from.
105c1395a2aSHaggai Eran  * @send: copy from the send queue when non-zero, use the receive queue
106c1395a2aSHaggai Eran  *	  otherwise.
107c1395a2aSHaggai Eran  * @wqe_index:  index to start copying from. For send work queues, the
108c1395a2aSHaggai Eran  *		wqe_index is in units of MLX5_SEND_WQE_BB.
109c1395a2aSHaggai Eran  *		For receive work queue, it is the number of work queue
110c1395a2aSHaggai Eran  *		element in the queue.
111c1395a2aSHaggai Eran  * @buffer: destination buffer.
112c1395a2aSHaggai Eran  * @length: maximum number of bytes to copy.
113c1395a2aSHaggai Eran  *
114c1395a2aSHaggai Eran  * Copies at least a single WQE, but may copy more data.
115c1395a2aSHaggai Eran  *
116c1395a2aSHaggai Eran  * Return: the number of bytes copied, or an error code.
117c1395a2aSHaggai Eran  */
118c1395a2aSHaggai Eran int mlx5_ib_read_user_wqe(struct mlx5_ib_qp *qp, int send, int wqe_index,
119c1395a2aSHaggai Eran 			  void *buffer, u32 length)
120c1395a2aSHaggai Eran {
121c1395a2aSHaggai Eran 	struct ib_device *ibdev = qp->ibqp.device;
122c1395a2aSHaggai Eran 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
123c1395a2aSHaggai Eran 	struct mlx5_ib_wq *wq = send ? &qp->sq : &qp->rq;
124c1395a2aSHaggai Eran 	size_t offset;
125c1395a2aSHaggai Eran 	size_t wq_end;
126c1395a2aSHaggai Eran 	struct ib_umem *umem = qp->umem;
127c1395a2aSHaggai Eran 	u32 first_copy_length;
128c1395a2aSHaggai Eran 	int wqe_length;
129c1395a2aSHaggai Eran 	int ret;
130c1395a2aSHaggai Eran 
131c1395a2aSHaggai Eran 	if (wq->wqe_cnt == 0) {
132c1395a2aSHaggai Eran 		mlx5_ib_dbg(dev, "mlx5_ib_read_user_wqe for a QP with wqe_cnt == 0. qp_type: 0x%x\n",
133c1395a2aSHaggai Eran 			    qp->ibqp.qp_type);
134c1395a2aSHaggai Eran 		return -EINVAL;
135c1395a2aSHaggai Eran 	}
136c1395a2aSHaggai Eran 
137c1395a2aSHaggai Eran 	offset = wq->offset + ((wqe_index % wq->wqe_cnt) << wq->wqe_shift);
138c1395a2aSHaggai Eran 	wq_end = wq->offset + (wq->wqe_cnt << wq->wqe_shift);
139c1395a2aSHaggai Eran 
140c1395a2aSHaggai Eran 	if (send && length < sizeof(struct mlx5_wqe_ctrl_seg))
141c1395a2aSHaggai Eran 		return -EINVAL;
142c1395a2aSHaggai Eran 
143c1395a2aSHaggai Eran 	if (offset > umem->length ||
144c1395a2aSHaggai Eran 	    (send && offset + sizeof(struct mlx5_wqe_ctrl_seg) > umem->length))
145c1395a2aSHaggai Eran 		return -EINVAL;
146c1395a2aSHaggai Eran 
147c1395a2aSHaggai Eran 	first_copy_length = min_t(u32, offset + length, wq_end) - offset;
148c1395a2aSHaggai Eran 	ret = ib_umem_copy_from(buffer, umem, offset, first_copy_length);
149c1395a2aSHaggai Eran 	if (ret)
150c1395a2aSHaggai Eran 		return ret;
151c1395a2aSHaggai Eran 
152c1395a2aSHaggai Eran 	if (send) {
153c1395a2aSHaggai Eran 		struct mlx5_wqe_ctrl_seg *ctrl = buffer;
154c1395a2aSHaggai Eran 		int ds = be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_DS_MASK;
155c1395a2aSHaggai Eran 
156c1395a2aSHaggai Eran 		wqe_length = ds * MLX5_WQE_DS_UNITS;
157c1395a2aSHaggai Eran 	} else {
158c1395a2aSHaggai Eran 		wqe_length = 1 << wq->wqe_shift;
159c1395a2aSHaggai Eran 	}
160c1395a2aSHaggai Eran 
161c1395a2aSHaggai Eran 	if (wqe_length <= first_copy_length)
162c1395a2aSHaggai Eran 		return first_copy_length;
163c1395a2aSHaggai Eran 
164c1395a2aSHaggai Eran 	ret = ib_umem_copy_from(buffer + first_copy_length, umem, wq->offset,
165c1395a2aSHaggai Eran 				wqe_length - first_copy_length);
166c1395a2aSHaggai Eran 	if (ret)
167c1395a2aSHaggai Eran 		return ret;
168c1395a2aSHaggai Eran 
169c1395a2aSHaggai Eran 	return wqe_length;
170c1395a2aSHaggai Eran }
171c1395a2aSHaggai Eran 
172e126ba97SEli Cohen static void mlx5_ib_qp_event(struct mlx5_core_qp *qp, int type)
173e126ba97SEli Cohen {
174e126ba97SEli Cohen 	struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
175e126ba97SEli Cohen 	struct ib_event event;
176e126ba97SEli Cohen 
177e126ba97SEli Cohen 	if (type == MLX5_EVENT_TYPE_PATH_MIG)
178e126ba97SEli Cohen 		to_mibqp(qp)->port = to_mibqp(qp)->alt_port;
179e126ba97SEli Cohen 
180e126ba97SEli Cohen 	if (ibqp->event_handler) {
181e126ba97SEli Cohen 		event.device     = ibqp->device;
182e126ba97SEli Cohen 		event.element.qp = ibqp;
183e126ba97SEli Cohen 		switch (type) {
184e126ba97SEli Cohen 		case MLX5_EVENT_TYPE_PATH_MIG:
185e126ba97SEli Cohen 			event.event = IB_EVENT_PATH_MIG;
186e126ba97SEli Cohen 			break;
187e126ba97SEli Cohen 		case MLX5_EVENT_TYPE_COMM_EST:
188e126ba97SEli Cohen 			event.event = IB_EVENT_COMM_EST;
189e126ba97SEli Cohen 			break;
190e126ba97SEli Cohen 		case MLX5_EVENT_TYPE_SQ_DRAINED:
191e126ba97SEli Cohen 			event.event = IB_EVENT_SQ_DRAINED;
192e126ba97SEli Cohen 			break;
193e126ba97SEli Cohen 		case MLX5_EVENT_TYPE_SRQ_LAST_WQE:
194e126ba97SEli Cohen 			event.event = IB_EVENT_QP_LAST_WQE_REACHED;
195e126ba97SEli Cohen 			break;
196e126ba97SEli Cohen 		case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
197e126ba97SEli Cohen 			event.event = IB_EVENT_QP_FATAL;
198e126ba97SEli Cohen 			break;
199e126ba97SEli Cohen 		case MLX5_EVENT_TYPE_PATH_MIG_FAILED:
200e126ba97SEli Cohen 			event.event = IB_EVENT_PATH_MIG_ERR;
201e126ba97SEli Cohen 			break;
202e126ba97SEli Cohen 		case MLX5_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
203e126ba97SEli Cohen 			event.event = IB_EVENT_QP_REQ_ERR;
204e126ba97SEli Cohen 			break;
205e126ba97SEli Cohen 		case MLX5_EVENT_TYPE_WQ_ACCESS_ERROR:
206e126ba97SEli Cohen 			event.event = IB_EVENT_QP_ACCESS_ERR;
207e126ba97SEli Cohen 			break;
208e126ba97SEli Cohen 		default:
209e126ba97SEli Cohen 			pr_warn("mlx5_ib: Unexpected event type %d on QP %06x\n", type, qp->qpn);
210e126ba97SEli Cohen 			return;
211e126ba97SEli Cohen 		}
212e126ba97SEli Cohen 
213e126ba97SEli Cohen 		ibqp->event_handler(&event, ibqp->qp_context);
214e126ba97SEli Cohen 	}
215e126ba97SEli Cohen }
216e126ba97SEli Cohen 
217e126ba97SEli Cohen static int set_rq_size(struct mlx5_ib_dev *dev, struct ib_qp_cap *cap,
218e126ba97SEli Cohen 		       int has_rq, struct mlx5_ib_qp *qp, struct mlx5_ib_create_qp *ucmd)
219e126ba97SEli Cohen {
220e126ba97SEli Cohen 	int wqe_size;
221e126ba97SEli Cohen 	int wq_size;
222e126ba97SEli Cohen 
223e126ba97SEli Cohen 	/* Sanity check RQ size before proceeding */
224938fe83cSSaeed Mahameed 	if (cap->max_recv_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz)))
225e126ba97SEli Cohen 		return -EINVAL;
226e126ba97SEli Cohen 
227e126ba97SEli Cohen 	if (!has_rq) {
228e126ba97SEli Cohen 		qp->rq.max_gs = 0;
229e126ba97SEli Cohen 		qp->rq.wqe_cnt = 0;
230e126ba97SEli Cohen 		qp->rq.wqe_shift = 0;
231e126ba97SEli Cohen 	} else {
232e126ba97SEli Cohen 		if (ucmd) {
233e126ba97SEli Cohen 			qp->rq.wqe_cnt = ucmd->rq_wqe_count;
234e126ba97SEli Cohen 			qp->rq.wqe_shift = ucmd->rq_wqe_shift;
235e126ba97SEli Cohen 			qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
236e126ba97SEli Cohen 			qp->rq.max_post = qp->rq.wqe_cnt;
237e126ba97SEli Cohen 		} else {
238e126ba97SEli Cohen 			wqe_size = qp->wq_sig ? sizeof(struct mlx5_wqe_signature_seg) : 0;
239e126ba97SEli Cohen 			wqe_size += cap->max_recv_sge * sizeof(struct mlx5_wqe_data_seg);
240e126ba97SEli Cohen 			wqe_size = roundup_pow_of_two(wqe_size);
241e126ba97SEli Cohen 			wq_size = roundup_pow_of_two(cap->max_recv_wr) * wqe_size;
242e126ba97SEli Cohen 			wq_size = max_t(int, wq_size, MLX5_SEND_WQE_BB);
243e126ba97SEli Cohen 			qp->rq.wqe_cnt = wq_size / wqe_size;
244938fe83cSSaeed Mahameed 			if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq)) {
245e126ba97SEli Cohen 				mlx5_ib_dbg(dev, "wqe_size %d, max %d\n",
246e126ba97SEli Cohen 					    wqe_size,
247938fe83cSSaeed Mahameed 					    MLX5_CAP_GEN(dev->mdev,
248938fe83cSSaeed Mahameed 							 max_wqe_sz_rq));
249e126ba97SEli Cohen 				return -EINVAL;
250e126ba97SEli Cohen 			}
251e126ba97SEli Cohen 			qp->rq.wqe_shift = ilog2(wqe_size);
252e126ba97SEli Cohen 			qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
253e126ba97SEli Cohen 			qp->rq.max_post = qp->rq.wqe_cnt;
254e126ba97SEli Cohen 		}
255e126ba97SEli Cohen 	}
256e126ba97SEli Cohen 
257e126ba97SEli Cohen 	return 0;
258e126ba97SEli Cohen }
259e126ba97SEli Cohen 
260e126ba97SEli Cohen static int sq_overhead(enum ib_qp_type qp_type)
261e126ba97SEli Cohen {
262618af384SAndi Shyti 	int size = 0;
263e126ba97SEli Cohen 
264e126ba97SEli Cohen 	switch (qp_type) {
265e126ba97SEli Cohen 	case IB_QPT_XRC_INI:
266b125a54bSEli Cohen 		size += sizeof(struct mlx5_wqe_xrc_seg);
267e126ba97SEli Cohen 		/* fall through */
268e126ba97SEli Cohen 	case IB_QPT_RC:
269e126ba97SEli Cohen 		size += sizeof(struct mlx5_wqe_ctrl_seg) +
270e126ba97SEli Cohen 			sizeof(struct mlx5_wqe_atomic_seg) +
271e126ba97SEli Cohen 			sizeof(struct mlx5_wqe_raddr_seg);
272e126ba97SEli Cohen 		break;
273e126ba97SEli Cohen 
274b125a54bSEli Cohen 	case IB_QPT_XRC_TGT:
275b125a54bSEli Cohen 		return 0;
276b125a54bSEli Cohen 
277e126ba97SEli Cohen 	case IB_QPT_UC:
278b125a54bSEli Cohen 		size += sizeof(struct mlx5_wqe_ctrl_seg) +
2799e65dc37SEli Cohen 			sizeof(struct mlx5_wqe_raddr_seg) +
2809e65dc37SEli Cohen 			sizeof(struct mlx5_wqe_umr_ctrl_seg) +
2819e65dc37SEli Cohen 			sizeof(struct mlx5_mkey_seg);
282e126ba97SEli Cohen 		break;
283e126ba97SEli Cohen 
284e126ba97SEli Cohen 	case IB_QPT_UD:
285e126ba97SEli Cohen 	case IB_QPT_SMI:
286e126ba97SEli Cohen 	case IB_QPT_GSI:
287b125a54bSEli Cohen 		size += sizeof(struct mlx5_wqe_ctrl_seg) +
288e126ba97SEli Cohen 			sizeof(struct mlx5_wqe_datagram_seg);
289e126ba97SEli Cohen 		break;
290e126ba97SEli Cohen 
291e126ba97SEli Cohen 	case MLX5_IB_QPT_REG_UMR:
292b125a54bSEli Cohen 		size += sizeof(struct mlx5_wqe_ctrl_seg) +
293e126ba97SEli Cohen 			sizeof(struct mlx5_wqe_umr_ctrl_seg) +
294e126ba97SEli Cohen 			sizeof(struct mlx5_mkey_seg);
295e126ba97SEli Cohen 		break;
296e126ba97SEli Cohen 
297e126ba97SEli Cohen 	default:
298e126ba97SEli Cohen 		return -EINVAL;
299e126ba97SEli Cohen 	}
300e126ba97SEli Cohen 
301e126ba97SEli Cohen 	return size;
302e126ba97SEli Cohen }
303e126ba97SEli Cohen 
304e126ba97SEli Cohen static int calc_send_wqe(struct ib_qp_init_attr *attr)
305e126ba97SEli Cohen {
306e126ba97SEli Cohen 	int inl_size = 0;
307e126ba97SEli Cohen 	int size;
308e126ba97SEli Cohen 
309e126ba97SEli Cohen 	size = sq_overhead(attr->qp_type);
310e126ba97SEli Cohen 	if (size < 0)
311e126ba97SEli Cohen 		return size;
312e126ba97SEli Cohen 
313e126ba97SEli Cohen 	if (attr->cap.max_inline_data) {
314e126ba97SEli Cohen 		inl_size = size + sizeof(struct mlx5_wqe_inline_seg) +
315e126ba97SEli Cohen 			attr->cap.max_inline_data;
316e126ba97SEli Cohen 	}
317e126ba97SEli Cohen 
318e126ba97SEli Cohen 	size += attr->cap.max_send_sge * sizeof(struct mlx5_wqe_data_seg);
319e1e66cc2SSagi Grimberg 	if (attr->create_flags & IB_QP_CREATE_SIGNATURE_EN &&
320e1e66cc2SSagi Grimberg 	    ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB) < MLX5_SIG_WQE_SIZE)
321e1e66cc2SSagi Grimberg 			return MLX5_SIG_WQE_SIZE;
322e1e66cc2SSagi Grimberg 	else
323e126ba97SEli Cohen 		return ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB);
324e126ba97SEli Cohen }
325e126ba97SEli Cohen 
326e126ba97SEli Cohen static int calc_sq_size(struct mlx5_ib_dev *dev, struct ib_qp_init_attr *attr,
327e126ba97SEli Cohen 			struct mlx5_ib_qp *qp)
328e126ba97SEli Cohen {
329e126ba97SEli Cohen 	int wqe_size;
330e126ba97SEli Cohen 	int wq_size;
331e126ba97SEli Cohen 
332e126ba97SEli Cohen 	if (!attr->cap.max_send_wr)
333e126ba97SEli Cohen 		return 0;
334e126ba97SEli Cohen 
335e126ba97SEli Cohen 	wqe_size = calc_send_wqe(attr);
336e126ba97SEli Cohen 	mlx5_ib_dbg(dev, "wqe_size %d\n", wqe_size);
337e126ba97SEli Cohen 	if (wqe_size < 0)
338e126ba97SEli Cohen 		return wqe_size;
339e126ba97SEli Cohen 
340938fe83cSSaeed Mahameed 	if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
341b125a54bSEli Cohen 		mlx5_ib_dbg(dev, "wqe_size(%d) > max_sq_desc_sz(%d)\n",
342938fe83cSSaeed Mahameed 			    wqe_size, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
343e126ba97SEli Cohen 		return -EINVAL;
344e126ba97SEli Cohen 	}
345e126ba97SEli Cohen 
346e126ba97SEli Cohen 	qp->max_inline_data = wqe_size - sq_overhead(attr->qp_type) -
347e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_inline_seg);
348e126ba97SEli Cohen 	attr->cap.max_inline_data = qp->max_inline_data;
349e126ba97SEli Cohen 
350e1e66cc2SSagi Grimberg 	if (attr->create_flags & IB_QP_CREATE_SIGNATURE_EN)
351e1e66cc2SSagi Grimberg 		qp->signature_en = true;
352e1e66cc2SSagi Grimberg 
353e126ba97SEli Cohen 	wq_size = roundup_pow_of_two(attr->cap.max_send_wr * wqe_size);
354e126ba97SEli Cohen 	qp->sq.wqe_cnt = wq_size / MLX5_SEND_WQE_BB;
355938fe83cSSaeed Mahameed 	if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
356b125a54bSEli Cohen 		mlx5_ib_dbg(dev, "wqe count(%d) exceeds limits(%d)\n",
357938fe83cSSaeed Mahameed 			    qp->sq.wqe_cnt,
358938fe83cSSaeed Mahameed 			    1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
359b125a54bSEli Cohen 		return -ENOMEM;
360b125a54bSEli Cohen 	}
361e126ba97SEli Cohen 	qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
362e126ba97SEli Cohen 	qp->sq.max_gs = attr->cap.max_send_sge;
363b125a54bSEli Cohen 	qp->sq.max_post = wq_size / wqe_size;
364b125a54bSEli Cohen 	attr->cap.max_send_wr = qp->sq.max_post;
365e126ba97SEli Cohen 
366e126ba97SEli Cohen 	return wq_size;
367e126ba97SEli Cohen }
368e126ba97SEli Cohen 
369e126ba97SEli Cohen static int set_user_buf_size(struct mlx5_ib_dev *dev,
370e126ba97SEli Cohen 			    struct mlx5_ib_qp *qp,
371e126ba97SEli Cohen 			    struct mlx5_ib_create_qp *ucmd)
372e126ba97SEli Cohen {
373e126ba97SEli Cohen 	int desc_sz = 1 << qp->sq.wqe_shift;
374e126ba97SEli Cohen 
375938fe83cSSaeed Mahameed 	if (desc_sz > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
376e126ba97SEli Cohen 		mlx5_ib_warn(dev, "desc_sz %d, max_sq_desc_sz %d\n",
377938fe83cSSaeed Mahameed 			     desc_sz, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
378e126ba97SEli Cohen 		return -EINVAL;
379e126ba97SEli Cohen 	}
380e126ba97SEli Cohen 
381e126ba97SEli Cohen 	if (ucmd->sq_wqe_count && ((1 << ilog2(ucmd->sq_wqe_count)) != ucmd->sq_wqe_count)) {
382e126ba97SEli Cohen 		mlx5_ib_warn(dev, "sq_wqe_count %d, sq_wqe_count %d\n",
383e126ba97SEli Cohen 			     ucmd->sq_wqe_count, ucmd->sq_wqe_count);
384e126ba97SEli Cohen 		return -EINVAL;
385e126ba97SEli Cohen 	}
386e126ba97SEli Cohen 
387e126ba97SEli Cohen 	qp->sq.wqe_cnt = ucmd->sq_wqe_count;
388e126ba97SEli Cohen 
389938fe83cSSaeed Mahameed 	if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
390e126ba97SEli Cohen 		mlx5_ib_warn(dev, "wqe_cnt %d, max_wqes %d\n",
391938fe83cSSaeed Mahameed 			     qp->sq.wqe_cnt,
392938fe83cSSaeed Mahameed 			     1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
393e126ba97SEli Cohen 		return -EINVAL;
394e126ba97SEli Cohen 	}
395e126ba97SEli Cohen 
396e126ba97SEli Cohen 	qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
397e126ba97SEli Cohen 		(qp->sq.wqe_cnt << 6);
398e126ba97SEli Cohen 
399e126ba97SEli Cohen 	return 0;
400e126ba97SEli Cohen }
401e126ba97SEli Cohen 
402e126ba97SEli Cohen static int qp_has_rq(struct ib_qp_init_attr *attr)
403e126ba97SEli Cohen {
404e126ba97SEli Cohen 	if (attr->qp_type == IB_QPT_XRC_INI ||
405e126ba97SEli Cohen 	    attr->qp_type == IB_QPT_XRC_TGT || attr->srq ||
406e126ba97SEli Cohen 	    attr->qp_type == MLX5_IB_QPT_REG_UMR ||
407e126ba97SEli Cohen 	    !attr->cap.max_recv_wr)
408e126ba97SEli Cohen 		return 0;
409e126ba97SEli Cohen 
410e126ba97SEli Cohen 	return 1;
411e126ba97SEli Cohen }
412e126ba97SEli Cohen 
413c1be5232SEli Cohen static int first_med_uuar(void)
414c1be5232SEli Cohen {
415c1be5232SEli Cohen 	return 1;
416c1be5232SEli Cohen }
417c1be5232SEli Cohen 
418c1be5232SEli Cohen static int next_uuar(int n)
419c1be5232SEli Cohen {
420c1be5232SEli Cohen 	n++;
421c1be5232SEli Cohen 
422c1be5232SEli Cohen 	while (((n % 4) & 2))
423c1be5232SEli Cohen 		n++;
424c1be5232SEli Cohen 
425c1be5232SEli Cohen 	return n;
426c1be5232SEli Cohen }
427c1be5232SEli Cohen 
428c1be5232SEli Cohen static int num_med_uuar(struct mlx5_uuar_info *uuari)
429c1be5232SEli Cohen {
430c1be5232SEli Cohen 	int n;
431c1be5232SEli Cohen 
432c1be5232SEli Cohen 	n = uuari->num_uars * MLX5_NON_FP_BF_REGS_PER_PAGE -
433c1be5232SEli Cohen 		uuari->num_low_latency_uuars - 1;
434c1be5232SEli Cohen 
435c1be5232SEli Cohen 	return n >= 0 ? n : 0;
436c1be5232SEli Cohen }
437c1be5232SEli Cohen 
438c1be5232SEli Cohen static int max_uuari(struct mlx5_uuar_info *uuari)
439c1be5232SEli Cohen {
440c1be5232SEli Cohen 	return uuari->num_uars * 4;
441c1be5232SEli Cohen }
442c1be5232SEli Cohen 
443c1be5232SEli Cohen static int first_hi_uuar(struct mlx5_uuar_info *uuari)
444c1be5232SEli Cohen {
445c1be5232SEli Cohen 	int med;
446c1be5232SEli Cohen 	int i;
447c1be5232SEli Cohen 	int t;
448c1be5232SEli Cohen 
449c1be5232SEli Cohen 	med = num_med_uuar(uuari);
450c1be5232SEli Cohen 	for (t = 0, i = first_med_uuar();; i = next_uuar(i)) {
451c1be5232SEli Cohen 		t++;
452c1be5232SEli Cohen 		if (t == med)
453c1be5232SEli Cohen 			return next_uuar(i);
454c1be5232SEli Cohen 	}
455c1be5232SEli Cohen 
456c1be5232SEli Cohen 	return 0;
457c1be5232SEli Cohen }
458c1be5232SEli Cohen 
459e126ba97SEli Cohen static int alloc_high_class_uuar(struct mlx5_uuar_info *uuari)
460e126ba97SEli Cohen {
461e126ba97SEli Cohen 	int i;
462e126ba97SEli Cohen 
463c1be5232SEli Cohen 	for (i = first_hi_uuar(uuari); i < max_uuari(uuari); i = next_uuar(i)) {
464e126ba97SEli Cohen 		if (!test_bit(i, uuari->bitmap)) {
465e126ba97SEli Cohen 			set_bit(i, uuari->bitmap);
466e126ba97SEli Cohen 			uuari->count[i]++;
467e126ba97SEli Cohen 			return i;
468e126ba97SEli Cohen 		}
469e126ba97SEli Cohen 	}
470e126ba97SEli Cohen 
471e126ba97SEli Cohen 	return -ENOMEM;
472e126ba97SEli Cohen }
473e126ba97SEli Cohen 
474e126ba97SEli Cohen static int alloc_med_class_uuar(struct mlx5_uuar_info *uuari)
475e126ba97SEli Cohen {
476c1be5232SEli Cohen 	int minidx = first_med_uuar();
477e126ba97SEli Cohen 	int i;
478e126ba97SEli Cohen 
479c1be5232SEli Cohen 	for (i = first_med_uuar(); i < first_hi_uuar(uuari); i = next_uuar(i)) {
480e126ba97SEli Cohen 		if (uuari->count[i] < uuari->count[minidx])
481e126ba97SEli Cohen 			minidx = i;
482e126ba97SEli Cohen 	}
483e126ba97SEli Cohen 
484e126ba97SEli Cohen 	uuari->count[minidx]++;
485e126ba97SEli Cohen 	return minidx;
486e126ba97SEli Cohen }
487e126ba97SEli Cohen 
488e126ba97SEli Cohen static int alloc_uuar(struct mlx5_uuar_info *uuari,
489e126ba97SEli Cohen 		      enum mlx5_ib_latency_class lat)
490e126ba97SEli Cohen {
491e126ba97SEli Cohen 	int uuarn = -EINVAL;
492e126ba97SEli Cohen 
493e126ba97SEli Cohen 	mutex_lock(&uuari->lock);
494e126ba97SEli Cohen 	switch (lat) {
495e126ba97SEli Cohen 	case MLX5_IB_LATENCY_CLASS_LOW:
496e126ba97SEli Cohen 		uuarn = 0;
497e126ba97SEli Cohen 		uuari->count[uuarn]++;
498e126ba97SEli Cohen 		break;
499e126ba97SEli Cohen 
500e126ba97SEli Cohen 	case MLX5_IB_LATENCY_CLASS_MEDIUM:
50178c0f98cSEli Cohen 		if (uuari->ver < 2)
50278c0f98cSEli Cohen 			uuarn = -ENOMEM;
50378c0f98cSEli Cohen 		else
504e126ba97SEli Cohen 			uuarn = alloc_med_class_uuar(uuari);
505e126ba97SEli Cohen 		break;
506e126ba97SEli Cohen 
507e126ba97SEli Cohen 	case MLX5_IB_LATENCY_CLASS_HIGH:
50878c0f98cSEli Cohen 		if (uuari->ver < 2)
50978c0f98cSEli Cohen 			uuarn = -ENOMEM;
51078c0f98cSEli Cohen 		else
511e126ba97SEli Cohen 			uuarn = alloc_high_class_uuar(uuari);
512e126ba97SEli Cohen 		break;
513e126ba97SEli Cohen 
514e126ba97SEli Cohen 	case MLX5_IB_LATENCY_CLASS_FAST_PATH:
515e126ba97SEli Cohen 		uuarn = 2;
516e126ba97SEli Cohen 		break;
517e126ba97SEli Cohen 	}
518e126ba97SEli Cohen 	mutex_unlock(&uuari->lock);
519e126ba97SEli Cohen 
520e126ba97SEli Cohen 	return uuarn;
521e126ba97SEli Cohen }
522e126ba97SEli Cohen 
523e126ba97SEli Cohen static void free_med_class_uuar(struct mlx5_uuar_info *uuari, int uuarn)
524e126ba97SEli Cohen {
525e126ba97SEli Cohen 	clear_bit(uuarn, uuari->bitmap);
526e126ba97SEli Cohen 	--uuari->count[uuarn];
527e126ba97SEli Cohen }
528e126ba97SEli Cohen 
529e126ba97SEli Cohen static void free_high_class_uuar(struct mlx5_uuar_info *uuari, int uuarn)
530e126ba97SEli Cohen {
531e126ba97SEli Cohen 	clear_bit(uuarn, uuari->bitmap);
532e126ba97SEli Cohen 	--uuari->count[uuarn];
533e126ba97SEli Cohen }
534e126ba97SEli Cohen 
535e126ba97SEli Cohen static void free_uuar(struct mlx5_uuar_info *uuari, int uuarn)
536e126ba97SEli Cohen {
537e126ba97SEli Cohen 	int nuuars = uuari->num_uars * MLX5_BF_REGS_PER_PAGE;
538e126ba97SEli Cohen 	int high_uuar = nuuars - uuari->num_low_latency_uuars;
539e126ba97SEli Cohen 
540e126ba97SEli Cohen 	mutex_lock(&uuari->lock);
541e126ba97SEli Cohen 	if (uuarn == 0) {
542e126ba97SEli Cohen 		--uuari->count[uuarn];
543e126ba97SEli Cohen 		goto out;
544e126ba97SEli Cohen 	}
545e126ba97SEli Cohen 
546e126ba97SEli Cohen 	if (uuarn < high_uuar) {
547e126ba97SEli Cohen 		free_med_class_uuar(uuari, uuarn);
548e126ba97SEli Cohen 		goto out;
549e126ba97SEli Cohen 	}
550e126ba97SEli Cohen 
551e126ba97SEli Cohen 	free_high_class_uuar(uuari, uuarn);
552e126ba97SEli Cohen 
553e126ba97SEli Cohen out:
554e126ba97SEli Cohen 	mutex_unlock(&uuari->lock);
555e126ba97SEli Cohen }
556e126ba97SEli Cohen 
557e126ba97SEli Cohen static enum mlx5_qp_state to_mlx5_state(enum ib_qp_state state)
558e126ba97SEli Cohen {
559e126ba97SEli Cohen 	switch (state) {
560e126ba97SEli Cohen 	case IB_QPS_RESET:	return MLX5_QP_STATE_RST;
561e126ba97SEli Cohen 	case IB_QPS_INIT:	return MLX5_QP_STATE_INIT;
562e126ba97SEli Cohen 	case IB_QPS_RTR:	return MLX5_QP_STATE_RTR;
563e126ba97SEli Cohen 	case IB_QPS_RTS:	return MLX5_QP_STATE_RTS;
564e126ba97SEli Cohen 	case IB_QPS_SQD:	return MLX5_QP_STATE_SQD;
565e126ba97SEli Cohen 	case IB_QPS_SQE:	return MLX5_QP_STATE_SQER;
566e126ba97SEli Cohen 	case IB_QPS_ERR:	return MLX5_QP_STATE_ERR;
567e126ba97SEli Cohen 	default:		return -1;
568e126ba97SEli Cohen 	}
569e126ba97SEli Cohen }
570e126ba97SEli Cohen 
571e126ba97SEli Cohen static int to_mlx5_st(enum ib_qp_type type)
572e126ba97SEli Cohen {
573e126ba97SEli Cohen 	switch (type) {
574e126ba97SEli Cohen 	case IB_QPT_RC:			return MLX5_QP_ST_RC;
575e126ba97SEli Cohen 	case IB_QPT_UC:			return MLX5_QP_ST_UC;
576e126ba97SEli Cohen 	case IB_QPT_UD:			return MLX5_QP_ST_UD;
577e126ba97SEli Cohen 	case MLX5_IB_QPT_REG_UMR:	return MLX5_QP_ST_REG_UMR;
578e126ba97SEli Cohen 	case IB_QPT_XRC_INI:
579e126ba97SEli Cohen 	case IB_QPT_XRC_TGT:		return MLX5_QP_ST_XRC;
580e126ba97SEli Cohen 	case IB_QPT_SMI:		return MLX5_QP_ST_QP0;
581e126ba97SEli Cohen 	case IB_QPT_GSI:		return MLX5_QP_ST_QP1;
582e126ba97SEli Cohen 	case IB_QPT_RAW_IPV6:		return MLX5_QP_ST_RAW_IPV6;
583e126ba97SEli Cohen 	case IB_QPT_RAW_ETHERTYPE:	return MLX5_QP_ST_RAW_ETHERTYPE;
584e126ba97SEli Cohen 	case IB_QPT_RAW_PACKET:
585e126ba97SEli Cohen 	case IB_QPT_MAX:
586e126ba97SEli Cohen 	default:		return -EINVAL;
587e126ba97SEli Cohen 	}
588e126ba97SEli Cohen }
589e126ba97SEli Cohen 
590e126ba97SEli Cohen static int uuarn_to_uar_index(struct mlx5_uuar_info *uuari, int uuarn)
591e126ba97SEli Cohen {
592e126ba97SEli Cohen 	return uuari->uars[uuarn / MLX5_BF_REGS_PER_PAGE].index;
593e126ba97SEli Cohen }
594e126ba97SEli Cohen 
595e126ba97SEli Cohen static int create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
596e126ba97SEli Cohen 			  struct mlx5_ib_qp *qp, struct ib_udata *udata,
597e126ba97SEli Cohen 			  struct mlx5_create_qp_mbox_in **in,
598e126ba97SEli Cohen 			  struct mlx5_ib_create_qp_resp *resp, int *inlen)
599e126ba97SEli Cohen {
600e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
601e126ba97SEli Cohen 	struct mlx5_ib_create_qp ucmd;
6029e9c47d0SEli Cohen 	int page_shift = 0;
603e126ba97SEli Cohen 	int uar_index;
604e126ba97SEli Cohen 	int npages;
6059e9c47d0SEli Cohen 	u32 offset = 0;
606e126ba97SEli Cohen 	int uuarn;
6079e9c47d0SEli Cohen 	int ncont = 0;
608e126ba97SEli Cohen 	int err;
609e126ba97SEli Cohen 
610e126ba97SEli Cohen 	err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
611e126ba97SEli Cohen 	if (err) {
612e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "copy failed\n");
613e126ba97SEli Cohen 		return err;
614e126ba97SEli Cohen 	}
615e126ba97SEli Cohen 
616e126ba97SEli Cohen 	context = to_mucontext(pd->uobject->context);
617e126ba97SEli Cohen 	/*
618e126ba97SEli Cohen 	 * TBD: should come from the verbs when we have the API
619e126ba97SEli Cohen 	 */
620051f2630SLeon Romanovsky 	if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
621051f2630SLeon Romanovsky 		/* In CROSS_CHANNEL CQ and QP must use the same UAR */
622051f2630SLeon Romanovsky 		uuarn = MLX5_CROSS_CHANNEL_UUAR;
623051f2630SLeon Romanovsky 	else {
624e126ba97SEli Cohen 		uuarn = alloc_uuar(&context->uuari, MLX5_IB_LATENCY_CLASS_HIGH);
625e126ba97SEli Cohen 		if (uuarn < 0) {
626e126ba97SEli Cohen 			mlx5_ib_dbg(dev, "failed to allocate low latency UUAR\n");
627c1be5232SEli Cohen 			mlx5_ib_dbg(dev, "reverting to medium latency\n");
628c1be5232SEli Cohen 			uuarn = alloc_uuar(&context->uuari, MLX5_IB_LATENCY_CLASS_MEDIUM);
629c1be5232SEli Cohen 			if (uuarn < 0) {
630c1be5232SEli Cohen 				mlx5_ib_dbg(dev, "failed to allocate medium latency UUAR\n");
631e126ba97SEli Cohen 				mlx5_ib_dbg(dev, "reverting to high latency\n");
632e126ba97SEli Cohen 				uuarn = alloc_uuar(&context->uuari, MLX5_IB_LATENCY_CLASS_LOW);
633e126ba97SEli Cohen 				if (uuarn < 0) {
634c1be5232SEli Cohen 					mlx5_ib_warn(dev, "uuar allocation failed\n");
635e126ba97SEli Cohen 					return uuarn;
636e126ba97SEli Cohen 				}
637e126ba97SEli Cohen 			}
638c1be5232SEli Cohen 		}
639051f2630SLeon Romanovsky 	}
640e126ba97SEli Cohen 
641e126ba97SEli Cohen 	uar_index = uuarn_to_uar_index(&context->uuari, uuarn);
642e126ba97SEli Cohen 	mlx5_ib_dbg(dev, "uuarn 0x%x, uar_index 0x%x\n", uuarn, uar_index);
643e126ba97SEli Cohen 
64448fea837SHaggai Eran 	qp->rq.offset = 0;
64548fea837SHaggai Eran 	qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
64648fea837SHaggai Eran 	qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
64748fea837SHaggai Eran 
648e126ba97SEli Cohen 	err = set_user_buf_size(dev, qp, &ucmd);
649e126ba97SEli Cohen 	if (err)
650e126ba97SEli Cohen 		goto err_uuar;
651e126ba97SEli Cohen 
6529e9c47d0SEli Cohen 	if (ucmd.buf_addr && qp->buf_size) {
653e126ba97SEli Cohen 		qp->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr,
654e126ba97SEli Cohen 				       qp->buf_size, 0, 0);
655e126ba97SEli Cohen 		if (IS_ERR(qp->umem)) {
656e126ba97SEli Cohen 			mlx5_ib_dbg(dev, "umem_get failed\n");
657e126ba97SEli Cohen 			err = PTR_ERR(qp->umem);
658e126ba97SEli Cohen 			goto err_uuar;
659e126ba97SEli Cohen 		}
6609e9c47d0SEli Cohen 	} else {
6619e9c47d0SEli Cohen 		qp->umem = NULL;
6629e9c47d0SEli Cohen 	}
663e126ba97SEli Cohen 
6649e9c47d0SEli Cohen 	if (qp->umem) {
665e126ba97SEli Cohen 		mlx5_ib_cont_pages(qp->umem, ucmd.buf_addr, &npages, &page_shift,
666e126ba97SEli Cohen 				   &ncont, NULL);
667e126ba97SEli Cohen 		err = mlx5_ib_get_buf_offset(ucmd.buf_addr, page_shift, &offset);
668e126ba97SEli Cohen 		if (err) {
669e126ba97SEli Cohen 			mlx5_ib_warn(dev, "bad offset\n");
670e126ba97SEli Cohen 			goto err_umem;
671e126ba97SEli Cohen 		}
672e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "addr 0x%llx, size %d, npages %d, page_shift %d, ncont %d, offset %d\n",
673e126ba97SEli Cohen 			    ucmd.buf_addr, qp->buf_size, npages, page_shift, ncont, offset);
6749e9c47d0SEli Cohen 	}
675e126ba97SEli Cohen 
676e126ba97SEli Cohen 	*inlen = sizeof(**in) + sizeof(*(*in)->pas) * ncont;
677e126ba97SEli Cohen 	*in = mlx5_vzalloc(*inlen);
678e126ba97SEli Cohen 	if (!*in) {
679e126ba97SEli Cohen 		err = -ENOMEM;
680e126ba97SEli Cohen 		goto err_umem;
681e126ba97SEli Cohen 	}
6829e9c47d0SEli Cohen 	if (qp->umem)
683e126ba97SEli Cohen 		mlx5_ib_populate_pas(dev, qp->umem, page_shift, (*in)->pas, 0);
684e126ba97SEli Cohen 	(*in)->ctx.log_pg_sz_remote_qpn =
6851b77d2bdSEli Cohen 		cpu_to_be32((page_shift - MLX5_ADAPTER_PAGE_SHIFT) << 24);
686e126ba97SEli Cohen 	(*in)->ctx.params2 = cpu_to_be32(offset << 6);
687e126ba97SEli Cohen 
688e126ba97SEli Cohen 	(*in)->ctx.qp_counter_set_usr_page = cpu_to_be32(uar_index);
689e126ba97SEli Cohen 	resp->uuar_index = uuarn;
690e126ba97SEli Cohen 	qp->uuarn = uuarn;
691e126ba97SEli Cohen 
692e126ba97SEli Cohen 	err = mlx5_ib_db_map_user(context, ucmd.db_addr, &qp->db);
693e126ba97SEli Cohen 	if (err) {
694e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "map failed\n");
695e126ba97SEli Cohen 		goto err_free;
696e126ba97SEli Cohen 	}
697e126ba97SEli Cohen 
698e126ba97SEli Cohen 	err = ib_copy_to_udata(udata, resp, sizeof(*resp));
699e126ba97SEli Cohen 	if (err) {
700e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "copy failed\n");
701e126ba97SEli Cohen 		goto err_unmap;
702e126ba97SEli Cohen 	}
703e126ba97SEli Cohen 	qp->create_type = MLX5_QP_USER;
704e126ba97SEli Cohen 
705e126ba97SEli Cohen 	return 0;
706e126ba97SEli Cohen 
707e126ba97SEli Cohen err_unmap:
708e126ba97SEli Cohen 	mlx5_ib_db_unmap_user(context, &qp->db);
709e126ba97SEli Cohen 
710e126ba97SEli Cohen err_free:
711479163f4SAl Viro 	kvfree(*in);
712e126ba97SEli Cohen 
713e126ba97SEli Cohen err_umem:
7149e9c47d0SEli Cohen 	if (qp->umem)
715e126ba97SEli Cohen 		ib_umem_release(qp->umem);
716e126ba97SEli Cohen 
717e126ba97SEli Cohen err_uuar:
718e126ba97SEli Cohen 	free_uuar(&context->uuari, uuarn);
719e126ba97SEli Cohen 	return err;
720e126ba97SEli Cohen }
721e126ba97SEli Cohen 
722e126ba97SEli Cohen static void destroy_qp_user(struct ib_pd *pd, struct mlx5_ib_qp *qp)
723e126ba97SEli Cohen {
724e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
725e126ba97SEli Cohen 
726e126ba97SEli Cohen 	context = to_mucontext(pd->uobject->context);
727e126ba97SEli Cohen 	mlx5_ib_db_unmap_user(context, &qp->db);
7289e9c47d0SEli Cohen 	if (qp->umem)
729e126ba97SEli Cohen 		ib_umem_release(qp->umem);
730e126ba97SEli Cohen 	free_uuar(&context->uuari, qp->uuarn);
731e126ba97SEli Cohen }
732e126ba97SEli Cohen 
733e126ba97SEli Cohen static int create_kernel_qp(struct mlx5_ib_dev *dev,
734e126ba97SEli Cohen 			    struct ib_qp_init_attr *init_attr,
735e126ba97SEli Cohen 			    struct mlx5_ib_qp *qp,
736e126ba97SEli Cohen 			    struct mlx5_create_qp_mbox_in **in, int *inlen)
737e126ba97SEli Cohen {
738e126ba97SEli Cohen 	enum mlx5_ib_latency_class lc = MLX5_IB_LATENCY_CLASS_LOW;
739e126ba97SEli Cohen 	struct mlx5_uuar_info *uuari;
740e126ba97SEli Cohen 	int uar_index;
741e126ba97SEli Cohen 	int uuarn;
742e126ba97SEli Cohen 	int err;
743e126ba97SEli Cohen 
7449603b61dSJack Morgenstein 	uuari = &dev->mdev->priv.uuari;
745652c1a05SOr Gerlitz 	if (init_attr->create_flags & ~(IB_QP_CREATE_SIGNATURE_EN | IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK))
7461a4c3a3dSEli Cohen 		return -EINVAL;
747e126ba97SEli Cohen 
748e126ba97SEli Cohen 	if (init_attr->qp_type == MLX5_IB_QPT_REG_UMR)
749e126ba97SEli Cohen 		lc = MLX5_IB_LATENCY_CLASS_FAST_PATH;
750e126ba97SEli Cohen 
751e126ba97SEli Cohen 	uuarn = alloc_uuar(uuari, lc);
752e126ba97SEli Cohen 	if (uuarn < 0) {
753e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "\n");
754e126ba97SEli Cohen 		return -ENOMEM;
755e126ba97SEli Cohen 	}
756e126ba97SEli Cohen 
757e126ba97SEli Cohen 	qp->bf = &uuari->bfs[uuarn];
758e126ba97SEli Cohen 	uar_index = qp->bf->uar->index;
759e126ba97SEli Cohen 
760e126ba97SEli Cohen 	err = calc_sq_size(dev, init_attr, qp);
761e126ba97SEli Cohen 	if (err < 0) {
762e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "err %d\n", err);
763e126ba97SEli Cohen 		goto err_uuar;
764e126ba97SEli Cohen 	}
765e126ba97SEli Cohen 
766e126ba97SEli Cohen 	qp->rq.offset = 0;
767e126ba97SEli Cohen 	qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
768e126ba97SEli Cohen 	qp->buf_size = err + (qp->rq.wqe_cnt << qp->rq.wqe_shift);
769e126ba97SEli Cohen 
77064ffaa21SAmir Vadai 	err = mlx5_buf_alloc(dev->mdev, qp->buf_size, &qp->buf);
771e126ba97SEli Cohen 	if (err) {
772e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "err %d\n", err);
773e126ba97SEli Cohen 		goto err_uuar;
774e126ba97SEli Cohen 	}
775e126ba97SEli Cohen 
776e126ba97SEli Cohen 	qp->sq.qend = mlx5_get_send_wqe(qp, qp->sq.wqe_cnt);
777e126ba97SEli Cohen 	*inlen = sizeof(**in) + sizeof(*(*in)->pas) * qp->buf.npages;
778e126ba97SEli Cohen 	*in = mlx5_vzalloc(*inlen);
779e126ba97SEli Cohen 	if (!*in) {
780e126ba97SEli Cohen 		err = -ENOMEM;
781e126ba97SEli Cohen 		goto err_buf;
782e126ba97SEli Cohen 	}
783e126ba97SEli Cohen 	(*in)->ctx.qp_counter_set_usr_page = cpu_to_be32(uar_index);
7841b77d2bdSEli Cohen 	(*in)->ctx.log_pg_sz_remote_qpn =
7851b77d2bdSEli Cohen 		cpu_to_be32((qp->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT) << 24);
786e126ba97SEli Cohen 	/* Set "fast registration enabled" for all kernel QPs */
787e126ba97SEli Cohen 	(*in)->ctx.params1 |= cpu_to_be32(1 << 11);
788e126ba97SEli Cohen 	(*in)->ctx.sq_crq_size |= cpu_to_be16(1 << 4);
789e126ba97SEli Cohen 
790e126ba97SEli Cohen 	mlx5_fill_page_array(&qp->buf, (*in)->pas);
791e126ba97SEli Cohen 
7929603b61dSJack Morgenstein 	err = mlx5_db_alloc(dev->mdev, &qp->db);
793e126ba97SEli Cohen 	if (err) {
794e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "err %d\n", err);
795e126ba97SEli Cohen 		goto err_free;
796e126ba97SEli Cohen 	}
797e126ba97SEli Cohen 
798e126ba97SEli Cohen 	qp->sq.wrid = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wrid), GFP_KERNEL);
799e126ba97SEli Cohen 	qp->sq.wr_data = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wr_data), GFP_KERNEL);
800e126ba97SEli Cohen 	qp->rq.wrid = kmalloc(qp->rq.wqe_cnt * sizeof(*qp->rq.wrid), GFP_KERNEL);
801e126ba97SEli Cohen 	qp->sq.w_list = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.w_list), GFP_KERNEL);
802e126ba97SEli Cohen 	qp->sq.wqe_head = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wqe_head), GFP_KERNEL);
803e126ba97SEli Cohen 
804e126ba97SEli Cohen 	if (!qp->sq.wrid || !qp->sq.wr_data || !qp->rq.wrid ||
805e126ba97SEli Cohen 	    !qp->sq.w_list || !qp->sq.wqe_head) {
806e126ba97SEli Cohen 		err = -ENOMEM;
807e126ba97SEli Cohen 		goto err_wrid;
808e126ba97SEli Cohen 	}
809e126ba97SEli Cohen 	qp->create_type = MLX5_QP_KERNEL;
810e126ba97SEli Cohen 
811e126ba97SEli Cohen 	return 0;
812e126ba97SEli Cohen 
813e126ba97SEli Cohen err_wrid:
8149603b61dSJack Morgenstein 	mlx5_db_free(dev->mdev, &qp->db);
815e126ba97SEli Cohen 	kfree(qp->sq.wqe_head);
816e126ba97SEli Cohen 	kfree(qp->sq.w_list);
817e126ba97SEli Cohen 	kfree(qp->sq.wrid);
818e126ba97SEli Cohen 	kfree(qp->sq.wr_data);
819e126ba97SEli Cohen 	kfree(qp->rq.wrid);
820e126ba97SEli Cohen 
821e126ba97SEli Cohen err_free:
822479163f4SAl Viro 	kvfree(*in);
823e126ba97SEli Cohen 
824e126ba97SEli Cohen err_buf:
8259603b61dSJack Morgenstein 	mlx5_buf_free(dev->mdev, &qp->buf);
826e126ba97SEli Cohen 
827e126ba97SEli Cohen err_uuar:
8289603b61dSJack Morgenstein 	free_uuar(&dev->mdev->priv.uuari, uuarn);
829e126ba97SEli Cohen 	return err;
830e126ba97SEli Cohen }
831e126ba97SEli Cohen 
832e126ba97SEli Cohen static void destroy_qp_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
833e126ba97SEli Cohen {
8349603b61dSJack Morgenstein 	mlx5_db_free(dev->mdev, &qp->db);
835e126ba97SEli Cohen 	kfree(qp->sq.wqe_head);
836e126ba97SEli Cohen 	kfree(qp->sq.w_list);
837e126ba97SEli Cohen 	kfree(qp->sq.wrid);
838e126ba97SEli Cohen 	kfree(qp->sq.wr_data);
839e126ba97SEli Cohen 	kfree(qp->rq.wrid);
8409603b61dSJack Morgenstein 	mlx5_buf_free(dev->mdev, &qp->buf);
8419603b61dSJack Morgenstein 	free_uuar(&dev->mdev->priv.uuari, qp->bf->uuarn);
842e126ba97SEli Cohen }
843e126ba97SEli Cohen 
844e126ba97SEli Cohen static __be32 get_rx_type(struct mlx5_ib_qp *qp, struct ib_qp_init_attr *attr)
845e126ba97SEli Cohen {
846e126ba97SEli Cohen 	if (attr->srq || (attr->qp_type == IB_QPT_XRC_TGT) ||
847e126ba97SEli Cohen 	    (attr->qp_type == IB_QPT_XRC_INI))
848e126ba97SEli Cohen 		return cpu_to_be32(MLX5_SRQ_RQ);
849e126ba97SEli Cohen 	else if (!qp->has_rq)
850e126ba97SEli Cohen 		return cpu_to_be32(MLX5_ZERO_LEN_RQ);
851e126ba97SEli Cohen 	else
852e126ba97SEli Cohen 		return cpu_to_be32(MLX5_NON_ZERO_RQ);
853e126ba97SEli Cohen }
854e126ba97SEli Cohen 
855e126ba97SEli Cohen static int is_connected(enum ib_qp_type qp_type)
856e126ba97SEli Cohen {
857e126ba97SEli Cohen 	if (qp_type == IB_QPT_RC || qp_type == IB_QPT_UC)
858e126ba97SEli Cohen 		return 1;
859e126ba97SEli Cohen 
860e126ba97SEli Cohen 	return 0;
861e126ba97SEli Cohen }
862e126ba97SEli Cohen 
863e126ba97SEli Cohen static int create_qp_common(struct mlx5_ib_dev *dev, struct ib_pd *pd,
864e126ba97SEli Cohen 			    struct ib_qp_init_attr *init_attr,
865e126ba97SEli Cohen 			    struct ib_udata *udata, struct mlx5_ib_qp *qp)
866e126ba97SEli Cohen {
867e126ba97SEli Cohen 	struct mlx5_ib_resources *devr = &dev->devr;
868938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
869e126ba97SEli Cohen 	struct mlx5_ib_create_qp_resp resp;
870e126ba97SEli Cohen 	struct mlx5_create_qp_mbox_in *in;
871e126ba97SEli Cohen 	struct mlx5_ib_create_qp ucmd;
872e126ba97SEli Cohen 	int inlen = sizeof(*in);
873e126ba97SEli Cohen 	int err;
874cfb5e088SHaggai Abramovsky 	u32 uidx = MLX5_IB_DEFAULT_UIDX;
875cfb5e088SHaggai Abramovsky 	void *qpc;
876e126ba97SEli Cohen 
8776aec21f6SHaggai Eran 	mlx5_ib_odp_create_qp(qp);
8786aec21f6SHaggai Eran 
879e126ba97SEli Cohen 	mutex_init(&qp->mutex);
880e126ba97SEli Cohen 	spin_lock_init(&qp->sq.lock);
881e126ba97SEli Cohen 	spin_lock_init(&qp->rq.lock);
882e126ba97SEli Cohen 
883f360d88aSEli Cohen 	if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK) {
884938fe83cSSaeed Mahameed 		if (!MLX5_CAP_GEN(mdev, block_lb_mc)) {
885f360d88aSEli Cohen 			mlx5_ib_dbg(dev, "block multicast loopback isn't supported\n");
886f360d88aSEli Cohen 			return -EINVAL;
887f360d88aSEli Cohen 		} else {
888f360d88aSEli Cohen 			qp->flags |= MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK;
889f360d88aSEli Cohen 		}
890f360d88aSEli Cohen 	}
891f360d88aSEli Cohen 
892051f2630SLeon Romanovsky 	if (init_attr->create_flags &
893051f2630SLeon Romanovsky 			(IB_QP_CREATE_CROSS_CHANNEL |
894051f2630SLeon Romanovsky 			 IB_QP_CREATE_MANAGED_SEND |
895051f2630SLeon Romanovsky 			 IB_QP_CREATE_MANAGED_RECV)) {
896051f2630SLeon Romanovsky 		if (!MLX5_CAP_GEN(mdev, cd)) {
897051f2630SLeon Romanovsky 			mlx5_ib_dbg(dev, "cross-channel isn't supported\n");
898051f2630SLeon Romanovsky 			return -EINVAL;
899051f2630SLeon Romanovsky 		}
900051f2630SLeon Romanovsky 		if (init_attr->create_flags & IB_QP_CREATE_CROSS_CHANNEL)
901051f2630SLeon Romanovsky 			qp->flags |= MLX5_IB_QP_CROSS_CHANNEL;
902051f2630SLeon Romanovsky 		if (init_attr->create_flags & IB_QP_CREATE_MANAGED_SEND)
903051f2630SLeon Romanovsky 			qp->flags |= MLX5_IB_QP_MANAGED_SEND;
904051f2630SLeon Romanovsky 		if (init_attr->create_flags & IB_QP_CREATE_MANAGED_RECV)
905051f2630SLeon Romanovsky 			qp->flags |= MLX5_IB_QP_MANAGED_RECV;
906051f2630SLeon Romanovsky 	}
907e126ba97SEli Cohen 	if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
908e126ba97SEli Cohen 		qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;
909e126ba97SEli Cohen 
910e126ba97SEli Cohen 	if (pd && pd->uobject) {
911e126ba97SEli Cohen 		if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
912e126ba97SEli Cohen 			mlx5_ib_dbg(dev, "copy failed\n");
913e126ba97SEli Cohen 			return -EFAULT;
914e126ba97SEli Cohen 		}
915e126ba97SEli Cohen 
916cfb5e088SHaggai Abramovsky 		err = get_qp_user_index(to_mucontext(pd->uobject->context),
917cfb5e088SHaggai Abramovsky 					&ucmd, udata->inlen, &uidx);
918cfb5e088SHaggai Abramovsky 		if (err)
919cfb5e088SHaggai Abramovsky 			return err;
920cfb5e088SHaggai Abramovsky 
921e126ba97SEli Cohen 		qp->wq_sig = !!(ucmd.flags & MLX5_QP_FLAG_SIGNATURE);
922e126ba97SEli Cohen 		qp->scat_cqe = !!(ucmd.flags & MLX5_QP_FLAG_SCATTER_CQE);
923e126ba97SEli Cohen 	} else {
924e126ba97SEli Cohen 		qp->wq_sig = !!wq_signature;
925e126ba97SEli Cohen 	}
926e126ba97SEli Cohen 
927e126ba97SEli Cohen 	qp->has_rq = qp_has_rq(init_attr);
928e126ba97SEli Cohen 	err = set_rq_size(dev, &init_attr->cap, qp->has_rq,
929e126ba97SEli Cohen 			  qp, (pd && pd->uobject) ? &ucmd : NULL);
930e126ba97SEli Cohen 	if (err) {
931e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "err %d\n", err);
932e126ba97SEli Cohen 		return err;
933e126ba97SEli Cohen 	}
934e126ba97SEli Cohen 
935e126ba97SEli Cohen 	if (pd) {
936e126ba97SEli Cohen 		if (pd->uobject) {
937938fe83cSSaeed Mahameed 			__u32 max_wqes =
938938fe83cSSaeed Mahameed 				1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
939e126ba97SEli Cohen 			mlx5_ib_dbg(dev, "requested sq_wqe_count (%d)\n", ucmd.sq_wqe_count);
940e126ba97SEli Cohen 			if (ucmd.rq_wqe_shift != qp->rq.wqe_shift ||
941e126ba97SEli Cohen 			    ucmd.rq_wqe_count != qp->rq.wqe_cnt) {
942e126ba97SEli Cohen 				mlx5_ib_dbg(dev, "invalid rq params\n");
943e126ba97SEli Cohen 				return -EINVAL;
944e126ba97SEli Cohen 			}
945938fe83cSSaeed Mahameed 			if (ucmd.sq_wqe_count > max_wqes) {
946e126ba97SEli Cohen 				mlx5_ib_dbg(dev, "requested sq_wqe_count (%d) > max allowed (%d)\n",
947938fe83cSSaeed Mahameed 					    ucmd.sq_wqe_count, max_wqes);
948e126ba97SEli Cohen 				return -EINVAL;
949e126ba97SEli Cohen 			}
950e126ba97SEli Cohen 			err = create_user_qp(dev, pd, qp, udata, &in, &resp, &inlen);
951e126ba97SEli Cohen 			if (err)
952e126ba97SEli Cohen 				mlx5_ib_dbg(dev, "err %d\n", err);
953e126ba97SEli Cohen 		} else {
954e126ba97SEli Cohen 			err = create_kernel_qp(dev, init_attr, qp, &in, &inlen);
955e126ba97SEli Cohen 			if (err)
956e126ba97SEli Cohen 				mlx5_ib_dbg(dev, "err %d\n", err);
957e126ba97SEli Cohen 		}
958e126ba97SEli Cohen 
959e126ba97SEli Cohen 		if (err)
960e126ba97SEli Cohen 			return err;
961e126ba97SEli Cohen 	} else {
962e126ba97SEli Cohen 		in = mlx5_vzalloc(sizeof(*in));
963e126ba97SEli Cohen 		if (!in)
964e126ba97SEli Cohen 			return -ENOMEM;
965e126ba97SEli Cohen 
966e126ba97SEli Cohen 		qp->create_type = MLX5_QP_EMPTY;
967e126ba97SEli Cohen 	}
968e126ba97SEli Cohen 
969e126ba97SEli Cohen 	if (is_sqp(init_attr->qp_type))
970e126ba97SEli Cohen 		qp->port = init_attr->port_num;
971e126ba97SEli Cohen 
972e126ba97SEli Cohen 	in->ctx.flags = cpu_to_be32(to_mlx5_st(init_attr->qp_type) << 16 |
973e126ba97SEli Cohen 				    MLX5_QP_PM_MIGRATED << 11);
974e126ba97SEli Cohen 
975e126ba97SEli Cohen 	if (init_attr->qp_type != MLX5_IB_QPT_REG_UMR)
976e126ba97SEli Cohen 		in->ctx.flags_pd = cpu_to_be32(to_mpd(pd ? pd : devr->p0)->pdn);
977e126ba97SEli Cohen 	else
978e126ba97SEli Cohen 		in->ctx.flags_pd = cpu_to_be32(MLX5_QP_LAT_SENSITIVE);
979e126ba97SEli Cohen 
980e126ba97SEli Cohen 	if (qp->wq_sig)
981e126ba97SEli Cohen 		in->ctx.flags_pd |= cpu_to_be32(MLX5_QP_ENABLE_SIG);
982e126ba97SEli Cohen 
983f360d88aSEli Cohen 	if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
984f360d88aSEli Cohen 		in->ctx.flags_pd |= cpu_to_be32(MLX5_QP_BLOCK_MCAST);
985f360d88aSEli Cohen 
986051f2630SLeon Romanovsky 	if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
987051f2630SLeon Romanovsky 		in->ctx.params2 |= cpu_to_be32(MLX5_QP_BIT_CC_MASTER);
988051f2630SLeon Romanovsky 	if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
989051f2630SLeon Romanovsky 		in->ctx.params2 |= cpu_to_be32(MLX5_QP_BIT_CC_SLAVE_SEND);
990051f2630SLeon Romanovsky 	if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
991051f2630SLeon Romanovsky 		in->ctx.params2 |= cpu_to_be32(MLX5_QP_BIT_CC_SLAVE_RECV);
992051f2630SLeon Romanovsky 
993e126ba97SEli Cohen 	if (qp->scat_cqe && is_connected(init_attr->qp_type)) {
994e126ba97SEli Cohen 		int rcqe_sz;
995e126ba97SEli Cohen 		int scqe_sz;
996e126ba97SEli Cohen 
997e126ba97SEli Cohen 		rcqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->recv_cq);
998e126ba97SEli Cohen 		scqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->send_cq);
999e126ba97SEli Cohen 
1000e126ba97SEli Cohen 		if (rcqe_sz == 128)
1001e126ba97SEli Cohen 			in->ctx.cs_res = MLX5_RES_SCAT_DATA64_CQE;
1002e126ba97SEli Cohen 		else
1003e126ba97SEli Cohen 			in->ctx.cs_res = MLX5_RES_SCAT_DATA32_CQE;
1004e126ba97SEli Cohen 
1005e126ba97SEli Cohen 		if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR) {
1006e126ba97SEli Cohen 			if (scqe_sz == 128)
1007e126ba97SEli Cohen 				in->ctx.cs_req = MLX5_REQ_SCAT_DATA64_CQE;
1008e126ba97SEli Cohen 			else
1009e126ba97SEli Cohen 				in->ctx.cs_req = MLX5_REQ_SCAT_DATA32_CQE;
1010e126ba97SEli Cohen 		}
1011e126ba97SEli Cohen 	}
1012e126ba97SEli Cohen 
1013e126ba97SEli Cohen 	if (qp->rq.wqe_cnt) {
1014e126ba97SEli Cohen 		in->ctx.rq_size_stride = (qp->rq.wqe_shift - 4);
1015e126ba97SEli Cohen 		in->ctx.rq_size_stride |= ilog2(qp->rq.wqe_cnt) << 3;
1016e126ba97SEli Cohen 	}
1017e126ba97SEli Cohen 
1018e126ba97SEli Cohen 	in->ctx.rq_type_srqn = get_rx_type(qp, init_attr);
1019e126ba97SEli Cohen 
1020e126ba97SEli Cohen 	if (qp->sq.wqe_cnt)
1021e126ba97SEli Cohen 		in->ctx.sq_crq_size |= cpu_to_be16(ilog2(qp->sq.wqe_cnt) << 11);
1022e126ba97SEli Cohen 	else
1023e126ba97SEli Cohen 		in->ctx.sq_crq_size |= cpu_to_be16(0x8000);
1024e126ba97SEli Cohen 
1025e126ba97SEli Cohen 	/* Set default resources */
1026e126ba97SEli Cohen 	switch (init_attr->qp_type) {
1027e126ba97SEli Cohen 	case IB_QPT_XRC_TGT:
1028e126ba97SEli Cohen 		in->ctx.cqn_recv = cpu_to_be32(to_mcq(devr->c0)->mcq.cqn);
1029e126ba97SEli Cohen 		in->ctx.cqn_send = cpu_to_be32(to_mcq(devr->c0)->mcq.cqn);
1030e126ba97SEli Cohen 		in->ctx.rq_type_srqn |= cpu_to_be32(to_msrq(devr->s0)->msrq.srqn);
1031e126ba97SEli Cohen 		in->ctx.xrcd = cpu_to_be32(to_mxrcd(init_attr->xrcd)->xrcdn);
1032e126ba97SEli Cohen 		break;
1033e126ba97SEli Cohen 	case IB_QPT_XRC_INI:
1034e126ba97SEli Cohen 		in->ctx.cqn_recv = cpu_to_be32(to_mcq(devr->c0)->mcq.cqn);
1035e126ba97SEli Cohen 		in->ctx.xrcd = cpu_to_be32(to_mxrcd(devr->x1)->xrcdn);
1036e126ba97SEli Cohen 		in->ctx.rq_type_srqn |= cpu_to_be32(to_msrq(devr->s0)->msrq.srqn);
1037e126ba97SEli Cohen 		break;
1038e126ba97SEli Cohen 	default:
1039e126ba97SEli Cohen 		if (init_attr->srq) {
1040e126ba97SEli Cohen 			in->ctx.xrcd = cpu_to_be32(to_mxrcd(devr->x0)->xrcdn);
1041e126ba97SEli Cohen 			in->ctx.rq_type_srqn |= cpu_to_be32(to_msrq(init_attr->srq)->msrq.srqn);
1042e126ba97SEli Cohen 		} else {
1043e126ba97SEli Cohen 			in->ctx.xrcd = cpu_to_be32(to_mxrcd(devr->x1)->xrcdn);
10444aa17b28SHaggai Abramonvsky 			in->ctx.rq_type_srqn |=
10454aa17b28SHaggai Abramonvsky 				cpu_to_be32(to_msrq(devr->s1)->msrq.srqn);
1046e126ba97SEli Cohen 		}
1047e126ba97SEli Cohen 	}
1048e126ba97SEli Cohen 
1049e126ba97SEli Cohen 	if (init_attr->send_cq)
1050e126ba97SEli Cohen 		in->ctx.cqn_send = cpu_to_be32(to_mcq(init_attr->send_cq)->mcq.cqn);
1051e126ba97SEli Cohen 
1052e126ba97SEli Cohen 	if (init_attr->recv_cq)
1053e126ba97SEli Cohen 		in->ctx.cqn_recv = cpu_to_be32(to_mcq(init_attr->recv_cq)->mcq.cqn);
1054e126ba97SEli Cohen 
1055e126ba97SEli Cohen 	in->ctx.db_rec_addr = cpu_to_be64(qp->db.dma);
1056e126ba97SEli Cohen 
1057cfb5e088SHaggai Abramovsky 	if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1) {
1058cfb5e088SHaggai Abramovsky 		qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);
1059cfb5e088SHaggai Abramovsky 		/* 0xffffff means we ask to work with cqe version 0 */
1060cfb5e088SHaggai Abramovsky 		MLX5_SET(qpc, qpc, user_index, uidx);
1061cfb5e088SHaggai Abramovsky 	}
1062cfb5e088SHaggai Abramovsky 
10639603b61dSJack Morgenstein 	err = mlx5_core_create_qp(dev->mdev, &qp->mqp, in, inlen);
1064e126ba97SEli Cohen 	if (err) {
1065e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "create qp failed\n");
1066e126ba97SEli Cohen 		goto err_create;
1067e126ba97SEli Cohen 	}
1068e126ba97SEli Cohen 
1069479163f4SAl Viro 	kvfree(in);
1070e126ba97SEli Cohen 	/* Hardware wants QPN written in big-endian order (after
1071e126ba97SEli Cohen 	 * shifting) for send doorbell.  Precompute this value to save
1072e126ba97SEli Cohen 	 * a little bit when posting sends.
1073e126ba97SEli Cohen 	 */
1074e126ba97SEli Cohen 	qp->doorbell_qpn = swab32(qp->mqp.qpn << 8);
1075e126ba97SEli Cohen 
1076e126ba97SEli Cohen 	qp->mqp.event = mlx5_ib_qp_event;
1077e126ba97SEli Cohen 
1078e126ba97SEli Cohen 	return 0;
1079e126ba97SEli Cohen 
1080e126ba97SEli Cohen err_create:
1081e126ba97SEli Cohen 	if (qp->create_type == MLX5_QP_USER)
1082e126ba97SEli Cohen 		destroy_qp_user(pd, qp);
1083e126ba97SEli Cohen 	else if (qp->create_type == MLX5_QP_KERNEL)
1084e126ba97SEli Cohen 		destroy_qp_kernel(dev, qp);
1085e126ba97SEli Cohen 
1086479163f4SAl Viro 	kvfree(in);
1087e126ba97SEli Cohen 	return err;
1088e126ba97SEli Cohen }
1089e126ba97SEli Cohen 
1090e126ba97SEli Cohen static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
1091e126ba97SEli Cohen 	__acquires(&send_cq->lock) __acquires(&recv_cq->lock)
1092e126ba97SEli Cohen {
1093e126ba97SEli Cohen 	if (send_cq) {
1094e126ba97SEli Cohen 		if (recv_cq) {
1095e126ba97SEli Cohen 			if (send_cq->mcq.cqn < recv_cq->mcq.cqn)  {
1096e126ba97SEli Cohen 				spin_lock_irq(&send_cq->lock);
1097e126ba97SEli Cohen 				spin_lock_nested(&recv_cq->lock,
1098e126ba97SEli Cohen 						 SINGLE_DEPTH_NESTING);
1099e126ba97SEli Cohen 			} else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
1100e126ba97SEli Cohen 				spin_lock_irq(&send_cq->lock);
1101e126ba97SEli Cohen 				__acquire(&recv_cq->lock);
1102e126ba97SEli Cohen 			} else {
1103e126ba97SEli Cohen 				spin_lock_irq(&recv_cq->lock);
1104e126ba97SEli Cohen 				spin_lock_nested(&send_cq->lock,
1105e126ba97SEli Cohen 						 SINGLE_DEPTH_NESTING);
1106e126ba97SEli Cohen 			}
1107e126ba97SEli Cohen 		} else {
1108e126ba97SEli Cohen 			spin_lock_irq(&send_cq->lock);
11096a4f139aSEli Cohen 			__acquire(&recv_cq->lock);
1110e126ba97SEli Cohen 		}
1111e126ba97SEli Cohen 	} else if (recv_cq) {
1112e126ba97SEli Cohen 		spin_lock_irq(&recv_cq->lock);
11136a4f139aSEli Cohen 		__acquire(&send_cq->lock);
11146a4f139aSEli Cohen 	} else {
11156a4f139aSEli Cohen 		__acquire(&send_cq->lock);
11166a4f139aSEli Cohen 		__acquire(&recv_cq->lock);
1117e126ba97SEli Cohen 	}
1118e126ba97SEli Cohen }
1119e126ba97SEli Cohen 
1120e126ba97SEli Cohen static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
1121e126ba97SEli Cohen 	__releases(&send_cq->lock) __releases(&recv_cq->lock)
1122e126ba97SEli Cohen {
1123e126ba97SEli Cohen 	if (send_cq) {
1124e126ba97SEli Cohen 		if (recv_cq) {
1125e126ba97SEli Cohen 			if (send_cq->mcq.cqn < recv_cq->mcq.cqn)  {
1126e126ba97SEli Cohen 				spin_unlock(&recv_cq->lock);
1127e126ba97SEli Cohen 				spin_unlock_irq(&send_cq->lock);
1128e126ba97SEli Cohen 			} else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
1129e126ba97SEli Cohen 				__release(&recv_cq->lock);
1130e126ba97SEli Cohen 				spin_unlock_irq(&send_cq->lock);
1131e126ba97SEli Cohen 			} else {
1132e126ba97SEli Cohen 				spin_unlock(&send_cq->lock);
1133e126ba97SEli Cohen 				spin_unlock_irq(&recv_cq->lock);
1134e126ba97SEli Cohen 			}
1135e126ba97SEli Cohen 		} else {
11366a4f139aSEli Cohen 			__release(&recv_cq->lock);
1137e126ba97SEli Cohen 			spin_unlock_irq(&send_cq->lock);
1138e126ba97SEli Cohen 		}
1139e126ba97SEli Cohen 	} else if (recv_cq) {
11406a4f139aSEli Cohen 		__release(&send_cq->lock);
1141e126ba97SEli Cohen 		spin_unlock_irq(&recv_cq->lock);
11426a4f139aSEli Cohen 	} else {
11436a4f139aSEli Cohen 		__release(&recv_cq->lock);
11446a4f139aSEli Cohen 		__release(&send_cq->lock);
1145e126ba97SEli Cohen 	}
1146e126ba97SEli Cohen }
1147e126ba97SEli Cohen 
1148e126ba97SEli Cohen static struct mlx5_ib_pd *get_pd(struct mlx5_ib_qp *qp)
1149e126ba97SEli Cohen {
1150e126ba97SEli Cohen 	return to_mpd(qp->ibqp.pd);
1151e126ba97SEli Cohen }
1152e126ba97SEli Cohen 
1153e126ba97SEli Cohen static void get_cqs(struct mlx5_ib_qp *qp,
1154e126ba97SEli Cohen 		    struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq)
1155e126ba97SEli Cohen {
1156e126ba97SEli Cohen 	switch (qp->ibqp.qp_type) {
1157e126ba97SEli Cohen 	case IB_QPT_XRC_TGT:
1158e126ba97SEli Cohen 		*send_cq = NULL;
1159e126ba97SEli Cohen 		*recv_cq = NULL;
1160e126ba97SEli Cohen 		break;
1161e126ba97SEli Cohen 	case MLX5_IB_QPT_REG_UMR:
1162e126ba97SEli Cohen 	case IB_QPT_XRC_INI:
1163e126ba97SEli Cohen 		*send_cq = to_mcq(qp->ibqp.send_cq);
1164e126ba97SEli Cohen 		*recv_cq = NULL;
1165e126ba97SEli Cohen 		break;
1166e126ba97SEli Cohen 
1167e126ba97SEli Cohen 	case IB_QPT_SMI:
1168e126ba97SEli Cohen 	case IB_QPT_GSI:
1169e126ba97SEli Cohen 	case IB_QPT_RC:
1170e126ba97SEli Cohen 	case IB_QPT_UC:
1171e126ba97SEli Cohen 	case IB_QPT_UD:
1172e126ba97SEli Cohen 	case IB_QPT_RAW_IPV6:
1173e126ba97SEli Cohen 	case IB_QPT_RAW_ETHERTYPE:
1174e126ba97SEli Cohen 		*send_cq = to_mcq(qp->ibqp.send_cq);
1175e126ba97SEli Cohen 		*recv_cq = to_mcq(qp->ibqp.recv_cq);
1176e126ba97SEli Cohen 		break;
1177e126ba97SEli Cohen 
1178e126ba97SEli Cohen 	case IB_QPT_RAW_PACKET:
1179e126ba97SEli Cohen 	case IB_QPT_MAX:
1180e126ba97SEli Cohen 	default:
1181e126ba97SEli Cohen 		*send_cq = NULL;
1182e126ba97SEli Cohen 		*recv_cq = NULL;
1183e126ba97SEli Cohen 		break;
1184e126ba97SEli Cohen 	}
1185e126ba97SEli Cohen }
1186e126ba97SEli Cohen 
1187e126ba97SEli Cohen static void destroy_qp_common(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
1188e126ba97SEli Cohen {
1189e126ba97SEli Cohen 	struct mlx5_ib_cq *send_cq, *recv_cq;
1190e126ba97SEli Cohen 	struct mlx5_modify_qp_mbox_in *in;
1191e126ba97SEli Cohen 	int err;
1192e126ba97SEli Cohen 
1193e126ba97SEli Cohen 	in = kzalloc(sizeof(*in), GFP_KERNEL);
1194e126ba97SEli Cohen 	if (!in)
1195e126ba97SEli Cohen 		return;
11967bef7ad2SEli Cohen 
11976aec21f6SHaggai Eran 	if (qp->state != IB_QPS_RESET) {
11986aec21f6SHaggai Eran 		mlx5_ib_qp_disable_pagefaults(qp);
11999603b61dSJack Morgenstein 		if (mlx5_core_qp_modify(dev->mdev, to_mlx5_state(qp->state),
1200c3c6c9c8SHaggai Abramonvsky 					MLX5_QP_STATE_RST, in, 0, &qp->mqp))
1201e126ba97SEli Cohen 			mlx5_ib_warn(dev, "mlx5_ib: modify QP %06x to RESET failed\n",
1202e126ba97SEli Cohen 				     qp->mqp.qpn);
12036aec21f6SHaggai Eran 	}
1204e126ba97SEli Cohen 
1205e126ba97SEli Cohen 	get_cqs(qp, &send_cq, &recv_cq);
1206e126ba97SEli Cohen 
1207e126ba97SEli Cohen 	if (qp->create_type == MLX5_QP_KERNEL) {
1208e126ba97SEli Cohen 		mlx5_ib_lock_cqs(send_cq, recv_cq);
1209e126ba97SEli Cohen 		__mlx5_ib_cq_clean(recv_cq, qp->mqp.qpn,
1210e126ba97SEli Cohen 				   qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
1211e126ba97SEli Cohen 		if (send_cq != recv_cq)
1212e126ba97SEli Cohen 			__mlx5_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
1213e126ba97SEli Cohen 		mlx5_ib_unlock_cqs(send_cq, recv_cq);
1214e126ba97SEli Cohen 	}
1215e126ba97SEli Cohen 
12169603b61dSJack Morgenstein 	err = mlx5_core_destroy_qp(dev->mdev, &qp->mqp);
1217e126ba97SEli Cohen 	if (err)
1218e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed to destroy QP 0x%x\n", qp->mqp.qpn);
1219e126ba97SEli Cohen 	kfree(in);
1220e126ba97SEli Cohen 
1221e126ba97SEli Cohen 
1222e126ba97SEli Cohen 	if (qp->create_type == MLX5_QP_KERNEL)
1223e126ba97SEli Cohen 		destroy_qp_kernel(dev, qp);
1224e126ba97SEli Cohen 	else if (qp->create_type == MLX5_QP_USER)
1225e126ba97SEli Cohen 		destroy_qp_user(&get_pd(qp)->ibpd, qp);
1226e126ba97SEli Cohen }
1227e126ba97SEli Cohen 
1228e126ba97SEli Cohen static const char *ib_qp_type_str(enum ib_qp_type type)
1229e126ba97SEli Cohen {
1230e126ba97SEli Cohen 	switch (type) {
1231e126ba97SEli Cohen 	case IB_QPT_SMI:
1232e126ba97SEli Cohen 		return "IB_QPT_SMI";
1233e126ba97SEli Cohen 	case IB_QPT_GSI:
1234e126ba97SEli Cohen 		return "IB_QPT_GSI";
1235e126ba97SEli Cohen 	case IB_QPT_RC:
1236e126ba97SEli Cohen 		return "IB_QPT_RC";
1237e126ba97SEli Cohen 	case IB_QPT_UC:
1238e126ba97SEli Cohen 		return "IB_QPT_UC";
1239e126ba97SEli Cohen 	case IB_QPT_UD:
1240e126ba97SEli Cohen 		return "IB_QPT_UD";
1241e126ba97SEli Cohen 	case IB_QPT_RAW_IPV6:
1242e126ba97SEli Cohen 		return "IB_QPT_RAW_IPV6";
1243e126ba97SEli Cohen 	case IB_QPT_RAW_ETHERTYPE:
1244e126ba97SEli Cohen 		return "IB_QPT_RAW_ETHERTYPE";
1245e126ba97SEli Cohen 	case IB_QPT_XRC_INI:
1246e126ba97SEli Cohen 		return "IB_QPT_XRC_INI";
1247e126ba97SEli Cohen 	case IB_QPT_XRC_TGT:
1248e126ba97SEli Cohen 		return "IB_QPT_XRC_TGT";
1249e126ba97SEli Cohen 	case IB_QPT_RAW_PACKET:
1250e126ba97SEli Cohen 		return "IB_QPT_RAW_PACKET";
1251e126ba97SEli Cohen 	case MLX5_IB_QPT_REG_UMR:
1252e126ba97SEli Cohen 		return "MLX5_IB_QPT_REG_UMR";
1253e126ba97SEli Cohen 	case IB_QPT_MAX:
1254e126ba97SEli Cohen 	default:
1255e126ba97SEli Cohen 		return "Invalid QP type";
1256e126ba97SEli Cohen 	}
1257e126ba97SEli Cohen }
1258e126ba97SEli Cohen 
1259e126ba97SEli Cohen struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd,
1260e126ba97SEli Cohen 				struct ib_qp_init_attr *init_attr,
1261e126ba97SEli Cohen 				struct ib_udata *udata)
1262e126ba97SEli Cohen {
1263e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
1264e126ba97SEli Cohen 	struct mlx5_ib_qp *qp;
1265e126ba97SEli Cohen 	u16 xrcdn = 0;
1266e126ba97SEli Cohen 	int err;
1267e126ba97SEli Cohen 
1268e126ba97SEli Cohen 	if (pd) {
1269e126ba97SEli Cohen 		dev = to_mdev(pd->device);
1270e126ba97SEli Cohen 	} else {
1271e126ba97SEli Cohen 		/* being cautious here */
1272e126ba97SEli Cohen 		if (init_attr->qp_type != IB_QPT_XRC_TGT &&
1273e126ba97SEli Cohen 		    init_attr->qp_type != MLX5_IB_QPT_REG_UMR) {
1274e126ba97SEli Cohen 			pr_warn("%s: no PD for transport %s\n", __func__,
1275e126ba97SEli Cohen 				ib_qp_type_str(init_attr->qp_type));
1276e126ba97SEli Cohen 			return ERR_PTR(-EINVAL);
1277e126ba97SEli Cohen 		}
1278e126ba97SEli Cohen 		dev = to_mdev(to_mxrcd(init_attr->xrcd)->ibxrcd.device);
1279e126ba97SEli Cohen 	}
1280e126ba97SEli Cohen 
1281e126ba97SEli Cohen 	switch (init_attr->qp_type) {
1282e126ba97SEli Cohen 	case IB_QPT_XRC_TGT:
1283e126ba97SEli Cohen 	case IB_QPT_XRC_INI:
1284938fe83cSSaeed Mahameed 		if (!MLX5_CAP_GEN(dev->mdev, xrc)) {
1285e126ba97SEli Cohen 			mlx5_ib_dbg(dev, "XRC not supported\n");
1286e126ba97SEli Cohen 			return ERR_PTR(-ENOSYS);
1287e126ba97SEli Cohen 		}
1288e126ba97SEli Cohen 		init_attr->recv_cq = NULL;
1289e126ba97SEli Cohen 		if (init_attr->qp_type == IB_QPT_XRC_TGT) {
1290e126ba97SEli Cohen 			xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
1291e126ba97SEli Cohen 			init_attr->send_cq = NULL;
1292e126ba97SEli Cohen 		}
1293e126ba97SEli Cohen 
1294e126ba97SEli Cohen 		/* fall through */
1295e126ba97SEli Cohen 	case IB_QPT_RC:
1296e126ba97SEli Cohen 	case IB_QPT_UC:
1297e126ba97SEli Cohen 	case IB_QPT_UD:
1298e126ba97SEli Cohen 	case IB_QPT_SMI:
1299e126ba97SEli Cohen 	case IB_QPT_GSI:
1300e126ba97SEli Cohen 	case MLX5_IB_QPT_REG_UMR:
1301e126ba97SEli Cohen 		qp = kzalloc(sizeof(*qp), GFP_KERNEL);
1302e126ba97SEli Cohen 		if (!qp)
1303e126ba97SEli Cohen 			return ERR_PTR(-ENOMEM);
1304e126ba97SEli Cohen 
1305e126ba97SEli Cohen 		err = create_qp_common(dev, pd, init_attr, udata, qp);
1306e126ba97SEli Cohen 		if (err) {
1307e126ba97SEli Cohen 			mlx5_ib_dbg(dev, "create_qp_common failed\n");
1308e126ba97SEli Cohen 			kfree(qp);
1309e126ba97SEli Cohen 			return ERR_PTR(err);
1310e126ba97SEli Cohen 		}
1311e126ba97SEli Cohen 
1312e126ba97SEli Cohen 		if (is_qp0(init_attr->qp_type))
1313e126ba97SEli Cohen 			qp->ibqp.qp_num = 0;
1314e126ba97SEli Cohen 		else if (is_qp1(init_attr->qp_type))
1315e126ba97SEli Cohen 			qp->ibqp.qp_num = 1;
1316e126ba97SEli Cohen 		else
1317e126ba97SEli Cohen 			qp->ibqp.qp_num = qp->mqp.qpn;
1318e126ba97SEli Cohen 
1319e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "ib qpnum 0x%x, mlx qpn 0x%x, rcqn 0x%x, scqn 0x%x\n",
1320e126ba97SEli Cohen 			    qp->ibqp.qp_num, qp->mqp.qpn, to_mcq(init_attr->recv_cq)->mcq.cqn,
1321e126ba97SEli Cohen 			    to_mcq(init_attr->send_cq)->mcq.cqn);
1322e126ba97SEli Cohen 
1323e126ba97SEli Cohen 		qp->xrcdn = xrcdn;
1324e126ba97SEli Cohen 
1325e126ba97SEli Cohen 		break;
1326e126ba97SEli Cohen 
1327e126ba97SEli Cohen 	case IB_QPT_RAW_IPV6:
1328e126ba97SEli Cohen 	case IB_QPT_RAW_ETHERTYPE:
1329e126ba97SEli Cohen 	case IB_QPT_RAW_PACKET:
1330e126ba97SEli Cohen 	case IB_QPT_MAX:
1331e126ba97SEli Cohen 	default:
1332e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "unsupported qp type %d\n",
1333e126ba97SEli Cohen 			    init_attr->qp_type);
1334e126ba97SEli Cohen 		/* Don't support raw QPs */
1335e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
1336e126ba97SEli Cohen 	}
1337e126ba97SEli Cohen 
1338e126ba97SEli Cohen 	return &qp->ibqp;
1339e126ba97SEli Cohen }
1340e126ba97SEli Cohen 
1341e126ba97SEli Cohen int mlx5_ib_destroy_qp(struct ib_qp *qp)
1342e126ba97SEli Cohen {
1343e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
1344e126ba97SEli Cohen 	struct mlx5_ib_qp *mqp = to_mqp(qp);
1345e126ba97SEli Cohen 
1346e126ba97SEli Cohen 	destroy_qp_common(dev, mqp);
1347e126ba97SEli Cohen 
1348e126ba97SEli Cohen 	kfree(mqp);
1349e126ba97SEli Cohen 
1350e126ba97SEli Cohen 	return 0;
1351e126ba97SEli Cohen }
1352e126ba97SEli Cohen 
1353e126ba97SEli Cohen static __be32 to_mlx5_access_flags(struct mlx5_ib_qp *qp, const struct ib_qp_attr *attr,
1354e126ba97SEli Cohen 				   int attr_mask)
1355e126ba97SEli Cohen {
1356e126ba97SEli Cohen 	u32 hw_access_flags = 0;
1357e126ba97SEli Cohen 	u8 dest_rd_atomic;
1358e126ba97SEli Cohen 	u32 access_flags;
1359e126ba97SEli Cohen 
1360e126ba97SEli Cohen 	if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1361e126ba97SEli Cohen 		dest_rd_atomic = attr->max_dest_rd_atomic;
1362e126ba97SEli Cohen 	else
1363e126ba97SEli Cohen 		dest_rd_atomic = qp->resp_depth;
1364e126ba97SEli Cohen 
1365e126ba97SEli Cohen 	if (attr_mask & IB_QP_ACCESS_FLAGS)
1366e126ba97SEli Cohen 		access_flags = attr->qp_access_flags;
1367e126ba97SEli Cohen 	else
1368e126ba97SEli Cohen 		access_flags = qp->atomic_rd_en;
1369e126ba97SEli Cohen 
1370e126ba97SEli Cohen 	if (!dest_rd_atomic)
1371e126ba97SEli Cohen 		access_flags &= IB_ACCESS_REMOTE_WRITE;
1372e126ba97SEli Cohen 
1373e126ba97SEli Cohen 	if (access_flags & IB_ACCESS_REMOTE_READ)
1374e126ba97SEli Cohen 		hw_access_flags |= MLX5_QP_BIT_RRE;
1375e126ba97SEli Cohen 	if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
1376e126ba97SEli Cohen 		hw_access_flags |= (MLX5_QP_BIT_RAE | MLX5_ATOMIC_MODE_CX);
1377e126ba97SEli Cohen 	if (access_flags & IB_ACCESS_REMOTE_WRITE)
1378e126ba97SEli Cohen 		hw_access_flags |= MLX5_QP_BIT_RWE;
1379e126ba97SEli Cohen 
1380e126ba97SEli Cohen 	return cpu_to_be32(hw_access_flags);
1381e126ba97SEli Cohen }
1382e126ba97SEli Cohen 
1383e126ba97SEli Cohen enum {
1384e126ba97SEli Cohen 	MLX5_PATH_FLAG_FL	= 1 << 0,
1385e126ba97SEli Cohen 	MLX5_PATH_FLAG_FREE_AR	= 1 << 1,
1386e126ba97SEli Cohen 	MLX5_PATH_FLAG_COUNTER	= 1 << 2,
1387e126ba97SEli Cohen };
1388e126ba97SEli Cohen 
1389e126ba97SEli Cohen static int ib_rate_to_mlx5(struct mlx5_ib_dev *dev, u8 rate)
1390e126ba97SEli Cohen {
1391e126ba97SEli Cohen 	if (rate == IB_RATE_PORT_CURRENT) {
1392e126ba97SEli Cohen 		return 0;
1393e126ba97SEli Cohen 	} else if (rate < IB_RATE_2_5_GBPS || rate > IB_RATE_300_GBPS) {
1394e126ba97SEli Cohen 		return -EINVAL;
1395e126ba97SEli Cohen 	} else {
1396e126ba97SEli Cohen 		while (rate != IB_RATE_2_5_GBPS &&
1397e126ba97SEli Cohen 		       !(1 << (rate + MLX5_STAT_RATE_OFFSET) &
1398938fe83cSSaeed Mahameed 			 MLX5_CAP_GEN(dev->mdev, stat_rate_support)))
1399e126ba97SEli Cohen 			--rate;
1400e126ba97SEli Cohen 	}
1401e126ba97SEli Cohen 
1402e126ba97SEli Cohen 	return rate + MLX5_STAT_RATE_OFFSET;
1403e126ba97SEli Cohen }
1404e126ba97SEli Cohen 
1405e126ba97SEli Cohen static int mlx5_set_path(struct mlx5_ib_dev *dev, const struct ib_ah_attr *ah,
1406e126ba97SEli Cohen 			 struct mlx5_qp_path *path, u8 port, int attr_mask,
1407e126ba97SEli Cohen 			 u32 path_flags, const struct ib_qp_attr *attr)
1408e126ba97SEli Cohen {
14092811ba51SAchiad Shochat 	enum rdma_link_layer ll = rdma_port_get_link_layer(&dev->ib_dev, port);
1410e126ba97SEli Cohen 	int err;
1411e126ba97SEli Cohen 
1412e126ba97SEli Cohen 	if (attr_mask & IB_QP_PKEY_INDEX)
1413e126ba97SEli Cohen 		path->pkey_index = attr->pkey_index;
1414e126ba97SEli Cohen 
1415e126ba97SEli Cohen 	if (ah->ah_flags & IB_AH_GRH) {
1416938fe83cSSaeed Mahameed 		if (ah->grh.sgid_index >=
1417938fe83cSSaeed Mahameed 		    dev->mdev->port_caps[port - 1].gid_table_len) {
1418f4f01b54SJoe Perches 			pr_err("sgid_index (%u) too large. max is %d\n",
1419938fe83cSSaeed Mahameed 			       ah->grh.sgid_index,
1420938fe83cSSaeed Mahameed 			       dev->mdev->port_caps[port - 1].gid_table_len);
1421f83b4263SEli Cohen 			return -EINVAL;
1422f83b4263SEli Cohen 		}
14232811ba51SAchiad Shochat 	}
14242811ba51SAchiad Shochat 
14252811ba51SAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET) {
14262811ba51SAchiad Shochat 		if (!(ah->ah_flags & IB_AH_GRH))
14272811ba51SAchiad Shochat 			return -EINVAL;
14282811ba51SAchiad Shochat 		memcpy(path->rmac, ah->dmac, sizeof(ah->dmac));
14292811ba51SAchiad Shochat 		path->udp_sport = mlx5_get_roce_udp_sport(dev, port,
14302811ba51SAchiad Shochat 							  ah->grh.sgid_index);
14312811ba51SAchiad Shochat 		path->dci_cfi_prio_sl = (ah->sl & 0x7) << 4;
14322811ba51SAchiad Shochat 	} else {
14332811ba51SAchiad Shochat 		path->fl = (path_flags & MLX5_PATH_FLAG_FL) ? 0x80 : 0;
14342811ba51SAchiad Shochat 		path->free_ar = (path_flags & MLX5_PATH_FLAG_FREE_AR) ? 0x80 :
14352811ba51SAchiad Shochat 									0;
14362811ba51SAchiad Shochat 		path->rlid = cpu_to_be16(ah->dlid);
14372811ba51SAchiad Shochat 		path->grh_mlid = ah->src_path_bits & 0x7f;
14382811ba51SAchiad Shochat 		if (ah->ah_flags & IB_AH_GRH)
1439e126ba97SEli Cohen 			path->grh_mlid	|= 1 << 7;
14402811ba51SAchiad Shochat 		path->dci_cfi_prio_sl = ah->sl & 0xf;
14412811ba51SAchiad Shochat 	}
14422811ba51SAchiad Shochat 
14432811ba51SAchiad Shochat 	if (ah->ah_flags & IB_AH_GRH) {
1444e126ba97SEli Cohen 		path->mgid_index = ah->grh.sgid_index;
1445e126ba97SEli Cohen 		path->hop_limit  = ah->grh.hop_limit;
1446e126ba97SEli Cohen 		path->tclass_flowlabel =
1447e126ba97SEli Cohen 			cpu_to_be32((ah->grh.traffic_class << 20) |
1448e126ba97SEli Cohen 				    (ah->grh.flow_label));
1449e126ba97SEli Cohen 		memcpy(path->rgid, ah->grh.dgid.raw, 16);
1450e126ba97SEli Cohen 	}
1451e126ba97SEli Cohen 
1452e126ba97SEli Cohen 	err = ib_rate_to_mlx5(dev, ah->static_rate);
1453e126ba97SEli Cohen 	if (err < 0)
1454e126ba97SEli Cohen 		return err;
1455e126ba97SEli Cohen 	path->static_rate = err;
1456e126ba97SEli Cohen 	path->port = port;
1457e126ba97SEli Cohen 
1458e126ba97SEli Cohen 	if (attr_mask & IB_QP_TIMEOUT)
1459e126ba97SEli Cohen 		path->ackto_lt = attr->timeout << 3;
1460e126ba97SEli Cohen 
1461e126ba97SEli Cohen 	return 0;
1462e126ba97SEli Cohen }
1463e126ba97SEli Cohen 
1464e126ba97SEli Cohen static enum mlx5_qp_optpar opt_mask[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE][MLX5_QP_ST_MAX] = {
1465e126ba97SEli Cohen 	[MLX5_QP_STATE_INIT] = {
1466e126ba97SEli Cohen 		[MLX5_QP_STATE_INIT] = {
1467e126ba97SEli Cohen 			[MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE		|
1468e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RAE		|
1469e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RWE		|
1470e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_PKEY_INDEX	|
1471e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_PRI_PORT,
1472e126ba97SEli Cohen 			[MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE		|
1473e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_PKEY_INDEX	|
1474e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_PRI_PORT,
1475e126ba97SEli Cohen 			[MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX	|
1476e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_Q_KEY		|
1477e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_PRI_PORT,
1478e126ba97SEli Cohen 		},
1479e126ba97SEli Cohen 		[MLX5_QP_STATE_RTR] = {
1480e126ba97SEli Cohen 			[MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH  |
1481e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RRE            |
1482e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RAE            |
1483e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RWE            |
1484e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_PKEY_INDEX,
1485e126ba97SEli Cohen 			[MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH  |
1486e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RWE            |
1487e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_PKEY_INDEX,
1488e126ba97SEli Cohen 			[MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX     |
1489e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_Q_KEY,
1490e126ba97SEli Cohen 			[MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_PKEY_INDEX	|
1491e126ba97SEli Cohen 					   MLX5_QP_OPTPAR_Q_KEY,
1492a4774e90SEli Cohen 			[MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
1493a4774e90SEli Cohen 					  MLX5_QP_OPTPAR_RRE            |
1494a4774e90SEli Cohen 					  MLX5_QP_OPTPAR_RAE            |
1495a4774e90SEli Cohen 					  MLX5_QP_OPTPAR_RWE            |
1496a4774e90SEli Cohen 					  MLX5_QP_OPTPAR_PKEY_INDEX,
1497e126ba97SEli Cohen 		},
1498e126ba97SEli Cohen 	},
1499e126ba97SEli Cohen 	[MLX5_QP_STATE_RTR] = {
1500e126ba97SEli Cohen 		[MLX5_QP_STATE_RTS] = {
1501e126ba97SEli Cohen 			[MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH	|
1502e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RRE		|
1503e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RAE		|
1504e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RWE		|
1505e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_PM_STATE	|
1506e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RNR_TIMEOUT,
1507e126ba97SEli Cohen 			[MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH	|
1508e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RWE		|
1509e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_PM_STATE,
1510e126ba97SEli Cohen 			[MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
1511e126ba97SEli Cohen 		},
1512e126ba97SEli Cohen 	},
1513e126ba97SEli Cohen 	[MLX5_QP_STATE_RTS] = {
1514e126ba97SEli Cohen 		[MLX5_QP_STATE_RTS] = {
1515e126ba97SEli Cohen 			[MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE		|
1516e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RAE		|
1517e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RWE		|
1518e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RNR_TIMEOUT	|
1519c2a3431eSEli Cohen 					  MLX5_QP_OPTPAR_PM_STATE	|
1520c2a3431eSEli Cohen 					  MLX5_QP_OPTPAR_ALT_ADDR_PATH,
1521e126ba97SEli Cohen 			[MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE		|
1522c2a3431eSEli Cohen 					  MLX5_QP_OPTPAR_PM_STATE	|
1523c2a3431eSEli Cohen 					  MLX5_QP_OPTPAR_ALT_ADDR_PATH,
1524e126ba97SEli Cohen 			[MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY		|
1525e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_SRQN		|
1526e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_CQN_RCV,
1527e126ba97SEli Cohen 		},
1528e126ba97SEli Cohen 	},
1529e126ba97SEli Cohen 	[MLX5_QP_STATE_SQER] = {
1530e126ba97SEli Cohen 		[MLX5_QP_STATE_RTS] = {
1531e126ba97SEli Cohen 			[MLX5_QP_ST_UD]	 = MLX5_QP_OPTPAR_Q_KEY,
1532e126ba97SEli Cohen 			[MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_Q_KEY,
153375959f56SEli Cohen 			[MLX5_QP_ST_UC]	 = MLX5_QP_OPTPAR_RWE,
1534a4774e90SEli Cohen 			[MLX5_QP_ST_RC]	 = MLX5_QP_OPTPAR_RNR_TIMEOUT	|
1535a4774e90SEli Cohen 					   MLX5_QP_OPTPAR_RWE		|
1536a4774e90SEli Cohen 					   MLX5_QP_OPTPAR_RAE		|
1537a4774e90SEli Cohen 					   MLX5_QP_OPTPAR_RRE,
1538e126ba97SEli Cohen 		},
1539e126ba97SEli Cohen 	},
1540e126ba97SEli Cohen };
1541e126ba97SEli Cohen 
1542e126ba97SEli Cohen static int ib_nr_to_mlx5_nr(int ib_mask)
1543e126ba97SEli Cohen {
1544e126ba97SEli Cohen 	switch (ib_mask) {
1545e126ba97SEli Cohen 	case IB_QP_STATE:
1546e126ba97SEli Cohen 		return 0;
1547e126ba97SEli Cohen 	case IB_QP_CUR_STATE:
1548e126ba97SEli Cohen 		return 0;
1549e126ba97SEli Cohen 	case IB_QP_EN_SQD_ASYNC_NOTIFY:
1550e126ba97SEli Cohen 		return 0;
1551e126ba97SEli Cohen 	case IB_QP_ACCESS_FLAGS:
1552e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_RWE | MLX5_QP_OPTPAR_RRE |
1553e126ba97SEli Cohen 			MLX5_QP_OPTPAR_RAE;
1554e126ba97SEli Cohen 	case IB_QP_PKEY_INDEX:
1555e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_PKEY_INDEX;
1556e126ba97SEli Cohen 	case IB_QP_PORT:
1557e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_PRI_PORT;
1558e126ba97SEli Cohen 	case IB_QP_QKEY:
1559e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_Q_KEY;
1560e126ba97SEli Cohen 	case IB_QP_AV:
1561e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH |
1562e126ba97SEli Cohen 			MLX5_QP_OPTPAR_PRI_PORT;
1563e126ba97SEli Cohen 	case IB_QP_PATH_MTU:
1564e126ba97SEli Cohen 		return 0;
1565e126ba97SEli Cohen 	case IB_QP_TIMEOUT:
1566e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_ACK_TIMEOUT;
1567e126ba97SEli Cohen 	case IB_QP_RETRY_CNT:
1568e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_RETRY_COUNT;
1569e126ba97SEli Cohen 	case IB_QP_RNR_RETRY:
1570e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_RNR_RETRY;
1571e126ba97SEli Cohen 	case IB_QP_RQ_PSN:
1572e126ba97SEli Cohen 		return 0;
1573e126ba97SEli Cohen 	case IB_QP_MAX_QP_RD_ATOMIC:
1574e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_SRA_MAX;
1575e126ba97SEli Cohen 	case IB_QP_ALT_PATH:
1576e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_ALT_ADDR_PATH;
1577e126ba97SEli Cohen 	case IB_QP_MIN_RNR_TIMER:
1578e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_RNR_TIMEOUT;
1579e126ba97SEli Cohen 	case IB_QP_SQ_PSN:
1580e126ba97SEli Cohen 		return 0;
1581e126ba97SEli Cohen 	case IB_QP_MAX_DEST_RD_ATOMIC:
1582e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_RRA_MAX | MLX5_QP_OPTPAR_RWE |
1583e126ba97SEli Cohen 			MLX5_QP_OPTPAR_RRE | MLX5_QP_OPTPAR_RAE;
1584e126ba97SEli Cohen 	case IB_QP_PATH_MIG_STATE:
1585e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_PM_STATE;
1586e126ba97SEli Cohen 	case IB_QP_CAP:
1587e126ba97SEli Cohen 		return 0;
1588e126ba97SEli Cohen 	case IB_QP_DEST_QPN:
1589e126ba97SEli Cohen 		return 0;
1590e126ba97SEli Cohen 	}
1591e126ba97SEli Cohen 	return 0;
1592e126ba97SEli Cohen }
1593e126ba97SEli Cohen 
1594e126ba97SEli Cohen static int ib_mask_to_mlx5_opt(int ib_mask)
1595e126ba97SEli Cohen {
1596e126ba97SEli Cohen 	int result = 0;
1597e126ba97SEli Cohen 	int i;
1598e126ba97SEli Cohen 
1599e126ba97SEli Cohen 	for (i = 0; i < 8 * sizeof(int); i++) {
1600e126ba97SEli Cohen 		if ((1 << i) & ib_mask)
1601e126ba97SEli Cohen 			result |= ib_nr_to_mlx5_nr(1 << i);
1602e126ba97SEli Cohen 	}
1603e126ba97SEli Cohen 
1604e126ba97SEli Cohen 	return result;
1605e126ba97SEli Cohen }
1606e126ba97SEli Cohen 
1607e126ba97SEli Cohen static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
1608e126ba97SEli Cohen 			       const struct ib_qp_attr *attr, int attr_mask,
1609e126ba97SEli Cohen 			       enum ib_qp_state cur_state, enum ib_qp_state new_state)
1610e126ba97SEli Cohen {
1611e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
1612e126ba97SEli Cohen 	struct mlx5_ib_qp *qp = to_mqp(ibqp);
1613e126ba97SEli Cohen 	struct mlx5_ib_cq *send_cq, *recv_cq;
1614e126ba97SEli Cohen 	struct mlx5_qp_context *context;
1615e126ba97SEli Cohen 	struct mlx5_modify_qp_mbox_in *in;
1616e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
1617e126ba97SEli Cohen 	enum mlx5_qp_state mlx5_cur, mlx5_new;
1618e126ba97SEli Cohen 	enum mlx5_qp_optpar optpar;
1619e126ba97SEli Cohen 	int sqd_event;
1620e126ba97SEli Cohen 	int mlx5_st;
1621e126ba97SEli Cohen 	int err;
1622e126ba97SEli Cohen 
1623e126ba97SEli Cohen 	in = kzalloc(sizeof(*in), GFP_KERNEL);
1624e126ba97SEli Cohen 	if (!in)
1625e126ba97SEli Cohen 		return -ENOMEM;
1626e126ba97SEli Cohen 
1627e126ba97SEli Cohen 	context = &in->ctx;
1628e126ba97SEli Cohen 	err = to_mlx5_st(ibqp->qp_type);
1629e126ba97SEli Cohen 	if (err < 0)
1630e126ba97SEli Cohen 		goto out;
1631e126ba97SEli Cohen 
1632e126ba97SEli Cohen 	context->flags = cpu_to_be32(err << 16);
1633e126ba97SEli Cohen 
1634e126ba97SEli Cohen 	if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
1635e126ba97SEli Cohen 		context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
1636e126ba97SEli Cohen 	} else {
1637e126ba97SEli Cohen 		switch (attr->path_mig_state) {
1638e126ba97SEli Cohen 		case IB_MIG_MIGRATED:
1639e126ba97SEli Cohen 			context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
1640e126ba97SEli Cohen 			break;
1641e126ba97SEli Cohen 		case IB_MIG_REARM:
1642e126ba97SEli Cohen 			context->flags |= cpu_to_be32(MLX5_QP_PM_REARM << 11);
1643e126ba97SEli Cohen 			break;
1644e126ba97SEli Cohen 		case IB_MIG_ARMED:
1645e126ba97SEli Cohen 			context->flags |= cpu_to_be32(MLX5_QP_PM_ARMED << 11);
1646e126ba97SEli Cohen 			break;
1647e126ba97SEli Cohen 		}
1648e126ba97SEli Cohen 	}
1649e126ba97SEli Cohen 
1650e126ba97SEli Cohen 	if (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI) {
1651e126ba97SEli Cohen 		context->mtu_msgmax = (IB_MTU_256 << 5) | 8;
1652e126ba97SEli Cohen 	} else if (ibqp->qp_type == IB_QPT_UD ||
1653e126ba97SEli Cohen 		   ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
1654e126ba97SEli Cohen 		context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
1655e126ba97SEli Cohen 	} else if (attr_mask & IB_QP_PATH_MTU) {
1656e126ba97SEli Cohen 		if (attr->path_mtu < IB_MTU_256 ||
1657e126ba97SEli Cohen 		    attr->path_mtu > IB_MTU_4096) {
1658e126ba97SEli Cohen 			mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
1659e126ba97SEli Cohen 			err = -EINVAL;
1660e126ba97SEli Cohen 			goto out;
1661e126ba97SEli Cohen 		}
1662938fe83cSSaeed Mahameed 		context->mtu_msgmax = (attr->path_mtu << 5) |
1663938fe83cSSaeed Mahameed 				      (u8)MLX5_CAP_GEN(dev->mdev, log_max_msg);
1664e126ba97SEli Cohen 	}
1665e126ba97SEli Cohen 
1666e126ba97SEli Cohen 	if (attr_mask & IB_QP_DEST_QPN)
1667e126ba97SEli Cohen 		context->log_pg_sz_remote_qpn = cpu_to_be32(attr->dest_qp_num);
1668e126ba97SEli Cohen 
1669e126ba97SEli Cohen 	if (attr_mask & IB_QP_PKEY_INDEX)
1670e126ba97SEli Cohen 		context->pri_path.pkey_index = attr->pkey_index;
1671e126ba97SEli Cohen 
1672e126ba97SEli Cohen 	/* todo implement counter_index functionality */
1673e126ba97SEli Cohen 
1674e126ba97SEli Cohen 	if (is_sqp(ibqp->qp_type))
1675e126ba97SEli Cohen 		context->pri_path.port = qp->port;
1676e126ba97SEli Cohen 
1677e126ba97SEli Cohen 	if (attr_mask & IB_QP_PORT)
1678e126ba97SEli Cohen 		context->pri_path.port = attr->port_num;
1679e126ba97SEli Cohen 
1680e126ba97SEli Cohen 	if (attr_mask & IB_QP_AV) {
1681e126ba97SEli Cohen 		err = mlx5_set_path(dev, &attr->ah_attr, &context->pri_path,
1682e126ba97SEli Cohen 				    attr_mask & IB_QP_PORT ? attr->port_num : qp->port,
1683e126ba97SEli Cohen 				    attr_mask, 0, attr);
1684e126ba97SEli Cohen 		if (err)
1685e126ba97SEli Cohen 			goto out;
1686e126ba97SEli Cohen 	}
1687e126ba97SEli Cohen 
1688e126ba97SEli Cohen 	if (attr_mask & IB_QP_TIMEOUT)
1689e126ba97SEli Cohen 		context->pri_path.ackto_lt |= attr->timeout << 3;
1690e126ba97SEli Cohen 
1691e126ba97SEli Cohen 	if (attr_mask & IB_QP_ALT_PATH) {
1692e126ba97SEli Cohen 		err = mlx5_set_path(dev, &attr->alt_ah_attr, &context->alt_path,
1693e126ba97SEli Cohen 				    attr->alt_port_num, attr_mask, 0, attr);
1694e126ba97SEli Cohen 		if (err)
1695e126ba97SEli Cohen 			goto out;
1696e126ba97SEli Cohen 	}
1697e126ba97SEli Cohen 
1698e126ba97SEli Cohen 	pd = get_pd(qp);
1699e126ba97SEli Cohen 	get_cqs(qp, &send_cq, &recv_cq);
1700e126ba97SEli Cohen 
1701e126ba97SEli Cohen 	context->flags_pd = cpu_to_be32(pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
1702e126ba97SEli Cohen 	context->cqn_send = send_cq ? cpu_to_be32(send_cq->mcq.cqn) : 0;
1703e126ba97SEli Cohen 	context->cqn_recv = recv_cq ? cpu_to_be32(recv_cq->mcq.cqn) : 0;
1704e126ba97SEli Cohen 	context->params1  = cpu_to_be32(MLX5_IB_ACK_REQ_FREQ << 28);
1705e126ba97SEli Cohen 
1706e126ba97SEli Cohen 	if (attr_mask & IB_QP_RNR_RETRY)
1707e126ba97SEli Cohen 		context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
1708e126ba97SEli Cohen 
1709e126ba97SEli Cohen 	if (attr_mask & IB_QP_RETRY_CNT)
1710e126ba97SEli Cohen 		context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
1711e126ba97SEli Cohen 
1712e126ba97SEli Cohen 	if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
1713e126ba97SEli Cohen 		if (attr->max_rd_atomic)
1714e126ba97SEli Cohen 			context->params1 |=
1715e126ba97SEli Cohen 				cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
1716e126ba97SEli Cohen 	}
1717e126ba97SEli Cohen 
1718e126ba97SEli Cohen 	if (attr_mask & IB_QP_SQ_PSN)
1719e126ba97SEli Cohen 		context->next_send_psn = cpu_to_be32(attr->sq_psn);
1720e126ba97SEli Cohen 
1721e126ba97SEli Cohen 	if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
1722e126ba97SEli Cohen 		if (attr->max_dest_rd_atomic)
1723e126ba97SEli Cohen 			context->params2 |=
1724e126ba97SEli Cohen 				cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
1725e126ba97SEli Cohen 	}
1726e126ba97SEli Cohen 
1727e126ba97SEli Cohen 	if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC))
1728e126ba97SEli Cohen 		context->params2 |= to_mlx5_access_flags(qp, attr, attr_mask);
1729e126ba97SEli Cohen 
1730e126ba97SEli Cohen 	if (attr_mask & IB_QP_MIN_RNR_TIMER)
1731e126ba97SEli Cohen 		context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
1732e126ba97SEli Cohen 
1733e126ba97SEli Cohen 	if (attr_mask & IB_QP_RQ_PSN)
1734e126ba97SEli Cohen 		context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
1735e126ba97SEli Cohen 
1736e126ba97SEli Cohen 	if (attr_mask & IB_QP_QKEY)
1737e126ba97SEli Cohen 		context->qkey = cpu_to_be32(attr->qkey);
1738e126ba97SEli Cohen 
1739e126ba97SEli Cohen 	if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
1740e126ba97SEli Cohen 		context->db_rec_addr = cpu_to_be64(qp->db.dma);
1741e126ba97SEli Cohen 
1742e126ba97SEli Cohen 	if (cur_state == IB_QPS_RTS && new_state == IB_QPS_SQD	&&
1743e126ba97SEli Cohen 	    attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY && attr->en_sqd_async_notify)
1744e126ba97SEli Cohen 		sqd_event = 1;
1745e126ba97SEli Cohen 	else
1746e126ba97SEli Cohen 		sqd_event = 0;
1747e126ba97SEli Cohen 
1748e126ba97SEli Cohen 	if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
1749e126ba97SEli Cohen 		context->sq_crq_size |= cpu_to_be16(1 << 4);
1750e126ba97SEli Cohen 
1751e126ba97SEli Cohen 
1752e126ba97SEli Cohen 	mlx5_cur = to_mlx5_state(cur_state);
1753e126ba97SEli Cohen 	mlx5_new = to_mlx5_state(new_state);
1754e126ba97SEli Cohen 	mlx5_st = to_mlx5_st(ibqp->qp_type);
175507c9113fSEli Cohen 	if (mlx5_st < 0)
1756e126ba97SEli Cohen 		goto out;
1757e126ba97SEli Cohen 
17586aec21f6SHaggai Eran 	/* If moving to a reset or error state, we must disable page faults on
17596aec21f6SHaggai Eran 	 * this QP and flush all current page faults. Otherwise a stale page
17606aec21f6SHaggai Eran 	 * fault may attempt to work on this QP after it is reset and moved
17616aec21f6SHaggai Eran 	 * again to RTS, and may cause the driver and the device to get out of
17626aec21f6SHaggai Eran 	 * sync. */
17636aec21f6SHaggai Eran 	if (cur_state != IB_QPS_RESET && cur_state != IB_QPS_ERR &&
17646aec21f6SHaggai Eran 	    (new_state == IB_QPS_RESET || new_state == IB_QPS_ERR))
17656aec21f6SHaggai Eran 		mlx5_ib_qp_disable_pagefaults(qp);
17666aec21f6SHaggai Eran 
1767e126ba97SEli Cohen 	optpar = ib_mask_to_mlx5_opt(attr_mask);
1768e126ba97SEli Cohen 	optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
1769e126ba97SEli Cohen 	in->optparam = cpu_to_be32(optpar);
17709603b61dSJack Morgenstein 	err = mlx5_core_qp_modify(dev->mdev, to_mlx5_state(cur_state),
1771e126ba97SEli Cohen 				  to_mlx5_state(new_state), in, sqd_event,
1772e126ba97SEli Cohen 				  &qp->mqp);
1773e126ba97SEli Cohen 	if (err)
1774e126ba97SEli Cohen 		goto out;
1775e126ba97SEli Cohen 
17766aec21f6SHaggai Eran 	if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
17776aec21f6SHaggai Eran 		mlx5_ib_qp_enable_pagefaults(qp);
17786aec21f6SHaggai Eran 
1779e126ba97SEli Cohen 	qp->state = new_state;
1780e126ba97SEli Cohen 
1781e126ba97SEli Cohen 	if (attr_mask & IB_QP_ACCESS_FLAGS)
1782e126ba97SEli Cohen 		qp->atomic_rd_en = attr->qp_access_flags;
1783e126ba97SEli Cohen 	if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1784e126ba97SEli Cohen 		qp->resp_depth = attr->max_dest_rd_atomic;
1785e126ba97SEli Cohen 	if (attr_mask & IB_QP_PORT)
1786e126ba97SEli Cohen 		qp->port = attr->port_num;
1787e126ba97SEli Cohen 	if (attr_mask & IB_QP_ALT_PATH)
1788e126ba97SEli Cohen 		qp->alt_port = attr->alt_port_num;
1789e126ba97SEli Cohen 
1790e126ba97SEli Cohen 	/*
1791e126ba97SEli Cohen 	 * If we moved a kernel QP to RESET, clean up all old CQ
1792e126ba97SEli Cohen 	 * entries and reinitialize the QP.
1793e126ba97SEli Cohen 	 */
1794e126ba97SEli Cohen 	if (new_state == IB_QPS_RESET && !ibqp->uobject) {
1795e126ba97SEli Cohen 		mlx5_ib_cq_clean(recv_cq, qp->mqp.qpn,
1796e126ba97SEli Cohen 				 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
1797e126ba97SEli Cohen 		if (send_cq != recv_cq)
1798e126ba97SEli Cohen 			mlx5_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
1799e126ba97SEli Cohen 
1800e126ba97SEli Cohen 		qp->rq.head = 0;
1801e126ba97SEli Cohen 		qp->rq.tail = 0;
1802e126ba97SEli Cohen 		qp->sq.head = 0;
1803e126ba97SEli Cohen 		qp->sq.tail = 0;
1804e126ba97SEli Cohen 		qp->sq.cur_post = 0;
1805e126ba97SEli Cohen 		qp->sq.last_poll = 0;
1806e126ba97SEli Cohen 		qp->db.db[MLX5_RCV_DBR] = 0;
1807e126ba97SEli Cohen 		qp->db.db[MLX5_SND_DBR] = 0;
1808e126ba97SEli Cohen 	}
1809e126ba97SEli Cohen 
1810e126ba97SEli Cohen out:
1811e126ba97SEli Cohen 	kfree(in);
1812e126ba97SEli Cohen 	return err;
1813e126ba97SEli Cohen }
1814e126ba97SEli Cohen 
1815e126ba97SEli Cohen int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1816e126ba97SEli Cohen 		      int attr_mask, struct ib_udata *udata)
1817e126ba97SEli Cohen {
1818e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
1819e126ba97SEli Cohen 	struct mlx5_ib_qp *qp = to_mqp(ibqp);
1820e126ba97SEli Cohen 	enum ib_qp_state cur_state, new_state;
1821e126ba97SEli Cohen 	int err = -EINVAL;
1822e126ba97SEli Cohen 	int port;
18232811ba51SAchiad Shochat 	enum rdma_link_layer ll = IB_LINK_LAYER_UNSPECIFIED;
1824e126ba97SEli Cohen 
1825e126ba97SEli Cohen 	mutex_lock(&qp->mutex);
1826e126ba97SEli Cohen 
1827e126ba97SEli Cohen 	cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
1828e126ba97SEli Cohen 	new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
1829e126ba97SEli Cohen 
18302811ba51SAchiad Shochat 	if (!(cur_state == new_state && cur_state == IB_QPS_RESET)) {
18312811ba51SAchiad Shochat 		port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
18322811ba51SAchiad Shochat 		ll = dev->ib_dev.get_link_layer(&dev->ib_dev, port);
18332811ba51SAchiad Shochat 	}
18342811ba51SAchiad Shochat 
1835e126ba97SEli Cohen 	if (ibqp->qp_type != MLX5_IB_QPT_REG_UMR &&
1836dd5f03beSMatan Barak 	    !ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type, attr_mask,
18372811ba51SAchiad Shochat 				ll))
1838e126ba97SEli Cohen 		goto out;
1839e126ba97SEli Cohen 
1840e126ba97SEli Cohen 	if ((attr_mask & IB_QP_PORT) &&
1841938fe83cSSaeed Mahameed 	    (attr->port_num == 0 ||
1842938fe83cSSaeed Mahameed 	     attr->port_num > MLX5_CAP_GEN(dev->mdev, num_ports)))
1843e126ba97SEli Cohen 		goto out;
1844e126ba97SEli Cohen 
1845e126ba97SEli Cohen 	if (attr_mask & IB_QP_PKEY_INDEX) {
1846e126ba97SEli Cohen 		port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
1847938fe83cSSaeed Mahameed 		if (attr->pkey_index >=
1848938fe83cSSaeed Mahameed 		    dev->mdev->port_caps[port - 1].pkey_table_len)
1849e126ba97SEli Cohen 			goto out;
1850e126ba97SEli Cohen 	}
1851e126ba97SEli Cohen 
1852e126ba97SEli Cohen 	if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
1853938fe83cSSaeed Mahameed 	    attr->max_rd_atomic >
1854938fe83cSSaeed Mahameed 	    (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_res_qp)))
1855e126ba97SEli Cohen 		goto out;
1856e126ba97SEli Cohen 
1857e126ba97SEli Cohen 	if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
1858938fe83cSSaeed Mahameed 	    attr->max_dest_rd_atomic >
1859938fe83cSSaeed Mahameed 	    (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_req_qp)))
1860e126ba97SEli Cohen 		goto out;
1861e126ba97SEli Cohen 
1862e126ba97SEli Cohen 	if (cur_state == new_state && cur_state == IB_QPS_RESET) {
1863e126ba97SEli Cohen 		err = 0;
1864e126ba97SEli Cohen 		goto out;
1865e126ba97SEli Cohen 	}
1866e126ba97SEli Cohen 
1867e126ba97SEli Cohen 	err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
1868e126ba97SEli Cohen 
1869e126ba97SEli Cohen out:
1870e126ba97SEli Cohen 	mutex_unlock(&qp->mutex);
1871e126ba97SEli Cohen 	return err;
1872e126ba97SEli Cohen }
1873e126ba97SEli Cohen 
1874e126ba97SEli Cohen static int mlx5_wq_overflow(struct mlx5_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
1875e126ba97SEli Cohen {
1876e126ba97SEli Cohen 	struct mlx5_ib_cq *cq;
1877e126ba97SEli Cohen 	unsigned cur;
1878e126ba97SEli Cohen 
1879e126ba97SEli Cohen 	cur = wq->head - wq->tail;
1880e126ba97SEli Cohen 	if (likely(cur + nreq < wq->max_post))
1881e126ba97SEli Cohen 		return 0;
1882e126ba97SEli Cohen 
1883e126ba97SEli Cohen 	cq = to_mcq(ib_cq);
1884e126ba97SEli Cohen 	spin_lock(&cq->lock);
1885e126ba97SEli Cohen 	cur = wq->head - wq->tail;
1886e126ba97SEli Cohen 	spin_unlock(&cq->lock);
1887e126ba97SEli Cohen 
1888e126ba97SEli Cohen 	return cur + nreq >= wq->max_post;
1889e126ba97SEli Cohen }
1890e126ba97SEli Cohen 
1891e126ba97SEli Cohen static __always_inline void set_raddr_seg(struct mlx5_wqe_raddr_seg *rseg,
1892e126ba97SEli Cohen 					  u64 remote_addr, u32 rkey)
1893e126ba97SEli Cohen {
1894e126ba97SEli Cohen 	rseg->raddr    = cpu_to_be64(remote_addr);
1895e126ba97SEli Cohen 	rseg->rkey     = cpu_to_be32(rkey);
1896e126ba97SEli Cohen 	rseg->reserved = 0;
1897e126ba97SEli Cohen }
1898e126ba97SEli Cohen 
1899e126ba97SEli Cohen static void set_datagram_seg(struct mlx5_wqe_datagram_seg *dseg,
1900e126ba97SEli Cohen 			     struct ib_send_wr *wr)
1901e126ba97SEli Cohen {
1902e622f2f4SChristoph Hellwig 	memcpy(&dseg->av, &to_mah(ud_wr(wr)->ah)->av, sizeof(struct mlx5_av));
1903e622f2f4SChristoph Hellwig 	dseg->av.dqp_dct = cpu_to_be32(ud_wr(wr)->remote_qpn | MLX5_EXTENDED_UD_AV);
1904e622f2f4SChristoph Hellwig 	dseg->av.key.qkey.qkey = cpu_to_be32(ud_wr(wr)->remote_qkey);
1905e126ba97SEli Cohen }
1906e126ba97SEli Cohen 
1907e126ba97SEli Cohen static void set_data_ptr_seg(struct mlx5_wqe_data_seg *dseg, struct ib_sge *sg)
1908e126ba97SEli Cohen {
1909e126ba97SEli Cohen 	dseg->byte_count = cpu_to_be32(sg->length);
1910e126ba97SEli Cohen 	dseg->lkey       = cpu_to_be32(sg->lkey);
1911e126ba97SEli Cohen 	dseg->addr       = cpu_to_be64(sg->addr);
1912e126ba97SEli Cohen }
1913e126ba97SEli Cohen 
1914e126ba97SEli Cohen static __be16 get_klm_octo(int npages)
1915e126ba97SEli Cohen {
1916e126ba97SEli Cohen 	return cpu_to_be16(ALIGN(npages, 8) / 2);
1917e126ba97SEli Cohen }
1918e126ba97SEli Cohen 
1919e126ba97SEli Cohen static __be64 frwr_mkey_mask(void)
1920e126ba97SEli Cohen {
1921e126ba97SEli Cohen 	u64 result;
1922e126ba97SEli Cohen 
1923e126ba97SEli Cohen 	result = MLX5_MKEY_MASK_LEN		|
1924e126ba97SEli Cohen 		MLX5_MKEY_MASK_PAGE_SIZE	|
1925e126ba97SEli Cohen 		MLX5_MKEY_MASK_START_ADDR	|
1926e126ba97SEli Cohen 		MLX5_MKEY_MASK_EN_RINVAL	|
1927e126ba97SEli Cohen 		MLX5_MKEY_MASK_KEY		|
1928e126ba97SEli Cohen 		MLX5_MKEY_MASK_LR		|
1929e126ba97SEli Cohen 		MLX5_MKEY_MASK_LW		|
1930e126ba97SEli Cohen 		MLX5_MKEY_MASK_RR		|
1931e126ba97SEli Cohen 		MLX5_MKEY_MASK_RW		|
1932e126ba97SEli Cohen 		MLX5_MKEY_MASK_A		|
1933e126ba97SEli Cohen 		MLX5_MKEY_MASK_SMALL_FENCE	|
1934e126ba97SEli Cohen 		MLX5_MKEY_MASK_FREE;
1935e126ba97SEli Cohen 
1936e126ba97SEli Cohen 	return cpu_to_be64(result);
1937e126ba97SEli Cohen }
1938e126ba97SEli Cohen 
1939e6631814SSagi Grimberg static __be64 sig_mkey_mask(void)
1940e6631814SSagi Grimberg {
1941e6631814SSagi Grimberg 	u64 result;
1942e6631814SSagi Grimberg 
1943e6631814SSagi Grimberg 	result = MLX5_MKEY_MASK_LEN		|
1944e6631814SSagi Grimberg 		MLX5_MKEY_MASK_PAGE_SIZE	|
1945e6631814SSagi Grimberg 		MLX5_MKEY_MASK_START_ADDR	|
1946d5436ba0SSagi Grimberg 		MLX5_MKEY_MASK_EN_SIGERR	|
1947e6631814SSagi Grimberg 		MLX5_MKEY_MASK_EN_RINVAL	|
1948e6631814SSagi Grimberg 		MLX5_MKEY_MASK_KEY		|
1949e6631814SSagi Grimberg 		MLX5_MKEY_MASK_LR		|
1950e6631814SSagi Grimberg 		MLX5_MKEY_MASK_LW		|
1951e6631814SSagi Grimberg 		MLX5_MKEY_MASK_RR		|
1952e6631814SSagi Grimberg 		MLX5_MKEY_MASK_RW		|
1953e6631814SSagi Grimberg 		MLX5_MKEY_MASK_SMALL_FENCE	|
1954e6631814SSagi Grimberg 		MLX5_MKEY_MASK_FREE		|
1955e6631814SSagi Grimberg 		MLX5_MKEY_MASK_BSF_EN;
1956e6631814SSagi Grimberg 
1957e6631814SSagi Grimberg 	return cpu_to_be64(result);
1958e6631814SSagi Grimberg }
1959e6631814SSagi Grimberg 
19608a187ee5SSagi Grimberg static void set_reg_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr,
19618a187ee5SSagi Grimberg 				struct mlx5_ib_mr *mr)
19628a187ee5SSagi Grimberg {
19638a187ee5SSagi Grimberg 	int ndescs = mr->ndescs;
19648a187ee5SSagi Grimberg 
19658a187ee5SSagi Grimberg 	memset(umr, 0, sizeof(*umr));
19668a187ee5SSagi Grimberg 	umr->flags = MLX5_UMR_CHECK_NOT_FREE;
19678a187ee5SSagi Grimberg 	umr->klm_octowords = get_klm_octo(ndescs);
19688a187ee5SSagi Grimberg 	umr->mkey_mask = frwr_mkey_mask();
19698a187ee5SSagi Grimberg }
19708a187ee5SSagi Grimberg 
1971dd01e66aSSagi Grimberg static void set_linv_umr_seg(struct mlx5_wqe_umr_ctrl_seg *umr)
1972e126ba97SEli Cohen {
1973e126ba97SEli Cohen 	memset(umr, 0, sizeof(*umr));
1974e126ba97SEli Cohen 	umr->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
1975e126ba97SEli Cohen 	umr->flags = 1 << 7;
1976e126ba97SEli Cohen }
1977e126ba97SEli Cohen 
1978968e78ddSHaggai Eran static __be64 get_umr_reg_mr_mask(void)
1979e126ba97SEli Cohen {
1980968e78ddSHaggai Eran 	u64 result;
1981e126ba97SEli Cohen 
1982968e78ddSHaggai Eran 	result = MLX5_MKEY_MASK_LEN		|
1983e126ba97SEli Cohen 		 MLX5_MKEY_MASK_PAGE_SIZE	|
1984e126ba97SEli Cohen 		 MLX5_MKEY_MASK_START_ADDR	|
1985e126ba97SEli Cohen 		 MLX5_MKEY_MASK_PD		|
1986e126ba97SEli Cohen 		 MLX5_MKEY_MASK_LR		|
1987e126ba97SEli Cohen 		 MLX5_MKEY_MASK_LW		|
1988746b5583SEli Cohen 		 MLX5_MKEY_MASK_KEY		|
1989e126ba97SEli Cohen 		 MLX5_MKEY_MASK_RR		|
1990e126ba97SEli Cohen 		 MLX5_MKEY_MASK_RW		|
1991e126ba97SEli Cohen 		 MLX5_MKEY_MASK_A		|
1992e126ba97SEli Cohen 		 MLX5_MKEY_MASK_FREE;
1993968e78ddSHaggai Eran 
1994968e78ddSHaggai Eran 	return cpu_to_be64(result);
1995968e78ddSHaggai Eran }
1996968e78ddSHaggai Eran 
1997968e78ddSHaggai Eran static __be64 get_umr_unreg_mr_mask(void)
1998968e78ddSHaggai Eran {
1999968e78ddSHaggai Eran 	u64 result;
2000968e78ddSHaggai Eran 
2001968e78ddSHaggai Eran 	result = MLX5_MKEY_MASK_FREE;
2002968e78ddSHaggai Eran 
2003968e78ddSHaggai Eran 	return cpu_to_be64(result);
2004968e78ddSHaggai Eran }
2005968e78ddSHaggai Eran 
2006968e78ddSHaggai Eran static __be64 get_umr_update_mtt_mask(void)
2007968e78ddSHaggai Eran {
2008968e78ddSHaggai Eran 	u64 result;
2009968e78ddSHaggai Eran 
2010968e78ddSHaggai Eran 	result = MLX5_MKEY_MASK_FREE;
2011968e78ddSHaggai Eran 
2012968e78ddSHaggai Eran 	return cpu_to_be64(result);
2013968e78ddSHaggai Eran }
2014968e78ddSHaggai Eran 
2015968e78ddSHaggai Eran static void set_reg_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
2016968e78ddSHaggai Eran 				struct ib_send_wr *wr)
2017968e78ddSHaggai Eran {
2018e622f2f4SChristoph Hellwig 	struct mlx5_umr_wr *umrwr = umr_wr(wr);
2019968e78ddSHaggai Eran 
2020968e78ddSHaggai Eran 	memset(umr, 0, sizeof(*umr));
2021968e78ddSHaggai Eran 
2022968e78ddSHaggai Eran 	if (wr->send_flags & MLX5_IB_SEND_UMR_FAIL_IF_FREE)
2023968e78ddSHaggai Eran 		umr->flags = MLX5_UMR_CHECK_FREE; /* fail if free */
2024968e78ddSHaggai Eran 	else
2025968e78ddSHaggai Eran 		umr->flags = MLX5_UMR_CHECK_NOT_FREE; /* fail if not free */
2026968e78ddSHaggai Eran 
2027968e78ddSHaggai Eran 	if (!(wr->send_flags & MLX5_IB_SEND_UMR_UNREG)) {
2028968e78ddSHaggai Eran 		umr->klm_octowords = get_klm_octo(umrwr->npages);
2029968e78ddSHaggai Eran 		if (wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_MTT) {
2030968e78ddSHaggai Eran 			umr->mkey_mask = get_umr_update_mtt_mask();
2031968e78ddSHaggai Eran 			umr->bsf_octowords = get_klm_octo(umrwr->target.offset);
2032968e78ddSHaggai Eran 			umr->flags |= MLX5_UMR_TRANSLATION_OFFSET_EN;
2033e126ba97SEli Cohen 		} else {
2034968e78ddSHaggai Eran 			umr->mkey_mask = get_umr_reg_mr_mask();
2035968e78ddSHaggai Eran 		}
2036968e78ddSHaggai Eran 	} else {
2037968e78ddSHaggai Eran 		umr->mkey_mask = get_umr_unreg_mr_mask();
2038e126ba97SEli Cohen 	}
2039e126ba97SEli Cohen 
2040e126ba97SEli Cohen 	if (!wr->num_sge)
2041968e78ddSHaggai Eran 		umr->flags |= MLX5_UMR_INLINE;
2042e126ba97SEli Cohen }
2043e126ba97SEli Cohen 
2044e126ba97SEli Cohen static u8 get_umr_flags(int acc)
2045e126ba97SEli Cohen {
2046e126ba97SEli Cohen 	return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC       : 0) |
2047e126ba97SEli Cohen 	       (acc & IB_ACCESS_REMOTE_WRITE  ? MLX5_PERM_REMOTE_WRITE : 0) |
2048e126ba97SEli Cohen 	       (acc & IB_ACCESS_REMOTE_READ   ? MLX5_PERM_REMOTE_READ  : 0) |
2049e126ba97SEli Cohen 	       (acc & IB_ACCESS_LOCAL_WRITE   ? MLX5_PERM_LOCAL_WRITE  : 0) |
20502ac45934SSagi Grimberg 		MLX5_PERM_LOCAL_READ | MLX5_PERM_UMR_EN;
2051e126ba97SEli Cohen }
2052e126ba97SEli Cohen 
20538a187ee5SSagi Grimberg static void set_reg_mkey_seg(struct mlx5_mkey_seg *seg,
20548a187ee5SSagi Grimberg 			     struct mlx5_ib_mr *mr,
20558a187ee5SSagi Grimberg 			     u32 key, int access)
20568a187ee5SSagi Grimberg {
20578a187ee5SSagi Grimberg 	int ndescs = ALIGN(mr->ndescs, 8) >> 1;
20588a187ee5SSagi Grimberg 
20598a187ee5SSagi Grimberg 	memset(seg, 0, sizeof(*seg));
20608a187ee5SSagi Grimberg 	seg->flags = get_umr_flags(access) | MLX5_ACCESS_MODE_MTT;
20618a187ee5SSagi Grimberg 	seg->qpn_mkey7_0 = cpu_to_be32((key & 0xff) | 0xffffff00);
20628a187ee5SSagi Grimberg 	seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL);
20638a187ee5SSagi Grimberg 	seg->start_addr = cpu_to_be64(mr->ibmr.iova);
20648a187ee5SSagi Grimberg 	seg->len = cpu_to_be64(mr->ibmr.length);
20658a187ee5SSagi Grimberg 	seg->xlt_oct_size = cpu_to_be32(ndescs);
20668a187ee5SSagi Grimberg 	seg->log2_page_size = ilog2(mr->ibmr.page_size);
20678a187ee5SSagi Grimberg }
20688a187ee5SSagi Grimberg 
2069dd01e66aSSagi Grimberg static void set_linv_mkey_seg(struct mlx5_mkey_seg *seg)
2070e126ba97SEli Cohen {
2071e126ba97SEli Cohen 	memset(seg, 0, sizeof(*seg));
2072968e78ddSHaggai Eran 	seg->status = MLX5_MKEY_STATUS_FREE;
2073e126ba97SEli Cohen }
2074e126ba97SEli Cohen 
2075e126ba97SEli Cohen static void set_reg_mkey_segment(struct mlx5_mkey_seg *seg, struct ib_send_wr *wr)
2076e126ba97SEli Cohen {
2077e622f2f4SChristoph Hellwig 	struct mlx5_umr_wr *umrwr = umr_wr(wr);
2078968e78ddSHaggai Eran 
2079e126ba97SEli Cohen 	memset(seg, 0, sizeof(*seg));
2080e126ba97SEli Cohen 	if (wr->send_flags & MLX5_IB_SEND_UMR_UNREG) {
2081968e78ddSHaggai Eran 		seg->status = MLX5_MKEY_STATUS_FREE;
2082e126ba97SEli Cohen 		return;
2083e126ba97SEli Cohen 	}
2084e126ba97SEli Cohen 
2085968e78ddSHaggai Eran 	seg->flags = convert_access(umrwr->access_flags);
2086968e78ddSHaggai Eran 	if (!(wr->send_flags & MLX5_IB_SEND_UMR_UPDATE_MTT)) {
2087968e78ddSHaggai Eran 		seg->flags_pd = cpu_to_be32(to_mpd(umrwr->pd)->pdn);
2088968e78ddSHaggai Eran 		seg->start_addr = cpu_to_be64(umrwr->target.virt_addr);
2089968e78ddSHaggai Eran 	}
2090968e78ddSHaggai Eran 	seg->len = cpu_to_be64(umrwr->length);
2091968e78ddSHaggai Eran 	seg->log2_page_size = umrwr->page_shift;
2092746b5583SEli Cohen 	seg->qpn_mkey7_0 = cpu_to_be32(0xffffff00 |
2093968e78ddSHaggai Eran 				       mlx5_mkey_variant(umrwr->mkey));
2094e126ba97SEli Cohen }
2095e126ba97SEli Cohen 
20968a187ee5SSagi Grimberg static void set_reg_data_seg(struct mlx5_wqe_data_seg *dseg,
20978a187ee5SSagi Grimberg 			     struct mlx5_ib_mr *mr,
20988a187ee5SSagi Grimberg 			     struct mlx5_ib_pd *pd)
20998a187ee5SSagi Grimberg {
21008a187ee5SSagi Grimberg 	int bcount = mr->desc_size * mr->ndescs;
21018a187ee5SSagi Grimberg 
21028a187ee5SSagi Grimberg 	dseg->addr = cpu_to_be64(mr->desc_map);
21038a187ee5SSagi Grimberg 	dseg->byte_count = cpu_to_be32(ALIGN(bcount, 64));
21048a187ee5SSagi Grimberg 	dseg->lkey = cpu_to_be32(pd->ibpd.local_dma_lkey);
21058a187ee5SSagi Grimberg }
21068a187ee5SSagi Grimberg 
2107e126ba97SEli Cohen static __be32 send_ieth(struct ib_send_wr *wr)
2108e126ba97SEli Cohen {
2109e126ba97SEli Cohen 	switch (wr->opcode) {
2110e126ba97SEli Cohen 	case IB_WR_SEND_WITH_IMM:
2111e126ba97SEli Cohen 	case IB_WR_RDMA_WRITE_WITH_IMM:
2112e126ba97SEli Cohen 		return wr->ex.imm_data;
2113e126ba97SEli Cohen 
2114e126ba97SEli Cohen 	case IB_WR_SEND_WITH_INV:
2115e126ba97SEli Cohen 		return cpu_to_be32(wr->ex.invalidate_rkey);
2116e126ba97SEli Cohen 
2117e126ba97SEli Cohen 	default:
2118e126ba97SEli Cohen 		return 0;
2119e126ba97SEli Cohen 	}
2120e126ba97SEli Cohen }
2121e126ba97SEli Cohen 
2122e126ba97SEli Cohen static u8 calc_sig(void *wqe, int size)
2123e126ba97SEli Cohen {
2124e126ba97SEli Cohen 	u8 *p = wqe;
2125e126ba97SEli Cohen 	u8 res = 0;
2126e126ba97SEli Cohen 	int i;
2127e126ba97SEli Cohen 
2128e126ba97SEli Cohen 	for (i = 0; i < size; i++)
2129e126ba97SEli Cohen 		res ^= p[i];
2130e126ba97SEli Cohen 
2131e126ba97SEli Cohen 	return ~res;
2132e126ba97SEli Cohen }
2133e126ba97SEli Cohen 
2134e126ba97SEli Cohen static u8 wq_sig(void *wqe)
2135e126ba97SEli Cohen {
2136e126ba97SEli Cohen 	return calc_sig(wqe, (*((u8 *)wqe + 8) & 0x3f) << 4);
2137e126ba97SEli Cohen }
2138e126ba97SEli Cohen 
2139e126ba97SEli Cohen static int set_data_inl_seg(struct mlx5_ib_qp *qp, struct ib_send_wr *wr,
2140e126ba97SEli Cohen 			    void *wqe, int *sz)
2141e126ba97SEli Cohen {
2142e126ba97SEli Cohen 	struct mlx5_wqe_inline_seg *seg;
2143e126ba97SEli Cohen 	void *qend = qp->sq.qend;
2144e126ba97SEli Cohen 	void *addr;
2145e126ba97SEli Cohen 	int inl = 0;
2146e126ba97SEli Cohen 	int copy;
2147e126ba97SEli Cohen 	int len;
2148e126ba97SEli Cohen 	int i;
2149e126ba97SEli Cohen 
2150e126ba97SEli Cohen 	seg = wqe;
2151e126ba97SEli Cohen 	wqe += sizeof(*seg);
2152e126ba97SEli Cohen 	for (i = 0; i < wr->num_sge; i++) {
2153e126ba97SEli Cohen 		addr = (void *)(unsigned long)(wr->sg_list[i].addr);
2154e126ba97SEli Cohen 		len  = wr->sg_list[i].length;
2155e126ba97SEli Cohen 		inl += len;
2156e126ba97SEli Cohen 
2157e126ba97SEli Cohen 		if (unlikely(inl > qp->max_inline_data))
2158e126ba97SEli Cohen 			return -ENOMEM;
2159e126ba97SEli Cohen 
2160e126ba97SEli Cohen 		if (unlikely(wqe + len > qend)) {
2161e126ba97SEli Cohen 			copy = qend - wqe;
2162e126ba97SEli Cohen 			memcpy(wqe, addr, copy);
2163e126ba97SEli Cohen 			addr += copy;
2164e126ba97SEli Cohen 			len -= copy;
2165e126ba97SEli Cohen 			wqe = mlx5_get_send_wqe(qp, 0);
2166e126ba97SEli Cohen 		}
2167e126ba97SEli Cohen 		memcpy(wqe, addr, len);
2168e126ba97SEli Cohen 		wqe += len;
2169e126ba97SEli Cohen 	}
2170e126ba97SEli Cohen 
2171e126ba97SEli Cohen 	seg->byte_count = cpu_to_be32(inl | MLX5_INLINE_SEG);
2172e126ba97SEli Cohen 
2173e126ba97SEli Cohen 	*sz = ALIGN(inl + sizeof(seg->byte_count), 16) / 16;
2174e126ba97SEli Cohen 
2175e126ba97SEli Cohen 	return 0;
2176e126ba97SEli Cohen }
2177e126ba97SEli Cohen 
2178e6631814SSagi Grimberg static u16 prot_field_size(enum ib_signature_type type)
2179e6631814SSagi Grimberg {
2180e6631814SSagi Grimberg 	switch (type) {
2181e6631814SSagi Grimberg 	case IB_SIG_TYPE_T10_DIF:
2182e6631814SSagi Grimberg 		return MLX5_DIF_SIZE;
2183e6631814SSagi Grimberg 	default:
2184e6631814SSagi Grimberg 		return 0;
2185e6631814SSagi Grimberg 	}
2186e6631814SSagi Grimberg }
2187e6631814SSagi Grimberg 
2188e6631814SSagi Grimberg static u8 bs_selector(int block_size)
2189e6631814SSagi Grimberg {
2190e6631814SSagi Grimberg 	switch (block_size) {
2191e6631814SSagi Grimberg 	case 512:	    return 0x1;
2192e6631814SSagi Grimberg 	case 520:	    return 0x2;
2193e6631814SSagi Grimberg 	case 4096:	    return 0x3;
2194e6631814SSagi Grimberg 	case 4160:	    return 0x4;
2195e6631814SSagi Grimberg 	case 1073741824:    return 0x5;
2196e6631814SSagi Grimberg 	default:	    return 0;
2197e6631814SSagi Grimberg 	}
2198e6631814SSagi Grimberg }
2199e6631814SSagi Grimberg 
220078eda2bbSSagi Grimberg static void mlx5_fill_inl_bsf(struct ib_sig_domain *domain,
2201142537f4SSagi Grimberg 			      struct mlx5_bsf_inl *inl)
2202e6631814SSagi Grimberg {
2203142537f4SSagi Grimberg 	/* Valid inline section and allow BSF refresh */
2204142537f4SSagi Grimberg 	inl->vld_refresh = cpu_to_be16(MLX5_BSF_INL_VALID |
2205142537f4SSagi Grimberg 				       MLX5_BSF_REFRESH_DIF);
2206142537f4SSagi Grimberg 	inl->dif_apptag = cpu_to_be16(domain->sig.dif.app_tag);
2207142537f4SSagi Grimberg 	inl->dif_reftag = cpu_to_be32(domain->sig.dif.ref_tag);
2208142537f4SSagi Grimberg 	/* repeating block */
2209142537f4SSagi Grimberg 	inl->rp_inv_seed = MLX5_BSF_REPEAT_BLOCK;
2210142537f4SSagi Grimberg 	inl->sig_type = domain->sig.dif.bg_type == IB_T10DIF_CRC ?
2211142537f4SSagi Grimberg 			MLX5_DIF_CRC : MLX5_DIF_IPCS;
2212e6631814SSagi Grimberg 
221378eda2bbSSagi Grimberg 	if (domain->sig.dif.ref_remap)
221478eda2bbSSagi Grimberg 		inl->dif_inc_ref_guard_check |= MLX5_BSF_INC_REFTAG;
2215e6631814SSagi Grimberg 
221678eda2bbSSagi Grimberg 	if (domain->sig.dif.app_escape) {
221778eda2bbSSagi Grimberg 		if (domain->sig.dif.ref_escape)
221878eda2bbSSagi Grimberg 			inl->dif_inc_ref_guard_check |= MLX5_BSF_APPREF_ESCAPE;
221978eda2bbSSagi Grimberg 		else
222078eda2bbSSagi Grimberg 			inl->dif_inc_ref_guard_check |= MLX5_BSF_APPTAG_ESCAPE;
2221e6631814SSagi Grimberg 	}
2222e6631814SSagi Grimberg 
222378eda2bbSSagi Grimberg 	inl->dif_app_bitmask_check =
222478eda2bbSSagi Grimberg 		cpu_to_be16(domain->sig.dif.apptag_check_mask);
2225e6631814SSagi Grimberg }
2226e6631814SSagi Grimberg 
2227e6631814SSagi Grimberg static int mlx5_set_bsf(struct ib_mr *sig_mr,
2228e6631814SSagi Grimberg 			struct ib_sig_attrs *sig_attrs,
2229e6631814SSagi Grimberg 			struct mlx5_bsf *bsf, u32 data_size)
2230e6631814SSagi Grimberg {
2231e6631814SSagi Grimberg 	struct mlx5_core_sig_ctx *msig = to_mmr(sig_mr)->sig;
2232e6631814SSagi Grimberg 	struct mlx5_bsf_basic *basic = &bsf->basic;
2233e6631814SSagi Grimberg 	struct ib_sig_domain *mem = &sig_attrs->mem;
2234e6631814SSagi Grimberg 	struct ib_sig_domain *wire = &sig_attrs->wire;
2235e6631814SSagi Grimberg 
2236c7f44fbdSSagi Grimberg 	memset(bsf, 0, sizeof(*bsf));
2237e6631814SSagi Grimberg 
2238142537f4SSagi Grimberg 	/* Basic + Extended + Inline */
2239142537f4SSagi Grimberg 	basic->bsf_size_sbs = 1 << 7;
2240e6631814SSagi Grimberg 	/* Input domain check byte mask */
2241e6631814SSagi Grimberg 	basic->check_byte_mask = sig_attrs->check_mask;
224278eda2bbSSagi Grimberg 	basic->raw_data_size = cpu_to_be32(data_size);
224378eda2bbSSagi Grimberg 
224478eda2bbSSagi Grimberg 	/* Memory domain */
224578eda2bbSSagi Grimberg 	switch (sig_attrs->mem.sig_type) {
224678eda2bbSSagi Grimberg 	case IB_SIG_TYPE_NONE:
224778eda2bbSSagi Grimberg 		break;
224878eda2bbSSagi Grimberg 	case IB_SIG_TYPE_T10_DIF:
224978eda2bbSSagi Grimberg 		basic->mem.bs_selector = bs_selector(mem->sig.dif.pi_interval);
225078eda2bbSSagi Grimberg 		basic->m_bfs_psv = cpu_to_be32(msig->psv_memory.psv_idx);
225178eda2bbSSagi Grimberg 		mlx5_fill_inl_bsf(mem, &bsf->m_inl);
225278eda2bbSSagi Grimberg 		break;
225378eda2bbSSagi Grimberg 	default:
225478eda2bbSSagi Grimberg 		return -EINVAL;
225578eda2bbSSagi Grimberg 	}
225678eda2bbSSagi Grimberg 
225778eda2bbSSagi Grimberg 	/* Wire domain */
225878eda2bbSSagi Grimberg 	switch (sig_attrs->wire.sig_type) {
225978eda2bbSSagi Grimberg 	case IB_SIG_TYPE_NONE:
226078eda2bbSSagi Grimberg 		break;
226178eda2bbSSagi Grimberg 	case IB_SIG_TYPE_T10_DIF:
2262e6631814SSagi Grimberg 		if (mem->sig.dif.pi_interval == wire->sig.dif.pi_interval &&
226378eda2bbSSagi Grimberg 		    mem->sig_type == wire->sig_type) {
2264e6631814SSagi Grimberg 			/* Same block structure */
2265142537f4SSagi Grimberg 			basic->bsf_size_sbs |= 1 << 4;
2266e6631814SSagi Grimberg 			if (mem->sig.dif.bg_type == wire->sig.dif.bg_type)
2267fd22f78cSSagi Grimberg 				basic->wire.copy_byte_mask |= MLX5_CPY_GRD_MASK;
2268c7f44fbdSSagi Grimberg 			if (mem->sig.dif.app_tag == wire->sig.dif.app_tag)
2269fd22f78cSSagi Grimberg 				basic->wire.copy_byte_mask |= MLX5_CPY_APP_MASK;
2270c7f44fbdSSagi Grimberg 			if (mem->sig.dif.ref_tag == wire->sig.dif.ref_tag)
2271fd22f78cSSagi Grimberg 				basic->wire.copy_byte_mask |= MLX5_CPY_REF_MASK;
2272e6631814SSagi Grimberg 		} else
2273e6631814SSagi Grimberg 			basic->wire.bs_selector = bs_selector(wire->sig.dif.pi_interval);
2274e6631814SSagi Grimberg 
2275142537f4SSagi Grimberg 		basic->w_bfs_psv = cpu_to_be32(msig->psv_wire.psv_idx);
227678eda2bbSSagi Grimberg 		mlx5_fill_inl_bsf(wire, &bsf->w_inl);
2277e6631814SSagi Grimberg 		break;
2278e6631814SSagi Grimberg 	default:
2279e6631814SSagi Grimberg 		return -EINVAL;
2280e6631814SSagi Grimberg 	}
2281e6631814SSagi Grimberg 
2282e6631814SSagi Grimberg 	return 0;
2283e6631814SSagi Grimberg }
2284e6631814SSagi Grimberg 
2285e622f2f4SChristoph Hellwig static int set_sig_data_segment(struct ib_sig_handover_wr *wr,
2286e622f2f4SChristoph Hellwig 				struct mlx5_ib_qp *qp, void **seg, int *size)
2287e6631814SSagi Grimberg {
2288e622f2f4SChristoph Hellwig 	struct ib_sig_attrs *sig_attrs = wr->sig_attrs;
2289e622f2f4SChristoph Hellwig 	struct ib_mr *sig_mr = wr->sig_mr;
2290e6631814SSagi Grimberg 	struct mlx5_bsf *bsf;
2291e622f2f4SChristoph Hellwig 	u32 data_len = wr->wr.sg_list->length;
2292e622f2f4SChristoph Hellwig 	u32 data_key = wr->wr.sg_list->lkey;
2293e622f2f4SChristoph Hellwig 	u64 data_va = wr->wr.sg_list->addr;
2294e6631814SSagi Grimberg 	int ret;
2295e6631814SSagi Grimberg 	int wqe_size;
2296e6631814SSagi Grimberg 
2297e622f2f4SChristoph Hellwig 	if (!wr->prot ||
2298e622f2f4SChristoph Hellwig 	    (data_key == wr->prot->lkey &&
2299e622f2f4SChristoph Hellwig 	     data_va == wr->prot->addr &&
2300e622f2f4SChristoph Hellwig 	     data_len == wr->prot->length)) {
2301e6631814SSagi Grimberg 		/**
2302e6631814SSagi Grimberg 		 * Source domain doesn't contain signature information
23035c273b16SSagi Grimberg 		 * or data and protection are interleaved in memory.
2304e6631814SSagi Grimberg 		 * So need construct:
2305e6631814SSagi Grimberg 		 *                  ------------------
2306e6631814SSagi Grimberg 		 *                 |     data_klm     |
2307e6631814SSagi Grimberg 		 *                  ------------------
2308e6631814SSagi Grimberg 		 *                 |       BSF        |
2309e6631814SSagi Grimberg 		 *                  ------------------
2310e6631814SSagi Grimberg 		 **/
2311e6631814SSagi Grimberg 		struct mlx5_klm *data_klm = *seg;
2312e6631814SSagi Grimberg 
2313e6631814SSagi Grimberg 		data_klm->bcount = cpu_to_be32(data_len);
2314e6631814SSagi Grimberg 		data_klm->key = cpu_to_be32(data_key);
2315e6631814SSagi Grimberg 		data_klm->va = cpu_to_be64(data_va);
2316e6631814SSagi Grimberg 		wqe_size = ALIGN(sizeof(*data_klm), 64);
2317e6631814SSagi Grimberg 	} else {
2318e6631814SSagi Grimberg 		/**
2319e6631814SSagi Grimberg 		 * Source domain contains signature information
2320e6631814SSagi Grimberg 		 * So need construct a strided block format:
2321e6631814SSagi Grimberg 		 *               ---------------------------
2322e6631814SSagi Grimberg 		 *              |     stride_block_ctrl     |
2323e6631814SSagi Grimberg 		 *               ---------------------------
2324e6631814SSagi Grimberg 		 *              |          data_klm         |
2325e6631814SSagi Grimberg 		 *               ---------------------------
2326e6631814SSagi Grimberg 		 *              |          prot_klm         |
2327e6631814SSagi Grimberg 		 *               ---------------------------
2328e6631814SSagi Grimberg 		 *              |             BSF           |
2329e6631814SSagi Grimberg 		 *               ---------------------------
2330e6631814SSagi Grimberg 		 **/
2331e6631814SSagi Grimberg 		struct mlx5_stride_block_ctrl_seg *sblock_ctrl;
2332e6631814SSagi Grimberg 		struct mlx5_stride_block_entry *data_sentry;
2333e6631814SSagi Grimberg 		struct mlx5_stride_block_entry *prot_sentry;
2334e622f2f4SChristoph Hellwig 		u32 prot_key = wr->prot->lkey;
2335e622f2f4SChristoph Hellwig 		u64 prot_va = wr->prot->addr;
2336e6631814SSagi Grimberg 		u16 block_size = sig_attrs->mem.sig.dif.pi_interval;
2337e6631814SSagi Grimberg 		int prot_size;
2338e6631814SSagi Grimberg 
2339e6631814SSagi Grimberg 		sblock_ctrl = *seg;
2340e6631814SSagi Grimberg 		data_sentry = (void *)sblock_ctrl + sizeof(*sblock_ctrl);
2341e6631814SSagi Grimberg 		prot_sentry = (void *)data_sentry + sizeof(*data_sentry);
2342e6631814SSagi Grimberg 
2343e6631814SSagi Grimberg 		prot_size = prot_field_size(sig_attrs->mem.sig_type);
2344e6631814SSagi Grimberg 		if (!prot_size) {
2345e6631814SSagi Grimberg 			pr_err("Bad block size given: %u\n", block_size);
2346e6631814SSagi Grimberg 			return -EINVAL;
2347e6631814SSagi Grimberg 		}
2348e6631814SSagi Grimberg 		sblock_ctrl->bcount_per_cycle = cpu_to_be32(block_size +
2349e6631814SSagi Grimberg 							    prot_size);
2350e6631814SSagi Grimberg 		sblock_ctrl->op = cpu_to_be32(MLX5_STRIDE_BLOCK_OP);
2351e6631814SSagi Grimberg 		sblock_ctrl->repeat_count = cpu_to_be32(data_len / block_size);
2352e6631814SSagi Grimberg 		sblock_ctrl->num_entries = cpu_to_be16(2);
2353e6631814SSagi Grimberg 
2354e6631814SSagi Grimberg 		data_sentry->bcount = cpu_to_be16(block_size);
2355e6631814SSagi Grimberg 		data_sentry->key = cpu_to_be32(data_key);
2356e6631814SSagi Grimberg 		data_sentry->va = cpu_to_be64(data_va);
23575c273b16SSagi Grimberg 		data_sentry->stride = cpu_to_be16(block_size);
23585c273b16SSagi Grimberg 
2359e6631814SSagi Grimberg 		prot_sentry->bcount = cpu_to_be16(prot_size);
2360e6631814SSagi Grimberg 		prot_sentry->key = cpu_to_be32(prot_key);
2361e6631814SSagi Grimberg 		prot_sentry->va = cpu_to_be64(prot_va);
2362e6631814SSagi Grimberg 		prot_sentry->stride = cpu_to_be16(prot_size);
23635c273b16SSagi Grimberg 
2364e6631814SSagi Grimberg 		wqe_size = ALIGN(sizeof(*sblock_ctrl) + sizeof(*data_sentry) +
2365e6631814SSagi Grimberg 				 sizeof(*prot_sentry), 64);
2366e6631814SSagi Grimberg 	}
2367e6631814SSagi Grimberg 
2368e6631814SSagi Grimberg 	*seg += wqe_size;
2369e6631814SSagi Grimberg 	*size += wqe_size / 16;
2370e6631814SSagi Grimberg 	if (unlikely((*seg == qp->sq.qend)))
2371e6631814SSagi Grimberg 		*seg = mlx5_get_send_wqe(qp, 0);
2372e6631814SSagi Grimberg 
2373e6631814SSagi Grimberg 	bsf = *seg;
2374e6631814SSagi Grimberg 	ret = mlx5_set_bsf(sig_mr, sig_attrs, bsf, data_len);
2375e6631814SSagi Grimberg 	if (ret)
2376e6631814SSagi Grimberg 		return -EINVAL;
2377e6631814SSagi Grimberg 
2378e6631814SSagi Grimberg 	*seg += sizeof(*bsf);
2379e6631814SSagi Grimberg 	*size += sizeof(*bsf) / 16;
2380e6631814SSagi Grimberg 	if (unlikely((*seg == qp->sq.qend)))
2381e6631814SSagi Grimberg 		*seg = mlx5_get_send_wqe(qp, 0);
2382e6631814SSagi Grimberg 
2383e6631814SSagi Grimberg 	return 0;
2384e6631814SSagi Grimberg }
2385e6631814SSagi Grimberg 
2386e6631814SSagi Grimberg static void set_sig_mkey_segment(struct mlx5_mkey_seg *seg,
2387e622f2f4SChristoph Hellwig 				 struct ib_sig_handover_wr *wr, u32 nelements,
2388e6631814SSagi Grimberg 				 u32 length, u32 pdn)
2389e6631814SSagi Grimberg {
2390e622f2f4SChristoph Hellwig 	struct ib_mr *sig_mr = wr->sig_mr;
2391e6631814SSagi Grimberg 	u32 sig_key = sig_mr->rkey;
2392d5436ba0SSagi Grimberg 	u8 sigerr = to_mmr(sig_mr)->sig->sigerr_count & 1;
2393e6631814SSagi Grimberg 
2394e6631814SSagi Grimberg 	memset(seg, 0, sizeof(*seg));
2395e6631814SSagi Grimberg 
2396e622f2f4SChristoph Hellwig 	seg->flags = get_umr_flags(wr->access_flags) |
2397e6631814SSagi Grimberg 				   MLX5_ACCESS_MODE_KLM;
2398e6631814SSagi Grimberg 	seg->qpn_mkey7_0 = cpu_to_be32((sig_key & 0xff) | 0xffffff00);
2399d5436ba0SSagi Grimberg 	seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL | sigerr << 26 |
2400e6631814SSagi Grimberg 				    MLX5_MKEY_BSF_EN | pdn);
2401e6631814SSagi Grimberg 	seg->len = cpu_to_be64(length);
2402e6631814SSagi Grimberg 	seg->xlt_oct_size = cpu_to_be32(be16_to_cpu(get_klm_octo(nelements)));
2403e6631814SSagi Grimberg 	seg->bsfs_octo_size = cpu_to_be32(MLX5_MKEY_BSF_OCTO_SIZE);
2404e6631814SSagi Grimberg }
2405e6631814SSagi Grimberg 
2406e6631814SSagi Grimberg static void set_sig_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
2407e622f2f4SChristoph Hellwig 				u32 nelements)
2408e6631814SSagi Grimberg {
2409e6631814SSagi Grimberg 	memset(umr, 0, sizeof(*umr));
2410e6631814SSagi Grimberg 
2411e6631814SSagi Grimberg 	umr->flags = MLX5_FLAGS_INLINE | MLX5_FLAGS_CHECK_FREE;
2412e6631814SSagi Grimberg 	umr->klm_octowords = get_klm_octo(nelements);
2413e6631814SSagi Grimberg 	umr->bsf_octowords = cpu_to_be16(MLX5_MKEY_BSF_OCTO_SIZE);
2414e6631814SSagi Grimberg 	umr->mkey_mask = sig_mkey_mask();
2415e6631814SSagi Grimberg }
2416e6631814SSagi Grimberg 
2417e6631814SSagi Grimberg 
2418e622f2f4SChristoph Hellwig static int set_sig_umr_wr(struct ib_send_wr *send_wr, struct mlx5_ib_qp *qp,
2419e6631814SSagi Grimberg 			  void **seg, int *size)
2420e6631814SSagi Grimberg {
2421e622f2f4SChristoph Hellwig 	struct ib_sig_handover_wr *wr = sig_handover_wr(send_wr);
2422e622f2f4SChristoph Hellwig 	struct mlx5_ib_mr *sig_mr = to_mmr(wr->sig_mr);
2423e6631814SSagi Grimberg 	u32 pdn = get_pd(qp)->pdn;
2424e6631814SSagi Grimberg 	u32 klm_oct_size;
2425e6631814SSagi Grimberg 	int region_len, ret;
2426e6631814SSagi Grimberg 
2427e622f2f4SChristoph Hellwig 	if (unlikely(wr->wr.num_sge != 1) ||
2428e622f2f4SChristoph Hellwig 	    unlikely(wr->access_flags & IB_ACCESS_REMOTE_ATOMIC) ||
2429d5436ba0SSagi Grimberg 	    unlikely(!sig_mr->sig) || unlikely(!qp->signature_en) ||
2430d5436ba0SSagi Grimberg 	    unlikely(!sig_mr->sig->sig_status_checked))
2431e6631814SSagi Grimberg 		return -EINVAL;
2432e6631814SSagi Grimberg 
2433e6631814SSagi Grimberg 	/* length of the protected region, data + protection */
2434e622f2f4SChristoph Hellwig 	region_len = wr->wr.sg_list->length;
2435e622f2f4SChristoph Hellwig 	if (wr->prot &&
2436e622f2f4SChristoph Hellwig 	    (wr->prot->lkey != wr->wr.sg_list->lkey  ||
2437e622f2f4SChristoph Hellwig 	     wr->prot->addr != wr->wr.sg_list->addr  ||
2438e622f2f4SChristoph Hellwig 	     wr->prot->length != wr->wr.sg_list->length))
2439e622f2f4SChristoph Hellwig 		region_len += wr->prot->length;
2440e6631814SSagi Grimberg 
2441e6631814SSagi Grimberg 	/**
2442e6631814SSagi Grimberg 	 * KLM octoword size - if protection was provided
2443e6631814SSagi Grimberg 	 * then we use strided block format (3 octowords),
2444e6631814SSagi Grimberg 	 * else we use single KLM (1 octoword)
2445e6631814SSagi Grimberg 	 **/
2446e622f2f4SChristoph Hellwig 	klm_oct_size = wr->prot ? 3 : 1;
2447e6631814SSagi Grimberg 
2448e622f2f4SChristoph Hellwig 	set_sig_umr_segment(*seg, klm_oct_size);
2449e6631814SSagi Grimberg 	*seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
2450e6631814SSagi Grimberg 	*size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
2451e6631814SSagi Grimberg 	if (unlikely((*seg == qp->sq.qend)))
2452e6631814SSagi Grimberg 		*seg = mlx5_get_send_wqe(qp, 0);
2453e6631814SSagi Grimberg 
2454e6631814SSagi Grimberg 	set_sig_mkey_segment(*seg, wr, klm_oct_size, region_len, pdn);
2455e6631814SSagi Grimberg 	*seg += sizeof(struct mlx5_mkey_seg);
2456e6631814SSagi Grimberg 	*size += sizeof(struct mlx5_mkey_seg) / 16;
2457e6631814SSagi Grimberg 	if (unlikely((*seg == qp->sq.qend)))
2458e6631814SSagi Grimberg 		*seg = mlx5_get_send_wqe(qp, 0);
2459e6631814SSagi Grimberg 
2460e6631814SSagi Grimberg 	ret = set_sig_data_segment(wr, qp, seg, size);
2461e6631814SSagi Grimberg 	if (ret)
2462e6631814SSagi Grimberg 		return ret;
2463e6631814SSagi Grimberg 
2464d5436ba0SSagi Grimberg 	sig_mr->sig->sig_status_checked = false;
2465e6631814SSagi Grimberg 	return 0;
2466e6631814SSagi Grimberg }
2467e6631814SSagi Grimberg 
2468e6631814SSagi Grimberg static int set_psv_wr(struct ib_sig_domain *domain,
2469e6631814SSagi Grimberg 		      u32 psv_idx, void **seg, int *size)
2470e6631814SSagi Grimberg {
2471e6631814SSagi Grimberg 	struct mlx5_seg_set_psv *psv_seg = *seg;
2472e6631814SSagi Grimberg 
2473e6631814SSagi Grimberg 	memset(psv_seg, 0, sizeof(*psv_seg));
2474e6631814SSagi Grimberg 	psv_seg->psv_num = cpu_to_be32(psv_idx);
2475e6631814SSagi Grimberg 	switch (domain->sig_type) {
247678eda2bbSSagi Grimberg 	case IB_SIG_TYPE_NONE:
247778eda2bbSSagi Grimberg 		break;
2478e6631814SSagi Grimberg 	case IB_SIG_TYPE_T10_DIF:
2479e6631814SSagi Grimberg 		psv_seg->transient_sig = cpu_to_be32(domain->sig.dif.bg << 16 |
2480e6631814SSagi Grimberg 						     domain->sig.dif.app_tag);
2481e6631814SSagi Grimberg 		psv_seg->ref_tag = cpu_to_be32(domain->sig.dif.ref_tag);
2482e6631814SSagi Grimberg 		break;
2483e6631814SSagi Grimberg 	default:
2484e6631814SSagi Grimberg 		pr_err("Bad signature type given.\n");
2485e6631814SSagi Grimberg 		return 1;
2486e6631814SSagi Grimberg 	}
2487e6631814SSagi Grimberg 
248878eda2bbSSagi Grimberg 	*seg += sizeof(*psv_seg);
248978eda2bbSSagi Grimberg 	*size += sizeof(*psv_seg) / 16;
249078eda2bbSSagi Grimberg 
2491e6631814SSagi Grimberg 	return 0;
2492e6631814SSagi Grimberg }
2493e6631814SSagi Grimberg 
24948a187ee5SSagi Grimberg static int set_reg_wr(struct mlx5_ib_qp *qp,
24958a187ee5SSagi Grimberg 		      struct ib_reg_wr *wr,
24968a187ee5SSagi Grimberg 		      void **seg, int *size)
24978a187ee5SSagi Grimberg {
24988a187ee5SSagi Grimberg 	struct mlx5_ib_mr *mr = to_mmr(wr->mr);
24998a187ee5SSagi Grimberg 	struct mlx5_ib_pd *pd = to_mpd(qp->ibqp.pd);
25008a187ee5SSagi Grimberg 
25018a187ee5SSagi Grimberg 	if (unlikely(wr->wr.send_flags & IB_SEND_INLINE)) {
25028a187ee5SSagi Grimberg 		mlx5_ib_warn(to_mdev(qp->ibqp.device),
25038a187ee5SSagi Grimberg 			     "Invalid IB_SEND_INLINE send flag\n");
25048a187ee5SSagi Grimberg 		return -EINVAL;
25058a187ee5SSagi Grimberg 	}
25068a187ee5SSagi Grimberg 
25078a187ee5SSagi Grimberg 	set_reg_umr_seg(*seg, mr);
25088a187ee5SSagi Grimberg 	*seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
25098a187ee5SSagi Grimberg 	*size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
25108a187ee5SSagi Grimberg 	if (unlikely((*seg == qp->sq.qend)))
25118a187ee5SSagi Grimberg 		*seg = mlx5_get_send_wqe(qp, 0);
25128a187ee5SSagi Grimberg 
25138a187ee5SSagi Grimberg 	set_reg_mkey_seg(*seg, mr, wr->key, wr->access);
25148a187ee5SSagi Grimberg 	*seg += sizeof(struct mlx5_mkey_seg);
25158a187ee5SSagi Grimberg 	*size += sizeof(struct mlx5_mkey_seg) / 16;
25168a187ee5SSagi Grimberg 	if (unlikely((*seg == qp->sq.qend)))
25178a187ee5SSagi Grimberg 		*seg = mlx5_get_send_wqe(qp, 0);
25188a187ee5SSagi Grimberg 
25198a187ee5SSagi Grimberg 	set_reg_data_seg(*seg, mr, pd);
25208a187ee5SSagi Grimberg 	*seg += sizeof(struct mlx5_wqe_data_seg);
25218a187ee5SSagi Grimberg 	*size += (sizeof(struct mlx5_wqe_data_seg) / 16);
25228a187ee5SSagi Grimberg 
25238a187ee5SSagi Grimberg 	return 0;
25248a187ee5SSagi Grimberg }
25258a187ee5SSagi Grimberg 
2526dd01e66aSSagi Grimberg static void set_linv_wr(struct mlx5_ib_qp *qp, void **seg, int *size)
2527e126ba97SEli Cohen {
2528dd01e66aSSagi Grimberg 	set_linv_umr_seg(*seg);
2529e126ba97SEli Cohen 	*seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
2530e126ba97SEli Cohen 	*size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
2531e126ba97SEli Cohen 	if (unlikely((*seg == qp->sq.qend)))
2532e126ba97SEli Cohen 		*seg = mlx5_get_send_wqe(qp, 0);
2533dd01e66aSSagi Grimberg 	set_linv_mkey_seg(*seg);
2534e126ba97SEli Cohen 	*seg += sizeof(struct mlx5_mkey_seg);
2535e126ba97SEli Cohen 	*size += sizeof(struct mlx5_mkey_seg) / 16;
2536e126ba97SEli Cohen 	if (unlikely((*seg == qp->sq.qend)))
2537e126ba97SEli Cohen 		*seg = mlx5_get_send_wqe(qp, 0);
2538e126ba97SEli Cohen }
2539e126ba97SEli Cohen 
2540e126ba97SEli Cohen static void dump_wqe(struct mlx5_ib_qp *qp, int idx, int size_16)
2541e126ba97SEli Cohen {
2542e126ba97SEli Cohen 	__be32 *p = NULL;
2543e126ba97SEli Cohen 	int tidx = idx;
2544e126ba97SEli Cohen 	int i, j;
2545e126ba97SEli Cohen 
2546e126ba97SEli Cohen 	pr_debug("dump wqe at %p\n", mlx5_get_send_wqe(qp, tidx));
2547e126ba97SEli Cohen 	for (i = 0, j = 0; i < size_16 * 4; i += 4, j += 4) {
2548e126ba97SEli Cohen 		if ((i & 0xf) == 0) {
2549e126ba97SEli Cohen 			void *buf = mlx5_get_send_wqe(qp, tidx);
2550e126ba97SEli Cohen 			tidx = (tidx + 1) & (qp->sq.wqe_cnt - 1);
2551e126ba97SEli Cohen 			p = buf;
2552e126ba97SEli Cohen 			j = 0;
2553e126ba97SEli Cohen 		}
2554e126ba97SEli Cohen 		pr_debug("%08x %08x %08x %08x\n", be32_to_cpu(p[j]),
2555e126ba97SEli Cohen 			 be32_to_cpu(p[j + 1]), be32_to_cpu(p[j + 2]),
2556e126ba97SEli Cohen 			 be32_to_cpu(p[j + 3]));
2557e126ba97SEli Cohen 	}
2558e126ba97SEli Cohen }
2559e126ba97SEli Cohen 
2560e126ba97SEli Cohen static void mlx5_bf_copy(u64 __iomem *dst, u64 *src,
2561e126ba97SEli Cohen 			 unsigned bytecnt, struct mlx5_ib_qp *qp)
2562e126ba97SEli Cohen {
2563e126ba97SEli Cohen 	while (bytecnt > 0) {
2564e126ba97SEli Cohen 		__iowrite64_copy(dst++, src++, 8);
2565e126ba97SEli Cohen 		__iowrite64_copy(dst++, src++, 8);
2566e126ba97SEli Cohen 		__iowrite64_copy(dst++, src++, 8);
2567e126ba97SEli Cohen 		__iowrite64_copy(dst++, src++, 8);
2568e126ba97SEli Cohen 		__iowrite64_copy(dst++, src++, 8);
2569e126ba97SEli Cohen 		__iowrite64_copy(dst++, src++, 8);
2570e126ba97SEli Cohen 		__iowrite64_copy(dst++, src++, 8);
2571e126ba97SEli Cohen 		__iowrite64_copy(dst++, src++, 8);
2572e126ba97SEli Cohen 		bytecnt -= 64;
2573e126ba97SEli Cohen 		if (unlikely(src == qp->sq.qend))
2574e126ba97SEli Cohen 			src = mlx5_get_send_wqe(qp, 0);
2575e126ba97SEli Cohen 	}
2576e126ba97SEli Cohen }
2577e126ba97SEli Cohen 
2578e126ba97SEli Cohen static u8 get_fence(u8 fence, struct ib_send_wr *wr)
2579e126ba97SEli Cohen {
2580e126ba97SEli Cohen 	if (unlikely(wr->opcode == IB_WR_LOCAL_INV &&
2581e126ba97SEli Cohen 		     wr->send_flags & IB_SEND_FENCE))
2582e126ba97SEli Cohen 		return MLX5_FENCE_MODE_STRONG_ORDERING;
2583e126ba97SEli Cohen 
2584e126ba97SEli Cohen 	if (unlikely(fence)) {
2585e126ba97SEli Cohen 		if (wr->send_flags & IB_SEND_FENCE)
2586e126ba97SEli Cohen 			return MLX5_FENCE_MODE_SMALL_AND_FENCE;
2587e126ba97SEli Cohen 		else
2588e126ba97SEli Cohen 			return fence;
2589e126ba97SEli Cohen 
2590e126ba97SEli Cohen 	} else {
2591e126ba97SEli Cohen 		return 0;
2592e126ba97SEli Cohen 	}
2593e126ba97SEli Cohen }
2594e126ba97SEli Cohen 
25956e5eadacSSagi Grimberg static int begin_wqe(struct mlx5_ib_qp *qp, void **seg,
25966e5eadacSSagi Grimberg 		     struct mlx5_wqe_ctrl_seg **ctrl,
25976a4f139aSEli Cohen 		     struct ib_send_wr *wr, unsigned *idx,
25986e5eadacSSagi Grimberg 		     int *size, int nreq)
25996e5eadacSSagi Grimberg {
26006e5eadacSSagi Grimberg 	int err = 0;
26016e5eadacSSagi Grimberg 
26026e5eadacSSagi Grimberg 	if (unlikely(mlx5_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq))) {
26036e5eadacSSagi Grimberg 		err = -ENOMEM;
26046e5eadacSSagi Grimberg 		return err;
26056e5eadacSSagi Grimberg 	}
26066e5eadacSSagi Grimberg 
26076e5eadacSSagi Grimberg 	*idx = qp->sq.cur_post & (qp->sq.wqe_cnt - 1);
26086e5eadacSSagi Grimberg 	*seg = mlx5_get_send_wqe(qp, *idx);
26096e5eadacSSagi Grimberg 	*ctrl = *seg;
26106e5eadacSSagi Grimberg 	*(uint32_t *)(*seg + 8) = 0;
26116e5eadacSSagi Grimberg 	(*ctrl)->imm = send_ieth(wr);
26126e5eadacSSagi Grimberg 	(*ctrl)->fm_ce_se = qp->sq_signal_bits |
26136e5eadacSSagi Grimberg 		(wr->send_flags & IB_SEND_SIGNALED ?
26146e5eadacSSagi Grimberg 		 MLX5_WQE_CTRL_CQ_UPDATE : 0) |
26156e5eadacSSagi Grimberg 		(wr->send_flags & IB_SEND_SOLICITED ?
26166e5eadacSSagi Grimberg 		 MLX5_WQE_CTRL_SOLICITED : 0);
26176e5eadacSSagi Grimberg 
26186e5eadacSSagi Grimberg 	*seg += sizeof(**ctrl);
26196e5eadacSSagi Grimberg 	*size = sizeof(**ctrl) / 16;
26206e5eadacSSagi Grimberg 
26216e5eadacSSagi Grimberg 	return err;
26226e5eadacSSagi Grimberg }
26236e5eadacSSagi Grimberg 
26246e5eadacSSagi Grimberg static void finish_wqe(struct mlx5_ib_qp *qp,
26256e5eadacSSagi Grimberg 		       struct mlx5_wqe_ctrl_seg *ctrl,
26266e5eadacSSagi Grimberg 		       u8 size, unsigned idx, u64 wr_id,
26276e5eadacSSagi Grimberg 		       int nreq, u8 fence, u8 next_fence,
26286e5eadacSSagi Grimberg 		       u32 mlx5_opcode)
26296e5eadacSSagi Grimberg {
26306e5eadacSSagi Grimberg 	u8 opmod = 0;
26316e5eadacSSagi Grimberg 
26326e5eadacSSagi Grimberg 	ctrl->opmod_idx_opcode = cpu_to_be32(((u32)(qp->sq.cur_post) << 8) |
26336e5eadacSSagi Grimberg 					     mlx5_opcode | ((u32)opmod << 24));
26346e5eadacSSagi Grimberg 	ctrl->qpn_ds = cpu_to_be32(size | (qp->mqp.qpn << 8));
26356e5eadacSSagi Grimberg 	ctrl->fm_ce_se |= fence;
26366e5eadacSSagi Grimberg 	qp->fm_cache = next_fence;
26376e5eadacSSagi Grimberg 	if (unlikely(qp->wq_sig))
26386e5eadacSSagi Grimberg 		ctrl->signature = wq_sig(ctrl);
26396e5eadacSSagi Grimberg 
26406e5eadacSSagi Grimberg 	qp->sq.wrid[idx] = wr_id;
26416e5eadacSSagi Grimberg 	qp->sq.w_list[idx].opcode = mlx5_opcode;
26426e5eadacSSagi Grimberg 	qp->sq.wqe_head[idx] = qp->sq.head + nreq;
26436e5eadacSSagi Grimberg 	qp->sq.cur_post += DIV_ROUND_UP(size * 16, MLX5_SEND_WQE_BB);
26446e5eadacSSagi Grimberg 	qp->sq.w_list[idx].next = qp->sq.cur_post;
26456e5eadacSSagi Grimberg }
26466e5eadacSSagi Grimberg 
26476e5eadacSSagi Grimberg 
2648e126ba97SEli Cohen int mlx5_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
2649e126ba97SEli Cohen 		      struct ib_send_wr **bad_wr)
2650e126ba97SEli Cohen {
2651e126ba97SEli Cohen 	struct mlx5_wqe_ctrl_seg *ctrl = NULL;  /* compiler warning */
2652e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2653e126ba97SEli Cohen 	struct mlx5_ib_qp *qp = to_mqp(ibqp);
2654e6631814SSagi Grimberg 	struct mlx5_ib_mr *mr;
2655e126ba97SEli Cohen 	struct mlx5_wqe_data_seg *dpseg;
2656e126ba97SEli Cohen 	struct mlx5_wqe_xrc_seg *xrc;
2657e126ba97SEli Cohen 	struct mlx5_bf *bf = qp->bf;
2658e126ba97SEli Cohen 	int uninitialized_var(size);
2659e126ba97SEli Cohen 	void *qend = qp->sq.qend;
2660e126ba97SEli Cohen 	unsigned long flags;
2661e126ba97SEli Cohen 	unsigned idx;
2662e126ba97SEli Cohen 	int err = 0;
2663e126ba97SEli Cohen 	int inl = 0;
2664e126ba97SEli Cohen 	int num_sge;
2665e126ba97SEli Cohen 	void *seg;
2666e126ba97SEli Cohen 	int nreq;
2667e126ba97SEli Cohen 	int i;
2668e126ba97SEli Cohen 	u8 next_fence = 0;
2669e126ba97SEli Cohen 	u8 fence;
2670e126ba97SEli Cohen 
2671e126ba97SEli Cohen 	spin_lock_irqsave(&qp->sq.lock, flags);
2672e126ba97SEli Cohen 
2673e126ba97SEli Cohen 	for (nreq = 0; wr; nreq++, wr = wr->next) {
2674a8f731ebSFabian Frederick 		if (unlikely(wr->opcode >= ARRAY_SIZE(mlx5_ib_opcode))) {
2675e126ba97SEli Cohen 			mlx5_ib_warn(dev, "\n");
2676e126ba97SEli Cohen 			err = -EINVAL;
2677e126ba97SEli Cohen 			*bad_wr = wr;
2678e126ba97SEli Cohen 			goto out;
2679e126ba97SEli Cohen 		}
2680e126ba97SEli Cohen 
2681e126ba97SEli Cohen 		fence = qp->fm_cache;
2682e126ba97SEli Cohen 		num_sge = wr->num_sge;
2683e126ba97SEli Cohen 		if (unlikely(num_sge > qp->sq.max_gs)) {
2684e126ba97SEli Cohen 			mlx5_ib_warn(dev, "\n");
2685e126ba97SEli Cohen 			err = -ENOMEM;
2686e126ba97SEli Cohen 			*bad_wr = wr;
2687e126ba97SEli Cohen 			goto out;
2688e126ba97SEli Cohen 		}
2689e126ba97SEli Cohen 
26906e5eadacSSagi Grimberg 		err = begin_wqe(qp, &seg, &ctrl, wr, &idx, &size, nreq);
26916e5eadacSSagi Grimberg 		if (err) {
26926e5eadacSSagi Grimberg 			mlx5_ib_warn(dev, "\n");
26936e5eadacSSagi Grimberg 			err = -ENOMEM;
26946e5eadacSSagi Grimberg 			*bad_wr = wr;
26956e5eadacSSagi Grimberg 			goto out;
26966e5eadacSSagi Grimberg 		}
2697e126ba97SEli Cohen 
2698e126ba97SEli Cohen 		switch (ibqp->qp_type) {
2699e126ba97SEli Cohen 		case IB_QPT_XRC_INI:
2700e126ba97SEli Cohen 			xrc = seg;
2701e126ba97SEli Cohen 			seg += sizeof(*xrc);
2702e126ba97SEli Cohen 			size += sizeof(*xrc) / 16;
2703e126ba97SEli Cohen 			/* fall through */
2704e126ba97SEli Cohen 		case IB_QPT_RC:
2705e126ba97SEli Cohen 			switch (wr->opcode) {
2706e126ba97SEli Cohen 			case IB_WR_RDMA_READ:
2707e126ba97SEli Cohen 			case IB_WR_RDMA_WRITE:
2708e126ba97SEli Cohen 			case IB_WR_RDMA_WRITE_WITH_IMM:
2709e622f2f4SChristoph Hellwig 				set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
2710e622f2f4SChristoph Hellwig 					      rdma_wr(wr)->rkey);
2711e126ba97SEli Cohen 				seg += sizeof(struct mlx5_wqe_raddr_seg);
2712e126ba97SEli Cohen 				size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
2713e126ba97SEli Cohen 				break;
2714e126ba97SEli Cohen 
2715e126ba97SEli Cohen 			case IB_WR_ATOMIC_CMP_AND_SWP:
2716e126ba97SEli Cohen 			case IB_WR_ATOMIC_FETCH_AND_ADD:
2717e126ba97SEli Cohen 			case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
271881bea28fSEli Cohen 				mlx5_ib_warn(dev, "Atomic operations are not supported yet\n");
271981bea28fSEli Cohen 				err = -ENOSYS;
272081bea28fSEli Cohen 				*bad_wr = wr;
272181bea28fSEli Cohen 				goto out;
2722e126ba97SEli Cohen 
2723e126ba97SEli Cohen 			case IB_WR_LOCAL_INV:
2724e126ba97SEli Cohen 				next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
2725e126ba97SEli Cohen 				qp->sq.wr_data[idx] = IB_WR_LOCAL_INV;
2726e126ba97SEli Cohen 				ctrl->imm = cpu_to_be32(wr->ex.invalidate_rkey);
2727dd01e66aSSagi Grimberg 				set_linv_wr(qp, &seg, &size);
2728e126ba97SEli Cohen 				num_sge = 0;
2729e126ba97SEli Cohen 				break;
2730e126ba97SEli Cohen 
27318a187ee5SSagi Grimberg 			case IB_WR_REG_MR:
27328a187ee5SSagi Grimberg 				next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
27338a187ee5SSagi Grimberg 				qp->sq.wr_data[idx] = IB_WR_REG_MR;
27348a187ee5SSagi Grimberg 				ctrl->imm = cpu_to_be32(reg_wr(wr)->key);
27358a187ee5SSagi Grimberg 				err = set_reg_wr(qp, reg_wr(wr), &seg, &size);
27368a187ee5SSagi Grimberg 				if (err) {
27378a187ee5SSagi Grimberg 					*bad_wr = wr;
27388a187ee5SSagi Grimberg 					goto out;
27398a187ee5SSagi Grimberg 				}
27408a187ee5SSagi Grimberg 				num_sge = 0;
27418a187ee5SSagi Grimberg 				break;
27428a187ee5SSagi Grimberg 
2743e6631814SSagi Grimberg 			case IB_WR_REG_SIG_MR:
2744e6631814SSagi Grimberg 				qp->sq.wr_data[idx] = IB_WR_REG_SIG_MR;
2745e622f2f4SChristoph Hellwig 				mr = to_mmr(sig_handover_wr(wr)->sig_mr);
2746e6631814SSagi Grimberg 
2747e6631814SSagi Grimberg 				ctrl->imm = cpu_to_be32(mr->ibmr.rkey);
2748e6631814SSagi Grimberg 				err = set_sig_umr_wr(wr, qp, &seg, &size);
2749e6631814SSagi Grimberg 				if (err) {
2750e6631814SSagi Grimberg 					mlx5_ib_warn(dev, "\n");
2751e6631814SSagi Grimberg 					*bad_wr = wr;
2752e6631814SSagi Grimberg 					goto out;
2753e6631814SSagi Grimberg 				}
2754e6631814SSagi Grimberg 
2755e6631814SSagi Grimberg 				finish_wqe(qp, ctrl, size, idx, wr->wr_id,
2756e6631814SSagi Grimberg 					   nreq, get_fence(fence, wr),
2757e6631814SSagi Grimberg 					   next_fence, MLX5_OPCODE_UMR);
2758e6631814SSagi Grimberg 				/*
2759e6631814SSagi Grimberg 				 * SET_PSV WQEs are not signaled and solicited
2760e6631814SSagi Grimberg 				 * on error
2761e6631814SSagi Grimberg 				 */
2762e6631814SSagi Grimberg 				wr->send_flags &= ~IB_SEND_SIGNALED;
2763e6631814SSagi Grimberg 				wr->send_flags |= IB_SEND_SOLICITED;
2764e6631814SSagi Grimberg 				err = begin_wqe(qp, &seg, &ctrl, wr,
2765e6631814SSagi Grimberg 						&idx, &size, nreq);
2766e6631814SSagi Grimberg 				if (err) {
2767e6631814SSagi Grimberg 					mlx5_ib_warn(dev, "\n");
2768e6631814SSagi Grimberg 					err = -ENOMEM;
2769e6631814SSagi Grimberg 					*bad_wr = wr;
2770e6631814SSagi Grimberg 					goto out;
2771e6631814SSagi Grimberg 				}
2772e6631814SSagi Grimberg 
2773e622f2f4SChristoph Hellwig 				err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->mem,
2774e6631814SSagi Grimberg 						 mr->sig->psv_memory.psv_idx, &seg,
2775e6631814SSagi Grimberg 						 &size);
2776e6631814SSagi Grimberg 				if (err) {
2777e6631814SSagi Grimberg 					mlx5_ib_warn(dev, "\n");
2778e6631814SSagi Grimberg 					*bad_wr = wr;
2779e6631814SSagi Grimberg 					goto out;
2780e6631814SSagi Grimberg 				}
2781e6631814SSagi Grimberg 
2782e6631814SSagi Grimberg 				finish_wqe(qp, ctrl, size, idx, wr->wr_id,
2783e6631814SSagi Grimberg 					   nreq, get_fence(fence, wr),
2784e6631814SSagi Grimberg 					   next_fence, MLX5_OPCODE_SET_PSV);
2785e6631814SSagi Grimberg 				err = begin_wqe(qp, &seg, &ctrl, wr,
2786e6631814SSagi Grimberg 						&idx, &size, nreq);
2787e6631814SSagi Grimberg 				if (err) {
2788e6631814SSagi Grimberg 					mlx5_ib_warn(dev, "\n");
2789e6631814SSagi Grimberg 					err = -ENOMEM;
2790e6631814SSagi Grimberg 					*bad_wr = wr;
2791e6631814SSagi Grimberg 					goto out;
2792e6631814SSagi Grimberg 				}
2793e6631814SSagi Grimberg 
2794e6631814SSagi Grimberg 				next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
2795e622f2f4SChristoph Hellwig 				err = set_psv_wr(&sig_handover_wr(wr)->sig_attrs->wire,
2796e6631814SSagi Grimberg 						 mr->sig->psv_wire.psv_idx, &seg,
2797e6631814SSagi Grimberg 						 &size);
2798e6631814SSagi Grimberg 				if (err) {
2799e6631814SSagi Grimberg 					mlx5_ib_warn(dev, "\n");
2800e6631814SSagi Grimberg 					*bad_wr = wr;
2801e6631814SSagi Grimberg 					goto out;
2802e6631814SSagi Grimberg 				}
2803e6631814SSagi Grimberg 
2804e6631814SSagi Grimberg 				finish_wqe(qp, ctrl, size, idx, wr->wr_id,
2805e6631814SSagi Grimberg 					   nreq, get_fence(fence, wr),
2806e6631814SSagi Grimberg 					   next_fence, MLX5_OPCODE_SET_PSV);
2807e6631814SSagi Grimberg 				num_sge = 0;
2808e6631814SSagi Grimberg 				goto skip_psv;
2809e6631814SSagi Grimberg 
2810e126ba97SEli Cohen 			default:
2811e126ba97SEli Cohen 				break;
2812e126ba97SEli Cohen 			}
2813e126ba97SEli Cohen 			break;
2814e126ba97SEli Cohen 
2815e126ba97SEli Cohen 		case IB_QPT_UC:
2816e126ba97SEli Cohen 			switch (wr->opcode) {
2817e126ba97SEli Cohen 			case IB_WR_RDMA_WRITE:
2818e126ba97SEli Cohen 			case IB_WR_RDMA_WRITE_WITH_IMM:
2819e622f2f4SChristoph Hellwig 				set_raddr_seg(seg, rdma_wr(wr)->remote_addr,
2820e622f2f4SChristoph Hellwig 					      rdma_wr(wr)->rkey);
2821e126ba97SEli Cohen 				seg  += sizeof(struct mlx5_wqe_raddr_seg);
2822e126ba97SEli Cohen 				size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
2823e126ba97SEli Cohen 				break;
2824e126ba97SEli Cohen 
2825e126ba97SEli Cohen 			default:
2826e126ba97SEli Cohen 				break;
2827e126ba97SEli Cohen 			}
2828e126ba97SEli Cohen 			break;
2829e126ba97SEli Cohen 
2830e126ba97SEli Cohen 		case IB_QPT_UD:
2831e126ba97SEli Cohen 		case IB_QPT_SMI:
2832e126ba97SEli Cohen 		case IB_QPT_GSI:
2833e126ba97SEli Cohen 			set_datagram_seg(seg, wr);
2834e126ba97SEli Cohen 			seg += sizeof(struct mlx5_wqe_datagram_seg);
2835e126ba97SEli Cohen 			size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
2836e126ba97SEli Cohen 			if (unlikely((seg == qend)))
2837e126ba97SEli Cohen 				seg = mlx5_get_send_wqe(qp, 0);
2838e126ba97SEli Cohen 			break;
2839e126ba97SEli Cohen 
2840e126ba97SEli Cohen 		case MLX5_IB_QPT_REG_UMR:
2841e126ba97SEli Cohen 			if (wr->opcode != MLX5_IB_WR_UMR) {
2842e126ba97SEli Cohen 				err = -EINVAL;
2843e126ba97SEli Cohen 				mlx5_ib_warn(dev, "bad opcode\n");
2844e126ba97SEli Cohen 				goto out;
2845e126ba97SEli Cohen 			}
2846e126ba97SEli Cohen 			qp->sq.wr_data[idx] = MLX5_IB_WR_UMR;
2847e622f2f4SChristoph Hellwig 			ctrl->imm = cpu_to_be32(umr_wr(wr)->mkey);
2848e126ba97SEli Cohen 			set_reg_umr_segment(seg, wr);
2849e126ba97SEli Cohen 			seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
2850e126ba97SEli Cohen 			size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
2851e126ba97SEli Cohen 			if (unlikely((seg == qend)))
2852e126ba97SEli Cohen 				seg = mlx5_get_send_wqe(qp, 0);
2853e126ba97SEli Cohen 			set_reg_mkey_segment(seg, wr);
2854e126ba97SEli Cohen 			seg += sizeof(struct mlx5_mkey_seg);
2855e126ba97SEli Cohen 			size += sizeof(struct mlx5_mkey_seg) / 16;
2856e126ba97SEli Cohen 			if (unlikely((seg == qend)))
2857e126ba97SEli Cohen 				seg = mlx5_get_send_wqe(qp, 0);
2858e126ba97SEli Cohen 			break;
2859e126ba97SEli Cohen 
2860e126ba97SEli Cohen 		default:
2861e126ba97SEli Cohen 			break;
2862e126ba97SEli Cohen 		}
2863e126ba97SEli Cohen 
2864e126ba97SEli Cohen 		if (wr->send_flags & IB_SEND_INLINE && num_sge) {
2865e126ba97SEli Cohen 			int uninitialized_var(sz);
2866e126ba97SEli Cohen 
2867e126ba97SEli Cohen 			err = set_data_inl_seg(qp, wr, seg, &sz);
2868e126ba97SEli Cohen 			if (unlikely(err)) {
2869e126ba97SEli Cohen 				mlx5_ib_warn(dev, "\n");
2870e126ba97SEli Cohen 				*bad_wr = wr;
2871e126ba97SEli Cohen 				goto out;
2872e126ba97SEli Cohen 			}
2873e126ba97SEli Cohen 			inl = 1;
2874e126ba97SEli Cohen 			size += sz;
2875e126ba97SEli Cohen 		} else {
2876e126ba97SEli Cohen 			dpseg = seg;
2877e126ba97SEli Cohen 			for (i = 0; i < num_sge; i++) {
2878e126ba97SEli Cohen 				if (unlikely(dpseg == qend)) {
2879e126ba97SEli Cohen 					seg = mlx5_get_send_wqe(qp, 0);
2880e126ba97SEli Cohen 					dpseg = seg;
2881e126ba97SEli Cohen 				}
2882e126ba97SEli Cohen 				if (likely(wr->sg_list[i].length)) {
2883e126ba97SEli Cohen 					set_data_ptr_seg(dpseg, wr->sg_list + i);
2884e126ba97SEli Cohen 					size += sizeof(struct mlx5_wqe_data_seg) / 16;
2885e126ba97SEli Cohen 					dpseg++;
2886e126ba97SEli Cohen 				}
2887e126ba97SEli Cohen 			}
2888e126ba97SEli Cohen 		}
2889e126ba97SEli Cohen 
28906e5eadacSSagi Grimberg 		finish_wqe(qp, ctrl, size, idx, wr->wr_id, nreq,
28916e5eadacSSagi Grimberg 			   get_fence(fence, wr), next_fence,
28926e5eadacSSagi Grimberg 			   mlx5_ib_opcode[wr->opcode]);
2893e6631814SSagi Grimberg skip_psv:
2894e126ba97SEli Cohen 		if (0)
2895e126ba97SEli Cohen 			dump_wqe(qp, idx, size);
2896e126ba97SEli Cohen 	}
2897e126ba97SEli Cohen 
2898e126ba97SEli Cohen out:
2899e126ba97SEli Cohen 	if (likely(nreq)) {
2900e126ba97SEli Cohen 		qp->sq.head += nreq;
2901e126ba97SEli Cohen 
2902e126ba97SEli Cohen 		/* Make sure that descriptors are written before
2903e126ba97SEli Cohen 		 * updating doorbell record and ringing the doorbell
2904e126ba97SEli Cohen 		 */
2905e126ba97SEli Cohen 		wmb();
2906e126ba97SEli Cohen 
2907e126ba97SEli Cohen 		qp->db.db[MLX5_SND_DBR] = cpu_to_be32(qp->sq.cur_post);
2908e126ba97SEli Cohen 
2909ada388f7SEli Cohen 		/* Make sure doorbell record is visible to the HCA before
2910ada388f7SEli Cohen 		 * we hit doorbell */
2911ada388f7SEli Cohen 		wmb();
2912ada388f7SEli Cohen 
2913e126ba97SEli Cohen 		if (bf->need_lock)
2914e126ba97SEli Cohen 			spin_lock(&bf->lock);
29156a4f139aSEli Cohen 		else
29166a4f139aSEli Cohen 			__acquire(&bf->lock);
2917e126ba97SEli Cohen 
2918e126ba97SEli Cohen 		/* TBD enable WC */
2919e126ba97SEli Cohen 		if (0 && nreq == 1 && bf->uuarn && inl && size > 1 && size <= bf->buf_size / 16) {
2920e126ba97SEli Cohen 			mlx5_bf_copy(bf->reg + bf->offset, (u64 *)ctrl, ALIGN(size * 16, 64), qp);
2921e126ba97SEli Cohen 			/* wc_wmb(); */
2922e126ba97SEli Cohen 		} else {
2923e126ba97SEli Cohen 			mlx5_write64((__be32 *)ctrl, bf->regreg + bf->offset,
2924e126ba97SEli Cohen 				     MLX5_GET_DOORBELL_LOCK(&bf->lock32));
2925e126ba97SEli Cohen 			/* Make sure doorbells don't leak out of SQ spinlock
2926e126ba97SEli Cohen 			 * and reach the HCA out of order.
2927e126ba97SEli Cohen 			 */
2928e126ba97SEli Cohen 			mmiowb();
2929e126ba97SEli Cohen 		}
2930e126ba97SEli Cohen 		bf->offset ^= bf->buf_size;
2931e126ba97SEli Cohen 		if (bf->need_lock)
2932e126ba97SEli Cohen 			spin_unlock(&bf->lock);
29336a4f139aSEli Cohen 		else
29346a4f139aSEli Cohen 			__release(&bf->lock);
2935e126ba97SEli Cohen 	}
2936e126ba97SEli Cohen 
2937e126ba97SEli Cohen 	spin_unlock_irqrestore(&qp->sq.lock, flags);
2938e126ba97SEli Cohen 
2939e126ba97SEli Cohen 	return err;
2940e126ba97SEli Cohen }
2941e126ba97SEli Cohen 
2942e126ba97SEli Cohen static void set_sig_seg(struct mlx5_rwqe_sig *sig, int size)
2943e126ba97SEli Cohen {
2944e126ba97SEli Cohen 	sig->signature = calc_sig(sig, size);
2945e126ba97SEli Cohen }
2946e126ba97SEli Cohen 
2947e126ba97SEli Cohen int mlx5_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
2948e126ba97SEli Cohen 		      struct ib_recv_wr **bad_wr)
2949e126ba97SEli Cohen {
2950e126ba97SEli Cohen 	struct mlx5_ib_qp *qp = to_mqp(ibqp);
2951e126ba97SEli Cohen 	struct mlx5_wqe_data_seg *scat;
2952e126ba97SEli Cohen 	struct mlx5_rwqe_sig *sig;
2953e126ba97SEli Cohen 	unsigned long flags;
2954e126ba97SEli Cohen 	int err = 0;
2955e126ba97SEli Cohen 	int nreq;
2956e126ba97SEli Cohen 	int ind;
2957e126ba97SEli Cohen 	int i;
2958e126ba97SEli Cohen 
2959e126ba97SEli Cohen 	spin_lock_irqsave(&qp->rq.lock, flags);
2960e126ba97SEli Cohen 
2961e126ba97SEli Cohen 	ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
2962e126ba97SEli Cohen 
2963e126ba97SEli Cohen 	for (nreq = 0; wr; nreq++, wr = wr->next) {
2964e126ba97SEli Cohen 		if (mlx5_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
2965e126ba97SEli Cohen 			err = -ENOMEM;
2966e126ba97SEli Cohen 			*bad_wr = wr;
2967e126ba97SEli Cohen 			goto out;
2968e126ba97SEli Cohen 		}
2969e126ba97SEli Cohen 
2970e126ba97SEli Cohen 		if (unlikely(wr->num_sge > qp->rq.max_gs)) {
2971e126ba97SEli Cohen 			err = -EINVAL;
2972e126ba97SEli Cohen 			*bad_wr = wr;
2973e126ba97SEli Cohen 			goto out;
2974e126ba97SEli Cohen 		}
2975e126ba97SEli Cohen 
2976e126ba97SEli Cohen 		scat = get_recv_wqe(qp, ind);
2977e126ba97SEli Cohen 		if (qp->wq_sig)
2978e126ba97SEli Cohen 			scat++;
2979e126ba97SEli Cohen 
2980e126ba97SEli Cohen 		for (i = 0; i < wr->num_sge; i++)
2981e126ba97SEli Cohen 			set_data_ptr_seg(scat + i, wr->sg_list + i);
2982e126ba97SEli Cohen 
2983e126ba97SEli Cohen 		if (i < qp->rq.max_gs) {
2984e126ba97SEli Cohen 			scat[i].byte_count = 0;
2985e126ba97SEli Cohen 			scat[i].lkey       = cpu_to_be32(MLX5_INVALID_LKEY);
2986e126ba97SEli Cohen 			scat[i].addr       = 0;
2987e126ba97SEli Cohen 		}
2988e126ba97SEli Cohen 
2989e126ba97SEli Cohen 		if (qp->wq_sig) {
2990e126ba97SEli Cohen 			sig = (struct mlx5_rwqe_sig *)scat;
2991e126ba97SEli Cohen 			set_sig_seg(sig, (qp->rq.max_gs + 1) << 2);
2992e126ba97SEli Cohen 		}
2993e126ba97SEli Cohen 
2994e126ba97SEli Cohen 		qp->rq.wrid[ind] = wr->wr_id;
2995e126ba97SEli Cohen 
2996e126ba97SEli Cohen 		ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
2997e126ba97SEli Cohen 	}
2998e126ba97SEli Cohen 
2999e126ba97SEli Cohen out:
3000e126ba97SEli Cohen 	if (likely(nreq)) {
3001e126ba97SEli Cohen 		qp->rq.head += nreq;
3002e126ba97SEli Cohen 
3003e126ba97SEli Cohen 		/* Make sure that descriptors are written before
3004e126ba97SEli Cohen 		 * doorbell record.
3005e126ba97SEli Cohen 		 */
3006e126ba97SEli Cohen 		wmb();
3007e126ba97SEli Cohen 
3008e126ba97SEli Cohen 		*qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
3009e126ba97SEli Cohen 	}
3010e126ba97SEli Cohen 
3011e126ba97SEli Cohen 	spin_unlock_irqrestore(&qp->rq.lock, flags);
3012e126ba97SEli Cohen 
3013e126ba97SEli Cohen 	return err;
3014e126ba97SEli Cohen }
3015e126ba97SEli Cohen 
3016e126ba97SEli Cohen static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
3017e126ba97SEli Cohen {
3018e126ba97SEli Cohen 	switch (mlx5_state) {
3019e126ba97SEli Cohen 	case MLX5_QP_STATE_RST:      return IB_QPS_RESET;
3020e126ba97SEli Cohen 	case MLX5_QP_STATE_INIT:     return IB_QPS_INIT;
3021e126ba97SEli Cohen 	case MLX5_QP_STATE_RTR:      return IB_QPS_RTR;
3022e126ba97SEli Cohen 	case MLX5_QP_STATE_RTS:      return IB_QPS_RTS;
3023e126ba97SEli Cohen 	case MLX5_QP_STATE_SQ_DRAINING:
3024e126ba97SEli Cohen 	case MLX5_QP_STATE_SQD:      return IB_QPS_SQD;
3025e126ba97SEli Cohen 	case MLX5_QP_STATE_SQER:     return IB_QPS_SQE;
3026e126ba97SEli Cohen 	case MLX5_QP_STATE_ERR:      return IB_QPS_ERR;
3027e126ba97SEli Cohen 	default:		     return -1;
3028e126ba97SEli Cohen 	}
3029e126ba97SEli Cohen }
3030e126ba97SEli Cohen 
3031e126ba97SEli Cohen static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
3032e126ba97SEli Cohen {
3033e126ba97SEli Cohen 	switch (mlx5_mig_state) {
3034e126ba97SEli Cohen 	case MLX5_QP_PM_ARMED:		return IB_MIG_ARMED;
3035e126ba97SEli Cohen 	case MLX5_QP_PM_REARM:		return IB_MIG_REARM;
3036e126ba97SEli Cohen 	case MLX5_QP_PM_MIGRATED:	return IB_MIG_MIGRATED;
3037e126ba97SEli Cohen 	default: return -1;
3038e126ba97SEli Cohen 	}
3039e126ba97SEli Cohen }
3040e126ba97SEli Cohen 
3041e126ba97SEli Cohen static int to_ib_qp_access_flags(int mlx5_flags)
3042e126ba97SEli Cohen {
3043e126ba97SEli Cohen 	int ib_flags = 0;
3044e126ba97SEli Cohen 
3045e126ba97SEli Cohen 	if (mlx5_flags & MLX5_QP_BIT_RRE)
3046e126ba97SEli Cohen 		ib_flags |= IB_ACCESS_REMOTE_READ;
3047e126ba97SEli Cohen 	if (mlx5_flags & MLX5_QP_BIT_RWE)
3048e126ba97SEli Cohen 		ib_flags |= IB_ACCESS_REMOTE_WRITE;
3049e126ba97SEli Cohen 	if (mlx5_flags & MLX5_QP_BIT_RAE)
3050e126ba97SEli Cohen 		ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
3051e126ba97SEli Cohen 
3052e126ba97SEli Cohen 	return ib_flags;
3053e126ba97SEli Cohen }
3054e126ba97SEli Cohen 
3055e126ba97SEli Cohen static void to_ib_ah_attr(struct mlx5_ib_dev *ibdev, struct ib_ah_attr *ib_ah_attr,
3056e126ba97SEli Cohen 				struct mlx5_qp_path *path)
3057e126ba97SEli Cohen {
30589603b61dSJack Morgenstein 	struct mlx5_core_dev *dev = ibdev->mdev;
3059e126ba97SEli Cohen 
3060e126ba97SEli Cohen 	memset(ib_ah_attr, 0, sizeof(*ib_ah_attr));
3061e126ba97SEli Cohen 	ib_ah_attr->port_num	  = path->port;
3062e126ba97SEli Cohen 
3063c7a08ac7SEli Cohen 	if (ib_ah_attr->port_num == 0 ||
3064938fe83cSSaeed Mahameed 	    ib_ah_attr->port_num > MLX5_CAP_GEN(dev, num_ports))
3065e126ba97SEli Cohen 		return;
3066e126ba97SEli Cohen 
30672811ba51SAchiad Shochat 	ib_ah_attr->sl = path->dci_cfi_prio_sl & 0xf;
3068e126ba97SEli Cohen 
3069e126ba97SEli Cohen 	ib_ah_attr->dlid	  = be16_to_cpu(path->rlid);
3070e126ba97SEli Cohen 	ib_ah_attr->src_path_bits = path->grh_mlid & 0x7f;
3071e126ba97SEli Cohen 	ib_ah_attr->static_rate   = path->static_rate ? path->static_rate - 5 : 0;
3072e126ba97SEli Cohen 	ib_ah_attr->ah_flags      = (path->grh_mlid & (1 << 7)) ? IB_AH_GRH : 0;
3073e126ba97SEli Cohen 	if (ib_ah_attr->ah_flags) {
3074e126ba97SEli Cohen 		ib_ah_attr->grh.sgid_index = path->mgid_index;
3075e126ba97SEli Cohen 		ib_ah_attr->grh.hop_limit  = path->hop_limit;
3076e126ba97SEli Cohen 		ib_ah_attr->grh.traffic_class =
3077e126ba97SEli Cohen 			(be32_to_cpu(path->tclass_flowlabel) >> 20) & 0xff;
3078e126ba97SEli Cohen 		ib_ah_attr->grh.flow_label =
3079e126ba97SEli Cohen 			be32_to_cpu(path->tclass_flowlabel) & 0xfffff;
3080e126ba97SEli Cohen 		memcpy(ib_ah_attr->grh.dgid.raw,
3081e126ba97SEli Cohen 		       path->rgid, sizeof(ib_ah_attr->grh.dgid.raw));
3082e126ba97SEli Cohen 	}
3083e126ba97SEli Cohen }
3084e126ba97SEli Cohen 
3085e126ba97SEli Cohen int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, int qp_attr_mask,
3086e126ba97SEli Cohen 		     struct ib_qp_init_attr *qp_init_attr)
3087e126ba97SEli Cohen {
3088e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3089e126ba97SEli Cohen 	struct mlx5_ib_qp *qp = to_mqp(ibqp);
3090e126ba97SEli Cohen 	struct mlx5_query_qp_mbox_out *outb;
3091e126ba97SEli Cohen 	struct mlx5_qp_context *context;
3092e126ba97SEli Cohen 	int mlx5_state;
3093e126ba97SEli Cohen 	int err = 0;
3094e126ba97SEli Cohen 
30956aec21f6SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
30966aec21f6SHaggai Eran 	/*
30976aec21f6SHaggai Eran 	 * Wait for any outstanding page faults, in case the user frees memory
30986aec21f6SHaggai Eran 	 * based upon this query's result.
30996aec21f6SHaggai Eran 	 */
31006aec21f6SHaggai Eran 	flush_workqueue(mlx5_ib_page_fault_wq);
31016aec21f6SHaggai Eran #endif
31026aec21f6SHaggai Eran 
3103e126ba97SEli Cohen 	mutex_lock(&qp->mutex);
3104e126ba97SEli Cohen 	outb = kzalloc(sizeof(*outb), GFP_KERNEL);
3105e126ba97SEli Cohen 	if (!outb) {
3106e126ba97SEli Cohen 		err = -ENOMEM;
3107e126ba97SEli Cohen 		goto out;
3108e126ba97SEli Cohen 	}
3109e126ba97SEli Cohen 	context = &outb->ctx;
31109603b61dSJack Morgenstein 	err = mlx5_core_qp_query(dev->mdev, &qp->mqp, outb, sizeof(*outb));
3111e126ba97SEli Cohen 	if (err)
3112e126ba97SEli Cohen 		goto out_free;
3113e126ba97SEli Cohen 
3114e126ba97SEli Cohen 	mlx5_state = be32_to_cpu(context->flags) >> 28;
3115e126ba97SEli Cohen 
3116e126ba97SEli Cohen 	qp->state		     = to_ib_qp_state(mlx5_state);
3117e126ba97SEli Cohen 	qp_attr->qp_state	     = qp->state;
3118e126ba97SEli Cohen 	qp_attr->path_mtu	     = context->mtu_msgmax >> 5;
3119e126ba97SEli Cohen 	qp_attr->path_mig_state	     =
3120e126ba97SEli Cohen 		to_ib_mig_state((be32_to_cpu(context->flags) >> 11) & 0x3);
3121e126ba97SEli Cohen 	qp_attr->qkey		     = be32_to_cpu(context->qkey);
3122e126ba97SEli Cohen 	qp_attr->rq_psn		     = be32_to_cpu(context->rnr_nextrecvpsn) & 0xffffff;
3123e126ba97SEli Cohen 	qp_attr->sq_psn		     = be32_to_cpu(context->next_send_psn) & 0xffffff;
3124e126ba97SEli Cohen 	qp_attr->dest_qp_num	     = be32_to_cpu(context->log_pg_sz_remote_qpn) & 0xffffff;
3125e126ba97SEli Cohen 	qp_attr->qp_access_flags     =
3126e126ba97SEli Cohen 		to_ib_qp_access_flags(be32_to_cpu(context->params2));
3127e126ba97SEli Cohen 
3128e126ba97SEli Cohen 	if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
3129e126ba97SEli Cohen 		to_ib_ah_attr(dev, &qp_attr->ah_attr, &context->pri_path);
3130e126ba97SEli Cohen 		to_ib_ah_attr(dev, &qp_attr->alt_ah_attr, &context->alt_path);
3131e126ba97SEli Cohen 		qp_attr->alt_pkey_index = context->alt_path.pkey_index & 0x7f;
3132e126ba97SEli Cohen 		qp_attr->alt_port_num	= qp_attr->alt_ah_attr.port_num;
3133e126ba97SEli Cohen 	}
3134e126ba97SEli Cohen 
3135e126ba97SEli Cohen 	qp_attr->pkey_index = context->pri_path.pkey_index & 0x7f;
3136e126ba97SEli Cohen 	qp_attr->port_num = context->pri_path.port;
3137e126ba97SEli Cohen 
3138e126ba97SEli Cohen 	/* qp_attr->en_sqd_async_notify is only applicable in modify qp */
3139e126ba97SEli Cohen 	qp_attr->sq_draining = mlx5_state == MLX5_QP_STATE_SQ_DRAINING;
3140e126ba97SEli Cohen 
3141e126ba97SEli Cohen 	qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context->params1) >> 21) & 0x7);
3142e126ba97SEli Cohen 
3143e126ba97SEli Cohen 	qp_attr->max_dest_rd_atomic =
3144e126ba97SEli Cohen 		1 << ((be32_to_cpu(context->params2) >> 21) & 0x7);
3145e126ba97SEli Cohen 	qp_attr->min_rnr_timer	    =
3146e126ba97SEli Cohen 		(be32_to_cpu(context->rnr_nextrecvpsn) >> 24) & 0x1f;
3147e126ba97SEli Cohen 	qp_attr->timeout	    = context->pri_path.ackto_lt >> 3;
3148e126ba97SEli Cohen 	qp_attr->retry_cnt	    = (be32_to_cpu(context->params1) >> 16) & 0x7;
3149e126ba97SEli Cohen 	qp_attr->rnr_retry	    = (be32_to_cpu(context->params1) >> 13) & 0x7;
3150e126ba97SEli Cohen 	qp_attr->alt_timeout	    = context->alt_path.ackto_lt >> 3;
3151e126ba97SEli Cohen 	qp_attr->cur_qp_state	     = qp_attr->qp_state;
3152e126ba97SEli Cohen 	qp_attr->cap.max_recv_wr     = qp->rq.wqe_cnt;
3153e126ba97SEli Cohen 	qp_attr->cap.max_recv_sge    = qp->rq.max_gs;
3154e126ba97SEli Cohen 
3155e126ba97SEli Cohen 	if (!ibqp->uobject) {
3156e126ba97SEli Cohen 		qp_attr->cap.max_send_wr  = qp->sq.wqe_cnt;
3157e126ba97SEli Cohen 		qp_attr->cap.max_send_sge = qp->sq.max_gs;
3158e126ba97SEli Cohen 	} else {
3159e126ba97SEli Cohen 		qp_attr->cap.max_send_wr  = 0;
3160e126ba97SEli Cohen 		qp_attr->cap.max_send_sge = 0;
3161e126ba97SEli Cohen 	}
3162e126ba97SEli Cohen 
3163e126ba97SEli Cohen 	/* We don't support inline sends for kernel QPs (yet), and we
3164e126ba97SEli Cohen 	 * don't know what userspace's value should be.
3165e126ba97SEli Cohen 	 */
3166e126ba97SEli Cohen 	qp_attr->cap.max_inline_data = 0;
3167e126ba97SEli Cohen 
3168e126ba97SEli Cohen 	qp_init_attr->cap	     = qp_attr->cap;
3169e126ba97SEli Cohen 
3170e126ba97SEli Cohen 	qp_init_attr->create_flags = 0;
3171e126ba97SEli Cohen 	if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
3172e126ba97SEli Cohen 		qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
3173e126ba97SEli Cohen 
3174051f2630SLeon Romanovsky 	if (qp->flags & MLX5_IB_QP_CROSS_CHANNEL)
3175051f2630SLeon Romanovsky 		qp_init_attr->create_flags |= IB_QP_CREATE_CROSS_CHANNEL;
3176051f2630SLeon Romanovsky 	if (qp->flags & MLX5_IB_QP_MANAGED_SEND)
3177051f2630SLeon Romanovsky 		qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_SEND;
3178051f2630SLeon Romanovsky 	if (qp->flags & MLX5_IB_QP_MANAGED_RECV)
3179051f2630SLeon Romanovsky 		qp_init_attr->create_flags |= IB_QP_CREATE_MANAGED_RECV;
3180051f2630SLeon Romanovsky 
3181e126ba97SEli Cohen 	qp_init_attr->sq_sig_type = qp->sq_signal_bits & MLX5_WQE_CTRL_CQ_UPDATE ?
3182e126ba97SEli Cohen 		IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
3183e126ba97SEli Cohen 
3184e126ba97SEli Cohen out_free:
3185e126ba97SEli Cohen 	kfree(outb);
3186e126ba97SEli Cohen 
3187e126ba97SEli Cohen out:
3188e126ba97SEli Cohen 	mutex_unlock(&qp->mutex);
3189e126ba97SEli Cohen 	return err;
3190e126ba97SEli Cohen }
3191e126ba97SEli Cohen 
3192e126ba97SEli Cohen struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
3193e126ba97SEli Cohen 					  struct ib_ucontext *context,
3194e126ba97SEli Cohen 					  struct ib_udata *udata)
3195e126ba97SEli Cohen {
3196e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3197e126ba97SEli Cohen 	struct mlx5_ib_xrcd *xrcd;
3198e126ba97SEli Cohen 	int err;
3199e126ba97SEli Cohen 
3200938fe83cSSaeed Mahameed 	if (!MLX5_CAP_GEN(dev->mdev, xrc))
3201e126ba97SEli Cohen 		return ERR_PTR(-ENOSYS);
3202e126ba97SEli Cohen 
3203e126ba97SEli Cohen 	xrcd = kmalloc(sizeof(*xrcd), GFP_KERNEL);
3204e126ba97SEli Cohen 	if (!xrcd)
3205e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
3206e126ba97SEli Cohen 
32079603b61dSJack Morgenstein 	err = mlx5_core_xrcd_alloc(dev->mdev, &xrcd->xrcdn);
3208e126ba97SEli Cohen 	if (err) {
3209e126ba97SEli Cohen 		kfree(xrcd);
3210e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
3211e126ba97SEli Cohen 	}
3212e126ba97SEli Cohen 
3213e126ba97SEli Cohen 	return &xrcd->ibxrcd;
3214e126ba97SEli Cohen }
3215e126ba97SEli Cohen 
3216e126ba97SEli Cohen int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
3217e126ba97SEli Cohen {
3218e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
3219e126ba97SEli Cohen 	u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
3220e126ba97SEli Cohen 	int err;
3221e126ba97SEli Cohen 
32229603b61dSJack Morgenstein 	err = mlx5_core_xrcd_dealloc(dev->mdev, xrcdn);
3223e126ba97SEli Cohen 	if (err) {
3224e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed to dealloc xrcdn 0x%x\n", xrcdn);
3225e126ba97SEli Cohen 		return err;
3226e126ba97SEli Cohen 	}
3227e126ba97SEli Cohen 
3228e126ba97SEli Cohen 	kfree(xrcd);
3229e126ba97SEli Cohen 
3230e126ba97SEli Cohen 	return 0;
3231e126ba97SEli Cohen }
3232