xref: /openbmc/linux/drivers/net/ethernet/mellanox/mlx4/qp.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
15a2cc190SJeff Kirsher /*
25a2cc190SJeff Kirsher  * Copyright (c) 2004 Topspin Communications.  All rights reserved.
35a2cc190SJeff Kirsher  * Copyright (c) 2005, 2006, 2007 Cisco Systems, Inc. All rights reserved.
45a2cc190SJeff Kirsher  * Copyright (c) 2005, 2006, 2007, 2008 Mellanox Technologies. All rights reserved.
55a2cc190SJeff Kirsher  * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
65a2cc190SJeff Kirsher  *
75a2cc190SJeff Kirsher  * This software is available to you under a choice of one of two
85a2cc190SJeff Kirsher  * licenses.  You may choose to be licensed under the terms of the GNU
95a2cc190SJeff Kirsher  * General Public License (GPL) Version 2, available from the file
105a2cc190SJeff Kirsher  * COPYING in the main directory of this source tree, or the
115a2cc190SJeff Kirsher  * OpenIB.org BSD license below:
125a2cc190SJeff Kirsher  *
135a2cc190SJeff Kirsher  *     Redistribution and use in source and binary forms, with or
145a2cc190SJeff Kirsher  *     without modification, are permitted provided that the following
155a2cc190SJeff Kirsher  *     conditions are met:
165a2cc190SJeff Kirsher  *
175a2cc190SJeff Kirsher  *      - Redistributions of source code must retain the above
185a2cc190SJeff Kirsher  *        copyright notice, this list of conditions and the following
195a2cc190SJeff Kirsher  *        disclaimer.
205a2cc190SJeff Kirsher  *
215a2cc190SJeff Kirsher  *      - Redistributions in binary form must reproduce the above
225a2cc190SJeff Kirsher  *        copyright notice, this list of conditions and the following
235a2cc190SJeff Kirsher  *        disclaimer in the documentation and/or other materials
245a2cc190SJeff Kirsher  *        provided with the distribution.
255a2cc190SJeff Kirsher  *
265a2cc190SJeff Kirsher  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
275a2cc190SJeff Kirsher  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
285a2cc190SJeff Kirsher  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
295a2cc190SJeff Kirsher  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
305a2cc190SJeff Kirsher  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
315a2cc190SJeff Kirsher  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
325a2cc190SJeff Kirsher  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
335a2cc190SJeff Kirsher  * SOFTWARE.
345a2cc190SJeff Kirsher  */
355a2cc190SJeff Kirsher 
365a2cc190SJeff Kirsher #include <linux/gfp.h>
37ee40fa06SPaul Gortmaker #include <linux/export.h>
38fe9a2603SJack Morgenstein 
395a2cc190SJeff Kirsher #include <linux/mlx4/cmd.h>
405a2cc190SJeff Kirsher #include <linux/mlx4/qp.h>
415a2cc190SJeff Kirsher 
425a2cc190SJeff Kirsher #include "mlx4.h"
435a2cc190SJeff Kirsher #include "icm.h"
445a2cc190SJeff Kirsher 
45ddae0349SEugenia Emantayev /* QP to support BF should have bits 6,7 cleared */
46ddae0349SEugenia Emantayev #define MLX4_BF_QP_SKIP_MASK	0xc0
47ddae0349SEugenia Emantayev #define MLX4_MAX_BF_QP_RANGE	0x40
48ddae0349SEugenia Emantayev 
mlx4_put_qp(struct mlx4_qp * qp)49*312b8f79SMark Zhang void mlx4_put_qp(struct mlx4_qp *qp)
50*312b8f79SMark Zhang {
51*312b8f79SMark Zhang 	if (refcount_dec_and_test(&qp->refcount))
52*312b8f79SMark Zhang 		complete(&qp->free);
53*312b8f79SMark Zhang }
54*312b8f79SMark Zhang EXPORT_SYMBOL_GPL(mlx4_put_qp);
55*312b8f79SMark Zhang 
mlx4_qp_event(struct mlx4_dev * dev,u32 qpn,int event_type)565a2cc190SJeff Kirsher void mlx4_qp_event(struct mlx4_dev *dev, u32 qpn, int event_type)
575a2cc190SJeff Kirsher {
585a2cc190SJeff Kirsher 	struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
595a2cc190SJeff Kirsher 	struct mlx4_qp *qp;
605a2cc190SJeff Kirsher 
615a2cc190SJeff Kirsher 	spin_lock(&qp_table->lock);
625a2cc190SJeff Kirsher 
635a2cc190SJeff Kirsher 	qp = __mlx4_qp_lookup(dev, qpn);
645a2cc190SJeff Kirsher 	if (qp)
650068895fSElena Reshetova 		refcount_inc(&qp->refcount);
665a2cc190SJeff Kirsher 
675a2cc190SJeff Kirsher 	spin_unlock(&qp_table->lock);
685a2cc190SJeff Kirsher 
695a2cc190SJeff Kirsher 	if (!qp) {
70fe9a2603SJack Morgenstein 		mlx4_dbg(dev, "Async event for none existent QP %08x\n", qpn);
715a2cc190SJeff Kirsher 		return;
725a2cc190SJeff Kirsher 	}
735a2cc190SJeff Kirsher 
74*312b8f79SMark Zhang 	/* Need to call mlx4_put_qp() in event handler */
755a2cc190SJeff Kirsher 	qp->event(qp, event_type);
765a2cc190SJeff Kirsher }
775a2cc190SJeff Kirsher 
78980e9001SJack Morgenstein /* used for INIT/CLOSE port logic */
is_master_qp0(struct mlx4_dev * dev,struct mlx4_qp * qp,int * real_qp0,int * proxy_qp0)7947605df9SJack Morgenstein static int is_master_qp0(struct mlx4_dev *dev, struct mlx4_qp *qp, int *real_qp0, int *proxy_qp0)
80fe9a2603SJack Morgenstein {
8147605df9SJack Morgenstein 	/* this procedure is called after we already know we are on the master */
82980e9001SJack Morgenstein 	/* qp0 is either the proxy qp0, or the real qp0 */
8347605df9SJack Morgenstein 	u32 pf_proxy_offset = dev->phys_caps.base_proxy_sqpn + 8 * mlx4_master_func_num(dev);
8447605df9SJack Morgenstein 	*proxy_qp0 = qp->qpn >= pf_proxy_offset && qp->qpn <= pf_proxy_offset + 1;
85980e9001SJack Morgenstein 
8647605df9SJack Morgenstein 	*real_qp0 = qp->qpn >= dev->phys_caps.base_sqpn &&
8747605df9SJack Morgenstein 		qp->qpn <= dev->phys_caps.base_sqpn + 1;
88980e9001SJack Morgenstein 
89980e9001SJack Morgenstein 	return *real_qp0 || *proxy_qp0;
90fe9a2603SJack Morgenstein }
91fe9a2603SJack Morgenstein 
__mlx4_qp_modify(struct mlx4_dev * dev,struct mlx4_mtt * mtt,enum mlx4_qp_state cur_state,enum mlx4_qp_state new_state,struct mlx4_qp_context * context,enum mlx4_qp_optpar optpar,int sqd_event,struct mlx4_qp * qp,int native)92fe9a2603SJack Morgenstein static int __mlx4_qp_modify(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
935a2cc190SJeff Kirsher 		     enum mlx4_qp_state cur_state, enum mlx4_qp_state new_state,
94fe9a2603SJack Morgenstein 		     struct mlx4_qp_context *context,
95fe9a2603SJack Morgenstein 		     enum mlx4_qp_optpar optpar,
96fe9a2603SJack Morgenstein 		     int sqd_event, struct mlx4_qp *qp, int native)
975a2cc190SJeff Kirsher {
985a2cc190SJeff Kirsher 	static const u16 op[MLX4_QP_NUM_STATE][MLX4_QP_NUM_STATE] = {
995a2cc190SJeff Kirsher 		[MLX4_QP_STATE_RST] = {
1005a2cc190SJeff Kirsher 			[MLX4_QP_STATE_RST]	= MLX4_CMD_2RST_QP,
1015a2cc190SJeff Kirsher 			[MLX4_QP_STATE_ERR]	= MLX4_CMD_2ERR_QP,
1025a2cc190SJeff Kirsher 			[MLX4_QP_STATE_INIT]	= MLX4_CMD_RST2INIT_QP,
1035a2cc190SJeff Kirsher 		},
1045a2cc190SJeff Kirsher 		[MLX4_QP_STATE_INIT]  = {
1055a2cc190SJeff Kirsher 			[MLX4_QP_STATE_RST]	= MLX4_CMD_2RST_QP,
1065a2cc190SJeff Kirsher 			[MLX4_QP_STATE_ERR]	= MLX4_CMD_2ERR_QP,
1075a2cc190SJeff Kirsher 			[MLX4_QP_STATE_INIT]	= MLX4_CMD_INIT2INIT_QP,
1085a2cc190SJeff Kirsher 			[MLX4_QP_STATE_RTR]	= MLX4_CMD_INIT2RTR_QP,
1095a2cc190SJeff Kirsher 		},
1105a2cc190SJeff Kirsher 		[MLX4_QP_STATE_RTR]   = {
1115a2cc190SJeff Kirsher 			[MLX4_QP_STATE_RST]	= MLX4_CMD_2RST_QP,
1125a2cc190SJeff Kirsher 			[MLX4_QP_STATE_ERR]	= MLX4_CMD_2ERR_QP,
1135a2cc190SJeff Kirsher 			[MLX4_QP_STATE_RTS]	= MLX4_CMD_RTR2RTS_QP,
1145a2cc190SJeff Kirsher 		},
1155a2cc190SJeff Kirsher 		[MLX4_QP_STATE_RTS]   = {
1165a2cc190SJeff Kirsher 			[MLX4_QP_STATE_RST]	= MLX4_CMD_2RST_QP,
1175a2cc190SJeff Kirsher 			[MLX4_QP_STATE_ERR]	= MLX4_CMD_2ERR_QP,
1185a2cc190SJeff Kirsher 			[MLX4_QP_STATE_RTS]	= MLX4_CMD_RTS2RTS_QP,
1195a2cc190SJeff Kirsher 			[MLX4_QP_STATE_SQD]	= MLX4_CMD_RTS2SQD_QP,
1205a2cc190SJeff Kirsher 		},
1215a2cc190SJeff Kirsher 		[MLX4_QP_STATE_SQD] = {
1225a2cc190SJeff Kirsher 			[MLX4_QP_STATE_RST]	= MLX4_CMD_2RST_QP,
1235a2cc190SJeff Kirsher 			[MLX4_QP_STATE_ERR]	= MLX4_CMD_2ERR_QP,
1245a2cc190SJeff Kirsher 			[MLX4_QP_STATE_RTS]	= MLX4_CMD_SQD2RTS_QP,
1255a2cc190SJeff Kirsher 			[MLX4_QP_STATE_SQD]	= MLX4_CMD_SQD2SQD_QP,
1265a2cc190SJeff Kirsher 		},
1275a2cc190SJeff Kirsher 		[MLX4_QP_STATE_SQER] = {
1285a2cc190SJeff Kirsher 			[MLX4_QP_STATE_RST]	= MLX4_CMD_2RST_QP,
1295a2cc190SJeff Kirsher 			[MLX4_QP_STATE_ERR]	= MLX4_CMD_2ERR_QP,
1305a2cc190SJeff Kirsher 			[MLX4_QP_STATE_RTS]	= MLX4_CMD_SQERR2RTS_QP,
1315a2cc190SJeff Kirsher 		},
1325a2cc190SJeff Kirsher 		[MLX4_QP_STATE_ERR] = {
1335a2cc190SJeff Kirsher 			[MLX4_QP_STATE_RST]	= MLX4_CMD_2RST_QP,
1345a2cc190SJeff Kirsher 			[MLX4_QP_STATE_ERR]	= MLX4_CMD_2ERR_QP,
1355a2cc190SJeff Kirsher 		}
1365a2cc190SJeff Kirsher 	};
1375a2cc190SJeff Kirsher 
138fe9a2603SJack Morgenstein 	struct mlx4_priv *priv = mlx4_priv(dev);
1395a2cc190SJeff Kirsher 	struct mlx4_cmd_mailbox *mailbox;
1405a2cc190SJeff Kirsher 	int ret = 0;
141980e9001SJack Morgenstein 	int real_qp0 = 0;
142980e9001SJack Morgenstein 	int proxy_qp0 = 0;
143fe9a2603SJack Morgenstein 	u8 port;
1445a2cc190SJeff Kirsher 
1455a2cc190SJeff Kirsher 	if (cur_state >= MLX4_QP_NUM_STATE || new_state >= MLX4_QP_NUM_STATE ||
1465a2cc190SJeff Kirsher 	    !op[cur_state][new_state])
1475a2cc190SJeff Kirsher 		return -EINVAL;
1485a2cc190SJeff Kirsher 
149fe9a2603SJack Morgenstein 	if (op[cur_state][new_state] == MLX4_CMD_2RST_QP) {
150fe9a2603SJack Morgenstein 		ret = mlx4_cmd(dev, 0, qp->qpn, 2,
151fe9a2603SJack Morgenstein 			MLX4_CMD_2RST_QP, MLX4_CMD_TIME_CLASS_A, native);
152fe9a2603SJack Morgenstein 		if (mlx4_is_master(dev) && cur_state != MLX4_QP_STATE_ERR &&
153fe9a2603SJack Morgenstein 		    cur_state != MLX4_QP_STATE_RST &&
15447605df9SJack Morgenstein 		    is_master_qp0(dev, qp, &real_qp0, &proxy_qp0)) {
155fe9a2603SJack Morgenstein 			port = (qp->qpn & 1) + 1;
156980e9001SJack Morgenstein 			if (proxy_qp0)
157980e9001SJack Morgenstein 				priv->mfunc.master.qp0_state[port].proxy_qp0_active = 0;
158980e9001SJack Morgenstein 			else
159fe9a2603SJack Morgenstein 				priv->mfunc.master.qp0_state[port].qp0_active = 0;
160fe9a2603SJack Morgenstein 		}
161fe9a2603SJack Morgenstein 		return ret;
162fe9a2603SJack Morgenstein 	}
1635a2cc190SJeff Kirsher 
1645a2cc190SJeff Kirsher 	mailbox = mlx4_alloc_cmd_mailbox(dev);
1655a2cc190SJeff Kirsher 	if (IS_ERR(mailbox))
1665a2cc190SJeff Kirsher 		return PTR_ERR(mailbox);
1675a2cc190SJeff Kirsher 
1685a2cc190SJeff Kirsher 	if (cur_state == MLX4_QP_STATE_RST && new_state == MLX4_QP_STATE_INIT) {
1695a2cc190SJeff Kirsher 		u64 mtt_addr = mlx4_mtt_addr(dev, mtt);
1705a2cc190SJeff Kirsher 		context->mtt_base_addr_h = mtt_addr >> 32;
1715a2cc190SJeff Kirsher 		context->mtt_base_addr_l = cpu_to_be32(mtt_addr & 0xffffffff);
1725a2cc190SJeff Kirsher 		context->log_page_size   = mtt->page_shift - MLX4_ICM_PAGE_SHIFT;
1735a2cc190SJeff Kirsher 	}
1745a2cc190SJeff Kirsher 
1753f723f42SMoni Shoua 	if ((cur_state == MLX4_QP_STATE_RTR) &&
1763f723f42SMoni Shoua 	    (new_state == MLX4_QP_STATE_RTS) &&
1773f723f42SMoni Shoua 	    dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ROCE_V1_V2)
1783f723f42SMoni Shoua 		context->roce_entropy =
1793f723f42SMoni Shoua 			cpu_to_be16(mlx4_qp_roce_entropy(dev, qp->qpn));
1803f723f42SMoni Shoua 
1815a2cc190SJeff Kirsher 	*(__be32 *) mailbox->buf = cpu_to_be32(optpar);
18231975e27Sstephen hemminger 	memcpy(mailbox->buf + 8, context, sizeof(*context));
1835a2cc190SJeff Kirsher 
1845a2cc190SJeff Kirsher 	((struct mlx4_qp_context *) (mailbox->buf + 8))->local_qpn =
1855a2cc190SJeff Kirsher 		cpu_to_be32(qp->qpn);
1865a2cc190SJeff Kirsher 
187eb41049fSMarcel Apfelbaum 	ret = mlx4_cmd(dev, mailbox->dma,
188fe9a2603SJack Morgenstein 		       qp->qpn | (!!sqd_event << 31),
1895a2cc190SJeff Kirsher 		       new_state == MLX4_QP_STATE_RST ? 2 : 0,
190fe9a2603SJack Morgenstein 		       op[cur_state][new_state], MLX4_CMD_TIME_CLASS_C, native);
1915a2cc190SJeff Kirsher 
19247605df9SJack Morgenstein 	if (mlx4_is_master(dev) && is_master_qp0(dev, qp, &real_qp0, &proxy_qp0)) {
193980e9001SJack Morgenstein 		port = (qp->qpn & 1) + 1;
194980e9001SJack Morgenstein 		if (cur_state != MLX4_QP_STATE_ERR &&
195980e9001SJack Morgenstein 		    cur_state != MLX4_QP_STATE_RST &&
196980e9001SJack Morgenstein 		    new_state == MLX4_QP_STATE_ERR) {
197980e9001SJack Morgenstein 			if (proxy_qp0)
198980e9001SJack Morgenstein 				priv->mfunc.master.qp0_state[port].proxy_qp0_active = 0;
199980e9001SJack Morgenstein 			else
200980e9001SJack Morgenstein 				priv->mfunc.master.qp0_state[port].qp0_active = 0;
201980e9001SJack Morgenstein 		} else if (new_state == MLX4_QP_STATE_RTR) {
202980e9001SJack Morgenstein 			if (proxy_qp0)
203980e9001SJack Morgenstein 				priv->mfunc.master.qp0_state[port].proxy_qp0_active = 1;
204980e9001SJack Morgenstein 			else
205980e9001SJack Morgenstein 				priv->mfunc.master.qp0_state[port].qp0_active = 1;
206980e9001SJack Morgenstein 		}
207980e9001SJack Morgenstein 	}
208980e9001SJack Morgenstein 
2095a2cc190SJeff Kirsher 	mlx4_free_cmd_mailbox(dev, mailbox);
2105a2cc190SJeff Kirsher 	return ret;
2115a2cc190SJeff Kirsher }
212fe9a2603SJack Morgenstein 
mlx4_qp_modify(struct mlx4_dev * dev,struct mlx4_mtt * mtt,enum mlx4_qp_state cur_state,enum mlx4_qp_state new_state,struct mlx4_qp_context * context,enum mlx4_qp_optpar optpar,int sqd_event,struct mlx4_qp * qp)213fe9a2603SJack Morgenstein int mlx4_qp_modify(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
214fe9a2603SJack Morgenstein 		   enum mlx4_qp_state cur_state, enum mlx4_qp_state new_state,
215fe9a2603SJack Morgenstein 		   struct mlx4_qp_context *context,
216fe9a2603SJack Morgenstein 		   enum mlx4_qp_optpar optpar,
217fe9a2603SJack Morgenstein 		   int sqd_event, struct mlx4_qp *qp)
218fe9a2603SJack Morgenstein {
219fe9a2603SJack Morgenstein 	return __mlx4_qp_modify(dev, mtt, cur_state, new_state, context,
220fe9a2603SJack Morgenstein 				optpar, sqd_event, qp, 0);
221fe9a2603SJack Morgenstein }
2225a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_qp_modify);
2235a2cc190SJeff Kirsher 
__mlx4_qp_reserve_range(struct mlx4_dev * dev,int cnt,int align,int * base,u8 flags)224c82e9aa0SEli Cohen int __mlx4_qp_reserve_range(struct mlx4_dev *dev, int cnt, int align,
225ddae0349SEugenia Emantayev 			    int *base, u8 flags)
226fe9a2603SJack Morgenstein {
227d57febe1SMatan Barak 	u32 uid;
228ddae0349SEugenia Emantayev 	int bf_qp = !!(flags & (u8)MLX4_RESERVE_ETH_BF_QP);
229ddae0349SEugenia Emantayev 
230fe9a2603SJack Morgenstein 	struct mlx4_priv *priv = mlx4_priv(dev);
231fe9a2603SJack Morgenstein 	struct mlx4_qp_table *qp_table = &priv->qp_table;
232fe9a2603SJack Morgenstein 
233ddae0349SEugenia Emantayev 	if (cnt > MLX4_MAX_BF_QP_RANGE && bf_qp)
234ddae0349SEugenia Emantayev 		return -ENOMEM;
235ddae0349SEugenia Emantayev 
236d57febe1SMatan Barak 	uid = MLX4_QP_TABLE_ZONE_GENERAL;
237d57febe1SMatan Barak 	if (flags & (u8)MLX4_RESERVE_A0_QP) {
238d57febe1SMatan Barak 		if (bf_qp)
239d57febe1SMatan Barak 			uid = MLX4_QP_TABLE_ZONE_RAW_ETH;
240d57febe1SMatan Barak 		else
241d57febe1SMatan Barak 			uid = MLX4_QP_TABLE_ZONE_RSS;
242d57febe1SMatan Barak 	}
243d57febe1SMatan Barak 
244d57febe1SMatan Barak 	*base = mlx4_zone_alloc_entries(qp_table->zones, uid, cnt, align,
245d57febe1SMatan Barak 					bf_qp ? MLX4_BF_QP_SKIP_MASK : 0, NULL);
246fe9a2603SJack Morgenstein 	if (*base == -1)
247fe9a2603SJack Morgenstein 		return -ENOMEM;
248fe9a2603SJack Morgenstein 
249fe9a2603SJack Morgenstein 	return 0;
250fe9a2603SJack Morgenstein }
251fe9a2603SJack Morgenstein 
mlx4_qp_reserve_range(struct mlx4_dev * dev,int cnt,int align,int * base,u8 flags,u8 usage)252ddae0349SEugenia Emantayev int mlx4_qp_reserve_range(struct mlx4_dev *dev, int cnt, int align,
253f3301870SMoshe Shemesh 			  int *base, u8 flags, u8 usage)
2545a2cc190SJeff Kirsher {
255f3301870SMoshe Shemesh 	u32 in_modifier = RES_QP | (((u32)usage & 3) << 30);
256e7dbeba8SJack Morgenstein 	u64 in_param = 0;
257fe9a2603SJack Morgenstein 	u64 out_param;
258fe9a2603SJack Morgenstein 	int err;
2595a2cc190SJeff Kirsher 
260ddae0349SEugenia Emantayev 	/* Turn off all unsupported QP allocation flags */
261ddae0349SEugenia Emantayev 	flags &= dev->caps.alloc_res_qp_mask;
262ddae0349SEugenia Emantayev 
263fe9a2603SJack Morgenstein 	if (mlx4_is_mfunc(dev)) {
264ddae0349SEugenia Emantayev 		set_param_l(&in_param, (((u32)flags) << 24) | (u32)cnt);
265fe9a2603SJack Morgenstein 		set_param_h(&in_param, align);
266fe9a2603SJack Morgenstein 		err = mlx4_cmd_imm(dev, in_param, &out_param,
267f3301870SMoshe Shemesh 				   in_modifier, RES_OP_RESERVE,
268fe9a2603SJack Morgenstein 				   MLX4_CMD_ALLOC_RES,
269fe9a2603SJack Morgenstein 				   MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
270fe9a2603SJack Morgenstein 		if (err)
271fe9a2603SJack Morgenstein 			return err;
2725a2cc190SJeff Kirsher 
273fe9a2603SJack Morgenstein 		*base = get_param_l(&out_param);
2745a2cc190SJeff Kirsher 		return 0;
2755a2cc190SJeff Kirsher 	}
276ddae0349SEugenia Emantayev 	return __mlx4_qp_reserve_range(dev, cnt, align, base, flags);
277fe9a2603SJack Morgenstein }
2785a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_qp_reserve_range);
2795a2cc190SJeff Kirsher 
__mlx4_qp_release_range(struct mlx4_dev * dev,int base_qpn,int cnt)280c82e9aa0SEli Cohen void __mlx4_qp_release_range(struct mlx4_dev *dev, int base_qpn, int cnt)
2815a2cc190SJeff Kirsher {
2825a2cc190SJeff Kirsher 	struct mlx4_priv *priv = mlx4_priv(dev);
2835a2cc190SJeff Kirsher 	struct mlx4_qp_table *qp_table = &priv->qp_table;
2845a2cc190SJeff Kirsher 
285fe9a2603SJack Morgenstein 	if (mlx4_is_qp_reserved(dev, (u32) base_qpn))
286fe9a2603SJack Morgenstein 		return;
287d57febe1SMatan Barak 	mlx4_zone_free_entries_unique(qp_table->zones, base_qpn, cnt);
2885a2cc190SJeff Kirsher }
289fe9a2603SJack Morgenstein 
mlx4_qp_release_range(struct mlx4_dev * dev,int base_qpn,int cnt)290fe9a2603SJack Morgenstein void mlx4_qp_release_range(struct mlx4_dev *dev, int base_qpn, int cnt)
291fe9a2603SJack Morgenstein {
292e7dbeba8SJack Morgenstein 	u64 in_param = 0;
293fe9a2603SJack Morgenstein 	int err;
294fe9a2603SJack Morgenstein 
295852f6927SJack Morgenstein 	if (!cnt)
296852f6927SJack Morgenstein 		return;
297852f6927SJack Morgenstein 
298fe9a2603SJack Morgenstein 	if (mlx4_is_mfunc(dev)) {
299fe9a2603SJack Morgenstein 		set_param_l(&in_param, base_qpn);
300fe9a2603SJack Morgenstein 		set_param_h(&in_param, cnt);
301fe9a2603SJack Morgenstein 		err = mlx4_cmd(dev, in_param, RES_QP, RES_OP_RESERVE,
302fe9a2603SJack Morgenstein 			       MLX4_CMD_FREE_RES,
303fe9a2603SJack Morgenstein 			       MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
304fe9a2603SJack Morgenstein 		if (err) {
3051a91de28SJoe Perches 			mlx4_warn(dev, "Failed to release qp range base:%d cnt:%d\n",
3061a91de28SJoe Perches 				  base_qpn, cnt);
307fe9a2603SJack Morgenstein 		}
308fe9a2603SJack Morgenstein 	} else
309fe9a2603SJack Morgenstein 		 __mlx4_qp_release_range(dev, base_qpn, cnt);
310fe9a2603SJack Morgenstein }
3115a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_qp_release_range);
3125a2cc190SJeff Kirsher 
__mlx4_qp_alloc_icm(struct mlx4_dev * dev,int qpn)3138900b894SLeon Romanovsky int __mlx4_qp_alloc_icm(struct mlx4_dev *dev, int qpn)
314fe9a2603SJack Morgenstein {
315fe9a2603SJack Morgenstein 	struct mlx4_priv *priv = mlx4_priv(dev);
316fe9a2603SJack Morgenstein 	struct mlx4_qp_table *qp_table = &priv->qp_table;
317fe9a2603SJack Morgenstein 	int err;
318fe9a2603SJack Morgenstein 
3198900b894SLeon Romanovsky 	err = mlx4_table_get(dev, &qp_table->qp_table, qpn);
320fe9a2603SJack Morgenstein 	if (err)
321fe9a2603SJack Morgenstein 		goto err_out;
322fe9a2603SJack Morgenstein 
3238900b894SLeon Romanovsky 	err = mlx4_table_get(dev, &qp_table->auxc_table, qpn);
324fe9a2603SJack Morgenstein 	if (err)
325fe9a2603SJack Morgenstein 		goto err_put_qp;
326fe9a2603SJack Morgenstein 
3278900b894SLeon Romanovsky 	err = mlx4_table_get(dev, &qp_table->altc_table, qpn);
328fe9a2603SJack Morgenstein 	if (err)
329fe9a2603SJack Morgenstein 		goto err_put_auxc;
330fe9a2603SJack Morgenstein 
3318900b894SLeon Romanovsky 	err = mlx4_table_get(dev, &qp_table->rdmarc_table, qpn);
332fe9a2603SJack Morgenstein 	if (err)
333fe9a2603SJack Morgenstein 		goto err_put_altc;
334fe9a2603SJack Morgenstein 
3358900b894SLeon Romanovsky 	err = mlx4_table_get(dev, &qp_table->cmpt_table, qpn);
336fe9a2603SJack Morgenstein 	if (err)
337fe9a2603SJack Morgenstein 		goto err_put_rdmarc;
338fe9a2603SJack Morgenstein 
339fe9a2603SJack Morgenstein 	return 0;
340fe9a2603SJack Morgenstein 
341fe9a2603SJack Morgenstein err_put_rdmarc:
342fe9a2603SJack Morgenstein 	mlx4_table_put(dev, &qp_table->rdmarc_table, qpn);
343fe9a2603SJack Morgenstein 
344fe9a2603SJack Morgenstein err_put_altc:
345fe9a2603SJack Morgenstein 	mlx4_table_put(dev, &qp_table->altc_table, qpn);
346fe9a2603SJack Morgenstein 
347fe9a2603SJack Morgenstein err_put_auxc:
348fe9a2603SJack Morgenstein 	mlx4_table_put(dev, &qp_table->auxc_table, qpn);
349fe9a2603SJack Morgenstein 
350fe9a2603SJack Morgenstein err_put_qp:
351fe9a2603SJack Morgenstein 	mlx4_table_put(dev, &qp_table->qp_table, qpn);
352fe9a2603SJack Morgenstein 
353fe9a2603SJack Morgenstein err_out:
354fe9a2603SJack Morgenstein 	return err;
355fe9a2603SJack Morgenstein }
356fe9a2603SJack Morgenstein 
mlx4_qp_alloc_icm(struct mlx4_dev * dev,int qpn)3578900b894SLeon Romanovsky static int mlx4_qp_alloc_icm(struct mlx4_dev *dev, int qpn)
358fe9a2603SJack Morgenstein {
359e7dbeba8SJack Morgenstein 	u64 param = 0;
360fe9a2603SJack Morgenstein 
361fe9a2603SJack Morgenstein 	if (mlx4_is_mfunc(dev)) {
362fe9a2603SJack Morgenstein 		set_param_l(&param, qpn);
363fe9a2603SJack Morgenstein 		return mlx4_cmd_imm(dev, param, &param, RES_QP, RES_OP_MAP_ICM,
364fe9a2603SJack Morgenstein 				    MLX4_CMD_ALLOC_RES, MLX4_CMD_TIME_CLASS_A,
365fe9a2603SJack Morgenstein 				    MLX4_CMD_WRAPPED);
366fe9a2603SJack Morgenstein 	}
3678900b894SLeon Romanovsky 	return __mlx4_qp_alloc_icm(dev, qpn);
368fe9a2603SJack Morgenstein }
369fe9a2603SJack Morgenstein 
__mlx4_qp_free_icm(struct mlx4_dev * dev,int qpn)370c82e9aa0SEli Cohen void __mlx4_qp_free_icm(struct mlx4_dev *dev, int qpn)
371fe9a2603SJack Morgenstein {
372fe9a2603SJack Morgenstein 	struct mlx4_priv *priv = mlx4_priv(dev);
373fe9a2603SJack Morgenstein 	struct mlx4_qp_table *qp_table = &priv->qp_table;
374fe9a2603SJack Morgenstein 
375fe9a2603SJack Morgenstein 	mlx4_table_put(dev, &qp_table->cmpt_table, qpn);
376fe9a2603SJack Morgenstein 	mlx4_table_put(dev, &qp_table->rdmarc_table, qpn);
377fe9a2603SJack Morgenstein 	mlx4_table_put(dev, &qp_table->altc_table, qpn);
378fe9a2603SJack Morgenstein 	mlx4_table_put(dev, &qp_table->auxc_table, qpn);
379fe9a2603SJack Morgenstein 	mlx4_table_put(dev, &qp_table->qp_table, qpn);
380fe9a2603SJack Morgenstein }
381fe9a2603SJack Morgenstein 
mlx4_qp_free_icm(struct mlx4_dev * dev,int qpn)382fe9a2603SJack Morgenstein static void mlx4_qp_free_icm(struct mlx4_dev *dev, int qpn)
383fe9a2603SJack Morgenstein {
384e7dbeba8SJack Morgenstein 	u64 in_param = 0;
385fe9a2603SJack Morgenstein 
386fe9a2603SJack Morgenstein 	if (mlx4_is_mfunc(dev)) {
387fe9a2603SJack Morgenstein 		set_param_l(&in_param, qpn);
388fe9a2603SJack Morgenstein 		if (mlx4_cmd(dev, in_param, RES_QP, RES_OP_MAP_ICM,
389fe9a2603SJack Morgenstein 			     MLX4_CMD_FREE_RES, MLX4_CMD_TIME_CLASS_A,
390fe9a2603SJack Morgenstein 			     MLX4_CMD_WRAPPED))
391fe9a2603SJack Morgenstein 			mlx4_warn(dev, "Failed to free icm of qp:%d\n", qpn);
392fe9a2603SJack Morgenstein 	} else
393fe9a2603SJack Morgenstein 		__mlx4_qp_free_icm(dev, qpn);
394fe9a2603SJack Morgenstein }
395fe9a2603SJack Morgenstein 
mlx4_qp_lookup(struct mlx4_dev * dev,u32 qpn)3966dc06c08STalat Batheesh struct mlx4_qp *mlx4_qp_lookup(struct mlx4_dev *dev, u32 qpn)
3976dc06c08STalat Batheesh {
3986dc06c08STalat Batheesh 	struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
3996dc06c08STalat Batheesh 	struct mlx4_qp *qp;
4006dc06c08STalat Batheesh 
401d546b67cSJack Morgenstein 	spin_lock_irq(&qp_table->lock);
4026dc06c08STalat Batheesh 
4036dc06c08STalat Batheesh 	qp = __mlx4_qp_lookup(dev, qpn);
4046dc06c08STalat Batheesh 
405d546b67cSJack Morgenstein 	spin_unlock_irq(&qp_table->lock);
4066dc06c08STalat Batheesh 	return qp;
4076dc06c08STalat Batheesh }
4086dc06c08STalat Batheesh 
mlx4_qp_alloc(struct mlx4_dev * dev,int qpn,struct mlx4_qp * qp)4098900b894SLeon Romanovsky int mlx4_qp_alloc(struct mlx4_dev *dev, int qpn, struct mlx4_qp *qp)
4105a2cc190SJeff Kirsher {
4115a2cc190SJeff Kirsher 	struct mlx4_priv *priv = mlx4_priv(dev);
4125a2cc190SJeff Kirsher 	struct mlx4_qp_table *qp_table = &priv->qp_table;
4135a2cc190SJeff Kirsher 	int err;
4145a2cc190SJeff Kirsher 
4155a2cc190SJeff Kirsher 	if (!qpn)
4165a2cc190SJeff Kirsher 		return -EINVAL;
4175a2cc190SJeff Kirsher 
4185a2cc190SJeff Kirsher 	qp->qpn = qpn;
4195a2cc190SJeff Kirsher 
4208900b894SLeon Romanovsky 	err = mlx4_qp_alloc_icm(dev, qpn);
4215a2cc190SJeff Kirsher 	if (err)
422fe9a2603SJack Morgenstein 		return err;
4235a2cc190SJeff Kirsher 
4245a2cc190SJeff Kirsher 	spin_lock_irq(&qp_table->lock);
425fe9a2603SJack Morgenstein 	err = radix_tree_insert(&dev->qp_table_tree, qp->qpn &
426fe9a2603SJack Morgenstein 				(dev->caps.num_qps - 1), qp);
4275a2cc190SJeff Kirsher 	spin_unlock_irq(&qp_table->lock);
4285a2cc190SJeff Kirsher 	if (err)
429fe9a2603SJack Morgenstein 		goto err_icm;
4305a2cc190SJeff Kirsher 
4310068895fSElena Reshetova 	refcount_set(&qp->refcount, 1);
4325a2cc190SJeff Kirsher 	init_completion(&qp->free);
4335a2cc190SJeff Kirsher 
4345a2cc190SJeff Kirsher 	return 0;
4355a2cc190SJeff Kirsher 
436fe9a2603SJack Morgenstein err_icm:
437fe9a2603SJack Morgenstein 	mlx4_qp_free_icm(dev, qpn);
4385a2cc190SJeff Kirsher 	return err;
4395a2cc190SJeff Kirsher }
440fe9a2603SJack Morgenstein 
4415a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_qp_alloc);
4425a2cc190SJeff Kirsher 
mlx4_update_qp(struct mlx4_dev * dev,u32 qpn,enum mlx4_update_qp_attr attr,struct mlx4_update_qp_params * params)44309e05c3fSMatan Barak int mlx4_update_qp(struct mlx4_dev *dev, u32 qpn,
444ce8d9e0dSMatan Barak 		   enum mlx4_update_qp_attr attr,
445ce8d9e0dSMatan Barak 		   struct mlx4_update_qp_params *params)
446ce8d9e0dSMatan Barak {
447ce8d9e0dSMatan Barak 	struct mlx4_cmd_mailbox *mailbox;
448ce8d9e0dSMatan Barak 	struct mlx4_update_qp_context *cmd;
449ce8d9e0dSMatan Barak 	u64 pri_addr_path_mask = 0;
45009e05c3fSMatan Barak 	u64 qp_mask = 0;
451ce8d9e0dSMatan Barak 	int err = 0;
452ce8d9e0dSMatan Barak 
453a5b3c56eSJack Morgenstein 	if (!attr || (attr & ~MLX4_UPDATE_QP_SUPPORTED_ATTRS))
454a5b3c56eSJack Morgenstein 		return -EINVAL;
455a5b3c56eSJack Morgenstein 
456ce8d9e0dSMatan Barak 	mailbox = mlx4_alloc_cmd_mailbox(dev);
457ce8d9e0dSMatan Barak 	if (IS_ERR(mailbox))
458ce8d9e0dSMatan Barak 		return PTR_ERR(mailbox);
459ce8d9e0dSMatan Barak 
460ce8d9e0dSMatan Barak 	cmd = (struct mlx4_update_qp_context *)mailbox->buf;
461ce8d9e0dSMatan Barak 
462ce8d9e0dSMatan Barak 	if (attr & MLX4_UPDATE_QP_SMAC) {
463ce8d9e0dSMatan Barak 		pri_addr_path_mask |= 1ULL << MLX4_UPD_QP_PATH_MASK_MAC_INDEX;
464ce8d9e0dSMatan Barak 		cmd->qp_context.pri_path.grh_mylmc = params->smac_index;
465ce8d9e0dSMatan Barak 	}
466ce8d9e0dSMatan Barak 
4679a892835SMaor Gottlieb 	if (attr & MLX4_UPDATE_QP_ETH_SRC_CHECK_MC_LB) {
4689a892835SMaor Gottlieb 		if (!(dev->caps.flags2
4699a892835SMaor Gottlieb 		      & MLX4_DEV_CAP_FLAG2_UPDATE_QP_SRC_CHECK_LB)) {
4709a892835SMaor Gottlieb 			mlx4_warn(dev,
4719a892835SMaor Gottlieb 				  "Trying to set src check LB, but it isn't supported\n");
472423b3aecSOr Gerlitz 			err = -EOPNOTSUPP;
4739a892835SMaor Gottlieb 			goto out;
4749a892835SMaor Gottlieb 		}
4759a892835SMaor Gottlieb 		pri_addr_path_mask |=
4769a892835SMaor Gottlieb 			1ULL << MLX4_UPD_QP_PATH_MASK_ETH_SRC_CHECK_MC_LB;
4779a892835SMaor Gottlieb 		if (params->flags &
4789a892835SMaor Gottlieb 		    MLX4_UPDATE_QP_PARAMS_FLAGS_ETH_CHECK_MC_LB) {
4799a892835SMaor Gottlieb 			cmd->qp_context.pri_path.fl |=
4809a892835SMaor Gottlieb 				MLX4_FL_ETH_SRC_CHECK_MC_LB;
4819a892835SMaor Gottlieb 		}
4829a892835SMaor Gottlieb 	}
4839a892835SMaor Gottlieb 
48409e05c3fSMatan Barak 	if (attr & MLX4_UPDATE_QP_VSD) {
48509e05c3fSMatan Barak 		qp_mask |= 1ULL << MLX4_UPD_QP_MASK_VSD;
48609e05c3fSMatan Barak 		if (params->flags & MLX4_UPDATE_QP_PARAMS_FLAGS_VSD_ENABLE)
48709e05c3fSMatan Barak 			cmd->qp_context.param3 |= cpu_to_be32(MLX4_STRIP_VLAN);
48809e05c3fSMatan Barak 	}
489ce8d9e0dSMatan Barak 
490fc31e256SOr Gerlitz 	if (attr & MLX4_UPDATE_QP_RATE_LIMIT) {
491fc31e256SOr Gerlitz 		qp_mask |= 1ULL << MLX4_UPD_QP_MASK_RATE_LIMIT;
492fc31e256SOr Gerlitz 		cmd->qp_context.rate_limit_params = cpu_to_be16((params->rate_unit << 14) | params->rate_val);
493fc31e256SOr Gerlitz 	}
494fc31e256SOr Gerlitz 
49508068cd5SIdo Shamay 	if (attr & MLX4_UPDATE_QP_QOS_VPORT) {
496269f9883SIdo Shamay 		if (!(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QOS_VPP)) {
497269f9883SIdo Shamay 			mlx4_warn(dev, "Granular QoS per VF is not enabled\n");
498269f9883SIdo Shamay 			err = -EOPNOTSUPP;
499269f9883SIdo Shamay 			goto out;
500269f9883SIdo Shamay 		}
501269f9883SIdo Shamay 
50208068cd5SIdo Shamay 		qp_mask |= 1ULL << MLX4_UPD_QP_MASK_QOS_VPP;
50308068cd5SIdo Shamay 		cmd->qp_context.qos_vport = params->qos_vport;
50408068cd5SIdo Shamay 	}
50508068cd5SIdo Shamay 
50609e05c3fSMatan Barak 	cmd->primary_addr_path_mask = cpu_to_be64(pri_addr_path_mask);
50709e05c3fSMatan Barak 	cmd->qp_mask = cpu_to_be64(qp_mask);
50809e05c3fSMatan Barak 
50909e05c3fSMatan Barak 	err = mlx4_cmd(dev, mailbox->dma, qpn & 0xffffff, 0,
510ce8d9e0dSMatan Barak 		       MLX4_CMD_UPDATE_QP, MLX4_CMD_TIME_CLASS_A,
511ce8d9e0dSMatan Barak 		       MLX4_CMD_NATIVE);
5129a892835SMaor Gottlieb out:
513ce8d9e0dSMatan Barak 	mlx4_free_cmd_mailbox(dev, mailbox);
514ce8d9e0dSMatan Barak 	return err;
515ce8d9e0dSMatan Barak }
516ce8d9e0dSMatan Barak EXPORT_SYMBOL_GPL(mlx4_update_qp);
517ce8d9e0dSMatan Barak 
mlx4_qp_remove(struct mlx4_dev * dev,struct mlx4_qp * qp)5185a2cc190SJeff Kirsher void mlx4_qp_remove(struct mlx4_dev *dev, struct mlx4_qp *qp)
5195a2cc190SJeff Kirsher {
5205a2cc190SJeff Kirsher 	struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
5215a2cc190SJeff Kirsher 	unsigned long flags;
5225a2cc190SJeff Kirsher 
5235a2cc190SJeff Kirsher 	spin_lock_irqsave(&qp_table->lock, flags);
5245a2cc190SJeff Kirsher 	radix_tree_delete(&dev->qp_table_tree, qp->qpn & (dev->caps.num_qps - 1));
5255a2cc190SJeff Kirsher 	spin_unlock_irqrestore(&qp_table->lock, flags);
5265a2cc190SJeff Kirsher }
5275a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_qp_remove);
5285a2cc190SJeff Kirsher 
mlx4_qp_free(struct mlx4_dev * dev,struct mlx4_qp * qp)5295a2cc190SJeff Kirsher void mlx4_qp_free(struct mlx4_dev *dev, struct mlx4_qp *qp)
5305a2cc190SJeff Kirsher {
531*312b8f79SMark Zhang 	mlx4_put_qp(qp);
5325a2cc190SJeff Kirsher 	wait_for_completion(&qp->free);
5335a2cc190SJeff Kirsher 
534fe9a2603SJack Morgenstein 	mlx4_qp_free_icm(dev, qp->qpn);
5355a2cc190SJeff Kirsher }
5365a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_qp_free);
5375a2cc190SJeff Kirsher 
mlx4_CONF_SPECIAL_QP(struct mlx4_dev * dev,u32 base_qpn)5385a2cc190SJeff Kirsher static int mlx4_CONF_SPECIAL_QP(struct mlx4_dev *dev, u32 base_qpn)
5395a2cc190SJeff Kirsher {
5405a2cc190SJeff Kirsher 	return mlx4_cmd(dev, 0, base_qpn, 0, MLX4_CMD_CONF_SPECIAL_QP,
541f9baff50SJack Morgenstein 			MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
5425a2cc190SJeff Kirsher }
5435a2cc190SJeff Kirsher 
544d57febe1SMatan Barak #define MLX4_QP_TABLE_RSS_ETH_PRIORITY 2
545d57febe1SMatan Barak #define MLX4_QP_TABLE_RAW_ETH_PRIORITY 1
546d57febe1SMatan Barak #define MLX4_QP_TABLE_RAW_ETH_SIZE     256
547d57febe1SMatan Barak 
mlx4_create_zones(struct mlx4_dev * dev,u32 reserved_bottom_general,u32 reserved_top_general,u32 reserved_bottom_rss,u32 start_offset_rss,u32 max_table_offset)548d57febe1SMatan Barak static int mlx4_create_zones(struct mlx4_dev *dev,
549d57febe1SMatan Barak 			     u32 reserved_bottom_general,
550d57febe1SMatan Barak 			     u32 reserved_top_general,
551d57febe1SMatan Barak 			     u32 reserved_bottom_rss,
552d57febe1SMatan Barak 			     u32 start_offset_rss,
553d57febe1SMatan Barak 			     u32 max_table_offset)
554d57febe1SMatan Barak {
555d57febe1SMatan Barak 	struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
556d57febe1SMatan Barak 	struct mlx4_bitmap (*bitmap)[MLX4_QP_TABLE_ZONE_NUM] = NULL;
557d57febe1SMatan Barak 	int bitmap_initialized = 0;
558d57febe1SMatan Barak 	u32 last_offset;
559d57febe1SMatan Barak 	int k;
560d57febe1SMatan Barak 	int err;
561d57febe1SMatan Barak 
562d57febe1SMatan Barak 	qp_table->zones = mlx4_zone_allocator_create(MLX4_ZONE_ALLOC_FLAGS_NO_OVERLAP);
563d57febe1SMatan Barak 
564d57febe1SMatan Barak 	if (NULL == qp_table->zones)
565d57febe1SMatan Barak 		return -ENOMEM;
566d57febe1SMatan Barak 
567d57febe1SMatan Barak 	bitmap = kmalloc(sizeof(*bitmap), GFP_KERNEL);
568d57febe1SMatan Barak 
569d57febe1SMatan Barak 	if (NULL == bitmap) {
570d57febe1SMatan Barak 		err = -ENOMEM;
571d57febe1SMatan Barak 		goto free_zone;
572d57febe1SMatan Barak 	}
573d57febe1SMatan Barak 
574d57febe1SMatan Barak 	err = mlx4_bitmap_init(*bitmap + MLX4_QP_TABLE_ZONE_GENERAL, dev->caps.num_qps,
575d57febe1SMatan Barak 			       (1 << 23) - 1, reserved_bottom_general,
576d57febe1SMatan Barak 			       reserved_top_general);
577d57febe1SMatan Barak 
578d57febe1SMatan Barak 	if (err)
579d57febe1SMatan Barak 		goto free_bitmap;
580d57febe1SMatan Barak 
581d57febe1SMatan Barak 	++bitmap_initialized;
582d57febe1SMatan Barak 
583d57febe1SMatan Barak 	err = mlx4_zone_add_one(qp_table->zones, *bitmap + MLX4_QP_TABLE_ZONE_GENERAL,
584d57febe1SMatan Barak 				MLX4_ZONE_FALLBACK_TO_HIGHER_PRIO |
585d57febe1SMatan Barak 				MLX4_ZONE_USE_RR, 0,
586d57febe1SMatan Barak 				0, qp_table->zones_uids + MLX4_QP_TABLE_ZONE_GENERAL);
587d57febe1SMatan Barak 
588d57febe1SMatan Barak 	if (err)
589d57febe1SMatan Barak 		goto free_bitmap;
590d57febe1SMatan Barak 
591d57febe1SMatan Barak 	err = mlx4_bitmap_init(*bitmap + MLX4_QP_TABLE_ZONE_RSS,
592d57febe1SMatan Barak 			       reserved_bottom_rss,
593d57febe1SMatan Barak 			       reserved_bottom_rss - 1,
594d57febe1SMatan Barak 			       dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
595d57febe1SMatan Barak 			       reserved_bottom_rss - start_offset_rss);
596d57febe1SMatan Barak 
597d57febe1SMatan Barak 	if (err)
598d57febe1SMatan Barak 		goto free_bitmap;
599d57febe1SMatan Barak 
600d57febe1SMatan Barak 	++bitmap_initialized;
601d57febe1SMatan Barak 
602d57febe1SMatan Barak 	err = mlx4_zone_add_one(qp_table->zones, *bitmap + MLX4_QP_TABLE_ZONE_RSS,
603d57febe1SMatan Barak 				MLX4_ZONE_ALLOW_ALLOC_FROM_LOWER_PRIO |
604d57febe1SMatan Barak 				MLX4_ZONE_ALLOW_ALLOC_FROM_EQ_PRIO |
605d57febe1SMatan Barak 				MLX4_ZONE_USE_RR, MLX4_QP_TABLE_RSS_ETH_PRIORITY,
606d57febe1SMatan Barak 				0, qp_table->zones_uids + MLX4_QP_TABLE_ZONE_RSS);
607d57febe1SMatan Barak 
608d57febe1SMatan Barak 	if (err)
609d57febe1SMatan Barak 		goto free_bitmap;
610d57febe1SMatan Barak 
611d57febe1SMatan Barak 	last_offset = dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW];
612d57febe1SMatan Barak 	/*  We have a single zone for the A0 steering QPs area of the FW. This area
613d57febe1SMatan Barak 	 *  needs to be split into subareas. One set of subareas is for RSS QPs
614d57febe1SMatan Barak 	 *  (in which qp number bits 6 and/or 7 are set); the other set of subareas
615d57febe1SMatan Barak 	 *  is for RAW_ETH QPs, which require that both bits 6 and 7 are zero.
616d57febe1SMatan Barak 	 *  Currently, the values returned by the FW (A0 steering area starting qp number
617d57febe1SMatan Barak 	 *  and A0 steering area size) are such that there are only two subareas -- one
618d57febe1SMatan Barak 	 *  for RSS and one for RAW_ETH.
619d57febe1SMatan Barak 	 */
620d57febe1SMatan Barak 	for (k = MLX4_QP_TABLE_ZONE_RSS + 1; k < sizeof(*bitmap)/sizeof((*bitmap)[0]);
621d57febe1SMatan Barak 	     k++) {
622d57febe1SMatan Barak 		int size;
623d57febe1SMatan Barak 		u32 offset = start_offset_rss;
624d57febe1SMatan Barak 		u32 bf_mask;
625d57febe1SMatan Barak 		u32 requested_size;
626d57febe1SMatan Barak 
627d57febe1SMatan Barak 		/* Assuming MLX4_BF_QP_SKIP_MASK is consecutive ones, this calculates
628d57febe1SMatan Barak 		 * a mask of all LSB bits set until (and not including) the first
629d57febe1SMatan Barak 		 * set bit of  MLX4_BF_QP_SKIP_MASK. For example, if MLX4_BF_QP_SKIP_MASK
630d57febe1SMatan Barak 		 * is 0xc0, bf_mask will be 0x3f.
631d57febe1SMatan Barak 		 */
632d57febe1SMatan Barak 		bf_mask = (MLX4_BF_QP_SKIP_MASK & ~(MLX4_BF_QP_SKIP_MASK - 1)) - 1;
633d57febe1SMatan Barak 		requested_size = min((u32)MLX4_QP_TABLE_RAW_ETH_SIZE, bf_mask + 1);
634d57febe1SMatan Barak 
635d57febe1SMatan Barak 		if (((last_offset & MLX4_BF_QP_SKIP_MASK) &&
636d57febe1SMatan Barak 		     ((int)(max_table_offset - last_offset)) >=
637d57febe1SMatan Barak 		     roundup_pow_of_two(MLX4_BF_QP_SKIP_MASK)) ||
638d57febe1SMatan Barak 		    (!(last_offset & MLX4_BF_QP_SKIP_MASK) &&
639d57febe1SMatan Barak 		     !((last_offset + requested_size - 1) &
640d57febe1SMatan Barak 		       MLX4_BF_QP_SKIP_MASK)))
641d57febe1SMatan Barak 			size = requested_size;
642d57febe1SMatan Barak 		else {
643d57febe1SMatan Barak 			u32 candidate_offset =
644d57febe1SMatan Barak 				(last_offset | MLX4_BF_QP_SKIP_MASK | bf_mask) + 1;
645d57febe1SMatan Barak 
646d57febe1SMatan Barak 			if (last_offset & MLX4_BF_QP_SKIP_MASK)
647d57febe1SMatan Barak 				last_offset = candidate_offset;
648d57febe1SMatan Barak 
649d57febe1SMatan Barak 			/* From this point, the BF bits are 0 */
650d57febe1SMatan Barak 
651d57febe1SMatan Barak 			if (last_offset > max_table_offset) {
652d57febe1SMatan Barak 				/* need to skip */
653d57febe1SMatan Barak 				size = -1;
654d57febe1SMatan Barak 			} else {
655d57febe1SMatan Barak 				size = min3(max_table_offset - last_offset,
656d57febe1SMatan Barak 					    bf_mask - (last_offset & bf_mask),
657d57febe1SMatan Barak 					    requested_size);
658d57febe1SMatan Barak 				if (size < requested_size) {
659d57febe1SMatan Barak 					int candidate_size;
660d57febe1SMatan Barak 
661d57febe1SMatan Barak 					candidate_size = min3(
662d57febe1SMatan Barak 						max_table_offset - candidate_offset,
663d57febe1SMatan Barak 						bf_mask - (last_offset & bf_mask),
664d57febe1SMatan Barak 						requested_size);
665d57febe1SMatan Barak 
666d57febe1SMatan Barak 					/*  We will not take this path if last_offset was
667d57febe1SMatan Barak 					 *  already set above to candidate_offset
668d57febe1SMatan Barak 					 */
669d57febe1SMatan Barak 					if (candidate_size > size) {
670d57febe1SMatan Barak 						last_offset = candidate_offset;
671d57febe1SMatan Barak 						size = candidate_size;
672d57febe1SMatan Barak 					}
673d57febe1SMatan Barak 				}
674d57febe1SMatan Barak 			}
675d57febe1SMatan Barak 		}
676d57febe1SMatan Barak 
677d57febe1SMatan Barak 		if (size > 0) {
678d57febe1SMatan Barak 			/* mlx4_bitmap_alloc_range will find a contiguous range of "size"
679d57febe1SMatan Barak 			 * QPs in which both bits 6 and 7 are zero, because we pass it the
680d57febe1SMatan Barak 			 * MLX4_BF_SKIP_MASK).
681d57febe1SMatan Barak 			 */
682d57febe1SMatan Barak 			offset = mlx4_bitmap_alloc_range(
683d57febe1SMatan Barak 					*bitmap + MLX4_QP_TABLE_ZONE_RSS,
684d57febe1SMatan Barak 					size, 1,
685d57febe1SMatan Barak 					MLX4_BF_QP_SKIP_MASK);
686d57febe1SMatan Barak 
687d57febe1SMatan Barak 			if (offset == (u32)-1) {
688d57febe1SMatan Barak 				err = -ENOMEM;
689d57febe1SMatan Barak 				break;
690d57febe1SMatan Barak 			}
691d57febe1SMatan Barak 
692d57febe1SMatan Barak 			last_offset = offset + size;
693d57febe1SMatan Barak 
694d57febe1SMatan Barak 			err = mlx4_bitmap_init(*bitmap + k, roundup_pow_of_two(size),
695d57febe1SMatan Barak 					       roundup_pow_of_two(size) - 1, 0,
696d57febe1SMatan Barak 					       roundup_pow_of_two(size) - size);
697d57febe1SMatan Barak 		} else {
698d57febe1SMatan Barak 			/* Add an empty bitmap, we'll allocate from different zones (since
699d57febe1SMatan Barak 			 * at least one is reserved)
700d57febe1SMatan Barak 			 */
701d57febe1SMatan Barak 			err = mlx4_bitmap_init(*bitmap + k, 1,
702d57febe1SMatan Barak 					       MLX4_QP_TABLE_RAW_ETH_SIZE - 1, 0,
703d57febe1SMatan Barak 					       0);
704594c61ffSPeter Kosyh 			if (!err)
705d57febe1SMatan Barak 				mlx4_bitmap_alloc_range(*bitmap + k, 1, 1, 0);
706d57febe1SMatan Barak 		}
707d57febe1SMatan Barak 
708d57febe1SMatan Barak 		if (err)
709d57febe1SMatan Barak 			break;
710d57febe1SMatan Barak 
711d57febe1SMatan Barak 		++bitmap_initialized;
712d57febe1SMatan Barak 
713d57febe1SMatan Barak 		err = mlx4_zone_add_one(qp_table->zones, *bitmap + k,
714d57febe1SMatan Barak 					MLX4_ZONE_ALLOW_ALLOC_FROM_LOWER_PRIO |
715d57febe1SMatan Barak 					MLX4_ZONE_ALLOW_ALLOC_FROM_EQ_PRIO |
716d57febe1SMatan Barak 					MLX4_ZONE_USE_RR, MLX4_QP_TABLE_RAW_ETH_PRIORITY,
717d57febe1SMatan Barak 					offset, qp_table->zones_uids + k);
718d57febe1SMatan Barak 
719d57febe1SMatan Barak 		if (err)
720d57febe1SMatan Barak 			break;
721d57febe1SMatan Barak 	}
722d57febe1SMatan Barak 
723d57febe1SMatan Barak 	if (err)
724d57febe1SMatan Barak 		goto free_bitmap;
725d57febe1SMatan Barak 
726d57febe1SMatan Barak 	qp_table->bitmap_gen = *bitmap;
727d57febe1SMatan Barak 
728d57febe1SMatan Barak 	return err;
729d57febe1SMatan Barak 
730d57febe1SMatan Barak free_bitmap:
731d57febe1SMatan Barak 	for (k = 0; k < bitmap_initialized; k++)
732d57febe1SMatan Barak 		mlx4_bitmap_cleanup(*bitmap + k);
733d57febe1SMatan Barak 	kfree(bitmap);
734d57febe1SMatan Barak free_zone:
735d57febe1SMatan Barak 	mlx4_zone_allocator_destroy(qp_table->zones);
736d57febe1SMatan Barak 	return err;
737d57febe1SMatan Barak }
738d57febe1SMatan Barak 
mlx4_cleanup_qp_zones(struct mlx4_dev * dev)739d57febe1SMatan Barak static void mlx4_cleanup_qp_zones(struct mlx4_dev *dev)
740d57febe1SMatan Barak {
741d57febe1SMatan Barak 	struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
742d57febe1SMatan Barak 
743d57febe1SMatan Barak 	if (qp_table->zones) {
744d57febe1SMatan Barak 		int i;
745d57febe1SMatan Barak 
746d57febe1SMatan Barak 		for (i = 0;
74719b8ece4SJason Wang 		     i < ARRAY_SIZE(qp_table->zones_uids);
748d57febe1SMatan Barak 		     i++) {
749d57febe1SMatan Barak 			struct mlx4_bitmap *bitmap =
750d57febe1SMatan Barak 				mlx4_zone_get_bitmap(qp_table->zones,
751d57febe1SMatan Barak 						     qp_table->zones_uids[i]);
752d57febe1SMatan Barak 
753d57febe1SMatan Barak 			mlx4_zone_remove_one(qp_table->zones, qp_table->zones_uids[i]);
754d57febe1SMatan Barak 			if (NULL == bitmap)
755d57febe1SMatan Barak 				continue;
756d57febe1SMatan Barak 
757d57febe1SMatan Barak 			mlx4_bitmap_cleanup(bitmap);
758d57febe1SMatan Barak 		}
759d57febe1SMatan Barak 		mlx4_zone_allocator_destroy(qp_table->zones);
760d57febe1SMatan Barak 		kfree(qp_table->bitmap_gen);
761d57febe1SMatan Barak 		qp_table->bitmap_gen = NULL;
762d57febe1SMatan Barak 		qp_table->zones = NULL;
763d57febe1SMatan Barak 	}
764d57febe1SMatan Barak }
765d57febe1SMatan Barak 
mlx4_init_qp_table(struct mlx4_dev * dev)7665a2cc190SJeff Kirsher int mlx4_init_qp_table(struct mlx4_dev *dev)
7675a2cc190SJeff Kirsher {
7685a2cc190SJeff Kirsher 	struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
7695a2cc190SJeff Kirsher 	int err;
7705a2cc190SJeff Kirsher 	int reserved_from_top = 0;
771ab256e5aSDotan Barak 	int reserved_from_bot;
77247605df9SJack Morgenstein 	int k;
773d57febe1SMatan Barak 	int fixed_reserved_from_bot_rv = 0;
774d57febe1SMatan Barak 	int bottom_reserved_for_rss_bitmap;
7757d077cd3SMatan Barak 	u32 max_table_offset = dev->caps.dmfs_high_rate_qpn_base +
7767d077cd3SMatan Barak 			dev->caps.dmfs_high_rate_qpn_range;
7775a2cc190SJeff Kirsher 
7785a2cc190SJeff Kirsher 	spin_lock_init(&qp_table->lock);
7795a2cc190SJeff Kirsher 	INIT_RADIX_TREE(&dev->qp_table_tree, GFP_ATOMIC);
780fe9a2603SJack Morgenstein 	if (mlx4_is_slave(dev))
781fe9a2603SJack Morgenstein 		return 0;
7825a2cc190SJeff Kirsher 
783d57febe1SMatan Barak 	/* We reserve 2 extra QPs per port for the special QPs.  The
7845a2cc190SJeff Kirsher 	 * block of special QPs must be aligned to a multiple of 8, so
7855a2cc190SJeff Kirsher 	 * round up.
786f470f8d4SLinus Torvalds 	 *
787f470f8d4SLinus Torvalds 	 * We also reserve the MSB of the 24-bit QP number to indicate
788f470f8d4SLinus Torvalds 	 * that a QP is an XRC QP.
7895a2cc190SJeff Kirsher 	 */
790d57febe1SMatan Barak 	for (k = 0; k <= MLX4_QP_REGION_BOTTOM; k++)
791d57febe1SMatan Barak 		fixed_reserved_from_bot_rv += dev->caps.reserved_qps_cnt[k];
792d57febe1SMatan Barak 
793d57febe1SMatan Barak 	if (fixed_reserved_from_bot_rv < max_table_offset)
794d57febe1SMatan Barak 		fixed_reserved_from_bot_rv = max_table_offset;
795d57febe1SMatan Barak 
796d57febe1SMatan Barak 	/* We reserve at least 1 extra for bitmaps that we don't have enough space for*/
797d57febe1SMatan Barak 	bottom_reserved_for_rss_bitmap =
798d57febe1SMatan Barak 		roundup_pow_of_two(fixed_reserved_from_bot_rv + 1);
799d57febe1SMatan Barak 	dev->phys_caps.base_sqpn = ALIGN(bottom_reserved_for_rss_bitmap, 8);
8005a2cc190SJeff Kirsher 
8015a2cc190SJeff Kirsher 	{
8025a2cc190SJeff Kirsher 		int sort[MLX4_NUM_QP_REGION];
8032df1cafaSFabian Frederick 		int i, j;
8045a2cc190SJeff Kirsher 		int last_base = dev->caps.num_qps;
8055a2cc190SJeff Kirsher 
8065a2cc190SJeff Kirsher 		for (i = 1; i < MLX4_NUM_QP_REGION; ++i)
8075a2cc190SJeff Kirsher 			sort[i] = i;
8085a2cc190SJeff Kirsher 
809d57febe1SMatan Barak 		for (i = MLX4_NUM_QP_REGION; i > MLX4_QP_REGION_BOTTOM; --i) {
810d57febe1SMatan Barak 			for (j = MLX4_QP_REGION_BOTTOM + 2; j < i; ++j) {
8115a2cc190SJeff Kirsher 				if (dev->caps.reserved_qps_cnt[sort[j]] >
8122df1cafaSFabian Frederick 				    dev->caps.reserved_qps_cnt[sort[j - 1]])
8132df1cafaSFabian Frederick 					swap(sort[j], sort[j - 1]);
8145a2cc190SJeff Kirsher 			}
8155a2cc190SJeff Kirsher 		}
8165a2cc190SJeff Kirsher 
817d57febe1SMatan Barak 		for (i = MLX4_QP_REGION_BOTTOM + 1; i < MLX4_NUM_QP_REGION; ++i) {
8185a2cc190SJeff Kirsher 			last_base -= dev->caps.reserved_qps_cnt[sort[i]];
8195a2cc190SJeff Kirsher 			dev->caps.reserved_qps_base[sort[i]] = last_base;
8205a2cc190SJeff Kirsher 			reserved_from_top +=
8215a2cc190SJeff Kirsher 				dev->caps.reserved_qps_cnt[sort[i]];
8225a2cc190SJeff Kirsher 		}
8235a2cc190SJeff Kirsher 	}
8245a2cc190SJeff Kirsher 
825e2c76824SJack Morgenstein        /* Reserve 8 real SQPs in both native and SRIOV modes.
826e2c76824SJack Morgenstein 	* In addition, in SRIOV mode, reserve 8 proxy SQPs per function
827e2c76824SJack Morgenstein 	* (for all PFs and VFs), and 8 corresponding tunnel QPs.
828e2c76824SJack Morgenstein 	* Each proxy SQP works opposite its own tunnel QP.
829e2c76824SJack Morgenstein 	*
830e2c76824SJack Morgenstein 	* The QPs are arranged as follows:
831e2c76824SJack Morgenstein 	* a. 8 real SQPs
832e2c76824SJack Morgenstein 	* b. All the proxy SQPs (8 per function)
833e2c76824SJack Morgenstein 	* c. All the tunnel QPs (8 per function)
834e2c76824SJack Morgenstein 	*/
835ab256e5aSDotan Barak 	reserved_from_bot = mlx4_num_reserved_sqps(dev);
836ab256e5aSDotan Barak 	if (reserved_from_bot + reserved_from_top > dev->caps.num_qps) {
837ab256e5aSDotan Barak 		mlx4_err(dev, "Number of reserved QPs is higher than number of QPs\n");
838ab256e5aSDotan Barak 		return -EINVAL;
839ab256e5aSDotan Barak 	}
840e2c76824SJack Morgenstein 
841d57febe1SMatan Barak 	err = mlx4_create_zones(dev, reserved_from_bot, reserved_from_bot,
842d57febe1SMatan Barak 				bottom_reserved_for_rss_bitmap,
843d57febe1SMatan Barak 				fixed_reserved_from_bot_rv,
844d57febe1SMatan Barak 				max_table_offset);
845d57febe1SMatan Barak 
8465a2cc190SJeff Kirsher 	if (err)
8475a2cc190SJeff Kirsher 		return err;
8485a2cc190SJeff Kirsher 
84947605df9SJack Morgenstein 	if (mlx4_is_mfunc(dev)) {
85047605df9SJack Morgenstein 		/* for PPF use */
85147605df9SJack Morgenstein 		dev->phys_caps.base_proxy_sqpn = dev->phys_caps.base_sqpn + 8;
85247605df9SJack Morgenstein 		dev->phys_caps.base_tunnel_sqpn = dev->phys_caps.base_sqpn + 8 + 8 * MLX4_MFUNC_MAX;
85347605df9SJack Morgenstein 
85447605df9SJack Morgenstein 		/* In mfunc, calculate proxy and tunnel qp offsets for the PF here,
85547605df9SJack Morgenstein 		 * since the PF does not call mlx4_slave_caps */
856942e7e5fSColin Ian King 		dev->caps.spec_qps = kcalloc(dev->caps.num_ports,
857942e7e5fSColin Ian King 					     sizeof(*dev->caps.spec_qps),
858942e7e5fSColin Ian King 					     GFP_KERNEL);
859c73c8b1eSEran Ben Elisha 		if (!dev->caps.spec_qps) {
86047605df9SJack Morgenstein 			err = -ENOMEM;
86147605df9SJack Morgenstein 			goto err_mem;
86247605df9SJack Morgenstein 		}
86347605df9SJack Morgenstein 
86447605df9SJack Morgenstein 		for (k = 0; k < dev->caps.num_ports; k++) {
865c73c8b1eSEran Ben Elisha 			dev->caps.spec_qps[k].qp0_proxy = dev->phys_caps.base_proxy_sqpn +
86647605df9SJack Morgenstein 				8 * mlx4_master_func_num(dev) + k;
867c73c8b1eSEran Ben Elisha 			dev->caps.spec_qps[k].qp0_tunnel = dev->caps.spec_qps[k].qp0_proxy + 8 * MLX4_MFUNC_MAX;
868c73c8b1eSEran Ben Elisha 			dev->caps.spec_qps[k].qp1_proxy = dev->phys_caps.base_proxy_sqpn +
86947605df9SJack Morgenstein 				8 * mlx4_master_func_num(dev) + MLX4_MAX_PORTS + k;
870c73c8b1eSEran Ben Elisha 			dev->caps.spec_qps[k].qp1_tunnel = dev->caps.spec_qps[k].qp1_proxy + 8 * MLX4_MFUNC_MAX;
87147605df9SJack Morgenstein 		}
87247605df9SJack Morgenstein 	}
87347605df9SJack Morgenstein 
87447605df9SJack Morgenstein 
87547605df9SJack Morgenstein 	err = mlx4_CONF_SPECIAL_QP(dev, dev->phys_caps.base_sqpn);
87647605df9SJack Morgenstein 	if (err)
87747605df9SJack Morgenstein 		goto err_mem;
878d57febe1SMatan Barak 
879d57febe1SMatan Barak 	return err;
88047605df9SJack Morgenstein 
88147605df9SJack Morgenstein err_mem:
882c73c8b1eSEran Ben Elisha 	kfree(dev->caps.spec_qps);
883c73c8b1eSEran Ben Elisha 	dev->caps.spec_qps = NULL;
884d57febe1SMatan Barak 	mlx4_cleanup_qp_zones(dev);
88547605df9SJack Morgenstein 	return err;
8865a2cc190SJeff Kirsher }
8875a2cc190SJeff Kirsher 
mlx4_cleanup_qp_table(struct mlx4_dev * dev)8885a2cc190SJeff Kirsher void mlx4_cleanup_qp_table(struct mlx4_dev *dev)
8895a2cc190SJeff Kirsher {
890fe9a2603SJack Morgenstein 	if (mlx4_is_slave(dev))
891fe9a2603SJack Morgenstein 		return;
892fe9a2603SJack Morgenstein 
8935a2cc190SJeff Kirsher 	mlx4_CONF_SPECIAL_QP(dev, 0);
894d57febe1SMatan Barak 
895d57febe1SMatan Barak 	mlx4_cleanup_qp_zones(dev);
8965a2cc190SJeff Kirsher }
8975a2cc190SJeff Kirsher 
mlx4_qp_query(struct mlx4_dev * dev,struct mlx4_qp * qp,struct mlx4_qp_context * context)8985a2cc190SJeff Kirsher int mlx4_qp_query(struct mlx4_dev *dev, struct mlx4_qp *qp,
8995a2cc190SJeff Kirsher 		  struct mlx4_qp_context *context)
9005a2cc190SJeff Kirsher {
9015a2cc190SJeff Kirsher 	struct mlx4_cmd_mailbox *mailbox;
9025a2cc190SJeff Kirsher 	int err;
9035a2cc190SJeff Kirsher 
9045a2cc190SJeff Kirsher 	mailbox = mlx4_alloc_cmd_mailbox(dev);
9055a2cc190SJeff Kirsher 	if (IS_ERR(mailbox))
9065a2cc190SJeff Kirsher 		return PTR_ERR(mailbox);
9075a2cc190SJeff Kirsher 
9085a2cc190SJeff Kirsher 	err = mlx4_cmd_box(dev, 0, mailbox->dma, qp->qpn, 0,
909f9baff50SJack Morgenstein 			   MLX4_CMD_QUERY_QP, MLX4_CMD_TIME_CLASS_A,
910f9baff50SJack Morgenstein 			   MLX4_CMD_WRAPPED);
9115a2cc190SJeff Kirsher 	if (!err)
91231975e27Sstephen hemminger 		memcpy(context, mailbox->buf + 8, sizeof(*context));
9135a2cc190SJeff Kirsher 
9145a2cc190SJeff Kirsher 	mlx4_free_cmd_mailbox(dev, mailbox);
9155a2cc190SJeff Kirsher 	return err;
9165a2cc190SJeff Kirsher }
9175a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_qp_query);
9185a2cc190SJeff Kirsher 
mlx4_qp_to_ready(struct mlx4_dev * dev,struct mlx4_mtt * mtt,struct mlx4_qp_context * context,struct mlx4_qp * qp,enum mlx4_qp_state * qp_state)9195a2cc190SJeff Kirsher int mlx4_qp_to_ready(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
9205a2cc190SJeff Kirsher 		     struct mlx4_qp_context *context,
9215a2cc190SJeff Kirsher 		     struct mlx4_qp *qp, enum mlx4_qp_state *qp_state)
9225a2cc190SJeff Kirsher {
9235a2cc190SJeff Kirsher 	int err;
9245a2cc190SJeff Kirsher 	int i;
9257cdd0a89SColin Ian King 	static const enum mlx4_qp_state states[] = {
9265a2cc190SJeff Kirsher 		MLX4_QP_STATE_RST,
9275a2cc190SJeff Kirsher 		MLX4_QP_STATE_INIT,
9285a2cc190SJeff Kirsher 		MLX4_QP_STATE_RTR,
9295a2cc190SJeff Kirsher 		MLX4_QP_STATE_RTS
9305a2cc190SJeff Kirsher 	};
9315a2cc190SJeff Kirsher 
9325a2cc190SJeff Kirsher 	for (i = 0; i < ARRAY_SIZE(states) - 1; i++) {
9335a2cc190SJeff Kirsher 		context->flags &= cpu_to_be32(~(0xf << 28));
9345a2cc190SJeff Kirsher 		context->flags |= cpu_to_be32(states[i + 1] << 28);
93553f33ae2SMoni Shoua 		if (states[i + 1] != MLX4_QP_STATE_RTR)
936bb428a5cSTariq Toukan 			context->params2 &= ~cpu_to_be32(MLX4_QP_BIT_FPP);
9375a2cc190SJeff Kirsher 		err = mlx4_qp_modify(dev, mtt, states[i], states[i + 1],
9385a2cc190SJeff Kirsher 				     context, 0, 0, qp);
9395a2cc190SJeff Kirsher 		if (err) {
9401a91de28SJoe Perches 			mlx4_err(dev, "Failed to bring QP to state: %d with error: %d\n",
9415a2cc190SJeff Kirsher 				 states[i + 1], err);
9425a2cc190SJeff Kirsher 			return err;
9435a2cc190SJeff Kirsher 		}
9445a2cc190SJeff Kirsher 
9455a2cc190SJeff Kirsher 		*qp_state = states[i + 1];
9465a2cc190SJeff Kirsher 	}
9475a2cc190SJeff Kirsher 
9485a2cc190SJeff Kirsher 	return 0;
9495a2cc190SJeff Kirsher }
9505a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_qp_to_ready);
9513f723f42SMoni Shoua 
mlx4_qp_roce_entropy(struct mlx4_dev * dev,u32 qpn)9523f723f42SMoni Shoua u16 mlx4_qp_roce_entropy(struct mlx4_dev *dev, u32 qpn)
9533f723f42SMoni Shoua {
9543f723f42SMoni Shoua 	struct mlx4_qp_context context;
9553f723f42SMoni Shoua 	struct mlx4_qp qp;
9563f723f42SMoni Shoua 	int err;
9573f723f42SMoni Shoua 
9583f723f42SMoni Shoua 	qp.qpn = qpn;
9593f723f42SMoni Shoua 	err = mlx4_qp_query(dev, &qp, &context);
9603f723f42SMoni Shoua 	if (!err) {
9613f723f42SMoni Shoua 		u32 dest_qpn = be32_to_cpu(context.remote_qpn) & 0xffffff;
9623f723f42SMoni Shoua 		u16 folded_dst = folded_qp(dest_qpn);
9633f723f42SMoni Shoua 		u16 folded_src = folded_qp(qpn);
9643f723f42SMoni Shoua 
9653f723f42SMoni Shoua 		return (dest_qpn != qpn) ?
9663f723f42SMoni Shoua 			((folded_dst ^ folded_src) | 0xC000) :
9673f723f42SMoni Shoua 			folded_src | 0xC000;
9683f723f42SMoni Shoua 	}
9693f723f42SMoni Shoua 	return 0xdead;
9703f723f42SMoni Shoua }
971