xref: /dpdk/drivers/net/cxgbe/base/t4vf_hw.c (revision 3e49a10f2ede1d047bbdec9ee56e2227459278f7)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Chelsio Communications.
3  * All rights reserved.
4  */
5 
6 #include <ethdev_driver.h>
7 #include <rte_ether.h>
8 
9 #include "common.h"
10 #include "t4_regs.h"
11 
12 /**
13  * t4vf_wait_dev_ready - wait till to reads of registers work
14  *
15  * Wait for the device to become ready (signified by our "who am I" register
16  * returning a value other than all 1's).  Return an error if it doesn't
17  * become ready ...
18  */
19 static int t4vf_wait_dev_ready(struct adapter *adapter)
20 {
21 	const u32 whoami = T4VF_PL_BASE_ADDR + A_PL_VF_WHOAMI;
22 	const u32 notready1 = 0xffffffff;
23 	const u32 notready2 = 0xeeeeeeee;
24 	u32 val;
25 
26 	val = t4_read_reg(adapter, whoami);
27 	if (val != notready1 && val != notready2)
28 		return 0;
29 
30 	msleep(500);
31 	val = t4_read_reg(adapter, whoami);
32 	if (val != notready1 && val != notready2)
33 		return 0;
34 
35 	dev_err(adapter, "Device didn't become ready for access, whoami = %#x\n",
36 		val);
37 	return -EIO;
38 }
39 
40 /*
41  * Get the reply to a mailbox command and store it in @rpl in big-endian order.
42  */
43 static void get_mbox_rpl(struct adapter *adap, __be64 *rpl, int nflit,
44 			 u32 mbox_addr)
45 {
46 	for ( ; nflit; nflit--, mbox_addr += 8)
47 		*rpl++ = cpu_to_be64(t4_read_reg64(adap, mbox_addr));
48 }
49 
50 /**
51  * t4vf_wr_mbox_core - send a command to FW through the mailbox
52  * @adapter: the adapter
53  * @cmd: the command to write
54  * @size: command length in bytes
55  * @rpl: where to optionally store the reply
56  * @sleep_ok: if true we may sleep while awaiting command completion
57  *
58  * Sends the given command to FW through the mailbox and waits for the
59  * FW to execute the command.  If @rpl is not %NULL it is used to store
60  * the FW's reply to the command.  The command and its optional reply
61  * are of the same length.  FW can take up to 500 ms to respond.
62  * @sleep_ok determines whether we may sleep while awaiting the response.
63  * If sleeping is allowed we use progressive backoff otherwise we spin.
64  *
65  * The return value is 0 on success or a negative errno on failure.  A
66  * failure can happen either because we are not able to execute the
67  * command or FW executes it but signals an error.  In the latter case
68  * the return value is the error code indicated by FW (negated).
69  */
70 int t4vf_wr_mbox_core(struct adapter *adapter,
71 		      const void __rte_may_alias *cmd,
72 		      int size, void *rpl, bool sleep_ok)
73 {
74 	/*
75 	 * We delay in small increments at first in an effort to maintain
76 	 * responsiveness for simple, fast executing commands but then back
77 	 * off to larger delays to a maximum retry delay.
78 	 */
79 	static const int delay[] = {
80 		1, 1, 3, 5, 10, 10, 20, 50, 100
81 	};
82 
83 
84 	u32 mbox_ctl = T4VF_CIM_BASE_ADDR + A_CIM_VF_EXT_MAILBOX_CTRL;
85 	__be64 cmd_rpl[MBOX_LEN / 8];
86 	struct mbox_entry *entry;
87 	unsigned int delay_idx;
88 	u32 v, mbox_data;
89 	const __be64 *p;
90 	int i, ret;
91 	int ms;
92 
93 	/* In T6, mailbox size is changed to 128 bytes to avoid
94 	 * invalidating the entire prefetch buffer.
95 	 */
96 	if (CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5)
97 		mbox_data = T4VF_MBDATA_BASE_ADDR;
98 	else
99 		mbox_data = T6VF_MBDATA_BASE_ADDR;
100 
101 	/*
102 	 * Commands must be multiples of 16 bytes in length and may not be
103 	 * larger than the size of the Mailbox Data register array.
104 	 */
105 	if ((size % 16) != 0 ||
106 			size > NUM_CIM_VF_MAILBOX_DATA_INSTANCES * 4)
107 		return -EINVAL;
108 
109 	entry = t4_os_alloc(sizeof(*entry));
110 	if (entry == NULL)
111 		return -ENOMEM;
112 
113 	/*
114 	 * Queue ourselves onto the mailbox access list.  When our entry is at
115 	 * the front of the list, we have rights to access the mailbox.  So we
116 	 * wait [for a while] till we're at the front [or bail out with an
117 	 * EBUSY] ...
118 	 */
119 	t4_os_atomic_add_tail(entry, &adapter->mbox_list, &adapter->mbox_lock);
120 
121 	delay_idx = 0;
122 	ms = delay[0];
123 
124 	for (i = 0; ; i += ms) {
125 		/*
126 		 * If we've waited too long, return a busy indication.  This
127 		 * really ought to be based on our initial position in the
128 		 * mailbox access list but this is a start.  We very rarely
129 		 * contend on access to the mailbox ...
130 		 */
131 		if (i > (2 * FW_CMD_MAX_TIMEOUT)) {
132 			t4_os_atomic_list_del(entry, &adapter->mbox_list,
133 					      &adapter->mbox_lock);
134 			ret = -EBUSY;
135 			goto out_free;
136 		}
137 
138 		/*
139 		 * If we're at the head, break out and start the mailbox
140 		 * protocol.
141 		 */
142 		if (t4_os_list_first_entry(&adapter->mbox_list) == entry)
143 			break;
144 
145 		/*
146 		 * Delay for a bit before checking again ...
147 		 */
148 		if (sleep_ok) {
149 			ms = delay[delay_idx];  /* last element may repeat */
150 			if (delay_idx < ARRAY_SIZE(delay) - 1)
151 				delay_idx++;
152 			msleep(ms);
153 		} else {
154 			rte_delay_ms(ms);
155 		}
156 	}
157 
158 	/*
159 	 * Loop trying to get ownership of the mailbox.  Return an error
160 	 * if we can't gain ownership.
161 	 */
162 	v = G_MBOWNER(t4_read_reg(adapter, mbox_ctl));
163 	for (i = 0; v == X_MBOWNER_NONE && i < 3; i++)
164 		v = G_MBOWNER(t4_read_reg(adapter, mbox_ctl));
165 
166 	if (v != X_MBOWNER_PL) {
167 		t4_os_atomic_list_del(entry, &adapter->mbox_list,
168 				      &adapter->mbox_lock);
169 		ret = (v == X_MBOWNER_FW) ? -EBUSY : -ETIMEDOUT;
170 		goto out_free;
171 	}
172 
173 	/*
174 	 * Write the command array into the Mailbox Data register array and
175 	 * transfer ownership of the mailbox to the firmware.
176 	 */
177 	for (i = 0, p = cmd; i < size; i += 8)
178 		t4_write_reg64(adapter, mbox_data + i, be64_to_cpu(*p++));
179 
180 	t4_read_reg(adapter, mbox_data);          /* flush write */
181 	t4_write_reg(adapter, mbox_ctl,
182 			F_MBMSGVALID | V_MBOWNER(X_MBOWNER_FW));
183 	t4_read_reg(adapter, mbox_ctl);          /* flush write */
184 	delay_idx = 0;
185 	ms = delay[0];
186 
187 	/*
188 	 * Spin waiting for firmware to acknowledge processing our command.
189 	 */
190 	for (i = 0; i < FW_CMD_MAX_TIMEOUT; i++) {
191 		if (sleep_ok) {
192 			ms = delay[delay_idx];  /* last element may repeat */
193 			if (delay_idx < ARRAY_SIZE(delay) - 1)
194 				delay_idx++;
195 			msleep(ms);
196 		} else {
197 			rte_delay_ms(ms);
198 		}
199 
200 		/*
201 		 * If we're the owner, see if this is the reply we wanted.
202 		 */
203 		v = t4_read_reg(adapter, mbox_ctl);
204 		if (G_MBOWNER(v) == X_MBOWNER_PL) {
205 			/*
206 			 * If the Message Valid bit isn't on, revoke ownership
207 			 * of the mailbox and continue waiting for our reply.
208 			 */
209 			if ((v & F_MBMSGVALID) == 0) {
210 				t4_write_reg(adapter, mbox_ctl,
211 					     V_MBOWNER(X_MBOWNER_NONE));
212 				continue;
213 			}
214 
215 			/*
216 			 * We now have our reply.  Extract the command return
217 			 * value, copy the reply back to our caller's buffer
218 			 * (if specified) and revoke ownership of the mailbox.
219 			 * We return the (negated) firmware command return
220 			 * code (this depends on FW_SUCCESS == 0).  (Again we
221 			 * avoid clogging the log with FW_VI_STATS_CMD
222 			 * reply results.)
223 			 */
224 
225 			/*
226 			 * Retrieve the command reply and release the mailbox.
227 			 */
228 			get_mbox_rpl(adapter, cmd_rpl, size / 8, mbox_data);
229 			t4_write_reg(adapter, mbox_ctl,
230 				     V_MBOWNER(X_MBOWNER_NONE));
231 			t4_os_atomic_list_del(entry, &adapter->mbox_list,
232 					      &adapter->mbox_lock);
233 
234 			/* return value in high-order host-endian word */
235 			v = be64_to_cpu(cmd_rpl[0]);
236 
237 			if (rpl) {
238 				/* request bit in high-order BE word */
239 				WARN_ON((be32_to_cpu(*(const u32 *)cmd)
240 					 & F_FW_CMD_REQUEST) == 0);
241 				memcpy(rpl, cmd_rpl, size);
242 			}
243 			ret = -((int)G_FW_CMD_RETVAL(v));
244 			goto out_free;
245 		}
246 	}
247 
248 	/*
249 	 * We timed out.  Return the error ...
250 	 */
251 	dev_err(adapter, "command %#x timed out\n",
252 		*(const u8 *)cmd);
253 	dev_err(adapter, "    Control = %#x\n", t4_read_reg(adapter, mbox_ctl));
254 	t4_os_atomic_list_del(entry, &adapter->mbox_list, &adapter->mbox_lock);
255 	ret = -ETIMEDOUT;
256 
257 out_free:
258 	t4_os_free(entry);
259 	return ret;
260 }
261 
262 /**
263  * t4vf_fw_reset - issue a reset to FW
264  * @adapter: the adapter
265  *
266  * Issues a reset command to FW.  For a Physical Function this would
267  * result in the Firmware resetting all of its state.  For a Virtual
268  * Function this just resets the state associated with the VF.
269  */
270 int t4vf_fw_reset(struct adapter *adapter)
271 {
272 	struct fw_reset_cmd cmd;
273 
274 	memset(&cmd, 0, sizeof(cmd));
275 	cmd.op_to_write = cpu_to_be32(V_FW_CMD_OP(FW_RESET_CMD) |
276 				      F_FW_CMD_WRITE);
277 	cmd.retval_len16 = cpu_to_be32(V_FW_CMD_LEN16(FW_LEN16(cmd)));
278 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
279 }
280 
281 /**
282  * t4vf_prep_adapter - prepare SW and HW for operation
283  * @adapter: the adapter
284  *
285  * Initialize adapter SW state for the various HW modules, set initial
286  * values for some adapter tunables, take PHYs out of reset, and
287  * initialize the MDIO interface.
288  */
289 int t4vf_prep_adapter(struct adapter *adapter)
290 {
291 	u32 pl_vf_rev;
292 	int ret, ver;
293 
294 	ret = t4vf_wait_dev_ready(adapter);
295 	if (ret < 0)
296 		return ret;
297 
298 	/*
299 	 * Default port and clock for debugging in case we can't reach
300 	 * firmware.
301 	 */
302 	adapter->params.nports = 1;
303 	adapter->params.vfres.pmask = 1;
304 	adapter->params.vpd.cclk = 50000;
305 
306 	pl_vf_rev = G_REV(t4_read_reg(adapter, A_PL_VF_REV));
307 	adapter->params.pci.device_id = adapter->pdev->id.device_id;
308 	adapter->params.pci.vendor_id = adapter->pdev->id.vendor_id;
309 
310 	/*
311 	 * WE DON'T NEED adapter->params.chip CODE ONCE PL_REV CONTAINS
312 	 * ADAPTER (VERSION << 4 | REVISION)
313 	 */
314 	ver = CHELSIO_PCI_ID_VER(adapter->params.pci.device_id);
315 	adapter->params.chip = 0;
316 	switch (ver) {
317 	case CHELSIO_T5:
318 		adapter->params.chip |= CHELSIO_CHIP_CODE(CHELSIO_T5,
319 							  pl_vf_rev);
320 		adapter->params.arch.sge_fl_db = F_DBPRIO | F_DBTYPE;
321 		adapter->params.arch.mps_tcam_size =
322 			NUM_MPS_T5_CLS_SRAM_L_INSTANCES;
323 		break;
324 	case CHELSIO_T6:
325 		adapter->params.chip |= CHELSIO_CHIP_CODE(CHELSIO_T6,
326 							  pl_vf_rev);
327 		adapter->params.arch.sge_fl_db = 0;
328 		adapter->params.arch.mps_tcam_size =
329 			NUM_MPS_T5_CLS_SRAM_L_INSTANCES;
330 		break;
331 	default:
332 		dev_err(adapter, "%s: Device %d is not supported\n",
333 			__func__, adapter->params.pci.device_id);
334 		return -EINVAL;
335 	}
336 	return 0;
337 }
338 
339 /**
340  * t4vf_query_params - query FW or device parameters
341  * @adapter: the adapter
342  * @nparams: the number of parameters
343  * @params: the parameter names
344  * @vals: the parameter values
345  *
346  * Reads the values of firmware or device parameters.  Up to 7 parameters
347  * can be queried at once.
348  */
349 int t4vf_query_params(struct adapter *adapter, unsigned int nparams,
350 		      const u32 *params, u32 *vals)
351 {
352 	struct fw_params_cmd cmd, rpl;
353 	struct fw_params_param *p;
354 	unsigned int i;
355 	size_t len16;
356 	int ret;
357 
358 	if (nparams > 7)
359 		return -EINVAL;
360 
361 	memset(&cmd, 0, sizeof(cmd));
362 	cmd.op_to_vfn = cpu_to_be32(V_FW_CMD_OP(FW_PARAMS_CMD) |
363 				    F_FW_CMD_REQUEST |
364 				    F_FW_CMD_READ);
365 	len16 = DIV_ROUND_UP(offsetof(struct fw_params_cmd,
366 			     param[nparams]), 16);
367 	cmd.retval_len16 = cpu_to_be32(V_FW_CMD_LEN16(len16));
368 	for (i = 0, p = &cmd.param[0]; i < nparams; i++, p++)
369 		p->mnem = cpu_to_be32(*params++);
370 	ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
371 	if (ret == 0)
372 		for (i = 0, p = &rpl.param[0]; i < nparams; i++, p++)
373 			*vals++ = be32_to_cpu(p->val);
374 	return ret;
375 }
376 
377 /**
378  * t4vf_get_vpd_params - retrieve device VPD paremeters
379  * @adapter: the adapter
380  *
381  * Retrives various device Vital Product Data parameters.  The parameters
382  * are stored in @adapter->params.vpd.
383  */
384 int t4vf_get_vpd_params(struct adapter *adapter)
385 {
386 	struct vpd_params *vpd_params = &adapter->params.vpd;
387 	u32 params[7], vals[7];
388 	int v;
389 
390 	params[0] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
391 		     V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_CCLK));
392 	v = t4vf_query_params(adapter, 1, params, vals);
393 	if (v != FW_SUCCESS)
394 		return v;
395 	vpd_params->cclk = vals[0];
396 	dev_debug(adapter, "%s: vpd_params->cclk = %u\n",
397 		  __func__, vpd_params->cclk);
398 	return 0;
399 }
400 
401 /**
402  * t4vf_get_dev_params - retrieve device paremeters
403  * @adapter: the adapter
404  *
405  * Retrives fw and tp version.
406  */
407 int t4vf_get_dev_params(struct adapter *adapter)
408 {
409 	u32 params[7], vals[7];
410 	int v;
411 
412 	params[0] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
413 		     V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_FWREV));
414 	params[1] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
415 		     V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_TPREV));
416 	v = t4vf_query_params(adapter, 2, params, vals);
417 	if (v != FW_SUCCESS)
418 		return v;
419 	adapter->params.fw_vers = vals[0];
420 	adapter->params.tp_vers = vals[1];
421 
422 	dev_info(adapter, "Firmware version: %u.%u.%u.%u\n",
423 		 G_FW_HDR_FW_VER_MAJOR(adapter->params.fw_vers),
424 		 G_FW_HDR_FW_VER_MINOR(adapter->params.fw_vers),
425 		 G_FW_HDR_FW_VER_MICRO(adapter->params.fw_vers),
426 		 G_FW_HDR_FW_VER_BUILD(adapter->params.fw_vers));
427 
428 	dev_info(adapter, "TP Microcode version: %u.%u.%u.%u\n",
429 		 G_FW_HDR_FW_VER_MAJOR(adapter->params.tp_vers),
430 		 G_FW_HDR_FW_VER_MINOR(adapter->params.tp_vers),
431 		 G_FW_HDR_FW_VER_MICRO(adapter->params.tp_vers),
432 		 G_FW_HDR_FW_VER_BUILD(adapter->params.tp_vers));
433 	return 0;
434 }
435 
436 /**
437  * t4vf_set_params - sets FW or device parameters
438  * @adapter: the adapter
439  * @nparams: the number of parameters
440  * @params: the parameter names
441  * @vals: the parameter values
442  *
443  * Sets the values of firmware or device parameters.  Up to 7 parameters
444  * can be specified at once.
445  */
446 int t4vf_set_params(struct adapter *adapter, unsigned int nparams,
447 		    const u32 *params, const u32 *vals)
448 {
449 	struct fw_params_param *p;
450 	struct fw_params_cmd cmd;
451 	unsigned int i;
452 	size_t len16;
453 
454 	if (nparams > 7)
455 		return -EINVAL;
456 
457 	memset(&cmd, 0, sizeof(cmd));
458 	cmd.op_to_vfn = cpu_to_be32(V_FW_CMD_OP(FW_PARAMS_CMD) |
459 				    F_FW_CMD_REQUEST |
460 				    F_FW_CMD_WRITE);
461 	len16 = DIV_ROUND_UP(offsetof(struct fw_params_cmd,
462 			     param[nparams]), 16);
463 	cmd.retval_len16 = cpu_to_be32(V_FW_CMD_LEN16(len16));
464 	for (i = 0, p = &cmd.param[0]; i < nparams; i++, p++) {
465 		p->mnem = cpu_to_be32(*params++);
466 		p->val = cpu_to_be32(*vals++);
467 	}
468 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
469 }
470 
471 unsigned int t4vf_get_pf_from_vf(struct adapter *adapter)
472 {
473 	u32 whoami;
474 
475 	whoami = t4_read_reg(adapter, T4VF_PL_BASE_ADDR + A_PL_VF_WHOAMI);
476 	return (CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5 ?
477 			G_SOURCEPF(whoami) : G_T6_SOURCEPF(whoami));
478 }
479 
480 /**
481  * t4vf_get_rss_glb_config - retrieve adapter RSS Global Configuration
482  * @adapter: the adapter
483  *
484  * Retrieves global RSS mode and parameters with which we have to live
485  * and stores them in the @adapter's RSS parameters.
486  */
487 int t4vf_get_rss_glb_config(struct adapter *adapter)
488 {
489 	struct rss_params *rss = &adapter->params.rss;
490 	struct fw_rss_glb_config_cmd cmd, rpl;
491 	int v;
492 
493 	/*
494 	 * Execute an RSS Global Configuration read command to retrieve
495 	 * our RSS configuration.
496 	 */
497 	memset(&cmd, 0, sizeof(cmd));
498 	cmd.op_to_write = cpu_to_be32(V_FW_CMD_OP(FW_RSS_GLB_CONFIG_CMD) |
499 				      F_FW_CMD_REQUEST |
500 				      F_FW_CMD_READ);
501 	cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
502 	v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
503 	if (v != FW_SUCCESS)
504 		return v;
505 
506 	/*
507 	 * Translate the big-endian RSS Global Configuration into our
508 	 * cpu-endian format based on the RSS mode.  We also do first level
509 	 * filtering at this point to weed out modes which don't support
510 	 * VF Drivers ...
511 	 */
512 	rss->mode = G_FW_RSS_GLB_CONFIG_CMD_MODE
513 			(be32_to_cpu(rpl.u.manual.mode_pkd));
514 	switch (rss->mode) {
515 	case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
516 		u32 word = be32_to_cpu
517 				(rpl.u.basicvirtual.synmapen_to_hashtoeplitz);
518 
519 		rss->u.basicvirtual.synmapen =
520 			((word & F_FW_RSS_GLB_CONFIG_CMD_SYNMAPEN) != 0);
521 		rss->u.basicvirtual.syn4tupenipv6 =
522 			((word & F_FW_RSS_GLB_CONFIG_CMD_SYN4TUPENIPV6) != 0);
523 		rss->u.basicvirtual.syn2tupenipv6 =
524 			((word & F_FW_RSS_GLB_CONFIG_CMD_SYN2TUPENIPV6) != 0);
525 		rss->u.basicvirtual.syn4tupenipv4 =
526 			((word & F_FW_RSS_GLB_CONFIG_CMD_SYN4TUPENIPV4) != 0);
527 		rss->u.basicvirtual.syn2tupenipv4 =
528 			((word & F_FW_RSS_GLB_CONFIG_CMD_SYN2TUPENIPV4) != 0);
529 		rss->u.basicvirtual.ofdmapen =
530 			((word & F_FW_RSS_GLB_CONFIG_CMD_OFDMAPEN) != 0);
531 		rss->u.basicvirtual.tnlmapen =
532 			((word & F_FW_RSS_GLB_CONFIG_CMD_TNLMAPEN) != 0);
533 		rss->u.basicvirtual.tnlalllookup =
534 			((word  & F_FW_RSS_GLB_CONFIG_CMD_TNLALLLKP) != 0);
535 		rss->u.basicvirtual.hashtoeplitz =
536 			((word & F_FW_RSS_GLB_CONFIG_CMD_HASHTOEPLITZ) != 0);
537 
538 		/* we need at least Tunnel Map Enable to be set */
539 		if (!rss->u.basicvirtual.tnlmapen)
540 			return -EINVAL;
541 		break;
542 	}
543 
544 	default:
545 		/* all unknown/unsupported RSS modes result in an error */
546 		return -EINVAL;
547 	}
548 	return 0;
549 }
550 
551 /**
552  * t4vf_get_vfres - retrieve VF resource limits
553  * @adapter: the adapter
554  *
555  * Retrieves configured resource limits and capabilities for a virtual
556  * function.  The results are stored in @adapter->vfres.
557  */
558 int t4vf_get_vfres(struct adapter *adapter)
559 {
560 	struct vf_resources *vfres = &adapter->params.vfres;
561 	struct fw_pfvf_cmd cmd, rpl;
562 	u32 word;
563 	int v;
564 
565 	/*
566 	 * Execute PFVF Read command to get VF resource limits; bail out early
567 	 * with error on command failure.
568 	 */
569 	memset(&cmd, 0, sizeof(cmd));
570 	cmd.op_to_vfn = cpu_to_be32(V_FW_CMD_OP(FW_PFVF_CMD) |
571 				    F_FW_CMD_REQUEST |
572 				    F_FW_CMD_READ);
573 	cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
574 	v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
575 	if (v != FW_SUCCESS)
576 		return v;
577 
578 	/*
579 	 * Extract VF resource limits and return success.
580 	 */
581 	word = be32_to_cpu(rpl.niqflint_niq);
582 	vfres->niqflint = G_FW_PFVF_CMD_NIQFLINT(word);
583 	vfres->niq = G_FW_PFVF_CMD_NIQ(word);
584 
585 	word = be32_to_cpu(rpl.type_to_neq);
586 	vfres->neq = G_FW_PFVF_CMD_NEQ(word);
587 	vfres->pmask = G_FW_PFVF_CMD_PMASK(word);
588 
589 	word = be32_to_cpu(rpl.tc_to_nexactf);
590 	vfres->tc = G_FW_PFVF_CMD_TC(word);
591 	vfres->nvi = G_FW_PFVF_CMD_NVI(word);
592 	vfres->nexactf = G_FW_PFVF_CMD_NEXACTF(word);
593 
594 	word = be32_to_cpu(rpl.r_caps_to_nethctrl);
595 	vfres->r_caps = G_FW_PFVF_CMD_R_CAPS(word);
596 	vfres->wx_caps = G_FW_PFVF_CMD_WX_CAPS(word);
597 	vfres->nethctrl = G_FW_PFVF_CMD_NETHCTRL(word);
598 	return 0;
599 }
600 
601 /**
602  * t4vf_get_port_stats_fw - collect "port" statistics via Firmware
603  * @adapter: the adapter
604  * @pidx: the port index
605  * @s: the stats structure to fill
606  *
607  * Collect statistics for the "port"'s Virtual Interface via Firmware
608  * commands.
609  */
610 static int t4vf_get_port_stats_fw(struct adapter *adapter, int pidx,
611 				  struct port_stats *p)
612 {
613 	struct port_info *pi = adap2pinfo(adapter, pidx);
614 	unsigned int rem = VI_VF_NUM_STATS;
615 	struct fw_vi_stats_vf fwstats;
616 	__be64 *fwsp = (__be64 *)&fwstats;
617 
618 	/*
619 	 * Grab the Virtual Interface statistics a chunk at a time via mailbox
620 	 * commands.  We could use a Work Request and get all of them at once
621 	 * but that's an asynchronous interface which is awkward to use.
622 	 */
623 	while (rem) {
624 		unsigned int ix = VI_VF_NUM_STATS - rem;
625 		unsigned int nstats = min(6U, rem);
626 		struct fw_vi_stats_cmd cmd, rpl;
627 		size_t len = (offsetof(struct fw_vi_stats_cmd, u) +
628 			      sizeof(struct fw_vi_stats_ctl));
629 		size_t len16 = DIV_ROUND_UP(len, 16);
630 		int ret;
631 
632 		memset(&cmd, 0, sizeof(cmd));
633 		cmd.op_to_viid = cpu_to_be32(V_FW_CMD_OP(FW_VI_STATS_CMD) |
634 					     V_FW_VI_STATS_CMD_VIID(pi->viid) |
635 					     F_FW_CMD_REQUEST |
636 					     F_FW_CMD_READ);
637 		cmd.retval_len16 = cpu_to_be32(V_FW_CMD_LEN16(len16));
638 		cmd.u.ctl.nstats_ix =
639 			cpu_to_be16(V_FW_VI_STATS_CMD_IX(ix) |
640 				    V_FW_VI_STATS_CMD_NSTATS(nstats));
641 		ret = t4vf_wr_mbox_ns(adapter, &cmd, len, &rpl);
642 		if (ret != FW_SUCCESS)
643 			return ret;
644 
645 		memcpy(fwsp, &rpl.u.ctl.stat0, sizeof(__be64) * nstats);
646 
647 		rem -= nstats;
648 		fwsp += nstats;
649 	}
650 
651 	/*
652 	 * Translate firmware statistics into host native statistics.
653 	 */
654 	p->tx_octets = be64_to_cpu(fwstats.tx_bcast_bytes) +
655 		       be64_to_cpu(fwstats.tx_mcast_bytes) +
656 		       be64_to_cpu(fwstats.tx_ucast_bytes);
657 	p->tx_bcast_frames = be64_to_cpu(fwstats.tx_bcast_frames);
658 	p->tx_mcast_frames = be64_to_cpu(fwstats.tx_mcast_frames);
659 	p->tx_ucast_frames = be64_to_cpu(fwstats.tx_ucast_frames);
660 	p->tx_drop = be64_to_cpu(fwstats.tx_drop_frames);
661 
662 	p->rx_bcast_frames = be64_to_cpu(fwstats.rx_bcast_frames);
663 	p->rx_mcast_frames = be64_to_cpu(fwstats.rx_mcast_frames);
664 	p->rx_ucast_frames = be64_to_cpu(fwstats.rx_ucast_frames);
665 	p->rx_len_err = be64_to_cpu(fwstats.rx_err_frames);
666 
667 	return 0;
668 }
669 
670 /**
671  *      t4vf_get_port_stats - collect "port" statistics
672  *      @adapter: the adapter
673  *      @pidx: the port index
674  *      @s: the stats structure to fill
675  *
676  *      Collect statistics for the "port"'s Virtual Interface.
677  */
678 void t4vf_get_port_stats(struct adapter *adapter, int pidx,
679 			 struct port_stats *p)
680 {
681 	/*
682 	 * If this is not the first Virtual Interface for our Virtual
683 	 * Function, we need to use Firmware commands to retrieve its
684 	 * MPS statistics.
685 	 */
686 	if (pidx != 0)
687 		t4vf_get_port_stats_fw(adapter, pidx, p);
688 
689 	/*
690 	 * But for the first VI, we can grab its statistics via the MPS
691 	 * register mapped into the VF register space.
692 	 */
693 #define GET_STAT(name) \
694 	t4_read_reg64(adapter, \
695 			T4VF_MPS_BASE_ADDR + A_MPS_VF_STAT_##name##_L)
696 	p->tx_octets = GET_STAT(TX_VF_BCAST_BYTES) +
697 		       GET_STAT(TX_VF_MCAST_BYTES) +
698 		       GET_STAT(TX_VF_UCAST_BYTES);
699 	p->tx_bcast_frames = GET_STAT(TX_VF_BCAST_FRAMES);
700 	p->tx_mcast_frames = GET_STAT(TX_VF_MCAST_FRAMES);
701 	p->tx_ucast_frames = GET_STAT(TX_VF_UCAST_FRAMES);
702 	p->tx_drop = GET_STAT(TX_VF_DROP_FRAMES);
703 
704 	p->rx_bcast_frames = GET_STAT(RX_VF_BCAST_FRAMES);
705 	p->rx_mcast_frames = GET_STAT(RX_VF_MCAST_FRAMES);
706 	p->rx_ucast_frames = GET_STAT(RX_VF_UCAST_FRAMES);
707 
708 	p->rx_len_err = GET_STAT(RX_VF_ERR_FRAMES);
709 #undef GET_STAT
710 }
711 
712 static int t4vf_alloc_vi(struct adapter *adapter, int port_id)
713 {
714 	struct fw_vi_cmd cmd, rpl;
715 	int v;
716 
717 	/*
718 	 * Execute a VI command to allocate Virtual Interface and return its
719 	 * VIID.
720 	 */
721 	memset(&cmd, 0, sizeof(cmd));
722 	cmd.op_to_vfn = cpu_to_be32(V_FW_CMD_OP(FW_VI_CMD) |
723 				    F_FW_CMD_REQUEST |
724 				    F_FW_CMD_WRITE |
725 				    F_FW_CMD_EXEC);
726 	cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(cmd) |
727 					 F_FW_VI_CMD_ALLOC);
728 	cmd.portid_pkd = V_FW_VI_CMD_PORTID(port_id);
729 	v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
730 	if (v != FW_SUCCESS)
731 		return v;
732 	return G_FW_VI_CMD_VIID(be16_to_cpu(rpl.type_to_viid));
733 }
734 
735 int t4vf_port_init(struct adapter *adapter)
736 {
737 	struct fw_port_cmd port_cmd, port_rpl, rpl;
738 	struct fw_vi_cmd vi_cmd, vi_rpl;
739 	u32 param, val, pcaps, acaps;
740 	enum fw_port_type port_type;
741 	int mdio_addr;
742 	int ret, i;
743 
744 	param = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_PFVF) |
745 		 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_PFVF_PORT_CAPS32));
746 	val = 1;
747 	ret = t4vf_set_params(adapter, 1, &param, &val);
748 	if (ret < 0)
749 		return ret;
750 
751 	for_each_port(adapter, i) {
752 		struct port_info *p = adap2pinfo(adapter, i);
753 		u32 lstatus32;
754 
755 		ret = t4vf_alloc_vi(adapter, p->port_id);
756 		if (ret < 0) {
757 			dev_err(&pdev->dev, "cannot allocate VI for port %d:"
758 				" err=%d\n", p->port_id, ret);
759 			return ret;
760 		}
761 		p->viid = ret;
762 
763 		/*
764 		 * Execute a VI Read command to get our Virtual Interface
765 		 * information like MAC address, etc.
766 		 */
767 		memset(&vi_cmd, 0, sizeof(vi_cmd));
768 		vi_cmd.op_to_vfn = cpu_to_be32(V_FW_CMD_OP(FW_VI_CMD) |
769 					       F_FW_CMD_REQUEST |
770 					       F_FW_CMD_READ);
771 		vi_cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(vi_cmd));
772 		vi_cmd.type_to_viid = cpu_to_be16(V_FW_VI_CMD_VIID(p->viid));
773 		ret = t4vf_wr_mbox(adapter, &vi_cmd, sizeof(vi_cmd), &vi_rpl);
774 		if (ret != FW_SUCCESS)
775 			return ret;
776 
777 		p->rss_size = G_FW_VI_CMD_RSSSIZE
778 				(be16_to_cpu(vi_rpl.norss_rsssize));
779 		t4_os_set_hw_addr(adapter, i, vi_rpl.mac);
780 
781 		/*
782 		 * If we don't have read access to our port information, we're
783 		 * done now.  Else, execute a PORT Read command to get it ...
784 		 */
785 		if (!(adapter->params.vfres.r_caps & FW_CMD_CAP_PORT))
786 			return 0;
787 
788 		memset(&port_cmd, 0, sizeof(port_cmd));
789 		port_cmd.op_to_portid =
790 			cpu_to_be32(V_FW_CMD_OP(FW_PORT_CMD) |
791 				    F_FW_CMD_REQUEST | F_FW_CMD_READ |
792 				    V_FW_PORT_CMD_PORTID(p->port_id));
793 		val = FW_PORT_ACTION_GET_PORT_INFO32;
794 		port_cmd.action_to_len16 =
795 			cpu_to_be32(V_FW_PORT_CMD_ACTION(val) |
796 				    FW_LEN16(port_cmd));
797 		ret = t4vf_wr_mbox(adapter, &port_cmd, sizeof(port_cmd),
798 				   &port_rpl);
799 		if (ret != FW_SUCCESS)
800 			return ret;
801 
802 		/*
803 		 * Extract the various fields from the Port Information message.
804 		 */
805 		rpl = port_rpl;
806 		lstatus32 = be32_to_cpu(rpl.u.info32.lstatus32_to_cbllen32);
807 
808 		port_type = G_FW_PORT_CMD_PORTTYPE32(lstatus32);
809 		mdio_addr = (lstatus32 & F_FW_PORT_CMD_MDIOCAP32) ?
810 			    (int)G_FW_PORT_CMD_MDIOADDR32(lstatus32) : -1;
811 		pcaps = be32_to_cpu(port_rpl.u.info32.pcaps32);
812 		acaps = be32_to_cpu(port_rpl.u.info32.acaps32);
813 
814 		t4_init_link_config(p, pcaps, acaps, mdio_addr, port_type,
815 				    FW_PORT_MOD_TYPE_NA);
816 	}
817 	return 0;
818 }
819