1 /*
2  * This file is part of the Chelsio T4 PCI-E SR-IOV Virtual Function Ethernet
3  * driver for Linux.
4  *
5  * Copyright (c) 2009-2010 Chelsio Communications, Inc. All rights reserved.
6  *
7  * This software is available to you under a choice of one of two
8  * licenses.  You may choose to be licensed under the terms of the GNU
9  * General Public License (GPL) Version 2, available from the file
10  * COPYING in the main directory of this source tree, or the
11  * OpenIB.org BSD license below:
12  *
13  *     Redistribution and use in source and binary forms, with or
14  *     without modification, are permitted provided that the following
15  *     conditions are met:
16  *
17  *      - Redistributions of source code must retain the above
18  *        copyright notice, this list of conditions and the following
19  *        disclaimer.
20  *
21  *      - Redistributions in binary form must reproduce the above
22  *        copyright notice, this list of conditions and the following
23  *        disclaimer in the documentation and/or other materials
24  *        provided with the distribution.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33  * SOFTWARE.
34  */
35 
36 #include <linux/pci.h>
37 
38 #include "t4vf_common.h"
39 #include "t4vf_defs.h"
40 
41 #include "../cxgb4/t4_regs.h"
42 #include "../cxgb4/t4_values.h"
43 #include "../cxgb4/t4fw_api.h"
44 
45 /*
46  * Wait for the device to become ready (signified by our "who am I" register
47  * returning a value other than all 1's).  Return an error if it doesn't
48  * become ready ...
49  */
50 int t4vf_wait_dev_ready(struct adapter *adapter)
51 {
52 	const u32 whoami = T4VF_PL_BASE_ADDR + PL_VF_WHOAMI;
53 	const u32 notready1 = 0xffffffff;
54 	const u32 notready2 = 0xeeeeeeee;
55 	u32 val;
56 
57 	val = t4_read_reg(adapter, whoami);
58 	if (val != notready1 && val != notready2)
59 		return 0;
60 	msleep(500);
61 	val = t4_read_reg(adapter, whoami);
62 	if (val != notready1 && val != notready2)
63 		return 0;
64 	else
65 		return -EIO;
66 }
67 
68 /*
69  * Get the reply to a mailbox command and store it in @rpl in big-endian order
70  * (since the firmware data structures are specified in a big-endian layout).
71  */
72 static void get_mbox_rpl(struct adapter *adapter, __be64 *rpl, int size,
73 			 u32 mbox_data)
74 {
75 	for ( ; size; size -= 8, mbox_data += 8)
76 		*rpl++ = cpu_to_be64(t4_read_reg64(adapter, mbox_data));
77 }
78 
79 /*
80  * Dump contents of mailbox with a leading tag.
81  */
82 static void dump_mbox(struct adapter *adapter, const char *tag, u32 mbox_data)
83 {
84 	dev_err(adapter->pdev_dev,
85 		"mbox %s: %llx %llx %llx %llx %llx %llx %llx %llx\n", tag,
86 		(unsigned long long)t4_read_reg64(adapter, mbox_data +  0),
87 		(unsigned long long)t4_read_reg64(adapter, mbox_data +  8),
88 		(unsigned long long)t4_read_reg64(adapter, mbox_data + 16),
89 		(unsigned long long)t4_read_reg64(adapter, mbox_data + 24),
90 		(unsigned long long)t4_read_reg64(adapter, mbox_data + 32),
91 		(unsigned long long)t4_read_reg64(adapter, mbox_data + 40),
92 		(unsigned long long)t4_read_reg64(adapter, mbox_data + 48),
93 		(unsigned long long)t4_read_reg64(adapter, mbox_data + 56));
94 }
95 
96 /**
97  *	t4vf_wr_mbox_core - send a command to FW through the mailbox
98  *	@adapter: the adapter
99  *	@cmd: the command to write
100  *	@size: command length in bytes
101  *	@rpl: where to optionally store the reply
102  *	@sleep_ok: if true we may sleep while awaiting command completion
103  *
104  *	Sends the given command to FW through the mailbox and waits for the
105  *	FW to execute the command.  If @rpl is not %NULL it is used to store
106  *	the FW's reply to the command.  The command and its optional reply
107  *	are of the same length.  FW can take up to 500 ms to respond.
108  *	@sleep_ok determines whether we may sleep while awaiting the response.
109  *	If sleeping is allowed we use progressive backoff otherwise we spin.
110  *
111  *	The return value is 0 on success or a negative errno on failure.  A
112  *	failure can happen either because we are not able to execute the
113  *	command or FW executes it but signals an error.  In the latter case
114  *	the return value is the error code indicated by FW (negated).
115  */
116 int t4vf_wr_mbox_core(struct adapter *adapter, const void *cmd, int size,
117 		      void *rpl, bool sleep_ok)
118 {
119 	static const int delay[] = {
120 		1, 1, 3, 5, 10, 10, 20, 50, 100
121 	};
122 
123 	u32 v, mbox_data;
124 	int i, ms, delay_idx;
125 	const __be64 *p;
126 	u32 mbox_ctl = T4VF_CIM_BASE_ADDR + CIM_VF_EXT_MAILBOX_CTRL;
127 
128 	/* In T6, mailbox size is changed to 128 bytes to avoid
129 	 * invalidating the entire prefetch buffer.
130 	 */
131 	if (CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5)
132 		mbox_data = T4VF_MBDATA_BASE_ADDR;
133 	else
134 		mbox_data = T6VF_MBDATA_BASE_ADDR;
135 
136 	/*
137 	 * Commands must be multiples of 16 bytes in length and may not be
138 	 * larger than the size of the Mailbox Data register array.
139 	 */
140 	if ((size % 16) != 0 ||
141 	    size > NUM_CIM_VF_MAILBOX_DATA_INSTANCES * 4)
142 		return -EINVAL;
143 
144 	/*
145 	 * Loop trying to get ownership of the mailbox.  Return an error
146 	 * if we can't gain ownership.
147 	 */
148 	v = MBOWNER_G(t4_read_reg(adapter, mbox_ctl));
149 	for (i = 0; v == MBOX_OWNER_NONE && i < 3; i++)
150 		v = MBOWNER_G(t4_read_reg(adapter, mbox_ctl));
151 	if (v != MBOX_OWNER_DRV)
152 		return v == MBOX_OWNER_FW ? -EBUSY : -ETIMEDOUT;
153 
154 	/*
155 	 * Write the command array into the Mailbox Data register array and
156 	 * transfer ownership of the mailbox to the firmware.
157 	 *
158 	 * For the VFs, the Mailbox Data "registers" are actually backed by
159 	 * T4's "MA" interface rather than PL Registers (as is the case for
160 	 * the PFs).  Because these are in different coherency domains, the
161 	 * write to the VF's PL-register-backed Mailbox Control can race in
162 	 * front of the writes to the MA-backed VF Mailbox Data "registers".
163 	 * So we need to do a read-back on at least one byte of the VF Mailbox
164 	 * Data registers before doing the write to the VF Mailbox Control
165 	 * register.
166 	 */
167 	for (i = 0, p = cmd; i < size; i += 8)
168 		t4_write_reg64(adapter, mbox_data + i, be64_to_cpu(*p++));
169 	t4_read_reg(adapter, mbox_data);         /* flush write */
170 
171 	t4_write_reg(adapter, mbox_ctl,
172 		     MBMSGVALID_F | MBOWNER_V(MBOX_OWNER_FW));
173 	t4_read_reg(adapter, mbox_ctl);          /* flush write */
174 
175 	/*
176 	 * Spin waiting for firmware to acknowledge processing our command.
177 	 */
178 	delay_idx = 0;
179 	ms = delay[0];
180 
181 	for (i = 0; i < FW_CMD_MAX_TIMEOUT; i += ms) {
182 		if (sleep_ok) {
183 			ms = delay[delay_idx];
184 			if (delay_idx < ARRAY_SIZE(delay) - 1)
185 				delay_idx++;
186 			msleep(ms);
187 		} else
188 			mdelay(ms);
189 
190 		/*
191 		 * If we're the owner, see if this is the reply we wanted.
192 		 */
193 		v = t4_read_reg(adapter, mbox_ctl);
194 		if (MBOWNER_G(v) == MBOX_OWNER_DRV) {
195 			/*
196 			 * If the Message Valid bit isn't on, revoke ownership
197 			 * of the mailbox and continue waiting for our reply.
198 			 */
199 			if ((v & MBMSGVALID_F) == 0) {
200 				t4_write_reg(adapter, mbox_ctl,
201 					     MBOWNER_V(MBOX_OWNER_NONE));
202 				continue;
203 			}
204 
205 			/*
206 			 * We now have our reply.  Extract the command return
207 			 * value, copy the reply back to our caller's buffer
208 			 * (if specified) and revoke ownership of the mailbox.
209 			 * We return the (negated) firmware command return
210 			 * code (this depends on FW_SUCCESS == 0).
211 			 */
212 
213 			/* return value in low-order little-endian word */
214 			v = t4_read_reg(adapter, mbox_data);
215 			if (FW_CMD_RETVAL_G(v))
216 				dump_mbox(adapter, "FW Error", mbox_data);
217 
218 			if (rpl) {
219 				/* request bit in high-order BE word */
220 				WARN_ON((be32_to_cpu(*(const __be32 *)cmd)
221 					 & FW_CMD_REQUEST_F) == 0);
222 				get_mbox_rpl(adapter, rpl, size, mbox_data);
223 				WARN_ON((be32_to_cpu(*(__be32 *)rpl)
224 					 & FW_CMD_REQUEST_F) != 0);
225 			}
226 			t4_write_reg(adapter, mbox_ctl,
227 				     MBOWNER_V(MBOX_OWNER_NONE));
228 			return -FW_CMD_RETVAL_G(v);
229 		}
230 	}
231 
232 	/*
233 	 * We timed out.  Return the error ...
234 	 */
235 	dump_mbox(adapter, "FW Timeout", mbox_data);
236 	return -ETIMEDOUT;
237 }
238 
239 /**
240  *	hash_mac_addr - return the hash value of a MAC address
241  *	@addr: the 48-bit Ethernet MAC address
242  *
243  *	Hashes a MAC address according to the hash function used by hardware
244  *	inexact (hash) address matching.
245  */
246 static int hash_mac_addr(const u8 *addr)
247 {
248 	u32 a = ((u32)addr[0] << 16) | ((u32)addr[1] << 8) | addr[2];
249 	u32 b = ((u32)addr[3] << 16) | ((u32)addr[4] << 8) | addr[5];
250 	a ^= b;
251 	a ^= (a >> 12);
252 	a ^= (a >> 6);
253 	return a & 0x3f;
254 }
255 
256 #define ADVERT_MASK (FW_PORT_CAP_SPEED_100M | FW_PORT_CAP_SPEED_1G |\
257 		     FW_PORT_CAP_SPEED_10G | FW_PORT_CAP_SPEED_40G | \
258 		     FW_PORT_CAP_SPEED_100G | FW_PORT_CAP_ANEG)
259 
260 /**
261  *	init_link_config - initialize a link's SW state
262  *	@lc: structure holding the link state
263  *	@caps: link capabilities
264  *
265  *	Initializes the SW state maintained for each link, including the link's
266  *	capabilities and default speed/flow-control/autonegotiation settings.
267  */
268 static void init_link_config(struct link_config *lc, unsigned int caps)
269 {
270 	lc->supported = caps;
271 	lc->requested_speed = 0;
272 	lc->speed = 0;
273 	lc->requested_fc = lc->fc = PAUSE_RX | PAUSE_TX;
274 	if (lc->supported & FW_PORT_CAP_ANEG) {
275 		lc->advertising = lc->supported & ADVERT_MASK;
276 		lc->autoneg = AUTONEG_ENABLE;
277 		lc->requested_fc |= PAUSE_AUTONEG;
278 	} else {
279 		lc->advertising = 0;
280 		lc->autoneg = AUTONEG_DISABLE;
281 	}
282 }
283 
284 /**
285  *	t4vf_port_init - initialize port hardware/software state
286  *	@adapter: the adapter
287  *	@pidx: the adapter port index
288  */
289 int t4vf_port_init(struct adapter *adapter, int pidx)
290 {
291 	struct port_info *pi = adap2pinfo(adapter, pidx);
292 	struct fw_vi_cmd vi_cmd, vi_rpl;
293 	struct fw_port_cmd port_cmd, port_rpl;
294 	int v;
295 
296 	/*
297 	 * Execute a VI Read command to get our Virtual Interface information
298 	 * like MAC address, etc.
299 	 */
300 	memset(&vi_cmd, 0, sizeof(vi_cmd));
301 	vi_cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_VI_CMD) |
302 				       FW_CMD_REQUEST_F |
303 				       FW_CMD_READ_F);
304 	vi_cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(vi_cmd));
305 	vi_cmd.type_viid = cpu_to_be16(FW_VI_CMD_VIID_V(pi->viid));
306 	v = t4vf_wr_mbox(adapter, &vi_cmd, sizeof(vi_cmd), &vi_rpl);
307 	if (v)
308 		return v;
309 
310 	BUG_ON(pi->port_id != FW_VI_CMD_PORTID_G(vi_rpl.portid_pkd));
311 	pi->rss_size = FW_VI_CMD_RSSSIZE_G(be16_to_cpu(vi_rpl.rsssize_pkd));
312 	t4_os_set_hw_addr(adapter, pidx, vi_rpl.mac);
313 
314 	/*
315 	 * If we don't have read access to our port information, we're done
316 	 * now.  Otherwise, execute a PORT Read command to get it ...
317 	 */
318 	if (!(adapter->params.vfres.r_caps & FW_CMD_CAP_PORT))
319 		return 0;
320 
321 	memset(&port_cmd, 0, sizeof(port_cmd));
322 	port_cmd.op_to_portid = cpu_to_be32(FW_CMD_OP_V(FW_PORT_CMD) |
323 					    FW_CMD_REQUEST_F |
324 					    FW_CMD_READ_F |
325 					    FW_PORT_CMD_PORTID_V(pi->port_id));
326 	port_cmd.action_to_len16 =
327 		cpu_to_be32(FW_PORT_CMD_ACTION_V(FW_PORT_ACTION_GET_PORT_INFO) |
328 			    FW_LEN16(port_cmd));
329 	v = t4vf_wr_mbox(adapter, &port_cmd, sizeof(port_cmd), &port_rpl);
330 	if (v)
331 		return v;
332 
333 	v = be32_to_cpu(port_rpl.u.info.lstatus_to_modtype);
334 	pi->mdio_addr = (v & FW_PORT_CMD_MDIOCAP_F) ?
335 			FW_PORT_CMD_MDIOADDR_G(v) : -1;
336 	pi->port_type = FW_PORT_CMD_PTYPE_G(v);
337 	pi->mod_type = FW_PORT_MOD_TYPE_NA;
338 
339 	init_link_config(&pi->link_cfg, be16_to_cpu(port_rpl.u.info.pcap));
340 
341 	return 0;
342 }
343 
344 /**
345  *      t4vf_fw_reset - issue a reset to FW
346  *      @adapter: the adapter
347  *
348  *	Issues a reset command to FW.  For a Physical Function this would
349  *	result in the Firmware resetting all of its state.  For a Virtual
350  *	Function this just resets the state associated with the VF.
351  */
352 int t4vf_fw_reset(struct adapter *adapter)
353 {
354 	struct fw_reset_cmd cmd;
355 
356 	memset(&cmd, 0, sizeof(cmd));
357 	cmd.op_to_write = cpu_to_be32(FW_CMD_OP_V(FW_RESET_CMD) |
358 				      FW_CMD_WRITE_F);
359 	cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
360 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
361 }
362 
363 /**
364  *	t4vf_query_params - query FW or device parameters
365  *	@adapter: the adapter
366  *	@nparams: the number of parameters
367  *	@params: the parameter names
368  *	@vals: the parameter values
369  *
370  *	Reads the values of firmware or device parameters.  Up to 7 parameters
371  *	can be queried at once.
372  */
373 static int t4vf_query_params(struct adapter *adapter, unsigned int nparams,
374 			     const u32 *params, u32 *vals)
375 {
376 	int i, ret;
377 	struct fw_params_cmd cmd, rpl;
378 	struct fw_params_param *p;
379 	size_t len16;
380 
381 	if (nparams > 7)
382 		return -EINVAL;
383 
384 	memset(&cmd, 0, sizeof(cmd));
385 	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_PARAMS_CMD) |
386 				    FW_CMD_REQUEST_F |
387 				    FW_CMD_READ_F);
388 	len16 = DIV_ROUND_UP(offsetof(struct fw_params_cmd,
389 				      param[nparams].mnem), 16);
390 	cmd.retval_len16 = cpu_to_be32(FW_CMD_LEN16_V(len16));
391 	for (i = 0, p = &cmd.param[0]; i < nparams; i++, p++)
392 		p->mnem = htonl(*params++);
393 
394 	ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
395 	if (ret == 0)
396 		for (i = 0, p = &rpl.param[0]; i < nparams; i++, p++)
397 			*vals++ = be32_to_cpu(p->val);
398 	return ret;
399 }
400 
401 /**
402  *	t4vf_set_params - sets FW or device parameters
403  *	@adapter: the adapter
404  *	@nparams: the number of parameters
405  *	@params: the parameter names
406  *	@vals: the parameter values
407  *
408  *	Sets the values of firmware or device parameters.  Up to 7 parameters
409  *	can be specified at once.
410  */
411 int t4vf_set_params(struct adapter *adapter, unsigned int nparams,
412 		    const u32 *params, const u32 *vals)
413 {
414 	int i;
415 	struct fw_params_cmd cmd;
416 	struct fw_params_param *p;
417 	size_t len16;
418 
419 	if (nparams > 7)
420 		return -EINVAL;
421 
422 	memset(&cmd, 0, sizeof(cmd));
423 	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_PARAMS_CMD) |
424 				    FW_CMD_REQUEST_F |
425 				    FW_CMD_WRITE_F);
426 	len16 = DIV_ROUND_UP(offsetof(struct fw_params_cmd,
427 				      param[nparams]), 16);
428 	cmd.retval_len16 = cpu_to_be32(FW_CMD_LEN16_V(len16));
429 	for (i = 0, p = &cmd.param[0]; i < nparams; i++, p++) {
430 		p->mnem = cpu_to_be32(*params++);
431 		p->val = cpu_to_be32(*vals++);
432 	}
433 
434 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
435 }
436 
437 /**
438  *	t4vf_bar2_sge_qregs - return BAR2 SGE Queue register information
439  *	@adapter: the adapter
440  *	@qid: the Queue ID
441  *	@qtype: the Ingress or Egress type for @qid
442  *	@pbar2_qoffset: BAR2 Queue Offset
443  *	@pbar2_qid: BAR2 Queue ID or 0 for Queue ID inferred SGE Queues
444  *
445  *	Returns the BAR2 SGE Queue Registers information associated with the
446  *	indicated Absolute Queue ID.  These are passed back in return value
447  *	pointers.  @qtype should be T4_BAR2_QTYPE_EGRESS for Egress Queue
448  *	and T4_BAR2_QTYPE_INGRESS for Ingress Queues.
449  *
450  *	This may return an error which indicates that BAR2 SGE Queue
451  *	registers aren't available.  If an error is not returned, then the
452  *	following values are returned:
453  *
454  *	  *@pbar2_qoffset: the BAR2 Offset of the @qid Registers
455  *	  *@pbar2_qid: the BAR2 SGE Queue ID or 0 of @qid
456  *
457  *	If the returned BAR2 Queue ID is 0, then BAR2 SGE registers which
458  *	require the "Inferred Queue ID" ability may be used.  E.g. the
459  *	Write Combining Doorbell Buffer. If the BAR2 Queue ID is not 0,
460  *	then these "Inferred Queue ID" register may not be used.
461  */
462 int t4vf_bar2_sge_qregs(struct adapter *adapter,
463 			unsigned int qid,
464 			enum t4_bar2_qtype qtype,
465 			u64 *pbar2_qoffset,
466 			unsigned int *pbar2_qid)
467 {
468 	unsigned int page_shift, page_size, qpp_shift, qpp_mask;
469 	u64 bar2_page_offset, bar2_qoffset;
470 	unsigned int bar2_qid, bar2_qid_offset, bar2_qinferred;
471 
472 	/* T4 doesn't support BAR2 SGE Queue registers.
473 	 */
474 	if (is_t4(adapter->params.chip))
475 		return -EINVAL;
476 
477 	/* Get our SGE Page Size parameters.
478 	 */
479 	page_shift = adapter->params.sge.sge_vf_hps + 10;
480 	page_size = 1 << page_shift;
481 
482 	/* Get the right Queues per Page parameters for our Queue.
483 	 */
484 	qpp_shift = (qtype == T4_BAR2_QTYPE_EGRESS
485 		     ? adapter->params.sge.sge_vf_eq_qpp
486 		     : adapter->params.sge.sge_vf_iq_qpp);
487 	qpp_mask = (1 << qpp_shift) - 1;
488 
489 	/* Calculate the basics of the BAR2 SGE Queue register area:
490 	 *  o The BAR2 page the Queue registers will be in.
491 	 *  o The BAR2 Queue ID.
492 	 *  o The BAR2 Queue ID Offset into the BAR2 page.
493 	 */
494 	bar2_page_offset = ((u64)(qid >> qpp_shift) << page_shift);
495 	bar2_qid = qid & qpp_mask;
496 	bar2_qid_offset = bar2_qid * SGE_UDB_SIZE;
497 
498 	/* If the BAR2 Queue ID Offset is less than the Page Size, then the
499 	 * hardware will infer the Absolute Queue ID simply from the writes to
500 	 * the BAR2 Queue ID Offset within the BAR2 Page (and we need to use a
501 	 * BAR2 Queue ID of 0 for those writes).  Otherwise, we'll simply
502 	 * write to the first BAR2 SGE Queue Area within the BAR2 Page with
503 	 * the BAR2 Queue ID and the hardware will infer the Absolute Queue ID
504 	 * from the BAR2 Page and BAR2 Queue ID.
505 	 *
506 	 * One important censequence of this is that some BAR2 SGE registers
507 	 * have a "Queue ID" field and we can write the BAR2 SGE Queue ID
508 	 * there.  But other registers synthesize the SGE Queue ID purely
509 	 * from the writes to the registers -- the Write Combined Doorbell
510 	 * Buffer is a good example.  These BAR2 SGE Registers are only
511 	 * available for those BAR2 SGE Register areas where the SGE Absolute
512 	 * Queue ID can be inferred from simple writes.
513 	 */
514 	bar2_qoffset = bar2_page_offset;
515 	bar2_qinferred = (bar2_qid_offset < page_size);
516 	if (bar2_qinferred) {
517 		bar2_qoffset += bar2_qid_offset;
518 		bar2_qid = 0;
519 	}
520 
521 	*pbar2_qoffset = bar2_qoffset;
522 	*pbar2_qid = bar2_qid;
523 	return 0;
524 }
525 
526 /**
527  *	t4vf_get_sge_params - retrieve adapter Scatter gather Engine parameters
528  *	@adapter: the adapter
529  *
530  *	Retrieves various core SGE parameters in the form of hardware SGE
531  *	register values.  The caller is responsible for decoding these as
532  *	needed.  The SGE parameters are stored in @adapter->params.sge.
533  */
534 int t4vf_get_sge_params(struct adapter *adapter)
535 {
536 	struct sge_params *sge_params = &adapter->params.sge;
537 	u32 params[7], vals[7];
538 	int v;
539 
540 	params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
541 		     FW_PARAMS_PARAM_XYZ_V(SGE_CONTROL_A));
542 	params[1] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
543 		     FW_PARAMS_PARAM_XYZ_V(SGE_HOST_PAGE_SIZE_A));
544 	params[2] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
545 		     FW_PARAMS_PARAM_XYZ_V(SGE_FL_BUFFER_SIZE0_A));
546 	params[3] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
547 		     FW_PARAMS_PARAM_XYZ_V(SGE_FL_BUFFER_SIZE1_A));
548 	params[4] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
549 		     FW_PARAMS_PARAM_XYZ_V(SGE_TIMER_VALUE_0_AND_1_A));
550 	params[5] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
551 		     FW_PARAMS_PARAM_XYZ_V(SGE_TIMER_VALUE_2_AND_3_A));
552 	params[6] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
553 		     FW_PARAMS_PARAM_XYZ_V(SGE_TIMER_VALUE_4_AND_5_A));
554 	v = t4vf_query_params(adapter, 7, params, vals);
555 	if (v)
556 		return v;
557 	sge_params->sge_control = vals[0];
558 	sge_params->sge_host_page_size = vals[1];
559 	sge_params->sge_fl_buffer_size[0] = vals[2];
560 	sge_params->sge_fl_buffer_size[1] = vals[3];
561 	sge_params->sge_timer_value_0_and_1 = vals[4];
562 	sge_params->sge_timer_value_2_and_3 = vals[5];
563 	sge_params->sge_timer_value_4_and_5 = vals[6];
564 
565 	/* T4 uses a single control field to specify both the PCIe Padding and
566 	 * Packing Boundary.  T5 introduced the ability to specify these
567 	 * separately with the Padding Boundary in SGE_CONTROL and and Packing
568 	 * Boundary in SGE_CONTROL2.  So for T5 and later we need to grab
569 	 * SGE_CONTROL in order to determine how ingress packet data will be
570 	 * laid out in Packed Buffer Mode.  Unfortunately, older versions of
571 	 * the firmware won't let us retrieve SGE_CONTROL2 so if we get a
572 	 * failure grabbing it we throw an error since we can't figure out the
573 	 * right value.
574 	 */
575 	if (!is_t4(adapter->params.chip)) {
576 		params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
577 			     FW_PARAMS_PARAM_XYZ_V(SGE_CONTROL2_A));
578 		v = t4vf_query_params(adapter, 1, params, vals);
579 		if (v != FW_SUCCESS) {
580 			dev_err(adapter->pdev_dev,
581 				"Unable to get SGE Control2; "
582 				"probably old firmware.\n");
583 			return v;
584 		}
585 		sge_params->sge_control2 = vals[0];
586 	}
587 
588 	params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
589 		     FW_PARAMS_PARAM_XYZ_V(SGE_INGRESS_RX_THRESHOLD_A));
590 	params[1] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
591 		     FW_PARAMS_PARAM_XYZ_V(SGE_CONM_CTRL_A));
592 	v = t4vf_query_params(adapter, 2, params, vals);
593 	if (v)
594 		return v;
595 	sge_params->sge_ingress_rx_threshold = vals[0];
596 	sge_params->sge_congestion_control = vals[1];
597 
598 	/* For T5 and later we want to use the new BAR2 Doorbells.
599 	 * Unfortunately, older firmware didn't allow the this register to be
600 	 * read.
601 	 */
602 	if (!is_t4(adapter->params.chip)) {
603 		u32 whoami;
604 		unsigned int pf, s_hps, s_qpp;
605 
606 		params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
607 			     FW_PARAMS_PARAM_XYZ_V(
608 				     SGE_EGRESS_QUEUES_PER_PAGE_VF_A));
609 		params[1] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_REG) |
610 			     FW_PARAMS_PARAM_XYZ_V(
611 				     SGE_INGRESS_QUEUES_PER_PAGE_VF_A));
612 		v = t4vf_query_params(adapter, 2, params, vals);
613 		if (v != FW_SUCCESS) {
614 			dev_warn(adapter->pdev_dev,
615 				 "Unable to get VF SGE Queues/Page; "
616 				 "probably old firmware.\n");
617 			return v;
618 		}
619 		sge_params->sge_egress_queues_per_page = vals[0];
620 		sge_params->sge_ingress_queues_per_page = vals[1];
621 
622 		/* We need the Queues/Page for our VF.  This is based on the
623 		 * PF from which we're instantiated and is indexed in the
624 		 * register we just read. Do it once here so other code in
625 		 * the driver can just use it.
626 		 */
627 		whoami = t4_read_reg(adapter,
628 				     T4VF_PL_BASE_ADDR + PL_VF_WHOAMI_A);
629 		pf = CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5 ?
630 			SOURCEPF_G(whoami) : T6_SOURCEPF_G(whoami);
631 
632 		s_hps = (HOSTPAGESIZEPF0_S +
633 			 (HOSTPAGESIZEPF1_S - HOSTPAGESIZEPF0_S) * pf);
634 		sge_params->sge_vf_hps =
635 			((sge_params->sge_host_page_size >> s_hps)
636 			 & HOSTPAGESIZEPF0_M);
637 
638 		s_qpp = (QUEUESPERPAGEPF0_S +
639 			 (QUEUESPERPAGEPF1_S - QUEUESPERPAGEPF0_S) * pf);
640 		sge_params->sge_vf_eq_qpp =
641 			((sge_params->sge_egress_queues_per_page >> s_qpp)
642 			 & QUEUESPERPAGEPF0_M);
643 		sge_params->sge_vf_iq_qpp =
644 			((sge_params->sge_ingress_queues_per_page >> s_qpp)
645 			 & QUEUESPERPAGEPF0_M);
646 	}
647 
648 	return 0;
649 }
650 
651 /**
652  *	t4vf_get_vpd_params - retrieve device VPD paremeters
653  *	@adapter: the adapter
654  *
655  *	Retrives various device Vital Product Data parameters.  The parameters
656  *	are stored in @adapter->params.vpd.
657  */
658 int t4vf_get_vpd_params(struct adapter *adapter)
659 {
660 	struct vpd_params *vpd_params = &adapter->params.vpd;
661 	u32 params[7], vals[7];
662 	int v;
663 
664 	params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
665 		     FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CCLK));
666 	v = t4vf_query_params(adapter, 1, params, vals);
667 	if (v)
668 		return v;
669 	vpd_params->cclk = vals[0];
670 
671 	return 0;
672 }
673 
674 /**
675  *	t4vf_get_dev_params - retrieve device paremeters
676  *	@adapter: the adapter
677  *
678  *	Retrives various device parameters.  The parameters are stored in
679  *	@adapter->params.dev.
680  */
681 int t4vf_get_dev_params(struct adapter *adapter)
682 {
683 	struct dev_params *dev_params = &adapter->params.dev;
684 	u32 params[7], vals[7];
685 	int v;
686 
687 	params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
688 		     FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_FWREV));
689 	params[1] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
690 		     FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_TPREV));
691 	v = t4vf_query_params(adapter, 2, params, vals);
692 	if (v)
693 		return v;
694 	dev_params->fwrev = vals[0];
695 	dev_params->tprev = vals[1];
696 
697 	return 0;
698 }
699 
700 /**
701  *	t4vf_get_rss_glb_config - retrieve adapter RSS Global Configuration
702  *	@adapter: the adapter
703  *
704  *	Retrieves global RSS mode and parameters with which we have to live
705  *	and stores them in the @adapter's RSS parameters.
706  */
707 int t4vf_get_rss_glb_config(struct adapter *adapter)
708 {
709 	struct rss_params *rss = &adapter->params.rss;
710 	struct fw_rss_glb_config_cmd cmd, rpl;
711 	int v;
712 
713 	/*
714 	 * Execute an RSS Global Configuration read command to retrieve
715 	 * our RSS configuration.
716 	 */
717 	memset(&cmd, 0, sizeof(cmd));
718 	cmd.op_to_write = cpu_to_be32(FW_CMD_OP_V(FW_RSS_GLB_CONFIG_CMD) |
719 				      FW_CMD_REQUEST_F |
720 				      FW_CMD_READ_F);
721 	cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
722 	v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
723 	if (v)
724 		return v;
725 
726 	/*
727 	 * Transate the big-endian RSS Global Configuration into our
728 	 * cpu-endian format based on the RSS mode.  We also do first level
729 	 * filtering at this point to weed out modes which don't support
730 	 * VF Drivers ...
731 	 */
732 	rss->mode = FW_RSS_GLB_CONFIG_CMD_MODE_G(
733 			be32_to_cpu(rpl.u.manual.mode_pkd));
734 	switch (rss->mode) {
735 	case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
736 		u32 word = be32_to_cpu(
737 				rpl.u.basicvirtual.synmapen_to_hashtoeplitz);
738 
739 		rss->u.basicvirtual.synmapen =
740 			((word & FW_RSS_GLB_CONFIG_CMD_SYNMAPEN_F) != 0);
741 		rss->u.basicvirtual.syn4tupenipv6 =
742 			((word & FW_RSS_GLB_CONFIG_CMD_SYN4TUPENIPV6_F) != 0);
743 		rss->u.basicvirtual.syn2tupenipv6 =
744 			((word & FW_RSS_GLB_CONFIG_CMD_SYN2TUPENIPV6_F) != 0);
745 		rss->u.basicvirtual.syn4tupenipv4 =
746 			((word & FW_RSS_GLB_CONFIG_CMD_SYN4TUPENIPV4_F) != 0);
747 		rss->u.basicvirtual.syn2tupenipv4 =
748 			((word & FW_RSS_GLB_CONFIG_CMD_SYN2TUPENIPV4_F) != 0);
749 
750 		rss->u.basicvirtual.ofdmapen =
751 			((word & FW_RSS_GLB_CONFIG_CMD_OFDMAPEN_F) != 0);
752 
753 		rss->u.basicvirtual.tnlmapen =
754 			((word & FW_RSS_GLB_CONFIG_CMD_TNLMAPEN_F) != 0);
755 		rss->u.basicvirtual.tnlalllookup =
756 			((word  & FW_RSS_GLB_CONFIG_CMD_TNLALLLKP_F) != 0);
757 
758 		rss->u.basicvirtual.hashtoeplitz =
759 			((word & FW_RSS_GLB_CONFIG_CMD_HASHTOEPLITZ_F) != 0);
760 
761 		/* we need at least Tunnel Map Enable to be set */
762 		if (!rss->u.basicvirtual.tnlmapen)
763 			return -EINVAL;
764 		break;
765 	}
766 
767 	default:
768 		/* all unknown/unsupported RSS modes result in an error */
769 		return -EINVAL;
770 	}
771 
772 	return 0;
773 }
774 
775 /**
776  *	t4vf_get_vfres - retrieve VF resource limits
777  *	@adapter: the adapter
778  *
779  *	Retrieves configured resource limits and capabilities for a virtual
780  *	function.  The results are stored in @adapter->vfres.
781  */
782 int t4vf_get_vfres(struct adapter *adapter)
783 {
784 	struct vf_resources *vfres = &adapter->params.vfres;
785 	struct fw_pfvf_cmd cmd, rpl;
786 	int v;
787 	u32 word;
788 
789 	/*
790 	 * Execute PFVF Read command to get VF resource limits; bail out early
791 	 * with error on command failure.
792 	 */
793 	memset(&cmd, 0, sizeof(cmd));
794 	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_PFVF_CMD) |
795 				    FW_CMD_REQUEST_F |
796 				    FW_CMD_READ_F);
797 	cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
798 	v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
799 	if (v)
800 		return v;
801 
802 	/*
803 	 * Extract VF resource limits and return success.
804 	 */
805 	word = be32_to_cpu(rpl.niqflint_niq);
806 	vfres->niqflint = FW_PFVF_CMD_NIQFLINT_G(word);
807 	vfres->niq = FW_PFVF_CMD_NIQ_G(word);
808 
809 	word = be32_to_cpu(rpl.type_to_neq);
810 	vfres->neq = FW_PFVF_CMD_NEQ_G(word);
811 	vfres->pmask = FW_PFVF_CMD_PMASK_G(word);
812 
813 	word = be32_to_cpu(rpl.tc_to_nexactf);
814 	vfres->tc = FW_PFVF_CMD_TC_G(word);
815 	vfres->nvi = FW_PFVF_CMD_NVI_G(word);
816 	vfres->nexactf = FW_PFVF_CMD_NEXACTF_G(word);
817 
818 	word = be32_to_cpu(rpl.r_caps_to_nethctrl);
819 	vfres->r_caps = FW_PFVF_CMD_R_CAPS_G(word);
820 	vfres->wx_caps = FW_PFVF_CMD_WX_CAPS_G(word);
821 	vfres->nethctrl = FW_PFVF_CMD_NETHCTRL_G(word);
822 
823 	return 0;
824 }
825 
826 /**
827  *	t4vf_read_rss_vi_config - read a VI's RSS configuration
828  *	@adapter: the adapter
829  *	@viid: Virtual Interface ID
830  *	@config: pointer to host-native VI RSS Configuration buffer
831  *
832  *	Reads the Virtual Interface's RSS configuration information and
833  *	translates it into CPU-native format.
834  */
835 int t4vf_read_rss_vi_config(struct adapter *adapter, unsigned int viid,
836 			    union rss_vi_config *config)
837 {
838 	struct fw_rss_vi_config_cmd cmd, rpl;
839 	int v;
840 
841 	memset(&cmd, 0, sizeof(cmd));
842 	cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_RSS_VI_CONFIG_CMD) |
843 				     FW_CMD_REQUEST_F |
844 				     FW_CMD_READ_F |
845 				     FW_RSS_VI_CONFIG_CMD_VIID(viid));
846 	cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
847 	v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
848 	if (v)
849 		return v;
850 
851 	switch (adapter->params.rss.mode) {
852 	case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
853 		u32 word = be32_to_cpu(rpl.u.basicvirtual.defaultq_to_udpen);
854 
855 		config->basicvirtual.ip6fourtupen =
856 			((word & FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F) != 0);
857 		config->basicvirtual.ip6twotupen =
858 			((word & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F) != 0);
859 		config->basicvirtual.ip4fourtupen =
860 			((word & FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F) != 0);
861 		config->basicvirtual.ip4twotupen =
862 			((word & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F) != 0);
863 		config->basicvirtual.udpen =
864 			((word & FW_RSS_VI_CONFIG_CMD_UDPEN_F) != 0);
865 		config->basicvirtual.defaultq =
866 			FW_RSS_VI_CONFIG_CMD_DEFAULTQ_G(word);
867 		break;
868 	}
869 
870 	default:
871 		return -EINVAL;
872 	}
873 
874 	return 0;
875 }
876 
877 /**
878  *	t4vf_write_rss_vi_config - write a VI's RSS configuration
879  *	@adapter: the adapter
880  *	@viid: Virtual Interface ID
881  *	@config: pointer to host-native VI RSS Configuration buffer
882  *
883  *	Write the Virtual Interface's RSS configuration information
884  *	(translating it into firmware-native format before writing).
885  */
886 int t4vf_write_rss_vi_config(struct adapter *adapter, unsigned int viid,
887 			     union rss_vi_config *config)
888 {
889 	struct fw_rss_vi_config_cmd cmd, rpl;
890 
891 	memset(&cmd, 0, sizeof(cmd));
892 	cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_RSS_VI_CONFIG_CMD) |
893 				     FW_CMD_REQUEST_F |
894 				     FW_CMD_WRITE_F |
895 				     FW_RSS_VI_CONFIG_CMD_VIID(viid));
896 	cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
897 	switch (adapter->params.rss.mode) {
898 	case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
899 		u32 word = 0;
900 
901 		if (config->basicvirtual.ip6fourtupen)
902 			word |= FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F;
903 		if (config->basicvirtual.ip6twotupen)
904 			word |= FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F;
905 		if (config->basicvirtual.ip4fourtupen)
906 			word |= FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F;
907 		if (config->basicvirtual.ip4twotupen)
908 			word |= FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F;
909 		if (config->basicvirtual.udpen)
910 			word |= FW_RSS_VI_CONFIG_CMD_UDPEN_F;
911 		word |= FW_RSS_VI_CONFIG_CMD_DEFAULTQ_V(
912 				config->basicvirtual.defaultq);
913 		cmd.u.basicvirtual.defaultq_to_udpen = cpu_to_be32(word);
914 		break;
915 	}
916 
917 	default:
918 		return -EINVAL;
919 	}
920 
921 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
922 }
923 
924 /**
925  *	t4vf_config_rss_range - configure a portion of the RSS mapping table
926  *	@adapter: the adapter
927  *	@viid: Virtual Interface of RSS Table Slice
928  *	@start: starting entry in the table to write
929  *	@n: how many table entries to write
930  *	@rspq: values for the "Response Queue" (Ingress Queue) lookup table
931  *	@nrspq: number of values in @rspq
932  *
933  *	Programs the selected part of the VI's RSS mapping table with the
934  *	provided values.  If @nrspq < @n the supplied values are used repeatedly
935  *	until the full table range is populated.
936  *
937  *	The caller must ensure the values in @rspq are in the range 0..1023.
938  */
939 int t4vf_config_rss_range(struct adapter *adapter, unsigned int viid,
940 			  int start, int n, const u16 *rspq, int nrspq)
941 {
942 	const u16 *rsp = rspq;
943 	const u16 *rsp_end = rspq+nrspq;
944 	struct fw_rss_ind_tbl_cmd cmd;
945 
946 	/*
947 	 * Initialize firmware command template to write the RSS table.
948 	 */
949 	memset(&cmd, 0, sizeof(cmd));
950 	cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_RSS_IND_TBL_CMD) |
951 				     FW_CMD_REQUEST_F |
952 				     FW_CMD_WRITE_F |
953 				     FW_RSS_IND_TBL_CMD_VIID_V(viid));
954 	cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
955 
956 	/*
957 	 * Each firmware RSS command can accommodate up to 32 RSS Ingress
958 	 * Queue Identifiers.  These Ingress Queue IDs are packed three to
959 	 * a 32-bit word as 10-bit values with the upper remaining 2 bits
960 	 * reserved.
961 	 */
962 	while (n > 0) {
963 		__be32 *qp = &cmd.iq0_to_iq2;
964 		int nq = min(n, 32);
965 		int ret;
966 
967 		/*
968 		 * Set up the firmware RSS command header to send the next
969 		 * "nq" Ingress Queue IDs to the firmware.
970 		 */
971 		cmd.niqid = cpu_to_be16(nq);
972 		cmd.startidx = cpu_to_be16(start);
973 
974 		/*
975 		 * "nq" more done for the start of the next loop.
976 		 */
977 		start += nq;
978 		n -= nq;
979 
980 		/*
981 		 * While there are still Ingress Queue IDs to stuff into the
982 		 * current firmware RSS command, retrieve them from the
983 		 * Ingress Queue ID array and insert them into the command.
984 		 */
985 		while (nq > 0) {
986 			/*
987 			 * Grab up to the next 3 Ingress Queue IDs (wrapping
988 			 * around the Ingress Queue ID array if necessary) and
989 			 * insert them into the firmware RSS command at the
990 			 * current 3-tuple position within the commad.
991 			 */
992 			u16 qbuf[3];
993 			u16 *qbp = qbuf;
994 			int nqbuf = min(3, nq);
995 
996 			nq -= nqbuf;
997 			qbuf[0] = qbuf[1] = qbuf[2] = 0;
998 			while (nqbuf) {
999 				nqbuf--;
1000 				*qbp++ = *rsp++;
1001 				if (rsp >= rsp_end)
1002 					rsp = rspq;
1003 			}
1004 			*qp++ = cpu_to_be32(FW_RSS_IND_TBL_CMD_IQ0_V(qbuf[0]) |
1005 					    FW_RSS_IND_TBL_CMD_IQ1_V(qbuf[1]) |
1006 					    FW_RSS_IND_TBL_CMD_IQ2_V(qbuf[2]));
1007 		}
1008 
1009 		/*
1010 		 * Send this portion of the RRS table update to the firmware;
1011 		 * bail out on any errors.
1012 		 */
1013 		ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
1014 		if (ret)
1015 			return ret;
1016 	}
1017 	return 0;
1018 }
1019 
1020 /**
1021  *	t4vf_alloc_vi - allocate a virtual interface on a port
1022  *	@adapter: the adapter
1023  *	@port_id: physical port associated with the VI
1024  *
1025  *	Allocate a new Virtual Interface and bind it to the indicated
1026  *	physical port.  Return the new Virtual Interface Identifier on
1027  *	success, or a [negative] error number on failure.
1028  */
1029 int t4vf_alloc_vi(struct adapter *adapter, int port_id)
1030 {
1031 	struct fw_vi_cmd cmd, rpl;
1032 	int v;
1033 
1034 	/*
1035 	 * Execute a VI command to allocate Virtual Interface and return its
1036 	 * VIID.
1037 	 */
1038 	memset(&cmd, 0, sizeof(cmd));
1039 	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_VI_CMD) |
1040 				    FW_CMD_REQUEST_F |
1041 				    FW_CMD_WRITE_F |
1042 				    FW_CMD_EXEC_F);
1043 	cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(cmd) |
1044 					 FW_VI_CMD_ALLOC_F);
1045 	cmd.portid_pkd = FW_VI_CMD_PORTID_V(port_id);
1046 	v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
1047 	if (v)
1048 		return v;
1049 
1050 	return FW_VI_CMD_VIID_G(be16_to_cpu(rpl.type_viid));
1051 }
1052 
1053 /**
1054  *	t4vf_free_vi -- free a virtual interface
1055  *	@adapter: the adapter
1056  *	@viid: the virtual interface identifier
1057  *
1058  *	Free a previously allocated Virtual Interface.  Return an error on
1059  *	failure.
1060  */
1061 int t4vf_free_vi(struct adapter *adapter, int viid)
1062 {
1063 	struct fw_vi_cmd cmd;
1064 
1065 	/*
1066 	 * Execute a VI command to free the Virtual Interface.
1067 	 */
1068 	memset(&cmd, 0, sizeof(cmd));
1069 	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_VI_CMD) |
1070 				    FW_CMD_REQUEST_F |
1071 				    FW_CMD_EXEC_F);
1072 	cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(cmd) |
1073 					 FW_VI_CMD_FREE_F);
1074 	cmd.type_viid = cpu_to_be16(FW_VI_CMD_VIID_V(viid));
1075 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
1076 }
1077 
1078 /**
1079  *	t4vf_enable_vi - enable/disable a virtual interface
1080  *	@adapter: the adapter
1081  *	@viid: the Virtual Interface ID
1082  *	@rx_en: 1=enable Rx, 0=disable Rx
1083  *	@tx_en: 1=enable Tx, 0=disable Tx
1084  *
1085  *	Enables/disables a virtual interface.
1086  */
1087 int t4vf_enable_vi(struct adapter *adapter, unsigned int viid,
1088 		   bool rx_en, bool tx_en)
1089 {
1090 	struct fw_vi_enable_cmd cmd;
1091 
1092 	memset(&cmd, 0, sizeof(cmd));
1093 	cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_VI_ENABLE_CMD) |
1094 				     FW_CMD_REQUEST_F |
1095 				     FW_CMD_EXEC_F |
1096 				     FW_VI_ENABLE_CMD_VIID_V(viid));
1097 	cmd.ien_to_len16 = cpu_to_be32(FW_VI_ENABLE_CMD_IEN_V(rx_en) |
1098 				       FW_VI_ENABLE_CMD_EEN_V(tx_en) |
1099 				       FW_LEN16(cmd));
1100 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
1101 }
1102 
1103 /**
1104  *	t4vf_identify_port - identify a VI's port by blinking its LED
1105  *	@adapter: the adapter
1106  *	@viid: the Virtual Interface ID
1107  *	@nblinks: how many times to blink LED at 2.5 Hz
1108  *
1109  *	Identifies a VI's port by blinking its LED.
1110  */
1111 int t4vf_identify_port(struct adapter *adapter, unsigned int viid,
1112 		       unsigned int nblinks)
1113 {
1114 	struct fw_vi_enable_cmd cmd;
1115 
1116 	memset(&cmd, 0, sizeof(cmd));
1117 	cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_VI_ENABLE_CMD) |
1118 				     FW_CMD_REQUEST_F |
1119 				     FW_CMD_EXEC_F |
1120 				     FW_VI_ENABLE_CMD_VIID_V(viid));
1121 	cmd.ien_to_len16 = cpu_to_be32(FW_VI_ENABLE_CMD_LED_F |
1122 				       FW_LEN16(cmd));
1123 	cmd.blinkdur = cpu_to_be16(nblinks);
1124 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
1125 }
1126 
1127 /**
1128  *	t4vf_set_rxmode - set Rx properties of a virtual interface
1129  *	@adapter: the adapter
1130  *	@viid: the VI id
1131  *	@mtu: the new MTU or -1 for no change
1132  *	@promisc: 1 to enable promiscuous mode, 0 to disable it, -1 no change
1133  *	@all_multi: 1 to enable all-multi mode, 0 to disable it, -1 no change
1134  *	@bcast: 1 to enable broadcast Rx, 0 to disable it, -1 no change
1135  *	@vlanex: 1 to enable hardware VLAN Tag extraction, 0 to disable it,
1136  *		-1 no change
1137  *
1138  *	Sets Rx properties of a virtual interface.
1139  */
1140 int t4vf_set_rxmode(struct adapter *adapter, unsigned int viid,
1141 		    int mtu, int promisc, int all_multi, int bcast, int vlanex,
1142 		    bool sleep_ok)
1143 {
1144 	struct fw_vi_rxmode_cmd cmd;
1145 
1146 	/* convert to FW values */
1147 	if (mtu < 0)
1148 		mtu = FW_VI_RXMODE_CMD_MTU_M;
1149 	if (promisc < 0)
1150 		promisc = FW_VI_RXMODE_CMD_PROMISCEN_M;
1151 	if (all_multi < 0)
1152 		all_multi = FW_VI_RXMODE_CMD_ALLMULTIEN_M;
1153 	if (bcast < 0)
1154 		bcast = FW_VI_RXMODE_CMD_BROADCASTEN_M;
1155 	if (vlanex < 0)
1156 		vlanex = FW_VI_RXMODE_CMD_VLANEXEN_M;
1157 
1158 	memset(&cmd, 0, sizeof(cmd));
1159 	cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_VI_RXMODE_CMD) |
1160 				     FW_CMD_REQUEST_F |
1161 				     FW_CMD_WRITE_F |
1162 				     FW_VI_RXMODE_CMD_VIID_V(viid));
1163 	cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
1164 	cmd.mtu_to_vlanexen =
1165 		cpu_to_be32(FW_VI_RXMODE_CMD_MTU_V(mtu) |
1166 			    FW_VI_RXMODE_CMD_PROMISCEN_V(promisc) |
1167 			    FW_VI_RXMODE_CMD_ALLMULTIEN_V(all_multi) |
1168 			    FW_VI_RXMODE_CMD_BROADCASTEN_V(bcast) |
1169 			    FW_VI_RXMODE_CMD_VLANEXEN_V(vlanex));
1170 	return t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), NULL, sleep_ok);
1171 }
1172 
1173 /**
1174  *	t4vf_alloc_mac_filt - allocates exact-match filters for MAC addresses
1175  *	@adapter: the adapter
1176  *	@viid: the Virtual Interface Identifier
1177  *	@free: if true any existing filters for this VI id are first removed
1178  *	@naddr: the number of MAC addresses to allocate filters for (up to 7)
1179  *	@addr: the MAC address(es)
1180  *	@idx: where to store the index of each allocated filter
1181  *	@hash: pointer to hash address filter bitmap
1182  *	@sleep_ok: call is allowed to sleep
1183  *
1184  *	Allocates an exact-match filter for each of the supplied addresses and
1185  *	sets it to the corresponding address.  If @idx is not %NULL it should
1186  *	have at least @naddr entries, each of which will be set to the index of
1187  *	the filter allocated for the corresponding MAC address.  If a filter
1188  *	could not be allocated for an address its index is set to 0xffff.
1189  *	If @hash is not %NULL addresses that fail to allocate an exact filter
1190  *	are hashed and update the hash filter bitmap pointed at by @hash.
1191  *
1192  *	Returns a negative error number or the number of filters allocated.
1193  */
1194 int t4vf_alloc_mac_filt(struct adapter *adapter, unsigned int viid, bool free,
1195 			unsigned int naddr, const u8 **addr, u16 *idx,
1196 			u64 *hash, bool sleep_ok)
1197 {
1198 	int offset, ret = 0;
1199 	unsigned nfilters = 0;
1200 	unsigned int rem = naddr;
1201 	struct fw_vi_mac_cmd cmd, rpl;
1202 	unsigned int max_naddr = adapter->params.arch.mps_tcam_size;
1203 
1204 	if (naddr > max_naddr)
1205 		return -EINVAL;
1206 
1207 	for (offset = 0; offset < naddr; /**/) {
1208 		unsigned int fw_naddr = (rem < ARRAY_SIZE(cmd.u.exact)
1209 					 ? rem
1210 					 : ARRAY_SIZE(cmd.u.exact));
1211 		size_t len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
1212 						     u.exact[fw_naddr]), 16);
1213 		struct fw_vi_mac_exact *p;
1214 		int i;
1215 
1216 		memset(&cmd, 0, sizeof(cmd));
1217 		cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_VI_MAC_CMD) |
1218 					     FW_CMD_REQUEST_F |
1219 					     FW_CMD_WRITE_F |
1220 					     (free ? FW_CMD_EXEC_F : 0) |
1221 					     FW_VI_MAC_CMD_VIID_V(viid));
1222 		cmd.freemacs_to_len16 =
1223 			cpu_to_be32(FW_VI_MAC_CMD_FREEMACS_V(free) |
1224 				    FW_CMD_LEN16_V(len16));
1225 
1226 		for (i = 0, p = cmd.u.exact; i < fw_naddr; i++, p++) {
1227 			p->valid_to_idx = cpu_to_be16(
1228 				FW_VI_MAC_CMD_VALID_F |
1229 				FW_VI_MAC_CMD_IDX_V(FW_VI_MAC_ADD_MAC));
1230 			memcpy(p->macaddr, addr[offset+i], sizeof(p->macaddr));
1231 		}
1232 
1233 
1234 		ret = t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), &rpl,
1235 					sleep_ok);
1236 		if (ret && ret != -ENOMEM)
1237 			break;
1238 
1239 		for (i = 0, p = rpl.u.exact; i < fw_naddr; i++, p++) {
1240 			u16 index = FW_VI_MAC_CMD_IDX_G(
1241 				be16_to_cpu(p->valid_to_idx));
1242 
1243 			if (idx)
1244 				idx[offset+i] =
1245 					(index >= max_naddr
1246 					 ? 0xffff
1247 					 : index);
1248 			if (index < max_naddr)
1249 				nfilters++;
1250 			else if (hash)
1251 				*hash |= (1ULL << hash_mac_addr(addr[offset+i]));
1252 		}
1253 
1254 		free = false;
1255 		offset += fw_naddr;
1256 		rem -= fw_naddr;
1257 	}
1258 
1259 	/*
1260 	 * If there were no errors or we merely ran out of room in our MAC
1261 	 * address arena, return the number of filters actually written.
1262 	 */
1263 	if (ret == 0 || ret == -ENOMEM)
1264 		ret = nfilters;
1265 	return ret;
1266 }
1267 
1268 /**
1269  *	t4vf_change_mac - modifies the exact-match filter for a MAC address
1270  *	@adapter: the adapter
1271  *	@viid: the Virtual Interface ID
1272  *	@idx: index of existing filter for old value of MAC address, or -1
1273  *	@addr: the new MAC address value
1274  *	@persist: if idx < 0, the new MAC allocation should be persistent
1275  *
1276  *	Modifies an exact-match filter and sets it to the new MAC address.
1277  *	Note that in general it is not possible to modify the value of a given
1278  *	filter so the generic way to modify an address filter is to free the
1279  *	one being used by the old address value and allocate a new filter for
1280  *	the new address value.  @idx can be -1 if the address is a new
1281  *	addition.
1282  *
1283  *	Returns a negative error number or the index of the filter with the new
1284  *	MAC value.
1285  */
1286 int t4vf_change_mac(struct adapter *adapter, unsigned int viid,
1287 		    int idx, const u8 *addr, bool persist)
1288 {
1289 	int ret;
1290 	struct fw_vi_mac_cmd cmd, rpl;
1291 	struct fw_vi_mac_exact *p = &cmd.u.exact[0];
1292 	size_t len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
1293 					     u.exact[1]), 16);
1294 	unsigned int max_mac_addr = adapter->params.arch.mps_tcam_size;
1295 
1296 	/*
1297 	 * If this is a new allocation, determine whether it should be
1298 	 * persistent (across a "freemacs" operation) or not.
1299 	 */
1300 	if (idx < 0)
1301 		idx = persist ? FW_VI_MAC_ADD_PERSIST_MAC : FW_VI_MAC_ADD_MAC;
1302 
1303 	memset(&cmd, 0, sizeof(cmd));
1304 	cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_VI_MAC_CMD) |
1305 				     FW_CMD_REQUEST_F |
1306 				     FW_CMD_WRITE_F |
1307 				     FW_VI_MAC_CMD_VIID_V(viid));
1308 	cmd.freemacs_to_len16 = cpu_to_be32(FW_CMD_LEN16_V(len16));
1309 	p->valid_to_idx = cpu_to_be16(FW_VI_MAC_CMD_VALID_F |
1310 				      FW_VI_MAC_CMD_IDX_V(idx));
1311 	memcpy(p->macaddr, addr, sizeof(p->macaddr));
1312 
1313 	ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
1314 	if (ret == 0) {
1315 		p = &rpl.u.exact[0];
1316 		ret = FW_VI_MAC_CMD_IDX_G(be16_to_cpu(p->valid_to_idx));
1317 		if (ret >= max_mac_addr)
1318 			ret = -ENOMEM;
1319 	}
1320 	return ret;
1321 }
1322 
1323 /**
1324  *	t4vf_set_addr_hash - program the MAC inexact-match hash filter
1325  *	@adapter: the adapter
1326  *	@viid: the Virtual Interface Identifier
1327  *	@ucast: whether the hash filter should also match unicast addresses
1328  *	@vec: the value to be written to the hash filter
1329  *	@sleep_ok: call is allowed to sleep
1330  *
1331  *	Sets the 64-bit inexact-match hash filter for a virtual interface.
1332  */
1333 int t4vf_set_addr_hash(struct adapter *adapter, unsigned int viid,
1334 		       bool ucast, u64 vec, bool sleep_ok)
1335 {
1336 	struct fw_vi_mac_cmd cmd;
1337 	size_t len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
1338 					     u.exact[0]), 16);
1339 
1340 	memset(&cmd, 0, sizeof(cmd));
1341 	cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_VI_MAC_CMD) |
1342 				     FW_CMD_REQUEST_F |
1343 				     FW_CMD_WRITE_F |
1344 				     FW_VI_ENABLE_CMD_VIID_V(viid));
1345 	cmd.freemacs_to_len16 = cpu_to_be32(FW_VI_MAC_CMD_HASHVECEN_F |
1346 					    FW_VI_MAC_CMD_HASHUNIEN_V(ucast) |
1347 					    FW_CMD_LEN16_V(len16));
1348 	cmd.u.hash.hashvec = cpu_to_be64(vec);
1349 	return t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), NULL, sleep_ok);
1350 }
1351 
1352 /**
1353  *	t4vf_get_port_stats - collect "port" statistics
1354  *	@adapter: the adapter
1355  *	@pidx: the port index
1356  *	@s: the stats structure to fill
1357  *
1358  *	Collect statistics for the "port"'s Virtual Interface.
1359  */
1360 int t4vf_get_port_stats(struct adapter *adapter, int pidx,
1361 			struct t4vf_port_stats *s)
1362 {
1363 	struct port_info *pi = adap2pinfo(adapter, pidx);
1364 	struct fw_vi_stats_vf fwstats;
1365 	unsigned int rem = VI_VF_NUM_STATS;
1366 	__be64 *fwsp = (__be64 *)&fwstats;
1367 
1368 	/*
1369 	 * Grab the Virtual Interface statistics a chunk at a time via mailbox
1370 	 * commands.  We could use a Work Request and get all of them at once
1371 	 * but that's an asynchronous interface which is awkward to use.
1372 	 */
1373 	while (rem) {
1374 		unsigned int ix = VI_VF_NUM_STATS - rem;
1375 		unsigned int nstats = min(6U, rem);
1376 		struct fw_vi_stats_cmd cmd, rpl;
1377 		size_t len = (offsetof(struct fw_vi_stats_cmd, u) +
1378 			      sizeof(struct fw_vi_stats_ctl));
1379 		size_t len16 = DIV_ROUND_UP(len, 16);
1380 		int ret;
1381 
1382 		memset(&cmd, 0, sizeof(cmd));
1383 		cmd.op_to_viid = cpu_to_be32(FW_CMD_OP_V(FW_VI_STATS_CMD) |
1384 					     FW_VI_STATS_CMD_VIID_V(pi->viid) |
1385 					     FW_CMD_REQUEST_F |
1386 					     FW_CMD_READ_F);
1387 		cmd.retval_len16 = cpu_to_be32(FW_CMD_LEN16_V(len16));
1388 		cmd.u.ctl.nstats_ix =
1389 			cpu_to_be16(FW_VI_STATS_CMD_IX_V(ix) |
1390 				    FW_VI_STATS_CMD_NSTATS_V(nstats));
1391 		ret = t4vf_wr_mbox_ns(adapter, &cmd, len, &rpl);
1392 		if (ret)
1393 			return ret;
1394 
1395 		memcpy(fwsp, &rpl.u.ctl.stat0, sizeof(__be64) * nstats);
1396 
1397 		rem -= nstats;
1398 		fwsp += nstats;
1399 	}
1400 
1401 	/*
1402 	 * Translate firmware statistics into host native statistics.
1403 	 */
1404 	s->tx_bcast_bytes = be64_to_cpu(fwstats.tx_bcast_bytes);
1405 	s->tx_bcast_frames = be64_to_cpu(fwstats.tx_bcast_frames);
1406 	s->tx_mcast_bytes = be64_to_cpu(fwstats.tx_mcast_bytes);
1407 	s->tx_mcast_frames = be64_to_cpu(fwstats.tx_mcast_frames);
1408 	s->tx_ucast_bytes = be64_to_cpu(fwstats.tx_ucast_bytes);
1409 	s->tx_ucast_frames = be64_to_cpu(fwstats.tx_ucast_frames);
1410 	s->tx_drop_frames = be64_to_cpu(fwstats.tx_drop_frames);
1411 	s->tx_offload_bytes = be64_to_cpu(fwstats.tx_offload_bytes);
1412 	s->tx_offload_frames = be64_to_cpu(fwstats.tx_offload_frames);
1413 
1414 	s->rx_bcast_bytes = be64_to_cpu(fwstats.rx_bcast_bytes);
1415 	s->rx_bcast_frames = be64_to_cpu(fwstats.rx_bcast_frames);
1416 	s->rx_mcast_bytes = be64_to_cpu(fwstats.rx_mcast_bytes);
1417 	s->rx_mcast_frames = be64_to_cpu(fwstats.rx_mcast_frames);
1418 	s->rx_ucast_bytes = be64_to_cpu(fwstats.rx_ucast_bytes);
1419 	s->rx_ucast_frames = be64_to_cpu(fwstats.rx_ucast_frames);
1420 
1421 	s->rx_err_frames = be64_to_cpu(fwstats.rx_err_frames);
1422 
1423 	return 0;
1424 }
1425 
1426 /**
1427  *	t4vf_iq_free - free an ingress queue and its free lists
1428  *	@adapter: the adapter
1429  *	@iqtype: the ingress queue type (FW_IQ_TYPE_FL_INT_CAP, etc.)
1430  *	@iqid: ingress queue ID
1431  *	@fl0id: FL0 queue ID or 0xffff if no attached FL0
1432  *	@fl1id: FL1 queue ID or 0xffff if no attached FL1
1433  *
1434  *	Frees an ingress queue and its associated free lists, if any.
1435  */
1436 int t4vf_iq_free(struct adapter *adapter, unsigned int iqtype,
1437 		 unsigned int iqid, unsigned int fl0id, unsigned int fl1id)
1438 {
1439 	struct fw_iq_cmd cmd;
1440 
1441 	memset(&cmd, 0, sizeof(cmd));
1442 	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_IQ_CMD) |
1443 				    FW_CMD_REQUEST_F |
1444 				    FW_CMD_EXEC_F);
1445 	cmd.alloc_to_len16 = cpu_to_be32(FW_IQ_CMD_FREE_F |
1446 					 FW_LEN16(cmd));
1447 	cmd.type_to_iqandstindex =
1448 		cpu_to_be32(FW_IQ_CMD_TYPE_V(iqtype));
1449 
1450 	cmd.iqid = cpu_to_be16(iqid);
1451 	cmd.fl0id = cpu_to_be16(fl0id);
1452 	cmd.fl1id = cpu_to_be16(fl1id);
1453 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
1454 }
1455 
1456 /**
1457  *	t4vf_eth_eq_free - free an Ethernet egress queue
1458  *	@adapter: the adapter
1459  *	@eqid: egress queue ID
1460  *
1461  *	Frees an Ethernet egress queue.
1462  */
1463 int t4vf_eth_eq_free(struct adapter *adapter, unsigned int eqid)
1464 {
1465 	struct fw_eq_eth_cmd cmd;
1466 
1467 	memset(&cmd, 0, sizeof(cmd));
1468 	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_EQ_ETH_CMD) |
1469 				    FW_CMD_REQUEST_F |
1470 				    FW_CMD_EXEC_F);
1471 	cmd.alloc_to_len16 = cpu_to_be32(FW_EQ_ETH_CMD_FREE_F |
1472 					 FW_LEN16(cmd));
1473 	cmd.eqid_pkd = cpu_to_be32(FW_EQ_ETH_CMD_EQID_V(eqid));
1474 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
1475 }
1476 
1477 /**
1478  *	t4vf_handle_fw_rpl - process a firmware reply message
1479  *	@adapter: the adapter
1480  *	@rpl: start of the firmware message
1481  *
1482  *	Processes a firmware message, such as link state change messages.
1483  */
1484 int t4vf_handle_fw_rpl(struct adapter *adapter, const __be64 *rpl)
1485 {
1486 	const struct fw_cmd_hdr *cmd_hdr = (const struct fw_cmd_hdr *)rpl;
1487 	u8 opcode = FW_CMD_OP_G(be32_to_cpu(cmd_hdr->hi));
1488 
1489 	switch (opcode) {
1490 	case FW_PORT_CMD: {
1491 		/*
1492 		 * Link/module state change message.
1493 		 */
1494 		const struct fw_port_cmd *port_cmd =
1495 			(const struct fw_port_cmd *)rpl;
1496 		u32 stat, mod;
1497 		int action, port_id, link_ok, speed, fc, pidx;
1498 
1499 		/*
1500 		 * Extract various fields from port status change message.
1501 		 */
1502 		action = FW_PORT_CMD_ACTION_G(
1503 			be32_to_cpu(port_cmd->action_to_len16));
1504 		if (action != FW_PORT_ACTION_GET_PORT_INFO) {
1505 			dev_err(adapter->pdev_dev,
1506 				"Unknown firmware PORT reply action %x\n",
1507 				action);
1508 			break;
1509 		}
1510 
1511 		port_id = FW_PORT_CMD_PORTID_G(
1512 			be32_to_cpu(port_cmd->op_to_portid));
1513 
1514 		stat = be32_to_cpu(port_cmd->u.info.lstatus_to_modtype);
1515 		link_ok = (stat & FW_PORT_CMD_LSTATUS_F) != 0;
1516 		speed = 0;
1517 		fc = 0;
1518 		if (stat & FW_PORT_CMD_RXPAUSE_F)
1519 			fc |= PAUSE_RX;
1520 		if (stat & FW_PORT_CMD_TXPAUSE_F)
1521 			fc |= PAUSE_TX;
1522 		if (stat & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_100M))
1523 			speed = 100;
1524 		else if (stat & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_1G))
1525 			speed = 1000;
1526 		else if (stat & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_10G))
1527 			speed = 10000;
1528 		else if (stat & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_40G))
1529 			speed = 40000;
1530 
1531 		/*
1532 		 * Scan all of our "ports" (Virtual Interfaces) looking for
1533 		 * those bound to the physical port which has changed.  If
1534 		 * our recorded state doesn't match the current state,
1535 		 * signal that change to the OS code.
1536 		 */
1537 		for_each_port(adapter, pidx) {
1538 			struct port_info *pi = adap2pinfo(adapter, pidx);
1539 			struct link_config *lc;
1540 
1541 			if (pi->port_id != port_id)
1542 				continue;
1543 
1544 			lc = &pi->link_cfg;
1545 
1546 			mod = FW_PORT_CMD_MODTYPE_G(stat);
1547 			if (mod != pi->mod_type) {
1548 				pi->mod_type = mod;
1549 				t4vf_os_portmod_changed(adapter, pidx);
1550 			}
1551 
1552 			if (link_ok != lc->link_ok || speed != lc->speed ||
1553 			    fc != lc->fc) {
1554 				/* something changed */
1555 				lc->link_ok = link_ok;
1556 				lc->speed = speed;
1557 				lc->fc = fc;
1558 				lc->supported =
1559 					be16_to_cpu(port_cmd->u.info.pcap);
1560 				t4vf_os_link_changed(adapter, pidx, link_ok);
1561 			}
1562 		}
1563 		break;
1564 	}
1565 
1566 	default:
1567 		dev_err(adapter->pdev_dev, "Unknown firmware reply %X\n",
1568 			opcode);
1569 	}
1570 	return 0;
1571 }
1572 
1573 /**
1574  */
1575 int t4vf_prep_adapter(struct adapter *adapter)
1576 {
1577 	int err;
1578 	unsigned int chipid;
1579 
1580 	/* Wait for the device to become ready before proceeding ...
1581 	 */
1582 	err = t4vf_wait_dev_ready(adapter);
1583 	if (err)
1584 		return err;
1585 
1586 	/* Default port and clock for debugging in case we can't reach
1587 	 * firmware.
1588 	 */
1589 	adapter->params.nports = 1;
1590 	adapter->params.vfres.pmask = 1;
1591 	adapter->params.vpd.cclk = 50000;
1592 
1593 	adapter->params.chip = 0;
1594 	switch (CHELSIO_PCI_ID_VER(adapter->pdev->device)) {
1595 	case CHELSIO_T4:
1596 		adapter->params.chip |= CHELSIO_CHIP_CODE(CHELSIO_T4, 0);
1597 		adapter->params.arch.sge_fl_db = DBPRIO_F;
1598 		adapter->params.arch.mps_tcam_size =
1599 				NUM_MPS_CLS_SRAM_L_INSTANCES;
1600 		break;
1601 
1602 	case CHELSIO_T5:
1603 		chipid = REV_G(t4_read_reg(adapter, PL_VF_REV_A));
1604 		adapter->params.chip |= CHELSIO_CHIP_CODE(CHELSIO_T5, chipid);
1605 		adapter->params.arch.sge_fl_db = DBPRIO_F | DBTYPE_F;
1606 		adapter->params.arch.mps_tcam_size =
1607 				NUM_MPS_T5_CLS_SRAM_L_INSTANCES;
1608 		break;
1609 
1610 	case CHELSIO_T6:
1611 		chipid = REV_G(t4_read_reg(adapter, PL_VF_REV_A));
1612 		adapter->params.chip |= CHELSIO_CHIP_CODE(CHELSIO_T6, chipid);
1613 		adapter->params.arch.sge_fl_db = 0;
1614 		adapter->params.arch.mps_tcam_size =
1615 				NUM_MPS_T5_CLS_SRAM_L_INSTANCES;
1616 		break;
1617 	}
1618 
1619 	return 0;
1620 }
1621