xref: /openbmc/linux/drivers/net/ethernet/mellanox/mlx4/cmd.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
15a2cc190SJeff Kirsher /*
25a2cc190SJeff Kirsher  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
35a2cc190SJeff Kirsher  * Copyright (c) 2005, 2006, 2007, 2008 Mellanox Technologies. All rights reserved.
45a2cc190SJeff Kirsher  * Copyright (c) 2005, 2006, 2007 Cisco Systems, Inc.  All rights reserved.
55a2cc190SJeff Kirsher  *
65a2cc190SJeff Kirsher  * This software is available to you under a choice of one of two
75a2cc190SJeff Kirsher  * licenses.  You may choose to be licensed under the terms of the GNU
85a2cc190SJeff Kirsher  * General Public License (GPL) Version 2, available from the file
95a2cc190SJeff Kirsher  * COPYING in the main directory of this source tree, or the
105a2cc190SJeff Kirsher  * OpenIB.org BSD license below:
115a2cc190SJeff Kirsher  *
125a2cc190SJeff Kirsher  *     Redistribution and use in source and binary forms, with or
135a2cc190SJeff Kirsher  *     without modification, are permitted provided that the following
145a2cc190SJeff Kirsher  *     conditions are met:
155a2cc190SJeff Kirsher  *
165a2cc190SJeff Kirsher  *      - Redistributions of source code must retain the above
175a2cc190SJeff Kirsher  *        copyright notice, this list of conditions and the following
185a2cc190SJeff Kirsher  *        disclaimer.
195a2cc190SJeff Kirsher  *
205a2cc190SJeff Kirsher  *      - Redistributions in binary form must reproduce the above
215a2cc190SJeff Kirsher  *        copyright notice, this list of conditions and the following
225a2cc190SJeff Kirsher  *        disclaimer in the documentation and/or other materials
235a2cc190SJeff Kirsher  *        provided with the distribution.
245a2cc190SJeff Kirsher  *
255a2cc190SJeff Kirsher  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
265a2cc190SJeff Kirsher  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
275a2cc190SJeff Kirsher  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
285a2cc190SJeff Kirsher  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
295a2cc190SJeff Kirsher  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
305a2cc190SJeff Kirsher  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
315a2cc190SJeff Kirsher  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
325a2cc190SJeff Kirsher  * SOFTWARE.
335a2cc190SJeff Kirsher  */
345a2cc190SJeff Kirsher 
355a2cc190SJeff Kirsher #include <linux/sched.h>
365a2cc190SJeff Kirsher #include <linux/slab.h>
37ee40fa06SPaul Gortmaker #include <linux/export.h>
385a2cc190SJeff Kirsher #include <linux/pci.h>
395a2cc190SJeff Kirsher #include <linux/errno.h>
405a2cc190SJeff Kirsher 
415a2cc190SJeff Kirsher #include <linux/mlx4/cmd.h>
42948e306dSRony Efraim #include <linux/mlx4/device.h>
43e8f081aaSYevgeny Petrilin #include <linux/semaphore.h>
440a9a0188SJack Morgenstein #include <rdma/ib_smi.h>
4555ad3592SYishai Hadas #include <linux/delay.h>
46745d8ae4SEugenia Emantayev #include <linux/etherdevice.h>
475a2cc190SJeff Kirsher 
485a2cc190SJeff Kirsher #include <asm/io.h>
495a2cc190SJeff Kirsher 
505a2cc190SJeff Kirsher #include "mlx4.h"
51e8f081aaSYevgeny Petrilin #include "fw.h"
5208068cd5SIdo Shamay #include "fw_qos.h"
539616982fSEran Ben Elisha #include "mlx4_stats.h"
545a2cc190SJeff Kirsher 
555a2cc190SJeff Kirsher #define CMD_POLL_TOKEN 0xffff
56e8f081aaSYevgeny Petrilin #define INBOX_MASK	0xffffffffffffff00ULL
57e8f081aaSYevgeny Petrilin 
58e8f081aaSYevgeny Petrilin #define CMD_CHAN_VER 1
59e8f081aaSYevgeny Petrilin #define CMD_CHAN_IF_REV 1
605a2cc190SJeff Kirsher 
615a2cc190SJeff Kirsher enum {
625a2cc190SJeff Kirsher 	/* command completed successfully: */
635a2cc190SJeff Kirsher 	CMD_STAT_OK		= 0x00,
645a2cc190SJeff Kirsher 	/* Internal error (such as a bus error) occurred while processing command: */
655a2cc190SJeff Kirsher 	CMD_STAT_INTERNAL_ERR	= 0x01,
665a2cc190SJeff Kirsher 	/* Operation/command not supported or opcode modifier not supported: */
675a2cc190SJeff Kirsher 	CMD_STAT_BAD_OP		= 0x02,
685a2cc190SJeff Kirsher 	/* Parameter not supported or parameter out of range: */
695a2cc190SJeff Kirsher 	CMD_STAT_BAD_PARAM	= 0x03,
705a2cc190SJeff Kirsher 	/* System not enabled or bad system state: */
715a2cc190SJeff Kirsher 	CMD_STAT_BAD_SYS_STATE	= 0x04,
725a2cc190SJeff Kirsher 	/* Attempt to access reserved or unallocaterd resource: */
735a2cc190SJeff Kirsher 	CMD_STAT_BAD_RESOURCE	= 0x05,
745a2cc190SJeff Kirsher 	/* Requested resource is currently executing a command, or is otherwise busy: */
755a2cc190SJeff Kirsher 	CMD_STAT_RESOURCE_BUSY	= 0x06,
765a2cc190SJeff Kirsher 	/* Required capability exceeds device limits: */
775a2cc190SJeff Kirsher 	CMD_STAT_EXCEED_LIM	= 0x08,
785a2cc190SJeff Kirsher 	/* Resource is not in the appropriate state or ownership: */
795a2cc190SJeff Kirsher 	CMD_STAT_BAD_RES_STATE	= 0x09,
805a2cc190SJeff Kirsher 	/* Index out of range: */
815a2cc190SJeff Kirsher 	CMD_STAT_BAD_INDEX	= 0x0a,
825a2cc190SJeff Kirsher 	/* FW image corrupted: */
835a2cc190SJeff Kirsher 	CMD_STAT_BAD_NVMEM	= 0x0b,
845a2cc190SJeff Kirsher 	/* Error in ICM mapping (e.g. not enough auxiliary ICM pages to execute command): */
855a2cc190SJeff Kirsher 	CMD_STAT_ICM_ERROR	= 0x0c,
865a2cc190SJeff Kirsher 	/* Attempt to modify a QP/EE which is not in the presumed state: */
875a2cc190SJeff Kirsher 	CMD_STAT_BAD_QP_STATE   = 0x10,
885a2cc190SJeff Kirsher 	/* Bad segment parameters (Address/Size): */
895a2cc190SJeff Kirsher 	CMD_STAT_BAD_SEG_PARAM	= 0x20,
905a2cc190SJeff Kirsher 	/* Memory Region has Memory Windows bound to: */
915a2cc190SJeff Kirsher 	CMD_STAT_REG_BOUND	= 0x21,
925a2cc190SJeff Kirsher 	/* HCA local attached memory not present: */
935a2cc190SJeff Kirsher 	CMD_STAT_LAM_NOT_PRE	= 0x22,
945a2cc190SJeff Kirsher 	/* Bad management packet (silently discarded): */
955a2cc190SJeff Kirsher 	CMD_STAT_BAD_PKT	= 0x30,
965a2cc190SJeff Kirsher 	/* More outstanding CQEs in CQ than new CQ size: */
975a2cc190SJeff Kirsher 	CMD_STAT_BAD_SIZE	= 0x40,
985a2cc190SJeff Kirsher 	/* Multi Function device support required: */
995a2cc190SJeff Kirsher 	CMD_STAT_MULTI_FUNC_REQ	= 0x50,
1005a2cc190SJeff Kirsher };
1015a2cc190SJeff Kirsher 
1025a2cc190SJeff Kirsher enum {
1035a2cc190SJeff Kirsher 	HCR_IN_PARAM_OFFSET	= 0x00,
1045a2cc190SJeff Kirsher 	HCR_IN_MODIFIER_OFFSET	= 0x08,
1055a2cc190SJeff Kirsher 	HCR_OUT_PARAM_OFFSET	= 0x0c,
1065a2cc190SJeff Kirsher 	HCR_TOKEN_OFFSET	= 0x14,
1075a2cc190SJeff Kirsher 	HCR_STATUS_OFFSET	= 0x18,
1085a2cc190SJeff Kirsher 
1095a2cc190SJeff Kirsher 	HCR_OPMOD_SHIFT		= 12,
1105a2cc190SJeff Kirsher 	HCR_T_BIT		= 21,
1115a2cc190SJeff Kirsher 	HCR_E_BIT		= 22,
1125a2cc190SJeff Kirsher 	HCR_GO_BIT		= 23
1135a2cc190SJeff Kirsher };
1145a2cc190SJeff Kirsher 
1155a2cc190SJeff Kirsher enum {
1165a2cc190SJeff Kirsher 	GO_BIT_TIMEOUT_MSECS	= 10000
1175a2cc190SJeff Kirsher };
1185a2cc190SJeff Kirsher 
119b01978caSJack Morgenstein enum mlx4_vlan_transition {
120b01978caSJack Morgenstein 	MLX4_VLAN_TRANSITION_VST_VST = 0,
121b01978caSJack Morgenstein 	MLX4_VLAN_TRANSITION_VST_VGT = 1,
122b01978caSJack Morgenstein 	MLX4_VLAN_TRANSITION_VGT_VST = 2,
123b01978caSJack Morgenstein 	MLX4_VLAN_TRANSITION_VGT_VGT = 3,
124b01978caSJack Morgenstein };
125b01978caSJack Morgenstein 
126b01978caSJack Morgenstein 
1275a2cc190SJeff Kirsher struct mlx4_cmd_context {
1285a2cc190SJeff Kirsher 	struct completion	done;
1295a2cc190SJeff Kirsher 	int			result;
1305a2cc190SJeff Kirsher 	int			next;
1315a2cc190SJeff Kirsher 	u64			out_param;
1325a2cc190SJeff Kirsher 	u16			token;
133e8f081aaSYevgeny Petrilin 	u8			fw_status;
1345a2cc190SJeff Kirsher };
1355a2cc190SJeff Kirsher 
136e8f081aaSYevgeny Petrilin static int mlx4_master_process_vhcr(struct mlx4_dev *dev, int slave,
137e8f081aaSYevgeny Petrilin 				    struct mlx4_vhcr_cmd *in_vhcr);
138e8f081aaSYevgeny Petrilin 
mlx4_status_to_errno(u8 status)1395a2cc190SJeff Kirsher static int mlx4_status_to_errno(u8 status)
1405a2cc190SJeff Kirsher {
1415a2cc190SJeff Kirsher 	static const int trans_table[] = {
1425a2cc190SJeff Kirsher 		[CMD_STAT_INTERNAL_ERR]	  = -EIO,
1435a2cc190SJeff Kirsher 		[CMD_STAT_BAD_OP]	  = -EPERM,
1445a2cc190SJeff Kirsher 		[CMD_STAT_BAD_PARAM]	  = -EINVAL,
1455a2cc190SJeff Kirsher 		[CMD_STAT_BAD_SYS_STATE]  = -ENXIO,
1465a2cc190SJeff Kirsher 		[CMD_STAT_BAD_RESOURCE]	  = -EBADF,
1475a2cc190SJeff Kirsher 		[CMD_STAT_RESOURCE_BUSY]  = -EBUSY,
1485a2cc190SJeff Kirsher 		[CMD_STAT_EXCEED_LIM]	  = -ENOMEM,
1495a2cc190SJeff Kirsher 		[CMD_STAT_BAD_RES_STATE]  = -EBADF,
1505a2cc190SJeff Kirsher 		[CMD_STAT_BAD_INDEX]	  = -EBADF,
1515a2cc190SJeff Kirsher 		[CMD_STAT_BAD_NVMEM]	  = -EFAULT,
1525a2cc190SJeff Kirsher 		[CMD_STAT_ICM_ERROR]	  = -ENFILE,
1535a2cc190SJeff Kirsher 		[CMD_STAT_BAD_QP_STATE]   = -EINVAL,
1545a2cc190SJeff Kirsher 		[CMD_STAT_BAD_SEG_PARAM]  = -EFAULT,
1555a2cc190SJeff Kirsher 		[CMD_STAT_REG_BOUND]	  = -EBUSY,
1565a2cc190SJeff Kirsher 		[CMD_STAT_LAM_NOT_PRE]	  = -EAGAIN,
1575a2cc190SJeff Kirsher 		[CMD_STAT_BAD_PKT]	  = -EINVAL,
1585a2cc190SJeff Kirsher 		[CMD_STAT_BAD_SIZE]	  = -ENOMEM,
1595a2cc190SJeff Kirsher 		[CMD_STAT_MULTI_FUNC_REQ] = -EACCES,
1605a2cc190SJeff Kirsher 	};
1615a2cc190SJeff Kirsher 
1625a2cc190SJeff Kirsher 	if (status >= ARRAY_SIZE(trans_table) ||
1635a2cc190SJeff Kirsher 	    (status != CMD_STAT_OK && trans_table[status] == 0))
1645a2cc190SJeff Kirsher 		return -EIO;
1655a2cc190SJeff Kirsher 
1665a2cc190SJeff Kirsher 	return trans_table[status];
1675a2cc190SJeff Kirsher }
1685a2cc190SJeff Kirsher 
mlx4_errno_to_status(int errno)16972be84f1SYevgeny Petrilin static u8 mlx4_errno_to_status(int errno)
17072be84f1SYevgeny Petrilin {
17172be84f1SYevgeny Petrilin 	switch (errno) {
17272be84f1SYevgeny Petrilin 	case -EPERM:
17372be84f1SYevgeny Petrilin 		return CMD_STAT_BAD_OP;
17472be84f1SYevgeny Petrilin 	case -EINVAL:
17572be84f1SYevgeny Petrilin 		return CMD_STAT_BAD_PARAM;
17672be84f1SYevgeny Petrilin 	case -ENXIO:
17772be84f1SYevgeny Petrilin 		return CMD_STAT_BAD_SYS_STATE;
17872be84f1SYevgeny Petrilin 	case -EBUSY:
17972be84f1SYevgeny Petrilin 		return CMD_STAT_RESOURCE_BUSY;
18072be84f1SYevgeny Petrilin 	case -ENOMEM:
18172be84f1SYevgeny Petrilin 		return CMD_STAT_EXCEED_LIM;
18272be84f1SYevgeny Petrilin 	case -ENFILE:
18372be84f1SYevgeny Petrilin 		return CMD_STAT_ICM_ERROR;
18472be84f1SYevgeny Petrilin 	default:
18572be84f1SYevgeny Petrilin 		return CMD_STAT_INTERNAL_ERR;
18672be84f1SYevgeny Petrilin 	}
18772be84f1SYevgeny Petrilin }
18872be84f1SYevgeny Petrilin 
mlx4_internal_err_ret_value(struct mlx4_dev * dev,u16 op,u8 op_modifier)189f5aef5aaSYishai Hadas static int mlx4_internal_err_ret_value(struct mlx4_dev *dev, u16 op,
190f5aef5aaSYishai Hadas 				       u8 op_modifier)
191f5aef5aaSYishai Hadas {
192f5aef5aaSYishai Hadas 	switch (op) {
193f5aef5aaSYishai Hadas 	case MLX4_CMD_UNMAP_ICM:
194f5aef5aaSYishai Hadas 	case MLX4_CMD_UNMAP_ICM_AUX:
195f5aef5aaSYishai Hadas 	case MLX4_CMD_UNMAP_FA:
196f5aef5aaSYishai Hadas 	case MLX4_CMD_2RST_QP:
197f5aef5aaSYishai Hadas 	case MLX4_CMD_HW2SW_EQ:
198f5aef5aaSYishai Hadas 	case MLX4_CMD_HW2SW_CQ:
199f5aef5aaSYishai Hadas 	case MLX4_CMD_HW2SW_SRQ:
200f5aef5aaSYishai Hadas 	case MLX4_CMD_HW2SW_MPT:
201f5aef5aaSYishai Hadas 	case MLX4_CMD_CLOSE_HCA:
202f5aef5aaSYishai Hadas 	case MLX4_QP_FLOW_STEERING_DETACH:
203f5aef5aaSYishai Hadas 	case MLX4_CMD_FREE_RES:
204f5aef5aaSYishai Hadas 	case MLX4_CMD_CLOSE_PORT:
205f5aef5aaSYishai Hadas 		return CMD_STAT_OK;
206f5aef5aaSYishai Hadas 
207f5aef5aaSYishai Hadas 	case MLX4_CMD_QP_ATTACH:
208f5aef5aaSYishai Hadas 		/* On Detach case return success */
209f5aef5aaSYishai Hadas 		if (op_modifier == 0)
210f5aef5aaSYishai Hadas 			return CMD_STAT_OK;
211f5aef5aaSYishai Hadas 		return mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
212f5aef5aaSYishai Hadas 
213f5aef5aaSYishai Hadas 	default:
214f5aef5aaSYishai Hadas 		return mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
215f5aef5aaSYishai Hadas 	}
216f5aef5aaSYishai Hadas }
217f5aef5aaSYishai Hadas 
mlx4_closing_cmd_fatal_error(u16 op,u8 fw_status)218f5aef5aaSYishai Hadas static int mlx4_closing_cmd_fatal_error(u16 op, u8 fw_status)
219f5aef5aaSYishai Hadas {
220f5aef5aaSYishai Hadas 	/* Any error during the closing commands below is considered fatal */
221f5aef5aaSYishai Hadas 	if (op == MLX4_CMD_CLOSE_HCA ||
222f5aef5aaSYishai Hadas 	    op == MLX4_CMD_HW2SW_EQ ||
223f5aef5aaSYishai Hadas 	    op == MLX4_CMD_HW2SW_CQ ||
224f5aef5aaSYishai Hadas 	    op == MLX4_CMD_2RST_QP ||
225f5aef5aaSYishai Hadas 	    op == MLX4_CMD_HW2SW_SRQ ||
226f5aef5aaSYishai Hadas 	    op == MLX4_CMD_SYNC_TPT ||
227f5aef5aaSYishai Hadas 	    op == MLX4_CMD_UNMAP_ICM ||
228f5aef5aaSYishai Hadas 	    op == MLX4_CMD_UNMAP_ICM_AUX ||
229f5aef5aaSYishai Hadas 	    op == MLX4_CMD_UNMAP_FA)
230f5aef5aaSYishai Hadas 		return 1;
231f5aef5aaSYishai Hadas 	/* Error on MLX4_CMD_HW2SW_MPT is fatal except when fw status equals
232f5aef5aaSYishai Hadas 	  * CMD_STAT_REG_BOUND.
233f5aef5aaSYishai Hadas 	  * This status indicates that memory region has memory windows bound to it
234f5aef5aaSYishai Hadas 	  * which may result from invalid user space usage and is not fatal.
235f5aef5aaSYishai Hadas 	  */
236f5aef5aaSYishai Hadas 	if (op == MLX4_CMD_HW2SW_MPT && fw_status != CMD_STAT_REG_BOUND)
237f5aef5aaSYishai Hadas 		return 1;
238f5aef5aaSYishai Hadas 	return 0;
239f5aef5aaSYishai Hadas }
240f5aef5aaSYishai Hadas 
mlx4_cmd_reset_flow(struct mlx4_dev * dev,u16 op,u8 op_modifier,int err)241f5aef5aaSYishai Hadas static int mlx4_cmd_reset_flow(struct mlx4_dev *dev, u16 op, u8 op_modifier,
242f5aef5aaSYishai Hadas 			       int err)
243f5aef5aaSYishai Hadas {
244f5aef5aaSYishai Hadas 	/* Only if reset flow is really active return code is based on
245f5aef5aaSYishai Hadas 	  * command, otherwise current error code is returned.
246f5aef5aaSYishai Hadas 	  */
247f5aef5aaSYishai Hadas 	if (mlx4_internal_err_reset) {
248f5aef5aaSYishai Hadas 		mlx4_enter_error_state(dev->persist);
249f5aef5aaSYishai Hadas 		err = mlx4_internal_err_ret_value(dev, op, op_modifier);
250f5aef5aaSYishai Hadas 	}
251f5aef5aaSYishai Hadas 
252f5aef5aaSYishai Hadas 	return err;
253f5aef5aaSYishai Hadas }
254f5aef5aaSYishai Hadas 
comm_pending(struct mlx4_dev * dev)255e8f081aaSYevgeny Petrilin static int comm_pending(struct mlx4_dev *dev)
256e8f081aaSYevgeny Petrilin {
257e8f081aaSYevgeny Petrilin 	struct mlx4_priv *priv = mlx4_priv(dev);
258e8f081aaSYevgeny Petrilin 	u32 status = readl(&priv->mfunc.comm->slave_read);
259e8f081aaSYevgeny Petrilin 
260e8f081aaSYevgeny Petrilin 	return (swab32(status) >> 31) != priv->cmd.comm_toggle;
261e8f081aaSYevgeny Petrilin }
262e8f081aaSYevgeny Petrilin 
mlx4_comm_cmd_post(struct mlx4_dev * dev,u8 cmd,u16 param)2630cd93027SYishai Hadas static int mlx4_comm_cmd_post(struct mlx4_dev *dev, u8 cmd, u16 param)
264e8f081aaSYevgeny Petrilin {
265e8f081aaSYevgeny Petrilin 	struct mlx4_priv *priv = mlx4_priv(dev);
266e8f081aaSYevgeny Petrilin 	u32 val;
267e8f081aaSYevgeny Petrilin 
2680cd93027SYishai Hadas 	/* To avoid writing to unknown addresses after the device state was
2690cd93027SYishai Hadas 	 * changed to internal error and the function was rest,
2700cd93027SYishai Hadas 	 * check the INTERNAL_ERROR flag which is updated under
2710cd93027SYishai Hadas 	 * device_state_mutex lock.
2720cd93027SYishai Hadas 	 */
2730cd93027SYishai Hadas 	mutex_lock(&dev->persist->device_state_mutex);
2740cd93027SYishai Hadas 
2750cd93027SYishai Hadas 	if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
2760cd93027SYishai Hadas 		mutex_unlock(&dev->persist->device_state_mutex);
2770cd93027SYishai Hadas 		return -EIO;
2780cd93027SYishai Hadas 	}
2790cd93027SYishai Hadas 
280e8f081aaSYevgeny Petrilin 	priv->cmd.comm_toggle ^= 1;
281e8f081aaSYevgeny Petrilin 	val = param | (cmd << 16) | (priv->cmd.comm_toggle << 31);
282e8f081aaSYevgeny Petrilin 	__raw_writel((__force u32) cpu_to_be32(val),
283e8f081aaSYevgeny Petrilin 		     &priv->mfunc.comm->slave_write);
2840cd93027SYishai Hadas 	mutex_unlock(&dev->persist->device_state_mutex);
2850cd93027SYishai Hadas 	return 0;
286e8f081aaSYevgeny Petrilin }
287e8f081aaSYevgeny Petrilin 
mlx4_comm_cmd_poll(struct mlx4_dev * dev,u8 cmd,u16 param,unsigned long timeout)288e8f081aaSYevgeny Petrilin static int mlx4_comm_cmd_poll(struct mlx4_dev *dev, u8 cmd, u16 param,
289e8f081aaSYevgeny Petrilin 		       unsigned long timeout)
290e8f081aaSYevgeny Petrilin {
291e8f081aaSYevgeny Petrilin 	struct mlx4_priv *priv = mlx4_priv(dev);
292e8f081aaSYevgeny Petrilin 	unsigned long end;
293e8f081aaSYevgeny Petrilin 	int err = 0;
294e8f081aaSYevgeny Petrilin 	int ret_from_pending = 0;
295e8f081aaSYevgeny Petrilin 
296e8f081aaSYevgeny Petrilin 	/* First, verify that the master reports correct status */
297e8f081aaSYevgeny Petrilin 	if (comm_pending(dev)) {
2981a91de28SJoe Perches 		mlx4_warn(dev, "Communication channel is not idle - my toggle is %d (cmd:0x%x)\n",
299e8f081aaSYevgeny Petrilin 			  priv->cmd.comm_toggle, cmd);
300e8f081aaSYevgeny Petrilin 		return -EAGAIN;
301e8f081aaSYevgeny Petrilin 	}
302e8f081aaSYevgeny Petrilin 
303e8f081aaSYevgeny Petrilin 	/* Write command */
304e8f081aaSYevgeny Petrilin 	down(&priv->cmd.poll_sem);
3050cd93027SYishai Hadas 	if (mlx4_comm_cmd_post(dev, cmd, param)) {
3060cd93027SYishai Hadas 		/* Only in case the device state is INTERNAL_ERROR,
3070cd93027SYishai Hadas 		 * mlx4_comm_cmd_post returns with an error
3080cd93027SYishai Hadas 		 */
3090cd93027SYishai Hadas 		err = mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
3100cd93027SYishai Hadas 		goto out;
3110cd93027SYishai Hadas 	}
312e8f081aaSYevgeny Petrilin 
313e8f081aaSYevgeny Petrilin 	end = msecs_to_jiffies(timeout) + jiffies;
314e8f081aaSYevgeny Petrilin 	while (comm_pending(dev) && time_before(jiffies, end))
315e8f081aaSYevgeny Petrilin 		cond_resched();
316e8f081aaSYevgeny Petrilin 	ret_from_pending = comm_pending(dev);
317e8f081aaSYevgeny Petrilin 	if (ret_from_pending) {
318e8f081aaSYevgeny Petrilin 		/* check if the slave is trying to boot in the middle of
319e8f081aaSYevgeny Petrilin 		 * FLR process. The only non-zero result in the RESET command
320e8f081aaSYevgeny Petrilin 		 * is MLX4_DELAY_RESET_SLAVE*/
321e8f081aaSYevgeny Petrilin 		if ((MLX4_COMM_CMD_RESET == cmd)) {
322e8f081aaSYevgeny Petrilin 			err = MLX4_DELAY_RESET_SLAVE;
3230cd93027SYishai Hadas 			goto out;
324e8f081aaSYevgeny Petrilin 		} else {
3250cd93027SYishai Hadas 			mlx4_warn(dev, "Communication channel command 0x%x timed out\n",
3260cd93027SYishai Hadas 				  cmd);
3270cd93027SYishai Hadas 			err = mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
328e8f081aaSYevgeny Petrilin 		}
329e8f081aaSYevgeny Petrilin 	}
330e8f081aaSYevgeny Petrilin 
3310cd93027SYishai Hadas 	if (err)
3320cd93027SYishai Hadas 		mlx4_enter_error_state(dev->persist);
3330cd93027SYishai Hadas out:
334e8f081aaSYevgeny Petrilin 	up(&priv->cmd.poll_sem);
335e8f081aaSYevgeny Petrilin 	return err;
336e8f081aaSYevgeny Petrilin }
337e8f081aaSYevgeny Petrilin 
mlx4_comm_cmd_wait(struct mlx4_dev * dev,u8 vhcr_cmd,u16 param,u16 op,unsigned long timeout)3380cd93027SYishai Hadas static int mlx4_comm_cmd_wait(struct mlx4_dev *dev, u8 vhcr_cmd,
3390cd93027SYishai Hadas 			      u16 param, u16 op, unsigned long timeout)
340e8f081aaSYevgeny Petrilin {
341e8f081aaSYevgeny Petrilin 	struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
342e8f081aaSYevgeny Petrilin 	struct mlx4_cmd_context *context;
34358a3de05SEugenia Emantayev 	unsigned long end;
344e8f081aaSYevgeny Petrilin 	int err = 0;
345e8f081aaSYevgeny Petrilin 
346e8f081aaSYevgeny Petrilin 	down(&cmd->event_sem);
347e8f081aaSYevgeny Petrilin 
348e8f081aaSYevgeny Petrilin 	spin_lock(&cmd->context_lock);
349e8f081aaSYevgeny Petrilin 	BUG_ON(cmd->free_head < 0);
350e8f081aaSYevgeny Petrilin 	context = &cmd->context[cmd->free_head];
351e8f081aaSYevgeny Petrilin 	context->token += cmd->token_mask + 1;
352e8f081aaSYevgeny Petrilin 	cmd->free_head = context->next;
353e8f081aaSYevgeny Petrilin 	spin_unlock(&cmd->context_lock);
354e8f081aaSYevgeny Petrilin 
355f5aef5aaSYishai Hadas 	reinit_completion(&context->done);
356e8f081aaSYevgeny Petrilin 
3570cd93027SYishai Hadas 	if (mlx4_comm_cmd_post(dev, vhcr_cmd, param)) {
3580cd93027SYishai Hadas 		/* Only in case the device state is INTERNAL_ERROR,
3590cd93027SYishai Hadas 		 * mlx4_comm_cmd_post returns with an error
3600cd93027SYishai Hadas 		 */
3610cd93027SYishai Hadas 		err = mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
3620cd93027SYishai Hadas 		goto out;
3630cd93027SYishai Hadas 	}
364e8f081aaSYevgeny Petrilin 
365e8f081aaSYevgeny Petrilin 	if (!wait_for_completion_timeout(&context->done,
366e8f081aaSYevgeny Petrilin 					 msecs_to_jiffies(timeout))) {
3670cd93027SYishai Hadas 		mlx4_warn(dev, "communication channel command 0x%x (op=0x%x) timed out\n",
3680cd93027SYishai Hadas 			  vhcr_cmd, op);
3690cd93027SYishai Hadas 		goto out_reset;
370e8f081aaSYevgeny Petrilin 	}
371e8f081aaSYevgeny Petrilin 
372e8f081aaSYevgeny Petrilin 	err = context->result;
373e8f081aaSYevgeny Petrilin 	if (err && context->fw_status != CMD_STAT_MULTI_FUNC_REQ) {
374e8f081aaSYevgeny Petrilin 		mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
3750cd93027SYishai Hadas 			 vhcr_cmd, context->fw_status);
3760cd93027SYishai Hadas 		if (mlx4_closing_cmd_fatal_error(op, context->fw_status))
3770cd93027SYishai Hadas 			goto out_reset;
378e8f081aaSYevgeny Petrilin 	}
379e8f081aaSYevgeny Petrilin 
38058a3de05SEugenia Emantayev 	/* wait for comm channel ready
38158a3de05SEugenia Emantayev 	 * this is necessary for prevention the race
38258a3de05SEugenia Emantayev 	 * when switching between event to polling mode
3830cd93027SYishai Hadas 	 * Skipping this section in case the device is in FATAL_ERROR state,
3840cd93027SYishai Hadas 	 * In this state, no commands are sent via the comm channel until
3850cd93027SYishai Hadas 	 * the device has returned from reset.
38658a3de05SEugenia Emantayev 	 */
3870cd93027SYishai Hadas 	if (!(dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)) {
38858a3de05SEugenia Emantayev 		end = msecs_to_jiffies(timeout) + jiffies;
38958a3de05SEugenia Emantayev 		while (comm_pending(dev) && time_before(jiffies, end))
39058a3de05SEugenia Emantayev 			cond_resched();
3910cd93027SYishai Hadas 	}
3920cd93027SYishai Hadas 	goto out;
39358a3de05SEugenia Emantayev 
3940cd93027SYishai Hadas out_reset:
3950cd93027SYishai Hadas 	err = mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
3960cd93027SYishai Hadas 	mlx4_enter_error_state(dev->persist);
3970cd93027SYishai Hadas out:
398e8f081aaSYevgeny Petrilin 	spin_lock(&cmd->context_lock);
399e8f081aaSYevgeny Petrilin 	context->next = cmd->free_head;
400e8f081aaSYevgeny Petrilin 	cmd->free_head = context - cmd->context;
401e8f081aaSYevgeny Petrilin 	spin_unlock(&cmd->context_lock);
402e8f081aaSYevgeny Petrilin 
403e8f081aaSYevgeny Petrilin 	up(&cmd->event_sem);
404e8f081aaSYevgeny Petrilin 	return err;
405e8f081aaSYevgeny Petrilin }
406e8f081aaSYevgeny Petrilin 
mlx4_comm_cmd(struct mlx4_dev * dev,u8 cmd,u16 param,u16 op,unsigned long timeout)407ab9c17a0SJack Morgenstein int mlx4_comm_cmd(struct mlx4_dev *dev, u8 cmd, u16 param,
4080cd93027SYishai Hadas 		  u16 op, unsigned long timeout)
409e8f081aaSYevgeny Petrilin {
4100cd93027SYishai Hadas 	if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
4110cd93027SYishai Hadas 		return mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
4120cd93027SYishai Hadas 
413e8f081aaSYevgeny Petrilin 	if (mlx4_priv(dev)->cmd.use_events)
4140cd93027SYishai Hadas 		return mlx4_comm_cmd_wait(dev, cmd, param, op, timeout);
415e8f081aaSYevgeny Petrilin 	return mlx4_comm_cmd_poll(dev, cmd, param, timeout);
416e8f081aaSYevgeny Petrilin }
417e8f081aaSYevgeny Petrilin 
cmd_pending(struct mlx4_dev * dev)4185a2cc190SJeff Kirsher static int cmd_pending(struct mlx4_dev *dev)
4195a2cc190SJeff Kirsher {
42057dbf29aSKleber Sacilotto de Souza 	u32 status;
42157dbf29aSKleber Sacilotto de Souza 
422872bf2fbSYishai Hadas 	if (pci_channel_offline(dev->persist->pdev))
42357dbf29aSKleber Sacilotto de Souza 		return -EIO;
42457dbf29aSKleber Sacilotto de Souza 
42557dbf29aSKleber Sacilotto de Souza 	status = readl(mlx4_priv(dev)->cmd.hcr + HCR_STATUS_OFFSET);
4265a2cc190SJeff Kirsher 
4275a2cc190SJeff Kirsher 	return (status & swab32(1 << HCR_GO_BIT)) ||
4285a2cc190SJeff Kirsher 		(mlx4_priv(dev)->cmd.toggle ==
4295a2cc190SJeff Kirsher 		 !!(status & swab32(1 << HCR_T_BIT)));
4305a2cc190SJeff Kirsher }
4315a2cc190SJeff Kirsher 
mlx4_cmd_post(struct mlx4_dev * dev,u64 in_param,u64 out_param,u32 in_modifier,u8 op_modifier,u16 op,u16 token,int event)4325a2cc190SJeff Kirsher static int mlx4_cmd_post(struct mlx4_dev *dev, u64 in_param, u64 out_param,
4335a2cc190SJeff Kirsher 			 u32 in_modifier, u8 op_modifier, u16 op, u16 token,
4345a2cc190SJeff Kirsher 			 int event)
4355a2cc190SJeff Kirsher {
4365a2cc190SJeff Kirsher 	struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
4375a2cc190SJeff Kirsher 	u32 __iomem *hcr = cmd->hcr;
438f5aef5aaSYishai Hadas 	int ret = -EIO;
4395a2cc190SJeff Kirsher 	unsigned long end;
4405a2cc190SJeff Kirsher 
441f5aef5aaSYishai Hadas 	mutex_lock(&dev->persist->device_state_mutex);
442f5aef5aaSYishai Hadas 	/* To avoid writing to unknown addresses after the device state was
443f5aef5aaSYishai Hadas 	  * changed to internal error and the chip was reset,
444f5aef5aaSYishai Hadas 	  * check the INTERNAL_ERROR flag which is updated under
445f5aef5aaSYishai Hadas 	  * device_state_mutex lock.
446f5aef5aaSYishai Hadas 	  */
447f5aef5aaSYishai Hadas 	if (pci_channel_offline(dev->persist->pdev) ||
448f5aef5aaSYishai Hadas 	    (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)) {
44957dbf29aSKleber Sacilotto de Souza 		/*
45057dbf29aSKleber Sacilotto de Souza 		 * Device is going through error recovery
45157dbf29aSKleber Sacilotto de Souza 		 * and cannot accept commands.
45257dbf29aSKleber Sacilotto de Souza 		 */
45357dbf29aSKleber Sacilotto de Souza 		goto out;
45457dbf29aSKleber Sacilotto de Souza 	}
45557dbf29aSKleber Sacilotto de Souza 
4565a2cc190SJeff Kirsher 	end = jiffies;
4575a2cc190SJeff Kirsher 	if (event)
4585a2cc190SJeff Kirsher 		end += msecs_to_jiffies(GO_BIT_TIMEOUT_MSECS);
4595a2cc190SJeff Kirsher 
4605a2cc190SJeff Kirsher 	while (cmd_pending(dev)) {
461872bf2fbSYishai Hadas 		if (pci_channel_offline(dev->persist->pdev)) {
46257dbf29aSKleber Sacilotto de Souza 			/*
46357dbf29aSKleber Sacilotto de Souza 			 * Device is going through error recovery
46457dbf29aSKleber Sacilotto de Souza 			 * and cannot accept commands.
46557dbf29aSKleber Sacilotto de Souza 			 */
46657dbf29aSKleber Sacilotto de Souza 			goto out;
46757dbf29aSKleber Sacilotto de Souza 		}
46857dbf29aSKleber Sacilotto de Souza 
469e8f081aaSYevgeny Petrilin 		if (time_after_eq(jiffies, end)) {
470e8f081aaSYevgeny Petrilin 			mlx4_err(dev, "%s:cmd_pending failed\n", __func__);
4715a2cc190SJeff Kirsher 			goto out;
472e8f081aaSYevgeny Petrilin 		}
4735a2cc190SJeff Kirsher 		cond_resched();
4745a2cc190SJeff Kirsher 	}
4755a2cc190SJeff Kirsher 
4765a2cc190SJeff Kirsher 	/*
4775a2cc190SJeff Kirsher 	 * We use writel (instead of something like memcpy_toio)
4785a2cc190SJeff Kirsher 	 * because writes of less than 32 bits to the HCR don't work
4795a2cc190SJeff Kirsher 	 * (and some architectures such as ia64 implement memcpy_toio
4805a2cc190SJeff Kirsher 	 * in terms of writeb).
4815a2cc190SJeff Kirsher 	 */
4825a2cc190SJeff Kirsher 	__raw_writel((__force u32) cpu_to_be32(in_param >> 32),		  hcr + 0);
4835a2cc190SJeff Kirsher 	__raw_writel((__force u32) cpu_to_be32(in_param & 0xfffffffful),  hcr + 1);
4845a2cc190SJeff Kirsher 	__raw_writel((__force u32) cpu_to_be32(in_modifier),		  hcr + 2);
4855a2cc190SJeff Kirsher 	__raw_writel((__force u32) cpu_to_be32(out_param >> 32),	  hcr + 3);
4865a2cc190SJeff Kirsher 	__raw_writel((__force u32) cpu_to_be32(out_param & 0xfffffffful), hcr + 4);
4875a2cc190SJeff Kirsher 	__raw_writel((__force u32) cpu_to_be32(token << 16),		  hcr + 5);
4885a2cc190SJeff Kirsher 
4895a2cc190SJeff Kirsher 	/* __raw_writel may not order writes. */
4905a2cc190SJeff Kirsher 	wmb();
4915a2cc190SJeff Kirsher 
4925a2cc190SJeff Kirsher 	__raw_writel((__force u32) cpu_to_be32((1 << HCR_GO_BIT)		|
4935a2cc190SJeff Kirsher 					       (cmd->toggle << HCR_T_BIT)	|
4945a2cc190SJeff Kirsher 					       (event ? (1 << HCR_E_BIT) : 0)	|
4955a2cc190SJeff Kirsher 					       (op_modifier << HCR_OPMOD_SHIFT) |
4965a2cc190SJeff Kirsher 					       op), hcr + 6);
4975a2cc190SJeff Kirsher 
4985a2cc190SJeff Kirsher 	cmd->toggle = cmd->toggle ^ 1;
4995a2cc190SJeff Kirsher 
5005a2cc190SJeff Kirsher 	ret = 0;
5015a2cc190SJeff Kirsher 
5025a2cc190SJeff Kirsher out:
503f5aef5aaSYishai Hadas 	if (ret)
504f5aef5aaSYishai Hadas 		mlx4_warn(dev, "Could not post command 0x%x: ret=%d, in_param=0x%llx, in_mod=0x%x, op_mod=0x%x\n",
505f5aef5aaSYishai Hadas 			  op, ret, in_param, in_modifier, op_modifier);
506f5aef5aaSYishai Hadas 	mutex_unlock(&dev->persist->device_state_mutex);
507f5aef5aaSYishai Hadas 
5085a2cc190SJeff Kirsher 	return ret;
5095a2cc190SJeff Kirsher }
5105a2cc190SJeff Kirsher 
mlx4_slave_cmd(struct mlx4_dev * dev,u64 in_param,u64 * out_param,int out_is_imm,u32 in_modifier,u8 op_modifier,u16 op,unsigned long timeout)511e8f081aaSYevgeny Petrilin static int mlx4_slave_cmd(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
512e8f081aaSYevgeny Petrilin 			  int out_is_imm, u32 in_modifier, u8 op_modifier,
513e8f081aaSYevgeny Petrilin 			  u16 op, unsigned long timeout)
514e8f081aaSYevgeny Petrilin {
515e8f081aaSYevgeny Petrilin 	struct mlx4_priv *priv = mlx4_priv(dev);
516e8f081aaSYevgeny Petrilin 	struct mlx4_vhcr_cmd *vhcr = priv->mfunc.vhcr;
517e8f081aaSYevgeny Petrilin 	int ret;
518e8f081aaSYevgeny Petrilin 
519f3d4c89eSRoland Dreier 	mutex_lock(&priv->cmd.slave_cmd_mutex);
520f3d4c89eSRoland Dreier 
521e8f081aaSYevgeny Petrilin 	vhcr->in_param = cpu_to_be64(in_param);
522e8f081aaSYevgeny Petrilin 	vhcr->out_param = out_param ? cpu_to_be64(*out_param) : 0;
523e8f081aaSYevgeny Petrilin 	vhcr->in_modifier = cpu_to_be32(in_modifier);
524e8f081aaSYevgeny Petrilin 	vhcr->opcode = cpu_to_be16((((u16) op_modifier) << 12) | (op & 0xfff));
525e8f081aaSYevgeny Petrilin 	vhcr->token = cpu_to_be16(CMD_POLL_TOKEN);
526e8f081aaSYevgeny Petrilin 	vhcr->status = 0;
527e8f081aaSYevgeny Petrilin 	vhcr->flags = !!(priv->cmd.use_events) << 6;
528f3d4c89eSRoland Dreier 
529e8f081aaSYevgeny Petrilin 	if (mlx4_is_master(dev)) {
530e8f081aaSYevgeny Petrilin 		ret = mlx4_master_process_vhcr(dev, dev->caps.function, vhcr);
531e8f081aaSYevgeny Petrilin 		if (!ret) {
532e8f081aaSYevgeny Petrilin 			if (out_is_imm) {
533e8f081aaSYevgeny Petrilin 				if (out_param)
534e8f081aaSYevgeny Petrilin 					*out_param =
535e8f081aaSYevgeny Petrilin 						be64_to_cpu(vhcr->out_param);
536e8f081aaSYevgeny Petrilin 				else {
5371a91de28SJoe Perches 					mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
5381a91de28SJoe Perches 						 op);
53972be84f1SYevgeny Petrilin 					vhcr->status = CMD_STAT_BAD_PARAM;
540e8f081aaSYevgeny Petrilin 				}
541e8f081aaSYevgeny Petrilin 			}
54272be84f1SYevgeny Petrilin 			ret = mlx4_status_to_errno(vhcr->status);
543e8f081aaSYevgeny Petrilin 		}
5440cd93027SYishai Hadas 		if (ret &&
5450cd93027SYishai Hadas 		    dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
5460cd93027SYishai Hadas 			ret = mlx4_internal_err_ret_value(dev, op, op_modifier);
547e8f081aaSYevgeny Petrilin 	} else {
5480cd93027SYishai Hadas 		ret = mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR_POST, 0, op,
549e8f081aaSYevgeny Petrilin 				    MLX4_COMM_TIME + timeout);
550e8f081aaSYevgeny Petrilin 		if (!ret) {
551e8f081aaSYevgeny Petrilin 			if (out_is_imm) {
552e8f081aaSYevgeny Petrilin 				if (out_param)
553e8f081aaSYevgeny Petrilin 					*out_param =
554e8f081aaSYevgeny Petrilin 						be64_to_cpu(vhcr->out_param);
555e8f081aaSYevgeny Petrilin 				else {
5561a91de28SJoe Perches 					mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
5571a91de28SJoe Perches 						 op);
55872be84f1SYevgeny Petrilin 					vhcr->status = CMD_STAT_BAD_PARAM;
559e8f081aaSYevgeny Petrilin 				}
560e8f081aaSYevgeny Petrilin 			}
56172be84f1SYevgeny Petrilin 			ret = mlx4_status_to_errno(vhcr->status);
5620cd93027SYishai Hadas 		} else {
5630cd93027SYishai Hadas 			if (dev->persist->state &
5640cd93027SYishai Hadas 			    MLX4_DEVICE_STATE_INTERNAL_ERROR)
5650cd93027SYishai Hadas 				ret = mlx4_internal_err_ret_value(dev, op,
5660cd93027SYishai Hadas 								  op_modifier);
5670cd93027SYishai Hadas 			else
5680cd93027SYishai Hadas 				mlx4_err(dev, "failed execution of VHCR_POST command opcode 0x%x\n", op);
5690cd93027SYishai Hadas 		}
570e8f081aaSYevgeny Petrilin 	}
571f3d4c89eSRoland Dreier 
572f3d4c89eSRoland Dreier 	mutex_unlock(&priv->cmd.slave_cmd_mutex);
573e8f081aaSYevgeny Petrilin 	return ret;
574e8f081aaSYevgeny Petrilin }
575e8f081aaSYevgeny Petrilin 
mlx4_cmd_poll(struct mlx4_dev * dev,u64 in_param,u64 * out_param,int out_is_imm,u32 in_modifier,u8 op_modifier,u16 op,unsigned long timeout)5765a2cc190SJeff Kirsher static int mlx4_cmd_poll(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
5775a2cc190SJeff Kirsher 			 int out_is_imm, u32 in_modifier, u8 op_modifier,
5785a2cc190SJeff Kirsher 			 u16 op, unsigned long timeout)
5795a2cc190SJeff Kirsher {
5805a2cc190SJeff Kirsher 	struct mlx4_priv *priv = mlx4_priv(dev);
5815a2cc190SJeff Kirsher 	void __iomem *hcr = priv->cmd.hcr;
5825a2cc190SJeff Kirsher 	int err = 0;
5835a2cc190SJeff Kirsher 	unsigned long end;
584e8f081aaSYevgeny Petrilin 	u32 stat;
5855a2cc190SJeff Kirsher 
5865a2cc190SJeff Kirsher 	down(&priv->cmd.poll_sem);
5875a2cc190SJeff Kirsher 
588f5aef5aaSYishai Hadas 	if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
58957dbf29aSKleber Sacilotto de Souza 		/*
59057dbf29aSKleber Sacilotto de Souza 		 * Device is going through error recovery
59157dbf29aSKleber Sacilotto de Souza 		 * and cannot accept commands.
59257dbf29aSKleber Sacilotto de Souza 		 */
593f5aef5aaSYishai Hadas 		err = mlx4_internal_err_ret_value(dev, op, op_modifier);
59457dbf29aSKleber Sacilotto de Souza 		goto out;
59557dbf29aSKleber Sacilotto de Souza 	}
59657dbf29aSKleber Sacilotto de Souza 
597c05a116fSEyal Perry 	if (out_is_imm && !out_param) {
598c05a116fSEyal Perry 		mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
599c05a116fSEyal Perry 			 op);
600c05a116fSEyal Perry 		err = -EINVAL;
601c05a116fSEyal Perry 		goto out;
602c05a116fSEyal Perry 	}
603c05a116fSEyal Perry 
6045a2cc190SJeff Kirsher 	err = mlx4_cmd_post(dev, in_param, out_param ? *out_param : 0,
6055a2cc190SJeff Kirsher 			    in_modifier, op_modifier, op, CMD_POLL_TOKEN, 0);
6065a2cc190SJeff Kirsher 	if (err)
607f5aef5aaSYishai Hadas 		goto out_reset;
6085a2cc190SJeff Kirsher 
6095a2cc190SJeff Kirsher 	end = msecs_to_jiffies(timeout) + jiffies;
61057dbf29aSKleber Sacilotto de Souza 	while (cmd_pending(dev) && time_before(jiffies, end)) {
611872bf2fbSYishai Hadas 		if (pci_channel_offline(dev->persist->pdev)) {
61257dbf29aSKleber Sacilotto de Souza 			/*
61357dbf29aSKleber Sacilotto de Souza 			 * Device is going through error recovery
61457dbf29aSKleber Sacilotto de Souza 			 * and cannot accept commands.
61557dbf29aSKleber Sacilotto de Souza 			 */
61657dbf29aSKleber Sacilotto de Souza 			err = -EIO;
617f5aef5aaSYishai Hadas 			goto out_reset;
618f5aef5aaSYishai Hadas 		}
619f5aef5aaSYishai Hadas 
620f5aef5aaSYishai Hadas 		if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
621f5aef5aaSYishai Hadas 			err = mlx4_internal_err_ret_value(dev, op, op_modifier);
62257dbf29aSKleber Sacilotto de Souza 			goto out;
62357dbf29aSKleber Sacilotto de Souza 		}
62457dbf29aSKleber Sacilotto de Souza 
6255a2cc190SJeff Kirsher 		cond_resched();
62657dbf29aSKleber Sacilotto de Souza 	}
6275a2cc190SJeff Kirsher 
6285a2cc190SJeff Kirsher 	if (cmd_pending(dev)) {
629674925edSDotan Barak 		mlx4_warn(dev, "command 0x%x timed out (go bit not cleared)\n",
630674925edSDotan Barak 			  op);
631f5aef5aaSYishai Hadas 		err = -EIO;
632f5aef5aaSYishai Hadas 		goto out_reset;
6335a2cc190SJeff Kirsher 	}
6345a2cc190SJeff Kirsher 
6355a2cc190SJeff Kirsher 	if (out_is_imm)
6365a2cc190SJeff Kirsher 		*out_param =
6375a2cc190SJeff Kirsher 			(u64) be32_to_cpu((__force __be32)
6385a2cc190SJeff Kirsher 					  __raw_readl(hcr + HCR_OUT_PARAM_OFFSET)) << 32 |
6395a2cc190SJeff Kirsher 			(u64) be32_to_cpu((__force __be32)
6405a2cc190SJeff Kirsher 					  __raw_readl(hcr + HCR_OUT_PARAM_OFFSET + 4));
641e8f081aaSYevgeny Petrilin 	stat = be32_to_cpu((__force __be32)
642e8f081aaSYevgeny Petrilin 			   __raw_readl(hcr + HCR_STATUS_OFFSET)) >> 24;
643e8f081aaSYevgeny Petrilin 	err = mlx4_status_to_errno(stat);
644f5aef5aaSYishai Hadas 	if (err) {
645e8f081aaSYevgeny Petrilin 		mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
646e8f081aaSYevgeny Petrilin 			 op, stat);
647f5aef5aaSYishai Hadas 		if (mlx4_closing_cmd_fatal_error(op, stat))
648f5aef5aaSYishai Hadas 			goto out_reset;
649f5aef5aaSYishai Hadas 		goto out;
650f5aef5aaSYishai Hadas 	}
6515a2cc190SJeff Kirsher 
652f5aef5aaSYishai Hadas out_reset:
653f5aef5aaSYishai Hadas 	if (err)
654f5aef5aaSYishai Hadas 		err = mlx4_cmd_reset_flow(dev, op, op_modifier, err);
6555a2cc190SJeff Kirsher out:
6565a2cc190SJeff Kirsher 	up(&priv->cmd.poll_sem);
6575a2cc190SJeff Kirsher 	return err;
6585a2cc190SJeff Kirsher }
6595a2cc190SJeff Kirsher 
mlx4_cmd_event(struct mlx4_dev * dev,u16 token,u8 status,u64 out_param)6605a2cc190SJeff Kirsher void mlx4_cmd_event(struct mlx4_dev *dev, u16 token, u8 status, u64 out_param)
6615a2cc190SJeff Kirsher {
6625a2cc190SJeff Kirsher 	struct mlx4_priv *priv = mlx4_priv(dev);
6635a2cc190SJeff Kirsher 	struct mlx4_cmd_context *context =
6645a2cc190SJeff Kirsher 		&priv->cmd.context[token & priv->cmd.token_mask];
6655a2cc190SJeff Kirsher 
6665a2cc190SJeff Kirsher 	/* previously timed out command completing at long last */
6675a2cc190SJeff Kirsher 	if (token != context->token)
6685a2cc190SJeff Kirsher 		return;
6695a2cc190SJeff Kirsher 
670e8f081aaSYevgeny Petrilin 	context->fw_status = status;
6715a2cc190SJeff Kirsher 	context->result    = mlx4_status_to_errno(status);
6725a2cc190SJeff Kirsher 	context->out_param = out_param;
6735a2cc190SJeff Kirsher 
6745a2cc190SJeff Kirsher 	complete(&context->done);
6755a2cc190SJeff Kirsher }
6765a2cc190SJeff Kirsher 
mlx4_cmd_wait(struct mlx4_dev * dev,u64 in_param,u64 * out_param,int out_is_imm,u32 in_modifier,u8 op_modifier,u16 op,unsigned long timeout)6775a2cc190SJeff Kirsher static int mlx4_cmd_wait(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
6785a2cc190SJeff Kirsher 			 int out_is_imm, u32 in_modifier, u8 op_modifier,
6795a2cc190SJeff Kirsher 			 u16 op, unsigned long timeout)
6805a2cc190SJeff Kirsher {
6815a2cc190SJeff Kirsher 	struct mlx4_cmd *cmd = &mlx4_priv(dev)->cmd;
6825a2cc190SJeff Kirsher 	struct mlx4_cmd_context *context;
6839f5b0317SJack Morgenstein 	long ret_wait;
6845a2cc190SJeff Kirsher 	int err = 0;
6855a2cc190SJeff Kirsher 
6865a2cc190SJeff Kirsher 	down(&cmd->event_sem);
6875a2cc190SJeff Kirsher 
6885a2cc190SJeff Kirsher 	spin_lock(&cmd->context_lock);
6895a2cc190SJeff Kirsher 	BUG_ON(cmd->free_head < 0);
6905a2cc190SJeff Kirsher 	context = &cmd->context[cmd->free_head];
6915a2cc190SJeff Kirsher 	context->token += cmd->token_mask + 1;
6925a2cc190SJeff Kirsher 	cmd->free_head = context->next;
6935a2cc190SJeff Kirsher 	spin_unlock(&cmd->context_lock);
6945a2cc190SJeff Kirsher 
695c05a116fSEyal Perry 	if (out_is_imm && !out_param) {
696c05a116fSEyal Perry 		mlx4_err(dev, "response expected while output mailbox is NULL for command 0x%x\n",
697c05a116fSEyal Perry 			 op);
698c05a116fSEyal Perry 		err = -EINVAL;
699c05a116fSEyal Perry 		goto out;
700c05a116fSEyal Perry 	}
701c05a116fSEyal Perry 
702f5aef5aaSYishai Hadas 	reinit_completion(&context->done);
7035a2cc190SJeff Kirsher 
704f5aef5aaSYishai Hadas 	err = mlx4_cmd_post(dev, in_param, out_param ? *out_param : 0,
7055a2cc190SJeff Kirsher 			    in_modifier, op_modifier, op, context->token, 1);
706f5aef5aaSYishai Hadas 	if (err)
707f5aef5aaSYishai Hadas 		goto out_reset;
7085a2cc190SJeff Kirsher 
7099f5b0317SJack Morgenstein 	if (op == MLX4_CMD_SENSE_PORT) {
7109f5b0317SJack Morgenstein 		ret_wait =
7119f5b0317SJack Morgenstein 			wait_for_completion_interruptible_timeout(&context->done,
7129f5b0317SJack Morgenstein 								  msecs_to_jiffies(timeout));
7139f5b0317SJack Morgenstein 		if (ret_wait < 0) {
7149f5b0317SJack Morgenstein 			context->fw_status = 0;
7159f5b0317SJack Morgenstein 			context->out_param = 0;
7169f5b0317SJack Morgenstein 			context->result = 0;
7179f5b0317SJack Morgenstein 		}
7189f5b0317SJack Morgenstein 	} else {
7199f5b0317SJack Morgenstein 		ret_wait = (long)wait_for_completion_timeout(&context->done,
7209f5b0317SJack Morgenstein 							     msecs_to_jiffies(timeout));
7219f5b0317SJack Morgenstein 	}
7229f5b0317SJack Morgenstein 	if (!ret_wait) {
723674925edSDotan Barak 		mlx4_warn(dev, "command 0x%x timed out (go bit not cleared)\n",
724674925edSDotan Barak 			  op);
725f4ecf29fSBenjamin Poirier 		if (op == MLX4_CMD_NOP) {
726f4ecf29fSBenjamin Poirier 			err = -EBUSY;
727f4ecf29fSBenjamin Poirier 			goto out;
728f4ecf29fSBenjamin Poirier 		} else {
729f5aef5aaSYishai Hadas 			err = -EIO;
730f5aef5aaSYishai Hadas 			goto out_reset;
7315a2cc190SJeff Kirsher 		}
732f4ecf29fSBenjamin Poirier 	}
7335a2cc190SJeff Kirsher 
7345a2cc190SJeff Kirsher 	err = context->result;
735e8f081aaSYevgeny Petrilin 	if (err) {
7361daa4303SJack Morgenstein 		/* Since we do not want to have this error message always
7371daa4303SJack Morgenstein 		 * displayed at driver start when there are ConnectX2 HCAs
7381daa4303SJack Morgenstein 		 * on the host, we deprecate the error message for this
7391daa4303SJack Morgenstein 		 * specific command/input_mod/opcode_mod/fw-status to be debug.
7401daa4303SJack Morgenstein 		 */
741fde913e2SJack Morgenstein 		if (op == MLX4_CMD_SET_PORT &&
742fde913e2SJack Morgenstein 		    (in_modifier == 1 || in_modifier == 2) &&
743a130b590SIdo Shamay 		    op_modifier == MLX4_SET_PORT_IB_OPCODE &&
744a130b590SIdo Shamay 		    context->fw_status == CMD_STAT_BAD_SIZE)
7451daa4303SJack Morgenstein 			mlx4_dbg(dev, "command 0x%x failed: fw status = 0x%x\n",
7461daa4303SJack Morgenstein 				 op, context->fw_status);
7471daa4303SJack Morgenstein 		else
748e8f081aaSYevgeny Petrilin 			mlx4_err(dev, "command 0x%x failed: fw status = 0x%x\n",
749e8f081aaSYevgeny Petrilin 				 op, context->fw_status);
750f5aef5aaSYishai Hadas 		if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
751f5aef5aaSYishai Hadas 			err = mlx4_internal_err_ret_value(dev, op, op_modifier);
752f5aef5aaSYishai Hadas 		else if (mlx4_closing_cmd_fatal_error(op, context->fw_status))
753f5aef5aaSYishai Hadas 			goto out_reset;
754f5aef5aaSYishai Hadas 
7555a2cc190SJeff Kirsher 		goto out;
756e8f081aaSYevgeny Petrilin 	}
7575a2cc190SJeff Kirsher 
7585a2cc190SJeff Kirsher 	if (out_is_imm)
7595a2cc190SJeff Kirsher 		*out_param = context->out_param;
7605a2cc190SJeff Kirsher 
761f5aef5aaSYishai Hadas out_reset:
762f5aef5aaSYishai Hadas 	if (err)
763f5aef5aaSYishai Hadas 		err = mlx4_cmd_reset_flow(dev, op, op_modifier, err);
7645a2cc190SJeff Kirsher out:
7655a2cc190SJeff Kirsher 	spin_lock(&cmd->context_lock);
7665a2cc190SJeff Kirsher 	context->next = cmd->free_head;
7675a2cc190SJeff Kirsher 	cmd->free_head = context - cmd->context;
7685a2cc190SJeff Kirsher 	spin_unlock(&cmd->context_lock);
7695a2cc190SJeff Kirsher 
7705a2cc190SJeff Kirsher 	up(&cmd->event_sem);
7715a2cc190SJeff Kirsher 	return err;
7725a2cc190SJeff Kirsher }
7735a2cc190SJeff Kirsher 
__mlx4_cmd(struct mlx4_dev * dev,u64 in_param,u64 * out_param,int out_is_imm,u32 in_modifier,u8 op_modifier,u16 op,unsigned long timeout,int native)7745a2cc190SJeff Kirsher int __mlx4_cmd(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
7755a2cc190SJeff Kirsher 	       int out_is_imm, u32 in_modifier, u8 op_modifier,
776f9baff50SJack Morgenstein 	       u16 op, unsigned long timeout, int native)
7775a2cc190SJeff Kirsher {
778872bf2fbSYishai Hadas 	if (pci_channel_offline(dev->persist->pdev))
779f5aef5aaSYishai Hadas 		return mlx4_cmd_reset_flow(dev, op, op_modifier, -EIO);
78057dbf29aSKleber Sacilotto de Souza 
781e8f081aaSYevgeny Petrilin 	if (!mlx4_is_mfunc(dev) || (native && mlx4_is_master(dev))) {
782a7e1f049SJack Morgenstein 		int ret;
783a7e1f049SJack Morgenstein 
784f5aef5aaSYishai Hadas 		if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
785f5aef5aaSYishai Hadas 			return mlx4_internal_err_ret_value(dev, op,
786f5aef5aaSYishai Hadas 							  op_modifier);
787a7e1f049SJack Morgenstein 		down_read(&mlx4_priv(dev)->cmd.switch_sem);
7885a2cc190SJeff Kirsher 		if (mlx4_priv(dev)->cmd.use_events)
789a7e1f049SJack Morgenstein 			ret = mlx4_cmd_wait(dev, in_param, out_param,
790e8f081aaSYevgeny Petrilin 					    out_is_imm, in_modifier,
791e8f081aaSYevgeny Petrilin 					    op_modifier, op, timeout);
7925a2cc190SJeff Kirsher 		else
793a7e1f049SJack Morgenstein 			ret = mlx4_cmd_poll(dev, in_param, out_param,
794e8f081aaSYevgeny Petrilin 					    out_is_imm, in_modifier,
795e8f081aaSYevgeny Petrilin 					    op_modifier, op, timeout);
796a7e1f049SJack Morgenstein 
797a7e1f049SJack Morgenstein 		up_read(&mlx4_priv(dev)->cmd.switch_sem);
798a7e1f049SJack Morgenstein 		return ret;
799e8f081aaSYevgeny Petrilin 	}
800e8f081aaSYevgeny Petrilin 	return mlx4_slave_cmd(dev, in_param, out_param, out_is_imm,
8015a2cc190SJeff Kirsher 			      in_modifier, op_modifier, op, timeout);
8025a2cc190SJeff Kirsher }
8035a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(__mlx4_cmd);
8045a2cc190SJeff Kirsher 
805e8f081aaSYevgeny Petrilin 
mlx4_ARM_COMM_CHANNEL(struct mlx4_dev * dev)80655ad3592SYishai Hadas int mlx4_ARM_COMM_CHANNEL(struct mlx4_dev *dev)
807e8f081aaSYevgeny Petrilin {
808e8f081aaSYevgeny Petrilin 	return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_ARM_COMM_CHANNEL,
809e8f081aaSYevgeny Petrilin 			MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
810e8f081aaSYevgeny Petrilin }
811e8f081aaSYevgeny Petrilin 
mlx4_ACCESS_MEM(struct mlx4_dev * dev,u64 master_addr,int slave,u64 slave_addr,int size,int is_read)812e8f081aaSYevgeny Petrilin static int mlx4_ACCESS_MEM(struct mlx4_dev *dev, u64 master_addr,
813e8f081aaSYevgeny Petrilin 			   int slave, u64 slave_addr,
814e8f081aaSYevgeny Petrilin 			   int size, int is_read)
815e8f081aaSYevgeny Petrilin {
816e8f081aaSYevgeny Petrilin 	u64 in_param;
817e8f081aaSYevgeny Petrilin 	u64 out_param;
818e8f081aaSYevgeny Petrilin 
819e8f081aaSYevgeny Petrilin 	if ((slave_addr & 0xfff) | (master_addr & 0xfff) |
820e8f081aaSYevgeny Petrilin 	    (slave & ~0x7f) | (size & 0xff)) {
8211a91de28SJoe Perches 		mlx4_err(dev, "Bad access mem params - slave_addr:0x%llx master_addr:0x%llx slave_id:%d size:%d\n",
822e8f081aaSYevgeny Petrilin 			 slave_addr, master_addr, slave, size);
823e8f081aaSYevgeny Petrilin 		return -EINVAL;
824e8f081aaSYevgeny Petrilin 	}
825e8f081aaSYevgeny Petrilin 
826e8f081aaSYevgeny Petrilin 	if (is_read) {
827e8f081aaSYevgeny Petrilin 		in_param = (u64) slave | slave_addr;
828e8f081aaSYevgeny Petrilin 		out_param = (u64) dev->caps.function | master_addr;
829e8f081aaSYevgeny Petrilin 	} else {
830e8f081aaSYevgeny Petrilin 		in_param = (u64) dev->caps.function | master_addr;
831e8f081aaSYevgeny Petrilin 		out_param = (u64) slave | slave_addr;
832e8f081aaSYevgeny Petrilin 	}
833e8f081aaSYevgeny Petrilin 
834e8f081aaSYevgeny Petrilin 	return mlx4_cmd_imm(dev, in_param, &out_param, size, 0,
835e8f081aaSYevgeny Petrilin 			    MLX4_CMD_ACCESS_MEM,
836e8f081aaSYevgeny Petrilin 			    MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
837e8f081aaSYevgeny Petrilin }
838e8f081aaSYevgeny Petrilin 
query_pkey_block(struct mlx4_dev * dev,u8 port,u16 index,u16 * pkey,struct mlx4_cmd_mailbox * inbox,struct mlx4_cmd_mailbox * outbox)8390a9a0188SJack Morgenstein static int query_pkey_block(struct mlx4_dev *dev, u8 port, u16 index, u16 *pkey,
8400a9a0188SJack Morgenstein 			       struct mlx4_cmd_mailbox *inbox,
8410a9a0188SJack Morgenstein 			       struct mlx4_cmd_mailbox *outbox)
8420a9a0188SJack Morgenstein {
8430a9a0188SJack Morgenstein 	struct ib_smp *in_mad = (struct ib_smp *)(inbox->buf);
8440a9a0188SJack Morgenstein 	struct ib_smp *out_mad = (struct ib_smp *)(outbox->buf);
8450a9a0188SJack Morgenstein 	int err;
8460a9a0188SJack Morgenstein 	int i;
8470a9a0188SJack Morgenstein 
8480a9a0188SJack Morgenstein 	if (index & 0x1f)
8490a9a0188SJack Morgenstein 		return -EINVAL;
8500a9a0188SJack Morgenstein 
8510a9a0188SJack Morgenstein 	in_mad->attr_mod = cpu_to_be32(index / 32);
8520a9a0188SJack Morgenstein 
8530a9a0188SJack Morgenstein 	err = mlx4_cmd_box(dev, inbox->dma, outbox->dma, port, 3,
8540a9a0188SJack Morgenstein 			   MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
8550a9a0188SJack Morgenstein 			   MLX4_CMD_NATIVE);
8560a9a0188SJack Morgenstein 	if (err)
8570a9a0188SJack Morgenstein 		return err;
8580a9a0188SJack Morgenstein 
8590a9a0188SJack Morgenstein 	for (i = 0; i < 32; ++i)
8600a9a0188SJack Morgenstein 		pkey[i] = be16_to_cpu(((__be16 *) out_mad->data)[i]);
8610a9a0188SJack Morgenstein 
8620a9a0188SJack Morgenstein 	return err;
8630a9a0188SJack Morgenstein }
8640a9a0188SJack Morgenstein 
get_full_pkey_table(struct mlx4_dev * dev,u8 port,u16 * table,struct mlx4_cmd_mailbox * inbox,struct mlx4_cmd_mailbox * outbox)8650a9a0188SJack Morgenstein static int get_full_pkey_table(struct mlx4_dev *dev, u8 port, u16 *table,
8660a9a0188SJack Morgenstein 			       struct mlx4_cmd_mailbox *inbox,
8670a9a0188SJack Morgenstein 			       struct mlx4_cmd_mailbox *outbox)
8680a9a0188SJack Morgenstein {
8690a9a0188SJack Morgenstein 	int i;
8700a9a0188SJack Morgenstein 	int err;
8710a9a0188SJack Morgenstein 
8720a9a0188SJack Morgenstein 	for (i = 0; i < dev->caps.pkey_table_len[port]; i += 32) {
8730a9a0188SJack Morgenstein 		err = query_pkey_block(dev, port, i, table + i, inbox, outbox);
8740a9a0188SJack Morgenstein 		if (err)
8750a9a0188SJack Morgenstein 			return err;
8760a9a0188SJack Morgenstein 	}
8770a9a0188SJack Morgenstein 
8780a9a0188SJack Morgenstein 	return 0;
8790a9a0188SJack Morgenstein }
8800a9a0188SJack Morgenstein #define PORT_CAPABILITY_LOCATION_IN_SMP 20
8810a9a0188SJack Morgenstein #define PORT_STATE_OFFSET 32
8820a9a0188SJack Morgenstein 
vf_port_state(struct mlx4_dev * dev,int port,int vf)8830a9a0188SJack Morgenstein static enum ib_port_state vf_port_state(struct mlx4_dev *dev, int port, int vf)
8840a9a0188SJack Morgenstein {
885a0c64a17SJack Morgenstein 	if (mlx4_get_slave_port_state(dev, vf, port) == SLAVE_PORT_UP)
886a0c64a17SJack Morgenstein 		return IB_PORT_ACTIVE;
887a0c64a17SJack Morgenstein 	else
8880a9a0188SJack Morgenstein 		return IB_PORT_DOWN;
8890a9a0188SJack Morgenstein }
8900a9a0188SJack Morgenstein 
mlx4_MAD_IFC_wrapper(struct mlx4_dev * dev,int slave,struct mlx4_vhcr * vhcr,struct mlx4_cmd_mailbox * inbox,struct mlx4_cmd_mailbox * outbox,struct mlx4_cmd_info * cmd)8910a9a0188SJack Morgenstein static int mlx4_MAD_IFC_wrapper(struct mlx4_dev *dev, int slave,
8920a9a0188SJack Morgenstein 				struct mlx4_vhcr *vhcr,
8930a9a0188SJack Morgenstein 				struct mlx4_cmd_mailbox *inbox,
8940a9a0188SJack Morgenstein 				struct mlx4_cmd_mailbox *outbox,
8950a9a0188SJack Morgenstein 				struct mlx4_cmd_info *cmd)
8960a9a0188SJack Morgenstein {
8970a9a0188SJack Morgenstein 	struct ib_smp *smp = inbox->buf;
8980a9a0188SJack Morgenstein 	u32 index;
8997c35ef45SOr Gerlitz 	u8 port, slave_port;
90097982f5aSJack Morgenstein 	u8 opcode_modifier;
9010a9a0188SJack Morgenstein 	u16 *table;
9020a9a0188SJack Morgenstein 	int err;
9030a9a0188SJack Morgenstein 	int vidx, pidx;
90497982f5aSJack Morgenstein 	int network_view;
9050a9a0188SJack Morgenstein 	struct mlx4_priv *priv = mlx4_priv(dev);
9060a9a0188SJack Morgenstein 	struct ib_smp *outsmp = outbox->buf;
9070a9a0188SJack Morgenstein 	__be16 *outtab = (__be16 *)(outsmp->data);
9080a9a0188SJack Morgenstein 	__be32 slave_cap_mask;
909afa8fd1dSJack Morgenstein 	__be64 slave_node_guid;
91097982f5aSJack Morgenstein 
9117c35ef45SOr Gerlitz 	slave_port = vhcr->in_modifier;
9127c35ef45SOr Gerlitz 	port = mlx4_slave_convert_port(dev, slave, slave_port);
9130a9a0188SJack Morgenstein 
91497982f5aSJack Morgenstein 	/* network-view bit is for driver use only, and should not be passed to FW */
91597982f5aSJack Morgenstein 	opcode_modifier = vhcr->op_modifier & ~0x8; /* clear netw view bit */
91697982f5aSJack Morgenstein 	network_view = !!(vhcr->op_modifier & 0x8);
91797982f5aSJack Morgenstein 
9180a9a0188SJack Morgenstein 	if (smp->base_version == 1 &&
9190a9a0188SJack Morgenstein 	    smp->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED &&
9200a9a0188SJack Morgenstein 	    smp->class_version == 1) {
92197982f5aSJack Morgenstein 		/* host view is paravirtualized */
92297982f5aSJack Morgenstein 		if (!network_view && smp->method == IB_MGMT_METHOD_GET) {
9230a9a0188SJack Morgenstein 			if (smp->attr_id == IB_SMP_ATTR_PKEY_TABLE) {
9240a9a0188SJack Morgenstein 				index = be32_to_cpu(smp->attr_mod);
9250a9a0188SJack Morgenstein 				if (port < 1 || port > dev->caps.num_ports)
9260a9a0188SJack Morgenstein 					return -EINVAL;
92719ab574fSMatan Barak 				table = kcalloc((dev->caps.pkey_table_len[port] / 32) + 1,
92819ab574fSMatan Barak 						sizeof(*table) * 32, GFP_KERNEL);
92919ab574fSMatan Barak 
9300a9a0188SJack Morgenstein 				if (!table)
9310a9a0188SJack Morgenstein 					return -ENOMEM;
9320a9a0188SJack Morgenstein 				/* need to get the full pkey table because the paravirtualized
9330a9a0188SJack Morgenstein 				 * pkeys may be scattered among several pkey blocks.
9340a9a0188SJack Morgenstein 				 */
9350a9a0188SJack Morgenstein 				err = get_full_pkey_table(dev, port, table, inbox, outbox);
9360a9a0188SJack Morgenstein 				if (!err) {
9370a9a0188SJack Morgenstein 					for (vidx = index * 32; vidx < (index + 1) * 32; ++vidx) {
9380a9a0188SJack Morgenstein 						pidx = priv->virt2phys_pkey[slave][port - 1][vidx];
9390a9a0188SJack Morgenstein 						outtab[vidx % 32] = cpu_to_be16(table[pidx]);
9400a9a0188SJack Morgenstein 					}
9410a9a0188SJack Morgenstein 				}
9420a9a0188SJack Morgenstein 				kfree(table);
9430a9a0188SJack Morgenstein 				return err;
9440a9a0188SJack Morgenstein 			}
9450a9a0188SJack Morgenstein 			if (smp->attr_id == IB_SMP_ATTR_PORT_INFO) {
9460a9a0188SJack Morgenstein 				/*get the slave specific caps:*/
9470a9a0188SJack Morgenstein 				/*do the command */
9487c35ef45SOr Gerlitz 				smp->attr_mod = cpu_to_be32(port);
9490a9a0188SJack Morgenstein 				err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
9507c35ef45SOr Gerlitz 					    port, opcode_modifier,
9510a9a0188SJack Morgenstein 					    vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
9520a9a0188SJack Morgenstein 				/* modify the response for slaves */
9530a9a0188SJack Morgenstein 				if (!err && slave != mlx4_master_func_num(dev)) {
9540a9a0188SJack Morgenstein 					u8 *state = outsmp->data + PORT_STATE_OFFSET;
9550a9a0188SJack Morgenstein 
9560a9a0188SJack Morgenstein 					*state = (*state & 0xf0) | vf_port_state(dev, port, slave);
9570a9a0188SJack Morgenstein 					slave_cap_mask = priv->mfunc.master.slave_state[slave].ib_cap_mask[port];
9580a9a0188SJack Morgenstein 					memcpy(outsmp->data + PORT_CAPABILITY_LOCATION_IN_SMP, &slave_cap_mask, 4);
9590a9a0188SJack Morgenstein 				}
9600a9a0188SJack Morgenstein 				return err;
9610a9a0188SJack Morgenstein 			}
9620a9a0188SJack Morgenstein 			if (smp->attr_id == IB_SMP_ATTR_GUID_INFO) {
963e9a7ff3aSYishai Hadas 				__be64 guid = mlx4_get_admin_guid(dev, slave,
964e9a7ff3aSYishai Hadas 								  port);
965e9a7ff3aSYishai Hadas 
966e9a7ff3aSYishai Hadas 				/* set the PF admin guid to the FW/HW burned
967e9a7ff3aSYishai Hadas 				 * GUID, if it wasn't yet set
968e9a7ff3aSYishai Hadas 				 */
969e9a7ff3aSYishai Hadas 				if (slave == 0 && guid == 0) {
970e9a7ff3aSYishai Hadas 					smp->attr_mod = 0;
971e9a7ff3aSYishai Hadas 					err = mlx4_cmd_box(dev,
972e9a7ff3aSYishai Hadas 							   inbox->dma,
973e9a7ff3aSYishai Hadas 							   outbox->dma,
974e9a7ff3aSYishai Hadas 							   vhcr->in_modifier,
975e9a7ff3aSYishai Hadas 							   opcode_modifier,
976e9a7ff3aSYishai Hadas 							   vhcr->op,
977e9a7ff3aSYishai Hadas 							   MLX4_CMD_TIME_CLASS_C,
978e9a7ff3aSYishai Hadas 							   MLX4_CMD_NATIVE);
979e9a7ff3aSYishai Hadas 					if (err)
9800a9a0188SJack Morgenstein 						return err;
981e9a7ff3aSYishai Hadas 					mlx4_set_admin_guid(dev,
982e9a7ff3aSYishai Hadas 							    *(__be64 *)outsmp->
983e9a7ff3aSYishai Hadas 							    data, slave, port);
984e9a7ff3aSYishai Hadas 				} else {
985e9a7ff3aSYishai Hadas 					memcpy(outsmp->data, &guid, 8);
986e9a7ff3aSYishai Hadas 				}
987e9a7ff3aSYishai Hadas 
988e9a7ff3aSYishai Hadas 				/* clean all other gids */
989e9a7ff3aSYishai Hadas 				memset(outsmp->data + 8, 0, 56);
990e9a7ff3aSYishai Hadas 				return 0;
9910a9a0188SJack Morgenstein 			}
992afa8fd1dSJack Morgenstein 			if (smp->attr_id == IB_SMP_ATTR_NODE_INFO) {
993afa8fd1dSJack Morgenstein 				err = mlx4_cmd_box(dev, inbox->dma, outbox->dma,
9947c35ef45SOr Gerlitz 					     port, opcode_modifier,
995afa8fd1dSJack Morgenstein 					     vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
996afa8fd1dSJack Morgenstein 				if (!err) {
997afa8fd1dSJack Morgenstein 					slave_node_guid =  mlx4_get_slave_node_guid(dev, slave);
998afa8fd1dSJack Morgenstein 					memcpy(outsmp->data + 12, &slave_node_guid, 8);
999afa8fd1dSJack Morgenstein 				}
1000afa8fd1dSJack Morgenstein 				return err;
1001afa8fd1dSJack Morgenstein 			}
10020a9a0188SJack Morgenstein 		}
10030a9a0188SJack Morgenstein 	}
100497982f5aSJack Morgenstein 
100597982f5aSJack Morgenstein 	/* Non-privileged VFs are only allowed "host" view LID-routed 'Get' MADs.
100697982f5aSJack Morgenstein 	 * These are the MADs used by ib verbs (such as ib_query_gids).
100797982f5aSJack Morgenstein 	 */
10080a9a0188SJack Morgenstein 	if (slave != mlx4_master_func_num(dev) &&
100997982f5aSJack Morgenstein 	    !mlx4_vf_smi_enabled(dev, slave, port)) {
101097982f5aSJack Morgenstein 		if (!(smp->mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED &&
101197982f5aSJack Morgenstein 		      smp->method == IB_MGMT_METHOD_GET) || network_view) {
101297982f5aSJack Morgenstein 			mlx4_err(dev, "Unprivileged slave %d is trying to execute a Subnet MGMT MAD, class 0x%x, method 0x%x, view=%s for attr 0x%x. Rejecting\n",
101373d4da7bSWengang Wang 				 slave, smp->mgmt_class, smp->method,
101497982f5aSJack Morgenstein 				 network_view ? "Network" : "Host",
10150a9a0188SJack Morgenstein 				 be16_to_cpu(smp->attr_id));
10160a9a0188SJack Morgenstein 			return -EPERM;
10170a9a0188SJack Morgenstein 		}
101897982f5aSJack Morgenstein 	}
101997982f5aSJack Morgenstein 
10200a9a0188SJack Morgenstein 	return mlx4_cmd_box(dev, inbox->dma, outbox->dma,
102197982f5aSJack Morgenstein 				    vhcr->in_modifier, opcode_modifier,
10220a9a0188SJack Morgenstein 				    vhcr->op, MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
10230a9a0188SJack Morgenstein }
10240a9a0188SJack Morgenstein 
mlx4_CMD_EPERM_wrapper(struct mlx4_dev * dev,int slave,struct mlx4_vhcr * vhcr,struct mlx4_cmd_mailbox * inbox,struct mlx4_cmd_mailbox * outbox,struct mlx4_cmd_info * cmd)1025b7475794SOr Gerlitz static int mlx4_CMD_EPERM_wrapper(struct mlx4_dev *dev, int slave,
1026fe6f700dSYevgeny Petrilin 		     struct mlx4_vhcr *vhcr,
1027fe6f700dSYevgeny Petrilin 		     struct mlx4_cmd_mailbox *inbox,
1028fe6f700dSYevgeny Petrilin 		     struct mlx4_cmd_mailbox *outbox,
1029fe6f700dSYevgeny Petrilin 		     struct mlx4_cmd_info *cmd)
1030fe6f700dSYevgeny Petrilin {
1031fe6f700dSYevgeny Petrilin 	return -EPERM;
1032fe6f700dSYevgeny Petrilin }
1033fe6f700dSYevgeny Petrilin 
mlx4_DMA_wrapper(struct mlx4_dev * dev,int slave,struct mlx4_vhcr * vhcr,struct mlx4_cmd_mailbox * inbox,struct mlx4_cmd_mailbox * outbox,struct mlx4_cmd_info * cmd)1034e8f081aaSYevgeny Petrilin int mlx4_DMA_wrapper(struct mlx4_dev *dev, int slave,
1035e8f081aaSYevgeny Petrilin 		     struct mlx4_vhcr *vhcr,
1036e8f081aaSYevgeny Petrilin 		     struct mlx4_cmd_mailbox *inbox,
1037e8f081aaSYevgeny Petrilin 		     struct mlx4_cmd_mailbox *outbox,
1038e8f081aaSYevgeny Petrilin 		     struct mlx4_cmd_info *cmd)
1039e8f081aaSYevgeny Petrilin {
1040e8f081aaSYevgeny Petrilin 	u64 in_param;
1041e8f081aaSYevgeny Petrilin 	u64 out_param;
1042e8f081aaSYevgeny Petrilin 	int err;
1043e8f081aaSYevgeny Petrilin 
1044e8f081aaSYevgeny Petrilin 	in_param = cmd->has_inbox ? (u64) inbox->dma : vhcr->in_param;
1045e8f081aaSYevgeny Petrilin 	out_param = cmd->has_outbox ? (u64) outbox->dma : vhcr->out_param;
1046e8f081aaSYevgeny Petrilin 	if (cmd->encode_slave_id) {
1047e8f081aaSYevgeny Petrilin 		in_param &= 0xffffffffffffff00ll;
1048e8f081aaSYevgeny Petrilin 		in_param |= slave;
1049e8f081aaSYevgeny Petrilin 	}
1050e8f081aaSYevgeny Petrilin 
1051e8f081aaSYevgeny Petrilin 	err = __mlx4_cmd(dev, in_param, &out_param, cmd->out_is_imm,
1052e8f081aaSYevgeny Petrilin 			 vhcr->in_modifier, vhcr->op_modifier, vhcr->op,
1053e8f081aaSYevgeny Petrilin 			 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
1054e8f081aaSYevgeny Petrilin 
1055e8f081aaSYevgeny Petrilin 	if (cmd->out_is_imm)
1056e8f081aaSYevgeny Petrilin 		vhcr->out_param = out_param;
1057e8f081aaSYevgeny Petrilin 
1058e8f081aaSYevgeny Petrilin 	return err;
1059e8f081aaSYevgeny Petrilin }
1060e8f081aaSYevgeny Petrilin 
1061e8f081aaSYevgeny Petrilin static struct mlx4_cmd_info cmd_info[] = {
1062e8f081aaSYevgeny Petrilin 	{
1063e8f081aaSYevgeny Petrilin 		.opcode = MLX4_CMD_QUERY_FW,
1064e8f081aaSYevgeny Petrilin 		.has_inbox = false,
1065e8f081aaSYevgeny Petrilin 		.has_outbox = true,
1066e8f081aaSYevgeny Petrilin 		.out_is_imm = false,
1067e8f081aaSYevgeny Petrilin 		.encode_slave_id = false,
1068e8f081aaSYevgeny Petrilin 		.verify = NULL,
1069b91cb3ebSJack Morgenstein 		.wrapper = mlx4_QUERY_FW_wrapper
1070e8f081aaSYevgeny Petrilin 	},
1071e8f081aaSYevgeny Petrilin 	{
1072e8f081aaSYevgeny Petrilin 		.opcode = MLX4_CMD_QUERY_HCA,
1073e8f081aaSYevgeny Petrilin 		.has_inbox = false,
1074e8f081aaSYevgeny Petrilin 		.has_outbox = true,
1075e8f081aaSYevgeny Petrilin 		.out_is_imm = false,
1076e8f081aaSYevgeny Petrilin 		.encode_slave_id = false,
1077e8f081aaSYevgeny Petrilin 		.verify = NULL,
1078e8f081aaSYevgeny Petrilin 		.wrapper = NULL
1079e8f081aaSYevgeny Petrilin 	},
1080e8f081aaSYevgeny Petrilin 	{
1081e8f081aaSYevgeny Petrilin 		.opcode = MLX4_CMD_QUERY_DEV_CAP,
1082e8f081aaSYevgeny Petrilin 		.has_inbox = false,
1083e8f081aaSYevgeny Petrilin 		.has_outbox = true,
1084e8f081aaSYevgeny Petrilin 		.out_is_imm = false,
1085e8f081aaSYevgeny Petrilin 		.encode_slave_id = false,
1086e8f081aaSYevgeny Petrilin 		.verify = NULL,
1087b91cb3ebSJack Morgenstein 		.wrapper = mlx4_QUERY_DEV_CAP_wrapper
1088e8f081aaSYevgeny Petrilin 	},
1089c82e9aa0SEli Cohen 	{
1090c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_QUERY_FUNC_CAP,
1091c82e9aa0SEli Cohen 		.has_inbox = false,
1092c82e9aa0SEli Cohen 		.has_outbox = true,
1093c82e9aa0SEli Cohen 		.out_is_imm = false,
1094c82e9aa0SEli Cohen 		.encode_slave_id = false,
1095c82e9aa0SEli Cohen 		.verify = NULL,
1096c82e9aa0SEli Cohen 		.wrapper = mlx4_QUERY_FUNC_CAP_wrapper
1097c82e9aa0SEli Cohen 	},
1098c82e9aa0SEli Cohen 	{
1099c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_QUERY_ADAPTER,
1100c82e9aa0SEli Cohen 		.has_inbox = false,
1101c82e9aa0SEli Cohen 		.has_outbox = true,
1102c82e9aa0SEli Cohen 		.out_is_imm = false,
1103c82e9aa0SEli Cohen 		.encode_slave_id = false,
1104c82e9aa0SEli Cohen 		.verify = NULL,
1105c82e9aa0SEli Cohen 		.wrapper = NULL
1106c82e9aa0SEli Cohen 	},
1107c82e9aa0SEli Cohen 	{
1108c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_INIT_PORT,
1109c82e9aa0SEli Cohen 		.has_inbox = false,
1110c82e9aa0SEli Cohen 		.has_outbox = false,
1111c82e9aa0SEli Cohen 		.out_is_imm = false,
1112c82e9aa0SEli Cohen 		.encode_slave_id = false,
1113c82e9aa0SEli Cohen 		.verify = NULL,
1114c82e9aa0SEli Cohen 		.wrapper = mlx4_INIT_PORT_wrapper
1115c82e9aa0SEli Cohen 	},
1116c82e9aa0SEli Cohen 	{
1117c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_CLOSE_PORT,
1118c82e9aa0SEli Cohen 		.has_inbox = false,
1119c82e9aa0SEli Cohen 		.has_outbox = false,
1120c82e9aa0SEli Cohen 		.out_is_imm  = false,
1121c82e9aa0SEli Cohen 		.encode_slave_id = false,
1122c82e9aa0SEli Cohen 		.verify = NULL,
1123c82e9aa0SEli Cohen 		.wrapper = mlx4_CLOSE_PORT_wrapper
1124c82e9aa0SEli Cohen 	},
1125c82e9aa0SEli Cohen 	{
1126c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_QUERY_PORT,
1127c82e9aa0SEli Cohen 		.has_inbox = false,
1128c82e9aa0SEli Cohen 		.has_outbox = true,
1129c82e9aa0SEli Cohen 		.out_is_imm = false,
1130c82e9aa0SEli Cohen 		.encode_slave_id = false,
1131c82e9aa0SEli Cohen 		.verify = NULL,
1132c82e9aa0SEli Cohen 		.wrapper = mlx4_QUERY_PORT_wrapper
1133c82e9aa0SEli Cohen 	},
1134c82e9aa0SEli Cohen 	{
1135ffe455adSEugenia Emantayev 		.opcode = MLX4_CMD_SET_PORT,
1136ffe455adSEugenia Emantayev 		.has_inbox = true,
1137ffe455adSEugenia Emantayev 		.has_outbox = false,
1138ffe455adSEugenia Emantayev 		.out_is_imm = false,
1139ffe455adSEugenia Emantayev 		.encode_slave_id = false,
1140ffe455adSEugenia Emantayev 		.verify = NULL,
1141ffe455adSEugenia Emantayev 		.wrapper = mlx4_SET_PORT_wrapper
1142ffe455adSEugenia Emantayev 	},
1143ffe455adSEugenia Emantayev 	{
1144c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_MAP_EQ,
1145c82e9aa0SEli Cohen 		.has_inbox = false,
1146c82e9aa0SEli Cohen 		.has_outbox = false,
1147c82e9aa0SEli Cohen 		.out_is_imm = false,
1148c82e9aa0SEli Cohen 		.encode_slave_id = false,
1149c82e9aa0SEli Cohen 		.verify = NULL,
1150c82e9aa0SEli Cohen 		.wrapper = mlx4_MAP_EQ_wrapper
1151c82e9aa0SEli Cohen 	},
1152c82e9aa0SEli Cohen 	{
1153c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_SW2HW_EQ,
1154c82e9aa0SEli Cohen 		.has_inbox = true,
1155c82e9aa0SEli Cohen 		.has_outbox = false,
1156c82e9aa0SEli Cohen 		.out_is_imm = false,
1157c82e9aa0SEli Cohen 		.encode_slave_id = true,
1158c82e9aa0SEli Cohen 		.verify = NULL,
1159c82e9aa0SEli Cohen 		.wrapper = mlx4_SW2HW_EQ_wrapper
1160c82e9aa0SEli Cohen 	},
1161c82e9aa0SEli Cohen 	{
1162c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_HW_HEALTH_CHECK,
1163c82e9aa0SEli Cohen 		.has_inbox = false,
1164c82e9aa0SEli Cohen 		.has_outbox = false,
1165c82e9aa0SEli Cohen 		.out_is_imm = false,
1166c82e9aa0SEli Cohen 		.encode_slave_id = false,
1167c82e9aa0SEli Cohen 		.verify = NULL,
1168c82e9aa0SEli Cohen 		.wrapper = NULL
1169c82e9aa0SEli Cohen 	},
1170c82e9aa0SEli Cohen 	{
1171c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_NOP,
1172c82e9aa0SEli Cohen 		.has_inbox = false,
1173c82e9aa0SEli Cohen 		.has_outbox = false,
1174c82e9aa0SEli Cohen 		.out_is_imm = false,
1175c82e9aa0SEli Cohen 		.encode_slave_id = false,
1176c82e9aa0SEli Cohen 		.verify = NULL,
1177c82e9aa0SEli Cohen 		.wrapper = NULL
1178c82e9aa0SEli Cohen 	},
1179c82e9aa0SEli Cohen 	{
1180d18f141aSOr Gerlitz 		.opcode = MLX4_CMD_CONFIG_DEV,
1181d18f141aSOr Gerlitz 		.has_inbox = false,
1182d475c95bSMatan Barak 		.has_outbox = true,
1183d18f141aSOr Gerlitz 		.out_is_imm = false,
1184d18f141aSOr Gerlitz 		.encode_slave_id = false,
1185d18f141aSOr Gerlitz 		.verify = NULL,
1186d475c95bSMatan Barak 		.wrapper = mlx4_CONFIG_DEV_wrapper
1187d18f141aSOr Gerlitz 	},
1188d18f141aSOr Gerlitz 	{
1189c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_ALLOC_RES,
1190c82e9aa0SEli Cohen 		.has_inbox = false,
1191c82e9aa0SEli Cohen 		.has_outbox = false,
1192c82e9aa0SEli Cohen 		.out_is_imm = true,
1193c82e9aa0SEli Cohen 		.encode_slave_id = false,
1194c82e9aa0SEli Cohen 		.verify = NULL,
1195c82e9aa0SEli Cohen 		.wrapper = mlx4_ALLOC_RES_wrapper
1196c82e9aa0SEli Cohen 	},
1197c82e9aa0SEli Cohen 	{
1198c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_FREE_RES,
1199c82e9aa0SEli Cohen 		.has_inbox = false,
1200c82e9aa0SEli Cohen 		.has_outbox = false,
1201c82e9aa0SEli Cohen 		.out_is_imm = false,
1202c82e9aa0SEli Cohen 		.encode_slave_id = false,
1203c82e9aa0SEli Cohen 		.verify = NULL,
1204c82e9aa0SEli Cohen 		.wrapper = mlx4_FREE_RES_wrapper
1205c82e9aa0SEli Cohen 	},
1206c82e9aa0SEli Cohen 	{
1207c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_SW2HW_MPT,
1208c82e9aa0SEli Cohen 		.has_inbox = true,
1209c82e9aa0SEli Cohen 		.has_outbox = false,
1210c82e9aa0SEli Cohen 		.out_is_imm = false,
1211c82e9aa0SEli Cohen 		.encode_slave_id = true,
1212c82e9aa0SEli Cohen 		.verify = NULL,
1213c82e9aa0SEli Cohen 		.wrapper = mlx4_SW2HW_MPT_wrapper
1214c82e9aa0SEli Cohen 	},
1215c82e9aa0SEli Cohen 	{
1216c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_QUERY_MPT,
1217c82e9aa0SEli Cohen 		.has_inbox = false,
1218c82e9aa0SEli Cohen 		.has_outbox = true,
1219c82e9aa0SEli Cohen 		.out_is_imm = false,
1220c82e9aa0SEli Cohen 		.encode_slave_id = false,
1221c82e9aa0SEli Cohen 		.verify = NULL,
1222c82e9aa0SEli Cohen 		.wrapper = mlx4_QUERY_MPT_wrapper
1223c82e9aa0SEli Cohen 	},
1224c82e9aa0SEli Cohen 	{
1225c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_HW2SW_MPT,
1226c82e9aa0SEli Cohen 		.has_inbox = false,
1227c82e9aa0SEli Cohen 		.has_outbox = false,
1228c82e9aa0SEli Cohen 		.out_is_imm = false,
1229c82e9aa0SEli Cohen 		.encode_slave_id = false,
1230c82e9aa0SEli Cohen 		.verify = NULL,
1231c82e9aa0SEli Cohen 		.wrapper = mlx4_HW2SW_MPT_wrapper
1232c82e9aa0SEli Cohen 	},
1233c82e9aa0SEli Cohen 	{
1234c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_READ_MTT,
1235c82e9aa0SEli Cohen 		.has_inbox = false,
1236c82e9aa0SEli Cohen 		.has_outbox = true,
1237c82e9aa0SEli Cohen 		.out_is_imm = false,
1238c82e9aa0SEli Cohen 		.encode_slave_id = false,
1239c82e9aa0SEli Cohen 		.verify = NULL,
1240c82e9aa0SEli Cohen 		.wrapper = NULL
1241c82e9aa0SEli Cohen 	},
1242c82e9aa0SEli Cohen 	{
1243c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_WRITE_MTT,
1244c82e9aa0SEli Cohen 		.has_inbox = true,
1245c82e9aa0SEli Cohen 		.has_outbox = false,
1246c82e9aa0SEli Cohen 		.out_is_imm = false,
1247c82e9aa0SEli Cohen 		.encode_slave_id = false,
1248c82e9aa0SEli Cohen 		.verify = NULL,
1249c82e9aa0SEli Cohen 		.wrapper = mlx4_WRITE_MTT_wrapper
1250c82e9aa0SEli Cohen 	},
1251c82e9aa0SEli Cohen 	{
1252c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_SYNC_TPT,
1253c82e9aa0SEli Cohen 		.has_inbox = true,
1254c82e9aa0SEli Cohen 		.has_outbox = false,
1255c82e9aa0SEli Cohen 		.out_is_imm = false,
1256c82e9aa0SEli Cohen 		.encode_slave_id = false,
1257c82e9aa0SEli Cohen 		.verify = NULL,
1258c82e9aa0SEli Cohen 		.wrapper = NULL
1259c82e9aa0SEli Cohen 	},
1260c82e9aa0SEli Cohen 	{
1261c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_HW2SW_EQ,
1262c82e9aa0SEli Cohen 		.has_inbox = false,
126330a5da5bSJack Morgenstein 		.has_outbox = false,
1264c82e9aa0SEli Cohen 		.out_is_imm = false,
1265c82e9aa0SEli Cohen 		.encode_slave_id = true,
1266c82e9aa0SEli Cohen 		.verify = NULL,
1267c82e9aa0SEli Cohen 		.wrapper = mlx4_HW2SW_EQ_wrapper
1268c82e9aa0SEli Cohen 	},
1269c82e9aa0SEli Cohen 	{
1270c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_QUERY_EQ,
1271c82e9aa0SEli Cohen 		.has_inbox = false,
1272c82e9aa0SEli Cohen 		.has_outbox = true,
1273c82e9aa0SEli Cohen 		.out_is_imm = false,
1274c82e9aa0SEli Cohen 		.encode_slave_id = true,
1275c82e9aa0SEli Cohen 		.verify = NULL,
1276c82e9aa0SEli Cohen 		.wrapper = mlx4_QUERY_EQ_wrapper
1277c82e9aa0SEli Cohen 	},
1278c82e9aa0SEli Cohen 	{
1279c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_SW2HW_CQ,
1280c82e9aa0SEli Cohen 		.has_inbox = true,
1281c82e9aa0SEli Cohen 		.has_outbox = false,
1282c82e9aa0SEli Cohen 		.out_is_imm = false,
1283c82e9aa0SEli Cohen 		.encode_slave_id = true,
1284c82e9aa0SEli Cohen 		.verify = NULL,
1285c82e9aa0SEli Cohen 		.wrapper = mlx4_SW2HW_CQ_wrapper
1286c82e9aa0SEli Cohen 	},
1287c82e9aa0SEli Cohen 	{
1288c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_HW2SW_CQ,
1289c82e9aa0SEli Cohen 		.has_inbox = false,
1290c82e9aa0SEli Cohen 		.has_outbox = false,
1291c82e9aa0SEli Cohen 		.out_is_imm = false,
1292c82e9aa0SEli Cohen 		.encode_slave_id = false,
1293c82e9aa0SEli Cohen 		.verify = NULL,
1294c82e9aa0SEli Cohen 		.wrapper = mlx4_HW2SW_CQ_wrapper
1295c82e9aa0SEli Cohen 	},
1296c82e9aa0SEli Cohen 	{
1297c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_QUERY_CQ,
1298c82e9aa0SEli Cohen 		.has_inbox = false,
1299c82e9aa0SEli Cohen 		.has_outbox = true,
1300c82e9aa0SEli Cohen 		.out_is_imm = false,
1301c82e9aa0SEli Cohen 		.encode_slave_id = false,
1302c82e9aa0SEli Cohen 		.verify = NULL,
1303c82e9aa0SEli Cohen 		.wrapper = mlx4_QUERY_CQ_wrapper
1304c82e9aa0SEli Cohen 	},
1305c82e9aa0SEli Cohen 	{
1306c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_MODIFY_CQ,
1307c82e9aa0SEli Cohen 		.has_inbox = true,
1308c82e9aa0SEli Cohen 		.has_outbox = false,
1309c82e9aa0SEli Cohen 		.out_is_imm = true,
1310c82e9aa0SEli Cohen 		.encode_slave_id = false,
1311c82e9aa0SEli Cohen 		.verify = NULL,
1312c82e9aa0SEli Cohen 		.wrapper = mlx4_MODIFY_CQ_wrapper
1313c82e9aa0SEli Cohen 	},
1314c82e9aa0SEli Cohen 	{
1315c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_SW2HW_SRQ,
1316c82e9aa0SEli Cohen 		.has_inbox = true,
1317c82e9aa0SEli Cohen 		.has_outbox = false,
1318c82e9aa0SEli Cohen 		.out_is_imm = false,
1319c82e9aa0SEli Cohen 		.encode_slave_id = true,
1320c82e9aa0SEli Cohen 		.verify = NULL,
1321c82e9aa0SEli Cohen 		.wrapper = mlx4_SW2HW_SRQ_wrapper
1322c82e9aa0SEli Cohen 	},
1323c82e9aa0SEli Cohen 	{
1324c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_HW2SW_SRQ,
1325c82e9aa0SEli Cohen 		.has_inbox = false,
1326c82e9aa0SEli Cohen 		.has_outbox = false,
1327c82e9aa0SEli Cohen 		.out_is_imm = false,
1328c82e9aa0SEli Cohen 		.encode_slave_id = false,
1329c82e9aa0SEli Cohen 		.verify = NULL,
1330c82e9aa0SEli Cohen 		.wrapper = mlx4_HW2SW_SRQ_wrapper
1331c82e9aa0SEli Cohen 	},
1332c82e9aa0SEli Cohen 	{
1333c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_QUERY_SRQ,
1334c82e9aa0SEli Cohen 		.has_inbox = false,
1335c82e9aa0SEli Cohen 		.has_outbox = true,
1336c82e9aa0SEli Cohen 		.out_is_imm = false,
1337c82e9aa0SEli Cohen 		.encode_slave_id = false,
1338c82e9aa0SEli Cohen 		.verify = NULL,
1339c82e9aa0SEli Cohen 		.wrapper = mlx4_QUERY_SRQ_wrapper
1340c82e9aa0SEli Cohen 	},
1341c82e9aa0SEli Cohen 	{
1342c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_ARM_SRQ,
1343c82e9aa0SEli Cohen 		.has_inbox = false,
1344c82e9aa0SEli Cohen 		.has_outbox = false,
1345c82e9aa0SEli Cohen 		.out_is_imm = false,
1346c82e9aa0SEli Cohen 		.encode_slave_id = false,
1347c82e9aa0SEli Cohen 		.verify = NULL,
1348c82e9aa0SEli Cohen 		.wrapper = mlx4_ARM_SRQ_wrapper
1349c82e9aa0SEli Cohen 	},
1350c82e9aa0SEli Cohen 	{
1351c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_RST2INIT_QP,
1352c82e9aa0SEli Cohen 		.has_inbox = true,
1353c82e9aa0SEli Cohen 		.has_outbox = false,
1354c82e9aa0SEli Cohen 		.out_is_imm = false,
1355c82e9aa0SEli Cohen 		.encode_slave_id = true,
1356c82e9aa0SEli Cohen 		.verify = NULL,
1357c82e9aa0SEli Cohen 		.wrapper = mlx4_RST2INIT_QP_wrapper
1358c82e9aa0SEli Cohen 	},
1359c82e9aa0SEli Cohen 	{
1360c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_INIT2INIT_QP,
1361c82e9aa0SEli Cohen 		.has_inbox = true,
1362c82e9aa0SEli Cohen 		.has_outbox = false,
1363c82e9aa0SEli Cohen 		.out_is_imm = false,
1364c82e9aa0SEli Cohen 		.encode_slave_id = false,
1365c82e9aa0SEli Cohen 		.verify = NULL,
136654679e14SJack Morgenstein 		.wrapper = mlx4_INIT2INIT_QP_wrapper
1367c82e9aa0SEli Cohen 	},
1368c82e9aa0SEli Cohen 	{
1369c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_INIT2RTR_QP,
1370c82e9aa0SEli Cohen 		.has_inbox = true,
1371c82e9aa0SEli Cohen 		.has_outbox = false,
1372c82e9aa0SEli Cohen 		.out_is_imm = false,
1373c82e9aa0SEli Cohen 		.encode_slave_id = false,
1374c82e9aa0SEli Cohen 		.verify = NULL,
1375c82e9aa0SEli Cohen 		.wrapper = mlx4_INIT2RTR_QP_wrapper
1376c82e9aa0SEli Cohen 	},
1377c82e9aa0SEli Cohen 	{
1378c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_RTR2RTS_QP,
1379c82e9aa0SEli Cohen 		.has_inbox = true,
1380c82e9aa0SEli Cohen 		.has_outbox = false,
1381c82e9aa0SEli Cohen 		.out_is_imm = false,
1382c82e9aa0SEli Cohen 		.encode_slave_id = false,
1383c82e9aa0SEli Cohen 		.verify = NULL,
138454679e14SJack Morgenstein 		.wrapper = mlx4_RTR2RTS_QP_wrapper
1385c82e9aa0SEli Cohen 	},
1386c82e9aa0SEli Cohen 	{
1387c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_RTS2RTS_QP,
1388c82e9aa0SEli Cohen 		.has_inbox = true,
1389c82e9aa0SEli Cohen 		.has_outbox = false,
1390c82e9aa0SEli Cohen 		.out_is_imm = false,
1391c82e9aa0SEli Cohen 		.encode_slave_id = false,
1392c82e9aa0SEli Cohen 		.verify = NULL,
139354679e14SJack Morgenstein 		.wrapper = mlx4_RTS2RTS_QP_wrapper
1394c82e9aa0SEli Cohen 	},
1395c82e9aa0SEli Cohen 	{
1396c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_SQERR2RTS_QP,
1397c82e9aa0SEli Cohen 		.has_inbox = true,
1398c82e9aa0SEli Cohen 		.has_outbox = false,
1399c82e9aa0SEli Cohen 		.out_is_imm = false,
1400c82e9aa0SEli Cohen 		.encode_slave_id = false,
1401c82e9aa0SEli Cohen 		.verify = NULL,
140254679e14SJack Morgenstein 		.wrapper = mlx4_SQERR2RTS_QP_wrapper
1403c82e9aa0SEli Cohen 	},
1404c82e9aa0SEli Cohen 	{
1405c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_2ERR_QP,
1406c82e9aa0SEli Cohen 		.has_inbox = false,
1407c82e9aa0SEli Cohen 		.has_outbox = false,
1408c82e9aa0SEli Cohen 		.out_is_imm = false,
1409c82e9aa0SEli Cohen 		.encode_slave_id = false,
1410c82e9aa0SEli Cohen 		.verify = NULL,
1411c82e9aa0SEli Cohen 		.wrapper = mlx4_GEN_QP_wrapper
1412c82e9aa0SEli Cohen 	},
1413c82e9aa0SEli Cohen 	{
1414c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_RTS2SQD_QP,
1415c82e9aa0SEli Cohen 		.has_inbox = false,
1416c82e9aa0SEli Cohen 		.has_outbox = false,
1417c82e9aa0SEli Cohen 		.out_is_imm = false,
1418c82e9aa0SEli Cohen 		.encode_slave_id = false,
1419c82e9aa0SEli Cohen 		.verify = NULL,
1420c82e9aa0SEli Cohen 		.wrapper = mlx4_GEN_QP_wrapper
1421c82e9aa0SEli Cohen 	},
1422c82e9aa0SEli Cohen 	{
1423c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_SQD2SQD_QP,
1424c82e9aa0SEli Cohen 		.has_inbox = true,
1425c82e9aa0SEli Cohen 		.has_outbox = false,
1426c82e9aa0SEli Cohen 		.out_is_imm = false,
1427c82e9aa0SEli Cohen 		.encode_slave_id = false,
1428c82e9aa0SEli Cohen 		.verify = NULL,
142954679e14SJack Morgenstein 		.wrapper = mlx4_SQD2SQD_QP_wrapper
1430c82e9aa0SEli Cohen 	},
1431c82e9aa0SEli Cohen 	{
1432c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_SQD2RTS_QP,
1433c82e9aa0SEli Cohen 		.has_inbox = true,
1434c82e9aa0SEli Cohen 		.has_outbox = false,
1435c82e9aa0SEli Cohen 		.out_is_imm = false,
1436c82e9aa0SEli Cohen 		.encode_slave_id = false,
1437c82e9aa0SEli Cohen 		.verify = NULL,
143854679e14SJack Morgenstein 		.wrapper = mlx4_SQD2RTS_QP_wrapper
1439c82e9aa0SEli Cohen 	},
1440c82e9aa0SEli Cohen 	{
1441c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_2RST_QP,
1442c82e9aa0SEli Cohen 		.has_inbox = false,
1443c82e9aa0SEli Cohen 		.has_outbox = false,
1444c82e9aa0SEli Cohen 		.out_is_imm = false,
1445c82e9aa0SEli Cohen 		.encode_slave_id = false,
1446c82e9aa0SEli Cohen 		.verify = NULL,
1447c82e9aa0SEli Cohen 		.wrapper = mlx4_2RST_QP_wrapper
1448c82e9aa0SEli Cohen 	},
1449c82e9aa0SEli Cohen 	{
1450c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_QUERY_QP,
1451c82e9aa0SEli Cohen 		.has_inbox = false,
1452c82e9aa0SEli Cohen 		.has_outbox = true,
1453c82e9aa0SEli Cohen 		.out_is_imm = false,
1454c82e9aa0SEli Cohen 		.encode_slave_id = false,
1455c82e9aa0SEli Cohen 		.verify = NULL,
1456c82e9aa0SEli Cohen 		.wrapper = mlx4_GEN_QP_wrapper
1457c82e9aa0SEli Cohen 	},
1458c82e9aa0SEli Cohen 	{
1459c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_SUSPEND_QP,
1460c82e9aa0SEli Cohen 		.has_inbox = false,
1461c82e9aa0SEli Cohen 		.has_outbox = false,
1462c82e9aa0SEli Cohen 		.out_is_imm = false,
1463c82e9aa0SEli Cohen 		.encode_slave_id = false,
1464c82e9aa0SEli Cohen 		.verify = NULL,
1465c82e9aa0SEli Cohen 		.wrapper = mlx4_GEN_QP_wrapper
1466c82e9aa0SEli Cohen 	},
1467c82e9aa0SEli Cohen 	{
1468c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_UNSUSPEND_QP,
1469c82e9aa0SEli Cohen 		.has_inbox = false,
1470c82e9aa0SEli Cohen 		.has_outbox = false,
1471c82e9aa0SEli Cohen 		.out_is_imm = false,
1472c82e9aa0SEli Cohen 		.encode_slave_id = false,
1473c82e9aa0SEli Cohen 		.verify = NULL,
1474c82e9aa0SEli Cohen 		.wrapper = mlx4_GEN_QP_wrapper
1475c82e9aa0SEli Cohen 	},
1476c82e9aa0SEli Cohen 	{
1477b01978caSJack Morgenstein 		.opcode = MLX4_CMD_UPDATE_QP,
1478ce8d9e0dSMatan Barak 		.has_inbox = true,
1479b01978caSJack Morgenstein 		.has_outbox = false,
1480b01978caSJack Morgenstein 		.out_is_imm = false,
1481b01978caSJack Morgenstein 		.encode_slave_id = false,
1482b01978caSJack Morgenstein 		.verify = NULL,
1483ce8d9e0dSMatan Barak 		.wrapper = mlx4_UPDATE_QP_wrapper
1484b01978caSJack Morgenstein 	},
1485b01978caSJack Morgenstein 	{
1486fe6f700dSYevgeny Petrilin 		.opcode = MLX4_CMD_GET_OP_REQ,
1487fe6f700dSYevgeny Petrilin 		.has_inbox = false,
1488fe6f700dSYevgeny Petrilin 		.has_outbox = false,
1489fe6f700dSYevgeny Petrilin 		.out_is_imm = false,
1490fe6f700dSYevgeny Petrilin 		.encode_slave_id = false,
1491fe6f700dSYevgeny Petrilin 		.verify = NULL,
1492b7475794SOr Gerlitz 		.wrapper = mlx4_CMD_EPERM_wrapper,
1493fe6f700dSYevgeny Petrilin 	},
1494fe6f700dSYevgeny Petrilin 	{
14957e95bb99SIdo Shamay 		.opcode = MLX4_CMD_ALLOCATE_VPP,
14967e95bb99SIdo Shamay 		.has_inbox = false,
14977e95bb99SIdo Shamay 		.has_outbox = true,
14987e95bb99SIdo Shamay 		.out_is_imm = false,
14997e95bb99SIdo Shamay 		.encode_slave_id = false,
15007e95bb99SIdo Shamay 		.verify = NULL,
15017e95bb99SIdo Shamay 		.wrapper = mlx4_CMD_EPERM_wrapper,
15027e95bb99SIdo Shamay 	},
15037e95bb99SIdo Shamay 	{
15041c29146dSIdo Shamay 		.opcode = MLX4_CMD_SET_VPORT_QOS,
15051c29146dSIdo Shamay 		.has_inbox = false,
15061c29146dSIdo Shamay 		.has_outbox = true,
15071c29146dSIdo Shamay 		.out_is_imm = false,
15081c29146dSIdo Shamay 		.encode_slave_id = false,
15091c29146dSIdo Shamay 		.verify = NULL,
15101c29146dSIdo Shamay 		.wrapper = mlx4_CMD_EPERM_wrapper,
15111c29146dSIdo Shamay 	},
15121c29146dSIdo Shamay 	{
15130a9a0188SJack Morgenstein 		.opcode = MLX4_CMD_CONF_SPECIAL_QP,
15140a9a0188SJack Morgenstein 		.has_inbox = false,
15150a9a0188SJack Morgenstein 		.has_outbox = false,
15160a9a0188SJack Morgenstein 		.out_is_imm = false,
15170a9a0188SJack Morgenstein 		.encode_slave_id = false,
15180a9a0188SJack Morgenstein 		.verify = NULL, /* XXX verify: only demux can do this */
15190a9a0188SJack Morgenstein 		.wrapper = NULL
15200a9a0188SJack Morgenstein 	},
15210a9a0188SJack Morgenstein 	{
15220a9a0188SJack Morgenstein 		.opcode = MLX4_CMD_MAD_IFC,
15230a9a0188SJack Morgenstein 		.has_inbox = true,
15240a9a0188SJack Morgenstein 		.has_outbox = true,
15250a9a0188SJack Morgenstein 		.out_is_imm = false,
15260a9a0188SJack Morgenstein 		.encode_slave_id = false,
15270a9a0188SJack Morgenstein 		.verify = NULL,
15280a9a0188SJack Morgenstein 		.wrapper = mlx4_MAD_IFC_wrapper
15290a9a0188SJack Morgenstein 	},
15300a9a0188SJack Morgenstein 	{
1531114840c3SJack Morgenstein 		.opcode = MLX4_CMD_MAD_DEMUX,
1532114840c3SJack Morgenstein 		.has_inbox = false,
1533114840c3SJack Morgenstein 		.has_outbox = false,
1534114840c3SJack Morgenstein 		.out_is_imm = false,
1535114840c3SJack Morgenstein 		.encode_slave_id = false,
1536114840c3SJack Morgenstein 		.verify = NULL,
1537114840c3SJack Morgenstein 		.wrapper = mlx4_CMD_EPERM_wrapper
1538114840c3SJack Morgenstein 	},
1539114840c3SJack Morgenstein 	{
1540c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_QUERY_IF_STAT,
1541c82e9aa0SEli Cohen 		.has_inbox = false,
1542c82e9aa0SEli Cohen 		.has_outbox = true,
1543c82e9aa0SEli Cohen 		.out_is_imm = false,
1544c82e9aa0SEli Cohen 		.encode_slave_id = false,
1545c82e9aa0SEli Cohen 		.verify = NULL,
1546c82e9aa0SEli Cohen 		.wrapper = mlx4_QUERY_IF_STAT_wrapper
1547c82e9aa0SEli Cohen 	},
1548adbc7ac5SSaeed Mahameed 	{
1549adbc7ac5SSaeed Mahameed 		.opcode = MLX4_CMD_ACCESS_REG,
1550adbc7ac5SSaeed Mahameed 		.has_inbox = true,
1551adbc7ac5SSaeed Mahameed 		.has_outbox = true,
1552adbc7ac5SSaeed Mahameed 		.out_is_imm = false,
1553adbc7ac5SSaeed Mahameed 		.encode_slave_id = false,
1554adbc7ac5SSaeed Mahameed 		.verify = NULL,
15556e806699SSaeed Mahameed 		.wrapper = mlx4_ACCESS_REG_wrapper,
1556adbc7ac5SSaeed Mahameed 	},
1557d237baa1SShani Michaeli 	{
1558d237baa1SShani Michaeli 		.opcode = MLX4_CMD_CONGESTION_CTRL_OPCODE,
1559d237baa1SShani Michaeli 		.has_inbox = false,
1560d237baa1SShani Michaeli 		.has_outbox = false,
1561d237baa1SShani Michaeli 		.out_is_imm = false,
1562d237baa1SShani Michaeli 		.encode_slave_id = false,
1563d237baa1SShani Michaeli 		.verify = NULL,
1564d237baa1SShani Michaeli 		.wrapper = mlx4_CMD_EPERM_wrapper,
1565d237baa1SShani Michaeli 	},
1566c82e9aa0SEli Cohen 	/* Native multicast commands are not available for guests */
1567c82e9aa0SEli Cohen 	{
1568c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_QP_ATTACH,
1569c82e9aa0SEli Cohen 		.has_inbox = true,
1570c82e9aa0SEli Cohen 		.has_outbox = false,
1571c82e9aa0SEli Cohen 		.out_is_imm = false,
1572c82e9aa0SEli Cohen 		.encode_slave_id = false,
1573c82e9aa0SEli Cohen 		.verify = NULL,
1574c82e9aa0SEli Cohen 		.wrapper = mlx4_QP_ATTACH_wrapper
1575c82e9aa0SEli Cohen 	},
1576c82e9aa0SEli Cohen 	{
15770ec2c0f8SEugenia Emantayev 		.opcode = MLX4_CMD_PROMISC,
15780ec2c0f8SEugenia Emantayev 		.has_inbox = false,
15790ec2c0f8SEugenia Emantayev 		.has_outbox = false,
15800ec2c0f8SEugenia Emantayev 		.out_is_imm = false,
15810ec2c0f8SEugenia Emantayev 		.encode_slave_id = false,
15820ec2c0f8SEugenia Emantayev 		.verify = NULL,
15830ec2c0f8SEugenia Emantayev 		.wrapper = mlx4_PROMISC_wrapper
15840ec2c0f8SEugenia Emantayev 	},
1585ffe455adSEugenia Emantayev 	/* Ethernet specific commands */
1586ffe455adSEugenia Emantayev 	{
1587ffe455adSEugenia Emantayev 		.opcode = MLX4_CMD_SET_VLAN_FLTR,
1588ffe455adSEugenia Emantayev 		.has_inbox = true,
1589ffe455adSEugenia Emantayev 		.has_outbox = false,
1590ffe455adSEugenia Emantayev 		.out_is_imm = false,
1591ffe455adSEugenia Emantayev 		.encode_slave_id = false,
1592ffe455adSEugenia Emantayev 		.verify = NULL,
1593ffe455adSEugenia Emantayev 		.wrapper = mlx4_SET_VLAN_FLTR_wrapper
1594ffe455adSEugenia Emantayev 	},
1595ffe455adSEugenia Emantayev 	{
1596ffe455adSEugenia Emantayev 		.opcode = MLX4_CMD_SET_MCAST_FLTR,
1597ffe455adSEugenia Emantayev 		.has_inbox = false,
1598ffe455adSEugenia Emantayev 		.has_outbox = false,
1599ffe455adSEugenia Emantayev 		.out_is_imm = false,
1600ffe455adSEugenia Emantayev 		.encode_slave_id = false,
1601ffe455adSEugenia Emantayev 		.verify = NULL,
1602ffe455adSEugenia Emantayev 		.wrapper = mlx4_SET_MCAST_FLTR_wrapper
1603ffe455adSEugenia Emantayev 	},
1604ffe455adSEugenia Emantayev 	{
1605ffe455adSEugenia Emantayev 		.opcode = MLX4_CMD_DUMP_ETH_STATS,
1606ffe455adSEugenia Emantayev 		.has_inbox = false,
1607ffe455adSEugenia Emantayev 		.has_outbox = true,
1608ffe455adSEugenia Emantayev 		.out_is_imm = false,
1609ffe455adSEugenia Emantayev 		.encode_slave_id = false,
1610ffe455adSEugenia Emantayev 		.verify = NULL,
1611ffe455adSEugenia Emantayev 		.wrapper = mlx4_DUMP_ETH_STATS_wrapper
1612ffe455adSEugenia Emantayev 	},
16130ec2c0f8SEugenia Emantayev 	{
1614c82e9aa0SEli Cohen 		.opcode = MLX4_CMD_INFORM_FLR_DONE,
1615c82e9aa0SEli Cohen 		.has_inbox = false,
1616c82e9aa0SEli Cohen 		.has_outbox = false,
1617c82e9aa0SEli Cohen 		.out_is_imm = false,
1618c82e9aa0SEli Cohen 		.encode_slave_id = false,
1619c82e9aa0SEli Cohen 		.verify = NULL,
1620c82e9aa0SEli Cohen 		.wrapper = NULL
1621c82e9aa0SEli Cohen 	},
16228fcfb4dbSHadar Hen Zion 	/* flow steering commands */
16238fcfb4dbSHadar Hen Zion 	{
16248fcfb4dbSHadar Hen Zion 		.opcode = MLX4_QP_FLOW_STEERING_ATTACH,
16258fcfb4dbSHadar Hen Zion 		.has_inbox = true,
16268fcfb4dbSHadar Hen Zion 		.has_outbox = false,
16278fcfb4dbSHadar Hen Zion 		.out_is_imm = true,
16288fcfb4dbSHadar Hen Zion 		.encode_slave_id = false,
16298fcfb4dbSHadar Hen Zion 		.verify = NULL,
16308fcfb4dbSHadar Hen Zion 		.wrapper = mlx4_QP_FLOW_STEERING_ATTACH_wrapper
16318fcfb4dbSHadar Hen Zion 	},
16328fcfb4dbSHadar Hen Zion 	{
16338fcfb4dbSHadar Hen Zion 		.opcode = MLX4_QP_FLOW_STEERING_DETACH,
16348fcfb4dbSHadar Hen Zion 		.has_inbox = false,
16358fcfb4dbSHadar Hen Zion 		.has_outbox = false,
16368fcfb4dbSHadar Hen Zion 		.out_is_imm = false,
16378fcfb4dbSHadar Hen Zion 		.encode_slave_id = false,
16388fcfb4dbSHadar Hen Zion 		.verify = NULL,
16398fcfb4dbSHadar Hen Zion 		.wrapper = mlx4_QP_FLOW_STEERING_DETACH_wrapper
16408fcfb4dbSHadar Hen Zion 	},
16414de65803SMatan Barak 	{
16424de65803SMatan Barak 		.opcode = MLX4_FLOW_STEERING_IB_UC_QP_RANGE,
16434de65803SMatan Barak 		.has_inbox = false,
16444de65803SMatan Barak 		.has_outbox = false,
16454de65803SMatan Barak 		.out_is_imm = false,
16464de65803SMatan Barak 		.encode_slave_id = false,
16474de65803SMatan Barak 		.verify = NULL,
1648b7475794SOr Gerlitz 		.wrapper = mlx4_CMD_EPERM_wrapper
16494de65803SMatan Barak 	},
165059e14e32SMoni Shoua 	{
165159e14e32SMoni Shoua 		.opcode = MLX4_CMD_VIRT_PORT_MAP,
165259e14e32SMoni Shoua 		.has_inbox = false,
165359e14e32SMoni Shoua 		.has_outbox = false,
165459e14e32SMoni Shoua 		.out_is_imm = false,
165559e14e32SMoni Shoua 		.encode_slave_id = false,
165659e14e32SMoni Shoua 		.verify = NULL,
165759e14e32SMoni Shoua 		.wrapper = mlx4_CMD_EPERM_wrapper
165859e14e32SMoni Shoua 	},
1659e8f081aaSYevgeny Petrilin };
1660e8f081aaSYevgeny Petrilin 
mlx4_master_process_vhcr(struct mlx4_dev * dev,int slave,struct mlx4_vhcr_cmd * in_vhcr)1661e8f081aaSYevgeny Petrilin static int mlx4_master_process_vhcr(struct mlx4_dev *dev, int slave,
1662e8f081aaSYevgeny Petrilin 				    struct mlx4_vhcr_cmd *in_vhcr)
1663e8f081aaSYevgeny Petrilin {
1664e8f081aaSYevgeny Petrilin 	struct mlx4_priv *priv = mlx4_priv(dev);
1665e8f081aaSYevgeny Petrilin 	struct mlx4_cmd_info *cmd = NULL;
1666e8f081aaSYevgeny Petrilin 	struct mlx4_vhcr_cmd *vhcr_cmd = in_vhcr ? in_vhcr : priv->mfunc.vhcr;
1667e8f081aaSYevgeny Petrilin 	struct mlx4_vhcr *vhcr;
1668e8f081aaSYevgeny Petrilin 	struct mlx4_cmd_mailbox *inbox = NULL;
1669e8f081aaSYevgeny Petrilin 	struct mlx4_cmd_mailbox *outbox = NULL;
1670e8f081aaSYevgeny Petrilin 	u64 in_param;
1671e8f081aaSYevgeny Petrilin 	u64 out_param;
1672e8f081aaSYevgeny Petrilin 	int ret = 0;
1673e8f081aaSYevgeny Petrilin 	int i;
167472be84f1SYevgeny Petrilin 	int err = 0;
1675e8f081aaSYevgeny Petrilin 
1676e8f081aaSYevgeny Petrilin 	/* Create sw representation of Virtual HCR */
1677e8f081aaSYevgeny Petrilin 	vhcr = kzalloc(sizeof(struct mlx4_vhcr), GFP_KERNEL);
1678e8f081aaSYevgeny Petrilin 	if (!vhcr)
1679e8f081aaSYevgeny Petrilin 		return -ENOMEM;
1680e8f081aaSYevgeny Petrilin 
1681e8f081aaSYevgeny Petrilin 	/* DMA in the vHCR */
1682e8f081aaSYevgeny Petrilin 	if (!in_vhcr) {
1683e8f081aaSYevgeny Petrilin 		ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
1684e8f081aaSYevgeny Petrilin 				      priv->mfunc.master.slave_state[slave].vhcr_dma,
1685e8f081aaSYevgeny Petrilin 				      ALIGN(sizeof(struct mlx4_vhcr_cmd),
1686e8f081aaSYevgeny Petrilin 					    MLX4_ACCESS_MEM_ALIGN), 1);
1687e8f081aaSYevgeny Petrilin 		if (ret) {
16880cd93027SYishai Hadas 			if (!(dev->persist->state &
16890cd93027SYishai Hadas 			    MLX4_DEVICE_STATE_INTERNAL_ERROR))
16901a91de28SJoe Perches 				mlx4_err(dev, "%s: Failed reading vhcr ret: 0x%x\n",
16911a91de28SJoe Perches 					 __func__, ret);
1692e8f081aaSYevgeny Petrilin 			kfree(vhcr);
1693e8f081aaSYevgeny Petrilin 			return ret;
1694e8f081aaSYevgeny Petrilin 		}
1695e8f081aaSYevgeny Petrilin 	}
1696e8f081aaSYevgeny Petrilin 
1697e8f081aaSYevgeny Petrilin 	/* Fill SW VHCR fields */
1698e8f081aaSYevgeny Petrilin 	vhcr->in_param = be64_to_cpu(vhcr_cmd->in_param);
1699e8f081aaSYevgeny Petrilin 	vhcr->out_param = be64_to_cpu(vhcr_cmd->out_param);
1700e8f081aaSYevgeny Petrilin 	vhcr->in_modifier = be32_to_cpu(vhcr_cmd->in_modifier);
1701e8f081aaSYevgeny Petrilin 	vhcr->token = be16_to_cpu(vhcr_cmd->token);
1702e8f081aaSYevgeny Petrilin 	vhcr->op = be16_to_cpu(vhcr_cmd->opcode) & 0xfff;
1703e8f081aaSYevgeny Petrilin 	vhcr->op_modifier = (u8) (be16_to_cpu(vhcr_cmd->opcode) >> 12);
1704e8f081aaSYevgeny Petrilin 	vhcr->e_bit = vhcr_cmd->flags & (1 << 6);
1705e8f081aaSYevgeny Petrilin 
1706e8f081aaSYevgeny Petrilin 	/* Lookup command */
1707e8f081aaSYevgeny Petrilin 	for (i = 0; i < ARRAY_SIZE(cmd_info); ++i) {
1708e8f081aaSYevgeny Petrilin 		if (vhcr->op == cmd_info[i].opcode) {
1709e8f081aaSYevgeny Petrilin 			cmd = &cmd_info[i];
1710e8f081aaSYevgeny Petrilin 			break;
1711e8f081aaSYevgeny Petrilin 		}
1712e8f081aaSYevgeny Petrilin 	}
1713e8f081aaSYevgeny Petrilin 	if (!cmd) {
1714e8f081aaSYevgeny Petrilin 		mlx4_err(dev, "Unknown command:0x%x accepted from slave:%d\n",
1715e8f081aaSYevgeny Petrilin 			 vhcr->op, slave);
171672be84f1SYevgeny Petrilin 		vhcr_cmd->status = CMD_STAT_BAD_PARAM;
1717e8f081aaSYevgeny Petrilin 		goto out_status;
1718e8f081aaSYevgeny Petrilin 	}
1719e8f081aaSYevgeny Petrilin 
1720e8f081aaSYevgeny Petrilin 	/* Read inbox */
1721e8f081aaSYevgeny Petrilin 	if (cmd->has_inbox) {
1722e8f081aaSYevgeny Petrilin 		vhcr->in_param &= INBOX_MASK;
1723e8f081aaSYevgeny Petrilin 		inbox = mlx4_alloc_cmd_mailbox(dev);
1724e8f081aaSYevgeny Petrilin 		if (IS_ERR(inbox)) {
172572be84f1SYevgeny Petrilin 			vhcr_cmd->status = CMD_STAT_BAD_SIZE;
1726e8f081aaSYevgeny Petrilin 			inbox = NULL;
172772be84f1SYevgeny Petrilin 			goto out_status;
1728e8f081aaSYevgeny Petrilin 		}
1729e8f081aaSYevgeny Petrilin 
17300cd93027SYishai Hadas 		ret = mlx4_ACCESS_MEM(dev, inbox->dma, slave,
1731e8f081aaSYevgeny Petrilin 				      vhcr->in_param,
17320cd93027SYishai Hadas 				      MLX4_MAILBOX_SIZE, 1);
17330cd93027SYishai Hadas 		if (ret) {
17340cd93027SYishai Hadas 			if (!(dev->persist->state &
17350cd93027SYishai Hadas 			    MLX4_DEVICE_STATE_INTERNAL_ERROR))
1736e8f081aaSYevgeny Petrilin 				mlx4_err(dev, "%s: Failed reading inbox (cmd:0x%x)\n",
1737e8f081aaSYevgeny Petrilin 					 __func__, cmd->opcode);
173872be84f1SYevgeny Petrilin 			vhcr_cmd->status = CMD_STAT_INTERNAL_ERR;
173972be84f1SYevgeny Petrilin 			goto out_status;
1740e8f081aaSYevgeny Petrilin 		}
1741e8f081aaSYevgeny Petrilin 	}
1742e8f081aaSYevgeny Petrilin 
1743e8f081aaSYevgeny Petrilin 	/* Apply permission and bound checks if applicable */
1744e8f081aaSYevgeny Petrilin 	if (cmd->verify && cmd->verify(dev, slave, vhcr, inbox)) {
17451a91de28SJoe Perches 		mlx4_warn(dev, "Command:0x%x from slave: %d failed protection checks for resource_id:%d\n",
17461a91de28SJoe Perches 			  vhcr->op, slave, vhcr->in_modifier);
174772be84f1SYevgeny Petrilin 		vhcr_cmd->status = CMD_STAT_BAD_OP;
1748e8f081aaSYevgeny Petrilin 		goto out_status;
1749e8f081aaSYevgeny Petrilin 	}
1750e8f081aaSYevgeny Petrilin 
1751e8f081aaSYevgeny Petrilin 	/* Allocate outbox */
1752e8f081aaSYevgeny Petrilin 	if (cmd->has_outbox) {
1753e8f081aaSYevgeny Petrilin 		outbox = mlx4_alloc_cmd_mailbox(dev);
1754e8f081aaSYevgeny Petrilin 		if (IS_ERR(outbox)) {
175572be84f1SYevgeny Petrilin 			vhcr_cmd->status = CMD_STAT_BAD_SIZE;
1756e8f081aaSYevgeny Petrilin 			outbox = NULL;
175772be84f1SYevgeny Petrilin 			goto out_status;
1758e8f081aaSYevgeny Petrilin 		}
1759e8f081aaSYevgeny Petrilin 	}
1760e8f081aaSYevgeny Petrilin 
1761e8f081aaSYevgeny Petrilin 	/* Execute the command! */
1762e8f081aaSYevgeny Petrilin 	if (cmd->wrapper) {
176372be84f1SYevgeny Petrilin 		err = cmd->wrapper(dev, slave, vhcr, inbox, outbox,
1764e8f081aaSYevgeny Petrilin 				   cmd);
1765e8f081aaSYevgeny Petrilin 		if (cmd->out_is_imm)
1766e8f081aaSYevgeny Petrilin 			vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
1767e8f081aaSYevgeny Petrilin 	} else {
1768e8f081aaSYevgeny Petrilin 		in_param = cmd->has_inbox ? (u64) inbox->dma :
1769e8f081aaSYevgeny Petrilin 			vhcr->in_param;
1770e8f081aaSYevgeny Petrilin 		out_param = cmd->has_outbox ? (u64) outbox->dma :
1771e8f081aaSYevgeny Petrilin 			vhcr->out_param;
177272be84f1SYevgeny Petrilin 		err = __mlx4_cmd(dev, in_param, &out_param,
1773e8f081aaSYevgeny Petrilin 				 cmd->out_is_imm, vhcr->in_modifier,
1774e8f081aaSYevgeny Petrilin 				 vhcr->op_modifier, vhcr->op,
1775e8f081aaSYevgeny Petrilin 				 MLX4_CMD_TIME_CLASS_A,
1776e8f081aaSYevgeny Petrilin 				 MLX4_CMD_NATIVE);
1777e8f081aaSYevgeny Petrilin 
1778e8f081aaSYevgeny Petrilin 		if (cmd->out_is_imm) {
1779e8f081aaSYevgeny Petrilin 			vhcr->out_param = out_param;
1780e8f081aaSYevgeny Petrilin 			vhcr_cmd->out_param = cpu_to_be64(vhcr->out_param);
1781e8f081aaSYevgeny Petrilin 		}
1782e8f081aaSYevgeny Petrilin 	}
1783e8f081aaSYevgeny Petrilin 
178472be84f1SYevgeny Petrilin 	if (err) {
178583bd5118SJack Morgenstein 		if (!(dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)) {
178683bd5118SJack Morgenstein 			if (vhcr->op == MLX4_CMD_ALLOC_RES &&
178783bd5118SJack Morgenstein 			    (vhcr->in_modifier & 0xff) == RES_COUNTER &&
178883bd5118SJack Morgenstein 			    err == -EDQUOT)
178983bd5118SJack Morgenstein 				mlx4_dbg(dev,
179083bd5118SJack Morgenstein 					 "Unable to allocate counter for slave %d (%d)\n",
179183bd5118SJack Morgenstein 					 slave, err);
179283bd5118SJack Morgenstein 			else
17931a91de28SJoe Perches 				mlx4_warn(dev, "vhcr command:0x%x slave:%d failed with error:%d, status %d\n",
179472be84f1SYevgeny Petrilin 					  vhcr->op, slave, vhcr->errno, err);
179583bd5118SJack Morgenstein 		}
179672be84f1SYevgeny Petrilin 		vhcr_cmd->status = mlx4_errno_to_status(err);
179772be84f1SYevgeny Petrilin 		goto out_status;
179872be84f1SYevgeny Petrilin 	}
179972be84f1SYevgeny Petrilin 
180072be84f1SYevgeny Petrilin 
1801e8f081aaSYevgeny Petrilin 	/* Write outbox if command completed successfully */
180272be84f1SYevgeny Petrilin 	if (cmd->has_outbox && !vhcr_cmd->status) {
1803e8f081aaSYevgeny Petrilin 		ret = mlx4_ACCESS_MEM(dev, outbox->dma, slave,
1804e8f081aaSYevgeny Petrilin 				      vhcr->out_param,
1805e8f081aaSYevgeny Petrilin 				      MLX4_MAILBOX_SIZE, MLX4_CMD_WRAPPED);
1806e8f081aaSYevgeny Petrilin 		if (ret) {
180772be84f1SYevgeny Petrilin 			/* If we failed to write back the outbox after the
180872be84f1SYevgeny Petrilin 			 *command was successfully executed, we must fail this
180972be84f1SYevgeny Petrilin 			 * slave, as it is now in undefined state */
18100cd93027SYishai Hadas 			if (!(dev->persist->state &
18110cd93027SYishai Hadas 			    MLX4_DEVICE_STATE_INTERNAL_ERROR))
1812e8f081aaSYevgeny Petrilin 				mlx4_err(dev, "%s:Failed writing outbox\n", __func__);
1813e8f081aaSYevgeny Petrilin 			goto out;
1814e8f081aaSYevgeny Petrilin 		}
1815e8f081aaSYevgeny Petrilin 	}
1816e8f081aaSYevgeny Petrilin 
1817e8f081aaSYevgeny Petrilin out_status:
1818e8f081aaSYevgeny Petrilin 	/* DMA back vhcr result */
1819e8f081aaSYevgeny Petrilin 	if (!in_vhcr) {
1820e8f081aaSYevgeny Petrilin 		ret = mlx4_ACCESS_MEM(dev, priv->mfunc.vhcr_dma, slave,
1821e8f081aaSYevgeny Petrilin 				      priv->mfunc.master.slave_state[slave].vhcr_dma,
1822e8f081aaSYevgeny Petrilin 				      ALIGN(sizeof(struct mlx4_vhcr),
1823e8f081aaSYevgeny Petrilin 					    MLX4_ACCESS_MEM_ALIGN),
1824e8f081aaSYevgeny Petrilin 				      MLX4_CMD_WRAPPED);
1825e8f081aaSYevgeny Petrilin 		if (ret)
1826e8f081aaSYevgeny Petrilin 			mlx4_err(dev, "%s:Failed writing vhcr result\n",
1827e8f081aaSYevgeny Petrilin 				 __func__);
1828e8f081aaSYevgeny Petrilin 		else if (vhcr->e_bit &&
1829e8f081aaSYevgeny Petrilin 			 mlx4_GEN_EQE(dev, slave, &priv->mfunc.master.cmd_eqe))
18301a91de28SJoe Perches 				mlx4_warn(dev, "Failed to generate command completion eqe for slave %d\n",
18311a91de28SJoe Perches 					  slave);
1832e8f081aaSYevgeny Petrilin 	}
1833e8f081aaSYevgeny Petrilin 
1834e8f081aaSYevgeny Petrilin out:
1835e8f081aaSYevgeny Petrilin 	kfree(vhcr);
1836e8f081aaSYevgeny Petrilin 	mlx4_free_cmd_mailbox(dev, inbox);
1837e8f081aaSYevgeny Petrilin 	mlx4_free_cmd_mailbox(dev, outbox);
1838e8f081aaSYevgeny Petrilin 	return ret;
1839e8f081aaSYevgeny Petrilin }
1840e8f081aaSYevgeny Petrilin 
mlx4_master_immediate_activate_vlan_qos(struct mlx4_priv * priv,int slave,int port)1841f094668cSJingoo Han static int mlx4_master_immediate_activate_vlan_qos(struct mlx4_priv *priv,
1842b01978caSJack Morgenstein 					    int slave, int port)
1843b01978caSJack Morgenstein {
1844b01978caSJack Morgenstein 	struct mlx4_vport_oper_state *vp_oper;
1845b01978caSJack Morgenstein 	struct mlx4_vport_state *vp_admin;
1846b01978caSJack Morgenstein 	struct mlx4_vf_immed_vlan_work *work;
18470a6eac24SRony Efraim 	struct mlx4_dev *dev = &(priv->dev);
1848b01978caSJack Morgenstein 	int err;
1849b01978caSJack Morgenstein 	int admin_vlan_ix = NO_INDX;
1850b01978caSJack Morgenstein 
1851b01978caSJack Morgenstein 	vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
1852b01978caSJack Morgenstein 	vp_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
1853b01978caSJack Morgenstein 
1854b01978caSJack Morgenstein 	if (vp_oper->state.default_vlan == vp_admin->default_vlan &&
18550a6eac24SRony Efraim 	    vp_oper->state.default_qos == vp_admin->default_qos &&
18567c3d21c8SMoshe Shemesh 	    vp_oper->state.vlan_proto == vp_admin->vlan_proto &&
185708068cd5SIdo Shamay 	    vp_oper->state.link_state == vp_admin->link_state &&
185808068cd5SIdo Shamay 	    vp_oper->state.qos_vport == vp_admin->qos_vport)
1859b01978caSJack Morgenstein 		return 0;
1860b01978caSJack Morgenstein 
18610a6eac24SRony Efraim 	if (!(priv->mfunc.master.slave_state[slave].active &&
1862f0f829bfSRony Efraim 	      dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_UPDATE_QP)) {
18630a6eac24SRony Efraim 		/* even if the UPDATE_QP command isn't supported, we still want
18640a6eac24SRony Efraim 		 * to set this VF link according to the admin directive
18650a6eac24SRony Efraim 		 */
18660a6eac24SRony Efraim 		vp_oper->state.link_state = vp_admin->link_state;
18670a6eac24SRony Efraim 		return -1;
18680a6eac24SRony Efraim 	}
18690a6eac24SRony Efraim 
18700a6eac24SRony Efraim 	mlx4_dbg(dev, "updating immediately admin params slave %d port %d\n",
18710a6eac24SRony Efraim 		 slave, port);
18721a91de28SJoe Perches 	mlx4_dbg(dev, "vlan %d QoS %d link down %d\n",
18731a91de28SJoe Perches 		 vp_admin->default_vlan, vp_admin->default_qos,
18741a91de28SJoe Perches 		 vp_admin->link_state);
18750a6eac24SRony Efraim 
1876b01978caSJack Morgenstein 	work = kzalloc(sizeof(*work), GFP_KERNEL);
1877b01978caSJack Morgenstein 	if (!work)
1878b01978caSJack Morgenstein 		return -ENOMEM;
1879b01978caSJack Morgenstein 
1880b01978caSJack Morgenstein 	if (vp_oper->state.default_vlan != vp_admin->default_vlan) {
1881f0f829bfSRony Efraim 		if (MLX4_VGT != vp_admin->default_vlan) {
1882b01978caSJack Morgenstein 			err = __mlx4_register_vlan(&priv->dev, port,
1883b01978caSJack Morgenstein 						   vp_admin->default_vlan,
1884b01978caSJack Morgenstein 						   &admin_vlan_ix);
1885b01978caSJack Morgenstein 			if (err) {
18869caf83c3SDan Carpenter 				kfree(work);
18871a91de28SJoe Perches 				mlx4_warn(&priv->dev,
1888b01978caSJack Morgenstein 					  "No vlan resources slave %d, port %d\n",
1889b01978caSJack Morgenstein 					  slave, port);
1890b01978caSJack Morgenstein 				return err;
1891b01978caSJack Morgenstein 			}
1892f0f829bfSRony Efraim 		} else {
1893f0f829bfSRony Efraim 			admin_vlan_ix = NO_INDX;
1894f0f829bfSRony Efraim 		}
1895b01978caSJack Morgenstein 		work->flags |= MLX4_VF_IMMED_VLAN_FLAG_VLAN;
18961a91de28SJoe Perches 		mlx4_dbg(&priv->dev,
1897b01978caSJack Morgenstein 			 "alloc vlan %d idx  %d slave %d port %d\n",
1898b01978caSJack Morgenstein 			 (int)(vp_admin->default_vlan),
1899b01978caSJack Morgenstein 			 admin_vlan_ix, slave, port);
1900b01978caSJack Morgenstein 	}
1901b01978caSJack Morgenstein 
1902b01978caSJack Morgenstein 	/* save original vlan ix and vlan id */
1903b01978caSJack Morgenstein 	work->orig_vlan_id = vp_oper->state.default_vlan;
1904b01978caSJack Morgenstein 	work->orig_vlan_ix = vp_oper->vlan_idx;
1905b01978caSJack Morgenstein 
1906b01978caSJack Morgenstein 	/* handle new qos */
1907b01978caSJack Morgenstein 	if (vp_oper->state.default_qos != vp_admin->default_qos)
1908b01978caSJack Morgenstein 		work->flags |= MLX4_VF_IMMED_VLAN_FLAG_QOS;
1909b01978caSJack Morgenstein 
1910b01978caSJack Morgenstein 	if (work->flags & MLX4_VF_IMMED_VLAN_FLAG_VLAN)
1911b01978caSJack Morgenstein 		vp_oper->vlan_idx = admin_vlan_ix;
1912b01978caSJack Morgenstein 
1913b01978caSJack Morgenstein 	vp_oper->state.default_vlan = vp_admin->default_vlan;
1914b01978caSJack Morgenstein 	vp_oper->state.default_qos = vp_admin->default_qos;
19157c3d21c8SMoshe Shemesh 	vp_oper->state.vlan_proto = vp_admin->vlan_proto;
19160a6eac24SRony Efraim 	vp_oper->state.link_state = vp_admin->link_state;
191708068cd5SIdo Shamay 	vp_oper->state.qos_vport = vp_admin->qos_vport;
19180a6eac24SRony Efraim 
19190a6eac24SRony Efraim 	if (vp_admin->link_state == IFLA_VF_LINK_STATE_DISABLE)
19200a6eac24SRony Efraim 		work->flags |= MLX4_VF_IMMED_VLAN_FLAG_LINK_DISABLE;
1921b01978caSJack Morgenstein 
1922b01978caSJack Morgenstein 	/* iterate over QPs owned by this slave, using UPDATE_QP */
1923b01978caSJack Morgenstein 	work->port = port;
1924b01978caSJack Morgenstein 	work->slave = slave;
1925b01978caSJack Morgenstein 	work->qos = vp_oper->state.default_qos;
192608068cd5SIdo Shamay 	work->qos_vport = vp_oper->state.qos_vport;
1927b01978caSJack Morgenstein 	work->vlan_id = vp_oper->state.default_vlan;
1928b01978caSJack Morgenstein 	work->vlan_ix = vp_oper->vlan_idx;
19297c3d21c8SMoshe Shemesh 	work->vlan_proto = vp_oper->state.vlan_proto;
1930b01978caSJack Morgenstein 	work->priv = priv;
1931b01978caSJack Morgenstein 	INIT_WORK(&work->work, mlx4_vf_immed_vlan_work_handler);
1932b01978caSJack Morgenstein 	queue_work(priv->mfunc.master.comm_wq, &work->work);
1933b01978caSJack Morgenstein 
1934b01978caSJack Morgenstein 	return 0;
1935b01978caSJack Morgenstein }
1936b01978caSJack Morgenstein 
mlx4_set_default_port_qos(struct mlx4_dev * dev,int port)1937666672d4SIdo Shamay static void mlx4_set_default_port_qos(struct mlx4_dev *dev, int port)
1938666672d4SIdo Shamay {
1939666672d4SIdo Shamay 	struct mlx4_qos_manager *port_qos_ctl;
1940666672d4SIdo Shamay 	struct mlx4_priv *priv = mlx4_priv(dev);
1941666672d4SIdo Shamay 
1942666672d4SIdo Shamay 	port_qos_ctl = &priv->mfunc.master.qos_ctl[port];
1943666672d4SIdo Shamay 	bitmap_zero(port_qos_ctl->priority_bm, MLX4_NUM_UP);
1944666672d4SIdo Shamay 
1945666672d4SIdo Shamay 	/* Enable only default prio at PF init routine */
1946666672d4SIdo Shamay 	set_bit(MLX4_DEFAULT_QOS_PRIO, port_qos_ctl->priority_bm);
1947666672d4SIdo Shamay }
1948666672d4SIdo Shamay 
mlx4_allocate_port_vpps(struct mlx4_dev * dev,int port)1949666672d4SIdo Shamay static void mlx4_allocate_port_vpps(struct mlx4_dev *dev, int port)
1950666672d4SIdo Shamay {
1951666672d4SIdo Shamay 	int i;
1952666672d4SIdo Shamay 	int err;
1953666672d4SIdo Shamay 	int num_vfs;
1954ba5c4dacSColin Ian King 	u16 available_vpp;
1955666672d4SIdo Shamay 	u8 vpp_param[MLX4_NUM_UP];
1956666672d4SIdo Shamay 	struct mlx4_qos_manager *port_qos;
1957666672d4SIdo Shamay 	struct mlx4_priv *priv = mlx4_priv(dev);
1958666672d4SIdo Shamay 
1959ba5c4dacSColin Ian King 	err = mlx4_ALLOCATE_VPP_get(dev, port, &available_vpp, vpp_param);
1960666672d4SIdo Shamay 	if (err) {
1961ba5c4dacSColin Ian King 		mlx4_info(dev, "Failed query available VPPs\n");
1962666672d4SIdo Shamay 		return;
1963666672d4SIdo Shamay 	}
1964666672d4SIdo Shamay 
1965666672d4SIdo Shamay 	port_qos = &priv->mfunc.master.qos_ctl[port];
1966ba5c4dacSColin Ian King 	num_vfs = (available_vpp /
1967666672d4SIdo Shamay 		   bitmap_weight(port_qos->priority_bm, MLX4_NUM_UP));
1968666672d4SIdo Shamay 
1969666672d4SIdo Shamay 	for (i = 0; i < MLX4_NUM_UP; i++) {
1970666672d4SIdo Shamay 		if (test_bit(i, port_qos->priority_bm))
1971666672d4SIdo Shamay 			vpp_param[i] = num_vfs;
1972666672d4SIdo Shamay 	}
1973666672d4SIdo Shamay 
1974666672d4SIdo Shamay 	err = mlx4_ALLOCATE_VPP_set(dev, port, vpp_param);
1975666672d4SIdo Shamay 	if (err) {
1976666672d4SIdo Shamay 		mlx4_info(dev, "Failed allocating VPPs\n");
1977666672d4SIdo Shamay 		return;
1978666672d4SIdo Shamay 	}
1979666672d4SIdo Shamay 
1980666672d4SIdo Shamay 	/* Query actual allocated VPP, just to make sure */
1981ba5c4dacSColin Ian King 	err = mlx4_ALLOCATE_VPP_get(dev, port, &available_vpp, vpp_param);
1982666672d4SIdo Shamay 	if (err) {
1983ba5c4dacSColin Ian King 		mlx4_info(dev, "Failed query available VPPs\n");
1984666672d4SIdo Shamay 		return;
1985666672d4SIdo Shamay 	}
1986666672d4SIdo Shamay 
1987666672d4SIdo Shamay 	port_qos->num_of_qos_vfs = num_vfs;
1988ba5c4dacSColin Ian King 	mlx4_dbg(dev, "Port %d Available VPPs %d\n", port, available_vpp);
1989666672d4SIdo Shamay 
1990666672d4SIdo Shamay 	for (i = 0; i < MLX4_NUM_UP; i++)
1991666672d4SIdo Shamay 		mlx4_dbg(dev, "Port %d UP %d Allocated %d VPPs\n", port, i,
1992666672d4SIdo Shamay 			 vpp_param[i]);
1993666672d4SIdo Shamay }
1994b01978caSJack Morgenstein 
mlx4_master_activate_admin_state(struct mlx4_priv * priv,int slave)19950eb62b93SRony Efraim static int mlx4_master_activate_admin_state(struct mlx4_priv *priv, int slave)
19960eb62b93SRony Efraim {
19973a351118SYury Norov 	int p, port, err;
19983f7fb021SRony Efraim 	struct mlx4_vport_state *vp_admin;
19993f7fb021SRony Efraim 	struct mlx4_vport_oper_state *vp_oper;
2000b42959dcSMoshe Shemesh 	struct mlx4_slave_state *slave_state =
2001b42959dcSMoshe Shemesh 		&priv->mfunc.master.slave_state[slave];
2002449fc488SMatan Barak 	struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
2003449fc488SMatan Barak 			&priv->dev, slave);
20043f7fb021SRony Efraim 
20053a351118SYury Norov 	for_each_set_bit(p, actv_ports.ports, priv->dev.caps.num_ports) {
20063a351118SYury Norov 		port = p + 1;
200799ec41d0SJack Morgenstein 		priv->mfunc.master.vf_oper[slave].smi_enabled[port] =
200899ec41d0SJack Morgenstein 			priv->mfunc.master.vf_admin[slave].enable_smi[port];
20093f7fb021SRony Efraim 		vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
20103f7fb021SRony Efraim 		vp_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
2011b42959dcSMoshe Shemesh 		if (vp_admin->vlan_proto != htons(ETH_P_8021AD) ||
2012b42959dcSMoshe Shemesh 		    slave_state->vst_qinq_supported) {
2013b42959dcSMoshe Shemesh 			vp_oper->state.vlan_proto   = vp_admin->vlan_proto;
2014b42959dcSMoshe Shemesh 			vp_oper->state.default_vlan = vp_admin->default_vlan;
2015b42959dcSMoshe Shemesh 			vp_oper->state.default_qos  = vp_admin->default_qos;
2016b42959dcSMoshe Shemesh 		}
2017b42959dcSMoshe Shemesh 		vp_oper->state.link_state = vp_admin->link_state;
2018b42959dcSMoshe Shemesh 		vp_oper->state.mac        = vp_admin->mac;
2019b42959dcSMoshe Shemesh 		vp_oper->state.spoofchk   = vp_admin->spoofchk;
2020b42959dcSMoshe Shemesh 		vp_oper->state.tx_rate    = vp_admin->tx_rate;
2021b42959dcSMoshe Shemesh 		vp_oper->state.qos_vport  = vp_admin->qos_vport;
2022b42959dcSMoshe Shemesh 		vp_oper->state.guid       = vp_admin->guid;
2023b42959dcSMoshe Shemesh 
20243f7fb021SRony Efraim 		if (MLX4_VGT != vp_admin->default_vlan) {
20253f7fb021SRony Efraim 			err = __mlx4_register_vlan(&priv->dev, port,
20263f7fb021SRony Efraim 						   vp_admin->default_vlan, &(vp_oper->vlan_idx));
20273f7fb021SRony Efraim 			if (err) {
20283f7fb021SRony Efraim 				vp_oper->vlan_idx = NO_INDX;
20297c3d21c8SMoshe Shemesh 				vp_oper->state.default_vlan = MLX4_VGT;
20307c3d21c8SMoshe Shemesh 				vp_oper->state.vlan_proto = htons(ETH_P_8021Q);
20311a91de28SJoe Perches 				mlx4_warn(&priv->dev,
20321a84db56SMasanari Iida 					  "No vlan resources slave %d, port %d\n",
20333f7fb021SRony Efraim 					  slave, port);
20343f7fb021SRony Efraim 				return err;
20353f7fb021SRony Efraim 			}
20361a91de28SJoe Perches 			mlx4_dbg(&priv->dev, "alloc vlan %d idx  %d slave %d port %d\n",
20373f7fb021SRony Efraim 				 (int)(vp_oper->state.default_vlan),
20383f7fb021SRony Efraim 				 vp_oper->vlan_idx, slave, port);
20393f7fb021SRony Efraim 		}
2040e6b6a231SRony Efraim 		if (vp_admin->spoofchk) {
2041e6b6a231SRony Efraim 			vp_oper->mac_idx = __mlx4_register_mac(&priv->dev,
2042e6b6a231SRony Efraim 							       port,
2043e6b6a231SRony Efraim 							       vp_admin->mac);
2044e6b6a231SRony Efraim 			if (0 > vp_oper->mac_idx) {
2045e6b6a231SRony Efraim 				err = vp_oper->mac_idx;
2046e6b6a231SRony Efraim 				vp_oper->mac_idx = NO_INDX;
20471a91de28SJoe Perches 				mlx4_warn(&priv->dev,
20481a84db56SMasanari Iida 					  "No mac resources slave %d, port %d\n",
2049e6b6a231SRony Efraim 					  slave, port);
2050e6b6a231SRony Efraim 				return err;
2051e6b6a231SRony Efraim 			}
20521a91de28SJoe Perches 			mlx4_dbg(&priv->dev, "alloc mac %llx idx  %d slave %d port %d\n",
2053e6b6a231SRony Efraim 				 vp_oper->state.mac, vp_oper->mac_idx, slave, port);
2054e6b6a231SRony Efraim 		}
20550eb62b93SRony Efraim 	}
20560eb62b93SRony Efraim 	return 0;
20570eb62b93SRony Efraim }
20580eb62b93SRony Efraim 
mlx4_master_deactivate_admin_state(struct mlx4_priv * priv,int slave)20593f7fb021SRony Efraim static void mlx4_master_deactivate_admin_state(struct mlx4_priv *priv, int slave)
20603f7fb021SRony Efraim {
20613a351118SYury Norov 	int p, port;
20623f7fb021SRony Efraim 	struct mlx4_vport_oper_state *vp_oper;
2063449fc488SMatan Barak 	struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
2064449fc488SMatan Barak 			&priv->dev, slave);
20653f7fb021SRony Efraim 
20663a351118SYury Norov 	for_each_set_bit(p, actv_ports.ports, priv->dev.caps.num_ports) {
20673a351118SYury Norov 		port = p + 1;
206899ec41d0SJack Morgenstein 		priv->mfunc.master.vf_oper[slave].smi_enabled[port] =
206999ec41d0SJack Morgenstein 			MLX4_VF_SMI_DISABLED;
20703f7fb021SRony Efraim 		vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
20713f7fb021SRony Efraim 		if (NO_INDX != vp_oper->vlan_idx) {
20723f7fb021SRony Efraim 			__mlx4_unregister_vlan(&priv->dev,
20732009d005SJack Morgenstein 					       port, vp_oper->state.default_vlan);
20743f7fb021SRony Efraim 			vp_oper->vlan_idx = NO_INDX;
20753f7fb021SRony Efraim 		}
2076e6b6a231SRony Efraim 		if (NO_INDX != vp_oper->mac_idx) {
2077c32b7dfbSJack Morgenstein 			__mlx4_unregister_mac(&priv->dev, port, vp_oper->state.mac);
2078e6b6a231SRony Efraim 			vp_oper->mac_idx = NO_INDX;
2079e6b6a231SRony Efraim 		}
20803f7fb021SRony Efraim 	}
20813f7fb021SRony Efraim 	return;
20823f7fb021SRony Efraim }
20833f7fb021SRony Efraim 
mlx4_master_do_cmd(struct mlx4_dev * dev,int slave,u8 cmd,u16 param,u8 toggle)2084e8f081aaSYevgeny Petrilin static void mlx4_master_do_cmd(struct mlx4_dev *dev, int slave, u8 cmd,
2085e8f081aaSYevgeny Petrilin 			       u16 param, u8 toggle)
2086e8f081aaSYevgeny Petrilin {
2087e8f081aaSYevgeny Petrilin 	struct mlx4_priv *priv = mlx4_priv(dev);
2088e8f081aaSYevgeny Petrilin 	struct mlx4_slave_state *slave_state = priv->mfunc.master.slave_state;
2089e8f081aaSYevgeny Petrilin 	u32 reply;
2090e8f081aaSYevgeny Petrilin 	u8 is_going_down = 0;
2091803143fbSMarcel Apfelbaum 	int i;
2092311f813aSJack Morgenstein 	unsigned long flags;
2093e8f081aaSYevgeny Petrilin 
2094e8f081aaSYevgeny Petrilin 	slave_state[slave].comm_toggle ^= 1;
2095e8f081aaSYevgeny Petrilin 	reply = (u32) slave_state[slave].comm_toggle << 31;
2096e8f081aaSYevgeny Petrilin 	if (toggle != slave_state[slave].comm_toggle) {
20971a91de28SJoe Perches 		mlx4_warn(dev, "Incorrect toggle %d from slave %d. *** MASTER STATE COMPROMISED ***\n",
20981a91de28SJoe Perches 			  toggle, slave);
2099e8f081aaSYevgeny Petrilin 		goto reset_slave;
2100e8f081aaSYevgeny Petrilin 	}
2101e8f081aaSYevgeny Petrilin 	if (cmd == MLX4_COMM_CMD_RESET) {
2102e8f081aaSYevgeny Petrilin 		mlx4_warn(dev, "Received reset from slave:%d\n", slave);
2103e8f081aaSYevgeny Petrilin 		slave_state[slave].active = false;
21042c957ff2SJack Morgenstein 		slave_state[slave].old_vlan_api = false;
2105b42959dcSMoshe Shemesh 		slave_state[slave].vst_qinq_supported = false;
21063f7fb021SRony Efraim 		mlx4_master_deactivate_admin_state(priv, slave);
2107803143fbSMarcel Apfelbaum 		for (i = 0; i < MLX4_EVENT_TYPES_NUM; ++i) {
2108803143fbSMarcel Apfelbaum 				slave_state[slave].event_eq[i].eqn = -1;
2109803143fbSMarcel Apfelbaum 				slave_state[slave].event_eq[i].token = 0;
2110803143fbSMarcel Apfelbaum 		}
2111e8f081aaSYevgeny Petrilin 		/*check if we are in the middle of FLR process,
2112e8f081aaSYevgeny Petrilin 		if so return "retry" status to the slave*/
2113162344edSOr Gerlitz 		if (MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd)
2114e8f081aaSYevgeny Petrilin 			goto inform_slave_state;
2115e8f081aaSYevgeny Petrilin 
2116*7ba189acSPetr Pavlu 		mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_SHUTDOWN, &slave);
2117fc06573dSJack Morgenstein 
2118e8f081aaSYevgeny Petrilin 		/* write the version in the event field */
2119e8f081aaSYevgeny Petrilin 		reply |= mlx4_comm_get_version();
2120e8f081aaSYevgeny Petrilin 
2121e8f081aaSYevgeny Petrilin 		goto reset_slave;
2122e8f081aaSYevgeny Petrilin 	}
2123e8f081aaSYevgeny Petrilin 	/*command from slave in the middle of FLR*/
2124e8f081aaSYevgeny Petrilin 	if (cmd != MLX4_COMM_CMD_RESET &&
2125e8f081aaSYevgeny Petrilin 	    MLX4_COMM_CMD_FLR == slave_state[slave].last_cmd) {
21261a91de28SJoe Perches 		mlx4_warn(dev, "slave:%d is Trying to run cmd(0x%x) in the middle of FLR\n",
21271a91de28SJoe Perches 			  slave, cmd);
2128e8f081aaSYevgeny Petrilin 		return;
2129e8f081aaSYevgeny Petrilin 	}
2130e8f081aaSYevgeny Petrilin 
2131e8f081aaSYevgeny Petrilin 	switch (cmd) {
2132e8f081aaSYevgeny Petrilin 	case MLX4_COMM_CMD_VHCR0:
2133e8f081aaSYevgeny Petrilin 		if (slave_state[slave].last_cmd != MLX4_COMM_CMD_RESET)
2134e8f081aaSYevgeny Petrilin 			goto reset_slave;
2135e8f081aaSYevgeny Petrilin 		slave_state[slave].vhcr_dma = ((u64) param) << 48;
2136e8f081aaSYevgeny Petrilin 		priv->mfunc.master.slave_state[slave].cookie = 0;
2137e8f081aaSYevgeny Petrilin 		break;
2138e8f081aaSYevgeny Petrilin 	case MLX4_COMM_CMD_VHCR1:
2139e8f081aaSYevgeny Petrilin 		if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR0)
2140e8f081aaSYevgeny Petrilin 			goto reset_slave;
2141e8f081aaSYevgeny Petrilin 		slave_state[slave].vhcr_dma |= ((u64) param) << 32;
2142e8f081aaSYevgeny Petrilin 		break;
2143e8f081aaSYevgeny Petrilin 	case MLX4_COMM_CMD_VHCR2:
2144e8f081aaSYevgeny Petrilin 		if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR1)
2145e8f081aaSYevgeny Petrilin 			goto reset_slave;
2146e8f081aaSYevgeny Petrilin 		slave_state[slave].vhcr_dma |= ((u64) param) << 16;
2147e8f081aaSYevgeny Petrilin 		break;
2148e8f081aaSYevgeny Petrilin 	case MLX4_COMM_CMD_VHCR_EN:
2149e8f081aaSYevgeny Petrilin 		if (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR2)
2150e8f081aaSYevgeny Petrilin 			goto reset_slave;
2151e8f081aaSYevgeny Petrilin 		slave_state[slave].vhcr_dma |= param;
21523f7fb021SRony Efraim 		if (mlx4_master_activate_admin_state(priv, slave))
21533f7fb021SRony Efraim 				goto reset_slave;
2154e8f081aaSYevgeny Petrilin 		slave_state[slave].active = true;
2155*7ba189acSPetr Pavlu 		mlx4_dispatch_event(dev, MLX4_DEV_EVENT_SLAVE_INIT, &slave);
2156e8f081aaSYevgeny Petrilin 		break;
2157e8f081aaSYevgeny Petrilin 	case MLX4_COMM_CMD_VHCR_POST:
2158e8f081aaSYevgeny Petrilin 		if ((slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_EN) &&
215955ad3592SYishai Hadas 		    (slave_state[slave].last_cmd != MLX4_COMM_CMD_VHCR_POST)) {
216055ad3592SYishai Hadas 			mlx4_warn(dev, "slave:%d is out of sync, cmd=0x%x, last command=0x%x, reset is needed\n",
216155ad3592SYishai Hadas 				  slave, cmd, slave_state[slave].last_cmd);
2162e8f081aaSYevgeny Petrilin 			goto reset_slave;
216355ad3592SYishai Hadas 		}
2164f3d4c89eSRoland Dreier 
2165f3d4c89eSRoland Dreier 		mutex_lock(&priv->cmd.slave_cmd_mutex);
2166e8f081aaSYevgeny Petrilin 		if (mlx4_master_process_vhcr(dev, slave, NULL)) {
21671a91de28SJoe Perches 			mlx4_err(dev, "Failed processing vhcr for slave:%d, resetting slave\n",
21681a91de28SJoe Perches 				 slave);
2169f3d4c89eSRoland Dreier 			mutex_unlock(&priv->cmd.slave_cmd_mutex);
2170e8f081aaSYevgeny Petrilin 			goto reset_slave;
2171e8f081aaSYevgeny Petrilin 		}
2172f3d4c89eSRoland Dreier 		mutex_unlock(&priv->cmd.slave_cmd_mutex);
2173e8f081aaSYevgeny Petrilin 		break;
2174e8f081aaSYevgeny Petrilin 	default:
2175e8f081aaSYevgeny Petrilin 		mlx4_warn(dev, "Bad comm cmd:%d from slave:%d\n", cmd, slave);
2176e8f081aaSYevgeny Petrilin 		goto reset_slave;
2177e8f081aaSYevgeny Petrilin 	}
2178311f813aSJack Morgenstein 	spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
2179e8f081aaSYevgeny Petrilin 	if (!slave_state[slave].is_slave_going_down)
2180e8f081aaSYevgeny Petrilin 		slave_state[slave].last_cmd = cmd;
2181e8f081aaSYevgeny Petrilin 	else
2182e8f081aaSYevgeny Petrilin 		is_going_down = 1;
2183311f813aSJack Morgenstein 	spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
2184e8f081aaSYevgeny Petrilin 	if (is_going_down) {
21851a91de28SJoe Perches 		mlx4_warn(dev, "Slave is going down aborting command(%d) executing from slave:%d\n",
2186e8f081aaSYevgeny Petrilin 			  cmd, slave);
2187e8f081aaSYevgeny Petrilin 		return;
2188e8f081aaSYevgeny Petrilin 	}
2189e8f081aaSYevgeny Petrilin 	__raw_writel((__force u32) cpu_to_be32(reply),
2190e8f081aaSYevgeny Petrilin 		     &priv->mfunc.comm[slave].slave_read);
2191e8f081aaSYevgeny Petrilin 
2192e8f081aaSYevgeny Petrilin 	return;
2193e8f081aaSYevgeny Petrilin 
2194e8f081aaSYevgeny Petrilin reset_slave:
2195c82e9aa0SEli Cohen 	/* cleanup any slave resources */
219655ad3592SYishai Hadas 	if (dev->persist->interface_state & MLX4_INTERFACE_STATE_UP)
2197c82e9aa0SEli Cohen 		mlx4_delete_all_resources_for_slave(dev, slave);
219855ad3592SYishai Hadas 
219955ad3592SYishai Hadas 	if (cmd != MLX4_COMM_CMD_RESET) {
220055ad3592SYishai Hadas 		mlx4_warn(dev, "Turn on internal error to force reset, slave=%d, cmd=0x%x\n",
220155ad3592SYishai Hadas 			  slave, cmd);
220255ad3592SYishai Hadas 		/* Turn on internal error letting slave reset itself immeditaly,
220355ad3592SYishai Hadas 		 * otherwise it might take till timeout on command is passed
220455ad3592SYishai Hadas 		 */
220555ad3592SYishai Hadas 		reply |= ((u32)COMM_CHAN_EVENT_INTERNAL_ERR);
220655ad3592SYishai Hadas 	}
220755ad3592SYishai Hadas 
2208311f813aSJack Morgenstein 	spin_lock_irqsave(&priv->mfunc.master.slave_state_lock, flags);
2209e8f081aaSYevgeny Petrilin 	if (!slave_state[slave].is_slave_going_down)
2210e8f081aaSYevgeny Petrilin 		slave_state[slave].last_cmd = MLX4_COMM_CMD_RESET;
2211311f813aSJack Morgenstein 	spin_unlock_irqrestore(&priv->mfunc.master.slave_state_lock, flags);
2212e8f081aaSYevgeny Petrilin 	/*with slave in the middle of flr, no need to clean resources again.*/
2213e8f081aaSYevgeny Petrilin inform_slave_state:
2214e8f081aaSYevgeny Petrilin 	memset(&slave_state[slave].event_eq, 0,
2215e8f081aaSYevgeny Petrilin 	       sizeof(struct mlx4_slave_event_eq_info));
2216e8f081aaSYevgeny Petrilin 	__raw_writel((__force u32) cpu_to_be32(reply),
2217e8f081aaSYevgeny Petrilin 		     &priv->mfunc.comm[slave].slave_read);
2218e8f081aaSYevgeny Petrilin 	wmb();
2219e8f081aaSYevgeny Petrilin }
2220e8f081aaSYevgeny Petrilin 
2221e8f081aaSYevgeny Petrilin /* master command processing */
mlx4_master_comm_channel(struct work_struct * work)2222e8f081aaSYevgeny Petrilin void mlx4_master_comm_channel(struct work_struct *work)
2223e8f081aaSYevgeny Petrilin {
2224e8f081aaSYevgeny Petrilin 	struct mlx4_mfunc_master_ctx *master =
2225e8f081aaSYevgeny Petrilin 		container_of(work,
2226e8f081aaSYevgeny Petrilin 			     struct mlx4_mfunc_master_ctx,
2227e8f081aaSYevgeny Petrilin 			     comm_work);
2228e8f081aaSYevgeny Petrilin 	struct mlx4_mfunc *mfunc =
2229e8f081aaSYevgeny Petrilin 		container_of(master, struct mlx4_mfunc, master);
2230e8f081aaSYevgeny Petrilin 	struct mlx4_priv *priv =
2231e8f081aaSYevgeny Petrilin 		container_of(mfunc, struct mlx4_priv, mfunc);
2232e8f081aaSYevgeny Petrilin 	struct mlx4_dev *dev = &priv->dev;
223379ebfb11SHans Westgaard Ry 	u32 lbit_vec[COMM_CHANNEL_BIT_ARRAY_SIZE];
223479ebfb11SHans Westgaard Ry 	u32 nmbr_bits;
2235e8f081aaSYevgeny Petrilin 	u32 comm_cmd;
223679ebfb11SHans Westgaard Ry 	int i, slave;
2237e8f081aaSYevgeny Petrilin 	int toggle;
223879ebfb11SHans Westgaard Ry 	bool first = true;
2239e8f081aaSYevgeny Petrilin 	int served = 0;
2240e8f081aaSYevgeny Petrilin 	int reported = 0;
2241e8f081aaSYevgeny Petrilin 	u32 slt;
2242e8f081aaSYevgeny Petrilin 
224379ebfb11SHans Westgaard Ry 	for (i = 0; i < COMM_CHANNEL_BIT_ARRAY_SIZE; i++)
224479ebfb11SHans Westgaard Ry 		lbit_vec[i] = be32_to_cpu(master->comm_arm_bit_vector[i]);
224579ebfb11SHans Westgaard Ry 	nmbr_bits = dev->persist->num_vfs + 1;
224679ebfb11SHans Westgaard Ry 	if (++master->next_slave >= nmbr_bits)
224779ebfb11SHans Westgaard Ry 		master->next_slave = 0;
224879ebfb11SHans Westgaard Ry 	slave = master->next_slave;
224979ebfb11SHans Westgaard Ry 	while (true) {
225079ebfb11SHans Westgaard Ry 		slave = find_next_bit((const unsigned long *)&lbit_vec, nmbr_bits, slave);
225179ebfb11SHans Westgaard Ry 		if  (!first && slave >= master->next_slave)
225279ebfb11SHans Westgaard Ry 			break;
225379ebfb11SHans Westgaard Ry 		if (slave == nmbr_bits) {
225479ebfb11SHans Westgaard Ry 			if (!first)
225579ebfb11SHans Westgaard Ry 				break;
225679ebfb11SHans Westgaard Ry 			first = false;
225779ebfb11SHans Westgaard Ry 			slave = 0;
2258e8f081aaSYevgeny Petrilin 			continue;
225979ebfb11SHans Westgaard Ry 		}
2260e8f081aaSYevgeny Petrilin 		++reported;
226179ebfb11SHans Westgaard Ry 		comm_cmd = swab32(readl(&mfunc->comm[slave].slave_write));
226279ebfb11SHans Westgaard Ry 		slt = swab32(readl(&mfunc->comm[slave].slave_read)) >> 31;
2263e8f081aaSYevgeny Petrilin 		toggle = comm_cmd >> 31;
2264e8f081aaSYevgeny Petrilin 		if (toggle != slt) {
2265e8f081aaSYevgeny Petrilin 			if (master->slave_state[slave].comm_toggle
2266e8f081aaSYevgeny Petrilin 			    != slt) {
2267c20862c8SAmir Vadai 				pr_info("slave %d out of sync. read toggle %d, state toggle %d. Resynching.\n",
22681a91de28SJoe Perches 					slave, slt,
2269e8f081aaSYevgeny Petrilin 					master->slave_state[slave].comm_toggle);
2270e8f081aaSYevgeny Petrilin 				master->slave_state[slave].comm_toggle =
2271e8f081aaSYevgeny Petrilin 					slt;
2272e8f081aaSYevgeny Petrilin 			}
2273e8f081aaSYevgeny Petrilin 			mlx4_master_do_cmd(dev, slave,
2274e8f081aaSYevgeny Petrilin 					   comm_cmd >> 16 & 0xff,
2275e8f081aaSYevgeny Petrilin 					   comm_cmd & 0xffff, toggle);
2276e8f081aaSYevgeny Petrilin 			++served;
2277e8f081aaSYevgeny Petrilin 		}
227879ebfb11SHans Westgaard Ry 		slave++;
2279e8f081aaSYevgeny Petrilin 	}
2280e8f081aaSYevgeny Petrilin 
2281e8f081aaSYevgeny Petrilin 	if (reported && reported != served)
22821a91de28SJoe Perches 		mlx4_warn(dev, "Got command event with bitmask from %d slaves but %d were served\n",
2283e8f081aaSYevgeny Petrilin 			  reported, served);
2284e8f081aaSYevgeny Petrilin 
2285e8f081aaSYevgeny Petrilin 	if (mlx4_ARM_COMM_CHANNEL(dev))
2286e8f081aaSYevgeny Petrilin 		mlx4_warn(dev, "Failed to arm comm channel events\n");
2287e8f081aaSYevgeny Petrilin }
2288e8f081aaSYevgeny Petrilin 
sync_toggles(struct mlx4_dev * dev)2289ab9c17a0SJack Morgenstein static int sync_toggles(struct mlx4_dev *dev)
2290ab9c17a0SJack Morgenstein {
2291ab9c17a0SJack Morgenstein 	struct mlx4_priv *priv = mlx4_priv(dev);
229255ad3592SYishai Hadas 	u32 wr_toggle;
229355ad3592SYishai Hadas 	u32 rd_toggle;
2294ab9c17a0SJack Morgenstein 	unsigned long end;
2295ab9c17a0SJack Morgenstein 
229655ad3592SYishai Hadas 	wr_toggle = swab32(readl(&priv->mfunc.comm->slave_write));
229755ad3592SYishai Hadas 	if (wr_toggle == 0xffffffff)
229855ad3592SYishai Hadas 		end = jiffies + msecs_to_jiffies(30000);
229955ad3592SYishai Hadas 	else
2300ab9c17a0SJack Morgenstein 		end = jiffies + msecs_to_jiffies(5000);
2301ab9c17a0SJack Morgenstein 
2302ab9c17a0SJack Morgenstein 	while (time_before(jiffies, end)) {
230355ad3592SYishai Hadas 		rd_toggle = swab32(readl(&priv->mfunc.comm->slave_read));
230455ad3592SYishai Hadas 		if (wr_toggle == 0xffffffff || rd_toggle == 0xffffffff) {
230555ad3592SYishai Hadas 			/* PCI might be offline */
23064cbe4dacSJack Morgenstein 
23074cbe4dacSJack Morgenstein 			/* If device removal has been requested,
23084cbe4dacSJack Morgenstein 			 * do not continue retrying.
23094cbe4dacSJack Morgenstein 			 */
23104cbe4dacSJack Morgenstein 			if (dev->persist->interface_state &
23114cbe4dacSJack Morgenstein 			    MLX4_INTERFACE_STATE_NOWAIT) {
23124cbe4dacSJack Morgenstein 				mlx4_warn(dev,
23134cbe4dacSJack Morgenstein 					  "communication channel is offline\n");
23144cbe4dacSJack Morgenstein 				return -EIO;
23154cbe4dacSJack Morgenstein 			}
23164cbe4dacSJack Morgenstein 
231755ad3592SYishai Hadas 			msleep(100);
231855ad3592SYishai Hadas 			wr_toggle = swab32(readl(&priv->mfunc.comm->
231955ad3592SYishai Hadas 					   slave_write));
232055ad3592SYishai Hadas 			continue;
232155ad3592SYishai Hadas 		}
232255ad3592SYishai Hadas 
232355ad3592SYishai Hadas 		if (rd_toggle >> 31 == wr_toggle >> 31) {
232455ad3592SYishai Hadas 			priv->cmd.comm_toggle = rd_toggle >> 31;
2325ab9c17a0SJack Morgenstein 			return 0;
2326ab9c17a0SJack Morgenstein 		}
2327ab9c17a0SJack Morgenstein 
2328ab9c17a0SJack Morgenstein 		cond_resched();
2329ab9c17a0SJack Morgenstein 	}
2330ab9c17a0SJack Morgenstein 
2331ab9c17a0SJack Morgenstein 	/*
2332ab9c17a0SJack Morgenstein 	 * we could reach here if for example the previous VM using this
2333ab9c17a0SJack Morgenstein 	 * function misbehaved and left the channel with unsynced state. We
2334ab9c17a0SJack Morgenstein 	 * should fix this here and give this VM a chance to use a properly
2335ab9c17a0SJack Morgenstein 	 * synced channel
2336ab9c17a0SJack Morgenstein 	 */
2337ab9c17a0SJack Morgenstein 	mlx4_warn(dev, "recovering from previously mis-behaved VM\n");
2338ab9c17a0SJack Morgenstein 	__raw_writel((__force u32) 0, &priv->mfunc.comm->slave_read);
2339ab9c17a0SJack Morgenstein 	__raw_writel((__force u32) 0, &priv->mfunc.comm->slave_write);
2340ab9c17a0SJack Morgenstein 	priv->cmd.comm_toggle = 0;
2341ab9c17a0SJack Morgenstein 
2342ab9c17a0SJack Morgenstein 	return 0;
2343ab9c17a0SJack Morgenstein }
2344ab9c17a0SJack Morgenstein 
mlx4_multi_func_init(struct mlx4_dev * dev)2345ab9c17a0SJack Morgenstein int mlx4_multi_func_init(struct mlx4_dev *dev)
2346ab9c17a0SJack Morgenstein {
2347ab9c17a0SJack Morgenstein 	struct mlx4_priv *priv = mlx4_priv(dev);
2348ab9c17a0SJack Morgenstein 	struct mlx4_slave_state *s_state;
2349803143fbSMarcel Apfelbaum 	int i, j, err, port;
2350ab9c17a0SJack Morgenstein 
2351ab9c17a0SJack Morgenstein 	if (mlx4_is_master(dev))
2352ab9c17a0SJack Morgenstein 		priv->mfunc.comm =
2353872bf2fbSYishai Hadas 		ioremap(pci_resource_start(dev->persist->pdev,
2354872bf2fbSYishai Hadas 					   priv->fw.comm_bar) +
2355ab9c17a0SJack Morgenstein 			priv->fw.comm_base, MLX4_COMM_PAGESIZE);
2356ab9c17a0SJack Morgenstein 	else
2357ab9c17a0SJack Morgenstein 		priv->mfunc.comm =
2358872bf2fbSYishai Hadas 		ioremap(pci_resource_start(dev->persist->pdev, 2) +
2359ab9c17a0SJack Morgenstein 			MLX4_SLAVE_COMM_BASE, MLX4_COMM_PAGESIZE);
2360ab9c17a0SJack Morgenstein 	if (!priv->mfunc.comm) {
23611a91de28SJoe Perches 		mlx4_err(dev, "Couldn't map communication vector\n");
2362ab9c17a0SJack Morgenstein 		goto err_vhcr;
2363ab9c17a0SJack Morgenstein 	}
2364ab9c17a0SJack Morgenstein 
2365ab9c17a0SJack Morgenstein 	if (mlx4_is_master(dev)) {
23664abccb61SIdo Shamay 		struct mlx4_vf_oper_state *vf_oper;
23674abccb61SIdo Shamay 		struct mlx4_vf_admin_state *vf_admin;
23684abccb61SIdo Shamay 
2369ab9c17a0SJack Morgenstein 		priv->mfunc.master.slave_state =
23706396bb22SKees Cook 			kcalloc(dev->num_slaves,
23716396bb22SKees Cook 				sizeof(struct mlx4_slave_state),
23726396bb22SKees Cook 				GFP_KERNEL);
2373ab9c17a0SJack Morgenstein 		if (!priv->mfunc.master.slave_state)
2374ab9c17a0SJack Morgenstein 			goto err_comm;
2375ab9c17a0SJack Morgenstein 
23760eb62b93SRony Efraim 		priv->mfunc.master.vf_admin =
23776396bb22SKees Cook 			kcalloc(dev->num_slaves,
23786396bb22SKees Cook 				sizeof(struct mlx4_vf_admin_state),
23796396bb22SKees Cook 				GFP_KERNEL);
23800eb62b93SRony Efraim 		if (!priv->mfunc.master.vf_admin)
23810eb62b93SRony Efraim 			goto err_comm_admin;
23820eb62b93SRony Efraim 
23830eb62b93SRony Efraim 		priv->mfunc.master.vf_oper =
23846396bb22SKees Cook 			kcalloc(dev->num_slaves,
23856396bb22SKees Cook 				sizeof(struct mlx4_vf_oper_state),
23866396bb22SKees Cook 				GFP_KERNEL);
23870eb62b93SRony Efraim 		if (!priv->mfunc.master.vf_oper)
23880eb62b93SRony Efraim 			goto err_comm_oper;
23890eb62b93SRony Efraim 
239079ebfb11SHans Westgaard Ry 		priv->mfunc.master.next_slave = 0;
239179ebfb11SHans Westgaard Ry 
2392ab9c17a0SJack Morgenstein 		for (i = 0; i < dev->num_slaves; ++i) {
23934abccb61SIdo Shamay 			vf_admin = &priv->mfunc.master.vf_admin[i];
23944abccb61SIdo Shamay 			vf_oper = &priv->mfunc.master.vf_oper[i];
2395ab9c17a0SJack Morgenstein 			s_state = &priv->mfunc.master.slave_state[i];
2396ab9c17a0SJack Morgenstein 			s_state->last_cmd = MLX4_COMM_CMD_RESET;
2397b42959dcSMoshe Shemesh 			s_state->vst_qinq_supported = false;
2398bffb023aSJack Morgenstein 			mutex_init(&priv->mfunc.master.gen_eqe_mutex[i]);
2399803143fbSMarcel Apfelbaum 			for (j = 0; j < MLX4_EVENT_TYPES_NUM; ++j)
2400803143fbSMarcel Apfelbaum 				s_state->event_eq[j].eqn = -1;
2401ab9c17a0SJack Morgenstein 			__raw_writel((__force u32) 0,
2402ab9c17a0SJack Morgenstein 				     &priv->mfunc.comm[i].slave_write);
2403ab9c17a0SJack Morgenstein 			__raw_writel((__force u32) 0,
2404ab9c17a0SJack Morgenstein 				     &priv->mfunc.comm[i].slave_read);
2405ab9c17a0SJack Morgenstein 			for (port = 1; port <= MLX4_MAX_PORTS; port++) {
24064abccb61SIdo Shamay 				struct mlx4_vport_state *admin_vport;
24074abccb61SIdo Shamay 				struct mlx4_vport_state *oper_vport;
24084abccb61SIdo Shamay 
2409ab9c17a0SJack Morgenstein 				s_state->vlan_filter[port] =
2410ab9c17a0SJack Morgenstein 					kzalloc(sizeof(struct mlx4_vlan_fltr),
2411ab9c17a0SJack Morgenstein 						GFP_KERNEL);
2412ab9c17a0SJack Morgenstein 				if (!s_state->vlan_filter[port]) {
2413ab9c17a0SJack Morgenstein 					if (--port)
2414ab9c17a0SJack Morgenstein 						kfree(s_state->vlan_filter[port]);
2415ab9c17a0SJack Morgenstein 					goto err_slaves;
2416ab9c17a0SJack Morgenstein 				}
24174abccb61SIdo Shamay 
24184abccb61SIdo Shamay 				admin_vport = &vf_admin->vport[port];
24194abccb61SIdo Shamay 				oper_vport = &vf_oper->vport[port].state;
2420ab9c17a0SJack Morgenstein 				INIT_LIST_HEAD(&s_state->mcast_filters[port]);
24214abccb61SIdo Shamay 				admin_vport->default_vlan = MLX4_VGT;
24224abccb61SIdo Shamay 				oper_vport->default_vlan = MLX4_VGT;
242308068cd5SIdo Shamay 				admin_vport->qos_vport =
242408068cd5SIdo Shamay 						MLX4_VPP_DEFAULT_VPORT;
242508068cd5SIdo Shamay 				oper_vport->qos_vport = MLX4_VPP_DEFAULT_VPORT;
24267c3d21c8SMoshe Shemesh 				admin_vport->vlan_proto = htons(ETH_P_8021Q);
24277c3d21c8SMoshe Shemesh 				oper_vport->vlan_proto = htons(ETH_P_8021Q);
24284abccb61SIdo Shamay 				vf_oper->vport[port].vlan_idx = NO_INDX;
24294abccb61SIdo Shamay 				vf_oper->vport[port].mac_idx = NO_INDX;
2430fb517a4fSYishai Hadas 				mlx4_set_random_admin_guid(dev, i, port);
2431ab9c17a0SJack Morgenstein 			}
2432ab9c17a0SJack Morgenstein 			spin_lock_init(&s_state->lock);
2433ab9c17a0SJack Morgenstein 		}
2434ab9c17a0SJack Morgenstein 
2435666672d4SIdo Shamay 		if (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QOS_VPP) {
2436666672d4SIdo Shamay 			for (port = 1; port <= dev->caps.num_ports; port++) {
2437666672d4SIdo Shamay 				if (mlx4_is_eth(dev, port)) {
2438666672d4SIdo Shamay 					mlx4_set_default_port_qos(dev, port);
2439666672d4SIdo Shamay 					mlx4_allocate_port_vpps(dev, port);
2440666672d4SIdo Shamay 				}
2441666672d4SIdo Shamay 			}
2442666672d4SIdo Shamay 		}
2443666672d4SIdo Shamay 
2444c02b0501SCarol L Soto 		memset(&priv->mfunc.master.cmd_eqe, 0, sizeof(struct mlx4_eqe));
2445ab9c17a0SJack Morgenstein 		priv->mfunc.master.cmd_eqe.type = MLX4_EVENT_TYPE_CMD;
2446ab9c17a0SJack Morgenstein 		INIT_WORK(&priv->mfunc.master.comm_work,
2447ab9c17a0SJack Morgenstein 			  mlx4_master_comm_channel);
2448ab9c17a0SJack Morgenstein 		INIT_WORK(&priv->mfunc.master.slave_event_work,
2449ab9c17a0SJack Morgenstein 			  mlx4_gen_slave_eqe);
2450ab9c17a0SJack Morgenstein 		INIT_WORK(&priv->mfunc.master.slave_flr_event_work,
2451ab9c17a0SJack Morgenstein 			  mlx4_master_handle_slave_flr);
2452ab9c17a0SJack Morgenstein 		spin_lock_init(&priv->mfunc.master.slave_state_lock);
2453992e8e6eSJack Morgenstein 		spin_lock_init(&priv->mfunc.master.slave_eq.event_lock);
2454ab9c17a0SJack Morgenstein 		priv->mfunc.master.comm_wq =
2455ab9c17a0SJack Morgenstein 			create_singlethread_workqueue("mlx4_comm");
2456ab9c17a0SJack Morgenstein 		if (!priv->mfunc.master.comm_wq)
2457ab9c17a0SJack Morgenstein 			goto err_slaves;
2458ab9c17a0SJack Morgenstein 
2459ab9c17a0SJack Morgenstein 		if (mlx4_init_resource_tracker(dev))
2460ab9c17a0SJack Morgenstein 			goto err_thread;
2461ab9c17a0SJack Morgenstein 
2462ab9c17a0SJack Morgenstein 	} else {
2463ab9c17a0SJack Morgenstein 		err = sync_toggles(dev);
2464ab9c17a0SJack Morgenstein 		if (err) {
2465ab9c17a0SJack Morgenstein 			mlx4_err(dev, "Couldn't sync toggles\n");
2466ab9c17a0SJack Morgenstein 			goto err_comm;
2467ab9c17a0SJack Morgenstein 		}
2468ab9c17a0SJack Morgenstein 	}
2469ab9c17a0SJack Morgenstein 	return 0;
2470ab9c17a0SJack Morgenstein 
2471ab9c17a0SJack Morgenstein err_thread:
2472ab9c17a0SJack Morgenstein 	destroy_workqueue(priv->mfunc.master.comm_wq);
2473ab9c17a0SJack Morgenstein err_slaves:
2474fa51b247SRasmus Villemoes 	while (i--) {
2475ab9c17a0SJack Morgenstein 		for (port = 1; port <= MLX4_MAX_PORTS; port++)
2476ab9c17a0SJack Morgenstein 			kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
2477ab9c17a0SJack Morgenstein 	}
24780eb62b93SRony Efraim 	kfree(priv->mfunc.master.vf_oper);
24790eb62b93SRony Efraim err_comm_oper:
24800eb62b93SRony Efraim 	kfree(priv->mfunc.master.vf_admin);
24810eb62b93SRony Efraim err_comm_admin:
2482ab9c17a0SJack Morgenstein 	kfree(priv->mfunc.master.slave_state);
2483ab9c17a0SJack Morgenstein err_comm:
2484ab9c17a0SJack Morgenstein 	iounmap(priv->mfunc.comm);
248581d18419SJack Morgenstein 	priv->mfunc.comm = NULL;
2486ab9c17a0SJack Morgenstein err_vhcr:
2487872bf2fbSYishai Hadas 	dma_free_coherent(&dev->persist->pdev->dev, PAGE_SIZE,
2488ab9c17a0SJack Morgenstein 			  priv->mfunc.vhcr,
2489ab9c17a0SJack Morgenstein 			  priv->mfunc.vhcr_dma);
2490ab9c17a0SJack Morgenstein 	priv->mfunc.vhcr = NULL;
2491ab9c17a0SJack Morgenstein 	return -ENOMEM;
2492ab9c17a0SJack Morgenstein }
2493ab9c17a0SJack Morgenstein 
mlx4_cmd_init(struct mlx4_dev * dev)24945a2cc190SJeff Kirsher int mlx4_cmd_init(struct mlx4_dev *dev)
24955a2cc190SJeff Kirsher {
24965a2cc190SJeff Kirsher 	struct mlx4_priv *priv = mlx4_priv(dev);
2497ffc39f6dSMatan Barak 	int flags = 0;
24985a2cc190SJeff Kirsher 
2499ffc39f6dSMatan Barak 	if (!priv->cmd.initialized) {
2500a7e1f049SJack Morgenstein 		init_rwsem(&priv->cmd.switch_sem);
2501f3d4c89eSRoland Dreier 		mutex_init(&priv->cmd.slave_cmd_mutex);
25025a2cc190SJeff Kirsher 		sema_init(&priv->cmd.poll_sem, 1);
25035a2cc190SJeff Kirsher 		priv->cmd.use_events = 0;
25045a2cc190SJeff Kirsher 		priv->cmd.toggle     = 1;
2505ffc39f6dSMatan Barak 		priv->cmd.initialized = 1;
2506ffc39f6dSMatan Barak 		flags |= MLX4_CMD_CLEANUP_STRUCT;
2507ffc39f6dSMatan Barak 	}
25085a2cc190SJeff Kirsher 
2509ffc39f6dSMatan Barak 	if (!mlx4_is_slave(dev) && !priv->cmd.hcr) {
2510872bf2fbSYishai Hadas 		priv->cmd.hcr = ioremap(pci_resource_start(dev->persist->pdev,
2511872bf2fbSYishai Hadas 					0) + MLX4_HCR_BASE, MLX4_HCR_SIZE);
25125a2cc190SJeff Kirsher 		if (!priv->cmd.hcr) {
25131a91de28SJoe Perches 			mlx4_err(dev, "Couldn't map command register\n");
2514ffc39f6dSMatan Barak 			goto err;
25155a2cc190SJeff Kirsher 		}
2516ffc39f6dSMatan Barak 		flags |= MLX4_CMD_CLEANUP_HCR;
2517e8f081aaSYevgeny Petrilin 	}
25185a2cc190SJeff Kirsher 
2519ffc39f6dSMatan Barak 	if (mlx4_is_mfunc(dev) && !priv->mfunc.vhcr) {
2520872bf2fbSYishai Hadas 		priv->mfunc.vhcr = dma_alloc_coherent(&dev->persist->pdev->dev,
2521872bf2fbSYishai Hadas 						      PAGE_SIZE,
2522f3d4c89eSRoland Dreier 						      &priv->mfunc.vhcr_dma,
2523f3d4c89eSRoland Dreier 						      GFP_KERNEL);
2524d0320f75SJoe Perches 		if (!priv->mfunc.vhcr)
2525ffc39f6dSMatan Barak 			goto err;
2526ffc39f6dSMatan Barak 
2527ffc39f6dSMatan Barak 		flags |= MLX4_CMD_CLEANUP_VHCR;
2528f3d4c89eSRoland Dreier 	}
2529f3d4c89eSRoland Dreier 
2530ffc39f6dSMatan Barak 	if (!priv->cmd.pool) {
2531b9f761aaSRomain Perier 		priv->cmd.pool = dma_pool_create("mlx4_cmd",
2532b9f761aaSRomain Perier 						 &dev->persist->pdev->dev,
25335a2cc190SJeff Kirsher 						 MLX4_MAILBOX_SIZE,
25345a2cc190SJeff Kirsher 						 MLX4_MAILBOX_SIZE, 0);
2535e8f081aaSYevgeny Petrilin 		if (!priv->cmd.pool)
2536ffc39f6dSMatan Barak 			goto err;
2537ffc39f6dSMatan Barak 
2538ffc39f6dSMatan Barak 		flags |= MLX4_CMD_CLEANUP_POOL;
2539ffc39f6dSMatan Barak 	}
25405a2cc190SJeff Kirsher 
25415a2cc190SJeff Kirsher 	return 0;
2542e8f081aaSYevgeny Petrilin 
2543ffc39f6dSMatan Barak err:
2544ffc39f6dSMatan Barak 	mlx4_cmd_cleanup(dev, flags);
2545e8f081aaSYevgeny Petrilin 	return -ENOMEM;
25465a2cc190SJeff Kirsher }
25475a2cc190SJeff Kirsher 
mlx4_report_internal_err_comm_event(struct mlx4_dev * dev)254855ad3592SYishai Hadas void mlx4_report_internal_err_comm_event(struct mlx4_dev *dev)
254955ad3592SYishai Hadas {
255055ad3592SYishai Hadas 	struct mlx4_priv *priv = mlx4_priv(dev);
255155ad3592SYishai Hadas 	int slave;
255255ad3592SYishai Hadas 	u32 slave_read;
255355ad3592SYishai Hadas 
255481d18419SJack Morgenstein 	/* If the comm channel has not yet been initialized,
255581d18419SJack Morgenstein 	 * skip reporting the internal error event to all
255681d18419SJack Morgenstein 	 * the communication channels.
255781d18419SJack Morgenstein 	 */
255881d18419SJack Morgenstein 	if (!priv->mfunc.comm)
255981d18419SJack Morgenstein 		return;
256081d18419SJack Morgenstein 
256155ad3592SYishai Hadas 	/* Report an internal error event to all
256255ad3592SYishai Hadas 	 * communication channels.
256355ad3592SYishai Hadas 	 */
256455ad3592SYishai Hadas 	for (slave = 0; slave < dev->num_slaves; slave++) {
256555ad3592SYishai Hadas 		slave_read = swab32(readl(&priv->mfunc.comm[slave].slave_read));
256655ad3592SYishai Hadas 		slave_read |= (u32)COMM_CHAN_EVENT_INTERNAL_ERR;
256755ad3592SYishai Hadas 		__raw_writel((__force u32)cpu_to_be32(slave_read),
256855ad3592SYishai Hadas 			     &priv->mfunc.comm[slave].slave_read);
256955ad3592SYishai Hadas 	}
257055ad3592SYishai Hadas }
257155ad3592SYishai Hadas 
mlx4_multi_func_cleanup(struct mlx4_dev * dev)2572ab9c17a0SJack Morgenstein void mlx4_multi_func_cleanup(struct mlx4_dev *dev)
2573ab9c17a0SJack Morgenstein {
2574ab9c17a0SJack Morgenstein 	struct mlx4_priv *priv = mlx4_priv(dev);
2575ab9c17a0SJack Morgenstein 	int i, port;
2576ab9c17a0SJack Morgenstein 
2577ab9c17a0SJack Morgenstein 	if (mlx4_is_master(dev)) {
2578ab9c17a0SJack Morgenstein 		destroy_workqueue(priv->mfunc.master.comm_wq);
2579ab9c17a0SJack Morgenstein 		for (i = 0; i < dev->num_slaves; i++) {
2580ab9c17a0SJack Morgenstein 			for (port = 1; port <= MLX4_MAX_PORTS; port++)
2581ab9c17a0SJack Morgenstein 				kfree(priv->mfunc.master.slave_state[i].vlan_filter[port]);
2582ab9c17a0SJack Morgenstein 		}
2583ab9c17a0SJack Morgenstein 		kfree(priv->mfunc.master.slave_state);
25840eb62b93SRony Efraim 		kfree(priv->mfunc.master.vf_admin);
25850eb62b93SRony Efraim 		kfree(priv->mfunc.master.vf_oper);
258655ad3592SYishai Hadas 		dev->num_slaves = 0;
2587f08ad06cSEugenia Emantayev 	}
2588f08ad06cSEugenia Emantayev 
2589ab9c17a0SJack Morgenstein 	iounmap(priv->mfunc.comm);
259081d18419SJack Morgenstein 	priv->mfunc.comm = NULL;
2591ab9c17a0SJack Morgenstein }
2592ab9c17a0SJack Morgenstein 
mlx4_cmd_cleanup(struct mlx4_dev * dev,int cleanup_mask)2593ffc39f6dSMatan Barak void mlx4_cmd_cleanup(struct mlx4_dev *dev, int cleanup_mask)
25945a2cc190SJeff Kirsher {
25955a2cc190SJeff Kirsher 	struct mlx4_priv *priv = mlx4_priv(dev);
25965a2cc190SJeff Kirsher 
2597ffc39f6dSMatan Barak 	if (priv->cmd.pool && (cleanup_mask & MLX4_CMD_CLEANUP_POOL)) {
2598b9f761aaSRomain Perier 		dma_pool_destroy(priv->cmd.pool);
2599ffc39f6dSMatan Barak 		priv->cmd.pool = NULL;
2600ffc39f6dSMatan Barak 	}
2601e8f081aaSYevgeny Petrilin 
2602ffc39f6dSMatan Barak 	if (!mlx4_is_slave(dev) && priv->cmd.hcr &&
2603ffc39f6dSMatan Barak 	    (cleanup_mask & MLX4_CMD_CLEANUP_HCR)) {
26045a2cc190SJeff Kirsher 		iounmap(priv->cmd.hcr);
2605ffc39f6dSMatan Barak 		priv->cmd.hcr = NULL;
2606ffc39f6dSMatan Barak 	}
2607ffc39f6dSMatan Barak 	if (mlx4_is_mfunc(dev) && priv->mfunc.vhcr &&
2608ffc39f6dSMatan Barak 	    (cleanup_mask & MLX4_CMD_CLEANUP_VHCR)) {
2609872bf2fbSYishai Hadas 		dma_free_coherent(&dev->persist->pdev->dev, PAGE_SIZE,
2610f3d4c89eSRoland Dreier 				  priv->mfunc.vhcr, priv->mfunc.vhcr_dma);
2611f3d4c89eSRoland Dreier 		priv->mfunc.vhcr = NULL;
26125a2cc190SJeff Kirsher 	}
2613ffc39f6dSMatan Barak 	if (priv->cmd.initialized && (cleanup_mask & MLX4_CMD_CLEANUP_STRUCT))
2614ffc39f6dSMatan Barak 		priv->cmd.initialized = 0;
2615ffc39f6dSMatan Barak }
26165a2cc190SJeff Kirsher 
26175a2cc190SJeff Kirsher /*
26185a2cc190SJeff Kirsher  * Switch to using events to issue FW commands (can only be called
26195a2cc190SJeff Kirsher  * after event queue for command events has been initialized).
26205a2cc190SJeff Kirsher  */
mlx4_cmd_use_events(struct mlx4_dev * dev)26215a2cc190SJeff Kirsher int mlx4_cmd_use_events(struct mlx4_dev *dev)
26225a2cc190SJeff Kirsher {
26235a2cc190SJeff Kirsher 	struct mlx4_priv *priv = mlx4_priv(dev);
26245a2cc190SJeff Kirsher 	int i;
2625e8f081aaSYevgeny Petrilin 	int err = 0;
26265a2cc190SJeff Kirsher 
26276da2ec56SKees Cook 	priv->cmd.context = kmalloc_array(priv->cmd.max_cmds,
26285a2cc190SJeff Kirsher 					  sizeof(struct mlx4_cmd_context),
26295a2cc190SJeff Kirsher 					  GFP_KERNEL);
26305a2cc190SJeff Kirsher 	if (!priv->cmd.context)
26315a2cc190SJeff Kirsher 		return -ENOMEM;
26325a2cc190SJeff Kirsher 
2633c07d2792SJack Morgenstein 	if (mlx4_is_mfunc(dev))
2634c07d2792SJack Morgenstein 		mutex_lock(&priv->cmd.slave_cmd_mutex);
2635a7e1f049SJack Morgenstein 	down_write(&priv->cmd.switch_sem);
26365a2cc190SJeff Kirsher 	for (i = 0; i < priv->cmd.max_cmds; ++i) {
26375a2cc190SJeff Kirsher 		priv->cmd.context[i].token = i;
26385a2cc190SJeff Kirsher 		priv->cmd.context[i].next  = i + 1;
2639f5aef5aaSYishai Hadas 		/* To support fatal error flow, initialize all
2640f5aef5aaSYishai Hadas 		 * cmd contexts to allow simulating completions
2641f5aef5aaSYishai Hadas 		 * with complete() at any time.
2642f5aef5aaSYishai Hadas 		 */
2643f5aef5aaSYishai Hadas 		init_completion(&priv->cmd.context[i].done);
26445a2cc190SJeff Kirsher 	}
26455a2cc190SJeff Kirsher 
26465a2cc190SJeff Kirsher 	priv->cmd.context[priv->cmd.max_cmds - 1].next = -1;
26475a2cc190SJeff Kirsher 	priv->cmd.free_head = 0;
26485a2cc190SJeff Kirsher 
26495a2cc190SJeff Kirsher 	sema_init(&priv->cmd.event_sem, priv->cmd.max_cmds);
26505a2cc190SJeff Kirsher 
26515a2cc190SJeff Kirsher 	for (priv->cmd.token_mask = 1;
26525a2cc190SJeff Kirsher 	     priv->cmd.token_mask < priv->cmd.max_cmds;
26535a2cc190SJeff Kirsher 	     priv->cmd.token_mask <<= 1)
26545a2cc190SJeff Kirsher 		; /* nothing */
26555a2cc190SJeff Kirsher 	--priv->cmd.token_mask;
26565a2cc190SJeff Kirsher 
2657e8f081aaSYevgeny Petrilin 	down(&priv->cmd.poll_sem);
26585a2cc190SJeff Kirsher 	priv->cmd.use_events = 1;
2659a7e1f049SJack Morgenstein 	up_write(&priv->cmd.switch_sem);
2660c07d2792SJack Morgenstein 	if (mlx4_is_mfunc(dev))
2661c07d2792SJack Morgenstein 		mutex_unlock(&priv->cmd.slave_cmd_mutex);
26625a2cc190SJeff Kirsher 
2663e8f081aaSYevgeny Petrilin 	return err;
26645a2cc190SJeff Kirsher }
26655a2cc190SJeff Kirsher 
26665a2cc190SJeff Kirsher /*
26675a2cc190SJeff Kirsher  * Switch back to polling (used when shutting down the device)
26685a2cc190SJeff Kirsher  */
mlx4_cmd_use_polling(struct mlx4_dev * dev)26695a2cc190SJeff Kirsher void mlx4_cmd_use_polling(struct mlx4_dev *dev)
26705a2cc190SJeff Kirsher {
26715a2cc190SJeff Kirsher 	struct mlx4_priv *priv = mlx4_priv(dev);
26725a2cc190SJeff Kirsher 	int i;
26735a2cc190SJeff Kirsher 
2674c07d2792SJack Morgenstein 	if (mlx4_is_mfunc(dev))
2675c07d2792SJack Morgenstein 		mutex_lock(&priv->cmd.slave_cmd_mutex);
2676a7e1f049SJack Morgenstein 	down_write(&priv->cmd.switch_sem);
26775a2cc190SJeff Kirsher 	priv->cmd.use_events = 0;
26785a2cc190SJeff Kirsher 
26795a2cc190SJeff Kirsher 	for (i = 0; i < priv->cmd.max_cmds; ++i)
26805a2cc190SJeff Kirsher 		down(&priv->cmd.event_sem);
26815a2cc190SJeff Kirsher 
26825a2cc190SJeff Kirsher 	kfree(priv->cmd.context);
2683e15ce4b8SJack Morgenstein 	priv->cmd.context = NULL;
26845a2cc190SJeff Kirsher 
26855a2cc190SJeff Kirsher 	up(&priv->cmd.poll_sem);
2686a7e1f049SJack Morgenstein 	up_write(&priv->cmd.switch_sem);
2687c07d2792SJack Morgenstein 	if (mlx4_is_mfunc(dev))
2688c07d2792SJack Morgenstein 		mutex_unlock(&priv->cmd.slave_cmd_mutex);
26895a2cc190SJeff Kirsher }
26905a2cc190SJeff Kirsher 
mlx4_alloc_cmd_mailbox(struct mlx4_dev * dev)26915a2cc190SJeff Kirsher struct mlx4_cmd_mailbox *mlx4_alloc_cmd_mailbox(struct mlx4_dev *dev)
26925a2cc190SJeff Kirsher {
26935a2cc190SJeff Kirsher 	struct mlx4_cmd_mailbox *mailbox;
26945a2cc190SJeff Kirsher 
269531975e27Sstephen hemminger 	mailbox = kmalloc(sizeof(*mailbox), GFP_KERNEL);
26965a2cc190SJeff Kirsher 	if (!mailbox)
26975a2cc190SJeff Kirsher 		return ERR_PTR(-ENOMEM);
26985a2cc190SJeff Kirsher 
2699b9f761aaSRomain Perier 	mailbox->buf = dma_pool_zalloc(mlx4_priv(dev)->cmd.pool, GFP_KERNEL,
27005a2cc190SJeff Kirsher 				       &mailbox->dma);
27015a2cc190SJeff Kirsher 	if (!mailbox->buf) {
27025a2cc190SJeff Kirsher 		kfree(mailbox);
27035a2cc190SJeff Kirsher 		return ERR_PTR(-ENOMEM);
27045a2cc190SJeff Kirsher 	}
27055a2cc190SJeff Kirsher 
27065a2cc190SJeff Kirsher 	return mailbox;
27075a2cc190SJeff Kirsher }
27085a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_alloc_cmd_mailbox);
27095a2cc190SJeff Kirsher 
mlx4_free_cmd_mailbox(struct mlx4_dev * dev,struct mlx4_cmd_mailbox * mailbox)2710e8f081aaSYevgeny Petrilin void mlx4_free_cmd_mailbox(struct mlx4_dev *dev,
2711e8f081aaSYevgeny Petrilin 			   struct mlx4_cmd_mailbox *mailbox)
27125a2cc190SJeff Kirsher {
27135a2cc190SJeff Kirsher 	if (!mailbox)
27145a2cc190SJeff Kirsher 		return;
27155a2cc190SJeff Kirsher 
2716b9f761aaSRomain Perier 	dma_pool_free(mlx4_priv(dev)->cmd.pool, mailbox->buf, mailbox->dma);
27175a2cc190SJeff Kirsher 	kfree(mailbox);
27185a2cc190SJeff Kirsher }
27195a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_free_cmd_mailbox);
2720e8f081aaSYevgeny Petrilin 
mlx4_comm_get_version(void)2721e8f081aaSYevgeny Petrilin u32 mlx4_comm_get_version(void)
2722e8f081aaSYevgeny Petrilin {
2723e8f081aaSYevgeny Petrilin 	 return ((u32) CMD_CHAN_IF_REV << 8) | (u32) CMD_CHAN_VER;
2724e8f081aaSYevgeny Petrilin }
27258f7ba3caSRony Efraim 
mlx4_get_slave_indx(struct mlx4_dev * dev,int vf)27268f7ba3caSRony Efraim static int mlx4_get_slave_indx(struct mlx4_dev *dev, int vf)
27278f7ba3caSRony Efraim {
2728872bf2fbSYishai Hadas 	if ((vf < 0) || (vf >= dev->persist->num_vfs)) {
2729872bf2fbSYishai Hadas 		mlx4_err(dev, "Bad vf number:%d (number of activated vf: %d)\n",
2730872bf2fbSYishai Hadas 			 vf, dev->persist->num_vfs);
27318f7ba3caSRony Efraim 		return -EINVAL;
27328f7ba3caSRony Efraim 	}
27338f7ba3caSRony Efraim 
27348f7ba3caSRony Efraim 	return vf+1;
27358f7ba3caSRony Efraim }
27368f7ba3caSRony Efraim 
mlx4_get_vf_indx(struct mlx4_dev * dev,int slave)2737f74462acSMatan Barak int mlx4_get_vf_indx(struct mlx4_dev *dev, int slave)
2738f74462acSMatan Barak {
2739872bf2fbSYishai Hadas 	if (slave < 1 || slave > dev->persist->num_vfs) {
2740f74462acSMatan Barak 		mlx4_err(dev,
2741f74462acSMatan Barak 			 "Bad slave number:%d (number of activated slaves: %lu)\n",
2742f74462acSMatan Barak 			 slave, dev->num_slaves);
2743f74462acSMatan Barak 		return -EINVAL;
2744f74462acSMatan Barak 	}
2745f74462acSMatan Barak 	return slave - 1;
2746f74462acSMatan Barak }
2747f74462acSMatan Barak 
mlx4_cmd_wake_completions(struct mlx4_dev * dev)2748f5aef5aaSYishai Hadas void mlx4_cmd_wake_completions(struct mlx4_dev *dev)
2749f5aef5aaSYishai Hadas {
2750f5aef5aaSYishai Hadas 	struct mlx4_priv *priv = mlx4_priv(dev);
2751f5aef5aaSYishai Hadas 	struct mlx4_cmd_context *context;
2752f5aef5aaSYishai Hadas 	int i;
2753f5aef5aaSYishai Hadas 
2754f5aef5aaSYishai Hadas 	spin_lock(&priv->cmd.context_lock);
2755f5aef5aaSYishai Hadas 	if (priv->cmd.context) {
2756f5aef5aaSYishai Hadas 		for (i = 0; i < priv->cmd.max_cmds; ++i) {
2757f5aef5aaSYishai Hadas 			context = &priv->cmd.context[i];
2758f5aef5aaSYishai Hadas 			context->fw_status = CMD_STAT_INTERNAL_ERR;
2759f5aef5aaSYishai Hadas 			context->result    =
2760f5aef5aaSYishai Hadas 				mlx4_status_to_errno(CMD_STAT_INTERNAL_ERR);
2761f5aef5aaSYishai Hadas 			complete(&context->done);
2762f5aef5aaSYishai Hadas 		}
2763f5aef5aaSYishai Hadas 	}
2764f5aef5aaSYishai Hadas 	spin_unlock(&priv->cmd.context_lock);
2765f5aef5aaSYishai Hadas }
2766f5aef5aaSYishai Hadas 
mlx4_get_active_ports(struct mlx4_dev * dev,int slave)2767f74462acSMatan Barak struct mlx4_active_ports mlx4_get_active_ports(struct mlx4_dev *dev, int slave)
2768f74462acSMatan Barak {
2769f74462acSMatan Barak 	struct mlx4_active_ports actv_ports;
2770f74462acSMatan Barak 	int vf;
2771f74462acSMatan Barak 
2772f74462acSMatan Barak 	bitmap_zero(actv_ports.ports, MLX4_MAX_PORTS);
2773f74462acSMatan Barak 
2774f74462acSMatan Barak 	if (slave == 0) {
2775f74462acSMatan Barak 		bitmap_fill(actv_ports.ports, dev->caps.num_ports);
2776f74462acSMatan Barak 		return actv_ports;
2777f74462acSMatan Barak 	}
2778f74462acSMatan Barak 
2779f74462acSMatan Barak 	vf = mlx4_get_vf_indx(dev, slave);
2780f74462acSMatan Barak 	if (vf < 0)
2781f74462acSMatan Barak 		return actv_ports;
2782f74462acSMatan Barak 
2783f74462acSMatan Barak 	bitmap_set(actv_ports.ports, dev->dev_vfs[vf].min_port - 1,
2784f74462acSMatan Barak 		   min((int)dev->dev_vfs[mlx4_get_vf_indx(dev, slave)].n_ports,
2785f74462acSMatan Barak 		   dev->caps.num_ports));
2786f74462acSMatan Barak 
2787f74462acSMatan Barak 	return actv_ports;
2788f74462acSMatan Barak }
2789f74462acSMatan Barak EXPORT_SYMBOL_GPL(mlx4_get_active_ports);
2790f74462acSMatan Barak 
mlx4_slave_convert_port(struct mlx4_dev * dev,int slave,int port)2791f74462acSMatan Barak int mlx4_slave_convert_port(struct mlx4_dev *dev, int slave, int port)
2792f74462acSMatan Barak {
2793f74462acSMatan Barak 	unsigned n;
2794f74462acSMatan Barak 	struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
2795f74462acSMatan Barak 	unsigned m = bitmap_weight(actv_ports.ports, dev->caps.num_ports);
2796f74462acSMatan Barak 
2797f74462acSMatan Barak 	if (port <= 0 || port > m)
2798f74462acSMatan Barak 		return -EINVAL;
2799f74462acSMatan Barak 
2800f74462acSMatan Barak 	n = find_first_bit(actv_ports.ports, dev->caps.num_ports);
2801f74462acSMatan Barak 	if (port <= n)
2802f74462acSMatan Barak 		port = n + 1;
2803f74462acSMatan Barak 
2804f74462acSMatan Barak 	return port;
2805f74462acSMatan Barak }
2806f74462acSMatan Barak EXPORT_SYMBOL_GPL(mlx4_slave_convert_port);
2807f74462acSMatan Barak 
mlx4_phys_to_slave_port(struct mlx4_dev * dev,int slave,int port)2808f74462acSMatan Barak int mlx4_phys_to_slave_port(struct mlx4_dev *dev, int slave, int port)
2809f74462acSMatan Barak {
2810f74462acSMatan Barak 	struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
2811f74462acSMatan Barak 	if (test_bit(port - 1, actv_ports.ports))
2812f74462acSMatan Barak 		return port -
2813f74462acSMatan Barak 			find_first_bit(actv_ports.ports, dev->caps.num_ports);
2814f74462acSMatan Barak 
2815f74462acSMatan Barak 	return -1;
2816f74462acSMatan Barak }
2817f74462acSMatan Barak EXPORT_SYMBOL_GPL(mlx4_phys_to_slave_port);
2818f74462acSMatan Barak 
mlx4_phys_to_slaves_pport(struct mlx4_dev * dev,int port)2819f74462acSMatan Barak struct mlx4_slaves_pport mlx4_phys_to_slaves_pport(struct mlx4_dev *dev,
2820f74462acSMatan Barak 						   int port)
2821f74462acSMatan Barak {
2822f74462acSMatan Barak 	unsigned i;
2823f74462acSMatan Barak 	struct mlx4_slaves_pport slaves_pport;
2824f74462acSMatan Barak 
2825f74462acSMatan Barak 	bitmap_zero(slaves_pport.slaves, MLX4_MFUNC_MAX);
2826f74462acSMatan Barak 
2827f74462acSMatan Barak 	if (port <= 0 || port > dev->caps.num_ports)
2828f74462acSMatan Barak 		return slaves_pport;
2829f74462acSMatan Barak 
2830872bf2fbSYishai Hadas 	for (i = 0; i < dev->persist->num_vfs + 1; i++) {
2831f74462acSMatan Barak 		struct mlx4_active_ports actv_ports =
2832f74462acSMatan Barak 			mlx4_get_active_ports(dev, i);
2833f74462acSMatan Barak 		if (test_bit(port - 1, actv_ports.ports))
2834f74462acSMatan Barak 			set_bit(i, slaves_pport.slaves);
2835f74462acSMatan Barak 	}
2836f74462acSMatan Barak 
2837f74462acSMatan Barak 	return slaves_pport;
2838f74462acSMatan Barak }
2839f74462acSMatan Barak EXPORT_SYMBOL_GPL(mlx4_phys_to_slaves_pport);
2840f74462acSMatan Barak 
mlx4_phys_to_slaves_pport_actv(struct mlx4_dev * dev,const struct mlx4_active_ports * crit_ports)2841f74462acSMatan Barak struct mlx4_slaves_pport mlx4_phys_to_slaves_pport_actv(
2842f74462acSMatan Barak 		struct mlx4_dev *dev,
2843f74462acSMatan Barak 		const struct mlx4_active_ports *crit_ports)
2844f74462acSMatan Barak {
2845f74462acSMatan Barak 	unsigned i;
2846f74462acSMatan Barak 	struct mlx4_slaves_pport slaves_pport;
2847f74462acSMatan Barak 
2848f74462acSMatan Barak 	bitmap_zero(slaves_pport.slaves, MLX4_MFUNC_MAX);
2849f74462acSMatan Barak 
2850872bf2fbSYishai Hadas 	for (i = 0; i < dev->persist->num_vfs + 1; i++) {
2851f74462acSMatan Barak 		struct mlx4_active_ports actv_ports =
2852f74462acSMatan Barak 			mlx4_get_active_ports(dev, i);
2853f74462acSMatan Barak 		if (bitmap_equal(crit_ports->ports, actv_ports.ports,
2854f74462acSMatan Barak 				 dev->caps.num_ports))
2855f74462acSMatan Barak 			set_bit(i, slaves_pport.slaves);
2856f74462acSMatan Barak 	}
2857f74462acSMatan Barak 
2858f74462acSMatan Barak 	return slaves_pport;
2859f74462acSMatan Barak }
2860f74462acSMatan Barak EXPORT_SYMBOL_GPL(mlx4_phys_to_slaves_pport_actv);
2861f74462acSMatan Barak 
mlx4_slaves_closest_port(struct mlx4_dev * dev,int slave,int port)2862a91c772fSMatan Barak static int mlx4_slaves_closest_port(struct mlx4_dev *dev, int slave, int port)
2863a91c772fSMatan Barak {
2864a91c772fSMatan Barak 	struct mlx4_active_ports actv_ports = mlx4_get_active_ports(dev, slave);
2865a91c772fSMatan Barak 	int min_port = find_first_bit(actv_ports.ports, dev->caps.num_ports)
2866a91c772fSMatan Barak 			+ 1;
2867a91c772fSMatan Barak 	int max_port = min_port +
2868a91c772fSMatan Barak 		bitmap_weight(actv_ports.ports, dev->caps.num_ports);
2869a91c772fSMatan Barak 
2870a91c772fSMatan Barak 	if (port < min_port)
2871a91c772fSMatan Barak 		port = min_port;
2872a91c772fSMatan Barak 	else if (port >= max_port)
2873a91c772fSMatan Barak 		port = max_port - 1;
2874a91c772fSMatan Barak 
2875a91c772fSMatan Barak 	return port;
2876a91c772fSMatan Barak }
2877a91c772fSMatan Barak 
mlx4_set_vport_qos(struct mlx4_priv * priv,int slave,int port,int max_tx_rate)2878cda373f4SIdo Shamay static int mlx4_set_vport_qos(struct mlx4_priv *priv, int slave, int port,
2879cda373f4SIdo Shamay 			      int max_tx_rate)
2880cda373f4SIdo Shamay {
2881cda373f4SIdo Shamay 	int i;
2882cda373f4SIdo Shamay 	int err;
2883cda373f4SIdo Shamay 	struct mlx4_qos_manager *port_qos;
2884cda373f4SIdo Shamay 	struct mlx4_dev *dev = &priv->dev;
2885cda373f4SIdo Shamay 	struct mlx4_vport_qos_param vpp_qos[MLX4_NUM_UP];
2886cda373f4SIdo Shamay 
2887cda373f4SIdo Shamay 	port_qos = &priv->mfunc.master.qos_ctl[port];
2888cda373f4SIdo Shamay 	memset(vpp_qos, 0, sizeof(struct mlx4_vport_qos_param) * MLX4_NUM_UP);
2889cda373f4SIdo Shamay 
2890cda373f4SIdo Shamay 	if (slave > port_qos->num_of_qos_vfs) {
2891ba5c4dacSColin Ian King 		mlx4_info(dev, "No available VPP resources for this VF\n");
2892cda373f4SIdo Shamay 		return -EINVAL;
2893cda373f4SIdo Shamay 	}
2894cda373f4SIdo Shamay 
2895cda373f4SIdo Shamay 	/* Query for default QoS values from Vport 0 is needed */
2896cda373f4SIdo Shamay 	err = mlx4_SET_VPORT_QOS_get(dev, port, 0, vpp_qos);
2897cda373f4SIdo Shamay 	if (err) {
2898cda373f4SIdo Shamay 		mlx4_info(dev, "Failed to query Vport 0 QoS values\n");
2899cda373f4SIdo Shamay 		return err;
2900cda373f4SIdo Shamay 	}
2901cda373f4SIdo Shamay 
2902cda373f4SIdo Shamay 	for (i = 0; i < MLX4_NUM_UP; i++) {
2903cda373f4SIdo Shamay 		if (test_bit(i, port_qos->priority_bm) && max_tx_rate) {
2904cda373f4SIdo Shamay 			vpp_qos[i].max_avg_bw = max_tx_rate;
2905cda373f4SIdo Shamay 			vpp_qos[i].enable = 1;
2906cda373f4SIdo Shamay 		} else {
2907cda373f4SIdo Shamay 			/* if user supplied tx_rate == 0, meaning no rate limit
2908cda373f4SIdo Shamay 			 * configuration is required. so we are leaving the
2909cda373f4SIdo Shamay 			 * value of max_avg_bw as queried from Vport 0.
2910cda373f4SIdo Shamay 			 */
2911cda373f4SIdo Shamay 			vpp_qos[i].enable = 0;
2912cda373f4SIdo Shamay 		}
2913cda373f4SIdo Shamay 	}
2914cda373f4SIdo Shamay 
2915cda373f4SIdo Shamay 	err = mlx4_SET_VPORT_QOS_set(dev, port, slave, vpp_qos);
2916cda373f4SIdo Shamay 	if (err) {
2917cda373f4SIdo Shamay 		mlx4_info(dev, "Failed to set Vport %d QoS values\n", slave);
2918cda373f4SIdo Shamay 		return err;
2919cda373f4SIdo Shamay 	}
2920cda373f4SIdo Shamay 
2921cda373f4SIdo Shamay 	return 0;
2922cda373f4SIdo Shamay }
2923cda373f4SIdo Shamay 
mlx4_is_vf_vst_and_prio_qos(struct mlx4_dev * dev,int port,struct mlx4_vport_state * vf_admin)2924cda373f4SIdo Shamay static bool mlx4_is_vf_vst_and_prio_qos(struct mlx4_dev *dev, int port,
2925cda373f4SIdo Shamay 					struct mlx4_vport_state *vf_admin)
2926cda373f4SIdo Shamay {
2927cda373f4SIdo Shamay 	struct mlx4_qos_manager *info;
2928cda373f4SIdo Shamay 	struct mlx4_priv *priv = mlx4_priv(dev);
2929cda373f4SIdo Shamay 
2930cda373f4SIdo Shamay 	if (!mlx4_is_master(dev) ||
2931cda373f4SIdo Shamay 	    !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QOS_VPP))
2932cda373f4SIdo Shamay 		return false;
2933cda373f4SIdo Shamay 
2934cda373f4SIdo Shamay 	info = &priv->mfunc.master.qos_ctl[port];
2935cda373f4SIdo Shamay 
2936cda373f4SIdo Shamay 	if (vf_admin->default_vlan != MLX4_VGT &&
2937cda373f4SIdo Shamay 	    test_bit(vf_admin->default_qos, info->priority_bm))
2938cda373f4SIdo Shamay 		return true;
2939cda373f4SIdo Shamay 
2940cda373f4SIdo Shamay 	return false;
2941cda373f4SIdo Shamay }
2942cda373f4SIdo Shamay 
mlx4_valid_vf_state_change(struct mlx4_dev * dev,int port,struct mlx4_vport_state * vf_admin,int vlan,int qos)2943cda373f4SIdo Shamay static bool mlx4_valid_vf_state_change(struct mlx4_dev *dev, int port,
2944cda373f4SIdo Shamay 				       struct mlx4_vport_state *vf_admin,
2945cda373f4SIdo Shamay 				       int vlan, int qos)
2946cda373f4SIdo Shamay {
2947cda373f4SIdo Shamay 	struct mlx4_vport_state dummy_admin = {0};
2948cda373f4SIdo Shamay 
2949cda373f4SIdo Shamay 	if (!mlx4_is_vf_vst_and_prio_qos(dev, port, vf_admin) ||
2950cda373f4SIdo Shamay 	    !vf_admin->tx_rate)
2951cda373f4SIdo Shamay 		return true;
2952cda373f4SIdo Shamay 
2953cda373f4SIdo Shamay 	dummy_admin.default_qos = qos;
2954cda373f4SIdo Shamay 	dummy_admin.default_vlan = vlan;
2955cda373f4SIdo Shamay 
2956cda373f4SIdo Shamay 	/* VF wants to move to other VST state which is valid with current
2957cda373f4SIdo Shamay 	 * rate limit. Either differnt default vlan in VST or other
2958cda373f4SIdo Shamay 	 * supported QoS priority. Otherwise we don't allow this change when
2959cda373f4SIdo Shamay 	 * the TX rate is still configured.
2960cda373f4SIdo Shamay 	 */
2961cda373f4SIdo Shamay 	if (mlx4_is_vf_vst_and_prio_qos(dev, port, &dummy_admin))
2962cda373f4SIdo Shamay 		return true;
2963cda373f4SIdo Shamay 
2964cda373f4SIdo Shamay 	mlx4_info(dev, "Cannot change VF state to %s while rate is set\n",
2965cda373f4SIdo Shamay 		  (vlan == MLX4_VGT) ? "VGT" : "VST");
2966cda373f4SIdo Shamay 
2967cda373f4SIdo Shamay 	if (vlan != MLX4_VGT)
2968cda373f4SIdo Shamay 		mlx4_info(dev, "VST priority %d not supported for QoS\n", qos);
2969cda373f4SIdo Shamay 
2970cda373f4SIdo Shamay 	mlx4_info(dev, "Please set rate to 0 prior to this VF state change\n");
2971cda373f4SIdo Shamay 
2972cda373f4SIdo Shamay 	return false;
2973cda373f4SIdo Shamay }
2974cda373f4SIdo Shamay 
mlx4_set_vf_mac(struct mlx4_dev * dev,int port,int vf,u8 * mac)2975745d8ae4SEugenia Emantayev int mlx4_set_vf_mac(struct mlx4_dev *dev, int port, int vf, u8 *mac)
29768f7ba3caSRony Efraim {
29778f7ba3caSRony Efraim 	struct mlx4_priv *priv = mlx4_priv(dev);
29788f7ba3caSRony Efraim 	struct mlx4_vport_state *s_info;
29798f7ba3caSRony Efraim 	int slave;
29808f7ba3caSRony Efraim 
29818f7ba3caSRony Efraim 	if (!mlx4_is_master(dev))
29828f7ba3caSRony Efraim 		return -EPROTONOSUPPORT;
29838f7ba3caSRony Efraim 
2984745d8ae4SEugenia Emantayev 	if (is_multicast_ether_addr(mac))
2985745d8ae4SEugenia Emantayev 		return -EINVAL;
2986745d8ae4SEugenia Emantayev 
29878f7ba3caSRony Efraim 	slave = mlx4_get_slave_indx(dev, vf);
29888f7ba3caSRony Efraim 	if (slave < 0)
29898f7ba3caSRony Efraim 		return -EINVAL;
29908f7ba3caSRony Efraim 
2991a91c772fSMatan Barak 	port = mlx4_slaves_closest_port(dev, slave, port);
29928f7ba3caSRony Efraim 	s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
2993745d8ae4SEugenia Emantayev 
2994745d8ae4SEugenia Emantayev 	if (s_info->spoofchk && is_zero_ether_addr(mac)) {
2995745d8ae4SEugenia Emantayev 		mlx4_info(dev, "MAC invalidation is not allowed when spoofchk is on\n");
2996745d8ae4SEugenia Emantayev 		return -EPERM;
2997745d8ae4SEugenia Emantayev 	}
2998745d8ae4SEugenia Emantayev 
2999ded6e16bSJakub Kicinski 	s_info->mac = ether_addr_to_u64(mac);
3000613d8c18SCarol Soto 	mlx4_info(dev, "default mac on vf %d port %d to %llX will take effect only after vf restart\n",
30018f7ba3caSRony Efraim 		  vf, port, s_info->mac);
30028f7ba3caSRony Efraim 	return 0;
30038f7ba3caSRony Efraim }
30048f7ba3caSRony Efraim EXPORT_SYMBOL_GPL(mlx4_set_vf_mac);
30053f7fb021SRony Efraim 
3006b01978caSJack Morgenstein 
mlx4_set_vf_vlan(struct mlx4_dev * dev,int port,int vf,u16 vlan,u8 qos,__be16 proto)3007b42959dcSMoshe Shemesh int mlx4_set_vf_vlan(struct mlx4_dev *dev, int port, int vf, u16 vlan, u8 qos,
3008b42959dcSMoshe Shemesh 		     __be16 proto)
30093f7fb021SRony Efraim {
30103f7fb021SRony Efraim 	struct mlx4_priv *priv = mlx4_priv(dev);
3011b01978caSJack Morgenstein 	struct mlx4_vport_state *vf_admin;
3012b42959dcSMoshe Shemesh 	struct mlx4_slave_state *slave_state;
3013b42959dcSMoshe Shemesh 	struct mlx4_vport_oper_state *vf_oper;
30143f7fb021SRony Efraim 	int slave;
30153f7fb021SRony Efraim 
30163f7fb021SRony Efraim 	if ((!mlx4_is_master(dev)) ||
30173f7fb021SRony Efraim 	    !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_VLAN_CONTROL))
30183f7fb021SRony Efraim 		return -EPROTONOSUPPORT;
30193f7fb021SRony Efraim 
30203f7fb021SRony Efraim 	if ((vlan > 4095) || (qos > 7))
30213f7fb021SRony Efraim 		return -EINVAL;
30223f7fb021SRony Efraim 
3023b42959dcSMoshe Shemesh 	if (proto == htons(ETH_P_8021AD) &&
3024b42959dcSMoshe Shemesh 	    !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SVLAN_BY_QP))
3025b42959dcSMoshe Shemesh 		return -EPROTONOSUPPORT;
3026b42959dcSMoshe Shemesh 
3027b42959dcSMoshe Shemesh 	if (proto != htons(ETH_P_8021Q) &&
3028b42959dcSMoshe Shemesh 	    proto != htons(ETH_P_8021AD))
3029b42959dcSMoshe Shemesh 		return -EINVAL;
3030b42959dcSMoshe Shemesh 
3031b42959dcSMoshe Shemesh 	if ((proto == htons(ETH_P_8021AD)) &&
3032b42959dcSMoshe Shemesh 	    ((vlan == 0) || (vlan == MLX4_VGT)))
3033b42959dcSMoshe Shemesh 		return -EINVAL;
3034b42959dcSMoshe Shemesh 
30353f7fb021SRony Efraim 	slave = mlx4_get_slave_indx(dev, vf);
30363f7fb021SRony Efraim 	if (slave < 0)
30373f7fb021SRony Efraim 		return -EINVAL;
30383f7fb021SRony Efraim 
3039b42959dcSMoshe Shemesh 	slave_state = &priv->mfunc.master.slave_state[slave];
3040b42959dcSMoshe Shemesh 	if ((proto == htons(ETH_P_8021AD)) && (slave_state->active) &&
3041b42959dcSMoshe Shemesh 	    (!slave_state->vst_qinq_supported)) {
3042b42959dcSMoshe Shemesh 		mlx4_err(dev, "vf %d does not support VST QinQ mode\n", vf);
3043b42959dcSMoshe Shemesh 		return -EPROTONOSUPPORT;
3044b42959dcSMoshe Shemesh 	}
3045a91c772fSMatan Barak 	port = mlx4_slaves_closest_port(dev, slave, port);
3046b01978caSJack Morgenstein 	vf_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
3047b42959dcSMoshe Shemesh 	vf_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
3048b01978caSJack Morgenstein 
3049cda373f4SIdo Shamay 	if (!mlx4_valid_vf_state_change(dev, port, vf_admin, vlan, qos))
3050cda373f4SIdo Shamay 		return -EPERM;
3051cda373f4SIdo Shamay 
30523f7fb021SRony Efraim 	if ((0 == vlan) && (0 == qos))
3053b01978caSJack Morgenstein 		vf_admin->default_vlan = MLX4_VGT;
30543f7fb021SRony Efraim 	else
3055b01978caSJack Morgenstein 		vf_admin->default_vlan = vlan;
3056b01978caSJack Morgenstein 	vf_admin->default_qos = qos;
3057b42959dcSMoshe Shemesh 	vf_admin->vlan_proto = proto;
3058b01978caSJack Morgenstein 
3059cda373f4SIdo Shamay 	/* If rate was configured prior to VST, we saved the configured rate
3060cda373f4SIdo Shamay 	 * in vf_admin->rate and now, if priority supported we enforce the QoS
3061cda373f4SIdo Shamay 	 */
3062cda373f4SIdo Shamay 	if (mlx4_is_vf_vst_and_prio_qos(dev, port, vf_admin) &&
3063cda373f4SIdo Shamay 	    vf_admin->tx_rate)
3064cda373f4SIdo Shamay 		vf_admin->qos_vport = slave;
3065cda373f4SIdo Shamay 
3066b42959dcSMoshe Shemesh 	/* Try to activate new vf state without restart,
3067b42959dcSMoshe Shemesh 	 * this option is not supported while moving to VST QinQ mode.
3068b42959dcSMoshe Shemesh 	 */
3069b42959dcSMoshe Shemesh 	if ((proto == htons(ETH_P_8021AD) &&
3070b42959dcSMoshe Shemesh 	     vf_oper->state.vlan_proto != proto) ||
3071b42959dcSMoshe Shemesh 	    mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
30720a6eac24SRony Efraim 		mlx4_info(dev,
30730a6eac24SRony Efraim 			  "updating vf %d port %d config will take effect on next VF restart\n",
3074b01978caSJack Morgenstein 			  vf, port);
30753f7fb021SRony Efraim 	return 0;
30763f7fb021SRony Efraim }
30773f7fb021SRony Efraim EXPORT_SYMBOL_GPL(mlx4_set_vf_vlan);
3078e6b6a231SRony Efraim 
mlx4_set_vf_rate(struct mlx4_dev * dev,int port,int vf,int min_tx_rate,int max_tx_rate)3079cda373f4SIdo Shamay int mlx4_set_vf_rate(struct mlx4_dev *dev, int port, int vf, int min_tx_rate,
3080cda373f4SIdo Shamay 		     int max_tx_rate)
3081cda373f4SIdo Shamay {
3082cda373f4SIdo Shamay 	int err;
3083cda373f4SIdo Shamay 	int slave;
3084cda373f4SIdo Shamay 	struct mlx4_vport_state *vf_admin;
3085cda373f4SIdo Shamay 	struct mlx4_priv *priv = mlx4_priv(dev);
3086cda373f4SIdo Shamay 
3087cda373f4SIdo Shamay 	if (!mlx4_is_master(dev) ||
3088cda373f4SIdo Shamay 	    !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QOS_VPP))
3089cda373f4SIdo Shamay 		return -EPROTONOSUPPORT;
3090cda373f4SIdo Shamay 
3091cda373f4SIdo Shamay 	if (min_tx_rate) {
3092cda373f4SIdo Shamay 		mlx4_info(dev, "Minimum BW share not supported\n");
3093cda373f4SIdo Shamay 		return -EPROTONOSUPPORT;
3094cda373f4SIdo Shamay 	}
3095cda373f4SIdo Shamay 
3096cda373f4SIdo Shamay 	slave = mlx4_get_slave_indx(dev, vf);
3097cda373f4SIdo Shamay 	if (slave < 0)
3098cda373f4SIdo Shamay 		return -EINVAL;
3099cda373f4SIdo Shamay 
3100cda373f4SIdo Shamay 	port = mlx4_slaves_closest_port(dev, slave, port);
3101cda373f4SIdo Shamay 	vf_admin = &priv->mfunc.master.vf_admin[slave].vport[port];
3102cda373f4SIdo Shamay 
3103cda373f4SIdo Shamay 	err = mlx4_set_vport_qos(priv, slave, port, max_tx_rate);
3104cda373f4SIdo Shamay 	if (err) {
3105cda373f4SIdo Shamay 		mlx4_info(dev, "vf %d failed to set rate %d\n", vf,
3106cda373f4SIdo Shamay 			  max_tx_rate);
3107cda373f4SIdo Shamay 		return err;
3108cda373f4SIdo Shamay 	}
3109cda373f4SIdo Shamay 
3110cda373f4SIdo Shamay 	vf_admin->tx_rate = max_tx_rate;
3111cda373f4SIdo Shamay 	/* if VF is not in supported mode (VST with supported prio),
3112cda373f4SIdo Shamay 	 * we do not change vport configuration for its QPs, but save
3113cda373f4SIdo Shamay 	 * the rate, so it will be enforced when it moves to supported
3114cda373f4SIdo Shamay 	 * mode next time.
3115cda373f4SIdo Shamay 	 */
3116cda373f4SIdo Shamay 	if (!mlx4_is_vf_vst_and_prio_qos(dev, port, vf_admin)) {
3117cda373f4SIdo Shamay 		mlx4_info(dev,
3118cda373f4SIdo Shamay 			  "rate set for VF %d when not in valid state\n", vf);
3119cda373f4SIdo Shamay 
3120cda373f4SIdo Shamay 		if (vf_admin->default_vlan != MLX4_VGT)
3121cda373f4SIdo Shamay 			mlx4_info(dev, "VST priority not supported by QoS\n");
3122cda373f4SIdo Shamay 		else
3123cda373f4SIdo Shamay 			mlx4_info(dev, "VF in VGT mode (needed VST)\n");
3124cda373f4SIdo Shamay 
3125cda373f4SIdo Shamay 		mlx4_info(dev,
3126cda373f4SIdo Shamay 			  "rate %d take affect when VF moves to valid state\n",
3127cda373f4SIdo Shamay 			  max_tx_rate);
3128cda373f4SIdo Shamay 		return 0;
3129cda373f4SIdo Shamay 	}
3130cda373f4SIdo Shamay 
3131cda373f4SIdo Shamay 	/* If user sets rate 0 assigning default vport for its QPs */
3132cda373f4SIdo Shamay 	vf_admin->qos_vport = max_tx_rate ? slave : MLX4_VPP_DEFAULT_VPORT;
3133cda373f4SIdo Shamay 
3134cda373f4SIdo Shamay 	if (priv->mfunc.master.slave_state[slave].active &&
3135cda373f4SIdo Shamay 	    dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_UPDATE_QP)
3136cda373f4SIdo Shamay 		mlx4_master_immediate_activate_vlan_qos(priv, slave, port);
3137cda373f4SIdo Shamay 
3138cda373f4SIdo Shamay 	return 0;
3139cda373f4SIdo Shamay }
3140cda373f4SIdo Shamay EXPORT_SYMBOL_GPL(mlx4_set_vf_rate);
3141cda373f4SIdo Shamay 
31425ea8bbfcSJack Morgenstein  /* mlx4_get_slave_default_vlan -
31435ea8bbfcSJack Morgenstein  * return true if VST ( default vlan)
31445ea8bbfcSJack Morgenstein  * if VST, will return vlan & qos (if not NULL)
31455ea8bbfcSJack Morgenstein  */
mlx4_get_slave_default_vlan(struct mlx4_dev * dev,int port,int slave,u16 * vlan,u8 * qos)31465ea8bbfcSJack Morgenstein bool mlx4_get_slave_default_vlan(struct mlx4_dev *dev, int port, int slave,
31475ea8bbfcSJack Morgenstein 				 u16 *vlan, u8 *qos)
31485ea8bbfcSJack Morgenstein {
31495ea8bbfcSJack Morgenstein 	struct mlx4_vport_oper_state *vp_oper;
31505ea8bbfcSJack Morgenstein 	struct mlx4_priv *priv;
31515ea8bbfcSJack Morgenstein 
31525ea8bbfcSJack Morgenstein 	priv = mlx4_priv(dev);
3153a91c772fSMatan Barak 	port = mlx4_slaves_closest_port(dev, slave, port);
31545ea8bbfcSJack Morgenstein 	vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
31555ea8bbfcSJack Morgenstein 
31565ea8bbfcSJack Morgenstein 	if (MLX4_VGT != vp_oper->state.default_vlan) {
31575ea8bbfcSJack Morgenstein 		if (vlan)
31585ea8bbfcSJack Morgenstein 			*vlan = vp_oper->state.default_vlan;
31595ea8bbfcSJack Morgenstein 		if (qos)
31605ea8bbfcSJack Morgenstein 			*qos = vp_oper->state.default_qos;
31615ea8bbfcSJack Morgenstein 		return true;
31625ea8bbfcSJack Morgenstein 	}
31635ea8bbfcSJack Morgenstein 	return false;
31645ea8bbfcSJack Morgenstein }
31655ea8bbfcSJack Morgenstein EXPORT_SYMBOL_GPL(mlx4_get_slave_default_vlan);
31665ea8bbfcSJack Morgenstein 
mlx4_set_vf_spoofchk(struct mlx4_dev * dev,int port,int vf,bool setting)3167e6b6a231SRony Efraim int mlx4_set_vf_spoofchk(struct mlx4_dev *dev, int port, int vf, bool setting)
3168e6b6a231SRony Efraim {
3169e6b6a231SRony Efraim 	struct mlx4_priv *priv = mlx4_priv(dev);
3170e6b6a231SRony Efraim 	struct mlx4_vport_state *s_info;
3171e6b6a231SRony Efraim 	int slave;
3172745d8ae4SEugenia Emantayev 	u8 mac[ETH_ALEN];
3173e6b6a231SRony Efraim 
3174e6b6a231SRony Efraim 	if ((!mlx4_is_master(dev)) ||
3175e6b6a231SRony Efraim 	    !(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FSM))
3176e6b6a231SRony Efraim 		return -EPROTONOSUPPORT;
3177e6b6a231SRony Efraim 
3178e6b6a231SRony Efraim 	slave = mlx4_get_slave_indx(dev, vf);
3179e6b6a231SRony Efraim 	if (slave < 0)
3180e6b6a231SRony Efraim 		return -EINVAL;
3181e6b6a231SRony Efraim 
3182a91c772fSMatan Barak 	port = mlx4_slaves_closest_port(dev, slave, port);
3183e6b6a231SRony Efraim 	s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
3184745d8ae4SEugenia Emantayev 
31851bb96a07SJakub Kicinski 	u64_to_ether_addr(s_info->mac, mac);
3186745d8ae4SEugenia Emantayev 	if (setting && !is_valid_ether_addr(mac)) {
3187745d8ae4SEugenia Emantayev 		mlx4_info(dev, "Illegal MAC with spoofchk\n");
3188745d8ae4SEugenia Emantayev 		return -EPERM;
3189745d8ae4SEugenia Emantayev 	}
3190745d8ae4SEugenia Emantayev 
3191e6b6a231SRony Efraim 	s_info->spoofchk = setting;
3192e6b6a231SRony Efraim 
3193e6b6a231SRony Efraim 	return 0;
3194e6b6a231SRony Efraim }
3195e6b6a231SRony Efraim EXPORT_SYMBOL_GPL(mlx4_set_vf_spoofchk);
31962cccb9e4SRony Efraim 
mlx4_get_vf_config(struct mlx4_dev * dev,int port,int vf,struct ifla_vf_info * ivf)31972cccb9e4SRony Efraim int mlx4_get_vf_config(struct mlx4_dev *dev, int port, int vf, struct ifla_vf_info *ivf)
31982cccb9e4SRony Efraim {
31992cccb9e4SRony Efraim 	struct mlx4_priv *priv = mlx4_priv(dev);
32002cccb9e4SRony Efraim 	struct mlx4_vport_state *s_info;
32012cccb9e4SRony Efraim 	int slave;
32022cccb9e4SRony Efraim 
32032cccb9e4SRony Efraim 	if (!mlx4_is_master(dev))
32042cccb9e4SRony Efraim 		return -EPROTONOSUPPORT;
32052cccb9e4SRony Efraim 
32062cccb9e4SRony Efraim 	slave = mlx4_get_slave_indx(dev, vf);
32072cccb9e4SRony Efraim 	if (slave < 0)
32082cccb9e4SRony Efraim 		return -EINVAL;
32092cccb9e4SRony Efraim 
32102cccb9e4SRony Efraim 	s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
32112cccb9e4SRony Efraim 	ivf->vf = vf;
32122cccb9e4SRony Efraim 
32132cccb9e4SRony Efraim 	/* need to convert it to a func */
32142cccb9e4SRony Efraim 	ivf->mac[0] = ((s_info->mac >> (5*8)) & 0xff);
32152cccb9e4SRony Efraim 	ivf->mac[1] = ((s_info->mac >> (4*8)) & 0xff);
32162cccb9e4SRony Efraim 	ivf->mac[2] = ((s_info->mac >> (3*8)) & 0xff);
32172cccb9e4SRony Efraim 	ivf->mac[3] = ((s_info->mac >> (2*8)) & 0xff);
32182cccb9e4SRony Efraim 	ivf->mac[4] = ((s_info->mac >> (1*8)) & 0xff);
32192cccb9e4SRony Efraim 	ivf->mac[5] = ((s_info->mac)  & 0xff);
32202cccb9e4SRony Efraim 
32212cccb9e4SRony Efraim 	ivf->vlan		= s_info->default_vlan;
32222cccb9e4SRony Efraim 	ivf->qos		= s_info->default_qos;
3223b42959dcSMoshe Shemesh 	ivf->vlan_proto		= s_info->vlan_proto;
3224cda373f4SIdo Shamay 
3225cda373f4SIdo Shamay 	if (mlx4_is_vf_vst_and_prio_qos(dev, port, s_info))
3226ed616689SSucheta Chakraborty 		ivf->max_tx_rate = s_info->tx_rate;
3227cda373f4SIdo Shamay 	else
3228cda373f4SIdo Shamay 		ivf->max_tx_rate = 0;
3229cda373f4SIdo Shamay 
3230ed616689SSucheta Chakraborty 	ivf->min_tx_rate	= 0;
32312cccb9e4SRony Efraim 	ivf->spoofchk		= s_info->spoofchk;
3232948e306dSRony Efraim 	ivf->linkstate		= s_info->link_state;
32332cccb9e4SRony Efraim 
32342cccb9e4SRony Efraim 	return 0;
32352cccb9e4SRony Efraim }
32362cccb9e4SRony Efraim EXPORT_SYMBOL_GPL(mlx4_get_vf_config);
3237948e306dSRony Efraim 
mlx4_set_vf_link_state(struct mlx4_dev * dev,int port,int vf,int link_state)3238948e306dSRony Efraim int mlx4_set_vf_link_state(struct mlx4_dev *dev, int port, int vf, int link_state)
3239948e306dSRony Efraim {
3240948e306dSRony Efraim 	struct mlx4_priv *priv = mlx4_priv(dev);
3241948e306dSRony Efraim 	struct mlx4_vport_state *s_info;
3242948e306dSRony Efraim 	int slave;
3243948e306dSRony Efraim 	u8 link_stat_event;
3244948e306dSRony Efraim 
3245948e306dSRony Efraim 	slave = mlx4_get_slave_indx(dev, vf);
3246948e306dSRony Efraim 	if (slave < 0)
3247948e306dSRony Efraim 		return -EINVAL;
3248948e306dSRony Efraim 
3249a91c772fSMatan Barak 	port = mlx4_slaves_closest_port(dev, slave, port);
3250948e306dSRony Efraim 	switch (link_state) {
3251948e306dSRony Efraim 	case IFLA_VF_LINK_STATE_AUTO:
3252948e306dSRony Efraim 		/* get current link state */
3253948e306dSRony Efraim 		if (!priv->sense.do_sense_port[port])
3254948e306dSRony Efraim 			link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
3255948e306dSRony Efraim 		else
3256948e306dSRony Efraim 			link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
3257948e306dSRony Efraim 	    break;
3258948e306dSRony Efraim 
3259948e306dSRony Efraim 	case IFLA_VF_LINK_STATE_ENABLE:
3260948e306dSRony Efraim 		link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_ACTIVE;
3261948e306dSRony Efraim 	    break;
3262948e306dSRony Efraim 
3263948e306dSRony Efraim 	case IFLA_VF_LINK_STATE_DISABLE:
3264948e306dSRony Efraim 		link_stat_event = MLX4_PORT_CHANGE_SUBTYPE_DOWN;
3265948e306dSRony Efraim 	    break;
3266948e306dSRony Efraim 
3267948e306dSRony Efraim 	default:
3268948e306dSRony Efraim 		mlx4_warn(dev, "unknown value for link_state %02x on slave %d port %d\n",
3269948e306dSRony Efraim 			  link_state, slave, port);
3270948e306dSRony Efraim 		return -EINVAL;
3271bec03debSYueHaibing 	}
3272948e306dSRony Efraim 	s_info = &priv->mfunc.master.vf_admin[slave].vport[port];
3273948e306dSRony Efraim 	s_info->link_state = link_state;
3274948e306dSRony Efraim 
3275948e306dSRony Efraim 	/* send event */
3276948e306dSRony Efraim 	mlx4_gen_port_state_change_eqe(dev, slave, port, link_stat_event);
32770a6eac24SRony Efraim 
32780a6eac24SRony Efraim 	if (mlx4_master_immediate_activate_vlan_qos(priv, slave, port))
32790a6eac24SRony Efraim 		mlx4_dbg(dev,
328046ccf725SColin Ian King 			 "updating vf %d port %d no link state HW enforcement\n",
32810a6eac24SRony Efraim 			 vf, port);
3282948e306dSRony Efraim 	return 0;
3283948e306dSRony Efraim }
3284948e306dSRony Efraim EXPORT_SYMBOL_GPL(mlx4_set_vf_link_state);
328597982f5aSJack Morgenstein 
mlx4_get_counter_stats(struct mlx4_dev * dev,int counter_index,struct mlx4_counter * counter_stats,int reset)32869616982fSEran Ben Elisha int mlx4_get_counter_stats(struct mlx4_dev *dev, int counter_index,
32879616982fSEran Ben Elisha 			   struct mlx4_counter *counter_stats, int reset)
32889616982fSEran Ben Elisha {
32899616982fSEran Ben Elisha 	struct mlx4_cmd_mailbox *mailbox = NULL;
32909616982fSEran Ben Elisha 	struct mlx4_counter *tmp_counter;
32919616982fSEran Ben Elisha 	int err;
32929616982fSEran Ben Elisha 	u32 if_stat_in_mod;
32939616982fSEran Ben Elisha 
32949616982fSEran Ben Elisha 	if (!counter_stats)
32959616982fSEran Ben Elisha 		return -EINVAL;
32969616982fSEran Ben Elisha 
32979616982fSEran Ben Elisha 	if (counter_index == MLX4_SINK_COUNTER_INDEX(dev))
32989616982fSEran Ben Elisha 		return 0;
32999616982fSEran Ben Elisha 
33009616982fSEran Ben Elisha 	mailbox = mlx4_alloc_cmd_mailbox(dev);
33019616982fSEran Ben Elisha 	if (IS_ERR(mailbox))
33029616982fSEran Ben Elisha 		return PTR_ERR(mailbox);
33039616982fSEran Ben Elisha 
33049616982fSEran Ben Elisha 	memset(mailbox->buf, 0, sizeof(struct mlx4_counter));
33059616982fSEran Ben Elisha 	if_stat_in_mod = counter_index;
33069616982fSEran Ben Elisha 	if (reset)
33079616982fSEran Ben Elisha 		if_stat_in_mod |= MLX4_QUERY_IF_STAT_RESET;
33089616982fSEran Ben Elisha 	err = mlx4_cmd_box(dev, 0, mailbox->dma,
33099616982fSEran Ben Elisha 			   if_stat_in_mod, 0,
33109616982fSEran Ben Elisha 			   MLX4_CMD_QUERY_IF_STAT,
33119616982fSEran Ben Elisha 			   MLX4_CMD_TIME_CLASS_C,
33129616982fSEran Ben Elisha 			   MLX4_CMD_NATIVE);
33139616982fSEran Ben Elisha 	if (err) {
33149616982fSEran Ben Elisha 		mlx4_dbg(dev, "%s: failed to read statistics for counter index %d\n",
33159616982fSEran Ben Elisha 			 __func__, counter_index);
33169616982fSEran Ben Elisha 		goto if_stat_out;
33179616982fSEran Ben Elisha 	}
33189616982fSEran Ben Elisha 	tmp_counter = (struct mlx4_counter *)mailbox->buf;
33199616982fSEran Ben Elisha 	counter_stats->counter_mode = tmp_counter->counter_mode;
33209616982fSEran Ben Elisha 	if (counter_stats->counter_mode == 0) {
33219616982fSEran Ben Elisha 		counter_stats->rx_frames =
33229616982fSEran Ben Elisha 			cpu_to_be64(be64_to_cpu(counter_stats->rx_frames) +
33239616982fSEran Ben Elisha 				    be64_to_cpu(tmp_counter->rx_frames));
33249616982fSEran Ben Elisha 		counter_stats->tx_frames =
33259616982fSEran Ben Elisha 			cpu_to_be64(be64_to_cpu(counter_stats->tx_frames) +
33269616982fSEran Ben Elisha 				    be64_to_cpu(tmp_counter->tx_frames));
33279616982fSEran Ben Elisha 		counter_stats->rx_bytes =
33289616982fSEran Ben Elisha 			cpu_to_be64(be64_to_cpu(counter_stats->rx_bytes) +
33299616982fSEran Ben Elisha 				    be64_to_cpu(tmp_counter->rx_bytes));
33309616982fSEran Ben Elisha 		counter_stats->tx_bytes =
33319616982fSEran Ben Elisha 			cpu_to_be64(be64_to_cpu(counter_stats->tx_bytes) +
33329616982fSEran Ben Elisha 				    be64_to_cpu(tmp_counter->tx_bytes));
33339616982fSEran Ben Elisha 	}
33349616982fSEran Ben Elisha 
33359616982fSEran Ben Elisha if_stat_out:
33369616982fSEran Ben Elisha 	mlx4_free_cmd_mailbox(dev, mailbox);
33379616982fSEran Ben Elisha 
33389616982fSEran Ben Elisha 	return err;
33399616982fSEran Ben Elisha }
33409616982fSEran Ben Elisha EXPORT_SYMBOL_GPL(mlx4_get_counter_stats);
33419616982fSEran Ben Elisha 
mlx4_get_vf_stats(struct mlx4_dev * dev,int port,int vf_idx,struct ifla_vf_stats * vf_stats)334262a89055SEran Ben Elisha int mlx4_get_vf_stats(struct mlx4_dev *dev, int port, int vf_idx,
334362a89055SEran Ben Elisha 		      struct ifla_vf_stats *vf_stats)
334462a89055SEran Ben Elisha {
334562a89055SEran Ben Elisha 	struct mlx4_counter tmp_vf_stats;
334662a89055SEran Ben Elisha 	int slave;
334762a89055SEran Ben Elisha 	int err = 0;
334862a89055SEran Ben Elisha 
334962a89055SEran Ben Elisha 	if (!vf_stats)
335062a89055SEran Ben Elisha 		return -EINVAL;
335162a89055SEran Ben Elisha 
335262a89055SEran Ben Elisha 	if (!mlx4_is_master(dev))
335362a89055SEran Ben Elisha 		return -EPROTONOSUPPORT;
335462a89055SEran Ben Elisha 
335562a89055SEran Ben Elisha 	slave = mlx4_get_slave_indx(dev, vf_idx);
335662a89055SEran Ben Elisha 	if (slave < 0)
335762a89055SEran Ben Elisha 		return -EINVAL;
335862a89055SEran Ben Elisha 
335962a89055SEran Ben Elisha 	port = mlx4_slaves_closest_port(dev, slave, port);
336062a89055SEran Ben Elisha 	err = mlx4_calc_vf_counters(dev, slave, port, &tmp_vf_stats);
336162a89055SEran Ben Elisha 	if (!err && tmp_vf_stats.counter_mode == 0) {
336262a89055SEran Ben Elisha 		vf_stats->rx_packets = be64_to_cpu(tmp_vf_stats.rx_frames);
336362a89055SEran Ben Elisha 		vf_stats->tx_packets = be64_to_cpu(tmp_vf_stats.tx_frames);
336462a89055SEran Ben Elisha 		vf_stats->rx_bytes = be64_to_cpu(tmp_vf_stats.rx_bytes);
336562a89055SEran Ben Elisha 		vf_stats->tx_bytes = be64_to_cpu(tmp_vf_stats.tx_bytes);
336662a89055SEran Ben Elisha 	}
336762a89055SEran Ben Elisha 
336862a89055SEran Ben Elisha 	return err;
336962a89055SEran Ben Elisha }
337062a89055SEran Ben Elisha EXPORT_SYMBOL_GPL(mlx4_get_vf_stats);
337162a89055SEran Ben Elisha 
mlx4_vf_smi_enabled(struct mlx4_dev * dev,int slave,int port)337297982f5aSJack Morgenstein int mlx4_vf_smi_enabled(struct mlx4_dev *dev, int slave, int port)
337397982f5aSJack Morgenstein {
337499ec41d0SJack Morgenstein 	struct mlx4_priv *priv = mlx4_priv(dev);
337599ec41d0SJack Morgenstein 
337699ec41d0SJack Morgenstein 	if (slave < 1 || slave >= dev->num_slaves ||
337799ec41d0SJack Morgenstein 	    port < 1 || port > MLX4_MAX_PORTS)
337897982f5aSJack Morgenstein 		return 0;
337999ec41d0SJack Morgenstein 
338099ec41d0SJack Morgenstein 	return priv->mfunc.master.vf_oper[slave].smi_enabled[port] ==
338199ec41d0SJack Morgenstein 		MLX4_VF_SMI_ENABLED;
338297982f5aSJack Morgenstein }
338397982f5aSJack Morgenstein EXPORT_SYMBOL_GPL(mlx4_vf_smi_enabled);
338465fed8a8SJack Morgenstein 
mlx4_vf_get_enable_smi_admin(struct mlx4_dev * dev,int slave,int port)338565fed8a8SJack Morgenstein int mlx4_vf_get_enable_smi_admin(struct mlx4_dev *dev, int slave, int port)
338665fed8a8SJack Morgenstein {
338765fed8a8SJack Morgenstein 	struct mlx4_priv *priv = mlx4_priv(dev);
338865fed8a8SJack Morgenstein 
338965fed8a8SJack Morgenstein 	if (slave == mlx4_master_func_num(dev))
339065fed8a8SJack Morgenstein 		return 1;
339165fed8a8SJack Morgenstein 
339265fed8a8SJack Morgenstein 	if (slave < 1 || slave >= dev->num_slaves ||
339365fed8a8SJack Morgenstein 	    port < 1 || port > MLX4_MAX_PORTS)
339465fed8a8SJack Morgenstein 		return 0;
339565fed8a8SJack Morgenstein 
339665fed8a8SJack Morgenstein 	return priv->mfunc.master.vf_admin[slave].enable_smi[port] ==
339765fed8a8SJack Morgenstein 		MLX4_VF_SMI_ENABLED;
339865fed8a8SJack Morgenstein }
339965fed8a8SJack Morgenstein EXPORT_SYMBOL_GPL(mlx4_vf_get_enable_smi_admin);
340065fed8a8SJack Morgenstein 
mlx4_vf_set_enable_smi_admin(struct mlx4_dev * dev,int slave,int port,int enabled)340165fed8a8SJack Morgenstein int mlx4_vf_set_enable_smi_admin(struct mlx4_dev *dev, int slave, int port,
340265fed8a8SJack Morgenstein 				 int enabled)
340365fed8a8SJack Morgenstein {
340465fed8a8SJack Morgenstein 	struct mlx4_priv *priv = mlx4_priv(dev);
3405be9b9ecaSOr Gerlitz 	struct mlx4_active_ports actv_ports = mlx4_get_active_ports(
3406be9b9ecaSOr Gerlitz 			&priv->dev, slave);
3407be9b9ecaSOr Gerlitz 	int min_port = find_first_bit(actv_ports.ports,
3408be9b9ecaSOr Gerlitz 				      priv->dev.caps.num_ports) + 1;
3409be9b9ecaSOr Gerlitz 	int max_port = min_port - 1 +
3410be9b9ecaSOr Gerlitz 		bitmap_weight(actv_ports.ports, priv->dev.caps.num_ports);
341165fed8a8SJack Morgenstein 
341265fed8a8SJack Morgenstein 	if (slave == mlx4_master_func_num(dev))
341365fed8a8SJack Morgenstein 		return 0;
341465fed8a8SJack Morgenstein 
341565fed8a8SJack Morgenstein 	if (slave < 1 || slave >= dev->num_slaves ||
341665fed8a8SJack Morgenstein 	    port < 1 || port > MLX4_MAX_PORTS ||
341765fed8a8SJack Morgenstein 	    enabled < 0 || enabled > 1)
341865fed8a8SJack Morgenstein 		return -EINVAL;
341965fed8a8SJack Morgenstein 
3420be9b9ecaSOr Gerlitz 	if (min_port == max_port && dev->caps.num_ports > 1) {
3421be9b9ecaSOr Gerlitz 		mlx4_info(dev, "SMI access disallowed for single ported VFs\n");
3422be9b9ecaSOr Gerlitz 		return -EPROTONOSUPPORT;
3423be9b9ecaSOr Gerlitz 	}
3424be9b9ecaSOr Gerlitz 
342565fed8a8SJack Morgenstein 	priv->mfunc.master.vf_admin[slave].enable_smi[port] = enabled;
342665fed8a8SJack Morgenstein 	return 0;
342765fed8a8SJack Morgenstein }
342865fed8a8SJack Morgenstein EXPORT_SYMBOL_GPL(mlx4_vf_set_enable_smi_admin);
3429