xref: /openbmc/linux/drivers/infiniband/hw/mlx5/qp.c (revision bf0bf77f)
1e126ba97SEli Cohen /*
2e126ba97SEli Cohen  * Copyright (c) 2013, Mellanox Technologies inc.  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>
35e126ba97SEli Cohen #include "mlx5_ib.h"
36e126ba97SEli Cohen #include "user.h"
37e126ba97SEli Cohen 
38e126ba97SEli Cohen /* not supported currently */
39e126ba97SEli Cohen static int wq_signature;
40e126ba97SEli Cohen 
41e126ba97SEli Cohen enum {
42e126ba97SEli Cohen 	MLX5_IB_ACK_REQ_FREQ	= 8,
43e126ba97SEli Cohen };
44e126ba97SEli Cohen 
45e126ba97SEli Cohen enum {
46e126ba97SEli Cohen 	MLX5_IB_DEFAULT_SCHED_QUEUE	= 0x83,
47e126ba97SEli Cohen 	MLX5_IB_DEFAULT_QP0_SCHED_QUEUE	= 0x3f,
48e126ba97SEli Cohen 	MLX5_IB_LINK_TYPE_IB		= 0,
49e126ba97SEli Cohen 	MLX5_IB_LINK_TYPE_ETH		= 1
50e126ba97SEli Cohen };
51e126ba97SEli Cohen 
52e126ba97SEli Cohen enum {
53e126ba97SEli Cohen 	MLX5_IB_SQ_STRIDE	= 6,
54e126ba97SEli Cohen 	MLX5_IB_CACHE_LINE_SIZE	= 64,
55e126ba97SEli Cohen };
56e126ba97SEli Cohen 
57e126ba97SEli Cohen static const u32 mlx5_ib_opcode[] = {
58e126ba97SEli Cohen 	[IB_WR_SEND]				= MLX5_OPCODE_SEND,
59e126ba97SEli Cohen 	[IB_WR_SEND_WITH_IMM]			= MLX5_OPCODE_SEND_IMM,
60e126ba97SEli Cohen 	[IB_WR_RDMA_WRITE]			= MLX5_OPCODE_RDMA_WRITE,
61e126ba97SEli Cohen 	[IB_WR_RDMA_WRITE_WITH_IMM]		= MLX5_OPCODE_RDMA_WRITE_IMM,
62e126ba97SEli Cohen 	[IB_WR_RDMA_READ]			= MLX5_OPCODE_RDMA_READ,
63e126ba97SEli Cohen 	[IB_WR_ATOMIC_CMP_AND_SWP]		= MLX5_OPCODE_ATOMIC_CS,
64e126ba97SEli Cohen 	[IB_WR_ATOMIC_FETCH_AND_ADD]		= MLX5_OPCODE_ATOMIC_FA,
65e126ba97SEli Cohen 	[IB_WR_SEND_WITH_INV]			= MLX5_OPCODE_SEND_INVAL,
66e126ba97SEli Cohen 	[IB_WR_LOCAL_INV]			= MLX5_OPCODE_UMR,
67e126ba97SEli Cohen 	[IB_WR_FAST_REG_MR]			= MLX5_OPCODE_UMR,
68e126ba97SEli Cohen 	[IB_WR_MASKED_ATOMIC_CMP_AND_SWP]	= MLX5_OPCODE_ATOMIC_MASKED_CS,
69e126ba97SEli Cohen 	[IB_WR_MASKED_ATOMIC_FETCH_AND_ADD]	= MLX5_OPCODE_ATOMIC_MASKED_FA,
70e126ba97SEli Cohen 	[MLX5_IB_WR_UMR]			= MLX5_OPCODE_UMR,
71e126ba97SEli Cohen };
72e126ba97SEli Cohen 
73e126ba97SEli Cohen struct umr_wr {
74e126ba97SEli Cohen 	u64				virt_addr;
75e126ba97SEli Cohen 	struct ib_pd		       *pd;
76e126ba97SEli Cohen 	unsigned int			page_shift;
77e126ba97SEli Cohen 	unsigned int			npages;
78e126ba97SEli Cohen 	u32				length;
79e126ba97SEli Cohen 	int				access_flags;
80e126ba97SEli Cohen 	u32				mkey;
81e126ba97SEli Cohen };
82e126ba97SEli Cohen 
83e126ba97SEli Cohen static int is_qp0(enum ib_qp_type qp_type)
84e126ba97SEli Cohen {
85e126ba97SEli Cohen 	return qp_type == IB_QPT_SMI;
86e126ba97SEli Cohen }
87e126ba97SEli Cohen 
88e126ba97SEli Cohen static int is_qp1(enum ib_qp_type qp_type)
89e126ba97SEli Cohen {
90e126ba97SEli Cohen 	return qp_type == IB_QPT_GSI;
91e126ba97SEli Cohen }
92e126ba97SEli Cohen 
93e126ba97SEli Cohen static int is_sqp(enum ib_qp_type qp_type)
94e126ba97SEli Cohen {
95e126ba97SEli Cohen 	return is_qp0(qp_type) || is_qp1(qp_type);
96e126ba97SEli Cohen }
97e126ba97SEli Cohen 
98e126ba97SEli Cohen static void *get_wqe(struct mlx5_ib_qp *qp, int offset)
99e126ba97SEli Cohen {
100e126ba97SEli Cohen 	return mlx5_buf_offset(&qp->buf, offset);
101e126ba97SEli Cohen }
102e126ba97SEli Cohen 
103e126ba97SEli Cohen static void *get_recv_wqe(struct mlx5_ib_qp *qp, int n)
104e126ba97SEli Cohen {
105e126ba97SEli Cohen 	return get_wqe(qp, qp->rq.offset + (n << qp->rq.wqe_shift));
106e126ba97SEli Cohen }
107e126ba97SEli Cohen 
108e126ba97SEli Cohen void *mlx5_get_send_wqe(struct mlx5_ib_qp *qp, int n)
109e126ba97SEli Cohen {
110e126ba97SEli Cohen 	return get_wqe(qp, qp->sq.offset + (n << MLX5_IB_SQ_STRIDE));
111e126ba97SEli Cohen }
112e126ba97SEli Cohen 
113e126ba97SEli Cohen static void mlx5_ib_qp_event(struct mlx5_core_qp *qp, int type)
114e126ba97SEli Cohen {
115e126ba97SEli Cohen 	struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
116e126ba97SEli Cohen 	struct ib_event event;
117e126ba97SEli Cohen 
118e126ba97SEli Cohen 	if (type == MLX5_EVENT_TYPE_PATH_MIG)
119e126ba97SEli Cohen 		to_mibqp(qp)->port = to_mibqp(qp)->alt_port;
120e126ba97SEli Cohen 
121e126ba97SEli Cohen 	if (ibqp->event_handler) {
122e126ba97SEli Cohen 		event.device     = ibqp->device;
123e126ba97SEli Cohen 		event.element.qp = ibqp;
124e126ba97SEli Cohen 		switch (type) {
125e126ba97SEli Cohen 		case MLX5_EVENT_TYPE_PATH_MIG:
126e126ba97SEli Cohen 			event.event = IB_EVENT_PATH_MIG;
127e126ba97SEli Cohen 			break;
128e126ba97SEli Cohen 		case MLX5_EVENT_TYPE_COMM_EST:
129e126ba97SEli Cohen 			event.event = IB_EVENT_COMM_EST;
130e126ba97SEli Cohen 			break;
131e126ba97SEli Cohen 		case MLX5_EVENT_TYPE_SQ_DRAINED:
132e126ba97SEli Cohen 			event.event = IB_EVENT_SQ_DRAINED;
133e126ba97SEli Cohen 			break;
134e126ba97SEli Cohen 		case MLX5_EVENT_TYPE_SRQ_LAST_WQE:
135e126ba97SEli Cohen 			event.event = IB_EVENT_QP_LAST_WQE_REACHED;
136e126ba97SEli Cohen 			break;
137e126ba97SEli Cohen 		case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
138e126ba97SEli Cohen 			event.event = IB_EVENT_QP_FATAL;
139e126ba97SEli Cohen 			break;
140e126ba97SEli Cohen 		case MLX5_EVENT_TYPE_PATH_MIG_FAILED:
141e126ba97SEli Cohen 			event.event = IB_EVENT_PATH_MIG_ERR;
142e126ba97SEli Cohen 			break;
143e126ba97SEli Cohen 		case MLX5_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
144e126ba97SEli Cohen 			event.event = IB_EVENT_QP_REQ_ERR;
145e126ba97SEli Cohen 			break;
146e126ba97SEli Cohen 		case MLX5_EVENT_TYPE_WQ_ACCESS_ERROR:
147e126ba97SEli Cohen 			event.event = IB_EVENT_QP_ACCESS_ERR;
148e126ba97SEli Cohen 			break;
149e126ba97SEli Cohen 		default:
150e126ba97SEli Cohen 			pr_warn("mlx5_ib: Unexpected event type %d on QP %06x\n", type, qp->qpn);
151e126ba97SEli Cohen 			return;
152e126ba97SEli Cohen 		}
153e126ba97SEli Cohen 
154e126ba97SEli Cohen 		ibqp->event_handler(&event, ibqp->qp_context);
155e126ba97SEli Cohen 	}
156e126ba97SEli Cohen }
157e126ba97SEli Cohen 
158e126ba97SEli Cohen static int set_rq_size(struct mlx5_ib_dev *dev, struct ib_qp_cap *cap,
159e126ba97SEli Cohen 		       int has_rq, struct mlx5_ib_qp *qp, struct mlx5_ib_create_qp *ucmd)
160e126ba97SEli Cohen {
161e126ba97SEli Cohen 	int wqe_size;
162e126ba97SEli Cohen 	int wq_size;
163e126ba97SEli Cohen 
164e126ba97SEli Cohen 	/* Sanity check RQ size before proceeding */
165e126ba97SEli Cohen 	if (cap->max_recv_wr  > dev->mdev.caps.max_wqes)
166e126ba97SEli Cohen 		return -EINVAL;
167e126ba97SEli Cohen 
168e126ba97SEli Cohen 	if (!has_rq) {
169e126ba97SEli Cohen 		qp->rq.max_gs = 0;
170e126ba97SEli Cohen 		qp->rq.wqe_cnt = 0;
171e126ba97SEli Cohen 		qp->rq.wqe_shift = 0;
172e126ba97SEli Cohen 	} else {
173e126ba97SEli Cohen 		if (ucmd) {
174e126ba97SEli Cohen 			qp->rq.wqe_cnt = ucmd->rq_wqe_count;
175e126ba97SEli Cohen 			qp->rq.wqe_shift = ucmd->rq_wqe_shift;
176e126ba97SEli Cohen 			qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
177e126ba97SEli Cohen 			qp->rq.max_post = qp->rq.wqe_cnt;
178e126ba97SEli Cohen 		} else {
179e126ba97SEli Cohen 			wqe_size = qp->wq_sig ? sizeof(struct mlx5_wqe_signature_seg) : 0;
180e126ba97SEli Cohen 			wqe_size += cap->max_recv_sge * sizeof(struct mlx5_wqe_data_seg);
181e126ba97SEli Cohen 			wqe_size = roundup_pow_of_two(wqe_size);
182e126ba97SEli Cohen 			wq_size = roundup_pow_of_two(cap->max_recv_wr) * wqe_size;
183e126ba97SEli Cohen 			wq_size = max_t(int, wq_size, MLX5_SEND_WQE_BB);
184e126ba97SEli Cohen 			qp->rq.wqe_cnt = wq_size / wqe_size;
185e126ba97SEli Cohen 			if (wqe_size > dev->mdev.caps.max_rq_desc_sz) {
186e126ba97SEli Cohen 				mlx5_ib_dbg(dev, "wqe_size %d, max %d\n",
187e126ba97SEli Cohen 					    wqe_size,
188e126ba97SEli Cohen 					    dev->mdev.caps.max_rq_desc_sz);
189e126ba97SEli Cohen 				return -EINVAL;
190e126ba97SEli Cohen 			}
191e126ba97SEli Cohen 			qp->rq.wqe_shift = ilog2(wqe_size);
192e126ba97SEli Cohen 			qp->rq.max_gs = (1 << qp->rq.wqe_shift) / sizeof(struct mlx5_wqe_data_seg) - qp->wq_sig;
193e126ba97SEli Cohen 			qp->rq.max_post = qp->rq.wqe_cnt;
194e126ba97SEli Cohen 		}
195e126ba97SEli Cohen 	}
196e126ba97SEli Cohen 
197e126ba97SEli Cohen 	return 0;
198e126ba97SEli Cohen }
199e126ba97SEli Cohen 
200e126ba97SEli Cohen static int sq_overhead(enum ib_qp_type qp_type)
201e126ba97SEli Cohen {
202618af384SAndi Shyti 	int size = 0;
203e126ba97SEli Cohen 
204e126ba97SEli Cohen 	switch (qp_type) {
205e126ba97SEli Cohen 	case IB_QPT_XRC_INI:
206b125a54bSEli Cohen 		size += sizeof(struct mlx5_wqe_xrc_seg);
207e126ba97SEli Cohen 		/* fall through */
208e126ba97SEli Cohen 	case IB_QPT_RC:
209e126ba97SEli Cohen 		size += sizeof(struct mlx5_wqe_ctrl_seg) +
210e126ba97SEli Cohen 			sizeof(struct mlx5_wqe_atomic_seg) +
211e126ba97SEli Cohen 			sizeof(struct mlx5_wqe_raddr_seg);
212e126ba97SEli Cohen 		break;
213e126ba97SEli Cohen 
214b125a54bSEli Cohen 	case IB_QPT_XRC_TGT:
215b125a54bSEli Cohen 		return 0;
216b125a54bSEli Cohen 
217e126ba97SEli Cohen 	case IB_QPT_UC:
218b125a54bSEli Cohen 		size += sizeof(struct mlx5_wqe_ctrl_seg) +
219e126ba97SEli Cohen 			sizeof(struct mlx5_wqe_raddr_seg);
220e126ba97SEli Cohen 		break;
221e126ba97SEli Cohen 
222e126ba97SEli Cohen 	case IB_QPT_UD:
223e126ba97SEli Cohen 	case IB_QPT_SMI:
224e126ba97SEli Cohen 	case IB_QPT_GSI:
225b125a54bSEli Cohen 		size += sizeof(struct mlx5_wqe_ctrl_seg) +
226e126ba97SEli Cohen 			sizeof(struct mlx5_wqe_datagram_seg);
227e126ba97SEli Cohen 		break;
228e126ba97SEli Cohen 
229e126ba97SEli Cohen 	case MLX5_IB_QPT_REG_UMR:
230b125a54bSEli Cohen 		size += sizeof(struct mlx5_wqe_ctrl_seg) +
231e126ba97SEli Cohen 			sizeof(struct mlx5_wqe_umr_ctrl_seg) +
232e126ba97SEli Cohen 			sizeof(struct mlx5_mkey_seg);
233e126ba97SEli Cohen 		break;
234e126ba97SEli Cohen 
235e126ba97SEli Cohen 	default:
236e126ba97SEli Cohen 		return -EINVAL;
237e126ba97SEli Cohen 	}
238e126ba97SEli Cohen 
239e126ba97SEli Cohen 	return size;
240e126ba97SEli Cohen }
241e126ba97SEli Cohen 
242e126ba97SEli Cohen static int calc_send_wqe(struct ib_qp_init_attr *attr)
243e126ba97SEli Cohen {
244e126ba97SEli Cohen 	int inl_size = 0;
245e126ba97SEli Cohen 	int size;
246e126ba97SEli Cohen 
247e126ba97SEli Cohen 	size = sq_overhead(attr->qp_type);
248e126ba97SEli Cohen 	if (size < 0)
249e126ba97SEli Cohen 		return size;
250e126ba97SEli Cohen 
251e126ba97SEli Cohen 	if (attr->cap.max_inline_data) {
252e126ba97SEli Cohen 		inl_size = size + sizeof(struct mlx5_wqe_inline_seg) +
253e126ba97SEli Cohen 			attr->cap.max_inline_data;
254e126ba97SEli Cohen 	}
255e126ba97SEli Cohen 
256e126ba97SEli Cohen 	size += attr->cap.max_send_sge * sizeof(struct mlx5_wqe_data_seg);
257e126ba97SEli Cohen 
258e126ba97SEli Cohen 	return ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB);
259e126ba97SEli Cohen }
260e126ba97SEli Cohen 
261e126ba97SEli Cohen static int calc_sq_size(struct mlx5_ib_dev *dev, struct ib_qp_init_attr *attr,
262e126ba97SEli Cohen 			struct mlx5_ib_qp *qp)
263e126ba97SEli Cohen {
264e126ba97SEli Cohen 	int wqe_size;
265e126ba97SEli Cohen 	int wq_size;
266e126ba97SEli Cohen 
267e126ba97SEli Cohen 	if (!attr->cap.max_send_wr)
268e126ba97SEli Cohen 		return 0;
269e126ba97SEli Cohen 
270e126ba97SEli Cohen 	wqe_size = calc_send_wqe(attr);
271e126ba97SEli Cohen 	mlx5_ib_dbg(dev, "wqe_size %d\n", wqe_size);
272e126ba97SEli Cohen 	if (wqe_size < 0)
273e126ba97SEli Cohen 		return wqe_size;
274e126ba97SEli Cohen 
275e126ba97SEli Cohen 	if (wqe_size > dev->mdev.caps.max_sq_desc_sz) {
276b125a54bSEli Cohen 		mlx5_ib_dbg(dev, "wqe_size(%d) > max_sq_desc_sz(%d)\n",
277b125a54bSEli Cohen 			    wqe_size, dev->mdev.caps.max_sq_desc_sz);
278e126ba97SEli Cohen 		return -EINVAL;
279e126ba97SEli Cohen 	}
280e126ba97SEli Cohen 
281e126ba97SEli Cohen 	qp->max_inline_data = wqe_size - sq_overhead(attr->qp_type) -
282e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_inline_seg);
283e126ba97SEli Cohen 	attr->cap.max_inline_data = qp->max_inline_data;
284e126ba97SEli Cohen 
285e126ba97SEli Cohen 	wq_size = roundup_pow_of_two(attr->cap.max_send_wr * wqe_size);
286e126ba97SEli Cohen 	qp->sq.wqe_cnt = wq_size / MLX5_SEND_WQE_BB;
287b125a54bSEli Cohen 	if (qp->sq.wqe_cnt > dev->mdev.caps.max_wqes) {
288b125a54bSEli Cohen 		mlx5_ib_dbg(dev, "wqe count(%d) exceeds limits(%d)\n",
289b125a54bSEli Cohen 			    qp->sq.wqe_cnt, dev->mdev.caps.max_wqes);
290b125a54bSEli Cohen 		return -ENOMEM;
291b125a54bSEli Cohen 	}
292e126ba97SEli Cohen 	qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
293e126ba97SEli Cohen 	qp->sq.max_gs = attr->cap.max_send_sge;
294b125a54bSEli Cohen 	qp->sq.max_post = wq_size / wqe_size;
295b125a54bSEli Cohen 	attr->cap.max_send_wr = qp->sq.max_post;
296e126ba97SEli Cohen 
297e126ba97SEli Cohen 	return wq_size;
298e126ba97SEli Cohen }
299e126ba97SEli Cohen 
300e126ba97SEli Cohen static int set_user_buf_size(struct mlx5_ib_dev *dev,
301e126ba97SEli Cohen 			    struct mlx5_ib_qp *qp,
302e126ba97SEli Cohen 			    struct mlx5_ib_create_qp *ucmd)
303e126ba97SEli Cohen {
304e126ba97SEli Cohen 	int desc_sz = 1 << qp->sq.wqe_shift;
305e126ba97SEli Cohen 
306e126ba97SEli Cohen 	if (desc_sz > dev->mdev.caps.max_sq_desc_sz) {
307e126ba97SEli Cohen 		mlx5_ib_warn(dev, "desc_sz %d, max_sq_desc_sz %d\n",
308e126ba97SEli Cohen 			     desc_sz, dev->mdev.caps.max_sq_desc_sz);
309e126ba97SEli Cohen 		return -EINVAL;
310e126ba97SEli Cohen 	}
311e126ba97SEli Cohen 
312e126ba97SEli Cohen 	if (ucmd->sq_wqe_count && ((1 << ilog2(ucmd->sq_wqe_count)) != ucmd->sq_wqe_count)) {
313e126ba97SEli Cohen 		mlx5_ib_warn(dev, "sq_wqe_count %d, sq_wqe_count %d\n",
314e126ba97SEli Cohen 			     ucmd->sq_wqe_count, ucmd->sq_wqe_count);
315e126ba97SEli Cohen 		return -EINVAL;
316e126ba97SEli Cohen 	}
317e126ba97SEli Cohen 
318e126ba97SEli Cohen 	qp->sq.wqe_cnt = ucmd->sq_wqe_count;
319e126ba97SEli Cohen 
320e126ba97SEli Cohen 	if (qp->sq.wqe_cnt > dev->mdev.caps.max_wqes) {
321e126ba97SEli Cohen 		mlx5_ib_warn(dev, "wqe_cnt %d, max_wqes %d\n",
322e126ba97SEli Cohen 			     qp->sq.wqe_cnt, dev->mdev.caps.max_wqes);
323e126ba97SEli Cohen 		return -EINVAL;
324e126ba97SEli Cohen 	}
325e126ba97SEli Cohen 
326e126ba97SEli Cohen 	qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
327e126ba97SEli Cohen 		(qp->sq.wqe_cnt << 6);
328e126ba97SEli Cohen 
329e126ba97SEli Cohen 	return 0;
330e126ba97SEli Cohen }
331e126ba97SEli Cohen 
332e126ba97SEli Cohen static int qp_has_rq(struct ib_qp_init_attr *attr)
333e126ba97SEli Cohen {
334e126ba97SEli Cohen 	if (attr->qp_type == IB_QPT_XRC_INI ||
335e126ba97SEli Cohen 	    attr->qp_type == IB_QPT_XRC_TGT || attr->srq ||
336e126ba97SEli Cohen 	    attr->qp_type == MLX5_IB_QPT_REG_UMR ||
337e126ba97SEli Cohen 	    !attr->cap.max_recv_wr)
338e126ba97SEli Cohen 		return 0;
339e126ba97SEli Cohen 
340e126ba97SEli Cohen 	return 1;
341e126ba97SEli Cohen }
342e126ba97SEli Cohen 
343e126ba97SEli Cohen static int alloc_high_class_uuar(struct mlx5_uuar_info *uuari)
344e126ba97SEli Cohen {
345e126ba97SEli Cohen 	int nuuars = uuari->num_uars * MLX5_BF_REGS_PER_PAGE;
346e126ba97SEli Cohen 	int start_uuar;
347e126ba97SEli Cohen 	int i;
348e126ba97SEli Cohen 
349e126ba97SEli Cohen 	start_uuar = nuuars - uuari->num_low_latency_uuars;
350e126ba97SEli Cohen 	for (i = start_uuar; i < nuuars; i++) {
351e126ba97SEli Cohen 		if (!test_bit(i, uuari->bitmap)) {
352e126ba97SEli Cohen 			set_bit(i, uuari->bitmap);
353e126ba97SEli Cohen 			uuari->count[i]++;
354e126ba97SEli Cohen 			return i;
355e126ba97SEli Cohen 		}
356e126ba97SEli Cohen 	}
357e126ba97SEli Cohen 
358e126ba97SEli Cohen 	return -ENOMEM;
359e126ba97SEli Cohen }
360e126ba97SEli Cohen 
361e126ba97SEli Cohen static int alloc_med_class_uuar(struct mlx5_uuar_info *uuari)
362e126ba97SEli Cohen {
363e126ba97SEli Cohen 	int nuuars = uuari->num_uars * MLX5_BF_REGS_PER_PAGE;
364e126ba97SEli Cohen 	int minidx = 1;
365e126ba97SEli Cohen 	int uuarn;
366e126ba97SEli Cohen 	int end;
367e126ba97SEli Cohen 	int i;
368e126ba97SEli Cohen 
369e126ba97SEli Cohen 	end = nuuars - uuari->num_low_latency_uuars;
370e126ba97SEli Cohen 
371e126ba97SEli Cohen 	for (i = 1; i < end; i++) {
372e126ba97SEli Cohen 		uuarn = i & 3;
373e126ba97SEli Cohen 		if (uuarn == 2 || uuarn == 3)
374e126ba97SEli Cohen 			continue;
375e126ba97SEli Cohen 
376e126ba97SEli Cohen 		if (uuari->count[i] < uuari->count[minidx])
377e126ba97SEli Cohen 			minidx = i;
378e126ba97SEli Cohen 	}
379e126ba97SEli Cohen 
380e126ba97SEli Cohen 	uuari->count[minidx]++;
381e126ba97SEli Cohen 	return minidx;
382e126ba97SEli Cohen }
383e126ba97SEli Cohen 
384e126ba97SEli Cohen static int alloc_uuar(struct mlx5_uuar_info *uuari,
385e126ba97SEli Cohen 		      enum mlx5_ib_latency_class lat)
386e126ba97SEli Cohen {
387e126ba97SEli Cohen 	int uuarn = -EINVAL;
388e126ba97SEli Cohen 
389e126ba97SEli Cohen 	mutex_lock(&uuari->lock);
390e126ba97SEli Cohen 	switch (lat) {
391e126ba97SEli Cohen 	case MLX5_IB_LATENCY_CLASS_LOW:
392e126ba97SEli Cohen 		uuarn = 0;
393e126ba97SEli Cohen 		uuari->count[uuarn]++;
394e126ba97SEli Cohen 		break;
395e126ba97SEli Cohen 
396e126ba97SEli Cohen 	case MLX5_IB_LATENCY_CLASS_MEDIUM:
397e126ba97SEli Cohen 		uuarn = alloc_med_class_uuar(uuari);
398e126ba97SEli Cohen 		break;
399e126ba97SEli Cohen 
400e126ba97SEli Cohen 	case MLX5_IB_LATENCY_CLASS_HIGH:
401e126ba97SEli Cohen 		uuarn = alloc_high_class_uuar(uuari);
402e126ba97SEli Cohen 		break;
403e126ba97SEli Cohen 
404e126ba97SEli Cohen 	case MLX5_IB_LATENCY_CLASS_FAST_PATH:
405e126ba97SEli Cohen 		uuarn = 2;
406e126ba97SEli Cohen 		break;
407e126ba97SEli Cohen 	}
408e126ba97SEli Cohen 	mutex_unlock(&uuari->lock);
409e126ba97SEli Cohen 
410e126ba97SEli Cohen 	return uuarn;
411e126ba97SEli Cohen }
412e126ba97SEli Cohen 
413e126ba97SEli Cohen static void free_med_class_uuar(struct mlx5_uuar_info *uuari, int uuarn)
414e126ba97SEli Cohen {
415e126ba97SEli Cohen 	clear_bit(uuarn, uuari->bitmap);
416e126ba97SEli Cohen 	--uuari->count[uuarn];
417e126ba97SEli Cohen }
418e126ba97SEli Cohen 
419e126ba97SEli Cohen static void free_high_class_uuar(struct mlx5_uuar_info *uuari, int uuarn)
420e126ba97SEli Cohen {
421e126ba97SEli Cohen 	clear_bit(uuarn, uuari->bitmap);
422e126ba97SEli Cohen 	--uuari->count[uuarn];
423e126ba97SEli Cohen }
424e126ba97SEli Cohen 
425e126ba97SEli Cohen static void free_uuar(struct mlx5_uuar_info *uuari, int uuarn)
426e126ba97SEli Cohen {
427e126ba97SEli Cohen 	int nuuars = uuari->num_uars * MLX5_BF_REGS_PER_PAGE;
428e126ba97SEli Cohen 	int high_uuar = nuuars - uuari->num_low_latency_uuars;
429e126ba97SEli Cohen 
430e126ba97SEli Cohen 	mutex_lock(&uuari->lock);
431e126ba97SEli Cohen 	if (uuarn == 0) {
432e126ba97SEli Cohen 		--uuari->count[uuarn];
433e126ba97SEli Cohen 		goto out;
434e126ba97SEli Cohen 	}
435e126ba97SEli Cohen 
436e126ba97SEli Cohen 	if (uuarn < high_uuar) {
437e126ba97SEli Cohen 		free_med_class_uuar(uuari, uuarn);
438e126ba97SEli Cohen 		goto out;
439e126ba97SEli Cohen 	}
440e126ba97SEli Cohen 
441e126ba97SEli Cohen 	free_high_class_uuar(uuari, uuarn);
442e126ba97SEli Cohen 
443e126ba97SEli Cohen out:
444e126ba97SEli Cohen 	mutex_unlock(&uuari->lock);
445e126ba97SEli Cohen }
446e126ba97SEli Cohen 
447e126ba97SEli Cohen static enum mlx5_qp_state to_mlx5_state(enum ib_qp_state state)
448e126ba97SEli Cohen {
449e126ba97SEli Cohen 	switch (state) {
450e126ba97SEli Cohen 	case IB_QPS_RESET:	return MLX5_QP_STATE_RST;
451e126ba97SEli Cohen 	case IB_QPS_INIT:	return MLX5_QP_STATE_INIT;
452e126ba97SEli Cohen 	case IB_QPS_RTR:	return MLX5_QP_STATE_RTR;
453e126ba97SEli Cohen 	case IB_QPS_RTS:	return MLX5_QP_STATE_RTS;
454e126ba97SEli Cohen 	case IB_QPS_SQD:	return MLX5_QP_STATE_SQD;
455e126ba97SEli Cohen 	case IB_QPS_SQE:	return MLX5_QP_STATE_SQER;
456e126ba97SEli Cohen 	case IB_QPS_ERR:	return MLX5_QP_STATE_ERR;
457e126ba97SEli Cohen 	default:		return -1;
458e126ba97SEli Cohen 	}
459e126ba97SEli Cohen }
460e126ba97SEli Cohen 
461e126ba97SEli Cohen static int to_mlx5_st(enum ib_qp_type type)
462e126ba97SEli Cohen {
463e126ba97SEli Cohen 	switch (type) {
464e126ba97SEli Cohen 	case IB_QPT_RC:			return MLX5_QP_ST_RC;
465e126ba97SEli Cohen 	case IB_QPT_UC:			return MLX5_QP_ST_UC;
466e126ba97SEli Cohen 	case IB_QPT_UD:			return MLX5_QP_ST_UD;
467e126ba97SEli Cohen 	case MLX5_IB_QPT_REG_UMR:	return MLX5_QP_ST_REG_UMR;
468e126ba97SEli Cohen 	case IB_QPT_XRC_INI:
469e126ba97SEli Cohen 	case IB_QPT_XRC_TGT:		return MLX5_QP_ST_XRC;
470e126ba97SEli Cohen 	case IB_QPT_SMI:		return MLX5_QP_ST_QP0;
471e126ba97SEli Cohen 	case IB_QPT_GSI:		return MLX5_QP_ST_QP1;
472e126ba97SEli Cohen 	case IB_QPT_RAW_IPV6:		return MLX5_QP_ST_RAW_IPV6;
473e126ba97SEli Cohen 	case IB_QPT_RAW_ETHERTYPE:	return MLX5_QP_ST_RAW_ETHERTYPE;
474e126ba97SEli Cohen 	case IB_QPT_RAW_PACKET:
475e126ba97SEli Cohen 	case IB_QPT_MAX:
476e126ba97SEli Cohen 	default:		return -EINVAL;
477e126ba97SEli Cohen 	}
478e126ba97SEli Cohen }
479e126ba97SEli Cohen 
480e126ba97SEli Cohen static int uuarn_to_uar_index(struct mlx5_uuar_info *uuari, int uuarn)
481e126ba97SEli Cohen {
482e126ba97SEli Cohen 	return uuari->uars[uuarn / MLX5_BF_REGS_PER_PAGE].index;
483e126ba97SEli Cohen }
484e126ba97SEli Cohen 
485e126ba97SEli Cohen static int create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
486e126ba97SEli Cohen 			  struct mlx5_ib_qp *qp, struct ib_udata *udata,
487e126ba97SEli Cohen 			  struct mlx5_create_qp_mbox_in **in,
488e126ba97SEli Cohen 			  struct mlx5_ib_create_qp_resp *resp, int *inlen)
489e126ba97SEli Cohen {
490e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
491e126ba97SEli Cohen 	struct mlx5_ib_create_qp ucmd;
492e126ba97SEli Cohen 	int page_shift;
493e126ba97SEli Cohen 	int uar_index;
494e126ba97SEli Cohen 	int npages;
495e126ba97SEli Cohen 	u32 offset;
496e126ba97SEli Cohen 	int uuarn;
497e126ba97SEli Cohen 	int ncont;
498e126ba97SEli Cohen 	int err;
499e126ba97SEli Cohen 
500e126ba97SEli Cohen 	err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
501e126ba97SEli Cohen 	if (err) {
502e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "copy failed\n");
503e126ba97SEli Cohen 		return err;
504e126ba97SEli Cohen 	}
505e126ba97SEli Cohen 
506e126ba97SEli Cohen 	context = to_mucontext(pd->uobject->context);
507e126ba97SEli Cohen 	/*
508e126ba97SEli Cohen 	 * TBD: should come from the verbs when we have the API
509e126ba97SEli Cohen 	 */
510e126ba97SEli Cohen 	uuarn = alloc_uuar(&context->uuari, MLX5_IB_LATENCY_CLASS_HIGH);
511e126ba97SEli Cohen 	if (uuarn < 0) {
512e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "failed to allocate low latency UUAR\n");
513e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "reverting to high latency\n");
514e126ba97SEli Cohen 		uuarn = alloc_uuar(&context->uuari, MLX5_IB_LATENCY_CLASS_LOW);
515e126ba97SEli Cohen 		if (uuarn < 0) {
516e126ba97SEli Cohen 			mlx5_ib_dbg(dev, "uuar allocation failed\n");
517e126ba97SEli Cohen 			return uuarn;
518e126ba97SEli Cohen 		}
519e126ba97SEli Cohen 	}
520e126ba97SEli Cohen 
521e126ba97SEli Cohen 	uar_index = uuarn_to_uar_index(&context->uuari, uuarn);
522e126ba97SEli Cohen 	mlx5_ib_dbg(dev, "uuarn 0x%x, uar_index 0x%x\n", uuarn, uar_index);
523e126ba97SEli Cohen 
524e126ba97SEli Cohen 	err = set_user_buf_size(dev, qp, &ucmd);
525e126ba97SEli Cohen 	if (err)
526e126ba97SEli Cohen 		goto err_uuar;
527e126ba97SEli Cohen 
528e126ba97SEli Cohen 	qp->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr,
529e126ba97SEli Cohen 			       qp->buf_size, 0, 0);
530e126ba97SEli Cohen 	if (IS_ERR(qp->umem)) {
531e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "umem_get failed\n");
532e126ba97SEli Cohen 		err = PTR_ERR(qp->umem);
533e126ba97SEli Cohen 		goto err_uuar;
534e126ba97SEli Cohen 	}
535e126ba97SEli Cohen 
536e126ba97SEli Cohen 	mlx5_ib_cont_pages(qp->umem, ucmd.buf_addr, &npages, &page_shift,
537e126ba97SEli Cohen 			   &ncont, NULL);
538e126ba97SEli Cohen 	err = mlx5_ib_get_buf_offset(ucmd.buf_addr, page_shift, &offset);
539e126ba97SEli Cohen 	if (err) {
540e126ba97SEli Cohen 		mlx5_ib_warn(dev, "bad offset\n");
541e126ba97SEli Cohen 		goto err_umem;
542e126ba97SEli Cohen 	}
543e126ba97SEli Cohen 	mlx5_ib_dbg(dev, "addr 0x%llx, size %d, npages %d, page_shift %d, ncont %d, offset %d\n",
544e126ba97SEli Cohen 		    ucmd.buf_addr, qp->buf_size, npages, page_shift, ncont, offset);
545e126ba97SEli Cohen 
546e126ba97SEli Cohen 	*inlen = sizeof(**in) + sizeof(*(*in)->pas) * ncont;
547e126ba97SEli Cohen 	*in = mlx5_vzalloc(*inlen);
548e126ba97SEli Cohen 	if (!*in) {
549e126ba97SEli Cohen 		err = -ENOMEM;
550e126ba97SEli Cohen 		goto err_umem;
551e126ba97SEli Cohen 	}
552e126ba97SEli Cohen 	mlx5_ib_populate_pas(dev, qp->umem, page_shift, (*in)->pas, 0);
553e126ba97SEli Cohen 	(*in)->ctx.log_pg_sz_remote_qpn =
554bf0bf77fSEli Cohen 		cpu_to_be32((page_shift - 12) << 24);
555e126ba97SEli Cohen 	(*in)->ctx.params2 = cpu_to_be32(offset << 6);
556e126ba97SEli Cohen 
557e126ba97SEli Cohen 	(*in)->ctx.qp_counter_set_usr_page = cpu_to_be32(uar_index);
558e126ba97SEli Cohen 	resp->uuar_index = uuarn;
559e126ba97SEli Cohen 	qp->uuarn = uuarn;
560e126ba97SEli Cohen 
561e126ba97SEli Cohen 	err = mlx5_ib_db_map_user(context, ucmd.db_addr, &qp->db);
562e126ba97SEli Cohen 	if (err) {
563e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "map failed\n");
564e126ba97SEli Cohen 		goto err_free;
565e126ba97SEli Cohen 	}
566e126ba97SEli Cohen 
567e126ba97SEli Cohen 	err = ib_copy_to_udata(udata, resp, sizeof(*resp));
568e126ba97SEli Cohen 	if (err) {
569e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "copy failed\n");
570e126ba97SEli Cohen 		goto err_unmap;
571e126ba97SEli Cohen 	}
572e126ba97SEli Cohen 	qp->create_type = MLX5_QP_USER;
573e126ba97SEli Cohen 
574e126ba97SEli Cohen 	return 0;
575e126ba97SEli Cohen 
576e126ba97SEli Cohen err_unmap:
577e126ba97SEli Cohen 	mlx5_ib_db_unmap_user(context, &qp->db);
578e126ba97SEli Cohen 
579e126ba97SEli Cohen err_free:
580e126ba97SEli Cohen 	mlx5_vfree(*in);
581e126ba97SEli Cohen 
582e126ba97SEli Cohen err_umem:
583e126ba97SEli Cohen 	ib_umem_release(qp->umem);
584e126ba97SEli Cohen 
585e126ba97SEli Cohen err_uuar:
586e126ba97SEli Cohen 	free_uuar(&context->uuari, uuarn);
587e126ba97SEli Cohen 	return err;
588e126ba97SEli Cohen }
589e126ba97SEli Cohen 
590e126ba97SEli Cohen static void destroy_qp_user(struct ib_pd *pd, struct mlx5_ib_qp *qp)
591e126ba97SEli Cohen {
592e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
593e126ba97SEli Cohen 
594e126ba97SEli Cohen 	context = to_mucontext(pd->uobject->context);
595e126ba97SEli Cohen 	mlx5_ib_db_unmap_user(context, &qp->db);
596e126ba97SEli Cohen 	ib_umem_release(qp->umem);
597e126ba97SEli Cohen 	free_uuar(&context->uuari, qp->uuarn);
598e126ba97SEli Cohen }
599e126ba97SEli Cohen 
600e126ba97SEli Cohen static int create_kernel_qp(struct mlx5_ib_dev *dev,
601e126ba97SEli Cohen 			    struct ib_qp_init_attr *init_attr,
602e126ba97SEli Cohen 			    struct mlx5_ib_qp *qp,
603e126ba97SEli Cohen 			    struct mlx5_create_qp_mbox_in **in, int *inlen)
604e126ba97SEli Cohen {
605e126ba97SEli Cohen 	enum mlx5_ib_latency_class lc = MLX5_IB_LATENCY_CLASS_LOW;
606e126ba97SEli Cohen 	struct mlx5_uuar_info *uuari;
607e126ba97SEli Cohen 	int uar_index;
608e126ba97SEli Cohen 	int uuarn;
609e126ba97SEli Cohen 	int err;
610e126ba97SEli Cohen 
611e126ba97SEli Cohen 	uuari = &dev->mdev.priv.uuari;
612e126ba97SEli Cohen 	if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK)
613e126ba97SEli Cohen 		qp->flags |= MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK;
614e126ba97SEli Cohen 
615e126ba97SEli Cohen 	if (init_attr->qp_type == MLX5_IB_QPT_REG_UMR)
616e126ba97SEli Cohen 		lc = MLX5_IB_LATENCY_CLASS_FAST_PATH;
617e126ba97SEli Cohen 
618e126ba97SEli Cohen 	uuarn = alloc_uuar(uuari, lc);
619e126ba97SEli Cohen 	if (uuarn < 0) {
620e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "\n");
621e126ba97SEli Cohen 		return -ENOMEM;
622e126ba97SEli Cohen 	}
623e126ba97SEli Cohen 
624e126ba97SEli Cohen 	qp->bf = &uuari->bfs[uuarn];
625e126ba97SEli Cohen 	uar_index = qp->bf->uar->index;
626e126ba97SEli Cohen 
627e126ba97SEli Cohen 	err = calc_sq_size(dev, init_attr, qp);
628e126ba97SEli Cohen 	if (err < 0) {
629e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "err %d\n", err);
630e126ba97SEli Cohen 		goto err_uuar;
631e126ba97SEli Cohen 	}
632e126ba97SEli Cohen 
633e126ba97SEli Cohen 	qp->rq.offset = 0;
634e126ba97SEli Cohen 	qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
635e126ba97SEli Cohen 	qp->buf_size = err + (qp->rq.wqe_cnt << qp->rq.wqe_shift);
636e126ba97SEli Cohen 
637e126ba97SEli Cohen 	err = mlx5_buf_alloc(&dev->mdev, qp->buf_size, PAGE_SIZE * 2, &qp->buf);
638e126ba97SEli Cohen 	if (err) {
639e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "err %d\n", err);
640e126ba97SEli Cohen 		goto err_uuar;
641e126ba97SEli Cohen 	}
642e126ba97SEli Cohen 
643e126ba97SEli Cohen 	qp->sq.qend = mlx5_get_send_wqe(qp, qp->sq.wqe_cnt);
644e126ba97SEli Cohen 	*inlen = sizeof(**in) + sizeof(*(*in)->pas) * qp->buf.npages;
645e126ba97SEli Cohen 	*in = mlx5_vzalloc(*inlen);
646e126ba97SEli Cohen 	if (!*in) {
647e126ba97SEli Cohen 		err = -ENOMEM;
648e126ba97SEli Cohen 		goto err_buf;
649e126ba97SEli Cohen 	}
650e126ba97SEli Cohen 	(*in)->ctx.qp_counter_set_usr_page = cpu_to_be32(uar_index);
651bf0bf77fSEli Cohen 	(*in)->ctx.log_pg_sz_remote_qpn = cpu_to_be32((qp->buf.page_shift - 12) << 24);
652e126ba97SEli Cohen 	/* Set "fast registration enabled" for all kernel QPs */
653e126ba97SEli Cohen 	(*in)->ctx.params1 |= cpu_to_be32(1 << 11);
654e126ba97SEli Cohen 	(*in)->ctx.sq_crq_size |= cpu_to_be16(1 << 4);
655e126ba97SEli Cohen 
656e126ba97SEli Cohen 	mlx5_fill_page_array(&qp->buf, (*in)->pas);
657e126ba97SEli Cohen 
658e126ba97SEli Cohen 	err = mlx5_db_alloc(&dev->mdev, &qp->db);
659e126ba97SEli Cohen 	if (err) {
660e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "err %d\n", err);
661e126ba97SEli Cohen 		goto err_free;
662e126ba97SEli Cohen 	}
663e126ba97SEli Cohen 
664e126ba97SEli Cohen 	qp->db.db[0] = 0;
665e126ba97SEli Cohen 	qp->db.db[1] = 0;
666e126ba97SEli Cohen 
667e126ba97SEli Cohen 	qp->sq.wrid = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wrid), GFP_KERNEL);
668e126ba97SEli Cohen 	qp->sq.wr_data = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wr_data), GFP_KERNEL);
669e126ba97SEli Cohen 	qp->rq.wrid = kmalloc(qp->rq.wqe_cnt * sizeof(*qp->rq.wrid), GFP_KERNEL);
670e126ba97SEli Cohen 	qp->sq.w_list = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.w_list), GFP_KERNEL);
671e126ba97SEli Cohen 	qp->sq.wqe_head = kmalloc(qp->sq.wqe_cnt * sizeof(*qp->sq.wqe_head), GFP_KERNEL);
672e126ba97SEli Cohen 
673e126ba97SEli Cohen 	if (!qp->sq.wrid || !qp->sq.wr_data || !qp->rq.wrid ||
674e126ba97SEli Cohen 	    !qp->sq.w_list || !qp->sq.wqe_head) {
675e126ba97SEli Cohen 		err = -ENOMEM;
676e126ba97SEli Cohen 		goto err_wrid;
677e126ba97SEli Cohen 	}
678e126ba97SEli Cohen 	qp->create_type = MLX5_QP_KERNEL;
679e126ba97SEli Cohen 
680e126ba97SEli Cohen 	return 0;
681e126ba97SEli Cohen 
682e126ba97SEli Cohen err_wrid:
683e126ba97SEli Cohen 	mlx5_db_free(&dev->mdev, &qp->db);
684e126ba97SEli Cohen 	kfree(qp->sq.wqe_head);
685e126ba97SEli Cohen 	kfree(qp->sq.w_list);
686e126ba97SEli Cohen 	kfree(qp->sq.wrid);
687e126ba97SEli Cohen 	kfree(qp->sq.wr_data);
688e126ba97SEli Cohen 	kfree(qp->rq.wrid);
689e126ba97SEli Cohen 
690e126ba97SEli Cohen err_free:
691e126ba97SEli Cohen 	mlx5_vfree(*in);
692e126ba97SEli Cohen 
693e126ba97SEli Cohen err_buf:
694e126ba97SEli Cohen 	mlx5_buf_free(&dev->mdev, &qp->buf);
695e126ba97SEli Cohen 
696e126ba97SEli Cohen err_uuar:
697e126ba97SEli Cohen 	free_uuar(&dev->mdev.priv.uuari, uuarn);
698e126ba97SEli Cohen 	return err;
699e126ba97SEli Cohen }
700e126ba97SEli Cohen 
701e126ba97SEli Cohen static void destroy_qp_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
702e126ba97SEli Cohen {
703e126ba97SEli Cohen 	mlx5_db_free(&dev->mdev, &qp->db);
704e126ba97SEli Cohen 	kfree(qp->sq.wqe_head);
705e126ba97SEli Cohen 	kfree(qp->sq.w_list);
706e126ba97SEli Cohen 	kfree(qp->sq.wrid);
707e126ba97SEli Cohen 	kfree(qp->sq.wr_data);
708e126ba97SEli Cohen 	kfree(qp->rq.wrid);
709e126ba97SEli Cohen 	mlx5_buf_free(&dev->mdev, &qp->buf);
710e126ba97SEli Cohen 	free_uuar(&dev->mdev.priv.uuari, qp->bf->uuarn);
711e126ba97SEli Cohen }
712e126ba97SEli Cohen 
713e126ba97SEli Cohen static __be32 get_rx_type(struct mlx5_ib_qp *qp, struct ib_qp_init_attr *attr)
714e126ba97SEli Cohen {
715e126ba97SEli Cohen 	if (attr->srq || (attr->qp_type == IB_QPT_XRC_TGT) ||
716e126ba97SEli Cohen 	    (attr->qp_type == IB_QPT_XRC_INI))
717e126ba97SEli Cohen 		return cpu_to_be32(MLX5_SRQ_RQ);
718e126ba97SEli Cohen 	else if (!qp->has_rq)
719e126ba97SEli Cohen 		return cpu_to_be32(MLX5_ZERO_LEN_RQ);
720e126ba97SEli Cohen 	else
721e126ba97SEli Cohen 		return cpu_to_be32(MLX5_NON_ZERO_RQ);
722e126ba97SEli Cohen }
723e126ba97SEli Cohen 
724e126ba97SEli Cohen static int is_connected(enum ib_qp_type qp_type)
725e126ba97SEli Cohen {
726e126ba97SEli Cohen 	if (qp_type == IB_QPT_RC || qp_type == IB_QPT_UC)
727e126ba97SEli Cohen 		return 1;
728e126ba97SEli Cohen 
729e126ba97SEli Cohen 	return 0;
730e126ba97SEli Cohen }
731e126ba97SEli Cohen 
732e126ba97SEli Cohen static int create_qp_common(struct mlx5_ib_dev *dev, struct ib_pd *pd,
733e126ba97SEli Cohen 			    struct ib_qp_init_attr *init_attr,
734e126ba97SEli Cohen 			    struct ib_udata *udata, struct mlx5_ib_qp *qp)
735e126ba97SEli Cohen {
736e126ba97SEli Cohen 	struct mlx5_ib_resources *devr = &dev->devr;
737e126ba97SEli Cohen 	struct mlx5_ib_create_qp_resp resp;
738e126ba97SEli Cohen 	struct mlx5_create_qp_mbox_in *in;
739e126ba97SEli Cohen 	struct mlx5_ib_create_qp ucmd;
740e126ba97SEli Cohen 	int inlen = sizeof(*in);
741e126ba97SEli Cohen 	int err;
742e126ba97SEli Cohen 
743e126ba97SEli Cohen 	mutex_init(&qp->mutex);
744e126ba97SEli Cohen 	spin_lock_init(&qp->sq.lock);
745e126ba97SEli Cohen 	spin_lock_init(&qp->rq.lock);
746e126ba97SEli Cohen 
747e126ba97SEli Cohen 	if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
748e126ba97SEli Cohen 		qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;
749e126ba97SEli Cohen 
750e126ba97SEli Cohen 	if (pd && pd->uobject) {
751e126ba97SEli Cohen 		if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
752e126ba97SEli Cohen 			mlx5_ib_dbg(dev, "copy failed\n");
753e126ba97SEli Cohen 			return -EFAULT;
754e126ba97SEli Cohen 		}
755e126ba97SEli Cohen 
756e126ba97SEli Cohen 		qp->wq_sig = !!(ucmd.flags & MLX5_QP_FLAG_SIGNATURE);
757e126ba97SEli Cohen 		qp->scat_cqe = !!(ucmd.flags & MLX5_QP_FLAG_SCATTER_CQE);
758e126ba97SEli Cohen 	} else {
759e126ba97SEli Cohen 		qp->wq_sig = !!wq_signature;
760e126ba97SEli Cohen 	}
761e126ba97SEli Cohen 
762e126ba97SEli Cohen 	qp->has_rq = qp_has_rq(init_attr);
763e126ba97SEli Cohen 	err = set_rq_size(dev, &init_attr->cap, qp->has_rq,
764e126ba97SEli Cohen 			  qp, (pd && pd->uobject) ? &ucmd : NULL);
765e126ba97SEli Cohen 	if (err) {
766e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "err %d\n", err);
767e126ba97SEli Cohen 		return err;
768e126ba97SEli Cohen 	}
769e126ba97SEli Cohen 
770e126ba97SEli Cohen 	if (pd) {
771e126ba97SEli Cohen 		if (pd->uobject) {
772e126ba97SEli Cohen 			mlx5_ib_dbg(dev, "requested sq_wqe_count (%d)\n", ucmd.sq_wqe_count);
773e126ba97SEli Cohen 			if (ucmd.rq_wqe_shift != qp->rq.wqe_shift ||
774e126ba97SEli Cohen 			    ucmd.rq_wqe_count != qp->rq.wqe_cnt) {
775e126ba97SEli Cohen 				mlx5_ib_dbg(dev, "invalid rq params\n");
776e126ba97SEli Cohen 				return -EINVAL;
777e126ba97SEli Cohen 			}
778e126ba97SEli Cohen 			if (ucmd.sq_wqe_count > dev->mdev.caps.max_wqes) {
779e126ba97SEli Cohen 				mlx5_ib_dbg(dev, "requested sq_wqe_count (%d) > max allowed (%d)\n",
780e126ba97SEli Cohen 					    ucmd.sq_wqe_count, dev->mdev.caps.max_wqes);
781e126ba97SEli Cohen 				return -EINVAL;
782e126ba97SEli Cohen 			}
783e126ba97SEli Cohen 			err = create_user_qp(dev, pd, qp, udata, &in, &resp, &inlen);
784e126ba97SEli Cohen 			if (err)
785e126ba97SEli Cohen 				mlx5_ib_dbg(dev, "err %d\n", err);
786e126ba97SEli Cohen 		} else {
787e126ba97SEli Cohen 			err = create_kernel_qp(dev, init_attr, qp, &in, &inlen);
788e126ba97SEli Cohen 			if (err)
789e126ba97SEli Cohen 				mlx5_ib_dbg(dev, "err %d\n", err);
790e126ba97SEli Cohen 			else
791e126ba97SEli Cohen 				qp->pa_lkey = to_mpd(pd)->pa_lkey;
792e126ba97SEli Cohen 		}
793e126ba97SEli Cohen 
794e126ba97SEli Cohen 		if (err)
795e126ba97SEli Cohen 			return err;
796e126ba97SEli Cohen 	} else {
797e126ba97SEli Cohen 		in = mlx5_vzalloc(sizeof(*in));
798e126ba97SEli Cohen 		if (!in)
799e126ba97SEli Cohen 			return -ENOMEM;
800e126ba97SEli Cohen 
801e126ba97SEli Cohen 		qp->create_type = MLX5_QP_EMPTY;
802e126ba97SEli Cohen 	}
803e126ba97SEli Cohen 
804e126ba97SEli Cohen 	if (is_sqp(init_attr->qp_type))
805e126ba97SEli Cohen 		qp->port = init_attr->port_num;
806e126ba97SEli Cohen 
807e126ba97SEli Cohen 	in->ctx.flags = cpu_to_be32(to_mlx5_st(init_attr->qp_type) << 16 |
808e126ba97SEli Cohen 				    MLX5_QP_PM_MIGRATED << 11);
809e126ba97SEli Cohen 
810e126ba97SEli Cohen 	if (init_attr->qp_type != MLX5_IB_QPT_REG_UMR)
811e126ba97SEli Cohen 		in->ctx.flags_pd = cpu_to_be32(to_mpd(pd ? pd : devr->p0)->pdn);
812e126ba97SEli Cohen 	else
813e126ba97SEli Cohen 		in->ctx.flags_pd = cpu_to_be32(MLX5_QP_LAT_SENSITIVE);
814e126ba97SEli Cohen 
815e126ba97SEli Cohen 	if (qp->wq_sig)
816e126ba97SEli Cohen 		in->ctx.flags_pd |= cpu_to_be32(MLX5_QP_ENABLE_SIG);
817e126ba97SEli Cohen 
818e126ba97SEli Cohen 	if (qp->scat_cqe && is_connected(init_attr->qp_type)) {
819e126ba97SEli Cohen 		int rcqe_sz;
820e126ba97SEli Cohen 		int scqe_sz;
821e126ba97SEli Cohen 
822e126ba97SEli Cohen 		rcqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->recv_cq);
823e126ba97SEli Cohen 		scqe_sz = mlx5_ib_get_cqe_size(dev, init_attr->send_cq);
824e126ba97SEli Cohen 
825e126ba97SEli Cohen 		if (rcqe_sz == 128)
826e126ba97SEli Cohen 			in->ctx.cs_res = MLX5_RES_SCAT_DATA64_CQE;
827e126ba97SEli Cohen 		else
828e126ba97SEli Cohen 			in->ctx.cs_res = MLX5_RES_SCAT_DATA32_CQE;
829e126ba97SEli Cohen 
830e126ba97SEli Cohen 		if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR) {
831e126ba97SEli Cohen 			if (scqe_sz == 128)
832e126ba97SEli Cohen 				in->ctx.cs_req = MLX5_REQ_SCAT_DATA64_CQE;
833e126ba97SEli Cohen 			else
834e126ba97SEli Cohen 				in->ctx.cs_req = MLX5_REQ_SCAT_DATA32_CQE;
835e126ba97SEli Cohen 		}
836e126ba97SEli Cohen 	}
837e126ba97SEli Cohen 
838e126ba97SEli Cohen 	if (qp->rq.wqe_cnt) {
839e126ba97SEli Cohen 		in->ctx.rq_size_stride = (qp->rq.wqe_shift - 4);
840e126ba97SEli Cohen 		in->ctx.rq_size_stride |= ilog2(qp->rq.wqe_cnt) << 3;
841e126ba97SEli Cohen 	}
842e126ba97SEli Cohen 
843e126ba97SEli Cohen 	in->ctx.rq_type_srqn = get_rx_type(qp, init_attr);
844e126ba97SEli Cohen 
845e126ba97SEli Cohen 	if (qp->sq.wqe_cnt)
846e126ba97SEli Cohen 		in->ctx.sq_crq_size |= cpu_to_be16(ilog2(qp->sq.wqe_cnt) << 11);
847e126ba97SEli Cohen 	else
848e126ba97SEli Cohen 		in->ctx.sq_crq_size |= cpu_to_be16(0x8000);
849e126ba97SEli Cohen 
850e126ba97SEli Cohen 	/* Set default resources */
851e126ba97SEli Cohen 	switch (init_attr->qp_type) {
852e126ba97SEli Cohen 	case IB_QPT_XRC_TGT:
853e126ba97SEli Cohen 		in->ctx.cqn_recv = cpu_to_be32(to_mcq(devr->c0)->mcq.cqn);
854e126ba97SEli Cohen 		in->ctx.cqn_send = cpu_to_be32(to_mcq(devr->c0)->mcq.cqn);
855e126ba97SEli Cohen 		in->ctx.rq_type_srqn |= cpu_to_be32(to_msrq(devr->s0)->msrq.srqn);
856e126ba97SEli Cohen 		in->ctx.xrcd = cpu_to_be32(to_mxrcd(init_attr->xrcd)->xrcdn);
857e126ba97SEli Cohen 		break;
858e126ba97SEli Cohen 	case IB_QPT_XRC_INI:
859e126ba97SEli Cohen 		in->ctx.cqn_recv = cpu_to_be32(to_mcq(devr->c0)->mcq.cqn);
860e126ba97SEli Cohen 		in->ctx.xrcd = cpu_to_be32(to_mxrcd(devr->x1)->xrcdn);
861e126ba97SEli Cohen 		in->ctx.rq_type_srqn |= cpu_to_be32(to_msrq(devr->s0)->msrq.srqn);
862e126ba97SEli Cohen 		break;
863e126ba97SEli Cohen 	default:
864e126ba97SEli Cohen 		if (init_attr->srq) {
865e126ba97SEli Cohen 			in->ctx.xrcd = cpu_to_be32(to_mxrcd(devr->x0)->xrcdn);
866e126ba97SEli Cohen 			in->ctx.rq_type_srqn |= cpu_to_be32(to_msrq(init_attr->srq)->msrq.srqn);
867e126ba97SEli Cohen 		} else {
868e126ba97SEli Cohen 			in->ctx.xrcd = cpu_to_be32(to_mxrcd(devr->x1)->xrcdn);
869e126ba97SEli Cohen 			in->ctx.rq_type_srqn |= cpu_to_be32(to_msrq(devr->s0)->msrq.srqn);
870e126ba97SEli Cohen 		}
871e126ba97SEli Cohen 	}
872e126ba97SEli Cohen 
873e126ba97SEli Cohen 	if (init_attr->send_cq)
874e126ba97SEli Cohen 		in->ctx.cqn_send = cpu_to_be32(to_mcq(init_attr->send_cq)->mcq.cqn);
875e126ba97SEli Cohen 
876e126ba97SEli Cohen 	if (init_attr->recv_cq)
877e126ba97SEli Cohen 		in->ctx.cqn_recv = cpu_to_be32(to_mcq(init_attr->recv_cq)->mcq.cqn);
878e126ba97SEli Cohen 
879e126ba97SEli Cohen 	in->ctx.db_rec_addr = cpu_to_be64(qp->db.dma);
880e126ba97SEli Cohen 
881e126ba97SEli Cohen 	err = mlx5_core_create_qp(&dev->mdev, &qp->mqp, in, inlen);
882e126ba97SEli Cohen 	if (err) {
883e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "create qp failed\n");
884e126ba97SEli Cohen 		goto err_create;
885e126ba97SEli Cohen 	}
886e126ba97SEli Cohen 
887e126ba97SEli Cohen 	mlx5_vfree(in);
888e126ba97SEli Cohen 	/* Hardware wants QPN written in big-endian order (after
889e126ba97SEli Cohen 	 * shifting) for send doorbell.  Precompute this value to save
890e126ba97SEli Cohen 	 * a little bit when posting sends.
891e126ba97SEli Cohen 	 */
892e126ba97SEli Cohen 	qp->doorbell_qpn = swab32(qp->mqp.qpn << 8);
893e126ba97SEli Cohen 
894e126ba97SEli Cohen 	qp->mqp.event = mlx5_ib_qp_event;
895e126ba97SEli Cohen 
896e126ba97SEli Cohen 	return 0;
897e126ba97SEli Cohen 
898e126ba97SEli Cohen err_create:
899e126ba97SEli Cohen 	if (qp->create_type == MLX5_QP_USER)
900e126ba97SEli Cohen 		destroy_qp_user(pd, qp);
901e126ba97SEli Cohen 	else if (qp->create_type == MLX5_QP_KERNEL)
902e126ba97SEli Cohen 		destroy_qp_kernel(dev, qp);
903e126ba97SEli Cohen 
904e126ba97SEli Cohen 	mlx5_vfree(in);
905e126ba97SEli Cohen 	return err;
906e126ba97SEli Cohen }
907e126ba97SEli Cohen 
908e126ba97SEli Cohen static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
909e126ba97SEli Cohen 	__acquires(&send_cq->lock) __acquires(&recv_cq->lock)
910e126ba97SEli Cohen {
911e126ba97SEli Cohen 	if (send_cq) {
912e126ba97SEli Cohen 		if (recv_cq) {
913e126ba97SEli Cohen 			if (send_cq->mcq.cqn < recv_cq->mcq.cqn)  {
914e126ba97SEli Cohen 				spin_lock_irq(&send_cq->lock);
915e126ba97SEli Cohen 				spin_lock_nested(&recv_cq->lock,
916e126ba97SEli Cohen 						 SINGLE_DEPTH_NESTING);
917e126ba97SEli Cohen 			} else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
918e126ba97SEli Cohen 				spin_lock_irq(&send_cq->lock);
919e126ba97SEli Cohen 				__acquire(&recv_cq->lock);
920e126ba97SEli Cohen 			} else {
921e126ba97SEli Cohen 				spin_lock_irq(&recv_cq->lock);
922e126ba97SEli Cohen 				spin_lock_nested(&send_cq->lock,
923e126ba97SEli Cohen 						 SINGLE_DEPTH_NESTING);
924e126ba97SEli Cohen 			}
925e126ba97SEli Cohen 		} else {
926e126ba97SEli Cohen 			spin_lock_irq(&send_cq->lock);
927e126ba97SEli Cohen 		}
928e126ba97SEli Cohen 	} else if (recv_cq) {
929e126ba97SEli Cohen 		spin_lock_irq(&recv_cq->lock);
930e126ba97SEli Cohen 	}
931e126ba97SEli Cohen }
932e126ba97SEli Cohen 
933e126ba97SEli Cohen static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
934e126ba97SEli Cohen 	__releases(&send_cq->lock) __releases(&recv_cq->lock)
935e126ba97SEli Cohen {
936e126ba97SEli Cohen 	if (send_cq) {
937e126ba97SEli Cohen 		if (recv_cq) {
938e126ba97SEli Cohen 			if (send_cq->mcq.cqn < recv_cq->mcq.cqn)  {
939e126ba97SEli Cohen 				spin_unlock(&recv_cq->lock);
940e126ba97SEli Cohen 				spin_unlock_irq(&send_cq->lock);
941e126ba97SEli Cohen 			} else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
942e126ba97SEli Cohen 				__release(&recv_cq->lock);
943e126ba97SEli Cohen 				spin_unlock_irq(&send_cq->lock);
944e126ba97SEli Cohen 			} else {
945e126ba97SEli Cohen 				spin_unlock(&send_cq->lock);
946e126ba97SEli Cohen 				spin_unlock_irq(&recv_cq->lock);
947e126ba97SEli Cohen 			}
948e126ba97SEli Cohen 		} else {
949e126ba97SEli Cohen 			spin_unlock_irq(&send_cq->lock);
950e126ba97SEli Cohen 		}
951e126ba97SEli Cohen 	} else if (recv_cq) {
952e126ba97SEli Cohen 		spin_unlock_irq(&recv_cq->lock);
953e126ba97SEli Cohen 	}
954e126ba97SEli Cohen }
955e126ba97SEli Cohen 
956e126ba97SEli Cohen static struct mlx5_ib_pd *get_pd(struct mlx5_ib_qp *qp)
957e126ba97SEli Cohen {
958e126ba97SEli Cohen 	return to_mpd(qp->ibqp.pd);
959e126ba97SEli Cohen }
960e126ba97SEli Cohen 
961e126ba97SEli Cohen static void get_cqs(struct mlx5_ib_qp *qp,
962e126ba97SEli Cohen 		    struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq)
963e126ba97SEli Cohen {
964e126ba97SEli Cohen 	switch (qp->ibqp.qp_type) {
965e126ba97SEli Cohen 	case IB_QPT_XRC_TGT:
966e126ba97SEli Cohen 		*send_cq = NULL;
967e126ba97SEli Cohen 		*recv_cq = NULL;
968e126ba97SEli Cohen 		break;
969e126ba97SEli Cohen 	case MLX5_IB_QPT_REG_UMR:
970e126ba97SEli Cohen 	case IB_QPT_XRC_INI:
971e126ba97SEli Cohen 		*send_cq = to_mcq(qp->ibqp.send_cq);
972e126ba97SEli Cohen 		*recv_cq = NULL;
973e126ba97SEli Cohen 		break;
974e126ba97SEli Cohen 
975e126ba97SEli Cohen 	case IB_QPT_SMI:
976e126ba97SEli Cohen 	case IB_QPT_GSI:
977e126ba97SEli Cohen 	case IB_QPT_RC:
978e126ba97SEli Cohen 	case IB_QPT_UC:
979e126ba97SEli Cohen 	case IB_QPT_UD:
980e126ba97SEli Cohen 	case IB_QPT_RAW_IPV6:
981e126ba97SEli Cohen 	case IB_QPT_RAW_ETHERTYPE:
982e126ba97SEli Cohen 		*send_cq = to_mcq(qp->ibqp.send_cq);
983e126ba97SEli Cohen 		*recv_cq = to_mcq(qp->ibqp.recv_cq);
984e126ba97SEli Cohen 		break;
985e126ba97SEli Cohen 
986e126ba97SEli Cohen 	case IB_QPT_RAW_PACKET:
987e126ba97SEli Cohen 	case IB_QPT_MAX:
988e126ba97SEli Cohen 	default:
989e126ba97SEli Cohen 		*send_cq = NULL;
990e126ba97SEli Cohen 		*recv_cq = NULL;
991e126ba97SEli Cohen 		break;
992e126ba97SEli Cohen 	}
993e126ba97SEli Cohen }
994e126ba97SEli Cohen 
995e126ba97SEli Cohen static void destroy_qp_common(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
996e126ba97SEli Cohen {
997e126ba97SEli Cohen 	struct mlx5_ib_cq *send_cq, *recv_cq;
998e126ba97SEli Cohen 	struct mlx5_modify_qp_mbox_in *in;
999e126ba97SEli Cohen 	int err;
1000e126ba97SEli Cohen 
1001e126ba97SEli Cohen 	in = kzalloc(sizeof(*in), GFP_KERNEL);
1002e126ba97SEli Cohen 	if (!in)
1003e126ba97SEli Cohen 		return;
1004e126ba97SEli Cohen 	if (qp->state != IB_QPS_RESET)
1005e126ba97SEli Cohen 		if (mlx5_core_qp_modify(&dev->mdev, to_mlx5_state(qp->state),
1006e126ba97SEli Cohen 					MLX5_QP_STATE_RST, in, sizeof(*in), &qp->mqp))
1007e126ba97SEli Cohen 			mlx5_ib_warn(dev, "mlx5_ib: modify QP %06x to RESET failed\n",
1008e126ba97SEli Cohen 				     qp->mqp.qpn);
1009e126ba97SEli Cohen 
1010e126ba97SEli Cohen 	get_cqs(qp, &send_cq, &recv_cq);
1011e126ba97SEli Cohen 
1012e126ba97SEli Cohen 	if (qp->create_type == MLX5_QP_KERNEL) {
1013e126ba97SEli Cohen 		mlx5_ib_lock_cqs(send_cq, recv_cq);
1014e126ba97SEli Cohen 		__mlx5_ib_cq_clean(recv_cq, qp->mqp.qpn,
1015e126ba97SEli Cohen 				   qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
1016e126ba97SEli Cohen 		if (send_cq != recv_cq)
1017e126ba97SEli Cohen 			__mlx5_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
1018e126ba97SEli Cohen 		mlx5_ib_unlock_cqs(send_cq, recv_cq);
1019e126ba97SEli Cohen 	}
1020e126ba97SEli Cohen 
1021e126ba97SEli Cohen 	err = mlx5_core_destroy_qp(&dev->mdev, &qp->mqp);
1022e126ba97SEli Cohen 	if (err)
1023e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed to destroy QP 0x%x\n", qp->mqp.qpn);
1024e126ba97SEli Cohen 	kfree(in);
1025e126ba97SEli Cohen 
1026e126ba97SEli Cohen 
1027e126ba97SEli Cohen 	if (qp->create_type == MLX5_QP_KERNEL)
1028e126ba97SEli Cohen 		destroy_qp_kernel(dev, qp);
1029e126ba97SEli Cohen 	else if (qp->create_type == MLX5_QP_USER)
1030e126ba97SEli Cohen 		destroy_qp_user(&get_pd(qp)->ibpd, qp);
1031e126ba97SEli Cohen }
1032e126ba97SEli Cohen 
1033e126ba97SEli Cohen static const char *ib_qp_type_str(enum ib_qp_type type)
1034e126ba97SEli Cohen {
1035e126ba97SEli Cohen 	switch (type) {
1036e126ba97SEli Cohen 	case IB_QPT_SMI:
1037e126ba97SEli Cohen 		return "IB_QPT_SMI";
1038e126ba97SEli Cohen 	case IB_QPT_GSI:
1039e126ba97SEli Cohen 		return "IB_QPT_GSI";
1040e126ba97SEli Cohen 	case IB_QPT_RC:
1041e126ba97SEli Cohen 		return "IB_QPT_RC";
1042e126ba97SEli Cohen 	case IB_QPT_UC:
1043e126ba97SEli Cohen 		return "IB_QPT_UC";
1044e126ba97SEli Cohen 	case IB_QPT_UD:
1045e126ba97SEli Cohen 		return "IB_QPT_UD";
1046e126ba97SEli Cohen 	case IB_QPT_RAW_IPV6:
1047e126ba97SEli Cohen 		return "IB_QPT_RAW_IPV6";
1048e126ba97SEli Cohen 	case IB_QPT_RAW_ETHERTYPE:
1049e126ba97SEli Cohen 		return "IB_QPT_RAW_ETHERTYPE";
1050e126ba97SEli Cohen 	case IB_QPT_XRC_INI:
1051e126ba97SEli Cohen 		return "IB_QPT_XRC_INI";
1052e126ba97SEli Cohen 	case IB_QPT_XRC_TGT:
1053e126ba97SEli Cohen 		return "IB_QPT_XRC_TGT";
1054e126ba97SEli Cohen 	case IB_QPT_RAW_PACKET:
1055e126ba97SEli Cohen 		return "IB_QPT_RAW_PACKET";
1056e126ba97SEli Cohen 	case MLX5_IB_QPT_REG_UMR:
1057e126ba97SEli Cohen 		return "MLX5_IB_QPT_REG_UMR";
1058e126ba97SEli Cohen 	case IB_QPT_MAX:
1059e126ba97SEli Cohen 	default:
1060e126ba97SEli Cohen 		return "Invalid QP type";
1061e126ba97SEli Cohen 	}
1062e126ba97SEli Cohen }
1063e126ba97SEli Cohen 
1064e126ba97SEli Cohen struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd,
1065e126ba97SEli Cohen 				struct ib_qp_init_attr *init_attr,
1066e126ba97SEli Cohen 				struct ib_udata *udata)
1067e126ba97SEli Cohen {
1068e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
1069e126ba97SEli Cohen 	struct mlx5_ib_qp *qp;
1070e126ba97SEli Cohen 	u16 xrcdn = 0;
1071e126ba97SEli Cohen 	int err;
1072e126ba97SEli Cohen 
1073e126ba97SEli Cohen 	if (pd) {
1074e126ba97SEli Cohen 		dev = to_mdev(pd->device);
1075e126ba97SEli Cohen 	} else {
1076e126ba97SEli Cohen 		/* being cautious here */
1077e126ba97SEli Cohen 		if (init_attr->qp_type != IB_QPT_XRC_TGT &&
1078e126ba97SEli Cohen 		    init_attr->qp_type != MLX5_IB_QPT_REG_UMR) {
1079e126ba97SEli Cohen 			pr_warn("%s: no PD for transport %s\n", __func__,
1080e126ba97SEli Cohen 				ib_qp_type_str(init_attr->qp_type));
1081e126ba97SEli Cohen 			return ERR_PTR(-EINVAL);
1082e126ba97SEli Cohen 		}
1083e126ba97SEli Cohen 		dev = to_mdev(to_mxrcd(init_attr->xrcd)->ibxrcd.device);
1084e126ba97SEli Cohen 	}
1085e126ba97SEli Cohen 
1086e126ba97SEli Cohen 	switch (init_attr->qp_type) {
1087e126ba97SEli Cohen 	case IB_QPT_XRC_TGT:
1088e126ba97SEli Cohen 	case IB_QPT_XRC_INI:
1089e126ba97SEli Cohen 		if (!(dev->mdev.caps.flags & MLX5_DEV_CAP_FLAG_XRC)) {
1090e126ba97SEli Cohen 			mlx5_ib_dbg(dev, "XRC not supported\n");
1091e126ba97SEli Cohen 			return ERR_PTR(-ENOSYS);
1092e126ba97SEli Cohen 		}
1093e126ba97SEli Cohen 		init_attr->recv_cq = NULL;
1094e126ba97SEli Cohen 		if (init_attr->qp_type == IB_QPT_XRC_TGT) {
1095e126ba97SEli Cohen 			xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
1096e126ba97SEli Cohen 			init_attr->send_cq = NULL;
1097e126ba97SEli Cohen 		}
1098e126ba97SEli Cohen 
1099e126ba97SEli Cohen 		/* fall through */
1100e126ba97SEli Cohen 	case IB_QPT_RC:
1101e126ba97SEli Cohen 	case IB_QPT_UC:
1102e126ba97SEli Cohen 	case IB_QPT_UD:
1103e126ba97SEli Cohen 	case IB_QPT_SMI:
1104e126ba97SEli Cohen 	case IB_QPT_GSI:
1105e126ba97SEli Cohen 	case MLX5_IB_QPT_REG_UMR:
1106e126ba97SEli Cohen 		qp = kzalloc(sizeof(*qp), GFP_KERNEL);
1107e126ba97SEli Cohen 		if (!qp)
1108e126ba97SEli Cohen 			return ERR_PTR(-ENOMEM);
1109e126ba97SEli Cohen 
1110e126ba97SEli Cohen 		err = create_qp_common(dev, pd, init_attr, udata, qp);
1111e126ba97SEli Cohen 		if (err) {
1112e126ba97SEli Cohen 			mlx5_ib_dbg(dev, "create_qp_common failed\n");
1113e126ba97SEli Cohen 			kfree(qp);
1114e126ba97SEli Cohen 			return ERR_PTR(err);
1115e126ba97SEli Cohen 		}
1116e126ba97SEli Cohen 
1117e126ba97SEli Cohen 		if (is_qp0(init_attr->qp_type))
1118e126ba97SEli Cohen 			qp->ibqp.qp_num = 0;
1119e126ba97SEli Cohen 		else if (is_qp1(init_attr->qp_type))
1120e126ba97SEli Cohen 			qp->ibqp.qp_num = 1;
1121e126ba97SEli Cohen 		else
1122e126ba97SEli Cohen 			qp->ibqp.qp_num = qp->mqp.qpn;
1123e126ba97SEli Cohen 
1124e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "ib qpnum 0x%x, mlx qpn 0x%x, rcqn 0x%x, scqn 0x%x\n",
1125e126ba97SEli Cohen 			    qp->ibqp.qp_num, qp->mqp.qpn, to_mcq(init_attr->recv_cq)->mcq.cqn,
1126e126ba97SEli Cohen 			    to_mcq(init_attr->send_cq)->mcq.cqn);
1127e126ba97SEli Cohen 
1128e126ba97SEli Cohen 		qp->xrcdn = xrcdn;
1129e126ba97SEli Cohen 
1130e126ba97SEli Cohen 		break;
1131e126ba97SEli Cohen 
1132e126ba97SEli Cohen 	case IB_QPT_RAW_IPV6:
1133e126ba97SEli Cohen 	case IB_QPT_RAW_ETHERTYPE:
1134e126ba97SEli Cohen 	case IB_QPT_RAW_PACKET:
1135e126ba97SEli Cohen 	case IB_QPT_MAX:
1136e126ba97SEli Cohen 	default:
1137e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "unsupported qp type %d\n",
1138e126ba97SEli Cohen 			    init_attr->qp_type);
1139e126ba97SEli Cohen 		/* Don't support raw QPs */
1140e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
1141e126ba97SEli Cohen 	}
1142e126ba97SEli Cohen 
1143e126ba97SEli Cohen 	return &qp->ibqp;
1144e126ba97SEli Cohen }
1145e126ba97SEli Cohen 
1146e126ba97SEli Cohen int mlx5_ib_destroy_qp(struct ib_qp *qp)
1147e126ba97SEli Cohen {
1148e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
1149e126ba97SEli Cohen 	struct mlx5_ib_qp *mqp = to_mqp(qp);
1150e126ba97SEli Cohen 
1151e126ba97SEli Cohen 	destroy_qp_common(dev, mqp);
1152e126ba97SEli Cohen 
1153e126ba97SEli Cohen 	kfree(mqp);
1154e126ba97SEli Cohen 
1155e126ba97SEli Cohen 	return 0;
1156e126ba97SEli Cohen }
1157e126ba97SEli Cohen 
1158e126ba97SEli Cohen static __be32 to_mlx5_access_flags(struct mlx5_ib_qp *qp, const struct ib_qp_attr *attr,
1159e126ba97SEli Cohen 				   int attr_mask)
1160e126ba97SEli Cohen {
1161e126ba97SEli Cohen 	u32 hw_access_flags = 0;
1162e126ba97SEli Cohen 	u8 dest_rd_atomic;
1163e126ba97SEli Cohen 	u32 access_flags;
1164e126ba97SEli Cohen 
1165e126ba97SEli Cohen 	if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1166e126ba97SEli Cohen 		dest_rd_atomic = attr->max_dest_rd_atomic;
1167e126ba97SEli Cohen 	else
1168e126ba97SEli Cohen 		dest_rd_atomic = qp->resp_depth;
1169e126ba97SEli Cohen 
1170e126ba97SEli Cohen 	if (attr_mask & IB_QP_ACCESS_FLAGS)
1171e126ba97SEli Cohen 		access_flags = attr->qp_access_flags;
1172e126ba97SEli Cohen 	else
1173e126ba97SEli Cohen 		access_flags = qp->atomic_rd_en;
1174e126ba97SEli Cohen 
1175e126ba97SEli Cohen 	if (!dest_rd_atomic)
1176e126ba97SEli Cohen 		access_flags &= IB_ACCESS_REMOTE_WRITE;
1177e126ba97SEli Cohen 
1178e126ba97SEli Cohen 	if (access_flags & IB_ACCESS_REMOTE_READ)
1179e126ba97SEli Cohen 		hw_access_flags |= MLX5_QP_BIT_RRE;
1180e126ba97SEli Cohen 	if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
1181e126ba97SEli Cohen 		hw_access_flags |= (MLX5_QP_BIT_RAE | MLX5_ATOMIC_MODE_CX);
1182e126ba97SEli Cohen 	if (access_flags & IB_ACCESS_REMOTE_WRITE)
1183e126ba97SEli Cohen 		hw_access_flags |= MLX5_QP_BIT_RWE;
1184e126ba97SEli Cohen 
1185e126ba97SEli Cohen 	return cpu_to_be32(hw_access_flags);
1186e126ba97SEli Cohen }
1187e126ba97SEli Cohen 
1188e126ba97SEli Cohen enum {
1189e126ba97SEli Cohen 	MLX5_PATH_FLAG_FL	= 1 << 0,
1190e126ba97SEli Cohen 	MLX5_PATH_FLAG_FREE_AR	= 1 << 1,
1191e126ba97SEli Cohen 	MLX5_PATH_FLAG_COUNTER	= 1 << 2,
1192e126ba97SEli Cohen };
1193e126ba97SEli Cohen 
1194e126ba97SEli Cohen static int ib_rate_to_mlx5(struct mlx5_ib_dev *dev, u8 rate)
1195e126ba97SEli Cohen {
1196e126ba97SEli Cohen 	if (rate == IB_RATE_PORT_CURRENT) {
1197e126ba97SEli Cohen 		return 0;
1198e126ba97SEli Cohen 	} else if (rate < IB_RATE_2_5_GBPS || rate > IB_RATE_300_GBPS) {
1199e126ba97SEli Cohen 		return -EINVAL;
1200e126ba97SEli Cohen 	} else {
1201e126ba97SEli Cohen 		while (rate != IB_RATE_2_5_GBPS &&
1202e126ba97SEli Cohen 		       !(1 << (rate + MLX5_STAT_RATE_OFFSET) &
1203e126ba97SEli Cohen 			 dev->mdev.caps.stat_rate_support))
1204e126ba97SEli Cohen 			--rate;
1205e126ba97SEli Cohen 	}
1206e126ba97SEli Cohen 
1207e126ba97SEli Cohen 	return rate + MLX5_STAT_RATE_OFFSET;
1208e126ba97SEli Cohen }
1209e126ba97SEli Cohen 
1210e126ba97SEli Cohen static int mlx5_set_path(struct mlx5_ib_dev *dev, const struct ib_ah_attr *ah,
1211e126ba97SEli Cohen 			 struct mlx5_qp_path *path, u8 port, int attr_mask,
1212e126ba97SEli Cohen 			 u32 path_flags, const struct ib_qp_attr *attr)
1213e126ba97SEli Cohen {
1214e126ba97SEli Cohen 	int err;
1215e126ba97SEli Cohen 
1216e126ba97SEli Cohen 	path->fl = (path_flags & MLX5_PATH_FLAG_FL) ? 0x80 : 0;
1217e126ba97SEli Cohen 	path->free_ar = (path_flags & MLX5_PATH_FLAG_FREE_AR) ? 0x80 : 0;
1218e126ba97SEli Cohen 
1219e126ba97SEli Cohen 	if (attr_mask & IB_QP_PKEY_INDEX)
1220e126ba97SEli Cohen 		path->pkey_index = attr->pkey_index;
1221e126ba97SEli Cohen 
1222e126ba97SEli Cohen 	path->grh_mlid	= ah->src_path_bits & 0x7f;
1223e126ba97SEli Cohen 	path->rlid	= cpu_to_be16(ah->dlid);
1224e126ba97SEli Cohen 
1225e126ba97SEli Cohen 	if (ah->ah_flags & IB_AH_GRH) {
1226e126ba97SEli Cohen 		path->grh_mlid |= 1 << 7;
1227e126ba97SEli Cohen 		path->mgid_index = ah->grh.sgid_index;
1228e126ba97SEli Cohen 		path->hop_limit  = ah->grh.hop_limit;
1229e126ba97SEli Cohen 		path->tclass_flowlabel =
1230e126ba97SEli Cohen 			cpu_to_be32((ah->grh.traffic_class << 20) |
1231e126ba97SEli Cohen 				    (ah->grh.flow_label));
1232e126ba97SEli Cohen 		memcpy(path->rgid, ah->grh.dgid.raw, 16);
1233e126ba97SEli Cohen 	}
1234e126ba97SEli Cohen 
1235e126ba97SEli Cohen 	err = ib_rate_to_mlx5(dev, ah->static_rate);
1236e126ba97SEli Cohen 	if (err < 0)
1237e126ba97SEli Cohen 		return err;
1238e126ba97SEli Cohen 	path->static_rate = err;
1239e126ba97SEli Cohen 	path->port = port;
1240e126ba97SEli Cohen 
1241e126ba97SEli Cohen 	if (ah->ah_flags & IB_AH_GRH) {
1242e126ba97SEli Cohen 		if (ah->grh.sgid_index >= dev->mdev.caps.port[port - 1].gid_table_len) {
1243e126ba97SEli Cohen 			pr_err(KERN_ERR "sgid_index (%u) too large. max is %d\n",
1244e126ba97SEli Cohen 			       ah->grh.sgid_index, dev->mdev.caps.port[port - 1].gid_table_len);
1245e126ba97SEli Cohen 			return -EINVAL;
1246e126ba97SEli Cohen 		}
1247e126ba97SEli Cohen 
1248e126ba97SEli Cohen 		path->grh_mlid |= 1 << 7;
1249e126ba97SEli Cohen 		path->mgid_index = ah->grh.sgid_index;
1250e126ba97SEli Cohen 		path->hop_limit  = ah->grh.hop_limit;
1251e126ba97SEli Cohen 		path->tclass_flowlabel =
1252e126ba97SEli Cohen 			cpu_to_be32((ah->grh.traffic_class << 20) |
1253e126ba97SEli Cohen 				    (ah->grh.flow_label));
1254e126ba97SEli Cohen 		memcpy(path->rgid, ah->grh.dgid.raw, 16);
1255e126ba97SEli Cohen 	}
1256e126ba97SEli Cohen 
1257e126ba97SEli Cohen 	if (attr_mask & IB_QP_TIMEOUT)
1258e126ba97SEli Cohen 		path->ackto_lt = attr->timeout << 3;
1259e126ba97SEli Cohen 
1260e126ba97SEli Cohen 	path->sl = ah->sl & 0xf;
1261e126ba97SEli Cohen 
1262e126ba97SEli Cohen 	return 0;
1263e126ba97SEli Cohen }
1264e126ba97SEli Cohen 
1265e126ba97SEli Cohen static enum mlx5_qp_optpar opt_mask[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE][MLX5_QP_ST_MAX] = {
1266e126ba97SEli Cohen 	[MLX5_QP_STATE_INIT] = {
1267e126ba97SEli Cohen 		[MLX5_QP_STATE_INIT] = {
1268e126ba97SEli Cohen 			[MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE		|
1269e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RAE		|
1270e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RWE		|
1271e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_PKEY_INDEX	|
1272e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_PRI_PORT,
1273e126ba97SEli Cohen 			[MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE		|
1274e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_PKEY_INDEX	|
1275e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_PRI_PORT,
1276e126ba97SEli Cohen 			[MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX	|
1277e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_Q_KEY		|
1278e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_PRI_PORT,
1279e126ba97SEli Cohen 		},
1280e126ba97SEli Cohen 		[MLX5_QP_STATE_RTR] = {
1281e126ba97SEli Cohen 			[MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH  |
1282e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RRE            |
1283e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RAE            |
1284e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RWE            |
1285e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_PKEY_INDEX,
1286e126ba97SEli Cohen 			[MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH  |
1287e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RWE            |
1288e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_PKEY_INDEX,
1289e126ba97SEli Cohen 			[MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX     |
1290e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_Q_KEY,
1291e126ba97SEli Cohen 			[MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_PKEY_INDEX	|
1292e126ba97SEli Cohen 					   MLX5_QP_OPTPAR_Q_KEY,
1293a4774e90SEli Cohen 			[MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
1294a4774e90SEli Cohen 					  MLX5_QP_OPTPAR_RRE            |
1295a4774e90SEli Cohen 					  MLX5_QP_OPTPAR_RAE            |
1296a4774e90SEli Cohen 					  MLX5_QP_OPTPAR_RWE            |
1297a4774e90SEli Cohen 					  MLX5_QP_OPTPAR_PKEY_INDEX,
1298e126ba97SEli Cohen 		},
1299e126ba97SEli Cohen 	},
1300e126ba97SEli Cohen 	[MLX5_QP_STATE_RTR] = {
1301e126ba97SEli Cohen 		[MLX5_QP_STATE_RTS] = {
1302e126ba97SEli Cohen 			[MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH	|
1303e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RRE		|
1304e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RAE		|
1305e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RWE		|
1306e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_PM_STATE	|
1307e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RNR_TIMEOUT,
1308e126ba97SEli Cohen 			[MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH	|
1309e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RWE		|
1310e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_PM_STATE,
1311e126ba97SEli Cohen 			[MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
1312e126ba97SEli Cohen 		},
1313e126ba97SEli Cohen 	},
1314e126ba97SEli Cohen 	[MLX5_QP_STATE_RTS] = {
1315e126ba97SEli Cohen 		[MLX5_QP_STATE_RTS] = {
1316e126ba97SEli Cohen 			[MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE		|
1317e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RAE		|
1318e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RWE		|
1319e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_RNR_TIMEOUT	|
1320e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_PM_STATE,
1321e126ba97SEli Cohen 			[MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE		|
1322e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_PM_STATE,
1323e126ba97SEli Cohen 			[MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY		|
1324e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_SRQN		|
1325e126ba97SEli Cohen 					  MLX5_QP_OPTPAR_CQN_RCV,
1326e126ba97SEli Cohen 		},
1327e126ba97SEli Cohen 	},
1328e126ba97SEli Cohen 	[MLX5_QP_STATE_SQER] = {
1329e126ba97SEli Cohen 		[MLX5_QP_STATE_RTS] = {
1330e126ba97SEli Cohen 			[MLX5_QP_ST_UD]	 = MLX5_QP_OPTPAR_Q_KEY,
1331e126ba97SEli Cohen 			[MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_Q_KEY,
133275959f56SEli Cohen 			[MLX5_QP_ST_UC]	 = MLX5_QP_OPTPAR_RWE,
1333a4774e90SEli Cohen 			[MLX5_QP_ST_RC]	 = MLX5_QP_OPTPAR_RNR_TIMEOUT	|
1334a4774e90SEli Cohen 					   MLX5_QP_OPTPAR_RWE		|
1335a4774e90SEli Cohen 					   MLX5_QP_OPTPAR_RAE		|
1336a4774e90SEli Cohen 					   MLX5_QP_OPTPAR_RRE,
1337e126ba97SEli Cohen 		},
1338e126ba97SEli Cohen 	},
1339e126ba97SEli Cohen };
1340e126ba97SEli Cohen 
1341e126ba97SEli Cohen static int ib_nr_to_mlx5_nr(int ib_mask)
1342e126ba97SEli Cohen {
1343e126ba97SEli Cohen 	switch (ib_mask) {
1344e126ba97SEli Cohen 	case IB_QP_STATE:
1345e126ba97SEli Cohen 		return 0;
1346e126ba97SEli Cohen 	case IB_QP_CUR_STATE:
1347e126ba97SEli Cohen 		return 0;
1348e126ba97SEli Cohen 	case IB_QP_EN_SQD_ASYNC_NOTIFY:
1349e126ba97SEli Cohen 		return 0;
1350e126ba97SEli Cohen 	case IB_QP_ACCESS_FLAGS:
1351e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_RWE | MLX5_QP_OPTPAR_RRE |
1352e126ba97SEli Cohen 			MLX5_QP_OPTPAR_RAE;
1353e126ba97SEli Cohen 	case IB_QP_PKEY_INDEX:
1354e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_PKEY_INDEX;
1355e126ba97SEli Cohen 	case IB_QP_PORT:
1356e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_PRI_PORT;
1357e126ba97SEli Cohen 	case IB_QP_QKEY:
1358e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_Q_KEY;
1359e126ba97SEli Cohen 	case IB_QP_AV:
1360e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH |
1361e126ba97SEli Cohen 			MLX5_QP_OPTPAR_PRI_PORT;
1362e126ba97SEli Cohen 	case IB_QP_PATH_MTU:
1363e126ba97SEli Cohen 		return 0;
1364e126ba97SEli Cohen 	case IB_QP_TIMEOUT:
1365e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_ACK_TIMEOUT;
1366e126ba97SEli Cohen 	case IB_QP_RETRY_CNT:
1367e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_RETRY_COUNT;
1368e126ba97SEli Cohen 	case IB_QP_RNR_RETRY:
1369e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_RNR_RETRY;
1370e126ba97SEli Cohen 	case IB_QP_RQ_PSN:
1371e126ba97SEli Cohen 		return 0;
1372e126ba97SEli Cohen 	case IB_QP_MAX_QP_RD_ATOMIC:
1373e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_SRA_MAX;
1374e126ba97SEli Cohen 	case IB_QP_ALT_PATH:
1375e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_ALT_ADDR_PATH;
1376e126ba97SEli Cohen 	case IB_QP_MIN_RNR_TIMER:
1377e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_RNR_TIMEOUT;
1378e126ba97SEli Cohen 	case IB_QP_SQ_PSN:
1379e126ba97SEli Cohen 		return 0;
1380e126ba97SEli Cohen 	case IB_QP_MAX_DEST_RD_ATOMIC:
1381e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_RRA_MAX | MLX5_QP_OPTPAR_RWE |
1382e126ba97SEli Cohen 			MLX5_QP_OPTPAR_RRE | MLX5_QP_OPTPAR_RAE;
1383e126ba97SEli Cohen 	case IB_QP_PATH_MIG_STATE:
1384e126ba97SEli Cohen 		return MLX5_QP_OPTPAR_PM_STATE;
1385e126ba97SEli Cohen 	case IB_QP_CAP:
1386e126ba97SEli Cohen 		return 0;
1387e126ba97SEli Cohen 	case IB_QP_DEST_QPN:
1388e126ba97SEli Cohen 		return 0;
1389e126ba97SEli Cohen 	}
1390e126ba97SEli Cohen 	return 0;
1391e126ba97SEli Cohen }
1392e126ba97SEli Cohen 
1393e126ba97SEli Cohen static int ib_mask_to_mlx5_opt(int ib_mask)
1394e126ba97SEli Cohen {
1395e126ba97SEli Cohen 	int result = 0;
1396e126ba97SEli Cohen 	int i;
1397e126ba97SEli Cohen 
1398e126ba97SEli Cohen 	for (i = 0; i < 8 * sizeof(int); i++) {
1399e126ba97SEli Cohen 		if ((1 << i) & ib_mask)
1400e126ba97SEli Cohen 			result |= ib_nr_to_mlx5_nr(1 << i);
1401e126ba97SEli Cohen 	}
1402e126ba97SEli Cohen 
1403e126ba97SEli Cohen 	return result;
1404e126ba97SEli Cohen }
1405e126ba97SEli Cohen 
1406e126ba97SEli Cohen static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
1407e126ba97SEli Cohen 			       const struct ib_qp_attr *attr, int attr_mask,
1408e126ba97SEli Cohen 			       enum ib_qp_state cur_state, enum ib_qp_state new_state)
1409e126ba97SEli Cohen {
1410e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
1411e126ba97SEli Cohen 	struct mlx5_ib_qp *qp = to_mqp(ibqp);
1412e126ba97SEli Cohen 	struct mlx5_ib_cq *send_cq, *recv_cq;
1413e126ba97SEli Cohen 	struct mlx5_qp_context *context;
1414e126ba97SEli Cohen 	struct mlx5_modify_qp_mbox_in *in;
1415e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
1416e126ba97SEli Cohen 	enum mlx5_qp_state mlx5_cur, mlx5_new;
1417e126ba97SEli Cohen 	enum mlx5_qp_optpar optpar;
1418e126ba97SEli Cohen 	int sqd_event;
1419e126ba97SEli Cohen 	int mlx5_st;
1420e126ba97SEli Cohen 	int err;
1421e126ba97SEli Cohen 
1422e126ba97SEli Cohen 	in = kzalloc(sizeof(*in), GFP_KERNEL);
1423e126ba97SEli Cohen 	if (!in)
1424e126ba97SEli Cohen 		return -ENOMEM;
1425e126ba97SEli Cohen 
1426e126ba97SEli Cohen 	context = &in->ctx;
1427e126ba97SEli Cohen 	err = to_mlx5_st(ibqp->qp_type);
1428e126ba97SEli Cohen 	if (err < 0)
1429e126ba97SEli Cohen 		goto out;
1430e126ba97SEli Cohen 
1431e126ba97SEli Cohen 	context->flags = cpu_to_be32(err << 16);
1432e126ba97SEli Cohen 
1433e126ba97SEli Cohen 	if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
1434e126ba97SEli Cohen 		context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
1435e126ba97SEli Cohen 	} else {
1436e126ba97SEli Cohen 		switch (attr->path_mig_state) {
1437e126ba97SEli Cohen 		case IB_MIG_MIGRATED:
1438e126ba97SEli Cohen 			context->flags |= cpu_to_be32(MLX5_QP_PM_MIGRATED << 11);
1439e126ba97SEli Cohen 			break;
1440e126ba97SEli Cohen 		case IB_MIG_REARM:
1441e126ba97SEli Cohen 			context->flags |= cpu_to_be32(MLX5_QP_PM_REARM << 11);
1442e126ba97SEli Cohen 			break;
1443e126ba97SEli Cohen 		case IB_MIG_ARMED:
1444e126ba97SEli Cohen 			context->flags |= cpu_to_be32(MLX5_QP_PM_ARMED << 11);
1445e126ba97SEli Cohen 			break;
1446e126ba97SEli Cohen 		}
1447e126ba97SEli Cohen 	}
1448e126ba97SEli Cohen 
1449e126ba97SEli Cohen 	if (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI) {
1450e126ba97SEli Cohen 		context->mtu_msgmax = (IB_MTU_256 << 5) | 8;
1451e126ba97SEli Cohen 	} else if (ibqp->qp_type == IB_QPT_UD ||
1452e126ba97SEli Cohen 		   ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
1453e126ba97SEli Cohen 		context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
1454e126ba97SEli Cohen 	} else if (attr_mask & IB_QP_PATH_MTU) {
1455e126ba97SEli Cohen 		if (attr->path_mtu < IB_MTU_256 ||
1456e126ba97SEli Cohen 		    attr->path_mtu > IB_MTU_4096) {
1457e126ba97SEli Cohen 			mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
1458e126ba97SEli Cohen 			err = -EINVAL;
1459e126ba97SEli Cohen 			goto out;
1460e126ba97SEli Cohen 		}
1461e126ba97SEli Cohen 		context->mtu_msgmax = (attr->path_mtu << 5) | dev->mdev.caps.log_max_msg;
1462e126ba97SEli Cohen 	}
1463e126ba97SEli Cohen 
1464e126ba97SEli Cohen 	if (attr_mask & IB_QP_DEST_QPN)
1465e126ba97SEli Cohen 		context->log_pg_sz_remote_qpn = cpu_to_be32(attr->dest_qp_num);
1466e126ba97SEli Cohen 
1467e126ba97SEli Cohen 	if (attr_mask & IB_QP_PKEY_INDEX)
1468e126ba97SEli Cohen 		context->pri_path.pkey_index = attr->pkey_index;
1469e126ba97SEli Cohen 
1470e126ba97SEli Cohen 	/* todo implement counter_index functionality */
1471e126ba97SEli Cohen 
1472e126ba97SEli Cohen 	if (is_sqp(ibqp->qp_type))
1473e126ba97SEli Cohen 		context->pri_path.port = qp->port;
1474e126ba97SEli Cohen 
1475e126ba97SEli Cohen 	if (attr_mask & IB_QP_PORT)
1476e126ba97SEli Cohen 		context->pri_path.port = attr->port_num;
1477e126ba97SEli Cohen 
1478e126ba97SEli Cohen 	if (attr_mask & IB_QP_AV) {
1479e126ba97SEli Cohen 		err = mlx5_set_path(dev, &attr->ah_attr, &context->pri_path,
1480e126ba97SEli Cohen 				    attr_mask & IB_QP_PORT ? attr->port_num : qp->port,
1481e126ba97SEli Cohen 				    attr_mask, 0, attr);
1482e126ba97SEli Cohen 		if (err)
1483e126ba97SEli Cohen 			goto out;
1484e126ba97SEli Cohen 	}
1485e126ba97SEli Cohen 
1486e126ba97SEli Cohen 	if (attr_mask & IB_QP_TIMEOUT)
1487e126ba97SEli Cohen 		context->pri_path.ackto_lt |= attr->timeout << 3;
1488e126ba97SEli Cohen 
1489e126ba97SEli Cohen 	if (attr_mask & IB_QP_ALT_PATH) {
1490e126ba97SEli Cohen 		err = mlx5_set_path(dev, &attr->alt_ah_attr, &context->alt_path,
1491e126ba97SEli Cohen 				    attr->alt_port_num, attr_mask, 0, attr);
1492e126ba97SEli Cohen 		if (err)
1493e126ba97SEli Cohen 			goto out;
1494e126ba97SEli Cohen 	}
1495e126ba97SEli Cohen 
1496e126ba97SEli Cohen 	pd = get_pd(qp);
1497e126ba97SEli Cohen 	get_cqs(qp, &send_cq, &recv_cq);
1498e126ba97SEli Cohen 
1499e126ba97SEli Cohen 	context->flags_pd = cpu_to_be32(pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
1500e126ba97SEli Cohen 	context->cqn_send = send_cq ? cpu_to_be32(send_cq->mcq.cqn) : 0;
1501e126ba97SEli Cohen 	context->cqn_recv = recv_cq ? cpu_to_be32(recv_cq->mcq.cqn) : 0;
1502e126ba97SEli Cohen 	context->params1  = cpu_to_be32(MLX5_IB_ACK_REQ_FREQ << 28);
1503e126ba97SEli Cohen 
1504e126ba97SEli Cohen 	if (attr_mask & IB_QP_RNR_RETRY)
1505e126ba97SEli Cohen 		context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
1506e126ba97SEli Cohen 
1507e126ba97SEli Cohen 	if (attr_mask & IB_QP_RETRY_CNT)
1508e126ba97SEli Cohen 		context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
1509e126ba97SEli Cohen 
1510e126ba97SEli Cohen 	if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
1511e126ba97SEli Cohen 		if (attr->max_rd_atomic)
1512e126ba97SEli Cohen 			context->params1 |=
1513e126ba97SEli Cohen 				cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
1514e126ba97SEli Cohen 	}
1515e126ba97SEli Cohen 
1516e126ba97SEli Cohen 	if (attr_mask & IB_QP_SQ_PSN)
1517e126ba97SEli Cohen 		context->next_send_psn = cpu_to_be32(attr->sq_psn);
1518e126ba97SEli Cohen 
1519e126ba97SEli Cohen 	if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
1520e126ba97SEli Cohen 		if (attr->max_dest_rd_atomic)
1521e126ba97SEli Cohen 			context->params2 |=
1522e126ba97SEli Cohen 				cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
1523e126ba97SEli Cohen 	}
1524e126ba97SEli Cohen 
1525e126ba97SEli Cohen 	if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC))
1526e126ba97SEli Cohen 		context->params2 |= to_mlx5_access_flags(qp, attr, attr_mask);
1527e126ba97SEli Cohen 
1528e126ba97SEli Cohen 	if (attr_mask & IB_QP_MIN_RNR_TIMER)
1529e126ba97SEli Cohen 		context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
1530e126ba97SEli Cohen 
1531e126ba97SEli Cohen 	if (attr_mask & IB_QP_RQ_PSN)
1532e126ba97SEli Cohen 		context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
1533e126ba97SEli Cohen 
1534e126ba97SEli Cohen 	if (attr_mask & IB_QP_QKEY)
1535e126ba97SEli Cohen 		context->qkey = cpu_to_be32(attr->qkey);
1536e126ba97SEli Cohen 
1537e126ba97SEli Cohen 	if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
1538e126ba97SEli Cohen 		context->db_rec_addr = cpu_to_be64(qp->db.dma);
1539e126ba97SEli Cohen 
1540e126ba97SEli Cohen 	if (cur_state == IB_QPS_RTS && new_state == IB_QPS_SQD	&&
1541e126ba97SEli Cohen 	    attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY && attr->en_sqd_async_notify)
1542e126ba97SEli Cohen 		sqd_event = 1;
1543e126ba97SEli Cohen 	else
1544e126ba97SEli Cohen 		sqd_event = 0;
1545e126ba97SEli Cohen 
1546e126ba97SEli Cohen 	if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
1547e126ba97SEli Cohen 		context->sq_crq_size |= cpu_to_be16(1 << 4);
1548e126ba97SEli Cohen 
1549e126ba97SEli Cohen 
1550e126ba97SEli Cohen 	mlx5_cur = to_mlx5_state(cur_state);
1551e126ba97SEli Cohen 	mlx5_new = to_mlx5_state(new_state);
1552e126ba97SEli Cohen 	mlx5_st = to_mlx5_st(ibqp->qp_type);
1553e126ba97SEli Cohen 	if (mlx5_cur < 0 || mlx5_new < 0 || mlx5_st < 0)
1554e126ba97SEli Cohen 		goto out;
1555e126ba97SEli Cohen 
1556e126ba97SEli Cohen 	optpar = ib_mask_to_mlx5_opt(attr_mask);
1557e126ba97SEli Cohen 	optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
1558e126ba97SEli Cohen 	in->optparam = cpu_to_be32(optpar);
1559e126ba97SEli Cohen 	err = mlx5_core_qp_modify(&dev->mdev, to_mlx5_state(cur_state),
1560e126ba97SEli Cohen 				  to_mlx5_state(new_state), in, sqd_event,
1561e126ba97SEli Cohen 				  &qp->mqp);
1562e126ba97SEli Cohen 	if (err)
1563e126ba97SEli Cohen 		goto out;
1564e126ba97SEli Cohen 
1565e126ba97SEli Cohen 	qp->state = new_state;
1566e126ba97SEli Cohen 
1567e126ba97SEli Cohen 	if (attr_mask & IB_QP_ACCESS_FLAGS)
1568e126ba97SEli Cohen 		qp->atomic_rd_en = attr->qp_access_flags;
1569e126ba97SEli Cohen 	if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1570e126ba97SEli Cohen 		qp->resp_depth = attr->max_dest_rd_atomic;
1571e126ba97SEli Cohen 	if (attr_mask & IB_QP_PORT)
1572e126ba97SEli Cohen 		qp->port = attr->port_num;
1573e126ba97SEli Cohen 	if (attr_mask & IB_QP_ALT_PATH)
1574e126ba97SEli Cohen 		qp->alt_port = attr->alt_port_num;
1575e126ba97SEli Cohen 
1576e126ba97SEli Cohen 	/*
1577e126ba97SEli Cohen 	 * If we moved a kernel QP to RESET, clean up all old CQ
1578e126ba97SEli Cohen 	 * entries and reinitialize the QP.
1579e126ba97SEli Cohen 	 */
1580e126ba97SEli Cohen 	if (new_state == IB_QPS_RESET && !ibqp->uobject) {
1581e126ba97SEli Cohen 		mlx5_ib_cq_clean(recv_cq, qp->mqp.qpn,
1582e126ba97SEli Cohen 				 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
1583e126ba97SEli Cohen 		if (send_cq != recv_cq)
1584e126ba97SEli Cohen 			mlx5_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
1585e126ba97SEli Cohen 
1586e126ba97SEli Cohen 		qp->rq.head = 0;
1587e126ba97SEli Cohen 		qp->rq.tail = 0;
1588e126ba97SEli Cohen 		qp->sq.head = 0;
1589e126ba97SEli Cohen 		qp->sq.tail = 0;
1590e126ba97SEli Cohen 		qp->sq.cur_post = 0;
1591e126ba97SEli Cohen 		qp->sq.last_poll = 0;
1592e126ba97SEli Cohen 		qp->db.db[MLX5_RCV_DBR] = 0;
1593e126ba97SEli Cohen 		qp->db.db[MLX5_SND_DBR] = 0;
1594e126ba97SEli Cohen 	}
1595e126ba97SEli Cohen 
1596e126ba97SEli Cohen out:
1597e126ba97SEli Cohen 	kfree(in);
1598e126ba97SEli Cohen 	return err;
1599e126ba97SEli Cohen }
1600e126ba97SEli Cohen 
1601e126ba97SEli Cohen int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1602e126ba97SEli Cohen 		      int attr_mask, struct ib_udata *udata)
1603e126ba97SEli Cohen {
1604e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
1605e126ba97SEli Cohen 	struct mlx5_ib_qp *qp = to_mqp(ibqp);
1606e126ba97SEli Cohen 	enum ib_qp_state cur_state, new_state;
1607e126ba97SEli Cohen 	int err = -EINVAL;
1608e126ba97SEli Cohen 	int port;
1609e126ba97SEli Cohen 
1610e126ba97SEli Cohen 	mutex_lock(&qp->mutex);
1611e126ba97SEli Cohen 
1612e126ba97SEli Cohen 	cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
1613e126ba97SEli Cohen 	new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
1614e126ba97SEli Cohen 
1615e126ba97SEli Cohen 	if (ibqp->qp_type != MLX5_IB_QPT_REG_UMR &&
1616e126ba97SEli Cohen 	    !ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type, attr_mask))
1617e126ba97SEli Cohen 		goto out;
1618e126ba97SEli Cohen 
1619e126ba97SEli Cohen 	if ((attr_mask & IB_QP_PORT) &&
1620e126ba97SEli Cohen 	    (attr->port_num == 0 || attr->port_num > dev->mdev.caps.num_ports))
1621e126ba97SEli Cohen 		goto out;
1622e126ba97SEli Cohen 
1623e126ba97SEli Cohen 	if (attr_mask & IB_QP_PKEY_INDEX) {
1624e126ba97SEli Cohen 		port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
1625e126ba97SEli Cohen 		if (attr->pkey_index >= dev->mdev.caps.port[port - 1].pkey_table_len)
1626e126ba97SEli Cohen 			goto out;
1627e126ba97SEli Cohen 	}
1628e126ba97SEli Cohen 
1629e126ba97SEli Cohen 	if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
1630e126ba97SEli Cohen 	    attr->max_rd_atomic > dev->mdev.caps.max_ra_res_qp)
1631e126ba97SEli Cohen 		goto out;
1632e126ba97SEli Cohen 
1633e126ba97SEli Cohen 	if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
1634e126ba97SEli Cohen 	    attr->max_dest_rd_atomic > dev->mdev.caps.max_ra_req_qp)
1635e126ba97SEli Cohen 		goto out;
1636e126ba97SEli Cohen 
1637e126ba97SEli Cohen 	if (cur_state == new_state && cur_state == IB_QPS_RESET) {
1638e126ba97SEli Cohen 		err = 0;
1639e126ba97SEli Cohen 		goto out;
1640e126ba97SEli Cohen 	}
1641e126ba97SEli Cohen 
1642e126ba97SEli Cohen 	err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
1643e126ba97SEli Cohen 
1644e126ba97SEli Cohen out:
1645e126ba97SEli Cohen 	mutex_unlock(&qp->mutex);
1646e126ba97SEli Cohen 	return err;
1647e126ba97SEli Cohen }
1648e126ba97SEli Cohen 
1649e126ba97SEli Cohen static int mlx5_wq_overflow(struct mlx5_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
1650e126ba97SEli Cohen {
1651e126ba97SEli Cohen 	struct mlx5_ib_cq *cq;
1652e126ba97SEli Cohen 	unsigned cur;
1653e126ba97SEli Cohen 
1654e126ba97SEli Cohen 	cur = wq->head - wq->tail;
1655e126ba97SEli Cohen 	if (likely(cur + nreq < wq->max_post))
1656e126ba97SEli Cohen 		return 0;
1657e126ba97SEli Cohen 
1658e126ba97SEli Cohen 	cq = to_mcq(ib_cq);
1659e126ba97SEli Cohen 	spin_lock(&cq->lock);
1660e126ba97SEli Cohen 	cur = wq->head - wq->tail;
1661e126ba97SEli Cohen 	spin_unlock(&cq->lock);
1662e126ba97SEli Cohen 
1663e126ba97SEli Cohen 	return cur + nreq >= wq->max_post;
1664e126ba97SEli Cohen }
1665e126ba97SEli Cohen 
1666e126ba97SEli Cohen static __always_inline void set_raddr_seg(struct mlx5_wqe_raddr_seg *rseg,
1667e126ba97SEli Cohen 					  u64 remote_addr, u32 rkey)
1668e126ba97SEli Cohen {
1669e126ba97SEli Cohen 	rseg->raddr    = cpu_to_be64(remote_addr);
1670e126ba97SEli Cohen 	rseg->rkey     = cpu_to_be32(rkey);
1671e126ba97SEli Cohen 	rseg->reserved = 0;
1672e126ba97SEli Cohen }
1673e126ba97SEli Cohen 
1674e126ba97SEli Cohen static void set_datagram_seg(struct mlx5_wqe_datagram_seg *dseg,
1675e126ba97SEli Cohen 			     struct ib_send_wr *wr)
1676e126ba97SEli Cohen {
1677e126ba97SEli Cohen 	memcpy(&dseg->av, &to_mah(wr->wr.ud.ah)->av, sizeof(struct mlx5_av));
1678e126ba97SEli Cohen 	dseg->av.dqp_dct = cpu_to_be32(wr->wr.ud.remote_qpn | MLX5_EXTENDED_UD_AV);
1679e126ba97SEli Cohen 	dseg->av.key.qkey.qkey = cpu_to_be32(wr->wr.ud.remote_qkey);
1680e126ba97SEli Cohen }
1681e126ba97SEli Cohen 
1682e126ba97SEli Cohen static void set_data_ptr_seg(struct mlx5_wqe_data_seg *dseg, struct ib_sge *sg)
1683e126ba97SEli Cohen {
1684e126ba97SEli Cohen 	dseg->byte_count = cpu_to_be32(sg->length);
1685e126ba97SEli Cohen 	dseg->lkey       = cpu_to_be32(sg->lkey);
1686e126ba97SEli Cohen 	dseg->addr       = cpu_to_be64(sg->addr);
1687e126ba97SEli Cohen }
1688e126ba97SEli Cohen 
1689e126ba97SEli Cohen static __be16 get_klm_octo(int npages)
1690e126ba97SEli Cohen {
1691e126ba97SEli Cohen 	return cpu_to_be16(ALIGN(npages, 8) / 2);
1692e126ba97SEli Cohen }
1693e126ba97SEli Cohen 
1694e126ba97SEli Cohen static __be64 frwr_mkey_mask(void)
1695e126ba97SEli Cohen {
1696e126ba97SEli Cohen 	u64 result;
1697e126ba97SEli Cohen 
1698e126ba97SEli Cohen 	result = MLX5_MKEY_MASK_LEN		|
1699e126ba97SEli Cohen 		MLX5_MKEY_MASK_PAGE_SIZE	|
1700e126ba97SEli Cohen 		MLX5_MKEY_MASK_START_ADDR	|
1701e126ba97SEli Cohen 		MLX5_MKEY_MASK_EN_RINVAL	|
1702e126ba97SEli Cohen 		MLX5_MKEY_MASK_KEY		|
1703e126ba97SEli Cohen 		MLX5_MKEY_MASK_LR		|
1704e126ba97SEli Cohen 		MLX5_MKEY_MASK_LW		|
1705e126ba97SEli Cohen 		MLX5_MKEY_MASK_RR		|
1706e126ba97SEli Cohen 		MLX5_MKEY_MASK_RW		|
1707e126ba97SEli Cohen 		MLX5_MKEY_MASK_A		|
1708e126ba97SEli Cohen 		MLX5_MKEY_MASK_SMALL_FENCE	|
1709e126ba97SEli Cohen 		MLX5_MKEY_MASK_FREE;
1710e126ba97SEli Cohen 
1711e126ba97SEli Cohen 	return cpu_to_be64(result);
1712e126ba97SEli Cohen }
1713e126ba97SEli Cohen 
1714e126ba97SEli Cohen static void set_frwr_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
1715e126ba97SEli Cohen 				 struct ib_send_wr *wr, int li)
1716e126ba97SEli Cohen {
1717e126ba97SEli Cohen 	memset(umr, 0, sizeof(*umr));
1718e126ba97SEli Cohen 
1719e126ba97SEli Cohen 	if (li) {
1720e126ba97SEli Cohen 		umr->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
1721e126ba97SEli Cohen 		umr->flags = 1 << 7;
1722e126ba97SEli Cohen 		return;
1723e126ba97SEli Cohen 	}
1724e126ba97SEli Cohen 
1725e126ba97SEli Cohen 	umr->flags = (1 << 5); /* fail if not free */
1726e126ba97SEli Cohen 	umr->klm_octowords = get_klm_octo(wr->wr.fast_reg.page_list_len);
1727e126ba97SEli Cohen 	umr->mkey_mask = frwr_mkey_mask();
1728e126ba97SEli Cohen }
1729e126ba97SEli Cohen 
1730e126ba97SEli Cohen static void set_reg_umr_segment(struct mlx5_wqe_umr_ctrl_seg *umr,
1731e126ba97SEli Cohen 				struct ib_send_wr *wr)
1732e126ba97SEli Cohen {
1733e126ba97SEli Cohen 	struct umr_wr *umrwr = (struct umr_wr *)&wr->wr.fast_reg;
1734e126ba97SEli Cohen 	u64 mask;
1735e126ba97SEli Cohen 
1736e126ba97SEli Cohen 	memset(umr, 0, sizeof(*umr));
1737e126ba97SEli Cohen 
1738e126ba97SEli Cohen 	if (!(wr->send_flags & MLX5_IB_SEND_UMR_UNREG)) {
1739e126ba97SEli Cohen 		umr->flags = 1 << 5; /* fail if not free */
1740e126ba97SEli Cohen 		umr->klm_octowords = get_klm_octo(umrwr->npages);
1741e126ba97SEli Cohen 		mask =  MLX5_MKEY_MASK_LEN		|
1742e126ba97SEli Cohen 			MLX5_MKEY_MASK_PAGE_SIZE	|
1743e126ba97SEli Cohen 			MLX5_MKEY_MASK_START_ADDR	|
1744e126ba97SEli Cohen 			MLX5_MKEY_MASK_PD		|
1745e126ba97SEli Cohen 			MLX5_MKEY_MASK_LR		|
1746e126ba97SEli Cohen 			MLX5_MKEY_MASK_LW		|
1747746b5583SEli Cohen 			MLX5_MKEY_MASK_KEY		|
1748e126ba97SEli Cohen 			MLX5_MKEY_MASK_RR		|
1749e126ba97SEli Cohen 			MLX5_MKEY_MASK_RW		|
1750e126ba97SEli Cohen 			MLX5_MKEY_MASK_A		|
1751e126ba97SEli Cohen 			MLX5_MKEY_MASK_FREE;
1752e126ba97SEli Cohen 		umr->mkey_mask = cpu_to_be64(mask);
1753e126ba97SEli Cohen 	} else {
1754e126ba97SEli Cohen 		umr->flags = 2 << 5; /* fail if free */
1755e126ba97SEli Cohen 		mask = MLX5_MKEY_MASK_FREE;
1756e126ba97SEli Cohen 		umr->mkey_mask = cpu_to_be64(mask);
1757e126ba97SEli Cohen 	}
1758e126ba97SEli Cohen 
1759e126ba97SEli Cohen 	if (!wr->num_sge)
1760e126ba97SEli Cohen 		umr->flags |= (1 << 7); /* inline */
1761e126ba97SEli Cohen }
1762e126ba97SEli Cohen 
1763e126ba97SEli Cohen static u8 get_umr_flags(int acc)
1764e126ba97SEli Cohen {
1765e126ba97SEli Cohen 	return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX5_PERM_ATOMIC       : 0) |
1766e126ba97SEli Cohen 	       (acc & IB_ACCESS_REMOTE_WRITE  ? MLX5_PERM_REMOTE_WRITE : 0) |
1767e126ba97SEli Cohen 	       (acc & IB_ACCESS_REMOTE_READ   ? MLX5_PERM_REMOTE_READ  : 0) |
1768e126ba97SEli Cohen 	       (acc & IB_ACCESS_LOCAL_WRITE   ? MLX5_PERM_LOCAL_WRITE  : 0) |
1769e126ba97SEli Cohen 		MLX5_PERM_LOCAL_READ | MLX5_PERM_UMR_EN | MLX5_ACCESS_MODE_MTT;
1770e126ba97SEli Cohen }
1771e126ba97SEli Cohen 
1772e126ba97SEli Cohen static void set_mkey_segment(struct mlx5_mkey_seg *seg, struct ib_send_wr *wr,
1773e126ba97SEli Cohen 			     int li, int *writ)
1774e126ba97SEli Cohen {
1775e126ba97SEli Cohen 	memset(seg, 0, sizeof(*seg));
1776e126ba97SEli Cohen 	if (li) {
1777e126ba97SEli Cohen 		seg->status = 1 << 6;
1778e126ba97SEli Cohen 		return;
1779e126ba97SEli Cohen 	}
1780e126ba97SEli Cohen 
1781e126ba97SEli Cohen 	seg->flags = get_umr_flags(wr->wr.fast_reg.access_flags);
1782e126ba97SEli Cohen 	*writ = seg->flags & (MLX5_PERM_LOCAL_WRITE | IB_ACCESS_REMOTE_WRITE);
1783e126ba97SEli Cohen 	seg->qpn_mkey7_0 = cpu_to_be32((wr->wr.fast_reg.rkey & 0xff) | 0xffffff00);
1784e126ba97SEli Cohen 	seg->flags_pd = cpu_to_be32(MLX5_MKEY_REMOTE_INVAL);
1785e126ba97SEli Cohen 	seg->start_addr = cpu_to_be64(wr->wr.fast_reg.iova_start);
1786e126ba97SEli Cohen 	seg->len = cpu_to_be64(wr->wr.fast_reg.length);
1787e126ba97SEli Cohen 	seg->xlt_oct_size = cpu_to_be32((wr->wr.fast_reg.page_list_len + 1) / 2);
1788e126ba97SEli Cohen 	seg->log2_page_size = wr->wr.fast_reg.page_shift;
1789e126ba97SEli Cohen }
1790e126ba97SEli Cohen 
1791e126ba97SEli Cohen static void set_reg_mkey_segment(struct mlx5_mkey_seg *seg, struct ib_send_wr *wr)
1792e126ba97SEli Cohen {
1793e126ba97SEli Cohen 	memset(seg, 0, sizeof(*seg));
1794e126ba97SEli Cohen 	if (wr->send_flags & MLX5_IB_SEND_UMR_UNREG) {
1795e126ba97SEli Cohen 		seg->status = 1 << 6;
1796e126ba97SEli Cohen 		return;
1797e126ba97SEli Cohen 	}
1798e126ba97SEli Cohen 
1799e126ba97SEli Cohen 	seg->flags = convert_access(wr->wr.fast_reg.access_flags);
1800e126ba97SEli Cohen 	seg->flags_pd = cpu_to_be32(to_mpd((struct ib_pd *)wr->wr.fast_reg.page_list)->pdn);
1801e126ba97SEli Cohen 	seg->start_addr = cpu_to_be64(wr->wr.fast_reg.iova_start);
1802e126ba97SEli Cohen 	seg->len = cpu_to_be64(wr->wr.fast_reg.length);
1803e126ba97SEli Cohen 	seg->log2_page_size = wr->wr.fast_reg.page_shift;
1804746b5583SEli Cohen 	seg->qpn_mkey7_0 = cpu_to_be32(0xffffff00 |
1805746b5583SEli Cohen 				       mlx5_mkey_variant(wr->wr.fast_reg.rkey));
1806e126ba97SEli Cohen }
1807e126ba97SEli Cohen 
1808e126ba97SEli Cohen static void set_frwr_pages(struct mlx5_wqe_data_seg *dseg,
1809e126ba97SEli Cohen 			   struct ib_send_wr *wr,
1810e126ba97SEli Cohen 			   struct mlx5_core_dev *mdev,
1811e126ba97SEli Cohen 			   struct mlx5_ib_pd *pd,
1812e126ba97SEli Cohen 			   int writ)
1813e126ba97SEli Cohen {
1814e126ba97SEli Cohen 	struct mlx5_ib_fast_reg_page_list *mfrpl = to_mfrpl(wr->wr.fast_reg.page_list);
1815e126ba97SEli Cohen 	u64 *page_list = wr->wr.fast_reg.page_list->page_list;
1816e126ba97SEli Cohen 	u64 perm = MLX5_EN_RD | (writ ? MLX5_EN_WR : 0);
1817e126ba97SEli Cohen 	int i;
1818e126ba97SEli Cohen 
1819e126ba97SEli Cohen 	for (i = 0; i < wr->wr.fast_reg.page_list_len; i++)
1820e126ba97SEli Cohen 		mfrpl->mapped_page_list[i] = cpu_to_be64(page_list[i] | perm);
1821e126ba97SEli Cohen 	dseg->addr = cpu_to_be64(mfrpl->map);
1822e126ba97SEli Cohen 	dseg->byte_count = cpu_to_be32(ALIGN(sizeof(u64) * wr->wr.fast_reg.page_list_len, 64));
1823e126ba97SEli Cohen 	dseg->lkey = cpu_to_be32(pd->pa_lkey);
1824e126ba97SEli Cohen }
1825e126ba97SEli Cohen 
1826e126ba97SEli Cohen static __be32 send_ieth(struct ib_send_wr *wr)
1827e126ba97SEli Cohen {
1828e126ba97SEli Cohen 	switch (wr->opcode) {
1829e126ba97SEli Cohen 	case IB_WR_SEND_WITH_IMM:
1830e126ba97SEli Cohen 	case IB_WR_RDMA_WRITE_WITH_IMM:
1831e126ba97SEli Cohen 		return wr->ex.imm_data;
1832e126ba97SEli Cohen 
1833e126ba97SEli Cohen 	case IB_WR_SEND_WITH_INV:
1834e126ba97SEli Cohen 		return cpu_to_be32(wr->ex.invalidate_rkey);
1835e126ba97SEli Cohen 
1836e126ba97SEli Cohen 	default:
1837e126ba97SEli Cohen 		return 0;
1838e126ba97SEli Cohen 	}
1839e126ba97SEli Cohen }
1840e126ba97SEli Cohen 
1841e126ba97SEli Cohen static u8 calc_sig(void *wqe, int size)
1842e126ba97SEli Cohen {
1843e126ba97SEli Cohen 	u8 *p = wqe;
1844e126ba97SEli Cohen 	u8 res = 0;
1845e126ba97SEli Cohen 	int i;
1846e126ba97SEli Cohen 
1847e126ba97SEli Cohen 	for (i = 0; i < size; i++)
1848e126ba97SEli Cohen 		res ^= p[i];
1849e126ba97SEli Cohen 
1850e126ba97SEli Cohen 	return ~res;
1851e126ba97SEli Cohen }
1852e126ba97SEli Cohen 
1853e126ba97SEli Cohen static u8 wq_sig(void *wqe)
1854e126ba97SEli Cohen {
1855e126ba97SEli Cohen 	return calc_sig(wqe, (*((u8 *)wqe + 8) & 0x3f) << 4);
1856e126ba97SEli Cohen }
1857e126ba97SEli Cohen 
1858e126ba97SEli Cohen static int set_data_inl_seg(struct mlx5_ib_qp *qp, struct ib_send_wr *wr,
1859e126ba97SEli Cohen 			    void *wqe, int *sz)
1860e126ba97SEli Cohen {
1861e126ba97SEli Cohen 	struct mlx5_wqe_inline_seg *seg;
1862e126ba97SEli Cohen 	void *qend = qp->sq.qend;
1863e126ba97SEli Cohen 	void *addr;
1864e126ba97SEli Cohen 	int inl = 0;
1865e126ba97SEli Cohen 	int copy;
1866e126ba97SEli Cohen 	int len;
1867e126ba97SEli Cohen 	int i;
1868e126ba97SEli Cohen 
1869e126ba97SEli Cohen 	seg = wqe;
1870e126ba97SEli Cohen 	wqe += sizeof(*seg);
1871e126ba97SEli Cohen 	for (i = 0; i < wr->num_sge; i++) {
1872e126ba97SEli Cohen 		addr = (void *)(unsigned long)(wr->sg_list[i].addr);
1873e126ba97SEli Cohen 		len  = wr->sg_list[i].length;
1874e126ba97SEli Cohen 		inl += len;
1875e126ba97SEli Cohen 
1876e126ba97SEli Cohen 		if (unlikely(inl > qp->max_inline_data))
1877e126ba97SEli Cohen 			return -ENOMEM;
1878e126ba97SEli Cohen 
1879e126ba97SEli Cohen 		if (unlikely(wqe + len > qend)) {
1880e126ba97SEli Cohen 			copy = qend - wqe;
1881e126ba97SEli Cohen 			memcpy(wqe, addr, copy);
1882e126ba97SEli Cohen 			addr += copy;
1883e126ba97SEli Cohen 			len -= copy;
1884e126ba97SEli Cohen 			wqe = mlx5_get_send_wqe(qp, 0);
1885e126ba97SEli Cohen 		}
1886e126ba97SEli Cohen 		memcpy(wqe, addr, len);
1887e126ba97SEli Cohen 		wqe += len;
1888e126ba97SEli Cohen 	}
1889e126ba97SEli Cohen 
1890e126ba97SEli Cohen 	seg->byte_count = cpu_to_be32(inl | MLX5_INLINE_SEG);
1891e126ba97SEli Cohen 
1892e126ba97SEli Cohen 	*sz = ALIGN(inl + sizeof(seg->byte_count), 16) / 16;
1893e126ba97SEli Cohen 
1894e126ba97SEli Cohen 	return 0;
1895e126ba97SEli Cohen }
1896e126ba97SEli Cohen 
1897e126ba97SEli Cohen static int set_frwr_li_wr(void **seg, struct ib_send_wr *wr, int *size,
1898e126ba97SEli Cohen 			  struct mlx5_core_dev *mdev, struct mlx5_ib_pd *pd, struct mlx5_ib_qp *qp)
1899e126ba97SEli Cohen {
1900e126ba97SEli Cohen 	int writ = 0;
1901e126ba97SEli Cohen 	int li;
1902e126ba97SEli Cohen 
1903e126ba97SEli Cohen 	li = wr->opcode == IB_WR_LOCAL_INV ? 1 : 0;
1904e126ba97SEli Cohen 	if (unlikely(wr->send_flags & IB_SEND_INLINE))
1905e126ba97SEli Cohen 		return -EINVAL;
1906e126ba97SEli Cohen 
1907e126ba97SEli Cohen 	set_frwr_umr_segment(*seg, wr, li);
1908e126ba97SEli Cohen 	*seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
1909e126ba97SEli Cohen 	*size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
1910e126ba97SEli Cohen 	if (unlikely((*seg == qp->sq.qend)))
1911e126ba97SEli Cohen 		*seg = mlx5_get_send_wqe(qp, 0);
1912e126ba97SEli Cohen 	set_mkey_segment(*seg, wr, li, &writ);
1913e126ba97SEli Cohen 	*seg += sizeof(struct mlx5_mkey_seg);
1914e126ba97SEli Cohen 	*size += sizeof(struct mlx5_mkey_seg) / 16;
1915e126ba97SEli Cohen 	if (unlikely((*seg == qp->sq.qend)))
1916e126ba97SEli Cohen 		*seg = mlx5_get_send_wqe(qp, 0);
1917e126ba97SEli Cohen 	if (!li) {
19189641b74eSEli Cohen 		if (unlikely(wr->wr.fast_reg.page_list_len >
19199641b74eSEli Cohen 			     wr->wr.fast_reg.page_list->max_page_list_len))
19209641b74eSEli Cohen 			return	-ENOMEM;
19219641b74eSEli Cohen 
1922e126ba97SEli Cohen 		set_frwr_pages(*seg, wr, mdev, pd, writ);
1923e126ba97SEli Cohen 		*seg += sizeof(struct mlx5_wqe_data_seg);
1924e126ba97SEli Cohen 		*size += (sizeof(struct mlx5_wqe_data_seg) / 16);
1925e126ba97SEli Cohen 	}
1926e126ba97SEli Cohen 	return 0;
1927e126ba97SEli Cohen }
1928e126ba97SEli Cohen 
1929e126ba97SEli Cohen static void dump_wqe(struct mlx5_ib_qp *qp, int idx, int size_16)
1930e126ba97SEli Cohen {
1931e126ba97SEli Cohen 	__be32 *p = NULL;
1932e126ba97SEli Cohen 	int tidx = idx;
1933e126ba97SEli Cohen 	int i, j;
1934e126ba97SEli Cohen 
1935e126ba97SEli Cohen 	pr_debug("dump wqe at %p\n", mlx5_get_send_wqe(qp, tidx));
1936e126ba97SEli Cohen 	for (i = 0, j = 0; i < size_16 * 4; i += 4, j += 4) {
1937e126ba97SEli Cohen 		if ((i & 0xf) == 0) {
1938e126ba97SEli Cohen 			void *buf = mlx5_get_send_wqe(qp, tidx);
1939e126ba97SEli Cohen 			tidx = (tidx + 1) & (qp->sq.wqe_cnt - 1);
1940e126ba97SEli Cohen 			p = buf;
1941e126ba97SEli Cohen 			j = 0;
1942e126ba97SEli Cohen 		}
1943e126ba97SEli Cohen 		pr_debug("%08x %08x %08x %08x\n", be32_to_cpu(p[j]),
1944e126ba97SEli Cohen 			 be32_to_cpu(p[j + 1]), be32_to_cpu(p[j + 2]),
1945e126ba97SEli Cohen 			 be32_to_cpu(p[j + 3]));
1946e126ba97SEli Cohen 	}
1947e126ba97SEli Cohen }
1948e126ba97SEli Cohen 
1949e126ba97SEli Cohen static void mlx5_bf_copy(u64 __iomem *dst, u64 *src,
1950e126ba97SEli Cohen 			 unsigned bytecnt, struct mlx5_ib_qp *qp)
1951e126ba97SEli Cohen {
1952e126ba97SEli Cohen 	while (bytecnt > 0) {
1953e126ba97SEli Cohen 		__iowrite64_copy(dst++, src++, 8);
1954e126ba97SEli Cohen 		__iowrite64_copy(dst++, src++, 8);
1955e126ba97SEli Cohen 		__iowrite64_copy(dst++, src++, 8);
1956e126ba97SEli Cohen 		__iowrite64_copy(dst++, src++, 8);
1957e126ba97SEli Cohen 		__iowrite64_copy(dst++, src++, 8);
1958e126ba97SEli Cohen 		__iowrite64_copy(dst++, src++, 8);
1959e126ba97SEli Cohen 		__iowrite64_copy(dst++, src++, 8);
1960e126ba97SEli Cohen 		__iowrite64_copy(dst++, src++, 8);
1961e126ba97SEli Cohen 		bytecnt -= 64;
1962e126ba97SEli Cohen 		if (unlikely(src == qp->sq.qend))
1963e126ba97SEli Cohen 			src = mlx5_get_send_wqe(qp, 0);
1964e126ba97SEli Cohen 	}
1965e126ba97SEli Cohen }
1966e126ba97SEli Cohen 
1967e126ba97SEli Cohen static u8 get_fence(u8 fence, struct ib_send_wr *wr)
1968e126ba97SEli Cohen {
1969e126ba97SEli Cohen 	if (unlikely(wr->opcode == IB_WR_LOCAL_INV &&
1970e126ba97SEli Cohen 		     wr->send_flags & IB_SEND_FENCE))
1971e126ba97SEli Cohen 		return MLX5_FENCE_MODE_STRONG_ORDERING;
1972e126ba97SEli Cohen 
1973e126ba97SEli Cohen 	if (unlikely(fence)) {
1974e126ba97SEli Cohen 		if (wr->send_flags & IB_SEND_FENCE)
1975e126ba97SEli Cohen 			return MLX5_FENCE_MODE_SMALL_AND_FENCE;
1976e126ba97SEli Cohen 		else
1977e126ba97SEli Cohen 			return fence;
1978e126ba97SEli Cohen 
1979e126ba97SEli Cohen 	} else {
1980e126ba97SEli Cohen 		return 0;
1981e126ba97SEli Cohen 	}
1982e126ba97SEli Cohen }
1983e126ba97SEli Cohen 
1984e126ba97SEli Cohen int mlx5_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
1985e126ba97SEli Cohen 		      struct ib_send_wr **bad_wr)
1986e126ba97SEli Cohen {
1987e126ba97SEli Cohen 	struct mlx5_wqe_ctrl_seg *ctrl = NULL;  /* compiler warning */
1988e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
1989e126ba97SEli Cohen 	struct mlx5_core_dev *mdev = &dev->mdev;
1990e126ba97SEli Cohen 	struct mlx5_ib_qp *qp = to_mqp(ibqp);
1991e126ba97SEli Cohen 	struct mlx5_wqe_data_seg *dpseg;
1992e126ba97SEli Cohen 	struct mlx5_wqe_xrc_seg *xrc;
1993e126ba97SEli Cohen 	struct mlx5_bf *bf = qp->bf;
1994e126ba97SEli Cohen 	int uninitialized_var(size);
1995e126ba97SEli Cohen 	void *qend = qp->sq.qend;
1996e126ba97SEli Cohen 	unsigned long flags;
1997e126ba97SEli Cohen 	u32 mlx5_opcode;
1998e126ba97SEli Cohen 	unsigned idx;
1999e126ba97SEli Cohen 	int err = 0;
2000e126ba97SEli Cohen 	int inl = 0;
2001e126ba97SEli Cohen 	int num_sge;
2002e126ba97SEli Cohen 	void *seg;
2003e126ba97SEli Cohen 	int nreq;
2004e126ba97SEli Cohen 	int i;
2005e126ba97SEli Cohen 	u8 next_fence = 0;
2006e126ba97SEli Cohen 	u8 opmod = 0;
2007e126ba97SEli Cohen 	u8 fence;
2008e126ba97SEli Cohen 
2009e126ba97SEli Cohen 	spin_lock_irqsave(&qp->sq.lock, flags);
2010e126ba97SEli Cohen 
2011e126ba97SEli Cohen 	for (nreq = 0; wr; nreq++, wr = wr->next) {
2012e126ba97SEli Cohen 		if (unlikely(wr->opcode >= sizeof(mlx5_ib_opcode) / sizeof(mlx5_ib_opcode[0]))) {
2013e126ba97SEli Cohen 			mlx5_ib_warn(dev, "\n");
2014e126ba97SEli Cohen 			err = -EINVAL;
2015e126ba97SEli Cohen 			*bad_wr = wr;
2016e126ba97SEli Cohen 			goto out;
2017e126ba97SEli Cohen 		}
2018e126ba97SEli Cohen 
2019e126ba97SEli Cohen 		if (unlikely(mlx5_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq))) {
2020e126ba97SEli Cohen 			mlx5_ib_warn(dev, "\n");
2021e126ba97SEli Cohen 			err = -ENOMEM;
2022e126ba97SEli Cohen 			*bad_wr = wr;
2023e126ba97SEli Cohen 			goto out;
2024e126ba97SEli Cohen 		}
2025e126ba97SEli Cohen 
2026e126ba97SEli Cohen 		fence = qp->fm_cache;
2027e126ba97SEli Cohen 		num_sge = wr->num_sge;
2028e126ba97SEli Cohen 		if (unlikely(num_sge > qp->sq.max_gs)) {
2029e126ba97SEli Cohen 			mlx5_ib_warn(dev, "\n");
2030e126ba97SEli Cohen 			err = -ENOMEM;
2031e126ba97SEli Cohen 			*bad_wr = wr;
2032e126ba97SEli Cohen 			goto out;
2033e126ba97SEli Cohen 		}
2034e126ba97SEli Cohen 
2035e126ba97SEli Cohen 		idx = qp->sq.cur_post & (qp->sq.wqe_cnt - 1);
2036e126ba97SEli Cohen 		seg = mlx5_get_send_wqe(qp, idx);
2037e126ba97SEli Cohen 		ctrl = seg;
2038e126ba97SEli Cohen 		*(uint32_t *)(seg + 8) = 0;
2039e126ba97SEli Cohen 		ctrl->imm = send_ieth(wr);
2040e126ba97SEli Cohen 		ctrl->fm_ce_se = qp->sq_signal_bits |
2041e126ba97SEli Cohen 			(wr->send_flags & IB_SEND_SIGNALED ?
2042e126ba97SEli Cohen 			 MLX5_WQE_CTRL_CQ_UPDATE : 0) |
2043e126ba97SEli Cohen 			(wr->send_flags & IB_SEND_SOLICITED ?
2044e126ba97SEli Cohen 			 MLX5_WQE_CTRL_SOLICITED : 0);
2045e126ba97SEli Cohen 
2046e126ba97SEli Cohen 		seg += sizeof(*ctrl);
2047e126ba97SEli Cohen 		size = sizeof(*ctrl) / 16;
2048e126ba97SEli Cohen 
2049e126ba97SEli Cohen 		switch (ibqp->qp_type) {
2050e126ba97SEli Cohen 		case IB_QPT_XRC_INI:
2051e126ba97SEli Cohen 			xrc = seg;
2052e126ba97SEli Cohen 			xrc->xrc_srqn = htonl(wr->xrc_remote_srq_num);
2053e126ba97SEli Cohen 			seg += sizeof(*xrc);
2054e126ba97SEli Cohen 			size += sizeof(*xrc) / 16;
2055e126ba97SEli Cohen 			/* fall through */
2056e126ba97SEli Cohen 		case IB_QPT_RC:
2057e126ba97SEli Cohen 			switch (wr->opcode) {
2058e126ba97SEli Cohen 			case IB_WR_RDMA_READ:
2059e126ba97SEli Cohen 			case IB_WR_RDMA_WRITE:
2060e126ba97SEli Cohen 			case IB_WR_RDMA_WRITE_WITH_IMM:
2061e126ba97SEli Cohen 				set_raddr_seg(seg, wr->wr.rdma.remote_addr,
2062e126ba97SEli Cohen 					      wr->wr.rdma.rkey);
2063e126ba97SEli Cohen 				seg  += sizeof(struct mlx5_wqe_raddr_seg);
2064e126ba97SEli Cohen 				size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
2065e126ba97SEli Cohen 				break;
2066e126ba97SEli Cohen 
2067e126ba97SEli Cohen 			case IB_WR_ATOMIC_CMP_AND_SWP:
2068e126ba97SEli Cohen 			case IB_WR_ATOMIC_FETCH_AND_ADD:
2069e126ba97SEli Cohen 			case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
207081bea28fSEli Cohen 				mlx5_ib_warn(dev, "Atomic operations are not supported yet\n");
207181bea28fSEli Cohen 				err = -ENOSYS;
207281bea28fSEli Cohen 				*bad_wr = wr;
207381bea28fSEli Cohen 				goto out;
2074e126ba97SEli Cohen 
2075e126ba97SEli Cohen 			case IB_WR_LOCAL_INV:
2076e126ba97SEli Cohen 				next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
2077e126ba97SEli Cohen 				qp->sq.wr_data[idx] = IB_WR_LOCAL_INV;
2078e126ba97SEli Cohen 				ctrl->imm = cpu_to_be32(wr->ex.invalidate_rkey);
2079e126ba97SEli Cohen 				err = set_frwr_li_wr(&seg, wr, &size, mdev, to_mpd(ibqp->pd), qp);
2080e126ba97SEli Cohen 				if (err) {
2081e126ba97SEli Cohen 					mlx5_ib_warn(dev, "\n");
2082e126ba97SEli Cohen 					*bad_wr = wr;
2083e126ba97SEli Cohen 					goto out;
2084e126ba97SEli Cohen 				}
2085e126ba97SEli Cohen 				num_sge = 0;
2086e126ba97SEli Cohen 				break;
2087e126ba97SEli Cohen 
2088e126ba97SEli Cohen 			case IB_WR_FAST_REG_MR:
2089e126ba97SEli Cohen 				next_fence = MLX5_FENCE_MODE_INITIATOR_SMALL;
2090e126ba97SEli Cohen 				qp->sq.wr_data[idx] = IB_WR_FAST_REG_MR;
2091e126ba97SEli Cohen 				ctrl->imm = cpu_to_be32(wr->wr.fast_reg.rkey);
2092e126ba97SEli Cohen 				err = set_frwr_li_wr(&seg, wr, &size, mdev, to_mpd(ibqp->pd), qp);
2093e126ba97SEli Cohen 				if (err) {
2094e126ba97SEli Cohen 					mlx5_ib_warn(dev, "\n");
2095e126ba97SEli Cohen 					*bad_wr = wr;
2096e126ba97SEli Cohen 					goto out;
2097e126ba97SEli Cohen 				}
2098e126ba97SEli Cohen 				num_sge = 0;
2099e126ba97SEli Cohen 				break;
2100e126ba97SEli Cohen 
2101e126ba97SEli Cohen 			default:
2102e126ba97SEli Cohen 				break;
2103e126ba97SEli Cohen 			}
2104e126ba97SEli Cohen 			break;
2105e126ba97SEli Cohen 
2106e126ba97SEli Cohen 		case IB_QPT_UC:
2107e126ba97SEli Cohen 			switch (wr->opcode) {
2108e126ba97SEli Cohen 			case IB_WR_RDMA_WRITE:
2109e126ba97SEli Cohen 			case IB_WR_RDMA_WRITE_WITH_IMM:
2110e126ba97SEli Cohen 				set_raddr_seg(seg, wr->wr.rdma.remote_addr,
2111e126ba97SEli Cohen 					      wr->wr.rdma.rkey);
2112e126ba97SEli Cohen 				seg  += sizeof(struct mlx5_wqe_raddr_seg);
2113e126ba97SEli Cohen 				size += sizeof(struct mlx5_wqe_raddr_seg) / 16;
2114e126ba97SEli Cohen 				break;
2115e126ba97SEli Cohen 
2116e126ba97SEli Cohen 			default:
2117e126ba97SEli Cohen 				break;
2118e126ba97SEli Cohen 			}
2119e126ba97SEli Cohen 			break;
2120e126ba97SEli Cohen 
2121e126ba97SEli Cohen 		case IB_QPT_UD:
2122e126ba97SEli Cohen 		case IB_QPT_SMI:
2123e126ba97SEli Cohen 		case IB_QPT_GSI:
2124e126ba97SEli Cohen 			set_datagram_seg(seg, wr);
2125e126ba97SEli Cohen 			seg  += sizeof(struct mlx5_wqe_datagram_seg);
2126e126ba97SEli Cohen 			size += sizeof(struct mlx5_wqe_datagram_seg) / 16;
2127e126ba97SEli Cohen 			if (unlikely((seg == qend)))
2128e126ba97SEli Cohen 				seg = mlx5_get_send_wqe(qp, 0);
2129e126ba97SEli Cohen 			break;
2130e126ba97SEli Cohen 
2131e126ba97SEli Cohen 		case MLX5_IB_QPT_REG_UMR:
2132e126ba97SEli Cohen 			if (wr->opcode != MLX5_IB_WR_UMR) {
2133e126ba97SEli Cohen 				err = -EINVAL;
2134e126ba97SEli Cohen 				mlx5_ib_warn(dev, "bad opcode\n");
2135e126ba97SEli Cohen 				goto out;
2136e126ba97SEli Cohen 			}
2137e126ba97SEli Cohen 			qp->sq.wr_data[idx] = MLX5_IB_WR_UMR;
2138e126ba97SEli Cohen 			ctrl->imm = cpu_to_be32(wr->wr.fast_reg.rkey);
2139e126ba97SEli Cohen 			set_reg_umr_segment(seg, wr);
2140e126ba97SEli Cohen 			seg += sizeof(struct mlx5_wqe_umr_ctrl_seg);
2141e126ba97SEli Cohen 			size += sizeof(struct mlx5_wqe_umr_ctrl_seg) / 16;
2142e126ba97SEli Cohen 			if (unlikely((seg == qend)))
2143e126ba97SEli Cohen 				seg = mlx5_get_send_wqe(qp, 0);
2144e126ba97SEli Cohen 			set_reg_mkey_segment(seg, wr);
2145e126ba97SEli Cohen 			seg += sizeof(struct mlx5_mkey_seg);
2146e126ba97SEli Cohen 			size += sizeof(struct mlx5_mkey_seg) / 16;
2147e126ba97SEli Cohen 			if (unlikely((seg == qend)))
2148e126ba97SEli Cohen 				seg = mlx5_get_send_wqe(qp, 0);
2149e126ba97SEli Cohen 			break;
2150e126ba97SEli Cohen 
2151e126ba97SEli Cohen 		default:
2152e126ba97SEli Cohen 			break;
2153e126ba97SEli Cohen 		}
2154e126ba97SEli Cohen 
2155e126ba97SEli Cohen 		if (wr->send_flags & IB_SEND_INLINE && num_sge) {
2156e126ba97SEli Cohen 			int uninitialized_var(sz);
2157e126ba97SEli Cohen 
2158e126ba97SEli Cohen 			err = set_data_inl_seg(qp, wr, seg, &sz);
2159e126ba97SEli Cohen 			if (unlikely(err)) {
2160e126ba97SEli Cohen 				mlx5_ib_warn(dev, "\n");
2161e126ba97SEli Cohen 				*bad_wr = wr;
2162e126ba97SEli Cohen 				goto out;
2163e126ba97SEli Cohen 			}
2164e126ba97SEli Cohen 			inl = 1;
2165e126ba97SEli Cohen 			size += sz;
2166e126ba97SEli Cohen 		} else {
2167e126ba97SEli Cohen 			dpseg = seg;
2168e126ba97SEli Cohen 			for (i = 0; i < num_sge; i++) {
2169e126ba97SEli Cohen 				if (unlikely(dpseg == qend)) {
2170e126ba97SEli Cohen 					seg = mlx5_get_send_wqe(qp, 0);
2171e126ba97SEli Cohen 					dpseg = seg;
2172e126ba97SEli Cohen 				}
2173e126ba97SEli Cohen 				if (likely(wr->sg_list[i].length)) {
2174e126ba97SEli Cohen 					set_data_ptr_seg(dpseg, wr->sg_list + i);
2175e126ba97SEli Cohen 					size += sizeof(struct mlx5_wqe_data_seg) / 16;
2176e126ba97SEli Cohen 					dpseg++;
2177e126ba97SEli Cohen 				}
2178e126ba97SEli Cohen 			}
2179e126ba97SEli Cohen 		}
2180e126ba97SEli Cohen 
2181e126ba97SEli Cohen 		mlx5_opcode = mlx5_ib_opcode[wr->opcode];
2182e126ba97SEli Cohen 		ctrl->opmod_idx_opcode = cpu_to_be32(((u32)(qp->sq.cur_post) << 8)	|
2183e126ba97SEli Cohen 						     mlx5_opcode			|
2184e126ba97SEli Cohen 						     ((u32)opmod << 24));
2185e126ba97SEli Cohen 		ctrl->qpn_ds = cpu_to_be32(size | (qp->mqp.qpn << 8));
2186e126ba97SEli Cohen 		ctrl->fm_ce_se |= get_fence(fence, wr);
2187e126ba97SEli Cohen 		qp->fm_cache = next_fence;
2188e126ba97SEli Cohen 		if (unlikely(qp->wq_sig))
2189e126ba97SEli Cohen 			ctrl->signature = wq_sig(ctrl);
2190e126ba97SEli Cohen 
2191e126ba97SEli Cohen 		qp->sq.wrid[idx] = wr->wr_id;
2192e126ba97SEli Cohen 		qp->sq.w_list[idx].opcode = mlx5_opcode;
2193e126ba97SEli Cohen 		qp->sq.wqe_head[idx] = qp->sq.head + nreq;
2194e126ba97SEli Cohen 		qp->sq.cur_post += DIV_ROUND_UP(size * 16, MLX5_SEND_WQE_BB);
2195e126ba97SEli Cohen 		qp->sq.w_list[idx].next = qp->sq.cur_post;
2196e126ba97SEli Cohen 
2197e126ba97SEli Cohen 		if (0)
2198e126ba97SEli Cohen 			dump_wqe(qp, idx, size);
2199e126ba97SEli Cohen 	}
2200e126ba97SEli Cohen 
2201e126ba97SEli Cohen out:
2202e126ba97SEli Cohen 	if (likely(nreq)) {
2203e126ba97SEli Cohen 		qp->sq.head += nreq;
2204e126ba97SEli Cohen 
2205e126ba97SEli Cohen 		/* Make sure that descriptors are written before
2206e126ba97SEli Cohen 		 * updating doorbell record and ringing the doorbell
2207e126ba97SEli Cohen 		 */
2208e126ba97SEli Cohen 		wmb();
2209e126ba97SEli Cohen 
2210e126ba97SEli Cohen 		qp->db.db[MLX5_SND_DBR] = cpu_to_be32(qp->sq.cur_post);
2211e126ba97SEli Cohen 
2212e126ba97SEli Cohen 		if (bf->need_lock)
2213e126ba97SEli Cohen 			spin_lock(&bf->lock);
2214e126ba97SEli Cohen 
2215e126ba97SEli Cohen 		/* TBD enable WC */
2216e126ba97SEli Cohen 		if (0 && nreq == 1 && bf->uuarn && inl && size > 1 && size <= bf->buf_size / 16) {
2217e126ba97SEli Cohen 			mlx5_bf_copy(bf->reg + bf->offset, (u64 *)ctrl, ALIGN(size * 16, 64), qp);
2218e126ba97SEli Cohen 			/* wc_wmb(); */
2219e126ba97SEli Cohen 		} else {
2220e126ba97SEli Cohen 			mlx5_write64((__be32 *)ctrl, bf->regreg + bf->offset,
2221e126ba97SEli Cohen 				     MLX5_GET_DOORBELL_LOCK(&bf->lock32));
2222e126ba97SEli Cohen 			/* Make sure doorbells don't leak out of SQ spinlock
2223e126ba97SEli Cohen 			 * and reach the HCA out of order.
2224e126ba97SEli Cohen 			 */
2225e126ba97SEli Cohen 			mmiowb();
2226e126ba97SEli Cohen 		}
2227e126ba97SEli Cohen 		bf->offset ^= bf->buf_size;
2228e126ba97SEli Cohen 		if (bf->need_lock)
2229e126ba97SEli Cohen 			spin_unlock(&bf->lock);
2230e126ba97SEli Cohen 	}
2231e126ba97SEli Cohen 
2232e126ba97SEli Cohen 	spin_unlock_irqrestore(&qp->sq.lock, flags);
2233e126ba97SEli Cohen 
2234e126ba97SEli Cohen 	return err;
2235e126ba97SEli Cohen }
2236e126ba97SEli Cohen 
2237e126ba97SEli Cohen static void set_sig_seg(struct mlx5_rwqe_sig *sig, int size)
2238e126ba97SEli Cohen {
2239e126ba97SEli Cohen 	sig->signature = calc_sig(sig, size);
2240e126ba97SEli Cohen }
2241e126ba97SEli Cohen 
2242e126ba97SEli Cohen int mlx5_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
2243e126ba97SEli Cohen 		      struct ib_recv_wr **bad_wr)
2244e126ba97SEli Cohen {
2245e126ba97SEli Cohen 	struct mlx5_ib_qp *qp = to_mqp(ibqp);
2246e126ba97SEli Cohen 	struct mlx5_wqe_data_seg *scat;
2247e126ba97SEli Cohen 	struct mlx5_rwqe_sig *sig;
2248e126ba97SEli Cohen 	unsigned long flags;
2249e126ba97SEli Cohen 	int err = 0;
2250e126ba97SEli Cohen 	int nreq;
2251e126ba97SEli Cohen 	int ind;
2252e126ba97SEli Cohen 	int i;
2253e126ba97SEli Cohen 
2254e126ba97SEli Cohen 	spin_lock_irqsave(&qp->rq.lock, flags);
2255e126ba97SEli Cohen 
2256e126ba97SEli Cohen 	ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
2257e126ba97SEli Cohen 
2258e126ba97SEli Cohen 	for (nreq = 0; wr; nreq++, wr = wr->next) {
2259e126ba97SEli Cohen 		if (mlx5_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
2260e126ba97SEli Cohen 			err = -ENOMEM;
2261e126ba97SEli Cohen 			*bad_wr = wr;
2262e126ba97SEli Cohen 			goto out;
2263e126ba97SEli Cohen 		}
2264e126ba97SEli Cohen 
2265e126ba97SEli Cohen 		if (unlikely(wr->num_sge > qp->rq.max_gs)) {
2266e126ba97SEli Cohen 			err = -EINVAL;
2267e126ba97SEli Cohen 			*bad_wr = wr;
2268e126ba97SEli Cohen 			goto out;
2269e126ba97SEli Cohen 		}
2270e126ba97SEli Cohen 
2271e126ba97SEli Cohen 		scat = get_recv_wqe(qp, ind);
2272e126ba97SEli Cohen 		if (qp->wq_sig)
2273e126ba97SEli Cohen 			scat++;
2274e126ba97SEli Cohen 
2275e126ba97SEli Cohen 		for (i = 0; i < wr->num_sge; i++)
2276e126ba97SEli Cohen 			set_data_ptr_seg(scat + i, wr->sg_list + i);
2277e126ba97SEli Cohen 
2278e126ba97SEli Cohen 		if (i < qp->rq.max_gs) {
2279e126ba97SEli Cohen 			scat[i].byte_count = 0;
2280e126ba97SEli Cohen 			scat[i].lkey       = cpu_to_be32(MLX5_INVALID_LKEY);
2281e126ba97SEli Cohen 			scat[i].addr       = 0;
2282e126ba97SEli Cohen 		}
2283e126ba97SEli Cohen 
2284e126ba97SEli Cohen 		if (qp->wq_sig) {
2285e126ba97SEli Cohen 			sig = (struct mlx5_rwqe_sig *)scat;
2286e126ba97SEli Cohen 			set_sig_seg(sig, (qp->rq.max_gs + 1) << 2);
2287e126ba97SEli Cohen 		}
2288e126ba97SEli Cohen 
2289e126ba97SEli Cohen 		qp->rq.wrid[ind] = wr->wr_id;
2290e126ba97SEli Cohen 
2291e126ba97SEli Cohen 		ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
2292e126ba97SEli Cohen 	}
2293e126ba97SEli Cohen 
2294e126ba97SEli Cohen out:
2295e126ba97SEli Cohen 	if (likely(nreq)) {
2296e126ba97SEli Cohen 		qp->rq.head += nreq;
2297e126ba97SEli Cohen 
2298e126ba97SEli Cohen 		/* Make sure that descriptors are written before
2299e126ba97SEli Cohen 		 * doorbell record.
2300e126ba97SEli Cohen 		 */
2301e126ba97SEli Cohen 		wmb();
2302e126ba97SEli Cohen 
2303e126ba97SEli Cohen 		*qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
2304e126ba97SEli Cohen 	}
2305e126ba97SEli Cohen 
2306e126ba97SEli Cohen 	spin_unlock_irqrestore(&qp->rq.lock, flags);
2307e126ba97SEli Cohen 
2308e126ba97SEli Cohen 	return err;
2309e126ba97SEli Cohen }
2310e126ba97SEli Cohen 
2311e126ba97SEli Cohen static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
2312e126ba97SEli Cohen {
2313e126ba97SEli Cohen 	switch (mlx5_state) {
2314e126ba97SEli Cohen 	case MLX5_QP_STATE_RST:      return IB_QPS_RESET;
2315e126ba97SEli Cohen 	case MLX5_QP_STATE_INIT:     return IB_QPS_INIT;
2316e126ba97SEli Cohen 	case MLX5_QP_STATE_RTR:      return IB_QPS_RTR;
2317e126ba97SEli Cohen 	case MLX5_QP_STATE_RTS:      return IB_QPS_RTS;
2318e126ba97SEli Cohen 	case MLX5_QP_STATE_SQ_DRAINING:
2319e126ba97SEli Cohen 	case MLX5_QP_STATE_SQD:      return IB_QPS_SQD;
2320e126ba97SEli Cohen 	case MLX5_QP_STATE_SQER:     return IB_QPS_SQE;
2321e126ba97SEli Cohen 	case MLX5_QP_STATE_ERR:      return IB_QPS_ERR;
2322e126ba97SEli Cohen 	default:		     return -1;
2323e126ba97SEli Cohen 	}
2324e126ba97SEli Cohen }
2325e126ba97SEli Cohen 
2326e126ba97SEli Cohen static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
2327e126ba97SEli Cohen {
2328e126ba97SEli Cohen 	switch (mlx5_mig_state) {
2329e126ba97SEli Cohen 	case MLX5_QP_PM_ARMED:		return IB_MIG_ARMED;
2330e126ba97SEli Cohen 	case MLX5_QP_PM_REARM:		return IB_MIG_REARM;
2331e126ba97SEli Cohen 	case MLX5_QP_PM_MIGRATED:	return IB_MIG_MIGRATED;
2332e126ba97SEli Cohen 	default: return -1;
2333e126ba97SEli Cohen 	}
2334e126ba97SEli Cohen }
2335e126ba97SEli Cohen 
2336e126ba97SEli Cohen static int to_ib_qp_access_flags(int mlx5_flags)
2337e126ba97SEli Cohen {
2338e126ba97SEli Cohen 	int ib_flags = 0;
2339e126ba97SEli Cohen 
2340e126ba97SEli Cohen 	if (mlx5_flags & MLX5_QP_BIT_RRE)
2341e126ba97SEli Cohen 		ib_flags |= IB_ACCESS_REMOTE_READ;
2342e126ba97SEli Cohen 	if (mlx5_flags & MLX5_QP_BIT_RWE)
2343e126ba97SEli Cohen 		ib_flags |= IB_ACCESS_REMOTE_WRITE;
2344e126ba97SEli Cohen 	if (mlx5_flags & MLX5_QP_BIT_RAE)
2345e126ba97SEli Cohen 		ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
2346e126ba97SEli Cohen 
2347e126ba97SEli Cohen 	return ib_flags;
2348e126ba97SEli Cohen }
2349e126ba97SEli Cohen 
2350e126ba97SEli Cohen static void to_ib_ah_attr(struct mlx5_ib_dev *ibdev, struct ib_ah_attr *ib_ah_attr,
2351e126ba97SEli Cohen 				struct mlx5_qp_path *path)
2352e126ba97SEli Cohen {
2353e126ba97SEli Cohen 	struct mlx5_core_dev *dev = &ibdev->mdev;
2354e126ba97SEli Cohen 
2355e126ba97SEli Cohen 	memset(ib_ah_attr, 0, sizeof(*ib_ah_attr));
2356e126ba97SEli Cohen 	ib_ah_attr->port_num	  = path->port;
2357e126ba97SEli Cohen 
2358e126ba97SEli Cohen 	if (ib_ah_attr->port_num == 0 || ib_ah_attr->port_num > dev->caps.num_ports)
2359e126ba97SEli Cohen 		return;
2360e126ba97SEli Cohen 
2361e126ba97SEli Cohen 	ib_ah_attr->sl = path->sl & 0xf;
2362e126ba97SEli Cohen 
2363e126ba97SEli Cohen 	ib_ah_attr->dlid	  = be16_to_cpu(path->rlid);
2364e126ba97SEli Cohen 	ib_ah_attr->src_path_bits = path->grh_mlid & 0x7f;
2365e126ba97SEli Cohen 	ib_ah_attr->static_rate   = path->static_rate ? path->static_rate - 5 : 0;
2366e126ba97SEli Cohen 	ib_ah_attr->ah_flags      = (path->grh_mlid & (1 << 7)) ? IB_AH_GRH : 0;
2367e126ba97SEli Cohen 	if (ib_ah_attr->ah_flags) {
2368e126ba97SEli Cohen 		ib_ah_attr->grh.sgid_index = path->mgid_index;
2369e126ba97SEli Cohen 		ib_ah_attr->grh.hop_limit  = path->hop_limit;
2370e126ba97SEli Cohen 		ib_ah_attr->grh.traffic_class =
2371e126ba97SEli Cohen 			(be32_to_cpu(path->tclass_flowlabel) >> 20) & 0xff;
2372e126ba97SEli Cohen 		ib_ah_attr->grh.flow_label =
2373e126ba97SEli Cohen 			be32_to_cpu(path->tclass_flowlabel) & 0xfffff;
2374e126ba97SEli Cohen 		memcpy(ib_ah_attr->grh.dgid.raw,
2375e126ba97SEli Cohen 		       path->rgid, sizeof(ib_ah_attr->grh.dgid.raw));
2376e126ba97SEli Cohen 	}
2377e126ba97SEli Cohen }
2378e126ba97SEli Cohen 
2379e126ba97SEli Cohen int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, int qp_attr_mask,
2380e126ba97SEli Cohen 		     struct ib_qp_init_attr *qp_init_attr)
2381e126ba97SEli Cohen {
2382e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2383e126ba97SEli Cohen 	struct mlx5_ib_qp *qp = to_mqp(ibqp);
2384e126ba97SEli Cohen 	struct mlx5_query_qp_mbox_out *outb;
2385e126ba97SEli Cohen 	struct mlx5_qp_context *context;
2386e126ba97SEli Cohen 	int mlx5_state;
2387e126ba97SEli Cohen 	int err = 0;
2388e126ba97SEli Cohen 
2389e126ba97SEli Cohen 	mutex_lock(&qp->mutex);
2390e126ba97SEli Cohen 	outb = kzalloc(sizeof(*outb), GFP_KERNEL);
2391e126ba97SEli Cohen 	if (!outb) {
2392e126ba97SEli Cohen 		err = -ENOMEM;
2393e126ba97SEli Cohen 		goto out;
2394e126ba97SEli Cohen 	}
2395e126ba97SEli Cohen 	context = &outb->ctx;
2396e126ba97SEli Cohen 	err = mlx5_core_qp_query(&dev->mdev, &qp->mqp, outb, sizeof(*outb));
2397e126ba97SEli Cohen 	if (err)
2398e126ba97SEli Cohen 		goto out_free;
2399e126ba97SEli Cohen 
2400e126ba97SEli Cohen 	mlx5_state = be32_to_cpu(context->flags) >> 28;
2401e126ba97SEli Cohen 
2402e126ba97SEli Cohen 	qp->state		     = to_ib_qp_state(mlx5_state);
2403e126ba97SEli Cohen 	qp_attr->qp_state	     = qp->state;
2404e126ba97SEli Cohen 	qp_attr->path_mtu	     = context->mtu_msgmax >> 5;
2405e126ba97SEli Cohen 	qp_attr->path_mig_state	     =
2406e126ba97SEli Cohen 		to_ib_mig_state((be32_to_cpu(context->flags) >> 11) & 0x3);
2407e126ba97SEli Cohen 	qp_attr->qkey		     = be32_to_cpu(context->qkey);
2408e126ba97SEli Cohen 	qp_attr->rq_psn		     = be32_to_cpu(context->rnr_nextrecvpsn) & 0xffffff;
2409e126ba97SEli Cohen 	qp_attr->sq_psn		     = be32_to_cpu(context->next_send_psn) & 0xffffff;
2410e126ba97SEli Cohen 	qp_attr->dest_qp_num	     = be32_to_cpu(context->log_pg_sz_remote_qpn) & 0xffffff;
2411e126ba97SEli Cohen 	qp_attr->qp_access_flags     =
2412e126ba97SEli Cohen 		to_ib_qp_access_flags(be32_to_cpu(context->params2));
2413e126ba97SEli Cohen 
2414e126ba97SEli Cohen 	if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
2415e126ba97SEli Cohen 		to_ib_ah_attr(dev, &qp_attr->ah_attr, &context->pri_path);
2416e126ba97SEli Cohen 		to_ib_ah_attr(dev, &qp_attr->alt_ah_attr, &context->alt_path);
2417e126ba97SEli Cohen 		qp_attr->alt_pkey_index = context->alt_path.pkey_index & 0x7f;
2418e126ba97SEli Cohen 		qp_attr->alt_port_num	= qp_attr->alt_ah_attr.port_num;
2419e126ba97SEli Cohen 	}
2420e126ba97SEli Cohen 
2421e126ba97SEli Cohen 	qp_attr->pkey_index = context->pri_path.pkey_index & 0x7f;
2422e126ba97SEli Cohen 	qp_attr->port_num = context->pri_path.port;
2423e126ba97SEli Cohen 
2424e126ba97SEli Cohen 	/* qp_attr->en_sqd_async_notify is only applicable in modify qp */
2425e126ba97SEli Cohen 	qp_attr->sq_draining = mlx5_state == MLX5_QP_STATE_SQ_DRAINING;
2426e126ba97SEli Cohen 
2427e126ba97SEli Cohen 	qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context->params1) >> 21) & 0x7);
2428e126ba97SEli Cohen 
2429e126ba97SEli Cohen 	qp_attr->max_dest_rd_atomic =
2430e126ba97SEli Cohen 		1 << ((be32_to_cpu(context->params2) >> 21) & 0x7);
2431e126ba97SEli Cohen 	qp_attr->min_rnr_timer	    =
2432e126ba97SEli Cohen 		(be32_to_cpu(context->rnr_nextrecvpsn) >> 24) & 0x1f;
2433e126ba97SEli Cohen 	qp_attr->timeout	    = context->pri_path.ackto_lt >> 3;
2434e126ba97SEli Cohen 	qp_attr->retry_cnt	    = (be32_to_cpu(context->params1) >> 16) & 0x7;
2435e126ba97SEli Cohen 	qp_attr->rnr_retry	    = (be32_to_cpu(context->params1) >> 13) & 0x7;
2436e126ba97SEli Cohen 	qp_attr->alt_timeout	    = context->alt_path.ackto_lt >> 3;
2437e126ba97SEli Cohen 	qp_attr->cur_qp_state	     = qp_attr->qp_state;
2438e126ba97SEli Cohen 	qp_attr->cap.max_recv_wr     = qp->rq.wqe_cnt;
2439e126ba97SEli Cohen 	qp_attr->cap.max_recv_sge    = qp->rq.max_gs;
2440e126ba97SEli Cohen 
2441e126ba97SEli Cohen 	if (!ibqp->uobject) {
2442e126ba97SEli Cohen 		qp_attr->cap.max_send_wr  = qp->sq.wqe_cnt;
2443e126ba97SEli Cohen 		qp_attr->cap.max_send_sge = qp->sq.max_gs;
2444e126ba97SEli Cohen 	} else {
2445e126ba97SEli Cohen 		qp_attr->cap.max_send_wr  = 0;
2446e126ba97SEli Cohen 		qp_attr->cap.max_send_sge = 0;
2447e126ba97SEli Cohen 	}
2448e126ba97SEli Cohen 
2449e126ba97SEli Cohen 	/* We don't support inline sends for kernel QPs (yet), and we
2450e126ba97SEli Cohen 	 * don't know what userspace's value should be.
2451e126ba97SEli Cohen 	 */
2452e126ba97SEli Cohen 	qp_attr->cap.max_inline_data = 0;
2453e126ba97SEli Cohen 
2454e126ba97SEli Cohen 	qp_init_attr->cap	     = qp_attr->cap;
2455e126ba97SEli Cohen 
2456e126ba97SEli Cohen 	qp_init_attr->create_flags = 0;
2457e126ba97SEli Cohen 	if (qp->flags & MLX5_IB_QP_BLOCK_MULTICAST_LOOPBACK)
2458e126ba97SEli Cohen 		qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
2459e126ba97SEli Cohen 
2460e126ba97SEli Cohen 	qp_init_attr->sq_sig_type = qp->sq_signal_bits & MLX5_WQE_CTRL_CQ_UPDATE ?
2461e126ba97SEli Cohen 		IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
2462e126ba97SEli Cohen 
2463e126ba97SEli Cohen out_free:
2464e126ba97SEli Cohen 	kfree(outb);
2465e126ba97SEli Cohen 
2466e126ba97SEli Cohen out:
2467e126ba97SEli Cohen 	mutex_unlock(&qp->mutex);
2468e126ba97SEli Cohen 	return err;
2469e126ba97SEli Cohen }
2470e126ba97SEli Cohen 
2471e126ba97SEli Cohen struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
2472e126ba97SEli Cohen 					  struct ib_ucontext *context,
2473e126ba97SEli Cohen 					  struct ib_udata *udata)
2474e126ba97SEli Cohen {
2475e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
2476e126ba97SEli Cohen 	struct mlx5_ib_xrcd *xrcd;
2477e126ba97SEli Cohen 	int err;
2478e126ba97SEli Cohen 
2479e126ba97SEli Cohen 	if (!(dev->mdev.caps.flags & MLX5_DEV_CAP_FLAG_XRC))
2480e126ba97SEli Cohen 		return ERR_PTR(-ENOSYS);
2481e126ba97SEli Cohen 
2482e126ba97SEli Cohen 	xrcd = kmalloc(sizeof(*xrcd), GFP_KERNEL);
2483e126ba97SEli Cohen 	if (!xrcd)
2484e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
2485e126ba97SEli Cohen 
2486e126ba97SEli Cohen 	err = mlx5_core_xrcd_alloc(&dev->mdev, &xrcd->xrcdn);
2487e126ba97SEli Cohen 	if (err) {
2488e126ba97SEli Cohen 		kfree(xrcd);
2489e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
2490e126ba97SEli Cohen 	}
2491e126ba97SEli Cohen 
2492e126ba97SEli Cohen 	return &xrcd->ibxrcd;
2493e126ba97SEli Cohen }
2494e126ba97SEli Cohen 
2495e126ba97SEli Cohen int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
2496e126ba97SEli Cohen {
2497e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
2498e126ba97SEli Cohen 	u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
2499e126ba97SEli Cohen 	int err;
2500e126ba97SEli Cohen 
2501e126ba97SEli Cohen 	err = mlx5_core_xrcd_dealloc(&dev->mdev, xrcdn);
2502e126ba97SEli Cohen 	if (err) {
2503e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed to dealloc xrcdn 0x%x\n", xrcdn);
2504e126ba97SEli Cohen 		return err;
2505e126ba97SEli Cohen 	}
2506e126ba97SEli Cohen 
2507e126ba97SEli Cohen 	kfree(xrcd);
2508e126ba97SEli Cohen 
2509e126ba97SEli Cohen 	return 0;
2510e126ba97SEli Cohen }
2511