xref: /openbmc/linux/drivers/net/ethernet/chelsio/cxgb4vf/t4vf_hw.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1f7917c00SJeff Kirsher /*
2f7917c00SJeff Kirsher  * This file is part of the Chelsio T4 PCI-E SR-IOV Virtual Function Ethernet
3f7917c00SJeff Kirsher  * driver for Linux.
4f7917c00SJeff Kirsher  *
5f7917c00SJeff Kirsher  * Copyright (c) 2009-2010 Chelsio Communications, Inc. All rights reserved.
6f7917c00SJeff Kirsher  *
7f7917c00SJeff Kirsher  * This software is available to you under a choice of one of two
8f7917c00SJeff Kirsher  * licenses.  You may choose to be licensed under the terms of the GNU
9f7917c00SJeff Kirsher  * General Public License (GPL) Version 2, available from the file
10f7917c00SJeff Kirsher  * COPYING in the main directory of this source tree, or the
11f7917c00SJeff Kirsher  * OpenIB.org BSD license below:
12f7917c00SJeff Kirsher  *
13f7917c00SJeff Kirsher  *     Redistribution and use in source and binary forms, with or
14f7917c00SJeff Kirsher  *     without modification, are permitted provided that the following
15f7917c00SJeff Kirsher  *     conditions are met:
16f7917c00SJeff Kirsher  *
17f7917c00SJeff Kirsher  *      - Redistributions of source code must retain the above
18f7917c00SJeff Kirsher  *        copyright notice, this list of conditions and the following
19f7917c00SJeff Kirsher  *        disclaimer.
20f7917c00SJeff Kirsher  *
21f7917c00SJeff Kirsher  *      - Redistributions in binary form must reproduce the above
22f7917c00SJeff Kirsher  *        copyright notice, this list of conditions and the following
23f7917c00SJeff Kirsher  *        disclaimer in the documentation and/or other materials
24f7917c00SJeff Kirsher  *        provided with the distribution.
25f7917c00SJeff Kirsher  *
26f7917c00SJeff Kirsher  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27f7917c00SJeff Kirsher  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28f7917c00SJeff Kirsher  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29f7917c00SJeff Kirsher  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30f7917c00SJeff Kirsher  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31f7917c00SJeff Kirsher  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32f7917c00SJeff Kirsher  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33f7917c00SJeff Kirsher  * SOFTWARE.
34f7917c00SJeff Kirsher  */
35f7917c00SJeff Kirsher 
36cc69837fSJakub Kicinski #include <linux/ethtool.h>
37f7917c00SJeff Kirsher #include <linux/pci.h>
38f7917c00SJeff Kirsher 
39f7917c00SJeff Kirsher #include "t4vf_common.h"
40f7917c00SJeff Kirsher #include "t4vf_defs.h"
41f7917c00SJeff Kirsher 
42f7917c00SJeff Kirsher #include "../cxgb4/t4_regs.h"
43f612b815SHariprasad Shenai #include "../cxgb4/t4_values.h"
44f7917c00SJeff Kirsher #include "../cxgb4/t4fw_api.h"
45f7917c00SJeff Kirsher 
46f7917c00SJeff Kirsher /*
47f7917c00SJeff Kirsher  * Wait for the device to become ready (signified by our "who am I" register
48f7917c00SJeff Kirsher  * returning a value other than all 1's).  Return an error if it doesn't
49f7917c00SJeff Kirsher  * become ready ...
50f7917c00SJeff Kirsher  */
t4vf_wait_dev_ready(struct adapter * adapter)51d289f864SBill Pemberton int t4vf_wait_dev_ready(struct adapter *adapter)
52f7917c00SJeff Kirsher {
53f7917c00SJeff Kirsher 	const u32 whoami = T4VF_PL_BASE_ADDR + PL_VF_WHOAMI;
54f7917c00SJeff Kirsher 	const u32 notready1 = 0xffffffff;
55f7917c00SJeff Kirsher 	const u32 notready2 = 0xeeeeeeee;
56f7917c00SJeff Kirsher 	u32 val;
57f7917c00SJeff Kirsher 
58f7917c00SJeff Kirsher 	val = t4_read_reg(adapter, whoami);
59f7917c00SJeff Kirsher 	if (val != notready1 && val != notready2)
60f7917c00SJeff Kirsher 		return 0;
61f7917c00SJeff Kirsher 	msleep(500);
62f7917c00SJeff Kirsher 	val = t4_read_reg(adapter, whoami);
63f7917c00SJeff Kirsher 	if (val != notready1 && val != notready2)
64f7917c00SJeff Kirsher 		return 0;
65f7917c00SJeff Kirsher 	else
66f7917c00SJeff Kirsher 		return -EIO;
67f7917c00SJeff Kirsher }
68f7917c00SJeff Kirsher 
69f7917c00SJeff Kirsher /*
70f7917c00SJeff Kirsher  * Get the reply to a mailbox command and store it in @rpl in big-endian order
71f7917c00SJeff Kirsher  * (since the firmware data structures are specified in a big-endian layout).
72f7917c00SJeff Kirsher  */
get_mbox_rpl(struct adapter * adapter,__be64 * rpl,int size,u32 mbox_data)73f7917c00SJeff Kirsher static void get_mbox_rpl(struct adapter *adapter, __be64 *rpl, int size,
74f7917c00SJeff Kirsher 			 u32 mbox_data)
75f7917c00SJeff Kirsher {
76f7917c00SJeff Kirsher 	for ( ; size; size -= 8, mbox_data += 8)
77f7917c00SJeff Kirsher 		*rpl++ = cpu_to_be64(t4_read_reg64(adapter, mbox_data));
78f7917c00SJeff Kirsher }
79f7917c00SJeff Kirsher 
80ae7b7576SHariprasad Shenai /**
81ae7b7576SHariprasad Shenai  *	t4vf_record_mbox - record a Firmware Mailbox Command/Reply in the log
82ae7b7576SHariprasad Shenai  *	@adapter: the adapter
83ae7b7576SHariprasad Shenai  *	@cmd: the Firmware Mailbox Command or Reply
84ae7b7576SHariprasad Shenai  *	@size: command length in bytes
85ae7b7576SHariprasad Shenai  *	@access: the time (ms) needed to access the Firmware Mailbox
86ae7b7576SHariprasad Shenai  *	@execute: the time (ms) the command spent being executed
87f7917c00SJeff Kirsher  */
t4vf_record_mbox(struct adapter * adapter,const __be64 * cmd,int size,int access,int execute)88ae7b7576SHariprasad Shenai static void t4vf_record_mbox(struct adapter *adapter, const __be64 *cmd,
89ae7b7576SHariprasad Shenai 			     int size, int access, int execute)
90f7917c00SJeff Kirsher {
91ae7b7576SHariprasad Shenai 	struct mbox_cmd_log *log = adapter->mbox_log;
92ae7b7576SHariprasad Shenai 	struct mbox_cmd *entry;
93ae7b7576SHariprasad Shenai 	int i;
94ae7b7576SHariprasad Shenai 
95ae7b7576SHariprasad Shenai 	entry = mbox_cmd_log_entry(log, log->cursor++);
96ae7b7576SHariprasad Shenai 	if (log->cursor == log->size)
97ae7b7576SHariprasad Shenai 		log->cursor = 0;
98ae7b7576SHariprasad Shenai 
99ae7b7576SHariprasad Shenai 	for (i = 0; i < size / 8; i++)
100ae7b7576SHariprasad Shenai 		entry->cmd[i] = be64_to_cpu(cmd[i]);
101ae7b7576SHariprasad Shenai 	while (i < MBOX_LEN / 8)
102ae7b7576SHariprasad Shenai 		entry->cmd[i++] = 0;
103ae7b7576SHariprasad Shenai 	entry->timestamp = jiffies;
104ae7b7576SHariprasad Shenai 	entry->seqno = log->seqno++;
105ae7b7576SHariprasad Shenai 	entry->access = access;
106ae7b7576SHariprasad Shenai 	entry->execute = execute;
107f7917c00SJeff Kirsher }
108f7917c00SJeff Kirsher 
109f7917c00SJeff Kirsher /**
110f7917c00SJeff Kirsher  *	t4vf_wr_mbox_core - send a command to FW through the mailbox
111f7917c00SJeff Kirsher  *	@adapter: the adapter
112f7917c00SJeff Kirsher  *	@cmd: the command to write
113f7917c00SJeff Kirsher  *	@size: command length in bytes
114f7917c00SJeff Kirsher  *	@rpl: where to optionally store the reply
115f7917c00SJeff Kirsher  *	@sleep_ok: if true we may sleep while awaiting command completion
116f7917c00SJeff Kirsher  *
117f7917c00SJeff Kirsher  *	Sends the given command to FW through the mailbox and waits for the
118f7917c00SJeff Kirsher  *	FW to execute the command.  If @rpl is not %NULL it is used to store
119f7917c00SJeff Kirsher  *	the FW's reply to the command.  The command and its optional reply
120f7917c00SJeff Kirsher  *	are of the same length.  FW can take up to 500 ms to respond.
121f7917c00SJeff Kirsher  *	@sleep_ok determines whether we may sleep while awaiting the response.
122f7917c00SJeff Kirsher  *	If sleeping is allowed we use progressive backoff otherwise we spin.
123f7917c00SJeff Kirsher  *
124f7917c00SJeff Kirsher  *	The return value is 0 on success or a negative errno on failure.  A
125f7917c00SJeff Kirsher  *	failure can happen either because we are not able to execute the
126f7917c00SJeff Kirsher  *	command or FW executes it but signals an error.  In the latter case
127f7917c00SJeff Kirsher  *	the return value is the error code indicated by FW (negated).
128f7917c00SJeff Kirsher  */
t4vf_wr_mbox_core(struct adapter * adapter,const void * cmd,int size,void * rpl,bool sleep_ok)129f7917c00SJeff Kirsher int t4vf_wr_mbox_core(struct adapter *adapter, const void *cmd, int size,
130f7917c00SJeff Kirsher 		      void *rpl, bool sleep_ok)
131f7917c00SJeff Kirsher {
132f7917c00SJeff Kirsher 	static const int delay[] = {
133f7917c00SJeff Kirsher 		1, 1, 3, 5, 10, 10, 20, 50, 100
134f7917c00SJeff Kirsher 	};
135f7917c00SJeff Kirsher 
136ae7b7576SHariprasad Shenai 	u16 access = 0, execute = 0;
13710aa3b78SHariprasad Shenai 	u32 v, mbox_data;
138ae7b7576SHariprasad Shenai 	int i, ms, delay_idx, ret;
139f7917c00SJeff Kirsher 	const __be64 *p;
140f7917c00SJeff Kirsher 	u32 mbox_ctl = T4VF_CIM_BASE_ADDR + CIM_VF_EXT_MAILBOX_CTRL;
141ae7b7576SHariprasad Shenai 	u32 cmd_op = FW_CMD_OP_G(be32_to_cpu(((struct fw_cmd_hdr *)cmd)->hi));
142ae7b7576SHariprasad Shenai 	__be64 cmd_rpl[MBOX_LEN / 8];
143b38066daSHariprasad Shenai 	struct mbox_list entry;
144f7917c00SJeff Kirsher 
14510aa3b78SHariprasad Shenai 	/* In T6, mailbox size is changed to 128 bytes to avoid
14610aa3b78SHariprasad Shenai 	 * invalidating the entire prefetch buffer.
14710aa3b78SHariprasad Shenai 	 */
14810aa3b78SHariprasad Shenai 	if (CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5)
14910aa3b78SHariprasad Shenai 		mbox_data = T4VF_MBDATA_BASE_ADDR;
15010aa3b78SHariprasad Shenai 	else
15110aa3b78SHariprasad Shenai 		mbox_data = T6VF_MBDATA_BASE_ADDR;
15210aa3b78SHariprasad Shenai 
153f7917c00SJeff Kirsher 	/*
154f7917c00SJeff Kirsher 	 * Commands must be multiples of 16 bytes in length and may not be
155f7917c00SJeff Kirsher 	 * larger than the size of the Mailbox Data register array.
156f7917c00SJeff Kirsher 	 */
157f7917c00SJeff Kirsher 	if ((size % 16) != 0 ||
158f7917c00SJeff Kirsher 	    size > NUM_CIM_VF_MAILBOX_DATA_INSTANCES * 4)
159f7917c00SJeff Kirsher 		return -EINVAL;
160f7917c00SJeff Kirsher 
161b38066daSHariprasad Shenai 	/* Queue ourselves onto the mailbox access list.  When our entry is at
162b38066daSHariprasad Shenai 	 * the front of the list, we have rights to access the mailbox.  So we
163b38066daSHariprasad Shenai 	 * wait [for a while] till we're at the front [or bail out with an
164b38066daSHariprasad Shenai 	 * EBUSY] ...
165b38066daSHariprasad Shenai 	 */
166b38066daSHariprasad Shenai 	spin_lock(&adapter->mbox_lock);
167b38066daSHariprasad Shenai 	list_add_tail(&entry.list, &adapter->mlist.list);
168b38066daSHariprasad Shenai 	spin_unlock(&adapter->mbox_lock);
169b38066daSHariprasad Shenai 
170b38066daSHariprasad Shenai 	delay_idx = 0;
171b38066daSHariprasad Shenai 	ms = delay[0];
172b38066daSHariprasad Shenai 
173b38066daSHariprasad Shenai 	for (i = 0; ; i += ms) {
174b38066daSHariprasad Shenai 		/* If we've waited too long, return a busy indication.  This
175b38066daSHariprasad Shenai 		 * really ought to be based on our initial position in the
176b38066daSHariprasad Shenai 		 * mailbox access list but this is a start.  We very rearely
177b38066daSHariprasad Shenai 		 * contend on access to the mailbox ...
178b38066daSHariprasad Shenai 		 */
179b38066daSHariprasad Shenai 		if (i > FW_CMD_MAX_TIMEOUT) {
180b38066daSHariprasad Shenai 			spin_lock(&adapter->mbox_lock);
181b38066daSHariprasad Shenai 			list_del(&entry.list);
182b38066daSHariprasad Shenai 			spin_unlock(&adapter->mbox_lock);
183b38066daSHariprasad Shenai 			ret = -EBUSY;
184b38066daSHariprasad Shenai 			t4vf_record_mbox(adapter, cmd, size, access, ret);
185b38066daSHariprasad Shenai 			return ret;
186b38066daSHariprasad Shenai 		}
187b38066daSHariprasad Shenai 
188b38066daSHariprasad Shenai 		/* If we're at the head, break out and start the mailbox
189b38066daSHariprasad Shenai 		 * protocol.
190b38066daSHariprasad Shenai 		 */
191b38066daSHariprasad Shenai 		if (list_first_entry(&adapter->mlist.list, struct mbox_list,
192b38066daSHariprasad Shenai 				     list) == &entry)
193b38066daSHariprasad Shenai 			break;
194b38066daSHariprasad Shenai 
195b38066daSHariprasad Shenai 		/* Delay for a bit before checking again ... */
196b38066daSHariprasad Shenai 		if (sleep_ok) {
197b38066daSHariprasad Shenai 			ms = delay[delay_idx];  /* last element may repeat */
198b38066daSHariprasad Shenai 			if (delay_idx < ARRAY_SIZE(delay) - 1)
199b38066daSHariprasad Shenai 				delay_idx++;
200b38066daSHariprasad Shenai 			msleep(ms);
201b38066daSHariprasad Shenai 		} else {
202b38066daSHariprasad Shenai 			mdelay(ms);
203b38066daSHariprasad Shenai 		}
204b38066daSHariprasad Shenai 	}
205b38066daSHariprasad Shenai 
206f7917c00SJeff Kirsher 	/*
207f7917c00SJeff Kirsher 	 * Loop trying to get ownership of the mailbox.  Return an error
208f7917c00SJeff Kirsher 	 * if we can't gain ownership.
209f7917c00SJeff Kirsher 	 */
21089c3a86cSHariprasad Shenai 	v = MBOWNER_G(t4_read_reg(adapter, mbox_ctl));
211f7917c00SJeff Kirsher 	for (i = 0; v == MBOX_OWNER_NONE && i < 3; i++)
21289c3a86cSHariprasad Shenai 		v = MBOWNER_G(t4_read_reg(adapter, mbox_ctl));
213ae7b7576SHariprasad Shenai 	if (v != MBOX_OWNER_DRV) {
214b38066daSHariprasad Shenai 		spin_lock(&adapter->mbox_lock);
215b38066daSHariprasad Shenai 		list_del(&entry.list);
216b38066daSHariprasad Shenai 		spin_unlock(&adapter->mbox_lock);
217ae7b7576SHariprasad Shenai 		ret = (v == MBOX_OWNER_FW) ? -EBUSY : -ETIMEDOUT;
218ae7b7576SHariprasad Shenai 		t4vf_record_mbox(adapter, cmd, size, access, ret);
219ae7b7576SHariprasad Shenai 		return ret;
220ae7b7576SHariprasad Shenai 	}
221f7917c00SJeff Kirsher 
222f7917c00SJeff Kirsher 	/*
223f7917c00SJeff Kirsher 	 * Write the command array into the Mailbox Data register array and
224f7917c00SJeff Kirsher 	 * transfer ownership of the mailbox to the firmware.
225f7917c00SJeff Kirsher 	 *
226f7917c00SJeff Kirsher 	 * For the VFs, the Mailbox Data "registers" are actually backed by
227f7917c00SJeff Kirsher 	 * T4's "MA" interface rather than PL Registers (as is the case for
228f7917c00SJeff Kirsher 	 * the PFs).  Because these are in different coherency domains, the
229f7917c00SJeff Kirsher 	 * write to the VF's PL-register-backed Mailbox Control can race in
230f7917c00SJeff Kirsher 	 * front of the writes to the MA-backed VF Mailbox Data "registers".
231f7917c00SJeff Kirsher 	 * So we need to do a read-back on at least one byte of the VF Mailbox
232f7917c00SJeff Kirsher 	 * Data registers before doing the write to the VF Mailbox Control
233f7917c00SJeff Kirsher 	 * register.
234f7917c00SJeff Kirsher 	 */
235ae7b7576SHariprasad Shenai 	if (cmd_op != FW_VI_STATS_CMD)
236ae7b7576SHariprasad Shenai 		t4vf_record_mbox(adapter, cmd, size, access, 0);
237f7917c00SJeff Kirsher 	for (i = 0, p = cmd; i < size; i += 8)
238f7917c00SJeff Kirsher 		t4_write_reg64(adapter, mbox_data + i, be64_to_cpu(*p++));
239f7917c00SJeff Kirsher 	t4_read_reg(adapter, mbox_data);         /* flush write */
240f7917c00SJeff Kirsher 
241f7917c00SJeff Kirsher 	t4_write_reg(adapter, mbox_ctl,
24289c3a86cSHariprasad Shenai 		     MBMSGVALID_F | MBOWNER_V(MBOX_OWNER_FW));
243f7917c00SJeff Kirsher 	t4_read_reg(adapter, mbox_ctl);          /* flush write */
244f7917c00SJeff Kirsher 
245f7917c00SJeff Kirsher 	/*
246f7917c00SJeff Kirsher 	 * Spin waiting for firmware to acknowledge processing our command.
247f7917c00SJeff Kirsher 	 */
248f7917c00SJeff Kirsher 	delay_idx = 0;
249f7917c00SJeff Kirsher 	ms = delay[0];
250f7917c00SJeff Kirsher 
251f7917c00SJeff Kirsher 	for (i = 0; i < FW_CMD_MAX_TIMEOUT; i += ms) {
252f7917c00SJeff Kirsher 		if (sleep_ok) {
253f7917c00SJeff Kirsher 			ms = delay[delay_idx];
254f7917c00SJeff Kirsher 			if (delay_idx < ARRAY_SIZE(delay) - 1)
255f7917c00SJeff Kirsher 				delay_idx++;
256f7917c00SJeff Kirsher 			msleep(ms);
257f7917c00SJeff Kirsher 		} else
258f7917c00SJeff Kirsher 			mdelay(ms);
259f7917c00SJeff Kirsher 
260f7917c00SJeff Kirsher 		/*
261f7917c00SJeff Kirsher 		 * If we're the owner, see if this is the reply we wanted.
262f7917c00SJeff Kirsher 		 */
263f7917c00SJeff Kirsher 		v = t4_read_reg(adapter, mbox_ctl);
26489c3a86cSHariprasad Shenai 		if (MBOWNER_G(v) == MBOX_OWNER_DRV) {
265f7917c00SJeff Kirsher 			/*
266f7917c00SJeff Kirsher 			 * If the Message Valid bit isn't on, revoke ownership
267f7917c00SJeff Kirsher 			 * of the mailbox and continue waiting for our reply.
268f7917c00SJeff Kirsher 			 */
26989c3a86cSHariprasad Shenai 			if ((v & MBMSGVALID_F) == 0) {
270f7917c00SJeff Kirsher 				t4_write_reg(adapter, mbox_ctl,
27189c3a86cSHariprasad Shenai 					     MBOWNER_V(MBOX_OWNER_NONE));
272f7917c00SJeff Kirsher 				continue;
273f7917c00SJeff Kirsher 			}
274f7917c00SJeff Kirsher 
275f7917c00SJeff Kirsher 			/*
276f7917c00SJeff Kirsher 			 * We now have our reply.  Extract the command return
277f7917c00SJeff Kirsher 			 * value, copy the reply back to our caller's buffer
278f7917c00SJeff Kirsher 			 * (if specified) and revoke ownership of the mailbox.
279f7917c00SJeff Kirsher 			 * We return the (negated) firmware command return
280f7917c00SJeff Kirsher 			 * code (this depends on FW_SUCCESS == 0).
281f7917c00SJeff Kirsher 			 */
282ae7b7576SHariprasad Shenai 			get_mbox_rpl(adapter, cmd_rpl, size, mbox_data);
283f7917c00SJeff Kirsher 
284f7917c00SJeff Kirsher 			/* return value in low-order little-endian word */
285ae7b7576SHariprasad Shenai 			v = be64_to_cpu(cmd_rpl[0]);
286f7917c00SJeff Kirsher 
287f7917c00SJeff Kirsher 			if (rpl) {
288f7917c00SJeff Kirsher 				/* request bit in high-order BE word */
2892ff2acf1SHariprasad Shenai 				WARN_ON((be32_to_cpu(*(const __be32 *)cmd)
290e2ac9628SHariprasad Shenai 					 & FW_CMD_REQUEST_F) == 0);
291ae7b7576SHariprasad Shenai 				memcpy(rpl, cmd_rpl, size);
2922ff2acf1SHariprasad Shenai 				WARN_ON((be32_to_cpu(*(__be32 *)rpl)
293e2ac9628SHariprasad Shenai 					 & FW_CMD_REQUEST_F) != 0);
294f7917c00SJeff Kirsher 			}
295f7917c00SJeff Kirsher 			t4_write_reg(adapter, mbox_ctl,
29689c3a86cSHariprasad Shenai 				     MBOWNER_V(MBOX_OWNER_NONE));
297ae7b7576SHariprasad Shenai 			execute = i + ms;
298ae7b7576SHariprasad Shenai 			if (cmd_op != FW_VI_STATS_CMD)
299ae7b7576SHariprasad Shenai 				t4vf_record_mbox(adapter, cmd_rpl, size, access,
300ae7b7576SHariprasad Shenai 						 execute);
301b38066daSHariprasad Shenai 			spin_lock(&adapter->mbox_lock);
302b38066daSHariprasad Shenai 			list_del(&entry.list);
303b38066daSHariprasad Shenai 			spin_unlock(&adapter->mbox_lock);
304e2ac9628SHariprasad Shenai 			return -FW_CMD_RETVAL_G(v);
305f7917c00SJeff Kirsher 		}
306f7917c00SJeff Kirsher 	}
307f7917c00SJeff Kirsher 
308ae7b7576SHariprasad Shenai 	/* We timed out.  Return the error ... */
309ae7b7576SHariprasad Shenai 	ret = -ETIMEDOUT;
310ae7b7576SHariprasad Shenai 	t4vf_record_mbox(adapter, cmd, size, access, ret);
311b38066daSHariprasad Shenai 	spin_lock(&adapter->mbox_lock);
312b38066daSHariprasad Shenai 	list_del(&entry.list);
313b38066daSHariprasad Shenai 	spin_unlock(&adapter->mbox_lock);
314ae7b7576SHariprasad Shenai 	return ret;
315f7917c00SJeff Kirsher }
316f7917c00SJeff Kirsher 
3179f764898SVishal Kulkarni /* In the Physical Function Driver Common Code, the ADVERT_MASK is used to
3189f764898SVishal Kulkarni  * mask out bits in the Advertised Port Capabilities which are managed via
3199f764898SVishal Kulkarni  * separate controls, like Pause Frames and Forward Error Correction.  In the
3209f764898SVishal Kulkarni  * Virtual Function Common Code, since we never perform L1 Configuration on
3219f764898SVishal Kulkarni  * the Link, the only things we really need to filter out are things which
3229f764898SVishal Kulkarni  * we decode and report separately like Speed.
3239f764898SVishal Kulkarni  */
324c3168cabSGanesh Goudar #define ADVERT_MASK (FW_PORT_CAP32_SPEED_V(FW_PORT_CAP32_SPEED_M) | \
3259f764898SVishal Kulkarni 		     FW_PORT_CAP32_802_3_PAUSE | \
3269f764898SVishal Kulkarni 		     FW_PORT_CAP32_802_3_ASM_DIR | \
3279f764898SVishal Kulkarni 		     FW_PORT_CAP32_FEC_V(FW_PORT_CAP32_FEC_M) | \
328c3168cabSGanesh Goudar 		     FW_PORT_CAP32_ANEG)
3295ad24defSHariprasad Shenai 
330f7917c00SJeff Kirsher /**
331c3168cabSGanesh Goudar  *	fwcaps16_to_caps32 - convert 16-bit Port Capabilities to 32-bits
332c3168cabSGanesh Goudar  *	@caps16: a 16-bit Port Capabilities value
333c3168cabSGanesh Goudar  *
334c3168cabSGanesh Goudar  *	Returns the equivalent 32-bit Port Capabilities value.
335c3168cabSGanesh Goudar  */
fwcaps16_to_caps32(fw_port_cap16_t caps16)336c3168cabSGanesh Goudar static fw_port_cap32_t fwcaps16_to_caps32(fw_port_cap16_t caps16)
337c3168cabSGanesh Goudar {
338c3168cabSGanesh Goudar 	fw_port_cap32_t caps32 = 0;
339c3168cabSGanesh Goudar 
340c3168cabSGanesh Goudar 	#define CAP16_TO_CAP32(__cap) \
341c3168cabSGanesh Goudar 		do { \
342c3168cabSGanesh Goudar 			if (caps16 & FW_PORT_CAP_##__cap) \
343c3168cabSGanesh Goudar 				caps32 |= FW_PORT_CAP32_##__cap; \
344c3168cabSGanesh Goudar 		} while (0)
345c3168cabSGanesh Goudar 
346c3168cabSGanesh Goudar 	CAP16_TO_CAP32(SPEED_100M);
347c3168cabSGanesh Goudar 	CAP16_TO_CAP32(SPEED_1G);
348c3168cabSGanesh Goudar 	CAP16_TO_CAP32(SPEED_25G);
349c3168cabSGanesh Goudar 	CAP16_TO_CAP32(SPEED_10G);
350c3168cabSGanesh Goudar 	CAP16_TO_CAP32(SPEED_40G);
351c3168cabSGanesh Goudar 	CAP16_TO_CAP32(SPEED_100G);
352c3168cabSGanesh Goudar 	CAP16_TO_CAP32(FC_RX);
353c3168cabSGanesh Goudar 	CAP16_TO_CAP32(FC_TX);
354c3168cabSGanesh Goudar 	CAP16_TO_CAP32(ANEG);
355c3168cabSGanesh Goudar 	CAP16_TO_CAP32(MDIAUTO);
3561d19023fSGanesh Goudar 	CAP16_TO_CAP32(MDISTRAIGHT);
357c3168cabSGanesh Goudar 	CAP16_TO_CAP32(FEC_RS);
358c3168cabSGanesh Goudar 	CAP16_TO_CAP32(FEC_BASER_RS);
359c3168cabSGanesh Goudar 	CAP16_TO_CAP32(802_3_PAUSE);
360c3168cabSGanesh Goudar 	CAP16_TO_CAP32(802_3_ASM_DIR);
361c3168cabSGanesh Goudar 
362c3168cabSGanesh Goudar 	#undef CAP16_TO_CAP32
363c3168cabSGanesh Goudar 
364c3168cabSGanesh Goudar 	return caps32;
365c3168cabSGanesh Goudar }
366c3168cabSGanesh Goudar 
367c3168cabSGanesh Goudar /* Translate Firmware Pause specification to Common Code */
fwcap_to_cc_pause(fw_port_cap32_t fw_pause)368c3168cabSGanesh Goudar static inline enum cc_pause fwcap_to_cc_pause(fw_port_cap32_t fw_pause)
369c3168cabSGanesh Goudar {
370c3168cabSGanesh Goudar 	enum cc_pause cc_pause = 0;
371c3168cabSGanesh Goudar 
372c3168cabSGanesh Goudar 	if (fw_pause & FW_PORT_CAP32_FC_RX)
373c3168cabSGanesh Goudar 		cc_pause |= PAUSE_RX;
374c3168cabSGanesh Goudar 	if (fw_pause & FW_PORT_CAP32_FC_TX)
375c3168cabSGanesh Goudar 		cc_pause |= PAUSE_TX;
376c3168cabSGanesh Goudar 
377c3168cabSGanesh Goudar 	return cc_pause;
378c3168cabSGanesh Goudar }
379c3168cabSGanesh Goudar 
380c3168cabSGanesh Goudar /* Translate Firmware Forward Error Correction specification to Common Code */
fwcap_to_cc_fec(fw_port_cap32_t fw_fec)381c3168cabSGanesh Goudar static inline enum cc_fec fwcap_to_cc_fec(fw_port_cap32_t fw_fec)
382c3168cabSGanesh Goudar {
383c3168cabSGanesh Goudar 	enum cc_fec cc_fec = 0;
384c3168cabSGanesh Goudar 
385c3168cabSGanesh Goudar 	if (fw_fec & FW_PORT_CAP32_FEC_RS)
386c3168cabSGanesh Goudar 		cc_fec |= FEC_RS;
387c3168cabSGanesh Goudar 	if (fw_fec & FW_PORT_CAP32_FEC_BASER_RS)
388c3168cabSGanesh Goudar 		cc_fec |= FEC_BASER_RS;
389c3168cabSGanesh Goudar 
390c3168cabSGanesh Goudar 	return cc_fec;
391c3168cabSGanesh Goudar }
392c3168cabSGanesh Goudar 
39320bb0c8fSRahul Lakkireddy /* Return the highest speed set in the port capabilities, in Mb/s. */
fwcap_to_speed(fw_port_cap32_t caps)394c3168cabSGanesh Goudar static unsigned int fwcap_to_speed(fw_port_cap32_t caps)
395c3168cabSGanesh Goudar {
396c3168cabSGanesh Goudar 	#define TEST_SPEED_RETURN(__caps_speed, __speed) \
397c3168cabSGanesh Goudar 		do { \
398c3168cabSGanesh Goudar 			if (caps & FW_PORT_CAP32_SPEED_##__caps_speed) \
399c3168cabSGanesh Goudar 				return __speed; \
400c3168cabSGanesh Goudar 		} while (0)
401c3168cabSGanesh Goudar 
402c3168cabSGanesh Goudar 	TEST_SPEED_RETURN(400G, 400000);
403c3168cabSGanesh Goudar 	TEST_SPEED_RETURN(200G, 200000);
404c3168cabSGanesh Goudar 	TEST_SPEED_RETURN(100G, 100000);
405c3168cabSGanesh Goudar 	TEST_SPEED_RETURN(50G,   50000);
406c3168cabSGanesh Goudar 	TEST_SPEED_RETURN(40G,   40000);
407c3168cabSGanesh Goudar 	TEST_SPEED_RETURN(25G,   25000);
408c3168cabSGanesh Goudar 	TEST_SPEED_RETURN(10G,   10000);
409c3168cabSGanesh Goudar 	TEST_SPEED_RETURN(1G,     1000);
410c3168cabSGanesh Goudar 	TEST_SPEED_RETURN(100M,    100);
411c3168cabSGanesh Goudar 
412c3168cabSGanesh Goudar 	#undef TEST_SPEED_RETURN
413c3168cabSGanesh Goudar 
414c3168cabSGanesh Goudar 	return 0;
415c3168cabSGanesh Goudar }
416c3168cabSGanesh Goudar 
41757ccaedbSGanesh Goudar /**
41857ccaedbSGanesh Goudar  *      fwcap_to_fwspeed - return highest speed in Port Capabilities
41957ccaedbSGanesh Goudar  *      @acaps: advertised Port Capabilities
42057ccaedbSGanesh Goudar  *
42157ccaedbSGanesh Goudar  *      Get the highest speed for the port from the advertised Port
42257ccaedbSGanesh Goudar  *      Capabilities.  It will be either the highest speed from the list of
42357ccaedbSGanesh Goudar  *      speeds or whatever user has set using ethtool.
42457ccaedbSGanesh Goudar  */
fwcap_to_fwspeed(fw_port_cap32_t acaps)42557ccaedbSGanesh Goudar static fw_port_cap32_t fwcap_to_fwspeed(fw_port_cap32_t acaps)
42657ccaedbSGanesh Goudar {
42757ccaedbSGanesh Goudar 	#define TEST_SPEED_RETURN(__caps_speed) \
42857ccaedbSGanesh Goudar 		do { \
42957ccaedbSGanesh Goudar 			if (acaps & FW_PORT_CAP32_SPEED_##__caps_speed) \
43057ccaedbSGanesh Goudar 				return FW_PORT_CAP32_SPEED_##__caps_speed; \
43157ccaedbSGanesh Goudar 		} while (0)
43257ccaedbSGanesh Goudar 
43357ccaedbSGanesh Goudar 	TEST_SPEED_RETURN(400G);
43457ccaedbSGanesh Goudar 	TEST_SPEED_RETURN(200G);
43557ccaedbSGanesh Goudar 	TEST_SPEED_RETURN(100G);
43657ccaedbSGanesh Goudar 	TEST_SPEED_RETURN(50G);
43757ccaedbSGanesh Goudar 	TEST_SPEED_RETURN(40G);
43857ccaedbSGanesh Goudar 	TEST_SPEED_RETURN(25G);
43957ccaedbSGanesh Goudar 	TEST_SPEED_RETURN(10G);
44057ccaedbSGanesh Goudar 	TEST_SPEED_RETURN(1G);
44157ccaedbSGanesh Goudar 	TEST_SPEED_RETURN(100M);
44257ccaedbSGanesh Goudar 
44357ccaedbSGanesh Goudar 	#undef TEST_SPEED_RETURN
44457ccaedbSGanesh Goudar 	return 0;
44557ccaedbSGanesh Goudar }
44657ccaedbSGanesh Goudar 
447c3168cabSGanesh Goudar /*
448f7917c00SJeff Kirsher  *	init_link_config - initialize a link's SW state
449f7917c00SJeff Kirsher  *	@lc: structure holding the link state
450c3168cabSGanesh Goudar  *	@pcaps: link Port Capabilities
451c3168cabSGanesh Goudar  *	@acaps: link current Advertised Port Capabilities
452f7917c00SJeff Kirsher  *
453f7917c00SJeff Kirsher  *	Initializes the SW state maintained for each link, including the link's
454f7917c00SJeff Kirsher  *	capabilities and default speed/flow-control/autonegotiation settings.
455f7917c00SJeff Kirsher  */
init_link_config(struct link_config * lc,fw_port_cap32_t pcaps,fw_port_cap32_t acaps)456c3168cabSGanesh Goudar static void init_link_config(struct link_config *lc,
457c3168cabSGanesh Goudar 			     fw_port_cap32_t pcaps,
458c3168cabSGanesh Goudar 			     fw_port_cap32_t acaps)
459f7917c00SJeff Kirsher {
460c3168cabSGanesh Goudar 	lc->pcaps = pcaps;
461c3168cabSGanesh Goudar 	lc->lpacaps = 0;
462c3168cabSGanesh Goudar 	lc->speed_caps = 0;
463f7917c00SJeff Kirsher 	lc->speed = 0;
464f7917c00SJeff Kirsher 	lc->requested_fc = lc->fc = PAUSE_RX | PAUSE_TX;
465c3168cabSGanesh Goudar 
466c3168cabSGanesh Goudar 	/* For Forward Error Control, we default to whatever the Firmware
467c3168cabSGanesh Goudar 	 * tells us the Link is currently advertising.
468c3168cabSGanesh Goudar 	 */
469c3168cabSGanesh Goudar 	lc->auto_fec = fwcap_to_cc_fec(acaps);
470c3168cabSGanesh Goudar 	lc->requested_fec = FEC_AUTO;
471c3168cabSGanesh Goudar 	lc->fec = lc->auto_fec;
472c3168cabSGanesh Goudar 
47357ccaedbSGanesh Goudar 	/* If the Port is capable of Auto-Negtotiation, initialize it as
47457ccaedbSGanesh Goudar 	 * "enabled" and copy over all of the Physical Port Capabilities
47557ccaedbSGanesh Goudar 	 * to the Advertised Port Capabilities.  Otherwise mark it as
47657ccaedbSGanesh Goudar 	 * Auto-Negotiate disabled and select the highest supported speed
47757ccaedbSGanesh Goudar 	 * for the link.  Note parallel structure in t4_link_l1cfg_core()
47857ccaedbSGanesh Goudar 	 * and t4_handle_get_port_info().
47957ccaedbSGanesh Goudar 	 */
480c3168cabSGanesh Goudar 	if (lc->pcaps & FW_PORT_CAP32_ANEG) {
481c3168cabSGanesh Goudar 		lc->acaps = acaps & ADVERT_MASK;
482f7917c00SJeff Kirsher 		lc->autoneg = AUTONEG_ENABLE;
483f7917c00SJeff Kirsher 		lc->requested_fc |= PAUSE_AUTONEG;
484f7917c00SJeff Kirsher 	} else {
485c3168cabSGanesh Goudar 		lc->acaps = 0;
486f7917c00SJeff Kirsher 		lc->autoneg = AUTONEG_DISABLE;
48757ccaedbSGanesh Goudar 		lc->speed_caps = fwcap_to_fwspeed(acaps);
488f7917c00SJeff Kirsher 	}
489f7917c00SJeff Kirsher }
490f7917c00SJeff Kirsher 
491f7917c00SJeff Kirsher /**
492f7917c00SJeff Kirsher  *	t4vf_port_init - initialize port hardware/software state
493f7917c00SJeff Kirsher  *	@adapter: the adapter
494f7917c00SJeff Kirsher  *	@pidx: the adapter port index
495f7917c00SJeff Kirsher  */
t4vf_port_init(struct adapter * adapter,int pidx)496d289f864SBill Pemberton int t4vf_port_init(struct adapter *adapter, int pidx)
497f7917c00SJeff Kirsher {
498f7917c00SJeff Kirsher 	struct port_info *pi = adap2pinfo(adapter, pidx);
499c3168cabSGanesh Goudar 	unsigned int fw_caps = adapter->params.fw_caps_support;
500f7917c00SJeff Kirsher 	struct fw_vi_cmd vi_cmd, vi_rpl;
501f7917c00SJeff Kirsher 	struct fw_port_cmd port_cmd, port_rpl;
502c3168cabSGanesh Goudar 	enum fw_port_type port_type;
503c3168cabSGanesh Goudar 	int mdio_addr;
504c3168cabSGanesh Goudar 	fw_port_cap32_t pcaps, acaps;
505c3168cabSGanesh Goudar 	int ret;
506c3168cabSGanesh Goudar 
507c3168cabSGanesh Goudar 	/* If we haven't yet determined whether we're talking to Firmware
508c3168cabSGanesh Goudar 	 * which knows the new 32-bit Port Capabilities, it's time to find
509c3168cabSGanesh Goudar 	 * out now.  This will also tell new Firmware to send us Port Status
510c3168cabSGanesh Goudar 	 * Updates using the new 32-bit Port Capabilities version of the
511c3168cabSGanesh Goudar 	 * Port Information message.
512c3168cabSGanesh Goudar 	 */
513c3168cabSGanesh Goudar 	if (fw_caps == FW_CAPS_UNKNOWN) {
514c3168cabSGanesh Goudar 		u32 param, val;
515c3168cabSGanesh Goudar 
516c3168cabSGanesh Goudar 		param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) |
517c3168cabSGanesh Goudar 			 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_PORT_CAPS32));
518c3168cabSGanesh Goudar 		val = 1;
519c3168cabSGanesh Goudar 		ret = t4vf_set_params(adapter, 1, &param, &val);
520c3168cabSGanesh Goudar 		fw_caps = (ret == 0 ? FW_CAPS32 : FW_CAPS16);
521c3168cabSGanesh Goudar 		adapter->params.fw_caps_support = fw_caps;
522c3168cabSGanesh Goudar 	}
523f7917c00SJeff Kirsher 
524f7917c00SJeff Kirsher 	/*
525f7917c00SJeff Kirsher 	 * Execute a VI Read command to get our Virtual Interface information
526f7917c00SJeff Kirsher 	 * like MAC address, etc.
527f7917c00SJeff Kirsher 	 */
528f7917c00SJeff Kirsher 	memset(&vi_cmd, 0, sizeof(vi_cmd));
529e2ac9628SHariprasad Shenai 	vi_cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_VI_CMD) |
530e2ac9628SHariprasad Shenai 				       FW_CMD_REQUEST_F |
531e2ac9628SHariprasad Shenai 				       FW_CMD_READ_F);
532f7917c00SJeff Kirsher 	vi_cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(vi_cmd));
5332b5fb1f2SHariprasad Shenai 	vi_cmd.type_viid = cpu_to_be16(FW_VI_CMD_VIID_V(pi->viid));
534c3168cabSGanesh Goudar 	ret = t4vf_wr_mbox(adapter, &vi_cmd, sizeof(vi_cmd), &vi_rpl);
535c3168cabSGanesh Goudar 	if (ret != FW_SUCCESS)
536c3168cabSGanesh Goudar 		return ret;
537f7917c00SJeff Kirsher 
5382b5fb1f2SHariprasad Shenai 	BUG_ON(pi->port_id != FW_VI_CMD_PORTID_G(vi_rpl.portid_pkd));
5392b5fb1f2SHariprasad Shenai 	pi->rss_size = FW_VI_CMD_RSSSIZE_G(be16_to_cpu(vi_rpl.rsssize_pkd));
540f7917c00SJeff Kirsher 	t4_os_set_hw_addr(adapter, pidx, vi_rpl.mac);
541f7917c00SJeff Kirsher 
542f7917c00SJeff Kirsher 	/*
543f7917c00SJeff Kirsher 	 * If we don't have read access to our port information, we're done
544f7917c00SJeff Kirsher 	 * now.  Otherwise, execute a PORT Read command to get it ...
545f7917c00SJeff Kirsher 	 */
546f7917c00SJeff Kirsher 	if (!(adapter->params.vfres.r_caps & FW_CMD_CAP_PORT))
547f7917c00SJeff Kirsher 		return 0;
548f7917c00SJeff Kirsher 
549f7917c00SJeff Kirsher 	memset(&port_cmd, 0, sizeof(port_cmd));
550e2ac9628SHariprasad Shenai 	port_cmd.op_to_portid = cpu_to_be32(FW_CMD_OP_V(FW_PORT_CMD) |
551e2ac9628SHariprasad Shenai 					    FW_CMD_REQUEST_F |
552e2ac9628SHariprasad Shenai 					    FW_CMD_READ_F |
5532b5fb1f2SHariprasad Shenai 					    FW_PORT_CMD_PORTID_V(pi->port_id));
554c3168cabSGanesh Goudar 	port_cmd.action_to_len16 = cpu_to_be32(
555c3168cabSGanesh Goudar 		FW_PORT_CMD_ACTION_V(fw_caps == FW_CAPS16
556c3168cabSGanesh Goudar 				     ? FW_PORT_ACTION_GET_PORT_INFO
557c3168cabSGanesh Goudar 				     : FW_PORT_ACTION_GET_PORT_INFO32) |
558f7917c00SJeff Kirsher 		FW_LEN16(port_cmd));
559c3168cabSGanesh Goudar 	ret = t4vf_wr_mbox(adapter, &port_cmd, sizeof(port_cmd), &port_rpl);
560c3168cabSGanesh Goudar 	if (ret != FW_SUCCESS)
561c3168cabSGanesh Goudar 		return ret;
562f7917c00SJeff Kirsher 
563c3168cabSGanesh Goudar 	/* Extract the various fields from the Port Information message. */
564c3168cabSGanesh Goudar 	if (fw_caps == FW_CAPS16) {
565c3168cabSGanesh Goudar 		u32 lstatus = be32_to_cpu(port_rpl.u.info.lstatus_to_modtype);
566c3168cabSGanesh Goudar 
567c3168cabSGanesh Goudar 		port_type = FW_PORT_CMD_PTYPE_G(lstatus);
568c3168cabSGanesh Goudar 		mdio_addr = ((lstatus & FW_PORT_CMD_MDIOCAP_F)
569c3168cabSGanesh Goudar 			     ? FW_PORT_CMD_MDIOADDR_G(lstatus)
570c3168cabSGanesh Goudar 			     : -1);
571c3168cabSGanesh Goudar 		pcaps = fwcaps16_to_caps32(be16_to_cpu(port_rpl.u.info.pcap));
572c3168cabSGanesh Goudar 		acaps = fwcaps16_to_caps32(be16_to_cpu(port_rpl.u.info.acap));
573c3168cabSGanesh Goudar 	} else {
574c3168cabSGanesh Goudar 		u32 lstatus32 =
575c3168cabSGanesh Goudar 			   be32_to_cpu(port_rpl.u.info32.lstatus32_to_cbllen32);
576c3168cabSGanesh Goudar 
577c3168cabSGanesh Goudar 		port_type = FW_PORT_CMD_PORTTYPE32_G(lstatus32);
578c3168cabSGanesh Goudar 		mdio_addr = ((lstatus32 & FW_PORT_CMD_MDIOCAP32_F)
579c3168cabSGanesh Goudar 			     ? FW_PORT_CMD_MDIOADDR32_G(lstatus32)
580c3168cabSGanesh Goudar 			     : -1);
581c3168cabSGanesh Goudar 		pcaps = be32_to_cpu(port_rpl.u.info32.pcaps32);
582c3168cabSGanesh Goudar 		acaps = be32_to_cpu(port_rpl.u.info32.acaps32);
583c3168cabSGanesh Goudar 	}
584c3168cabSGanesh Goudar 
585c3168cabSGanesh Goudar 	pi->port_type = port_type;
586c3168cabSGanesh Goudar 	pi->mdio_addr = mdio_addr;
5875ad24defSHariprasad Shenai 	pi->mod_type = FW_PORT_MOD_TYPE_NA;
5885ad24defSHariprasad Shenai 
589c3168cabSGanesh Goudar 	init_link_config(&pi->link_cfg, pcaps, acaps);
590f7917c00SJeff Kirsher 	return 0;
591f7917c00SJeff Kirsher }
592f7917c00SJeff Kirsher 
593f7917c00SJeff Kirsher /**
594f7917c00SJeff Kirsher  *      t4vf_fw_reset - issue a reset to FW
595f7917c00SJeff Kirsher  *      @adapter: the adapter
596f7917c00SJeff Kirsher  *
597f7917c00SJeff Kirsher  *	Issues a reset command to FW.  For a Physical Function this would
598dbedd44eSJoe Perches  *	result in the Firmware resetting all of its state.  For a Virtual
599f7917c00SJeff Kirsher  *	Function this just resets the state associated with the VF.
600f7917c00SJeff Kirsher  */
t4vf_fw_reset(struct adapter * adapter)601f7917c00SJeff Kirsher int t4vf_fw_reset(struct adapter *adapter)
602f7917c00SJeff Kirsher {
603f7917c00SJeff Kirsher 	struct fw_reset_cmd cmd;
604f7917c00SJeff Kirsher 
605f7917c00SJeff Kirsher 	memset(&cmd, 0, sizeof(cmd));
606e2ac9628SHariprasad Shenai 	cmd.op_to_write = cpu_to_be32(FW_CMD_OP_V(FW_RESET_CMD) |
607e2ac9628SHariprasad Shenai 				      FW_CMD_WRITE_F);
608f7917c00SJeff Kirsher 	cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
609f7917c00SJeff Kirsher 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
610f7917c00SJeff Kirsher }
611f7917c00SJeff Kirsher 
612f7917c00SJeff Kirsher /**
613f7917c00SJeff Kirsher  *	t4vf_query_params - query FW or device parameters
614f7917c00SJeff Kirsher  *	@adapter: the adapter
615f7917c00SJeff Kirsher  *	@nparams: the number of parameters
616f7917c00SJeff Kirsher  *	@params: the parameter names
617f7917c00SJeff Kirsher  *	@vals: the parameter values
618f7917c00SJeff Kirsher  *
619f7917c00SJeff Kirsher  *	Reads the values of firmware or device parameters.  Up to 7 parameters
620f7917c00SJeff Kirsher  *	can be queried at once.
621f7917c00SJeff Kirsher  */
t4vf_query_params(struct adapter * adapter,unsigned int nparams,const u32 * params,u32 * vals)622de5b8677Sstephen hemminger static int t4vf_query_params(struct adapter *adapter, unsigned int nparams,
623f7917c00SJeff Kirsher 			     const u32 *params, u32 *vals)
624f7917c00SJeff Kirsher {
625f7917c00SJeff Kirsher 	int i, ret;
626f7917c00SJeff Kirsher 	struct fw_params_cmd cmd, rpl;
627f7917c00SJeff Kirsher 	struct fw_params_param *p;
628f7917c00SJeff Kirsher 	size_t len16;
629f7917c00SJeff Kirsher 
630f7917c00SJeff Kirsher 	if (nparams > 7)
631f7917c00SJeff Kirsher 		return -EINVAL;
632f7917c00SJeff Kirsher 
633f7917c00SJeff Kirsher 	memset(&cmd, 0, sizeof(cmd));
634e2ac9628SHariprasad Shenai 	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_PARAMS_CMD) |
635e2ac9628SHariprasad Shenai 				    FW_CMD_REQUEST_F |
636e2ac9628SHariprasad Shenai 				    FW_CMD_READ_F);
637f7917c00SJeff Kirsher 	len16 = DIV_ROUND_UP(offsetof(struct fw_params_cmd,
638f7917c00SJeff Kirsher 				      param[nparams].mnem), 16);
639e2ac9628SHariprasad Shenai 	cmd.retval_len16 = cpu_to_be32(FW_CMD_LEN16_V(len16));
640f7917c00SJeff Kirsher 	for (i = 0, p = &cmd.param[0]; i < nparams; i++, p++)
641f7917c00SJeff Kirsher 		p->mnem = htonl(*params++);
642f7917c00SJeff Kirsher 
643f7917c00SJeff Kirsher 	ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
644f7917c00SJeff Kirsher 	if (ret == 0)
645f7917c00SJeff Kirsher 		for (i = 0, p = &rpl.param[0]; i < nparams; i++, p++)
646f7917c00SJeff Kirsher 			*vals++ = be32_to_cpu(p->val);
647f7917c00SJeff Kirsher 	return ret;
648f7917c00SJeff Kirsher }
649f7917c00SJeff Kirsher 
650f7917c00SJeff Kirsher /**
651f7917c00SJeff Kirsher  *	t4vf_set_params - sets FW or device parameters
652f7917c00SJeff Kirsher  *	@adapter: the adapter
653f7917c00SJeff Kirsher  *	@nparams: the number of parameters
654f7917c00SJeff Kirsher  *	@params: the parameter names
655f7917c00SJeff Kirsher  *	@vals: the parameter values
656f7917c00SJeff Kirsher  *
657f7917c00SJeff Kirsher  *	Sets the values of firmware or device parameters.  Up to 7 parameters
658f7917c00SJeff Kirsher  *	can be specified at once.
659f7917c00SJeff Kirsher  */
t4vf_set_params(struct adapter * adapter,unsigned int nparams,const u32 * params,const u32 * vals)660f7917c00SJeff Kirsher int t4vf_set_params(struct adapter *adapter, unsigned int nparams,
661f7917c00SJeff Kirsher 		    const u32 *params, const u32 *vals)
662f7917c00SJeff Kirsher {
663f7917c00SJeff Kirsher 	int i;
664f7917c00SJeff Kirsher 	struct fw_params_cmd cmd;
665f7917c00SJeff Kirsher 	struct fw_params_param *p;
666f7917c00SJeff Kirsher 	size_t len16;
667f7917c00SJeff Kirsher 
668f7917c00SJeff Kirsher 	if (nparams > 7)
669f7917c00SJeff Kirsher 		return -EINVAL;
670f7917c00SJeff Kirsher 
671f7917c00SJeff Kirsher 	memset(&cmd, 0, sizeof(cmd));
672e2ac9628SHariprasad Shenai 	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_PARAMS_CMD) |
673e2ac9628SHariprasad Shenai 				    FW_CMD_REQUEST_F |
674e2ac9628SHariprasad Shenai 				    FW_CMD_WRITE_F);
675f7917c00SJeff Kirsher 	len16 = DIV_ROUND_UP(offsetof(struct fw_params_cmd,
676f7917c00SJeff Kirsher 				      param[nparams]), 16);
677e2ac9628SHariprasad Shenai 	cmd.retval_len16 = cpu_to_be32(FW_CMD_LEN16_V(len16));
678f7917c00SJeff Kirsher 	for (i = 0, p = &cmd.param[0]; i < nparams; i++, p++) {
679f7917c00SJeff Kirsher 		p->mnem = cpu_to_be32(*params++);
680f7917c00SJeff Kirsher 		p->val = cpu_to_be32(*vals++);
681f7917c00SJeff Kirsher 	}
682f7917c00SJeff Kirsher 
683f7917c00SJeff Kirsher 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
684f7917c00SJeff Kirsher }
685f7917c00SJeff Kirsher 
686f7917c00SJeff Kirsher /**
687cb440364SHariprasad Shenai  *	t4vf_fl_pkt_align - return the fl packet alignment
688cb440364SHariprasad Shenai  *	@adapter: the adapter
689cb440364SHariprasad Shenai  *
690cb440364SHariprasad Shenai  *	T4 has a single field to specify the packing and padding boundary.
691cb440364SHariprasad Shenai  *	T5 onwards has separate fields for this and hence the alignment for
692cb440364SHariprasad Shenai  *	next packet offset is maximum of these two.  And T6 changes the
693cb440364SHariprasad Shenai  *	Ingress Padding Boundary Shift, so it's all a mess and it's best
694cb440364SHariprasad Shenai  *	if we put this in low-level Common Code ...
695cb440364SHariprasad Shenai  *
696cb440364SHariprasad Shenai  */
t4vf_fl_pkt_align(struct adapter * adapter)697cb440364SHariprasad Shenai int t4vf_fl_pkt_align(struct adapter *adapter)
698cb440364SHariprasad Shenai {
699cb440364SHariprasad Shenai 	u32 sge_control, sge_control2;
700cb440364SHariprasad Shenai 	unsigned int ingpadboundary, ingpackboundary, fl_align, ingpad_shift;
701cb440364SHariprasad Shenai 
702cb440364SHariprasad Shenai 	sge_control = adapter->params.sge.sge_control;
703cb440364SHariprasad Shenai 
704cb440364SHariprasad Shenai 	/* T4 uses a single control field to specify both the PCIe Padding and
705cb440364SHariprasad Shenai 	 * Packing Boundary.  T5 introduced the ability to specify these
706cb440364SHariprasad Shenai 	 * separately.  The actual Ingress Packet Data alignment boundary
707cb440364SHariprasad Shenai 	 * within Packed Buffer Mode is the maximum of these two
708cb440364SHariprasad Shenai 	 * specifications.  (Note that it makes no real practical sense to
709cb440364SHariprasad Shenai 	 * have the Pading Boudary be larger than the Packing Boundary but you
710cb440364SHariprasad Shenai 	 * could set the chip up that way and, in fact, legacy T4 code would
711cb440364SHariprasad Shenai 	 * end doing this because it would initialize the Padding Boundary and
712cb440364SHariprasad Shenai 	 * leave the Packing Boundary initialized to 0 (16 bytes).)
713cb440364SHariprasad Shenai 	 * Padding Boundary values in T6 starts from 8B,
714cb440364SHariprasad Shenai 	 * where as it is 32B for T4 and T5.
715cb440364SHariprasad Shenai 	 */
716cb440364SHariprasad Shenai 	if (CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5)
717cb440364SHariprasad Shenai 		ingpad_shift = INGPADBOUNDARY_SHIFT_X;
718cb440364SHariprasad Shenai 	else
719cb440364SHariprasad Shenai 		ingpad_shift = T6_INGPADBOUNDARY_SHIFT_X;
720cb440364SHariprasad Shenai 
721cb440364SHariprasad Shenai 	ingpadboundary = 1 << (INGPADBOUNDARY_G(sge_control) + ingpad_shift);
722cb440364SHariprasad Shenai 
723cb440364SHariprasad Shenai 	fl_align = ingpadboundary;
724cb440364SHariprasad Shenai 	if (!is_t4(adapter->params.chip)) {
725cb440364SHariprasad Shenai 		/* T5 has a different interpretation of one of the PCIe Packing
726cb440364SHariprasad Shenai 		 * Boundary values.
727cb440364SHariprasad Shenai 		 */
728cb440364SHariprasad Shenai 		sge_control2 = adapter->params.sge.sge_control2;
729cb440364SHariprasad Shenai 		ingpackboundary = INGPACKBOUNDARY_G(sge_control2);
730cb440364SHariprasad Shenai 		if (ingpackboundary == INGPACKBOUNDARY_16B_X)
731cb440364SHariprasad Shenai 			ingpackboundary = 16;
732cb440364SHariprasad Shenai 		else
733cb440364SHariprasad Shenai 			ingpackboundary = 1 << (ingpackboundary +
734cb440364SHariprasad Shenai 						INGPACKBOUNDARY_SHIFT_X);
735cb440364SHariprasad Shenai 
736cb440364SHariprasad Shenai 		fl_align = max(ingpadboundary, ingpackboundary);
737cb440364SHariprasad Shenai 	}
738cb440364SHariprasad Shenai 	return fl_align;
739cb440364SHariprasad Shenai }
740cb440364SHariprasad Shenai 
741cb440364SHariprasad Shenai /**
742b2612722SHariprasad Shenai  *	t4vf_bar2_sge_qregs - return BAR2 SGE Queue register information
743e85c9a7aSHariprasad Shenai  *	@adapter: the adapter
744e85c9a7aSHariprasad Shenai  *	@qid: the Queue ID
745e85c9a7aSHariprasad Shenai  *	@qtype: the Ingress or Egress type for @qid
746e85c9a7aSHariprasad Shenai  *	@pbar2_qoffset: BAR2 Queue Offset
747e85c9a7aSHariprasad Shenai  *	@pbar2_qid: BAR2 Queue ID or 0 for Queue ID inferred SGE Queues
748e85c9a7aSHariprasad Shenai  *
749e85c9a7aSHariprasad Shenai  *	Returns the BAR2 SGE Queue Registers information associated with the
750e85c9a7aSHariprasad Shenai  *	indicated Absolute Queue ID.  These are passed back in return value
751e85c9a7aSHariprasad Shenai  *	pointers.  @qtype should be T4_BAR2_QTYPE_EGRESS for Egress Queue
752e85c9a7aSHariprasad Shenai  *	and T4_BAR2_QTYPE_INGRESS for Ingress Queues.
753e85c9a7aSHariprasad Shenai  *
754e85c9a7aSHariprasad Shenai  *	This may return an error which indicates that BAR2 SGE Queue
755e85c9a7aSHariprasad Shenai  *	registers aren't available.  If an error is not returned, then the
756e85c9a7aSHariprasad Shenai  *	following values are returned:
757e85c9a7aSHariprasad Shenai  *
758e85c9a7aSHariprasad Shenai  *	  *@pbar2_qoffset: the BAR2 Offset of the @qid Registers
759e85c9a7aSHariprasad Shenai  *	  *@pbar2_qid: the BAR2 SGE Queue ID or 0 of @qid
760e85c9a7aSHariprasad Shenai  *
761e85c9a7aSHariprasad Shenai  *	If the returned BAR2 Queue ID is 0, then BAR2 SGE registers which
762e85c9a7aSHariprasad Shenai  *	require the "Inferred Queue ID" ability may be used.  E.g. the
763e85c9a7aSHariprasad Shenai  *	Write Combining Doorbell Buffer. If the BAR2 Queue ID is not 0,
764e85c9a7aSHariprasad Shenai  *	then these "Inferred Queue ID" register may not be used.
765e85c9a7aSHariprasad Shenai  */
t4vf_bar2_sge_qregs(struct adapter * adapter,unsigned int qid,enum t4_bar2_qtype qtype,u64 * pbar2_qoffset,unsigned int * pbar2_qid)766b2612722SHariprasad Shenai int t4vf_bar2_sge_qregs(struct adapter *adapter,
767e85c9a7aSHariprasad Shenai 			unsigned int qid,
768e85c9a7aSHariprasad Shenai 			enum t4_bar2_qtype qtype,
769e85c9a7aSHariprasad Shenai 			u64 *pbar2_qoffset,
770e85c9a7aSHariprasad Shenai 			unsigned int *pbar2_qid)
771e85c9a7aSHariprasad Shenai {
772e85c9a7aSHariprasad Shenai 	unsigned int page_shift, page_size, qpp_shift, qpp_mask;
773e85c9a7aSHariprasad Shenai 	u64 bar2_page_offset, bar2_qoffset;
774e85c9a7aSHariprasad Shenai 	unsigned int bar2_qid, bar2_qid_offset, bar2_qinferred;
775e85c9a7aSHariprasad Shenai 
776e85c9a7aSHariprasad Shenai 	/* T4 doesn't support BAR2 SGE Queue registers.
777e85c9a7aSHariprasad Shenai 	 */
778e85c9a7aSHariprasad Shenai 	if (is_t4(adapter->params.chip))
779e85c9a7aSHariprasad Shenai 		return -EINVAL;
780e85c9a7aSHariprasad Shenai 
781e85c9a7aSHariprasad Shenai 	/* Get our SGE Page Size parameters.
782e85c9a7aSHariprasad Shenai 	 */
783e85c9a7aSHariprasad Shenai 	page_shift = adapter->params.sge.sge_vf_hps + 10;
784e85c9a7aSHariprasad Shenai 	page_size = 1 << page_shift;
785e85c9a7aSHariprasad Shenai 
786e85c9a7aSHariprasad Shenai 	/* Get the right Queues per Page parameters for our Queue.
787e85c9a7aSHariprasad Shenai 	 */
788e85c9a7aSHariprasad Shenai 	qpp_shift = (qtype == T4_BAR2_QTYPE_EGRESS
789e85c9a7aSHariprasad Shenai 		     ? adapter->params.sge.sge_vf_eq_qpp
790e85c9a7aSHariprasad Shenai 		     : adapter->params.sge.sge_vf_iq_qpp);
791e85c9a7aSHariprasad Shenai 	qpp_mask = (1 << qpp_shift) - 1;
792e85c9a7aSHariprasad Shenai 
793e85c9a7aSHariprasad Shenai 	/* Calculate the basics of the BAR2 SGE Queue register area:
794e85c9a7aSHariprasad Shenai 	 *  o The BAR2 page the Queue registers will be in.
795e85c9a7aSHariprasad Shenai 	 *  o The BAR2 Queue ID.
796e85c9a7aSHariprasad Shenai 	 *  o The BAR2 Queue ID Offset into the BAR2 page.
797e85c9a7aSHariprasad Shenai 	 */
7982ff2acf1SHariprasad Shenai 	bar2_page_offset = ((u64)(qid >> qpp_shift) << page_shift);
799e85c9a7aSHariprasad Shenai 	bar2_qid = qid & qpp_mask;
800e85c9a7aSHariprasad Shenai 	bar2_qid_offset = bar2_qid * SGE_UDB_SIZE;
801e85c9a7aSHariprasad Shenai 
802e85c9a7aSHariprasad Shenai 	/* If the BAR2 Queue ID Offset is less than the Page Size, then the
803e85c9a7aSHariprasad Shenai 	 * hardware will infer the Absolute Queue ID simply from the writes to
804e85c9a7aSHariprasad Shenai 	 * the BAR2 Queue ID Offset within the BAR2 Page (and we need to use a
805e85c9a7aSHariprasad Shenai 	 * BAR2 Queue ID of 0 for those writes).  Otherwise, we'll simply
806e85c9a7aSHariprasad Shenai 	 * write to the first BAR2 SGE Queue Area within the BAR2 Page with
807e85c9a7aSHariprasad Shenai 	 * the BAR2 Queue ID and the hardware will infer the Absolute Queue ID
808e85c9a7aSHariprasad Shenai 	 * from the BAR2 Page and BAR2 Queue ID.
809e85c9a7aSHariprasad Shenai 	 *
810e85c9a7aSHariprasad Shenai 	 * One important censequence of this is that some BAR2 SGE registers
811e85c9a7aSHariprasad Shenai 	 * have a "Queue ID" field and we can write the BAR2 SGE Queue ID
812e85c9a7aSHariprasad Shenai 	 * there.  But other registers synthesize the SGE Queue ID purely
813e85c9a7aSHariprasad Shenai 	 * from the writes to the registers -- the Write Combined Doorbell
814e85c9a7aSHariprasad Shenai 	 * Buffer is a good example.  These BAR2 SGE Registers are only
815e85c9a7aSHariprasad Shenai 	 * available for those BAR2 SGE Register areas where the SGE Absolute
816e85c9a7aSHariprasad Shenai 	 * Queue ID can be inferred from simple writes.
817e85c9a7aSHariprasad Shenai 	 */
818e85c9a7aSHariprasad Shenai 	bar2_qoffset = bar2_page_offset;
819e85c9a7aSHariprasad Shenai 	bar2_qinferred = (bar2_qid_offset < page_size);
820e85c9a7aSHariprasad Shenai 	if (bar2_qinferred) {
821e85c9a7aSHariprasad Shenai 		bar2_qoffset += bar2_qid_offset;
822e85c9a7aSHariprasad Shenai 		bar2_qid = 0;
823e85c9a7aSHariprasad Shenai 	}
824e85c9a7aSHariprasad Shenai 
825e85c9a7aSHariprasad Shenai 	*pbar2_qoffset = bar2_qoffset;
826e85c9a7aSHariprasad Shenai 	*pbar2_qid = bar2_qid;
827e85c9a7aSHariprasad Shenai 	return 0;
828e85c9a7aSHariprasad Shenai }
829e85c9a7aSHariprasad Shenai 
t4vf_get_pf_from_vf(struct adapter * adapter)830858aa65cSHariprasad Shenai unsigned int t4vf_get_pf_from_vf(struct adapter *adapter)
831858aa65cSHariprasad Shenai {
832858aa65cSHariprasad Shenai 	u32 whoami;
833858aa65cSHariprasad Shenai 
834858aa65cSHariprasad Shenai 	whoami = t4_read_reg(adapter, T4VF_PL_BASE_ADDR + PL_VF_WHOAMI_A);
835858aa65cSHariprasad Shenai 	return (CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5 ?
836858aa65cSHariprasad Shenai 			SOURCEPF_G(whoami) : T6_SOURCEPF_G(whoami));
837858aa65cSHariprasad Shenai }
838858aa65cSHariprasad Shenai 
839e85c9a7aSHariprasad Shenai /**
840f7917c00SJeff Kirsher  *	t4vf_get_sge_params - retrieve adapter Scatter gather Engine parameters
841f7917c00SJeff Kirsher  *	@adapter: the adapter
842f7917c00SJeff Kirsher  *
843f7917c00SJeff Kirsher  *	Retrieves various core SGE parameters in the form of hardware SGE
844f7917c00SJeff Kirsher  *	register values.  The caller is responsible for decoding these as
845f7917c00SJeff Kirsher  *	needed.  The SGE parameters are stored in @adapter->params.sge.
846f7917c00SJeff Kirsher  */
t4vf_get_sge_params(struct adapter * adapter)847f7917c00SJeff Kirsher int t4vf_get_sge_params(struct adapter *adapter)
848f7917c00SJeff Kirsher {
849f7917c00SJeff Kirsher 	struct sge_params *sge_params = &adapter->params.sge;
850f7917c00SJeff Kirsher 	u32 params[7], vals[7];
851f7917c00SJeff Kirsher 	int v;
852f7917c00SJeff Kirsher 
8535167865aSHariprasad Shenai 	params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
854f612b815SHariprasad Shenai 		     FW_PARAMS_PARAM_XYZ_V(SGE_CONTROL_A));
8555167865aSHariprasad Shenai 	params[1] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
856f612b815SHariprasad Shenai 		     FW_PARAMS_PARAM_XYZ_V(SGE_HOST_PAGE_SIZE_A));
8575167865aSHariprasad Shenai 	params[2] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
858f612b815SHariprasad Shenai 		     FW_PARAMS_PARAM_XYZ_V(SGE_FL_BUFFER_SIZE0_A));
8595167865aSHariprasad Shenai 	params[3] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
860f612b815SHariprasad Shenai 		     FW_PARAMS_PARAM_XYZ_V(SGE_FL_BUFFER_SIZE1_A));
8615167865aSHariprasad Shenai 	params[4] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
862f061de42SHariprasad Shenai 		     FW_PARAMS_PARAM_XYZ_V(SGE_TIMER_VALUE_0_AND_1_A));
8635167865aSHariprasad Shenai 	params[5] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
864f061de42SHariprasad Shenai 		     FW_PARAMS_PARAM_XYZ_V(SGE_TIMER_VALUE_2_AND_3_A));
8655167865aSHariprasad Shenai 	params[6] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
866f061de42SHariprasad Shenai 		     FW_PARAMS_PARAM_XYZ_V(SGE_TIMER_VALUE_4_AND_5_A));
867f7917c00SJeff Kirsher 	v = t4vf_query_params(adapter, 7, params, vals);
868f7917c00SJeff Kirsher 	if (v)
869f7917c00SJeff Kirsher 		return v;
870f7917c00SJeff Kirsher 	sge_params->sge_control = vals[0];
871f7917c00SJeff Kirsher 	sge_params->sge_host_page_size = vals[1];
872f7917c00SJeff Kirsher 	sge_params->sge_fl_buffer_size[0] = vals[2];
873f7917c00SJeff Kirsher 	sge_params->sge_fl_buffer_size[1] = vals[3];
874f7917c00SJeff Kirsher 	sge_params->sge_timer_value_0_and_1 = vals[4];
875f7917c00SJeff Kirsher 	sge_params->sge_timer_value_2_and_3 = vals[5];
876f7917c00SJeff Kirsher 	sge_params->sge_timer_value_4_and_5 = vals[6];
877f7917c00SJeff Kirsher 
878ce8f407aSHariprasad Shenai 	/* T4 uses a single control field to specify both the PCIe Padding and
879ce8f407aSHariprasad Shenai 	 * Packing Boundary.  T5 introduced the ability to specify these
880*7747de17SJiang Jian 	 * separately with the Padding Boundary in SGE_CONTROL and Packing
881ce8f407aSHariprasad Shenai 	 * Boundary in SGE_CONTROL2.  So for T5 and later we need to grab
882ce8f407aSHariprasad Shenai 	 * SGE_CONTROL in order to determine how ingress packet data will be
883ce8f407aSHariprasad Shenai 	 * laid out in Packed Buffer Mode.  Unfortunately, older versions of
884ce8f407aSHariprasad Shenai 	 * the firmware won't let us retrieve SGE_CONTROL2 so if we get a
885ce8f407aSHariprasad Shenai 	 * failure grabbing it we throw an error since we can't figure out the
886ce8f407aSHariprasad Shenai 	 * right value.
887ce8f407aSHariprasad Shenai 	 */
888ce8f407aSHariprasad Shenai 	if (!is_t4(adapter->params.chip)) {
8895167865aSHariprasad Shenai 		params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
8905167865aSHariprasad Shenai 			     FW_PARAMS_PARAM_XYZ_V(SGE_CONTROL2_A));
891ce8f407aSHariprasad Shenai 		v = t4vf_query_params(adapter, 1, params, vals);
892ce8f407aSHariprasad Shenai 		if (v != FW_SUCCESS) {
893ce8f407aSHariprasad Shenai 			dev_err(adapter->pdev_dev,
894ce8f407aSHariprasad Shenai 				"Unable to get SGE Control2; "
895ce8f407aSHariprasad Shenai 				"probably old firmware.\n");
896ce8f407aSHariprasad Shenai 			return v;
897ce8f407aSHariprasad Shenai 		}
898ce8f407aSHariprasad Shenai 		sge_params->sge_control2 = vals[0];
899ce8f407aSHariprasad Shenai 	}
900ce8f407aSHariprasad Shenai 
9015167865aSHariprasad Shenai 	params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
902f612b815SHariprasad Shenai 		     FW_PARAMS_PARAM_XYZ_V(SGE_INGRESS_RX_THRESHOLD_A));
9035167865aSHariprasad Shenai 	params[1] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
904f612b815SHariprasad Shenai 		     FW_PARAMS_PARAM_XYZ_V(SGE_CONM_CTRL_A));
90550d21a66SHariprasad Shenai 	v = t4vf_query_params(adapter, 2, params, vals);
906f7917c00SJeff Kirsher 	if (v)
907f7917c00SJeff Kirsher 		return v;
908f7917c00SJeff Kirsher 	sge_params->sge_ingress_rx_threshold = vals[0];
90950d21a66SHariprasad Shenai 	sge_params->sge_congestion_control = vals[1];
910f7917c00SJeff Kirsher 
911e0a8b34aSHariprasad Shenai 	/* For T5 and later we want to use the new BAR2 Doorbells.
912e0a8b34aSHariprasad Shenai 	 * Unfortunately, older firmware didn't allow the this register to be
913e0a8b34aSHariprasad Shenai 	 * read.
914e0a8b34aSHariprasad Shenai 	 */
915e0a8b34aSHariprasad Shenai 	if (!is_t4(adapter->params.chip)) {
916e85c9a7aSHariprasad Shenai 		unsigned int pf, s_hps, s_qpp;
917e0a8b34aSHariprasad Shenai 
918e0a8b34aSHariprasad Shenai 		params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
919e0a8b34aSHariprasad Shenai 			     FW_PARAMS_PARAM_XYZ_V(
920e0a8b34aSHariprasad Shenai 				     SGE_EGRESS_QUEUES_PER_PAGE_VF_A));
921e0a8b34aSHariprasad Shenai 		params[1] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
922e0a8b34aSHariprasad Shenai 			     FW_PARAMS_PARAM_XYZ_V(
923e0a8b34aSHariprasad Shenai 				     SGE_INGRESS_QUEUES_PER_PAGE_VF_A));
924e0a8b34aSHariprasad Shenai 		v = t4vf_query_params(adapter, 2, params, vals);
925e0a8b34aSHariprasad Shenai 		if (v != FW_SUCCESS) {
926e0a8b34aSHariprasad Shenai 			dev_warn(adapter->pdev_dev,
927e0a8b34aSHariprasad Shenai 				 "Unable to get VF SGE Queues/Page; "
928e0a8b34aSHariprasad Shenai 				 "probably old firmware.\n");
929e0a8b34aSHariprasad Shenai 			return v;
930e0a8b34aSHariprasad Shenai 		}
931e0a8b34aSHariprasad Shenai 		sge_params->sge_egress_queues_per_page = vals[0];
932e0a8b34aSHariprasad Shenai 		sge_params->sge_ingress_queues_per_page = vals[1];
933e0a8b34aSHariprasad Shenai 
934e0a8b34aSHariprasad Shenai 		/* We need the Queues/Page for our VF.  This is based on the
935e0a8b34aSHariprasad Shenai 		 * PF from which we're instantiated and is indexed in the
936e0a8b34aSHariprasad Shenai 		 * register we just read. Do it once here so other code in
937e0a8b34aSHariprasad Shenai 		 * the driver can just use it.
938e0a8b34aSHariprasad Shenai 		 */
939858aa65cSHariprasad Shenai 		pf = t4vf_get_pf_from_vf(adapter);
940e85c9a7aSHariprasad Shenai 		s_hps = (HOSTPAGESIZEPF0_S +
941e85c9a7aSHariprasad Shenai 			 (HOSTPAGESIZEPF1_S - HOSTPAGESIZEPF0_S) * pf);
942e85c9a7aSHariprasad Shenai 		sge_params->sge_vf_hps =
943e85c9a7aSHariprasad Shenai 			((sge_params->sge_host_page_size >> s_hps)
944e85c9a7aSHariprasad Shenai 			 & HOSTPAGESIZEPF0_M);
945e85c9a7aSHariprasad Shenai 
946e0a8b34aSHariprasad Shenai 		s_qpp = (QUEUESPERPAGEPF0_S +
947e0a8b34aSHariprasad Shenai 			 (QUEUESPERPAGEPF1_S - QUEUESPERPAGEPF0_S) * pf);
948e0a8b34aSHariprasad Shenai 		sge_params->sge_vf_eq_qpp =
949e0a8b34aSHariprasad Shenai 			((sge_params->sge_egress_queues_per_page >> s_qpp)
950f612b815SHariprasad Shenai 			 & QUEUESPERPAGEPF0_M);
951e0a8b34aSHariprasad Shenai 		sge_params->sge_vf_iq_qpp =
952e0a8b34aSHariprasad Shenai 			((sge_params->sge_ingress_queues_per_page >> s_qpp)
953f612b815SHariprasad Shenai 			 & QUEUESPERPAGEPF0_M);
954e0a8b34aSHariprasad Shenai 	}
955e0a8b34aSHariprasad Shenai 
956f7917c00SJeff Kirsher 	return 0;
957f7917c00SJeff Kirsher }
958f7917c00SJeff Kirsher 
959f7917c00SJeff Kirsher /**
960f7917c00SJeff Kirsher  *	t4vf_get_vpd_params - retrieve device VPD paremeters
961f7917c00SJeff Kirsher  *	@adapter: the adapter
962f7917c00SJeff Kirsher  *
963f7917c00SJeff Kirsher  *	Retrives various device Vital Product Data parameters.  The parameters
964f7917c00SJeff Kirsher  *	are stored in @adapter->params.vpd.
965f7917c00SJeff Kirsher  */
t4vf_get_vpd_params(struct adapter * adapter)966f7917c00SJeff Kirsher int t4vf_get_vpd_params(struct adapter *adapter)
967f7917c00SJeff Kirsher {
968f7917c00SJeff Kirsher 	struct vpd_params *vpd_params = &adapter->params.vpd;
969f7917c00SJeff Kirsher 	u32 params[7], vals[7];
970f7917c00SJeff Kirsher 	int v;
971f7917c00SJeff Kirsher 
9725167865aSHariprasad Shenai 	params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
9735167865aSHariprasad Shenai 		     FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CCLK));
974f7917c00SJeff Kirsher 	v = t4vf_query_params(adapter, 1, params, vals);
975f7917c00SJeff Kirsher 	if (v)
976f7917c00SJeff Kirsher 		return v;
977f7917c00SJeff Kirsher 	vpd_params->cclk = vals[0];
978f7917c00SJeff Kirsher 
979f7917c00SJeff Kirsher 	return 0;
980f7917c00SJeff Kirsher }
981f7917c00SJeff Kirsher 
982f7917c00SJeff Kirsher /**
983f7917c00SJeff Kirsher  *	t4vf_get_dev_params - retrieve device paremeters
984f7917c00SJeff Kirsher  *	@adapter: the adapter
985f7917c00SJeff Kirsher  *
986f7917c00SJeff Kirsher  *	Retrives various device parameters.  The parameters are stored in
987f7917c00SJeff Kirsher  *	@adapter->params.dev.
988f7917c00SJeff Kirsher  */
t4vf_get_dev_params(struct adapter * adapter)989f7917c00SJeff Kirsher int t4vf_get_dev_params(struct adapter *adapter)
990f7917c00SJeff Kirsher {
991f7917c00SJeff Kirsher 	struct dev_params *dev_params = &adapter->params.dev;
992f7917c00SJeff Kirsher 	u32 params[7], vals[7];
993f7917c00SJeff Kirsher 	int v;
994f7917c00SJeff Kirsher 
9955167865aSHariprasad Shenai 	params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
9965167865aSHariprasad Shenai 		     FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_FWREV));
9975167865aSHariprasad Shenai 	params[1] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
9985167865aSHariprasad Shenai 		     FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_TPREV));
999f7917c00SJeff Kirsher 	v = t4vf_query_params(adapter, 2, params, vals);
1000f7917c00SJeff Kirsher 	if (v)
1001f7917c00SJeff Kirsher 		return v;
1002f7917c00SJeff Kirsher 	dev_params->fwrev = vals[0];
1003f7917c00SJeff Kirsher 	dev_params->tprev = vals[1];
1004f7917c00SJeff Kirsher 
1005f7917c00SJeff Kirsher 	return 0;
1006f7917c00SJeff Kirsher }
1007f7917c00SJeff Kirsher 
1008f7917c00SJeff Kirsher /**
1009f7917c00SJeff Kirsher  *	t4vf_get_rss_glb_config - retrieve adapter RSS Global Configuration
1010f7917c00SJeff Kirsher  *	@adapter: the adapter
1011f7917c00SJeff Kirsher  *
1012f7917c00SJeff Kirsher  *	Retrieves global RSS mode and parameters with which we have to live
1013f7917c00SJeff Kirsher  *	and stores them in the @adapter's RSS parameters.
1014f7917c00SJeff Kirsher  */
t4vf_get_rss_glb_config(struct adapter * adapter)1015f7917c00SJeff Kirsher int t4vf_get_rss_glb_config(struct adapter *adapter)
1016f7917c00SJeff Kirsher {
1017f7917c00SJeff Kirsher 	struct rss_params *rss = &adapter->params.rss;
1018f7917c00SJeff Kirsher 	struct fw_rss_glb_config_cmd cmd, rpl;
1019f7917c00SJeff Kirsher 	int v;
1020f7917c00SJeff Kirsher 
1021f7917c00SJeff Kirsher 	/*
1022f7917c00SJeff Kirsher 	 * Execute an RSS Global Configuration read command to retrieve
1023f7917c00SJeff Kirsher 	 * our RSS configuration.
1024f7917c00SJeff Kirsher 	 */
1025f7917c00SJeff Kirsher 	memset(&cmd, 0, sizeof(cmd));
1026e2ac9628SHariprasad Shenai 	cmd.op_to_write = cpu_to_be32(FW_CMD_OP_V(FW_RSS_GLB_CONFIG_CMD) |
1027e2ac9628SHariprasad Shenai 				      FW_CMD_REQUEST_F |
1028e2ac9628SHariprasad Shenai 				      FW_CMD_READ_F);
1029f7917c00SJeff Kirsher 	cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
1030f7917c00SJeff Kirsher 	v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
1031f7917c00SJeff Kirsher 	if (v)
1032f7917c00SJeff Kirsher 		return v;
1033f7917c00SJeff Kirsher 
1034f7917c00SJeff Kirsher 	/*
1035f7917c00SJeff Kirsher 	 * Transate the big-endian RSS Global Configuration into our
1036f7917c00SJeff Kirsher 	 * cpu-endian format based on the RSS mode.  We also do first level
1037f7917c00SJeff Kirsher 	 * filtering at this point to weed out modes which don't support
1038f7917c00SJeff Kirsher 	 * VF Drivers ...
1039f7917c00SJeff Kirsher 	 */
1040b2e1a3f0SHariprasad Shenai 	rss->mode = FW_RSS_GLB_CONFIG_CMD_MODE_G(
1041f7917c00SJeff Kirsher 			be32_to_cpu(rpl.u.manual.mode_pkd));
1042f7917c00SJeff Kirsher 	switch (rss->mode) {
1043f7917c00SJeff Kirsher 	case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
1044f7917c00SJeff Kirsher 		u32 word = be32_to_cpu(
1045f7917c00SJeff Kirsher 				rpl.u.basicvirtual.synmapen_to_hashtoeplitz);
1046f7917c00SJeff Kirsher 
1047f7917c00SJeff Kirsher 		rss->u.basicvirtual.synmapen =
1048b2e1a3f0SHariprasad Shenai 			((word & FW_RSS_GLB_CONFIG_CMD_SYNMAPEN_F) != 0);
1049f7917c00SJeff Kirsher 		rss->u.basicvirtual.syn4tupenipv6 =
1050b2e1a3f0SHariprasad Shenai 			((word & FW_RSS_GLB_CONFIG_CMD_SYN4TUPENIPV6_F) != 0);
1051f7917c00SJeff Kirsher 		rss->u.basicvirtual.syn2tupenipv6 =
1052b2e1a3f0SHariprasad Shenai 			((word & FW_RSS_GLB_CONFIG_CMD_SYN2TUPENIPV6_F) != 0);
1053f7917c00SJeff Kirsher 		rss->u.basicvirtual.syn4tupenipv4 =
1054b2e1a3f0SHariprasad Shenai 			((word & FW_RSS_GLB_CONFIG_CMD_SYN4TUPENIPV4_F) != 0);
1055f7917c00SJeff Kirsher 		rss->u.basicvirtual.syn2tupenipv4 =
1056b2e1a3f0SHariprasad Shenai 			((word & FW_RSS_GLB_CONFIG_CMD_SYN2TUPENIPV4_F) != 0);
1057f7917c00SJeff Kirsher 
1058f7917c00SJeff Kirsher 		rss->u.basicvirtual.ofdmapen =
1059b2e1a3f0SHariprasad Shenai 			((word & FW_RSS_GLB_CONFIG_CMD_OFDMAPEN_F) != 0);
1060f7917c00SJeff Kirsher 
1061f7917c00SJeff Kirsher 		rss->u.basicvirtual.tnlmapen =
1062b2e1a3f0SHariprasad Shenai 			((word & FW_RSS_GLB_CONFIG_CMD_TNLMAPEN_F) != 0);
1063f7917c00SJeff Kirsher 		rss->u.basicvirtual.tnlalllookup =
1064b2e1a3f0SHariprasad Shenai 			((word  & FW_RSS_GLB_CONFIG_CMD_TNLALLLKP_F) != 0);
1065f7917c00SJeff Kirsher 
1066f7917c00SJeff Kirsher 		rss->u.basicvirtual.hashtoeplitz =
1067b2e1a3f0SHariprasad Shenai 			((word & FW_RSS_GLB_CONFIG_CMD_HASHTOEPLITZ_F) != 0);
1068f7917c00SJeff Kirsher 
1069f7917c00SJeff Kirsher 		/* we need at least Tunnel Map Enable to be set */
1070f7917c00SJeff Kirsher 		if (!rss->u.basicvirtual.tnlmapen)
1071f7917c00SJeff Kirsher 			return -EINVAL;
1072f7917c00SJeff Kirsher 		break;
1073f7917c00SJeff Kirsher 	}
1074f7917c00SJeff Kirsher 
1075f7917c00SJeff Kirsher 	default:
1076f7917c00SJeff Kirsher 		/* all unknown/unsupported RSS modes result in an error */
1077f7917c00SJeff Kirsher 		return -EINVAL;
1078f7917c00SJeff Kirsher 	}
1079f7917c00SJeff Kirsher 
1080f7917c00SJeff Kirsher 	return 0;
1081f7917c00SJeff Kirsher }
1082f7917c00SJeff Kirsher 
1083f7917c00SJeff Kirsher /**
1084f7917c00SJeff Kirsher  *	t4vf_get_vfres - retrieve VF resource limits
1085f7917c00SJeff Kirsher  *	@adapter: the adapter
1086f7917c00SJeff Kirsher  *
1087f7917c00SJeff Kirsher  *	Retrieves configured resource limits and capabilities for a virtual
1088f7917c00SJeff Kirsher  *	function.  The results are stored in @adapter->vfres.
1089f7917c00SJeff Kirsher  */
t4vf_get_vfres(struct adapter * adapter)1090f7917c00SJeff Kirsher int t4vf_get_vfres(struct adapter *adapter)
1091f7917c00SJeff Kirsher {
1092f7917c00SJeff Kirsher 	struct vf_resources *vfres = &adapter->params.vfres;
1093f7917c00SJeff Kirsher 	struct fw_pfvf_cmd cmd, rpl;
1094f7917c00SJeff Kirsher 	int v;
1095f7917c00SJeff Kirsher 	u32 word;
1096f7917c00SJeff Kirsher 
1097f7917c00SJeff Kirsher 	/*
1098f7917c00SJeff Kirsher 	 * Execute PFVF Read command to get VF resource limits; bail out early
1099f7917c00SJeff Kirsher 	 * with error on command failure.
1100f7917c00SJeff Kirsher 	 */
1101f7917c00SJeff Kirsher 	memset(&cmd, 0, sizeof(cmd));
1102e2ac9628SHariprasad Shenai 	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_PFVF_CMD) |
1103e2ac9628SHariprasad Shenai 				    FW_CMD_REQUEST_F |
1104e2ac9628SHariprasad Shenai 				    FW_CMD_READ_F);
1105f7917c00SJeff Kirsher 	cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
1106f7917c00SJeff Kirsher 	v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
1107f7917c00SJeff Kirsher 	if (v)
1108f7917c00SJeff Kirsher 		return v;
1109f7917c00SJeff Kirsher 
1110f7917c00SJeff Kirsher 	/*
1111f7917c00SJeff Kirsher 	 * Extract VF resource limits and return success.
1112f7917c00SJeff Kirsher 	 */
1113f7917c00SJeff Kirsher 	word = be32_to_cpu(rpl.niqflint_niq);
11145167865aSHariprasad Shenai 	vfres->niqflint = FW_PFVF_CMD_NIQFLINT_G(word);
11155167865aSHariprasad Shenai 	vfres->niq = FW_PFVF_CMD_NIQ_G(word);
1116f7917c00SJeff Kirsher 
1117f7917c00SJeff Kirsher 	word = be32_to_cpu(rpl.type_to_neq);
11185167865aSHariprasad Shenai 	vfres->neq = FW_PFVF_CMD_NEQ_G(word);
11195167865aSHariprasad Shenai 	vfres->pmask = FW_PFVF_CMD_PMASK_G(word);
1120f7917c00SJeff Kirsher 
1121f7917c00SJeff Kirsher 	word = be32_to_cpu(rpl.tc_to_nexactf);
11225167865aSHariprasad Shenai 	vfres->tc = FW_PFVF_CMD_TC_G(word);
11235167865aSHariprasad Shenai 	vfres->nvi = FW_PFVF_CMD_NVI_G(word);
11245167865aSHariprasad Shenai 	vfres->nexactf = FW_PFVF_CMD_NEXACTF_G(word);
1125f7917c00SJeff Kirsher 
1126f7917c00SJeff Kirsher 	word = be32_to_cpu(rpl.r_caps_to_nethctrl);
11275167865aSHariprasad Shenai 	vfres->r_caps = FW_PFVF_CMD_R_CAPS_G(word);
11285167865aSHariprasad Shenai 	vfres->wx_caps = FW_PFVF_CMD_WX_CAPS_G(word);
11295167865aSHariprasad Shenai 	vfres->nethctrl = FW_PFVF_CMD_NETHCTRL_G(word);
1130f7917c00SJeff Kirsher 
1131f7917c00SJeff Kirsher 	return 0;
1132f7917c00SJeff Kirsher }
1133f7917c00SJeff Kirsher 
1134f7917c00SJeff Kirsher /**
1135f7917c00SJeff Kirsher  *	t4vf_read_rss_vi_config - read a VI's RSS configuration
1136f7917c00SJeff Kirsher  *	@adapter: the adapter
1137f7917c00SJeff Kirsher  *	@viid: Virtual Interface ID
1138f7917c00SJeff Kirsher  *	@config: pointer to host-native VI RSS Configuration buffer
1139f7917c00SJeff Kirsher  *
1140f7917c00SJeff Kirsher  *	Reads the Virtual Interface's RSS configuration information and
1141f7917c00SJeff Kirsher  *	translates it into CPU-native format.
1142f7917c00SJeff Kirsher  */
t4vf_read_rss_vi_config(struct adapter * adapter,unsigned int viid,union rss_vi_config * config)1143f7917c00SJeff Kirsher int t4vf_read_rss_vi_config(struct adapter *adapter, unsigned int viid,
1144f7917c00SJeff Kirsher 			    union rss_vi_config *config)
1145f7917c00SJeff Kirsher {
1146f7917c00SJeff Kirsher 	struct fw_rss_vi_config_cmd cmd, rpl;
1147f7917c00SJeff Kirsher 	int v;
1148f7917c00SJeff Kirsher 
1149f7917c00SJeff Kirsher 	memset(&cmd, 0, sizeof(cmd));
1150e2ac9628SHariprasad Shenai 	cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_RSS_VI_CONFIG_CMD) |
1151e2ac9628SHariprasad Shenai 				     FW_CMD_REQUEST_F |
1152e2ac9628SHariprasad Shenai 				     FW_CMD_READ_F |
1153f7917c00SJeff Kirsher 				     FW_RSS_VI_CONFIG_CMD_VIID(viid));
1154f7917c00SJeff Kirsher 	cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
1155f7917c00SJeff Kirsher 	v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
1156f7917c00SJeff Kirsher 	if (v)
1157f7917c00SJeff Kirsher 		return v;
1158f7917c00SJeff Kirsher 
1159f7917c00SJeff Kirsher 	switch (adapter->params.rss.mode) {
1160f7917c00SJeff Kirsher 	case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
1161f7917c00SJeff Kirsher 		u32 word = be32_to_cpu(rpl.u.basicvirtual.defaultq_to_udpen);
1162f7917c00SJeff Kirsher 
1163f7917c00SJeff Kirsher 		config->basicvirtual.ip6fourtupen =
1164b2e1a3f0SHariprasad Shenai 			((word & FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F) != 0);
1165f7917c00SJeff Kirsher 		config->basicvirtual.ip6twotupen =
1166b2e1a3f0SHariprasad Shenai 			((word & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F) != 0);
1167f7917c00SJeff Kirsher 		config->basicvirtual.ip4fourtupen =
1168b2e1a3f0SHariprasad Shenai 			((word & FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F) != 0);
1169f7917c00SJeff Kirsher 		config->basicvirtual.ip4twotupen =
1170b2e1a3f0SHariprasad Shenai 			((word & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F) != 0);
1171f7917c00SJeff Kirsher 		config->basicvirtual.udpen =
1172b2e1a3f0SHariprasad Shenai 			((word & FW_RSS_VI_CONFIG_CMD_UDPEN_F) != 0);
1173f7917c00SJeff Kirsher 		config->basicvirtual.defaultq =
1174b2e1a3f0SHariprasad Shenai 			FW_RSS_VI_CONFIG_CMD_DEFAULTQ_G(word);
1175f7917c00SJeff Kirsher 		break;
1176f7917c00SJeff Kirsher 	}
1177f7917c00SJeff Kirsher 
1178f7917c00SJeff Kirsher 	default:
1179f7917c00SJeff Kirsher 		return -EINVAL;
1180f7917c00SJeff Kirsher 	}
1181f7917c00SJeff Kirsher 
1182f7917c00SJeff Kirsher 	return 0;
1183f7917c00SJeff Kirsher }
1184f7917c00SJeff Kirsher 
1185f7917c00SJeff Kirsher /**
1186f7917c00SJeff Kirsher  *	t4vf_write_rss_vi_config - write a VI's RSS configuration
1187f7917c00SJeff Kirsher  *	@adapter: the adapter
1188f7917c00SJeff Kirsher  *	@viid: Virtual Interface ID
1189f7917c00SJeff Kirsher  *	@config: pointer to host-native VI RSS Configuration buffer
1190f7917c00SJeff Kirsher  *
1191f7917c00SJeff Kirsher  *	Write the Virtual Interface's RSS configuration information
1192f7917c00SJeff Kirsher  *	(translating it into firmware-native format before writing).
1193f7917c00SJeff Kirsher  */
t4vf_write_rss_vi_config(struct adapter * adapter,unsigned int viid,union rss_vi_config * config)1194f7917c00SJeff Kirsher int t4vf_write_rss_vi_config(struct adapter *adapter, unsigned int viid,
1195f7917c00SJeff Kirsher 			     union rss_vi_config *config)
1196f7917c00SJeff Kirsher {
1197f7917c00SJeff Kirsher 	struct fw_rss_vi_config_cmd cmd, rpl;
1198f7917c00SJeff Kirsher 
1199f7917c00SJeff Kirsher 	memset(&cmd, 0, sizeof(cmd));
1200e2ac9628SHariprasad Shenai 	cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_RSS_VI_CONFIG_CMD) |
1201e2ac9628SHariprasad Shenai 				     FW_CMD_REQUEST_F |
1202e2ac9628SHariprasad Shenai 				     FW_CMD_WRITE_F |
1203f7917c00SJeff Kirsher 				     FW_RSS_VI_CONFIG_CMD_VIID(viid));
1204f7917c00SJeff Kirsher 	cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
1205f7917c00SJeff Kirsher 	switch (adapter->params.rss.mode) {
1206f7917c00SJeff Kirsher 	case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
1207f7917c00SJeff Kirsher 		u32 word = 0;
1208f7917c00SJeff Kirsher 
1209f7917c00SJeff Kirsher 		if (config->basicvirtual.ip6fourtupen)
1210b2e1a3f0SHariprasad Shenai 			word |= FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F;
1211f7917c00SJeff Kirsher 		if (config->basicvirtual.ip6twotupen)
1212b2e1a3f0SHariprasad Shenai 			word |= FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F;
1213f7917c00SJeff Kirsher 		if (config->basicvirtual.ip4fourtupen)
1214b2e1a3f0SHariprasad Shenai 			word |= FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F;
1215f7917c00SJeff Kirsher 		if (config->basicvirtual.ip4twotupen)
1216b2e1a3f0SHariprasad Shenai 			word |= FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F;
1217f7917c00SJeff Kirsher 		if (config->basicvirtual.udpen)
1218b2e1a3f0SHariprasad Shenai 			word |= FW_RSS_VI_CONFIG_CMD_UDPEN_F;
1219b2e1a3f0SHariprasad Shenai 		word |= FW_RSS_VI_CONFIG_CMD_DEFAULTQ_V(
1220f7917c00SJeff Kirsher 				config->basicvirtual.defaultq);
1221f7917c00SJeff Kirsher 		cmd.u.basicvirtual.defaultq_to_udpen = cpu_to_be32(word);
1222f7917c00SJeff Kirsher 		break;
1223f7917c00SJeff Kirsher 	}
1224f7917c00SJeff Kirsher 
1225f7917c00SJeff Kirsher 	default:
1226f7917c00SJeff Kirsher 		return -EINVAL;
1227f7917c00SJeff Kirsher 	}
1228f7917c00SJeff Kirsher 
1229f7917c00SJeff Kirsher 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
1230f7917c00SJeff Kirsher }
1231f7917c00SJeff Kirsher 
1232f7917c00SJeff Kirsher /**
1233f7917c00SJeff Kirsher  *	t4vf_config_rss_range - configure a portion of the RSS mapping table
1234f7917c00SJeff Kirsher  *	@adapter: the adapter
1235f7917c00SJeff Kirsher  *	@viid: Virtual Interface of RSS Table Slice
1236f7917c00SJeff Kirsher  *	@start: starting entry in the table to write
1237f7917c00SJeff Kirsher  *	@n: how many table entries to write
1238f7917c00SJeff Kirsher  *	@rspq: values for the "Response Queue" (Ingress Queue) lookup table
1239f7917c00SJeff Kirsher  *	@nrspq: number of values in @rspq
1240f7917c00SJeff Kirsher  *
1241f7917c00SJeff Kirsher  *	Programs the selected part of the VI's RSS mapping table with the
1242f7917c00SJeff Kirsher  *	provided values.  If @nrspq < @n the supplied values are used repeatedly
1243f7917c00SJeff Kirsher  *	until the full table range is populated.
1244f7917c00SJeff Kirsher  *
1245f7917c00SJeff Kirsher  *	The caller must ensure the values in @rspq are in the range 0..1023.
1246f7917c00SJeff Kirsher  */
t4vf_config_rss_range(struct adapter * adapter,unsigned int viid,int start,int n,const u16 * rspq,int nrspq)1247f7917c00SJeff Kirsher int t4vf_config_rss_range(struct adapter *adapter, unsigned int viid,
1248f7917c00SJeff Kirsher 			  int start, int n, const u16 *rspq, int nrspq)
1249f7917c00SJeff Kirsher {
1250f7917c00SJeff Kirsher 	const u16 *rsp = rspq;
1251f7917c00SJeff Kirsher 	const u16 *rsp_end = rspq+nrspq;
1252f7917c00SJeff Kirsher 	struct fw_rss_ind_tbl_cmd cmd;
1253f7917c00SJeff Kirsher 
1254f7917c00SJeff Kirsher 	/*
1255f7917c00SJeff Kirsher 	 * Initialize firmware command template to write the RSS table.
1256f7917c00SJeff Kirsher 	 */
1257f7917c00SJeff Kirsher 	memset(&cmd, 0, sizeof(cmd));
1258e2ac9628SHariprasad Shenai 	cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_RSS_IND_TBL_CMD) |
1259e2ac9628SHariprasad Shenai 				     FW_CMD_REQUEST_F |
1260e2ac9628SHariprasad Shenai 				     FW_CMD_WRITE_F |
1261b2e1a3f0SHariprasad Shenai 				     FW_RSS_IND_TBL_CMD_VIID_V(viid));
1262f7917c00SJeff Kirsher 	cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
1263f7917c00SJeff Kirsher 
1264f7917c00SJeff Kirsher 	/*
1265f7917c00SJeff Kirsher 	 * Each firmware RSS command can accommodate up to 32 RSS Ingress
1266f7917c00SJeff Kirsher 	 * Queue Identifiers.  These Ingress Queue IDs are packed three to
1267f7917c00SJeff Kirsher 	 * a 32-bit word as 10-bit values with the upper remaining 2 bits
1268f7917c00SJeff Kirsher 	 * reserved.
1269f7917c00SJeff Kirsher 	 */
1270f7917c00SJeff Kirsher 	while (n > 0) {
1271f7917c00SJeff Kirsher 		__be32 *qp = &cmd.iq0_to_iq2;
1272f7917c00SJeff Kirsher 		int nq = min(n, 32);
1273f7917c00SJeff Kirsher 		int ret;
1274f7917c00SJeff Kirsher 
1275f7917c00SJeff Kirsher 		/*
1276f7917c00SJeff Kirsher 		 * Set up the firmware RSS command header to send the next
1277f7917c00SJeff Kirsher 		 * "nq" Ingress Queue IDs to the firmware.
1278f7917c00SJeff Kirsher 		 */
1279f7917c00SJeff Kirsher 		cmd.niqid = cpu_to_be16(nq);
1280f7917c00SJeff Kirsher 		cmd.startidx = cpu_to_be16(start);
1281f7917c00SJeff Kirsher 
1282f7917c00SJeff Kirsher 		/*
1283f7917c00SJeff Kirsher 		 * "nq" more done for the start of the next loop.
1284f7917c00SJeff Kirsher 		 */
1285f7917c00SJeff Kirsher 		start += nq;
1286f7917c00SJeff Kirsher 		n -= nq;
1287f7917c00SJeff Kirsher 
1288f7917c00SJeff Kirsher 		/*
1289f7917c00SJeff Kirsher 		 * While there are still Ingress Queue IDs to stuff into the
1290f7917c00SJeff Kirsher 		 * current firmware RSS command, retrieve them from the
1291f7917c00SJeff Kirsher 		 * Ingress Queue ID array and insert them into the command.
1292f7917c00SJeff Kirsher 		 */
1293f7917c00SJeff Kirsher 		while (nq > 0) {
1294f7917c00SJeff Kirsher 			/*
1295f7917c00SJeff Kirsher 			 * Grab up to the next 3 Ingress Queue IDs (wrapping
1296f7917c00SJeff Kirsher 			 * around the Ingress Queue ID array if necessary) and
1297f7917c00SJeff Kirsher 			 * insert them into the firmware RSS command at the
1298f7917c00SJeff Kirsher 			 * current 3-tuple position within the commad.
1299f7917c00SJeff Kirsher 			 */
1300f7917c00SJeff Kirsher 			u16 qbuf[3];
1301f7917c00SJeff Kirsher 			u16 *qbp = qbuf;
1302f7917c00SJeff Kirsher 			int nqbuf = min(3, nq);
1303f7917c00SJeff Kirsher 
1304f7917c00SJeff Kirsher 			nq -= nqbuf;
1305f7917c00SJeff Kirsher 			qbuf[0] = qbuf[1] = qbuf[2] = 0;
1306f7917c00SJeff Kirsher 			while (nqbuf) {
1307f7917c00SJeff Kirsher 				nqbuf--;
1308f7917c00SJeff Kirsher 				*qbp++ = *rsp++;
1309f7917c00SJeff Kirsher 				if (rsp >= rsp_end)
1310f7917c00SJeff Kirsher 					rsp = rspq;
1311f7917c00SJeff Kirsher 			}
1312b2e1a3f0SHariprasad Shenai 			*qp++ = cpu_to_be32(FW_RSS_IND_TBL_CMD_IQ0_V(qbuf[0]) |
1313b2e1a3f0SHariprasad Shenai 					    FW_RSS_IND_TBL_CMD_IQ1_V(qbuf[1]) |
1314b2e1a3f0SHariprasad Shenai 					    FW_RSS_IND_TBL_CMD_IQ2_V(qbuf[2]));
1315f7917c00SJeff Kirsher 		}
1316f7917c00SJeff Kirsher 
1317f7917c00SJeff Kirsher 		/*
1318f7917c00SJeff Kirsher 		 * Send this portion of the RRS table update to the firmware;
1319f7917c00SJeff Kirsher 		 * bail out on any errors.
1320f7917c00SJeff Kirsher 		 */
1321f7917c00SJeff Kirsher 		ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
1322f7917c00SJeff Kirsher 		if (ret)
1323f7917c00SJeff Kirsher 			return ret;
1324f7917c00SJeff Kirsher 	}
1325f7917c00SJeff Kirsher 	return 0;
1326f7917c00SJeff Kirsher }
1327f7917c00SJeff Kirsher 
1328f7917c00SJeff Kirsher /**
1329f7917c00SJeff Kirsher  *	t4vf_alloc_vi - allocate a virtual interface on a port
1330f7917c00SJeff Kirsher  *	@adapter: the adapter
1331f7917c00SJeff Kirsher  *	@port_id: physical port associated with the VI
1332f7917c00SJeff Kirsher  *
1333f7917c00SJeff Kirsher  *	Allocate a new Virtual Interface and bind it to the indicated
1334f7917c00SJeff Kirsher  *	physical port.  Return the new Virtual Interface Identifier on
1335f7917c00SJeff Kirsher  *	success, or a [negative] error number on failure.
1336f7917c00SJeff Kirsher  */
t4vf_alloc_vi(struct adapter * adapter,int port_id)1337f7917c00SJeff Kirsher int t4vf_alloc_vi(struct adapter *adapter, int port_id)
1338f7917c00SJeff Kirsher {
1339f7917c00SJeff Kirsher 	struct fw_vi_cmd cmd, rpl;
1340f7917c00SJeff Kirsher 	int v;
1341f7917c00SJeff Kirsher 
1342f7917c00SJeff Kirsher 	/*
1343f7917c00SJeff Kirsher 	 * Execute a VI command to allocate Virtual Interface and return its
1344f7917c00SJeff Kirsher 	 * VIID.
1345f7917c00SJeff Kirsher 	 */
1346f7917c00SJeff Kirsher 	memset(&cmd, 0, sizeof(cmd));
1347e2ac9628SHariprasad Shenai 	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_VI_CMD) |
1348e2ac9628SHariprasad Shenai 				    FW_CMD_REQUEST_F |
1349e2ac9628SHariprasad Shenai 				    FW_CMD_WRITE_F |
1350e2ac9628SHariprasad Shenai 				    FW_CMD_EXEC_F);
1351f7917c00SJeff Kirsher 	cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(cmd) |
13522b5fb1f2SHariprasad Shenai 					 FW_VI_CMD_ALLOC_F);
13532b5fb1f2SHariprasad Shenai 	cmd.portid_pkd = FW_VI_CMD_PORTID_V(port_id);
1354f7917c00SJeff Kirsher 	v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
1355f7917c00SJeff Kirsher 	if (v)
1356f7917c00SJeff Kirsher 		return v;
1357f7917c00SJeff Kirsher 
13582b5fb1f2SHariprasad Shenai 	return FW_VI_CMD_VIID_G(be16_to_cpu(rpl.type_viid));
1359f7917c00SJeff Kirsher }
1360f7917c00SJeff Kirsher 
1361f7917c00SJeff Kirsher /**
1362f7917c00SJeff Kirsher  *	t4vf_free_vi -- free a virtual interface
1363f7917c00SJeff Kirsher  *	@adapter: the adapter
1364f7917c00SJeff Kirsher  *	@viid: the virtual interface identifier
1365f7917c00SJeff Kirsher  *
1366f7917c00SJeff Kirsher  *	Free a previously allocated Virtual Interface.  Return an error on
1367f7917c00SJeff Kirsher  *	failure.
1368f7917c00SJeff Kirsher  */
t4vf_free_vi(struct adapter * adapter,int viid)1369f7917c00SJeff Kirsher int t4vf_free_vi(struct adapter *adapter, int viid)
1370f7917c00SJeff Kirsher {
1371f7917c00SJeff Kirsher 	struct fw_vi_cmd cmd;
1372f7917c00SJeff Kirsher 
1373f7917c00SJeff Kirsher 	/*
1374f7917c00SJeff Kirsher 	 * Execute a VI command to free the Virtual Interface.
1375f7917c00SJeff Kirsher 	 */
1376f7917c00SJeff Kirsher 	memset(&cmd, 0, sizeof(cmd));
1377e2ac9628SHariprasad Shenai 	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_VI_CMD) |
1378e2ac9628SHariprasad Shenai 				    FW_CMD_REQUEST_F |
1379e2ac9628SHariprasad Shenai 				    FW_CMD_EXEC_F);
1380f7917c00SJeff Kirsher 	cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(cmd) |
13812b5fb1f2SHariprasad Shenai 					 FW_VI_CMD_FREE_F);
13822b5fb1f2SHariprasad Shenai 	cmd.type_viid = cpu_to_be16(FW_VI_CMD_VIID_V(viid));
1383f7917c00SJeff Kirsher 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
1384f7917c00SJeff Kirsher }
1385f7917c00SJeff Kirsher 
1386f7917c00SJeff Kirsher /**
1387f7917c00SJeff Kirsher  *	t4vf_enable_vi - enable/disable a virtual interface
1388f7917c00SJeff Kirsher  *	@adapter: the adapter
1389f7917c00SJeff Kirsher  *	@viid: the Virtual Interface ID
1390f7917c00SJeff Kirsher  *	@rx_en: 1=enable Rx, 0=disable Rx
1391f7917c00SJeff Kirsher  *	@tx_en: 1=enable Tx, 0=disable Tx
1392f7917c00SJeff Kirsher  *
1393f7917c00SJeff Kirsher  *	Enables/disables a virtual interface.
1394f7917c00SJeff Kirsher  */
t4vf_enable_vi(struct adapter * adapter,unsigned int viid,bool rx_en,bool tx_en)1395f7917c00SJeff Kirsher int t4vf_enable_vi(struct adapter *adapter, unsigned int viid,
1396f7917c00SJeff Kirsher 		   bool rx_en, bool tx_en)
1397f7917c00SJeff Kirsher {
1398f7917c00SJeff Kirsher 	struct fw_vi_enable_cmd cmd;
1399f7917c00SJeff Kirsher 
1400f7917c00SJeff Kirsher 	memset(&cmd, 0, sizeof(cmd));
1401e2ac9628SHariprasad Shenai 	cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_VI_ENABLE_CMD) |
1402e2ac9628SHariprasad Shenai 				     FW_CMD_REQUEST_F |
1403e2ac9628SHariprasad Shenai 				     FW_CMD_EXEC_F |
14042b5fb1f2SHariprasad Shenai 				     FW_VI_ENABLE_CMD_VIID_V(viid));
14052b5fb1f2SHariprasad Shenai 	cmd.ien_to_len16 = cpu_to_be32(FW_VI_ENABLE_CMD_IEN_V(rx_en) |
14062b5fb1f2SHariprasad Shenai 				       FW_VI_ENABLE_CMD_EEN_V(tx_en) |
1407f7917c00SJeff Kirsher 				       FW_LEN16(cmd));
1408f7917c00SJeff Kirsher 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
1409f7917c00SJeff Kirsher }
1410f7917c00SJeff Kirsher 
1411f7917c00SJeff Kirsher /**
1412e2f4f4e9SArjun Vynipadath  *	t4vf_enable_pi - enable/disable a Port's virtual interface
1413e2f4f4e9SArjun Vynipadath  *	@adapter: the adapter
1414e2f4f4e9SArjun Vynipadath  *	@pi: the Port Information structure
1415e2f4f4e9SArjun Vynipadath  *	@rx_en: 1=enable Rx, 0=disable Rx
1416e2f4f4e9SArjun Vynipadath  *	@tx_en: 1=enable Tx, 0=disable Tx
1417e2f4f4e9SArjun Vynipadath  *
1418e2f4f4e9SArjun Vynipadath  *	Enables/disables a Port's virtual interface.  If the Virtual
1419e2f4f4e9SArjun Vynipadath  *	Interface enable/disable operation is successful, we notify the
1420e2f4f4e9SArjun Vynipadath  *	OS-specific code of a potential Link Status change via the OS Contract
1421e2f4f4e9SArjun Vynipadath  *	API t4vf_os_link_changed().
1422e2f4f4e9SArjun Vynipadath  */
t4vf_enable_pi(struct adapter * adapter,struct port_info * pi,bool rx_en,bool tx_en)1423e2f4f4e9SArjun Vynipadath int t4vf_enable_pi(struct adapter *adapter, struct port_info *pi,
1424e2f4f4e9SArjun Vynipadath 		   bool rx_en, bool tx_en)
1425e2f4f4e9SArjun Vynipadath {
1426e2f4f4e9SArjun Vynipadath 	int ret = t4vf_enable_vi(adapter, pi->viid, rx_en, tx_en);
1427e2f4f4e9SArjun Vynipadath 
1428e2f4f4e9SArjun Vynipadath 	if (ret)
1429e2f4f4e9SArjun Vynipadath 		return ret;
1430e2f4f4e9SArjun Vynipadath 	t4vf_os_link_changed(adapter, pi->pidx,
1431e2f4f4e9SArjun Vynipadath 			     rx_en && tx_en && pi->link_cfg.link_ok);
1432e2f4f4e9SArjun Vynipadath 	return 0;
1433e2f4f4e9SArjun Vynipadath }
1434e2f4f4e9SArjun Vynipadath 
1435e2f4f4e9SArjun Vynipadath /**
1436f7917c00SJeff Kirsher  *	t4vf_identify_port - identify a VI's port by blinking its LED
1437f7917c00SJeff Kirsher  *	@adapter: the adapter
1438f7917c00SJeff Kirsher  *	@viid: the Virtual Interface ID
1439f7917c00SJeff Kirsher  *	@nblinks: how many times to blink LED at 2.5 Hz
1440f7917c00SJeff Kirsher  *
1441f7917c00SJeff Kirsher  *	Identifies a VI's port by blinking its LED.
1442f7917c00SJeff Kirsher  */
t4vf_identify_port(struct adapter * adapter,unsigned int viid,unsigned int nblinks)1443f7917c00SJeff Kirsher int t4vf_identify_port(struct adapter *adapter, unsigned int viid,
1444f7917c00SJeff Kirsher 		       unsigned int nblinks)
1445f7917c00SJeff Kirsher {
1446f7917c00SJeff Kirsher 	struct fw_vi_enable_cmd cmd;
1447f7917c00SJeff Kirsher 
1448f7917c00SJeff Kirsher 	memset(&cmd, 0, sizeof(cmd));
1449e2ac9628SHariprasad Shenai 	cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_VI_ENABLE_CMD) |
1450e2ac9628SHariprasad Shenai 				     FW_CMD_REQUEST_F |
1451e2ac9628SHariprasad Shenai 				     FW_CMD_EXEC_F |
14522b5fb1f2SHariprasad Shenai 				     FW_VI_ENABLE_CMD_VIID_V(viid));
14532b5fb1f2SHariprasad Shenai 	cmd.ien_to_len16 = cpu_to_be32(FW_VI_ENABLE_CMD_LED_F |
1454f7917c00SJeff Kirsher 				       FW_LEN16(cmd));
1455f7917c00SJeff Kirsher 	cmd.blinkdur = cpu_to_be16(nblinks);
1456f7917c00SJeff Kirsher 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
1457f7917c00SJeff Kirsher }
1458f7917c00SJeff Kirsher 
1459f7917c00SJeff Kirsher /**
1460f7917c00SJeff Kirsher  *	t4vf_set_rxmode - set Rx properties of a virtual interface
1461f7917c00SJeff Kirsher  *	@adapter: the adapter
1462f7917c00SJeff Kirsher  *	@viid: the VI id
1463f7917c00SJeff Kirsher  *	@mtu: the new MTU or -1 for no change
1464f7917c00SJeff Kirsher  *	@promisc: 1 to enable promiscuous mode, 0 to disable it, -1 no change
1465f7917c00SJeff Kirsher  *	@all_multi: 1 to enable all-multi mode, 0 to disable it, -1 no change
1466f7917c00SJeff Kirsher  *	@bcast: 1 to enable broadcast Rx, 0 to disable it, -1 no change
1467f7917c00SJeff Kirsher  *	@vlanex: 1 to enable hardware VLAN Tag extraction, 0 to disable it,
1468f7917c00SJeff Kirsher  *		-1 no change
146920bb0c8fSRahul Lakkireddy  *	@sleep_ok: call is allowed to sleep
1470f7917c00SJeff Kirsher  *
1471f7917c00SJeff Kirsher  *	Sets Rx properties of a virtual interface.
1472f7917c00SJeff Kirsher  */
t4vf_set_rxmode(struct adapter * adapter,unsigned int viid,int mtu,int promisc,int all_multi,int bcast,int vlanex,bool sleep_ok)1473f7917c00SJeff Kirsher int t4vf_set_rxmode(struct adapter *adapter, unsigned int viid,
1474f7917c00SJeff Kirsher 		    int mtu, int promisc, int all_multi, int bcast, int vlanex,
1475f7917c00SJeff Kirsher 		    bool sleep_ok)
1476f7917c00SJeff Kirsher {
1477f7917c00SJeff Kirsher 	struct fw_vi_rxmode_cmd cmd;
1478f7917c00SJeff Kirsher 
1479f7917c00SJeff Kirsher 	/* convert to FW values */
1480f7917c00SJeff Kirsher 	if (mtu < 0)
14812b5fb1f2SHariprasad Shenai 		mtu = FW_VI_RXMODE_CMD_MTU_M;
1482f7917c00SJeff Kirsher 	if (promisc < 0)
14832b5fb1f2SHariprasad Shenai 		promisc = FW_VI_RXMODE_CMD_PROMISCEN_M;
1484f7917c00SJeff Kirsher 	if (all_multi < 0)
14852b5fb1f2SHariprasad Shenai 		all_multi = FW_VI_RXMODE_CMD_ALLMULTIEN_M;
1486f7917c00SJeff Kirsher 	if (bcast < 0)
14872b5fb1f2SHariprasad Shenai 		bcast = FW_VI_RXMODE_CMD_BROADCASTEN_M;
1488f7917c00SJeff Kirsher 	if (vlanex < 0)
14892b5fb1f2SHariprasad Shenai 		vlanex = FW_VI_RXMODE_CMD_VLANEXEN_M;
1490f7917c00SJeff Kirsher 
1491f7917c00SJeff Kirsher 	memset(&cmd, 0, sizeof(cmd));
1492e2ac9628SHariprasad Shenai 	cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_VI_RXMODE_CMD) |
1493e2ac9628SHariprasad Shenai 				     FW_CMD_REQUEST_F |
1494e2ac9628SHariprasad Shenai 				     FW_CMD_WRITE_F |
14952b5fb1f2SHariprasad Shenai 				     FW_VI_RXMODE_CMD_VIID_V(viid));
1496f7917c00SJeff Kirsher 	cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
1497f7917c00SJeff Kirsher 	cmd.mtu_to_vlanexen =
14982b5fb1f2SHariprasad Shenai 		cpu_to_be32(FW_VI_RXMODE_CMD_MTU_V(mtu) |
14992b5fb1f2SHariprasad Shenai 			    FW_VI_RXMODE_CMD_PROMISCEN_V(promisc) |
15002b5fb1f2SHariprasad Shenai 			    FW_VI_RXMODE_CMD_ALLMULTIEN_V(all_multi) |
15012b5fb1f2SHariprasad Shenai 			    FW_VI_RXMODE_CMD_BROADCASTEN_V(bcast) |
15022b5fb1f2SHariprasad Shenai 			    FW_VI_RXMODE_CMD_VLANEXEN_V(vlanex));
1503f7917c00SJeff Kirsher 	return t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), NULL, sleep_ok);
1504f7917c00SJeff Kirsher }
1505f7917c00SJeff Kirsher 
1506f7917c00SJeff Kirsher /**
1507f7917c00SJeff Kirsher  *	t4vf_alloc_mac_filt - allocates exact-match filters for MAC addresses
1508f7917c00SJeff Kirsher  *	@adapter: the adapter
1509f7917c00SJeff Kirsher  *	@viid: the Virtual Interface Identifier
1510f7917c00SJeff Kirsher  *	@free: if true any existing filters for this VI id are first removed
1511f7917c00SJeff Kirsher  *	@naddr: the number of MAC addresses to allocate filters for (up to 7)
1512f7917c00SJeff Kirsher  *	@addr: the MAC address(es)
1513f7917c00SJeff Kirsher  *	@idx: where to store the index of each allocated filter
1514f7917c00SJeff Kirsher  *	@hash: pointer to hash address filter bitmap
1515f7917c00SJeff Kirsher  *	@sleep_ok: call is allowed to sleep
1516f7917c00SJeff Kirsher  *
1517f7917c00SJeff Kirsher  *	Allocates an exact-match filter for each of the supplied addresses and
1518f7917c00SJeff Kirsher  *	sets it to the corresponding address.  If @idx is not %NULL it should
1519f7917c00SJeff Kirsher  *	have at least @naddr entries, each of which will be set to the index of
1520f7917c00SJeff Kirsher  *	the filter allocated for the corresponding MAC address.  If a filter
1521f7917c00SJeff Kirsher  *	could not be allocated for an address its index is set to 0xffff.
1522f7917c00SJeff Kirsher  *	If @hash is not %NULL addresses that fail to allocate an exact filter
1523f7917c00SJeff Kirsher  *	are hashed and update the hash filter bitmap pointed at by @hash.
1524f7917c00SJeff Kirsher  *
1525f7917c00SJeff Kirsher  *	Returns a negative error number or the number of filters allocated.
1526f7917c00SJeff Kirsher  */
t4vf_alloc_mac_filt(struct adapter * adapter,unsigned int viid,bool free,unsigned int naddr,const u8 ** addr,u16 * idx,u64 * hash,bool sleep_ok)1527f7917c00SJeff Kirsher int t4vf_alloc_mac_filt(struct adapter *adapter, unsigned int viid, bool free,
1528f7917c00SJeff Kirsher 			unsigned int naddr, const u8 **addr, u16 *idx,
1529f7917c00SJeff Kirsher 			u64 *hash, bool sleep_ok)
1530f7917c00SJeff Kirsher {
1531f7917c00SJeff Kirsher 	int offset, ret = 0;
1532f7917c00SJeff Kirsher 	unsigned nfilters = 0;
1533f7917c00SJeff Kirsher 	unsigned int rem = naddr;
1534f7917c00SJeff Kirsher 	struct fw_vi_mac_cmd cmd, rpl;
153541fc2e41SHariprasad Shenai 	unsigned int max_naddr = adapter->params.arch.mps_tcam_size;
1536f7917c00SJeff Kirsher 
1537622c62b5SSantosh Rastapur 	if (naddr > max_naddr)
1538f7917c00SJeff Kirsher 		return -EINVAL;
1539f7917c00SJeff Kirsher 
1540f7917c00SJeff Kirsher 	for (offset = 0; offset < naddr; /**/) {
1541f7917c00SJeff Kirsher 		unsigned int fw_naddr = (rem < ARRAY_SIZE(cmd.u.exact)
1542f7917c00SJeff Kirsher 					 ? rem
1543f7917c00SJeff Kirsher 					 : ARRAY_SIZE(cmd.u.exact));
1544f7917c00SJeff Kirsher 		size_t len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
1545f7917c00SJeff Kirsher 						     u.exact[fw_naddr]), 16);
1546f7917c00SJeff Kirsher 		struct fw_vi_mac_exact *p;
1547f7917c00SJeff Kirsher 		int i;
1548f7917c00SJeff Kirsher 
1549f7917c00SJeff Kirsher 		memset(&cmd, 0, sizeof(cmd));
1550e2ac9628SHariprasad Shenai 		cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_VI_MAC_CMD) |
1551e2ac9628SHariprasad Shenai 					     FW_CMD_REQUEST_F |
1552e2ac9628SHariprasad Shenai 					     FW_CMD_WRITE_F |
1553e2ac9628SHariprasad Shenai 					     (free ? FW_CMD_EXEC_F : 0) |
15542b5fb1f2SHariprasad Shenai 					     FW_VI_MAC_CMD_VIID_V(viid));
1555f7917c00SJeff Kirsher 		cmd.freemacs_to_len16 =
15562b5fb1f2SHariprasad Shenai 			cpu_to_be32(FW_VI_MAC_CMD_FREEMACS_V(free) |
1557e2ac9628SHariprasad Shenai 				    FW_CMD_LEN16_V(len16));
1558f7917c00SJeff Kirsher 
1559f7917c00SJeff Kirsher 		for (i = 0, p = cmd.u.exact; i < fw_naddr; i++, p++) {
1560f7917c00SJeff Kirsher 			p->valid_to_idx = cpu_to_be16(
15612b5fb1f2SHariprasad Shenai 				FW_VI_MAC_CMD_VALID_F |
15622b5fb1f2SHariprasad Shenai 				FW_VI_MAC_CMD_IDX_V(FW_VI_MAC_ADD_MAC));
1563f7917c00SJeff Kirsher 			memcpy(p->macaddr, addr[offset+i], sizeof(p->macaddr));
1564f7917c00SJeff Kirsher 		}
1565f7917c00SJeff Kirsher 
1566f7917c00SJeff Kirsher 
1567f7917c00SJeff Kirsher 		ret = t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), &rpl,
1568f7917c00SJeff Kirsher 					sleep_ok);
1569f7917c00SJeff Kirsher 		if (ret && ret != -ENOMEM)
1570f7917c00SJeff Kirsher 			break;
1571f7917c00SJeff Kirsher 
1572f7917c00SJeff Kirsher 		for (i = 0, p = rpl.u.exact; i < fw_naddr; i++, p++) {
15732b5fb1f2SHariprasad Shenai 			u16 index = FW_VI_MAC_CMD_IDX_G(
1574f7917c00SJeff Kirsher 				be16_to_cpu(p->valid_to_idx));
1575f7917c00SJeff Kirsher 
1576f7917c00SJeff Kirsher 			if (idx)
1577f7917c00SJeff Kirsher 				idx[offset+i] =
1578622c62b5SSantosh Rastapur 					(index >= max_naddr
1579f7917c00SJeff Kirsher 					 ? 0xffff
1580f7917c00SJeff Kirsher 					 : index);
1581622c62b5SSantosh Rastapur 			if (index < max_naddr)
1582f7917c00SJeff Kirsher 				nfilters++;
1583f7917c00SJeff Kirsher 			else if (hash)
1584f7917c00SJeff Kirsher 				*hash |= (1ULL << hash_mac_addr(addr[offset+i]));
1585f7917c00SJeff Kirsher 		}
1586f7917c00SJeff Kirsher 
1587f7917c00SJeff Kirsher 		free = false;
1588f7917c00SJeff Kirsher 		offset += fw_naddr;
1589f7917c00SJeff Kirsher 		rem -= fw_naddr;
1590f7917c00SJeff Kirsher 	}
1591f7917c00SJeff Kirsher 
1592f7917c00SJeff Kirsher 	/*
1593f7917c00SJeff Kirsher 	 * If there were no errors or we merely ran out of room in our MAC
1594f7917c00SJeff Kirsher 	 * address arena, return the number of filters actually written.
1595f7917c00SJeff Kirsher 	 */
1596f7917c00SJeff Kirsher 	if (ret == 0 || ret == -ENOMEM)
1597f7917c00SJeff Kirsher 		ret = nfilters;
1598f7917c00SJeff Kirsher 	return ret;
1599f7917c00SJeff Kirsher }
1600f7917c00SJeff Kirsher 
1601f7917c00SJeff Kirsher /**
1602fe5d2709SHariprasad Shenai  *	t4vf_free_mac_filt - frees exact-match filters of given MAC addresses
1603fe5d2709SHariprasad Shenai  *	@adapter: the adapter
1604fe5d2709SHariprasad Shenai  *	@viid: the VI id
1605fe5d2709SHariprasad Shenai  *	@naddr: the number of MAC addresses to allocate filters for (up to 7)
1606fe5d2709SHariprasad Shenai  *	@addr: the MAC address(es)
1607fe5d2709SHariprasad Shenai  *	@sleep_ok: call is allowed to sleep
1608fe5d2709SHariprasad Shenai  *
1609fe5d2709SHariprasad Shenai  *	Frees the exact-match filter for each of the supplied addresses
1610fe5d2709SHariprasad Shenai  *
1611fe5d2709SHariprasad Shenai  *	Returns a negative error number or the number of filters freed.
1612fe5d2709SHariprasad Shenai  */
t4vf_free_mac_filt(struct adapter * adapter,unsigned int viid,unsigned int naddr,const u8 ** addr,bool sleep_ok)1613fe5d2709SHariprasad Shenai int t4vf_free_mac_filt(struct adapter *adapter, unsigned int viid,
1614fe5d2709SHariprasad Shenai 		       unsigned int naddr, const u8 **addr, bool sleep_ok)
1615fe5d2709SHariprasad Shenai {
1616fe5d2709SHariprasad Shenai 	int offset, ret = 0;
1617fe5d2709SHariprasad Shenai 	struct fw_vi_mac_cmd cmd;
1618fe5d2709SHariprasad Shenai 	unsigned int nfilters = 0;
1619fe5d2709SHariprasad Shenai 	unsigned int max_naddr = adapter->params.arch.mps_tcam_size;
1620fe5d2709SHariprasad Shenai 	unsigned int rem = naddr;
1621fe5d2709SHariprasad Shenai 
1622fe5d2709SHariprasad Shenai 	if (naddr > max_naddr)
1623fe5d2709SHariprasad Shenai 		return -EINVAL;
1624fe5d2709SHariprasad Shenai 
1625fe5d2709SHariprasad Shenai 	for (offset = 0; offset < (int)naddr ; /**/) {
1626fe5d2709SHariprasad Shenai 		unsigned int fw_naddr = (rem < ARRAY_SIZE(cmd.u.exact) ?
1627fe5d2709SHariprasad Shenai 					 rem : ARRAY_SIZE(cmd.u.exact));
1628fe5d2709SHariprasad Shenai 		size_t len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
1629fe5d2709SHariprasad Shenai 						     u.exact[fw_naddr]), 16);
1630fe5d2709SHariprasad Shenai 		struct fw_vi_mac_exact *p;
1631fe5d2709SHariprasad Shenai 		int i;
1632fe5d2709SHariprasad Shenai 
1633fe5d2709SHariprasad Shenai 		memset(&cmd, 0, sizeof(cmd));
1634fe5d2709SHariprasad Shenai 		cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_VI_MAC_CMD) |
1635fe5d2709SHariprasad Shenai 				     FW_CMD_REQUEST_F |
1636fe5d2709SHariprasad Shenai 				     FW_CMD_WRITE_F |
1637fe5d2709SHariprasad Shenai 				     FW_CMD_EXEC_V(0) |
1638fe5d2709SHariprasad Shenai 				     FW_VI_MAC_CMD_VIID_V(viid));
1639fe5d2709SHariprasad Shenai 		cmd.freemacs_to_len16 =
1640fe5d2709SHariprasad Shenai 				cpu_to_be32(FW_VI_MAC_CMD_FREEMACS_V(0) |
1641fe5d2709SHariprasad Shenai 					    FW_CMD_LEN16_V(len16));
1642fe5d2709SHariprasad Shenai 
1643fe5d2709SHariprasad Shenai 		for (i = 0, p = cmd.u.exact; i < (int)fw_naddr; i++, p++) {
1644fe5d2709SHariprasad Shenai 			p->valid_to_idx = cpu_to_be16(
1645fe5d2709SHariprasad Shenai 				FW_VI_MAC_CMD_VALID_F |
1646fe5d2709SHariprasad Shenai 				FW_VI_MAC_CMD_IDX_V(FW_VI_MAC_MAC_BASED_FREE));
1647fe5d2709SHariprasad Shenai 			memcpy(p->macaddr, addr[offset+i], sizeof(p->macaddr));
1648fe5d2709SHariprasad Shenai 		}
1649fe5d2709SHariprasad Shenai 
1650fe5d2709SHariprasad Shenai 		ret = t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), &cmd,
1651fe5d2709SHariprasad Shenai 					sleep_ok);
1652fe5d2709SHariprasad Shenai 		if (ret)
1653fe5d2709SHariprasad Shenai 			break;
1654fe5d2709SHariprasad Shenai 
1655fe5d2709SHariprasad Shenai 		for (i = 0, p = cmd.u.exact; i < fw_naddr; i++, p++) {
1656fe5d2709SHariprasad Shenai 			u16 index = FW_VI_MAC_CMD_IDX_G(
1657fe5d2709SHariprasad Shenai 						be16_to_cpu(p->valid_to_idx));
1658fe5d2709SHariprasad Shenai 
1659fe5d2709SHariprasad Shenai 			if (index < max_naddr)
1660fe5d2709SHariprasad Shenai 				nfilters++;
1661fe5d2709SHariprasad Shenai 		}
1662fe5d2709SHariprasad Shenai 
1663fe5d2709SHariprasad Shenai 		offset += fw_naddr;
1664fe5d2709SHariprasad Shenai 		rem -= fw_naddr;
1665fe5d2709SHariprasad Shenai 	}
1666fe5d2709SHariprasad Shenai 
1667fe5d2709SHariprasad Shenai 	if (ret == 0)
1668fe5d2709SHariprasad Shenai 		ret = nfilters;
1669fe5d2709SHariprasad Shenai 	return ret;
1670fe5d2709SHariprasad Shenai }
1671fe5d2709SHariprasad Shenai 
1672fe5d2709SHariprasad Shenai /**
1673f7917c00SJeff Kirsher  *	t4vf_change_mac - modifies the exact-match filter for a MAC address
1674f7917c00SJeff Kirsher  *	@adapter: the adapter
1675f7917c00SJeff Kirsher  *	@viid: the Virtual Interface ID
1676f7917c00SJeff Kirsher  *	@idx: index of existing filter for old value of MAC address, or -1
1677f7917c00SJeff Kirsher  *	@addr: the new MAC address value
1678f7917c00SJeff Kirsher  *	@persist: if idx < 0, the new MAC allocation should be persistent
1679f7917c00SJeff Kirsher  *
1680f7917c00SJeff Kirsher  *	Modifies an exact-match filter and sets it to the new MAC address.
1681f7917c00SJeff Kirsher  *	Note that in general it is not possible to modify the value of a given
1682f7917c00SJeff Kirsher  *	filter so the generic way to modify an address filter is to free the
1683f7917c00SJeff Kirsher  *	one being used by the old address value and allocate a new filter for
1684f7917c00SJeff Kirsher  *	the new address value.  @idx can be -1 if the address is a new
1685f7917c00SJeff Kirsher  *	addition.
1686f7917c00SJeff Kirsher  *
1687f7917c00SJeff Kirsher  *	Returns a negative error number or the index of the filter with the new
1688f7917c00SJeff Kirsher  *	MAC value.
1689f7917c00SJeff Kirsher  */
t4vf_change_mac(struct adapter * adapter,unsigned int viid,int idx,const u8 * addr,bool persist)1690f7917c00SJeff Kirsher int t4vf_change_mac(struct adapter *adapter, unsigned int viid,
1691f7917c00SJeff Kirsher 		    int idx, const u8 *addr, bool persist)
1692f7917c00SJeff Kirsher {
1693f7917c00SJeff Kirsher 	int ret;
1694f7917c00SJeff Kirsher 	struct fw_vi_mac_cmd cmd, rpl;
1695f7917c00SJeff Kirsher 	struct fw_vi_mac_exact *p = &cmd.u.exact[0];
1696f7917c00SJeff Kirsher 	size_t len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
1697f7917c00SJeff Kirsher 					     u.exact[1]), 16);
169841fc2e41SHariprasad Shenai 	unsigned int max_mac_addr = adapter->params.arch.mps_tcam_size;
1699f7917c00SJeff Kirsher 
1700f7917c00SJeff Kirsher 	/*
1701f7917c00SJeff Kirsher 	 * If this is a new allocation, determine whether it should be
1702f7917c00SJeff Kirsher 	 * persistent (across a "freemacs" operation) or not.
1703f7917c00SJeff Kirsher 	 */
1704f7917c00SJeff Kirsher 	if (idx < 0)
1705f7917c00SJeff Kirsher 		idx = persist ? FW_VI_MAC_ADD_PERSIST_MAC : FW_VI_MAC_ADD_MAC;
1706f7917c00SJeff Kirsher 
1707f7917c00SJeff Kirsher 	memset(&cmd, 0, sizeof(cmd));
1708e2ac9628SHariprasad Shenai 	cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_VI_MAC_CMD) |
1709e2ac9628SHariprasad Shenai 				     FW_CMD_REQUEST_F |
1710e2ac9628SHariprasad Shenai 				     FW_CMD_WRITE_F |
17112b5fb1f2SHariprasad Shenai 				     FW_VI_MAC_CMD_VIID_V(viid));
1712e2ac9628SHariprasad Shenai 	cmd.freemacs_to_len16 = cpu_to_be32(FW_CMD_LEN16_V(len16));
17132b5fb1f2SHariprasad Shenai 	p->valid_to_idx = cpu_to_be16(FW_VI_MAC_CMD_VALID_F |
17142b5fb1f2SHariprasad Shenai 				      FW_VI_MAC_CMD_IDX_V(idx));
1715f7917c00SJeff Kirsher 	memcpy(p->macaddr, addr, sizeof(p->macaddr));
1716f7917c00SJeff Kirsher 
1717f7917c00SJeff Kirsher 	ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
1718f7917c00SJeff Kirsher 	if (ret == 0) {
1719f7917c00SJeff Kirsher 		p = &rpl.u.exact[0];
17202b5fb1f2SHariprasad Shenai 		ret = FW_VI_MAC_CMD_IDX_G(be16_to_cpu(p->valid_to_idx));
172141fc2e41SHariprasad Shenai 		if (ret >= max_mac_addr)
1722f7917c00SJeff Kirsher 			ret = -ENOMEM;
1723f7917c00SJeff Kirsher 	}
1724f7917c00SJeff Kirsher 	return ret;
1725f7917c00SJeff Kirsher }
1726f7917c00SJeff Kirsher 
1727f7917c00SJeff Kirsher /**
1728f7917c00SJeff Kirsher  *	t4vf_set_addr_hash - program the MAC inexact-match hash filter
1729f7917c00SJeff Kirsher  *	@adapter: the adapter
1730f7917c00SJeff Kirsher  *	@viid: the Virtual Interface Identifier
1731f7917c00SJeff Kirsher  *	@ucast: whether the hash filter should also match unicast addresses
1732f7917c00SJeff Kirsher  *	@vec: the value to be written to the hash filter
1733f7917c00SJeff Kirsher  *	@sleep_ok: call is allowed to sleep
1734f7917c00SJeff Kirsher  *
1735f7917c00SJeff Kirsher  *	Sets the 64-bit inexact-match hash filter for a virtual interface.
1736f7917c00SJeff Kirsher  */
t4vf_set_addr_hash(struct adapter * adapter,unsigned int viid,bool ucast,u64 vec,bool sleep_ok)1737f7917c00SJeff Kirsher int t4vf_set_addr_hash(struct adapter *adapter, unsigned int viid,
1738f7917c00SJeff Kirsher 		       bool ucast, u64 vec, bool sleep_ok)
1739f7917c00SJeff Kirsher {
1740f7917c00SJeff Kirsher 	struct fw_vi_mac_cmd cmd;
1741f7917c00SJeff Kirsher 	size_t len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
1742f7917c00SJeff Kirsher 					     u.exact[0]), 16);
1743f7917c00SJeff Kirsher 
1744f7917c00SJeff Kirsher 	memset(&cmd, 0, sizeof(cmd));
1745e2ac9628SHariprasad Shenai 	cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_VI_MAC_CMD) |
1746e2ac9628SHariprasad Shenai 				     FW_CMD_REQUEST_F |
1747e2ac9628SHariprasad Shenai 				     FW_CMD_WRITE_F |
17482b5fb1f2SHariprasad Shenai 				     FW_VI_ENABLE_CMD_VIID_V(viid));
17492b5fb1f2SHariprasad Shenai 	cmd.freemacs_to_len16 = cpu_to_be32(FW_VI_MAC_CMD_HASHVECEN_F |
17502b5fb1f2SHariprasad Shenai 					    FW_VI_MAC_CMD_HASHUNIEN_V(ucast) |
1751e2ac9628SHariprasad Shenai 					    FW_CMD_LEN16_V(len16));
1752f7917c00SJeff Kirsher 	cmd.u.hash.hashvec = cpu_to_be64(vec);
1753f7917c00SJeff Kirsher 	return t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), NULL, sleep_ok);
1754f7917c00SJeff Kirsher }
1755f7917c00SJeff Kirsher 
1756f7917c00SJeff Kirsher /**
1757f7917c00SJeff Kirsher  *	t4vf_get_port_stats - collect "port" statistics
1758f7917c00SJeff Kirsher  *	@adapter: the adapter
1759f7917c00SJeff Kirsher  *	@pidx: the port index
1760f7917c00SJeff Kirsher  *	@s: the stats structure to fill
1761f7917c00SJeff Kirsher  *
1762f7917c00SJeff Kirsher  *	Collect statistics for the "port"'s Virtual Interface.
1763f7917c00SJeff Kirsher  */
t4vf_get_port_stats(struct adapter * adapter,int pidx,struct t4vf_port_stats * s)1764f7917c00SJeff Kirsher int t4vf_get_port_stats(struct adapter *adapter, int pidx,
1765f7917c00SJeff Kirsher 			struct t4vf_port_stats *s)
1766f7917c00SJeff Kirsher {
1767f7917c00SJeff Kirsher 	struct port_info *pi = adap2pinfo(adapter, pidx);
1768f7917c00SJeff Kirsher 	struct fw_vi_stats_vf fwstats;
1769f7917c00SJeff Kirsher 	unsigned int rem = VI_VF_NUM_STATS;
1770f7917c00SJeff Kirsher 	__be64 *fwsp = (__be64 *)&fwstats;
1771f7917c00SJeff Kirsher 
1772f7917c00SJeff Kirsher 	/*
1773f7917c00SJeff Kirsher 	 * Grab the Virtual Interface statistics a chunk at a time via mailbox
1774f7917c00SJeff Kirsher 	 * commands.  We could use a Work Request and get all of them at once
1775f7917c00SJeff Kirsher 	 * but that's an asynchronous interface which is awkward to use.
1776f7917c00SJeff Kirsher 	 */
1777f7917c00SJeff Kirsher 	while (rem) {
1778f7917c00SJeff Kirsher 		unsigned int ix = VI_VF_NUM_STATS - rem;
1779f7917c00SJeff Kirsher 		unsigned int nstats = min(6U, rem);
1780f7917c00SJeff Kirsher 		struct fw_vi_stats_cmd cmd, rpl;
1781f7917c00SJeff Kirsher 		size_t len = (offsetof(struct fw_vi_stats_cmd, u) +
1782f7917c00SJeff Kirsher 			      sizeof(struct fw_vi_stats_ctl));
1783f7917c00SJeff Kirsher 		size_t len16 = DIV_ROUND_UP(len, 16);
1784f7917c00SJeff Kirsher 		int ret;
1785f7917c00SJeff Kirsher 
1786f7917c00SJeff Kirsher 		memset(&cmd, 0, sizeof(cmd));
1787e2ac9628SHariprasad Shenai 		cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_VI_STATS_CMD) |
17882b5fb1f2SHariprasad Shenai 					     FW_VI_STATS_CMD_VIID_V(pi->viid) |
1789e2ac9628SHariprasad Shenai 					     FW_CMD_REQUEST_F |
1790e2ac9628SHariprasad Shenai 					     FW_CMD_READ_F);
1791e2ac9628SHariprasad Shenai 		cmd.retval_len16 = cpu_to_be32(FW_CMD_LEN16_V(len16));
1792f7917c00SJeff Kirsher 		cmd.u.ctl.nstats_ix =
17932b5fb1f2SHariprasad Shenai 			cpu_to_be16(FW_VI_STATS_CMD_IX_V(ix) |
17942b5fb1f2SHariprasad Shenai 				    FW_VI_STATS_CMD_NSTATS_V(nstats));
1795f7917c00SJeff Kirsher 		ret = t4vf_wr_mbox_ns(adapter, &cmd, len, &rpl);
1796f7917c00SJeff Kirsher 		if (ret)
1797f7917c00SJeff Kirsher 			return ret;
1798f7917c00SJeff Kirsher 
1799f7917c00SJeff Kirsher 		memcpy(fwsp, &rpl.u.ctl.stat0, sizeof(__be64) * nstats);
1800f7917c00SJeff Kirsher 
1801f7917c00SJeff Kirsher 		rem -= nstats;
1802f7917c00SJeff Kirsher 		fwsp += nstats;
1803f7917c00SJeff Kirsher 	}
1804f7917c00SJeff Kirsher 
1805f7917c00SJeff Kirsher 	/*
1806f7917c00SJeff Kirsher 	 * Translate firmware statistics into host native statistics.
1807f7917c00SJeff Kirsher 	 */
1808f7917c00SJeff Kirsher 	s->tx_bcast_bytes = be64_to_cpu(fwstats.tx_bcast_bytes);
1809f7917c00SJeff Kirsher 	s->tx_bcast_frames = be64_to_cpu(fwstats.tx_bcast_frames);
1810f7917c00SJeff Kirsher 	s->tx_mcast_bytes = be64_to_cpu(fwstats.tx_mcast_bytes);
1811f7917c00SJeff Kirsher 	s->tx_mcast_frames = be64_to_cpu(fwstats.tx_mcast_frames);
1812f7917c00SJeff Kirsher 	s->tx_ucast_bytes = be64_to_cpu(fwstats.tx_ucast_bytes);
1813f7917c00SJeff Kirsher 	s->tx_ucast_frames = be64_to_cpu(fwstats.tx_ucast_frames);
1814f7917c00SJeff Kirsher 	s->tx_drop_frames = be64_to_cpu(fwstats.tx_drop_frames);
1815f7917c00SJeff Kirsher 	s->tx_offload_bytes = be64_to_cpu(fwstats.tx_offload_bytes);
1816f7917c00SJeff Kirsher 	s->tx_offload_frames = be64_to_cpu(fwstats.tx_offload_frames);
1817f7917c00SJeff Kirsher 
1818f7917c00SJeff Kirsher 	s->rx_bcast_bytes = be64_to_cpu(fwstats.rx_bcast_bytes);
1819f7917c00SJeff Kirsher 	s->rx_bcast_frames = be64_to_cpu(fwstats.rx_bcast_frames);
1820f7917c00SJeff Kirsher 	s->rx_mcast_bytes = be64_to_cpu(fwstats.rx_mcast_bytes);
1821f7917c00SJeff Kirsher 	s->rx_mcast_frames = be64_to_cpu(fwstats.rx_mcast_frames);
1822f7917c00SJeff Kirsher 	s->rx_ucast_bytes = be64_to_cpu(fwstats.rx_ucast_bytes);
1823f7917c00SJeff Kirsher 	s->rx_ucast_frames = be64_to_cpu(fwstats.rx_ucast_frames);
1824f7917c00SJeff Kirsher 
1825f7917c00SJeff Kirsher 	s->rx_err_frames = be64_to_cpu(fwstats.rx_err_frames);
1826f7917c00SJeff Kirsher 
1827f7917c00SJeff Kirsher 	return 0;
1828f7917c00SJeff Kirsher }
1829f7917c00SJeff Kirsher 
1830f7917c00SJeff Kirsher /**
1831f7917c00SJeff Kirsher  *	t4vf_iq_free - free an ingress queue and its free lists
1832f7917c00SJeff Kirsher  *	@adapter: the adapter
1833f7917c00SJeff Kirsher  *	@iqtype: the ingress queue type (FW_IQ_TYPE_FL_INT_CAP, etc.)
1834f7917c00SJeff Kirsher  *	@iqid: ingress queue ID
1835f7917c00SJeff Kirsher  *	@fl0id: FL0 queue ID or 0xffff if no attached FL0
1836f7917c00SJeff Kirsher  *	@fl1id: FL1 queue ID or 0xffff if no attached FL1
1837f7917c00SJeff Kirsher  *
1838f7917c00SJeff Kirsher  *	Frees an ingress queue and its associated free lists, if any.
1839f7917c00SJeff Kirsher  */
t4vf_iq_free(struct adapter * adapter,unsigned int iqtype,unsigned int iqid,unsigned int fl0id,unsigned int fl1id)1840f7917c00SJeff Kirsher int t4vf_iq_free(struct adapter *adapter, unsigned int iqtype,
1841f7917c00SJeff Kirsher 		 unsigned int iqid, unsigned int fl0id, unsigned int fl1id)
1842f7917c00SJeff Kirsher {
1843f7917c00SJeff Kirsher 	struct fw_iq_cmd cmd;
1844f7917c00SJeff Kirsher 
1845f7917c00SJeff Kirsher 	memset(&cmd, 0, sizeof(cmd));
1846e2ac9628SHariprasad Shenai 	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_IQ_CMD) |
1847e2ac9628SHariprasad Shenai 				    FW_CMD_REQUEST_F |
1848e2ac9628SHariprasad Shenai 				    FW_CMD_EXEC_F);
18496e4b51a6SHariprasad Shenai 	cmd.alloc_to_len16 = cpu_to_be32(FW_IQ_CMD_FREE_F |
1850f7917c00SJeff Kirsher 					 FW_LEN16(cmd));
1851f7917c00SJeff Kirsher 	cmd.type_to_iqandstindex =
18526e4b51a6SHariprasad Shenai 		cpu_to_be32(FW_IQ_CMD_TYPE_V(iqtype));
1853f7917c00SJeff Kirsher 
1854f7917c00SJeff Kirsher 	cmd.iqid = cpu_to_be16(iqid);
1855f7917c00SJeff Kirsher 	cmd.fl0id = cpu_to_be16(fl0id);
1856f7917c00SJeff Kirsher 	cmd.fl1id = cpu_to_be16(fl1id);
1857f7917c00SJeff Kirsher 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
1858f7917c00SJeff Kirsher }
1859f7917c00SJeff Kirsher 
1860f7917c00SJeff Kirsher /**
1861f7917c00SJeff Kirsher  *	t4vf_eth_eq_free - free an Ethernet egress queue
1862f7917c00SJeff Kirsher  *	@adapter: the adapter
1863f7917c00SJeff Kirsher  *	@eqid: egress queue ID
1864f7917c00SJeff Kirsher  *
1865f7917c00SJeff Kirsher  *	Frees an Ethernet egress queue.
1866f7917c00SJeff Kirsher  */
t4vf_eth_eq_free(struct adapter * adapter,unsigned int eqid)1867f7917c00SJeff Kirsher int t4vf_eth_eq_free(struct adapter *adapter, unsigned int eqid)
1868f7917c00SJeff Kirsher {
1869f7917c00SJeff Kirsher 	struct fw_eq_eth_cmd cmd;
1870f7917c00SJeff Kirsher 
1871f7917c00SJeff Kirsher 	memset(&cmd, 0, sizeof(cmd));
1872e2ac9628SHariprasad Shenai 	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_EQ_ETH_CMD) |
1873e2ac9628SHariprasad Shenai 				    FW_CMD_REQUEST_F |
1874e2ac9628SHariprasad Shenai 				    FW_CMD_EXEC_F);
18756e4b51a6SHariprasad Shenai 	cmd.alloc_to_len16 = cpu_to_be32(FW_EQ_ETH_CMD_FREE_F |
1876f7917c00SJeff Kirsher 					 FW_LEN16(cmd));
18776e4b51a6SHariprasad Shenai 	cmd.eqid_pkd = cpu_to_be32(FW_EQ_ETH_CMD_EQID_V(eqid));
1878f7917c00SJeff Kirsher 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
1879f7917c00SJeff Kirsher }
1880f7917c00SJeff Kirsher 
1881f7917c00SJeff Kirsher /**
1882c3168cabSGanesh Goudar  *	t4vf_link_down_rc_str - return a string for a Link Down Reason Code
1883c3168cabSGanesh Goudar  *	@link_down_rc: Link Down Reason Code
1884c3168cabSGanesh Goudar  *
1885c3168cabSGanesh Goudar  *	Returns a string representation of the Link Down Reason Code.
1886c3168cabSGanesh Goudar  */
t4vf_link_down_rc_str(unsigned char link_down_rc)1887ebf6b131SColin Ian King static const char *t4vf_link_down_rc_str(unsigned char link_down_rc)
1888c3168cabSGanesh Goudar {
1889c3168cabSGanesh Goudar 	static const char * const reason[] = {
1890c3168cabSGanesh Goudar 		"Link Down",
1891c3168cabSGanesh Goudar 		"Remote Fault",
1892c3168cabSGanesh Goudar 		"Auto-negotiation Failure",
1893c3168cabSGanesh Goudar 		"Reserved",
1894c3168cabSGanesh Goudar 		"Insufficient Airflow",
1895c3168cabSGanesh Goudar 		"Unable To Determine Reason",
1896c3168cabSGanesh Goudar 		"No RX Signal Detected",
1897c3168cabSGanesh Goudar 		"Reserved",
1898c3168cabSGanesh Goudar 	};
1899c3168cabSGanesh Goudar 
1900c3168cabSGanesh Goudar 	if (link_down_rc >= ARRAY_SIZE(reason))
1901c3168cabSGanesh Goudar 		return "Bad Reason Code";
1902c3168cabSGanesh Goudar 
1903c3168cabSGanesh Goudar 	return reason[link_down_rc];
1904c3168cabSGanesh Goudar }
1905c3168cabSGanesh Goudar 
1906c3168cabSGanesh Goudar /**
1907c3168cabSGanesh Goudar  *	t4vf_handle_get_port_info - process a FW reply message
1908c3168cabSGanesh Goudar  *	@pi: the port info
190920bb0c8fSRahul Lakkireddy  *	@cmd: start of the FW message
1910c3168cabSGanesh Goudar  *
1911c3168cabSGanesh Goudar  *	Processes a GET_PORT_INFO FW reply message.
1912c3168cabSGanesh Goudar  */
t4vf_handle_get_port_info(struct port_info * pi,const struct fw_port_cmd * cmd)1913ebf6b131SColin Ian King static void t4vf_handle_get_port_info(struct port_info *pi,
1914c3168cabSGanesh Goudar 				      const struct fw_port_cmd *cmd)
1915c3168cabSGanesh Goudar {
1916c3168cabSGanesh Goudar 	fw_port_cap32_t pcaps, acaps, lpacaps, linkattr;
19170caeaf6aSRahul Lakkireddy 	struct link_config *lc = &pi->link_cfg;
19180caeaf6aSRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
19190caeaf6aSRahul Lakkireddy 	unsigned int speed, fc, fec, adv_fc;
19200caeaf6aSRahul Lakkireddy 	enum fw_port_module_type mod_type;
19210caeaf6aSRahul Lakkireddy 	int action, link_ok, linkdnrc;
19220caeaf6aSRahul Lakkireddy 	enum fw_port_type port_type;
1923c3168cabSGanesh Goudar 
1924c3168cabSGanesh Goudar 	/* Extract the various fields from the Port Information message. */
19250caeaf6aSRahul Lakkireddy 	action = FW_PORT_CMD_ACTION_G(be32_to_cpu(cmd->action_to_len16));
1926c3168cabSGanesh Goudar 	switch (action) {
1927c3168cabSGanesh Goudar 	case FW_PORT_ACTION_GET_PORT_INFO: {
1928c3168cabSGanesh Goudar 		u32 lstatus = be32_to_cpu(cmd->u.info.lstatus_to_modtype);
1929c3168cabSGanesh Goudar 
1930c3168cabSGanesh Goudar 		link_ok = (lstatus & FW_PORT_CMD_LSTATUS_F) != 0;
1931c3168cabSGanesh Goudar 		linkdnrc = FW_PORT_CMD_LINKDNRC_G(lstatus);
1932c3168cabSGanesh Goudar 		port_type = FW_PORT_CMD_PTYPE_G(lstatus);
1933c3168cabSGanesh Goudar 		mod_type = FW_PORT_CMD_MODTYPE_G(lstatus);
1934c3168cabSGanesh Goudar 		pcaps = fwcaps16_to_caps32(be16_to_cpu(cmd->u.info.pcap));
1935c3168cabSGanesh Goudar 		acaps = fwcaps16_to_caps32(be16_to_cpu(cmd->u.info.acap));
1936c3168cabSGanesh Goudar 		lpacaps = fwcaps16_to_caps32(be16_to_cpu(cmd->u.info.lpacap));
1937c3168cabSGanesh Goudar 
1938c3168cabSGanesh Goudar 		/* Unfortunately the format of the Link Status in the old
1939c3168cabSGanesh Goudar 		 * 16-bit Port Information message isn't the same as the
1940c3168cabSGanesh Goudar 		 * 16-bit Port Capabilities bitfield used everywhere else ...
1941c3168cabSGanesh Goudar 		 */
1942c3168cabSGanesh Goudar 		linkattr = 0;
1943c3168cabSGanesh Goudar 		if (lstatus & FW_PORT_CMD_RXPAUSE_F)
1944c3168cabSGanesh Goudar 			linkattr |= FW_PORT_CAP32_FC_RX;
1945c3168cabSGanesh Goudar 		if (lstatus & FW_PORT_CMD_TXPAUSE_F)
1946c3168cabSGanesh Goudar 			linkattr |= FW_PORT_CAP32_FC_TX;
1947c3168cabSGanesh Goudar 		if (lstatus & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_100M))
1948c3168cabSGanesh Goudar 			linkattr |= FW_PORT_CAP32_SPEED_100M;
1949c3168cabSGanesh Goudar 		if (lstatus & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_1G))
1950c3168cabSGanesh Goudar 			linkattr |= FW_PORT_CAP32_SPEED_1G;
1951c3168cabSGanesh Goudar 		if (lstatus & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_10G))
1952c3168cabSGanesh Goudar 			linkattr |= FW_PORT_CAP32_SPEED_10G;
1953c3168cabSGanesh Goudar 		if (lstatus & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_25G))
1954c3168cabSGanesh Goudar 			linkattr |= FW_PORT_CAP32_SPEED_25G;
1955c3168cabSGanesh Goudar 		if (lstatus & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_40G))
1956c3168cabSGanesh Goudar 			linkattr |= FW_PORT_CAP32_SPEED_40G;
1957c3168cabSGanesh Goudar 		if (lstatus & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_100G))
1958c3168cabSGanesh Goudar 			linkattr |= FW_PORT_CAP32_SPEED_100G;
1959c3168cabSGanesh Goudar 
1960c3168cabSGanesh Goudar 		break;
1961c3168cabSGanesh Goudar 	}
1962c3168cabSGanesh Goudar 
1963c3168cabSGanesh Goudar 	case FW_PORT_ACTION_GET_PORT_INFO32: {
1964c3168cabSGanesh Goudar 		u32 lstatus32;
1965c3168cabSGanesh Goudar 
1966c3168cabSGanesh Goudar 		lstatus32 = be32_to_cpu(cmd->u.info32.lstatus32_to_cbllen32);
1967c3168cabSGanesh Goudar 		link_ok = (lstatus32 & FW_PORT_CMD_LSTATUS32_F) != 0;
1968c3168cabSGanesh Goudar 		linkdnrc = FW_PORT_CMD_LINKDNRC32_G(lstatus32);
1969c3168cabSGanesh Goudar 		port_type = FW_PORT_CMD_PORTTYPE32_G(lstatus32);
1970c3168cabSGanesh Goudar 		mod_type = FW_PORT_CMD_MODTYPE32_G(lstatus32);
1971c3168cabSGanesh Goudar 		pcaps = be32_to_cpu(cmd->u.info32.pcaps32);
1972c3168cabSGanesh Goudar 		acaps = be32_to_cpu(cmd->u.info32.acaps32);
1973c3168cabSGanesh Goudar 		lpacaps = be32_to_cpu(cmd->u.info32.lpacaps32);
1974c3168cabSGanesh Goudar 		linkattr = be32_to_cpu(cmd->u.info32.linkattr32);
1975c3168cabSGanesh Goudar 		break;
1976c3168cabSGanesh Goudar 	}
1977c3168cabSGanesh Goudar 
1978c3168cabSGanesh Goudar 	default:
1979c3168cabSGanesh Goudar 		dev_err(adapter->pdev_dev, "Handle Port Information: Bad Command/Action %#x\n",
1980c3168cabSGanesh Goudar 			be32_to_cpu(cmd->action_to_len16));
1981c3168cabSGanesh Goudar 		return;
1982c3168cabSGanesh Goudar 	}
1983c3168cabSGanesh Goudar 
1984c3168cabSGanesh Goudar 	fec = fwcap_to_cc_fec(acaps);
19850caeaf6aSRahul Lakkireddy 	adv_fc = fwcap_to_cc_pause(acaps);
1986c3168cabSGanesh Goudar 	fc = fwcap_to_cc_pause(linkattr);
1987c3168cabSGanesh Goudar 	speed = fwcap_to_speed(linkattr);
1988c3168cabSGanesh Goudar 
1989c3168cabSGanesh Goudar 	if (mod_type != pi->mod_type) {
1990c3168cabSGanesh Goudar 		/* When a new Transceiver Module is inserted, the Firmware
1991c3168cabSGanesh Goudar 		 * will examine any Forward Error Correction parameters
1992c3168cabSGanesh Goudar 		 * present in the Transceiver Module i2c EPROM and determine
1993c3168cabSGanesh Goudar 		 * the supported and recommended FEC settings from those
1994c3168cabSGanesh Goudar 		 * based on IEEE 802.3 standards.  We always record the
1995c3168cabSGanesh Goudar 		 * IEEE 802.3 recommended "automatic" settings.
1996c3168cabSGanesh Goudar 		 */
1997c3168cabSGanesh Goudar 		lc->auto_fec = fec;
1998c3168cabSGanesh Goudar 
1999c3168cabSGanesh Goudar 		/* Some versions of the early T6 Firmware "cheated" when
2000c3168cabSGanesh Goudar 		 * handling different Transceiver Modules by changing the
2001c3168cabSGanesh Goudar 		 * underlaying Port Type reported to the Host Drivers.  As
2002c3168cabSGanesh Goudar 		 * such we need to capture whatever Port Type the Firmware
2003c3168cabSGanesh Goudar 		 * sends us and record it in case it's different from what we
2004c3168cabSGanesh Goudar 		 * were told earlier.  Unfortunately, since Firmware is
2005c3168cabSGanesh Goudar 		 * forever, we'll need to keep this code here forever, but in
2006c3168cabSGanesh Goudar 		 * later T6 Firmware it should just be an assignment of the
2007c3168cabSGanesh Goudar 		 * same value already recorded.
2008c3168cabSGanesh Goudar 		 */
2009c3168cabSGanesh Goudar 		pi->port_type = port_type;
2010c3168cabSGanesh Goudar 
2011c3168cabSGanesh Goudar 		pi->mod_type = mod_type;
2012c3168cabSGanesh Goudar 		t4vf_os_portmod_changed(adapter, pi->pidx);
2013c3168cabSGanesh Goudar 	}
2014c3168cabSGanesh Goudar 
2015c3168cabSGanesh Goudar 	if (link_ok != lc->link_ok || speed != lc->speed ||
20160caeaf6aSRahul Lakkireddy 	    fc != lc->fc || adv_fc != lc->advertised_fc ||
20170caeaf6aSRahul Lakkireddy 	    fec != lc->fec) {
20180caeaf6aSRahul Lakkireddy 		/* something changed */
2019c3168cabSGanesh Goudar 		if (!link_ok && lc->link_ok) {
2020c3168cabSGanesh Goudar 			lc->link_down_rc = linkdnrc;
202195eb7882SVishal Kulkarni 			dev_warn_ratelimited(adapter->pdev_dev,
202295eb7882SVishal Kulkarni 					     "Port %d link down, reason: %s\n",
202395eb7882SVishal Kulkarni 					     pi->port_id,
202495eb7882SVishal Kulkarni 					     t4vf_link_down_rc_str(linkdnrc));
2025c3168cabSGanesh Goudar 		}
2026c3168cabSGanesh Goudar 		lc->link_ok = link_ok;
2027c3168cabSGanesh Goudar 		lc->speed = speed;
20280caeaf6aSRahul Lakkireddy 		lc->advertised_fc = adv_fc;
2029c3168cabSGanesh Goudar 		lc->fc = fc;
2030c3168cabSGanesh Goudar 		lc->fec = fec;
2031c3168cabSGanesh Goudar 
2032c3168cabSGanesh Goudar 		lc->pcaps = pcaps;
2033c3168cabSGanesh Goudar 		lc->lpacaps = lpacaps;
2034c3168cabSGanesh Goudar 		lc->acaps = acaps & ADVERT_MASK;
2035c3168cabSGanesh Goudar 
203657ccaedbSGanesh Goudar 		/* If we're not physically capable of Auto-Negotiation, note
203757ccaedbSGanesh Goudar 		 * this as Auto-Negotiation disabled.  Otherwise, we track
203857ccaedbSGanesh Goudar 		 * what Auto-Negotiation settings we have.  Note parallel
203957ccaedbSGanesh Goudar 		 * structure in init_link_config().
204057ccaedbSGanesh Goudar 		 */
204157ccaedbSGanesh Goudar 		if (!(lc->pcaps & FW_PORT_CAP32_ANEG)) {
204257ccaedbSGanesh Goudar 			lc->autoneg = AUTONEG_DISABLE;
204357ccaedbSGanesh Goudar 		} else if (lc->acaps & FW_PORT_CAP32_ANEG) {
2044c3168cabSGanesh Goudar 			lc->autoneg = AUTONEG_ENABLE;
2045c3168cabSGanesh Goudar 		} else {
2046c3168cabSGanesh Goudar 			/* When Autoneg is disabled, user needs to set
2047c3168cabSGanesh Goudar 			 * single speed.
2048c3168cabSGanesh Goudar 			 * Similar to cxgb4_ethtool.c: set_link_ksettings
2049c3168cabSGanesh Goudar 			 */
2050c3168cabSGanesh Goudar 			lc->acaps = 0;
2051c3168cabSGanesh Goudar 			lc->speed_caps = fwcap_to_speed(acaps);
2052c3168cabSGanesh Goudar 			lc->autoneg = AUTONEG_DISABLE;
2053c3168cabSGanesh Goudar 		}
2054c3168cabSGanesh Goudar 
2055c3168cabSGanesh Goudar 		t4vf_os_link_changed(adapter, pi->pidx, link_ok);
2056c3168cabSGanesh Goudar 	}
2057c3168cabSGanesh Goudar }
2058c3168cabSGanesh Goudar 
2059c3168cabSGanesh Goudar /**
2060c3168cabSGanesh Goudar  *	t4vf_update_port_info - retrieve and update port information if changed
2061c3168cabSGanesh Goudar  *	@pi: the port_info
2062c3168cabSGanesh Goudar  *
2063c3168cabSGanesh Goudar  *	We issue a Get Port Information Command to the Firmware and, if
2064c3168cabSGanesh Goudar  *	successful, we check to see if anything is different from what we
2065c3168cabSGanesh Goudar  *	last recorded and update things accordingly.
2066c3168cabSGanesh Goudar  */
t4vf_update_port_info(struct port_info * pi)2067c3168cabSGanesh Goudar int t4vf_update_port_info(struct port_info *pi)
2068c3168cabSGanesh Goudar {
2069c3168cabSGanesh Goudar 	unsigned int fw_caps = pi->adapter->params.fw_caps_support;
2070c3168cabSGanesh Goudar 	struct fw_port_cmd port_cmd;
2071c3168cabSGanesh Goudar 	int ret;
2072c3168cabSGanesh Goudar 
2073c3168cabSGanesh Goudar 	memset(&port_cmd, 0, sizeof(port_cmd));
2074c3168cabSGanesh Goudar 	port_cmd.op_to_portid = cpu_to_be32(FW_CMD_OP_V(FW_PORT_CMD) |
2075c3168cabSGanesh Goudar 					    FW_CMD_REQUEST_F | FW_CMD_READ_F |
2076c3168cabSGanesh Goudar 					    FW_PORT_CMD_PORTID_V(pi->port_id));
2077c3168cabSGanesh Goudar 	port_cmd.action_to_len16 = cpu_to_be32(
2078c3168cabSGanesh Goudar 		FW_PORT_CMD_ACTION_V(fw_caps == FW_CAPS16
2079c3168cabSGanesh Goudar 				     ? FW_PORT_ACTION_GET_PORT_INFO
2080c3168cabSGanesh Goudar 				     : FW_PORT_ACTION_GET_PORT_INFO32) |
2081c3168cabSGanesh Goudar 		FW_LEN16(port_cmd));
2082c3168cabSGanesh Goudar 	ret = t4vf_wr_mbox(pi->adapter, &port_cmd, sizeof(port_cmd),
2083c3168cabSGanesh Goudar 			   &port_cmd);
2084c3168cabSGanesh Goudar 	if (ret)
2085c3168cabSGanesh Goudar 		return ret;
2086c3168cabSGanesh Goudar 	t4vf_handle_get_port_info(pi, &port_cmd);
2087c3168cabSGanesh Goudar 	return 0;
2088c3168cabSGanesh Goudar }
2089c3168cabSGanesh Goudar 
2090c3168cabSGanesh Goudar /**
2091f7917c00SJeff Kirsher  *	t4vf_handle_fw_rpl - process a firmware reply message
2092f7917c00SJeff Kirsher  *	@adapter: the adapter
2093f7917c00SJeff Kirsher  *	@rpl: start of the firmware message
2094f7917c00SJeff Kirsher  *
2095f7917c00SJeff Kirsher  *	Processes a firmware message, such as link state change messages.
2096f7917c00SJeff Kirsher  */
t4vf_handle_fw_rpl(struct adapter * adapter,const __be64 * rpl)2097f7917c00SJeff Kirsher int t4vf_handle_fw_rpl(struct adapter *adapter, const __be64 *rpl)
2098f7917c00SJeff Kirsher {
2099f7917c00SJeff Kirsher 	const struct fw_cmd_hdr *cmd_hdr = (const struct fw_cmd_hdr *)rpl;
2100e2ac9628SHariprasad Shenai 	u8 opcode = FW_CMD_OP_G(be32_to_cpu(cmd_hdr->hi));
2101f7917c00SJeff Kirsher 
2102f7917c00SJeff Kirsher 	switch (opcode) {
2103f7917c00SJeff Kirsher 	case FW_PORT_CMD: {
2104f7917c00SJeff Kirsher 		/*
2105f7917c00SJeff Kirsher 		 * Link/module state change message.
2106f7917c00SJeff Kirsher 		 */
2107f7917c00SJeff Kirsher 		const struct fw_port_cmd *port_cmd =
2108f7917c00SJeff Kirsher 			(const struct fw_port_cmd *)rpl;
2109c3168cabSGanesh Goudar 		int action = FW_PORT_CMD_ACTION_G(
2110f7917c00SJeff Kirsher 			be32_to_cpu(port_cmd->action_to_len16));
2111c3168cabSGanesh Goudar 		int port_id, pidx;
2112c3168cabSGanesh Goudar 
2113c3168cabSGanesh Goudar 		if (action != FW_PORT_ACTION_GET_PORT_INFO &&
2114c3168cabSGanesh Goudar 		    action != FW_PORT_ACTION_GET_PORT_INFO32) {
2115f7917c00SJeff Kirsher 			dev_err(adapter->pdev_dev,
2116f7917c00SJeff Kirsher 				"Unknown firmware PORT reply action %x\n",
2117f7917c00SJeff Kirsher 				action);
2118f7917c00SJeff Kirsher 			break;
2119f7917c00SJeff Kirsher 		}
2120f7917c00SJeff Kirsher 
21212b5fb1f2SHariprasad Shenai 		port_id = FW_PORT_CMD_PORTID_G(
2122f7917c00SJeff Kirsher 			be32_to_cpu(port_cmd->op_to_portid));
2123f7917c00SJeff Kirsher 		for_each_port(adapter, pidx) {
2124f7917c00SJeff Kirsher 			struct port_info *pi = adap2pinfo(adapter, pidx);
2125f7917c00SJeff Kirsher 
2126f7917c00SJeff Kirsher 			if (pi->port_id != port_id)
2127f7917c00SJeff Kirsher 				continue;
2128c3168cabSGanesh Goudar 			t4vf_handle_get_port_info(pi, port_cmd);
2129f7917c00SJeff Kirsher 		}
2130f7917c00SJeff Kirsher 		break;
2131f7917c00SJeff Kirsher 	}
2132f7917c00SJeff Kirsher 
2133f7917c00SJeff Kirsher 	default:
2134f7917c00SJeff Kirsher 		dev_err(adapter->pdev_dev, "Unknown firmware reply %X\n",
2135f7917c00SJeff Kirsher 			opcode);
2136f7917c00SJeff Kirsher 	}
2137f7917c00SJeff Kirsher 	return 0;
2138f7917c00SJeff Kirsher }
2139e0a8b34aSHariprasad Shenai 
t4vf_prep_adapter(struct adapter * adapter)2140e0a8b34aSHariprasad Shenai int t4vf_prep_adapter(struct adapter *adapter)
2141e0a8b34aSHariprasad Shenai {
2142e0a8b34aSHariprasad Shenai 	int err;
2143e0a8b34aSHariprasad Shenai 	unsigned int chipid;
2144e0a8b34aSHariprasad Shenai 
2145e0a8b34aSHariprasad Shenai 	/* Wait for the device to become ready before proceeding ...
2146e0a8b34aSHariprasad Shenai 	 */
2147e0a8b34aSHariprasad Shenai 	err = t4vf_wait_dev_ready(adapter);
2148e0a8b34aSHariprasad Shenai 	if (err)
2149e0a8b34aSHariprasad Shenai 		return err;
2150e0a8b34aSHariprasad Shenai 
2151e0a8b34aSHariprasad Shenai 	/* Default port and clock for debugging in case we can't reach
2152e0a8b34aSHariprasad Shenai 	 * firmware.
2153e0a8b34aSHariprasad Shenai 	 */
2154e0a8b34aSHariprasad Shenai 	adapter->params.nports = 1;
2155e0a8b34aSHariprasad Shenai 	adapter->params.vfres.pmask = 1;
2156e0a8b34aSHariprasad Shenai 	adapter->params.vpd.cclk = 50000;
2157e0a8b34aSHariprasad Shenai 
2158e0a8b34aSHariprasad Shenai 	adapter->params.chip = 0;
2159e0a8b34aSHariprasad Shenai 	switch (CHELSIO_PCI_ID_VER(adapter->pdev->device)) {
2160e0a8b34aSHariprasad Shenai 	case CHELSIO_T4:
2161e0a8b34aSHariprasad Shenai 		adapter->params.chip |= CHELSIO_CHIP_CODE(CHELSIO_T4, 0);
216241fc2e41SHariprasad Shenai 		adapter->params.arch.sge_fl_db = DBPRIO_F;
216341fc2e41SHariprasad Shenai 		adapter->params.arch.mps_tcam_size =
216441fc2e41SHariprasad Shenai 				NUM_MPS_CLS_SRAM_L_INSTANCES;
2165e0a8b34aSHariprasad Shenai 		break;
2166e0a8b34aSHariprasad Shenai 
2167e0a8b34aSHariprasad Shenai 	case CHELSIO_T5:
21680d804338SHariprasad Shenai 		chipid = REV_G(t4_read_reg(adapter, PL_VF_REV_A));
2169e0a8b34aSHariprasad Shenai 		adapter->params.chip |= CHELSIO_CHIP_CODE(CHELSIO_T5, chipid);
217041fc2e41SHariprasad Shenai 		adapter->params.arch.sge_fl_db = DBPRIO_F | DBTYPE_F;
217141fc2e41SHariprasad Shenai 		adapter->params.arch.mps_tcam_size =
217241fc2e41SHariprasad Shenai 				NUM_MPS_T5_CLS_SRAM_L_INSTANCES;
217341fc2e41SHariprasad Shenai 		break;
217441fc2e41SHariprasad Shenai 
217541fc2e41SHariprasad Shenai 	case CHELSIO_T6:
217641fc2e41SHariprasad Shenai 		chipid = REV_G(t4_read_reg(adapter, PL_VF_REV_A));
217741fc2e41SHariprasad Shenai 		adapter->params.chip |= CHELSIO_CHIP_CODE(CHELSIO_T6, chipid);
217841fc2e41SHariprasad Shenai 		adapter->params.arch.sge_fl_db = 0;
217941fc2e41SHariprasad Shenai 		adapter->params.arch.mps_tcam_size =
218041fc2e41SHariprasad Shenai 				NUM_MPS_T5_CLS_SRAM_L_INSTANCES;
2181e0a8b34aSHariprasad Shenai 		break;
2182e0a8b34aSHariprasad Shenai 	}
2183e0a8b34aSHariprasad Shenai 
2184e0a8b34aSHariprasad Shenai 	return 0;
2185e0a8b34aSHariprasad Shenai }
2186858aa65cSHariprasad Shenai 
2187858aa65cSHariprasad Shenai /**
2188858aa65cSHariprasad Shenai  *	t4vf_get_vf_mac_acl - Get the MAC address to be set to
2189858aa65cSHariprasad Shenai  *			      the VI of this VF.
2190858aa65cSHariprasad Shenai  *	@adapter: The adapter
2191e0cdac65SNirranjan Kirubaharan  *	@port: The port associated with vf
2192858aa65cSHariprasad Shenai  *	@naddr: the number of ACL MAC addresses returned in addr
2193858aa65cSHariprasad Shenai  *	@addr: Placeholder for MAC addresses
2194858aa65cSHariprasad Shenai  *
2195858aa65cSHariprasad Shenai  *	Find the MAC address to be set to the VF's VI. The requested MAC address
2196858aa65cSHariprasad Shenai  *	is from the host OS via callback in the PF driver.
2197858aa65cSHariprasad Shenai  */
t4vf_get_vf_mac_acl(struct adapter * adapter,unsigned int port,unsigned int * naddr,u8 * addr)2198e0cdac65SNirranjan Kirubaharan int t4vf_get_vf_mac_acl(struct adapter *adapter, unsigned int port,
2199858aa65cSHariprasad Shenai 			unsigned int *naddr, u8 *addr)
2200858aa65cSHariprasad Shenai {
2201858aa65cSHariprasad Shenai 	struct fw_acl_mac_cmd cmd;
2202858aa65cSHariprasad Shenai 	int ret;
2203858aa65cSHariprasad Shenai 
2204858aa65cSHariprasad Shenai 	memset(&cmd, 0, sizeof(cmd));
2205858aa65cSHariprasad Shenai 	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_ACL_MAC_CMD) |
2206858aa65cSHariprasad Shenai 				    FW_CMD_REQUEST_F |
2207858aa65cSHariprasad Shenai 				    FW_CMD_READ_F);
2208858aa65cSHariprasad Shenai 	cmd.en_to_len16 = cpu_to_be32((unsigned int)FW_LEN16(cmd));
2209858aa65cSHariprasad Shenai 	ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &cmd);
2210858aa65cSHariprasad Shenai 	if (ret)
2211858aa65cSHariprasad Shenai 		return ret;
2212858aa65cSHariprasad Shenai 
2213858aa65cSHariprasad Shenai 	if (cmd.nmac < *naddr)
2214858aa65cSHariprasad Shenai 		*naddr = cmd.nmac;
2215858aa65cSHariprasad Shenai 
2216e0cdac65SNirranjan Kirubaharan 	switch (port) {
2217858aa65cSHariprasad Shenai 	case 3:
2218858aa65cSHariprasad Shenai 		memcpy(addr, cmd.macaddr3, sizeof(cmd.macaddr3));
2219858aa65cSHariprasad Shenai 		break;
2220858aa65cSHariprasad Shenai 	case 2:
2221858aa65cSHariprasad Shenai 		memcpy(addr, cmd.macaddr2, sizeof(cmd.macaddr2));
2222858aa65cSHariprasad Shenai 		break;
2223858aa65cSHariprasad Shenai 	case 1:
2224858aa65cSHariprasad Shenai 		memcpy(addr, cmd.macaddr1, sizeof(cmd.macaddr1));
2225858aa65cSHariprasad Shenai 		break;
2226858aa65cSHariprasad Shenai 	case 0:
2227858aa65cSHariprasad Shenai 		memcpy(addr, cmd.macaddr0, sizeof(cmd.macaddr0));
2228858aa65cSHariprasad Shenai 		break;
2229858aa65cSHariprasad Shenai 	}
2230858aa65cSHariprasad Shenai 
2231858aa65cSHariprasad Shenai 	return ret;
2232858aa65cSHariprasad Shenai }
22339d5fd927SGanesh Goudar 
22349d5fd927SGanesh Goudar /**
22359d5fd927SGanesh Goudar  *	t4vf_get_vf_vlan_acl - Get the VLAN ID to be set to
22369d5fd927SGanesh Goudar  *                             the VI of this VF.
22379d5fd927SGanesh Goudar  *	@adapter: The adapter
22389d5fd927SGanesh Goudar  *
22399d5fd927SGanesh Goudar  *	Find the VLAN ID to be set to the VF's VI. The requested VLAN ID
22409d5fd927SGanesh Goudar  *	is from the host OS via callback in the PF driver.
22419d5fd927SGanesh Goudar  */
t4vf_get_vf_vlan_acl(struct adapter * adapter)22429d5fd927SGanesh Goudar int t4vf_get_vf_vlan_acl(struct adapter *adapter)
22439d5fd927SGanesh Goudar {
22449d5fd927SGanesh Goudar 	struct fw_acl_vlan_cmd cmd;
22459d5fd927SGanesh Goudar 	int vlan = 0;
22469d5fd927SGanesh Goudar 	int ret = 0;
22479d5fd927SGanesh Goudar 
22489d5fd927SGanesh Goudar 	cmd.op_to_vfn = htonl(FW_CMD_OP_V(FW_ACL_VLAN_CMD) |
22499d5fd927SGanesh Goudar 			      FW_CMD_REQUEST_F | FW_CMD_READ_F);
22509d5fd927SGanesh Goudar 
22519d5fd927SGanesh Goudar 	/* Note: Do not enable the ACL */
22529d5fd927SGanesh Goudar 	cmd.en_to_len16 = cpu_to_be32((unsigned int)FW_LEN16(cmd));
22539d5fd927SGanesh Goudar 
22549d5fd927SGanesh Goudar 	ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &cmd);
22559d5fd927SGanesh Goudar 
22569d5fd927SGanesh Goudar 	if (!ret)
22579d5fd927SGanesh Goudar 		vlan = be16_to_cpu(cmd.vlanid[0]);
22589d5fd927SGanesh Goudar 
22599d5fd927SGanesh Goudar 	return vlan;
22609d5fd927SGanesh Goudar }
2261