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/version.h>
37 #include <linux/pci.h>
38 
39 #include "t4vf_common.h"
40 #include "t4vf_defs.h"
41 
42 #include "../cxgb4/t4_regs.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  */
t4vf_wait_dev_ready(struct adapter * adapter)50 int __devinit 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  */
get_mbox_rpl(struct adapter * adapter,__be64 * rpl,int size,u32 mbox_data)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  */
dump_mbox(struct adapter * adapter,const char * tag,u32 mbox_data)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  */
t4vf_wr_mbox_core(struct adapter * adapter,const void * cmd,int size,void * rpl,bool sleep_ok)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;
124 	int i, ms, delay_idx;
125 	const __be64 *p;
126 	u32 mbox_data = T4VF_MBDATA_BASE_ADDR;
127 	u32 mbox_ctl = T4VF_CIM_BASE_ADDR + CIM_VF_EXT_MAILBOX_CTRL;
128 
129 	/*
130 	 * Commands must be multiples of 16 bytes in length and may not be
131 	 * larger than the size of the Mailbox Data register array.
132 	 */
133 	if ((size % 16) != 0 ||
134 	    size > NUM_CIM_VF_MAILBOX_DATA_INSTANCES * 4)
135 		return -EINVAL;
136 
137 	/*
138 	 * Loop trying to get ownership of the mailbox.  Return an error
139 	 * if we can't gain ownership.
140 	 */
141 	v = MBOWNER_GET(t4_read_reg(adapter, mbox_ctl));
142 	for (i = 0; v == MBOX_OWNER_NONE && i < 3; i++)
143 		v = MBOWNER_GET(t4_read_reg(adapter, mbox_ctl));
144 	if (v != MBOX_OWNER_DRV)
145 		return v == MBOX_OWNER_FW ? -EBUSY : -ETIMEDOUT;
146 
147 	/*
148 	 * Write the command array into the Mailbox Data register array and
149 	 * transfer ownership of the mailbox to the firmware.
150 	 *
151 	 * For the VFs, the Mailbox Data "registers" are actually backed by
152 	 * T4's "MA" interface rather than PL Registers (as is the case for
153 	 * the PFs).  Because these are in different coherency domains, the
154 	 * write to the VF's PL-register-backed Mailbox Control can race in
155 	 * front of the writes to the MA-backed VF Mailbox Data "registers".
156 	 * So we need to do a read-back on at least one byte of the VF Mailbox
157 	 * Data registers before doing the write to the VF Mailbox Control
158 	 * register.
159 	 */
160 	for (i = 0, p = cmd; i < size; i += 8)
161 		t4_write_reg64(adapter, mbox_data + i, be64_to_cpu(*p++));
162 	t4_read_reg(adapter, mbox_data);         /* flush write */
163 
164 	t4_write_reg(adapter, mbox_ctl,
165 		     MBMSGVALID | MBOWNER(MBOX_OWNER_FW));
166 	t4_read_reg(adapter, mbox_ctl);          /* flush write */
167 
168 	/*
169 	 * Spin waiting for firmware to acknowledge processing our command.
170 	 */
171 	delay_idx = 0;
172 	ms = delay[0];
173 
174 	for (i = 0; i < FW_CMD_MAX_TIMEOUT; i += ms) {
175 		if (sleep_ok) {
176 			ms = delay[delay_idx];
177 			if (delay_idx < ARRAY_SIZE(delay) - 1)
178 				delay_idx++;
179 			msleep(ms);
180 		} else
181 			mdelay(ms);
182 
183 		/*
184 		 * If we're the owner, see if this is the reply we wanted.
185 		 */
186 		v = t4_read_reg(adapter, mbox_ctl);
187 		if (MBOWNER_GET(v) == MBOX_OWNER_DRV) {
188 			/*
189 			 * If the Message Valid bit isn't on, revoke ownership
190 			 * of the mailbox and continue waiting for our reply.
191 			 */
192 			if ((v & MBMSGVALID) == 0) {
193 				t4_write_reg(adapter, mbox_ctl,
194 					     MBOWNER(MBOX_OWNER_NONE));
195 				continue;
196 			}
197 
198 			/*
199 			 * We now have our reply.  Extract the command return
200 			 * value, copy the reply back to our caller's buffer
201 			 * (if specified) and revoke ownership of the mailbox.
202 			 * We return the (negated) firmware command return
203 			 * code (this depends on FW_SUCCESS == 0).
204 			 */
205 
206 			/* return value in low-order little-endian word */
207 			v = t4_read_reg(adapter, mbox_data);
208 			if (FW_CMD_RETVAL_GET(v))
209 				dump_mbox(adapter, "FW Error", mbox_data);
210 
211 			if (rpl) {
212 				/* request bit in high-order BE word */
213 				WARN_ON((be32_to_cpu(*(const u32 *)cmd)
214 					 & FW_CMD_REQUEST) == 0);
215 				get_mbox_rpl(adapter, rpl, size, mbox_data);
216 				WARN_ON((be32_to_cpu(*(u32 *)rpl)
217 					 & FW_CMD_REQUEST) != 0);
218 			}
219 			t4_write_reg(adapter, mbox_ctl,
220 				     MBOWNER(MBOX_OWNER_NONE));
221 			return -FW_CMD_RETVAL_GET(v);
222 		}
223 	}
224 
225 	/*
226 	 * We timed out.  Return the error ...
227 	 */
228 	dump_mbox(adapter, "FW Timeout", mbox_data);
229 	return -ETIMEDOUT;
230 }
231 
232 /**
233  *	hash_mac_addr - return the hash value of a MAC address
234  *	@addr: the 48-bit Ethernet MAC address
235  *
236  *	Hashes a MAC address according to the hash function used by hardware
237  *	inexact (hash) address matching.
238  */
hash_mac_addr(const u8 * addr)239 static int hash_mac_addr(const u8 *addr)
240 {
241 	u32 a = ((u32)addr[0] << 16) | ((u32)addr[1] << 8) | addr[2];
242 	u32 b = ((u32)addr[3] << 16) | ((u32)addr[4] << 8) | addr[5];
243 	a ^= b;
244 	a ^= (a >> 12);
245 	a ^= (a >> 6);
246 	return a & 0x3f;
247 }
248 
249 /**
250  *	init_link_config - initialize a link's SW state
251  *	@lc: structure holding the link state
252  *	@caps: link capabilities
253  *
254  *	Initializes the SW state maintained for each link, including the link's
255  *	capabilities and default speed/flow-control/autonegotiation settings.
256  */
init_link_config(struct link_config * lc,unsigned int caps)257 static void __devinit init_link_config(struct link_config *lc,
258 				       unsigned int caps)
259 {
260 	lc->supported = caps;
261 	lc->requested_speed = 0;
262 	lc->speed = 0;
263 	lc->requested_fc = lc->fc = PAUSE_RX | PAUSE_TX;
264 	if (lc->supported & SUPPORTED_Autoneg) {
265 		lc->advertising = lc->supported;
266 		lc->autoneg = AUTONEG_ENABLE;
267 		lc->requested_fc |= PAUSE_AUTONEG;
268 	} else {
269 		lc->advertising = 0;
270 		lc->autoneg = AUTONEG_DISABLE;
271 	}
272 }
273 
274 /**
275  *	t4vf_port_init - initialize port hardware/software state
276  *	@adapter: the adapter
277  *	@pidx: the adapter port index
278  */
t4vf_port_init(struct adapter * adapter,int pidx)279 int __devinit t4vf_port_init(struct adapter *adapter, int pidx)
280 {
281 	struct port_info *pi = adap2pinfo(adapter, pidx);
282 	struct fw_vi_cmd vi_cmd, vi_rpl;
283 	struct fw_port_cmd port_cmd, port_rpl;
284 	int v;
285 	u32 word;
286 
287 	/*
288 	 * Execute a VI Read command to get our Virtual Interface information
289 	 * like MAC address, etc.
290 	 */
291 	memset(&vi_cmd, 0, sizeof(vi_cmd));
292 	vi_cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_VI_CMD) |
293 				       FW_CMD_REQUEST |
294 				       FW_CMD_READ);
295 	vi_cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(vi_cmd));
296 	vi_cmd.type_viid = cpu_to_be16(FW_VI_CMD_VIID(pi->viid));
297 	v = t4vf_wr_mbox(adapter, &vi_cmd, sizeof(vi_cmd), &vi_rpl);
298 	if (v)
299 		return v;
300 
301 	BUG_ON(pi->port_id != FW_VI_CMD_PORTID_GET(vi_rpl.portid_pkd));
302 	pi->rss_size = FW_VI_CMD_RSSSIZE_GET(be16_to_cpu(vi_rpl.rsssize_pkd));
303 	t4_os_set_hw_addr(adapter, pidx, vi_rpl.mac);
304 
305 	/*
306 	 * If we don't have read access to our port information, we're done
307 	 * now.  Otherwise, execute a PORT Read command to get it ...
308 	 */
309 	if (!(adapter->params.vfres.r_caps & FW_CMD_CAP_PORT))
310 		return 0;
311 
312 	memset(&port_cmd, 0, sizeof(port_cmd));
313 	port_cmd.op_to_portid = cpu_to_be32(FW_CMD_OP(FW_PORT_CMD) |
314 					    FW_CMD_REQUEST |
315 					    FW_CMD_READ |
316 					    FW_PORT_CMD_PORTID(pi->port_id));
317 	port_cmd.action_to_len16 =
318 		cpu_to_be32(FW_PORT_CMD_ACTION(FW_PORT_ACTION_GET_PORT_INFO) |
319 			    FW_LEN16(port_cmd));
320 	v = t4vf_wr_mbox(adapter, &port_cmd, sizeof(port_cmd), &port_rpl);
321 	if (v)
322 		return v;
323 
324 	v = 0;
325 	word = be16_to_cpu(port_rpl.u.info.pcap);
326 	if (word & FW_PORT_CAP_SPEED_100M)
327 		v |= SUPPORTED_100baseT_Full;
328 	if (word & FW_PORT_CAP_SPEED_1G)
329 		v |= SUPPORTED_1000baseT_Full;
330 	if (word & FW_PORT_CAP_SPEED_10G)
331 		v |= SUPPORTED_10000baseT_Full;
332 	if (word & FW_PORT_CAP_ANEG)
333 		v |= SUPPORTED_Autoneg;
334 	init_link_config(&pi->link_cfg, v);
335 
336 	return 0;
337 }
338 
339 /**
340  *      t4vf_fw_reset - issue a reset to FW
341  *      @adapter: the adapter
342  *
343  *	Issues a reset command to FW.  For a Physical Function this would
344  *	result in the Firmware reseting all of its state.  For a Virtual
345  *	Function this just resets the state associated with the VF.
346  */
t4vf_fw_reset(struct adapter * adapter)347 int t4vf_fw_reset(struct adapter *adapter)
348 {
349 	struct fw_reset_cmd cmd;
350 
351 	memset(&cmd, 0, sizeof(cmd));
352 	cmd.op_to_write = cpu_to_be32(FW_CMD_OP(FW_RESET_CMD) |
353 				      FW_CMD_WRITE);
354 	cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
355 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
356 }
357 
358 /**
359  *	t4vf_query_params - query FW or device parameters
360  *	@adapter: the adapter
361  *	@nparams: the number of parameters
362  *	@params: the parameter names
363  *	@vals: the parameter values
364  *
365  *	Reads the values of firmware or device parameters.  Up to 7 parameters
366  *	can be queried at once.
367  */
t4vf_query_params(struct adapter * adapter,unsigned int nparams,const u32 * params,u32 * vals)368 int t4vf_query_params(struct adapter *adapter, unsigned int nparams,
369 		      const u32 *params, u32 *vals)
370 {
371 	int i, ret;
372 	struct fw_params_cmd cmd, rpl;
373 	struct fw_params_param *p;
374 	size_t len16;
375 
376 	if (nparams > 7)
377 		return -EINVAL;
378 
379 	memset(&cmd, 0, sizeof(cmd));
380 	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_PARAMS_CMD) |
381 				    FW_CMD_REQUEST |
382 				    FW_CMD_READ);
383 	len16 = DIV_ROUND_UP(offsetof(struct fw_params_cmd,
384 				      param[nparams].mnem), 16);
385 	cmd.retval_len16 = cpu_to_be32(FW_CMD_LEN16(len16));
386 	for (i = 0, p = &cmd.param[0]; i < nparams; i++, p++)
387 		p->mnem = htonl(*params++);
388 
389 	ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
390 	if (ret == 0)
391 		for (i = 0, p = &rpl.param[0]; i < nparams; i++, p++)
392 			*vals++ = be32_to_cpu(p->val);
393 	return ret;
394 }
395 
396 /**
397  *	t4vf_set_params - sets FW or device parameters
398  *	@adapter: the adapter
399  *	@nparams: the number of parameters
400  *	@params: the parameter names
401  *	@vals: the parameter values
402  *
403  *	Sets the values of firmware or device parameters.  Up to 7 parameters
404  *	can be specified at once.
405  */
t4vf_set_params(struct adapter * adapter,unsigned int nparams,const u32 * params,const u32 * vals)406 int t4vf_set_params(struct adapter *adapter, unsigned int nparams,
407 		    const u32 *params, const u32 *vals)
408 {
409 	int i;
410 	struct fw_params_cmd cmd;
411 	struct fw_params_param *p;
412 	size_t len16;
413 
414 	if (nparams > 7)
415 		return -EINVAL;
416 
417 	memset(&cmd, 0, sizeof(cmd));
418 	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_PARAMS_CMD) |
419 				    FW_CMD_REQUEST |
420 				    FW_CMD_WRITE);
421 	len16 = DIV_ROUND_UP(offsetof(struct fw_params_cmd,
422 				      param[nparams]), 16);
423 	cmd.retval_len16 = cpu_to_be32(FW_CMD_LEN16(len16));
424 	for (i = 0, p = &cmd.param[0]; i < nparams; i++, p++) {
425 		p->mnem = cpu_to_be32(*params++);
426 		p->val = cpu_to_be32(*vals++);
427 	}
428 
429 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
430 }
431 
432 /**
433  *	t4vf_get_sge_params - retrieve adapter Scatter gather Engine parameters
434  *	@adapter: the adapter
435  *
436  *	Retrieves various core SGE parameters in the form of hardware SGE
437  *	register values.  The caller is responsible for decoding these as
438  *	needed.  The SGE parameters are stored in @adapter->params.sge.
439  */
t4vf_get_sge_params(struct adapter * adapter)440 int t4vf_get_sge_params(struct adapter *adapter)
441 {
442 	struct sge_params *sge_params = &adapter->params.sge;
443 	u32 params[7], vals[7];
444 	int v;
445 
446 	params[0] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
447 		     FW_PARAMS_PARAM_XYZ(SGE_CONTROL));
448 	params[1] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
449 		     FW_PARAMS_PARAM_XYZ(SGE_HOST_PAGE_SIZE));
450 	params[2] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
451 		     FW_PARAMS_PARAM_XYZ(SGE_FL_BUFFER_SIZE0));
452 	params[3] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
453 		     FW_PARAMS_PARAM_XYZ(SGE_FL_BUFFER_SIZE1));
454 	params[4] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
455 		     FW_PARAMS_PARAM_XYZ(SGE_TIMER_VALUE_0_AND_1));
456 	params[5] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
457 		     FW_PARAMS_PARAM_XYZ(SGE_TIMER_VALUE_2_AND_3));
458 	params[6] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
459 		     FW_PARAMS_PARAM_XYZ(SGE_TIMER_VALUE_4_AND_5));
460 	v = t4vf_query_params(adapter, 7, params, vals);
461 	if (v)
462 		return v;
463 	sge_params->sge_control = vals[0];
464 	sge_params->sge_host_page_size = vals[1];
465 	sge_params->sge_fl_buffer_size[0] = vals[2];
466 	sge_params->sge_fl_buffer_size[1] = vals[3];
467 	sge_params->sge_timer_value_0_and_1 = vals[4];
468 	sge_params->sge_timer_value_2_and_3 = vals[5];
469 	sge_params->sge_timer_value_4_and_5 = vals[6];
470 
471 	params[0] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
472 		     FW_PARAMS_PARAM_XYZ(SGE_INGRESS_RX_THRESHOLD));
473 	v = t4vf_query_params(adapter, 1, params, vals);
474 	if (v)
475 		return v;
476 	sge_params->sge_ingress_rx_threshold = vals[0];
477 
478 	return 0;
479 }
480 
481 /**
482  *	t4vf_get_vpd_params - retrieve device VPD paremeters
483  *	@adapter: the adapter
484  *
485  *	Retrives various device Vital Product Data parameters.  The parameters
486  *	are stored in @adapter->params.vpd.
487  */
t4vf_get_vpd_params(struct adapter * adapter)488 int t4vf_get_vpd_params(struct adapter *adapter)
489 {
490 	struct vpd_params *vpd_params = &adapter->params.vpd;
491 	u32 params[7], vals[7];
492 	int v;
493 
494 	params[0] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
495 		     FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_CCLK));
496 	v = t4vf_query_params(adapter, 1, params, vals);
497 	if (v)
498 		return v;
499 	vpd_params->cclk = vals[0];
500 
501 	return 0;
502 }
503 
504 /**
505  *	t4vf_get_dev_params - retrieve device paremeters
506  *	@adapter: the adapter
507  *
508  *	Retrives various device parameters.  The parameters are stored in
509  *	@adapter->params.dev.
510  */
t4vf_get_dev_params(struct adapter * adapter)511 int t4vf_get_dev_params(struct adapter *adapter)
512 {
513 	struct dev_params *dev_params = &adapter->params.dev;
514 	u32 params[7], vals[7];
515 	int v;
516 
517 	params[0] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
518 		     FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_FWREV));
519 	params[1] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
520 		     FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_TPREV));
521 	v = t4vf_query_params(adapter, 2, params, vals);
522 	if (v)
523 		return v;
524 	dev_params->fwrev = vals[0];
525 	dev_params->tprev = vals[1];
526 
527 	return 0;
528 }
529 
530 /**
531  *	t4vf_get_rss_glb_config - retrieve adapter RSS Global Configuration
532  *	@adapter: the adapter
533  *
534  *	Retrieves global RSS mode and parameters with which we have to live
535  *	and stores them in the @adapter's RSS parameters.
536  */
t4vf_get_rss_glb_config(struct adapter * adapter)537 int t4vf_get_rss_glb_config(struct adapter *adapter)
538 {
539 	struct rss_params *rss = &adapter->params.rss;
540 	struct fw_rss_glb_config_cmd cmd, rpl;
541 	int v;
542 
543 	/*
544 	 * Execute an RSS Global Configuration read command to retrieve
545 	 * our RSS configuration.
546 	 */
547 	memset(&cmd, 0, sizeof(cmd));
548 	cmd.op_to_write = cpu_to_be32(FW_CMD_OP(FW_RSS_GLB_CONFIG_CMD) |
549 				      FW_CMD_REQUEST |
550 				      FW_CMD_READ);
551 	cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
552 	v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
553 	if (v)
554 		return v;
555 
556 	/*
557 	 * Transate the big-endian RSS Global Configuration into our
558 	 * cpu-endian format based on the RSS mode.  We also do first level
559 	 * filtering at this point to weed out modes which don't support
560 	 * VF Drivers ...
561 	 */
562 	rss->mode = FW_RSS_GLB_CONFIG_CMD_MODE_GET(
563 			be32_to_cpu(rpl.u.manual.mode_pkd));
564 	switch (rss->mode) {
565 	case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
566 		u32 word = be32_to_cpu(
567 				rpl.u.basicvirtual.synmapen_to_hashtoeplitz);
568 
569 		rss->u.basicvirtual.synmapen =
570 			((word & FW_RSS_GLB_CONFIG_CMD_SYNMAPEN) != 0);
571 		rss->u.basicvirtual.syn4tupenipv6 =
572 			((word & FW_RSS_GLB_CONFIG_CMD_SYN4TUPENIPV6) != 0);
573 		rss->u.basicvirtual.syn2tupenipv6 =
574 			((word & FW_RSS_GLB_CONFIG_CMD_SYN2TUPENIPV6) != 0);
575 		rss->u.basicvirtual.syn4tupenipv4 =
576 			((word & FW_RSS_GLB_CONFIG_CMD_SYN4TUPENIPV4) != 0);
577 		rss->u.basicvirtual.syn2tupenipv4 =
578 			((word & FW_RSS_GLB_CONFIG_CMD_SYN2TUPENIPV4) != 0);
579 
580 		rss->u.basicvirtual.ofdmapen =
581 			((word & FW_RSS_GLB_CONFIG_CMD_OFDMAPEN) != 0);
582 
583 		rss->u.basicvirtual.tnlmapen =
584 			((word & FW_RSS_GLB_CONFIG_CMD_TNLMAPEN) != 0);
585 		rss->u.basicvirtual.tnlalllookup =
586 			((word  & FW_RSS_GLB_CONFIG_CMD_TNLALLLKP) != 0);
587 
588 		rss->u.basicvirtual.hashtoeplitz =
589 			((word & FW_RSS_GLB_CONFIG_CMD_HASHTOEPLITZ) != 0);
590 
591 		/* we need at least Tunnel Map Enable to be set */
592 		if (!rss->u.basicvirtual.tnlmapen)
593 			return -EINVAL;
594 		break;
595 	}
596 
597 	default:
598 		/* all unknown/unsupported RSS modes result in an error */
599 		return -EINVAL;
600 	}
601 
602 	return 0;
603 }
604 
605 /**
606  *	t4vf_get_vfres - retrieve VF resource limits
607  *	@adapter: the adapter
608  *
609  *	Retrieves configured resource limits and capabilities for a virtual
610  *	function.  The results are stored in @adapter->vfres.
611  */
t4vf_get_vfres(struct adapter * adapter)612 int t4vf_get_vfres(struct adapter *adapter)
613 {
614 	struct vf_resources *vfres = &adapter->params.vfres;
615 	struct fw_pfvf_cmd cmd, rpl;
616 	int v;
617 	u32 word;
618 
619 	/*
620 	 * Execute PFVF Read command to get VF resource limits; bail out early
621 	 * with error on command failure.
622 	 */
623 	memset(&cmd, 0, sizeof(cmd));
624 	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_PFVF_CMD) |
625 				    FW_CMD_REQUEST |
626 				    FW_CMD_READ);
627 	cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
628 	v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
629 	if (v)
630 		return v;
631 
632 	/*
633 	 * Extract VF resource limits and return success.
634 	 */
635 	word = be32_to_cpu(rpl.niqflint_niq);
636 	vfres->niqflint = FW_PFVF_CMD_NIQFLINT_GET(word);
637 	vfres->niq = FW_PFVF_CMD_NIQ_GET(word);
638 
639 	word = be32_to_cpu(rpl.type_to_neq);
640 	vfres->neq = FW_PFVF_CMD_NEQ_GET(word);
641 	vfres->pmask = FW_PFVF_CMD_PMASK_GET(word);
642 
643 	word = be32_to_cpu(rpl.tc_to_nexactf);
644 	vfres->tc = FW_PFVF_CMD_TC_GET(word);
645 	vfres->nvi = FW_PFVF_CMD_NVI_GET(word);
646 	vfres->nexactf = FW_PFVF_CMD_NEXACTF_GET(word);
647 
648 	word = be32_to_cpu(rpl.r_caps_to_nethctrl);
649 	vfres->r_caps = FW_PFVF_CMD_R_CAPS_GET(word);
650 	vfres->wx_caps = FW_PFVF_CMD_WX_CAPS_GET(word);
651 	vfres->nethctrl = FW_PFVF_CMD_NETHCTRL_GET(word);
652 
653 	return 0;
654 }
655 
656 /**
657  *	t4vf_read_rss_vi_config - read a VI's RSS configuration
658  *	@adapter: the adapter
659  *	@viid: Virtual Interface ID
660  *	@config: pointer to host-native VI RSS Configuration buffer
661  *
662  *	Reads the Virtual Interface's RSS configuration information and
663  *	translates it into CPU-native format.
664  */
t4vf_read_rss_vi_config(struct adapter * adapter,unsigned int viid,union rss_vi_config * config)665 int t4vf_read_rss_vi_config(struct adapter *adapter, unsigned int viid,
666 			    union rss_vi_config *config)
667 {
668 	struct fw_rss_vi_config_cmd cmd, rpl;
669 	int v;
670 
671 	memset(&cmd, 0, sizeof(cmd));
672 	cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_RSS_VI_CONFIG_CMD) |
673 				     FW_CMD_REQUEST |
674 				     FW_CMD_READ |
675 				     FW_RSS_VI_CONFIG_CMD_VIID(viid));
676 	cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
677 	v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
678 	if (v)
679 		return v;
680 
681 	switch (adapter->params.rss.mode) {
682 	case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
683 		u32 word = be32_to_cpu(rpl.u.basicvirtual.defaultq_to_udpen);
684 
685 		config->basicvirtual.ip6fourtupen =
686 			((word & FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN) != 0);
687 		config->basicvirtual.ip6twotupen =
688 			((word & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN) != 0);
689 		config->basicvirtual.ip4fourtupen =
690 			((word & FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN) != 0);
691 		config->basicvirtual.ip4twotupen =
692 			((word & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN) != 0);
693 		config->basicvirtual.udpen =
694 			((word & FW_RSS_VI_CONFIG_CMD_UDPEN) != 0);
695 		config->basicvirtual.defaultq =
696 			FW_RSS_VI_CONFIG_CMD_DEFAULTQ_GET(word);
697 		break;
698 	}
699 
700 	default:
701 		return -EINVAL;
702 	}
703 
704 	return 0;
705 }
706 
707 /**
708  *	t4vf_write_rss_vi_config - write a VI's RSS configuration
709  *	@adapter: the adapter
710  *	@viid: Virtual Interface ID
711  *	@config: pointer to host-native VI RSS Configuration buffer
712  *
713  *	Write the Virtual Interface's RSS configuration information
714  *	(translating it into firmware-native format before writing).
715  */
t4vf_write_rss_vi_config(struct adapter * adapter,unsigned int viid,union rss_vi_config * config)716 int t4vf_write_rss_vi_config(struct adapter *adapter, unsigned int viid,
717 			     union rss_vi_config *config)
718 {
719 	struct fw_rss_vi_config_cmd cmd, rpl;
720 
721 	memset(&cmd, 0, sizeof(cmd));
722 	cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_RSS_VI_CONFIG_CMD) |
723 				     FW_CMD_REQUEST |
724 				     FW_CMD_WRITE |
725 				     FW_RSS_VI_CONFIG_CMD_VIID(viid));
726 	cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
727 	switch (adapter->params.rss.mode) {
728 	case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
729 		u32 word = 0;
730 
731 		if (config->basicvirtual.ip6fourtupen)
732 			word |= FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
733 		if (config->basicvirtual.ip6twotupen)
734 			word |= FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN;
735 		if (config->basicvirtual.ip4fourtupen)
736 			word |= FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
737 		if (config->basicvirtual.ip4twotupen)
738 			word |= FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN;
739 		if (config->basicvirtual.udpen)
740 			word |= FW_RSS_VI_CONFIG_CMD_UDPEN;
741 		word |= FW_RSS_VI_CONFIG_CMD_DEFAULTQ(
742 				config->basicvirtual.defaultq);
743 		cmd.u.basicvirtual.defaultq_to_udpen = cpu_to_be32(word);
744 		break;
745 	}
746 
747 	default:
748 		return -EINVAL;
749 	}
750 
751 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
752 }
753 
754 /**
755  *	t4vf_config_rss_range - configure a portion of the RSS mapping table
756  *	@adapter: the adapter
757  *	@viid: Virtual Interface of RSS Table Slice
758  *	@start: starting entry in the table to write
759  *	@n: how many table entries to write
760  *	@rspq: values for the "Response Queue" (Ingress Queue) lookup table
761  *	@nrspq: number of values in @rspq
762  *
763  *	Programs the selected part of the VI's RSS mapping table with the
764  *	provided values.  If @nrspq < @n the supplied values are used repeatedly
765  *	until the full table range is populated.
766  *
767  *	The caller must ensure the values in @rspq are in the range 0..1023.
768  */
t4vf_config_rss_range(struct adapter * adapter,unsigned int viid,int start,int n,const u16 * rspq,int nrspq)769 int t4vf_config_rss_range(struct adapter *adapter, unsigned int viid,
770 			  int start, int n, const u16 *rspq, int nrspq)
771 {
772 	const u16 *rsp = rspq;
773 	const u16 *rsp_end = rspq+nrspq;
774 	struct fw_rss_ind_tbl_cmd cmd;
775 
776 	/*
777 	 * Initialize firmware command template to write the RSS table.
778 	 */
779 	memset(&cmd, 0, sizeof(cmd));
780 	cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_RSS_IND_TBL_CMD) |
781 				     FW_CMD_REQUEST |
782 				     FW_CMD_WRITE |
783 				     FW_RSS_IND_TBL_CMD_VIID(viid));
784 	cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
785 
786 	/*
787 	 * Each firmware RSS command can accommodate up to 32 RSS Ingress
788 	 * Queue Identifiers.  These Ingress Queue IDs are packed three to
789 	 * a 32-bit word as 10-bit values with the upper remaining 2 bits
790 	 * reserved.
791 	 */
792 	while (n > 0) {
793 		__be32 *qp = &cmd.iq0_to_iq2;
794 		int nq = min(n, 32);
795 		int ret;
796 
797 		/*
798 		 * Set up the firmware RSS command header to send the next
799 		 * "nq" Ingress Queue IDs to the firmware.
800 		 */
801 		cmd.niqid = cpu_to_be16(nq);
802 		cmd.startidx = cpu_to_be16(start);
803 
804 		/*
805 		 * "nq" more done for the start of the next loop.
806 		 */
807 		start += nq;
808 		n -= nq;
809 
810 		/*
811 		 * While there are still Ingress Queue IDs to stuff into the
812 		 * current firmware RSS command, retrieve them from the
813 		 * Ingress Queue ID array and insert them into the command.
814 		 */
815 		while (nq > 0) {
816 			/*
817 			 * Grab up to the next 3 Ingress Queue IDs (wrapping
818 			 * around the Ingress Queue ID array if necessary) and
819 			 * insert them into the firmware RSS command at the
820 			 * current 3-tuple position within the commad.
821 			 */
822 			u16 qbuf[3];
823 			u16 *qbp = qbuf;
824 			int nqbuf = min(3, nq);
825 
826 			nq -= nqbuf;
827 			qbuf[0] = qbuf[1] = qbuf[2] = 0;
828 			while (nqbuf) {
829 				nqbuf--;
830 				*qbp++ = *rsp++;
831 				if (rsp >= rsp_end)
832 					rsp = rspq;
833 			}
834 			*qp++ = cpu_to_be32(FW_RSS_IND_TBL_CMD_IQ0(qbuf[0]) |
835 					    FW_RSS_IND_TBL_CMD_IQ1(qbuf[1]) |
836 					    FW_RSS_IND_TBL_CMD_IQ2(qbuf[2]));
837 		}
838 
839 		/*
840 		 * Send this portion of the RRS table update to the firmware;
841 		 * bail out on any errors.
842 		 */
843 		ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
844 		if (ret)
845 			return ret;
846 	}
847 	return 0;
848 }
849 
850 /**
851  *	t4vf_alloc_vi - allocate a virtual interface on a port
852  *	@adapter: the adapter
853  *	@port_id: physical port associated with the VI
854  *
855  *	Allocate a new Virtual Interface and bind it to the indicated
856  *	physical port.  Return the new Virtual Interface Identifier on
857  *	success, or a [negative] error number on failure.
858  */
t4vf_alloc_vi(struct adapter * adapter,int port_id)859 int t4vf_alloc_vi(struct adapter *adapter, int port_id)
860 {
861 	struct fw_vi_cmd cmd, rpl;
862 	int v;
863 
864 	/*
865 	 * Execute a VI command to allocate Virtual Interface and return its
866 	 * VIID.
867 	 */
868 	memset(&cmd, 0, sizeof(cmd));
869 	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_VI_CMD) |
870 				    FW_CMD_REQUEST |
871 				    FW_CMD_WRITE |
872 				    FW_CMD_EXEC);
873 	cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(cmd) |
874 					 FW_VI_CMD_ALLOC);
875 	cmd.portid_pkd = FW_VI_CMD_PORTID(port_id);
876 	v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
877 	if (v)
878 		return v;
879 
880 	return FW_VI_CMD_VIID_GET(be16_to_cpu(rpl.type_viid));
881 }
882 
883 /**
884  *	t4vf_free_vi -- free a virtual interface
885  *	@adapter: the adapter
886  *	@viid: the virtual interface identifier
887  *
888  *	Free a previously allocated Virtual Interface.  Return an error on
889  *	failure.
890  */
t4vf_free_vi(struct adapter * adapter,int viid)891 int t4vf_free_vi(struct adapter *adapter, int viid)
892 {
893 	struct fw_vi_cmd cmd;
894 
895 	/*
896 	 * Execute a VI command to free the Virtual Interface.
897 	 */
898 	memset(&cmd, 0, sizeof(cmd));
899 	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_VI_CMD) |
900 				    FW_CMD_REQUEST |
901 				    FW_CMD_EXEC);
902 	cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(cmd) |
903 					 FW_VI_CMD_FREE);
904 	cmd.type_viid = cpu_to_be16(FW_VI_CMD_VIID(viid));
905 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
906 }
907 
908 /**
909  *	t4vf_enable_vi - enable/disable a virtual interface
910  *	@adapter: the adapter
911  *	@viid: the Virtual Interface ID
912  *	@rx_en: 1=enable Rx, 0=disable Rx
913  *	@tx_en: 1=enable Tx, 0=disable Tx
914  *
915  *	Enables/disables a virtual interface.
916  */
t4vf_enable_vi(struct adapter * adapter,unsigned int viid,bool rx_en,bool tx_en)917 int t4vf_enable_vi(struct adapter *adapter, unsigned int viid,
918 		   bool rx_en, bool tx_en)
919 {
920 	struct fw_vi_enable_cmd cmd;
921 
922 	memset(&cmd, 0, sizeof(cmd));
923 	cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_ENABLE_CMD) |
924 				     FW_CMD_REQUEST |
925 				     FW_CMD_EXEC |
926 				     FW_VI_ENABLE_CMD_VIID(viid));
927 	cmd.ien_to_len16 = cpu_to_be32(FW_VI_ENABLE_CMD_IEN(rx_en) |
928 				       FW_VI_ENABLE_CMD_EEN(tx_en) |
929 				       FW_LEN16(cmd));
930 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
931 }
932 
933 /**
934  *	t4vf_identify_port - identify a VI's port by blinking its LED
935  *	@adapter: the adapter
936  *	@viid: the Virtual Interface ID
937  *	@nblinks: how many times to blink LED at 2.5 Hz
938  *
939  *	Identifies a VI's port by blinking its LED.
940  */
t4vf_identify_port(struct adapter * adapter,unsigned int viid,unsigned int nblinks)941 int t4vf_identify_port(struct adapter *adapter, unsigned int viid,
942 		       unsigned int nblinks)
943 {
944 	struct fw_vi_enable_cmd cmd;
945 
946 	memset(&cmd, 0, sizeof(cmd));
947 	cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_ENABLE_CMD) |
948 				     FW_CMD_REQUEST |
949 				     FW_CMD_EXEC |
950 				     FW_VI_ENABLE_CMD_VIID(viid));
951 	cmd.ien_to_len16 = cpu_to_be32(FW_VI_ENABLE_CMD_LED |
952 				       FW_LEN16(cmd));
953 	cmd.blinkdur = cpu_to_be16(nblinks);
954 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
955 }
956 
957 /**
958  *	t4vf_set_rxmode - set Rx properties of a virtual interface
959  *	@adapter: the adapter
960  *	@viid: the VI id
961  *	@mtu: the new MTU or -1 for no change
962  *	@promisc: 1 to enable promiscuous mode, 0 to disable it, -1 no change
963  *	@all_multi: 1 to enable all-multi mode, 0 to disable it, -1 no change
964  *	@bcast: 1 to enable broadcast Rx, 0 to disable it, -1 no change
965  *	@vlanex: 1 to enable hardware VLAN Tag extraction, 0 to disable it,
966  *		-1 no change
967  *
968  *	Sets Rx properties of a virtual interface.
969  */
t4vf_set_rxmode(struct adapter * adapter,unsigned int viid,int mtu,int promisc,int all_multi,int bcast,int vlanex,bool sleep_ok)970 int t4vf_set_rxmode(struct adapter *adapter, unsigned int viid,
971 		    int mtu, int promisc, int all_multi, int bcast, int vlanex,
972 		    bool sleep_ok)
973 {
974 	struct fw_vi_rxmode_cmd cmd;
975 
976 	/* convert to FW values */
977 	if (mtu < 0)
978 		mtu = FW_VI_RXMODE_CMD_MTU_MASK;
979 	if (promisc < 0)
980 		promisc = FW_VI_RXMODE_CMD_PROMISCEN_MASK;
981 	if (all_multi < 0)
982 		all_multi = FW_VI_RXMODE_CMD_ALLMULTIEN_MASK;
983 	if (bcast < 0)
984 		bcast = FW_VI_RXMODE_CMD_BROADCASTEN_MASK;
985 	if (vlanex < 0)
986 		vlanex = FW_VI_RXMODE_CMD_VLANEXEN_MASK;
987 
988 	memset(&cmd, 0, sizeof(cmd));
989 	cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_RXMODE_CMD) |
990 				     FW_CMD_REQUEST |
991 				     FW_CMD_WRITE |
992 				     FW_VI_RXMODE_CMD_VIID(viid));
993 	cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
994 	cmd.mtu_to_vlanexen =
995 		cpu_to_be32(FW_VI_RXMODE_CMD_MTU(mtu) |
996 			    FW_VI_RXMODE_CMD_PROMISCEN(promisc) |
997 			    FW_VI_RXMODE_CMD_ALLMULTIEN(all_multi) |
998 			    FW_VI_RXMODE_CMD_BROADCASTEN(bcast) |
999 			    FW_VI_RXMODE_CMD_VLANEXEN(vlanex));
1000 	return t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), NULL, sleep_ok);
1001 }
1002 
1003 /**
1004  *	t4vf_alloc_mac_filt - allocates exact-match filters for MAC addresses
1005  *	@adapter: the adapter
1006  *	@viid: the Virtual Interface Identifier
1007  *	@free: if true any existing filters for this VI id are first removed
1008  *	@naddr: the number of MAC addresses to allocate filters for (up to 7)
1009  *	@addr: the MAC address(es)
1010  *	@idx: where to store the index of each allocated filter
1011  *	@hash: pointer to hash address filter bitmap
1012  *	@sleep_ok: call is allowed to sleep
1013  *
1014  *	Allocates an exact-match filter for each of the supplied addresses and
1015  *	sets it to the corresponding address.  If @idx is not %NULL it should
1016  *	have at least @naddr entries, each of which will be set to the index of
1017  *	the filter allocated for the corresponding MAC address.  If a filter
1018  *	could not be allocated for an address its index is set to 0xffff.
1019  *	If @hash is not %NULL addresses that fail to allocate an exact filter
1020  *	are hashed and update the hash filter bitmap pointed at by @hash.
1021  *
1022  *	Returns a negative error number or the number of filters allocated.
1023  */
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)1024 int t4vf_alloc_mac_filt(struct adapter *adapter, unsigned int viid, bool free,
1025 			unsigned int naddr, const u8 **addr, u16 *idx,
1026 			u64 *hash, bool sleep_ok)
1027 {
1028 	int offset, ret = 0;
1029 	unsigned nfilters = 0;
1030 	unsigned int rem = naddr;
1031 	struct fw_vi_mac_cmd cmd, rpl;
1032 
1033 	if (naddr > FW_CLS_TCAM_NUM_ENTRIES)
1034 		return -EINVAL;
1035 
1036 	for (offset = 0; offset < naddr; /**/) {
1037 		unsigned int fw_naddr = (rem < ARRAY_SIZE(cmd.u.exact)
1038 					 ? rem
1039 					 : ARRAY_SIZE(cmd.u.exact));
1040 		size_t len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
1041 						     u.exact[fw_naddr]), 16);
1042 		struct fw_vi_mac_exact *p;
1043 		int i;
1044 
1045 		memset(&cmd, 0, sizeof(cmd));
1046 		cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_MAC_CMD) |
1047 					     FW_CMD_REQUEST |
1048 					     FW_CMD_WRITE |
1049 					     (free ? FW_CMD_EXEC : 0) |
1050 					     FW_VI_MAC_CMD_VIID(viid));
1051 		cmd.freemacs_to_len16 =
1052 			cpu_to_be32(FW_VI_MAC_CMD_FREEMACS(free) |
1053 				    FW_CMD_LEN16(len16));
1054 
1055 		for (i = 0, p = cmd.u.exact; i < fw_naddr; i++, p++) {
1056 			p->valid_to_idx = cpu_to_be16(
1057 				FW_VI_MAC_CMD_VALID |
1058 				FW_VI_MAC_CMD_IDX(FW_VI_MAC_ADD_MAC));
1059 			memcpy(p->macaddr, addr[offset+i], sizeof(p->macaddr));
1060 		}
1061 
1062 
1063 		ret = t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), &rpl,
1064 					sleep_ok);
1065 		if (ret && ret != -ENOMEM)
1066 			break;
1067 
1068 		for (i = 0, p = rpl.u.exact; i < fw_naddr; i++, p++) {
1069 			u16 index = FW_VI_MAC_CMD_IDX_GET(
1070 				be16_to_cpu(p->valid_to_idx));
1071 
1072 			if (idx)
1073 				idx[offset+i] =
1074 					(index >= FW_CLS_TCAM_NUM_ENTRIES
1075 					 ? 0xffff
1076 					 : index);
1077 			if (index < FW_CLS_TCAM_NUM_ENTRIES)
1078 				nfilters++;
1079 			else if (hash)
1080 				*hash |= (1ULL << hash_mac_addr(addr[offset+i]));
1081 		}
1082 
1083 		free = false;
1084 		offset += fw_naddr;
1085 		rem -= fw_naddr;
1086 	}
1087 
1088 	/*
1089 	 * If there were no errors or we merely ran out of room in our MAC
1090 	 * address arena, return the number of filters actually written.
1091 	 */
1092 	if (ret == 0 || ret == -ENOMEM)
1093 		ret = nfilters;
1094 	return ret;
1095 }
1096 
1097 /**
1098  *	t4vf_change_mac - modifies the exact-match filter for a MAC address
1099  *	@adapter: the adapter
1100  *	@viid: the Virtual Interface ID
1101  *	@idx: index of existing filter for old value of MAC address, or -1
1102  *	@addr: the new MAC address value
1103  *	@persist: if idx < 0, the new MAC allocation should be persistent
1104  *
1105  *	Modifies an exact-match filter and sets it to the new MAC address.
1106  *	Note that in general it is not possible to modify the value of a given
1107  *	filter so the generic way to modify an address filter is to free the
1108  *	one being used by the old address value and allocate a new filter for
1109  *	the new address value.  @idx can be -1 if the address is a new
1110  *	addition.
1111  *
1112  *	Returns a negative error number or the index of the filter with the new
1113  *	MAC value.
1114  */
t4vf_change_mac(struct adapter * adapter,unsigned int viid,int idx,const u8 * addr,bool persist)1115 int t4vf_change_mac(struct adapter *adapter, unsigned int viid,
1116 		    int idx, const u8 *addr, bool persist)
1117 {
1118 	int ret;
1119 	struct fw_vi_mac_cmd cmd, rpl;
1120 	struct fw_vi_mac_exact *p = &cmd.u.exact[0];
1121 	size_t len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
1122 					     u.exact[1]), 16);
1123 
1124 	/*
1125 	 * If this is a new allocation, determine whether it should be
1126 	 * persistent (across a "freemacs" operation) or not.
1127 	 */
1128 	if (idx < 0)
1129 		idx = persist ? FW_VI_MAC_ADD_PERSIST_MAC : FW_VI_MAC_ADD_MAC;
1130 
1131 	memset(&cmd, 0, sizeof(cmd));
1132 	cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_MAC_CMD) |
1133 				     FW_CMD_REQUEST |
1134 				     FW_CMD_WRITE |
1135 				     FW_VI_MAC_CMD_VIID(viid));
1136 	cmd.freemacs_to_len16 = cpu_to_be32(FW_CMD_LEN16(len16));
1137 	p->valid_to_idx = cpu_to_be16(FW_VI_MAC_CMD_VALID |
1138 				      FW_VI_MAC_CMD_IDX(idx));
1139 	memcpy(p->macaddr, addr, sizeof(p->macaddr));
1140 
1141 	ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
1142 	if (ret == 0) {
1143 		p = &rpl.u.exact[0];
1144 		ret = FW_VI_MAC_CMD_IDX_GET(be16_to_cpu(p->valid_to_idx));
1145 		if (ret >= FW_CLS_TCAM_NUM_ENTRIES)
1146 			ret = -ENOMEM;
1147 	}
1148 	return ret;
1149 }
1150 
1151 /**
1152  *	t4vf_set_addr_hash - program the MAC inexact-match hash filter
1153  *	@adapter: the adapter
1154  *	@viid: the Virtual Interface Identifier
1155  *	@ucast: whether the hash filter should also match unicast addresses
1156  *	@vec: the value to be written to the hash filter
1157  *	@sleep_ok: call is allowed to sleep
1158  *
1159  *	Sets the 64-bit inexact-match hash filter for a virtual interface.
1160  */
t4vf_set_addr_hash(struct adapter * adapter,unsigned int viid,bool ucast,u64 vec,bool sleep_ok)1161 int t4vf_set_addr_hash(struct adapter *adapter, unsigned int viid,
1162 		       bool ucast, u64 vec, bool sleep_ok)
1163 {
1164 	struct fw_vi_mac_cmd cmd;
1165 	size_t len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
1166 					     u.exact[0]), 16);
1167 
1168 	memset(&cmd, 0, sizeof(cmd));
1169 	cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_MAC_CMD) |
1170 				     FW_CMD_REQUEST |
1171 				     FW_CMD_WRITE |
1172 				     FW_VI_ENABLE_CMD_VIID(viid));
1173 	cmd.freemacs_to_len16 = cpu_to_be32(FW_VI_MAC_CMD_HASHVECEN |
1174 					    FW_VI_MAC_CMD_HASHUNIEN(ucast) |
1175 					    FW_CMD_LEN16(len16));
1176 	cmd.u.hash.hashvec = cpu_to_be64(vec);
1177 	return t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), NULL, sleep_ok);
1178 }
1179 
1180 /**
1181  *	t4vf_get_port_stats - collect "port" statistics
1182  *	@adapter: the adapter
1183  *	@pidx: the port index
1184  *	@s: the stats structure to fill
1185  *
1186  *	Collect statistics for the "port"'s Virtual Interface.
1187  */
t4vf_get_port_stats(struct adapter * adapter,int pidx,struct t4vf_port_stats * s)1188 int t4vf_get_port_stats(struct adapter *adapter, int pidx,
1189 			struct t4vf_port_stats *s)
1190 {
1191 	struct port_info *pi = adap2pinfo(adapter, pidx);
1192 	struct fw_vi_stats_vf fwstats;
1193 	unsigned int rem = VI_VF_NUM_STATS;
1194 	__be64 *fwsp = (__be64 *)&fwstats;
1195 
1196 	/*
1197 	 * Grab the Virtual Interface statistics a chunk at a time via mailbox
1198 	 * commands.  We could use a Work Request and get all of them at once
1199 	 * but that's an asynchronous interface which is awkward to use.
1200 	 */
1201 	while (rem) {
1202 		unsigned int ix = VI_VF_NUM_STATS - rem;
1203 		unsigned int nstats = min(6U, rem);
1204 		struct fw_vi_stats_cmd cmd, rpl;
1205 		size_t len = (offsetof(struct fw_vi_stats_cmd, u) +
1206 			      sizeof(struct fw_vi_stats_ctl));
1207 		size_t len16 = DIV_ROUND_UP(len, 16);
1208 		int ret;
1209 
1210 		memset(&cmd, 0, sizeof(cmd));
1211 		cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_STATS_CMD) |
1212 					     FW_VI_STATS_CMD_VIID(pi->viid) |
1213 					     FW_CMD_REQUEST |
1214 					     FW_CMD_READ);
1215 		cmd.retval_len16 = cpu_to_be32(FW_CMD_LEN16(len16));
1216 		cmd.u.ctl.nstats_ix =
1217 			cpu_to_be16(FW_VI_STATS_CMD_IX(ix) |
1218 				    FW_VI_STATS_CMD_NSTATS(nstats));
1219 		ret = t4vf_wr_mbox_ns(adapter, &cmd, len, &rpl);
1220 		if (ret)
1221 			return ret;
1222 
1223 		memcpy(fwsp, &rpl.u.ctl.stat0, sizeof(__be64) * nstats);
1224 
1225 		rem -= nstats;
1226 		fwsp += nstats;
1227 	}
1228 
1229 	/*
1230 	 * Translate firmware statistics into host native statistics.
1231 	 */
1232 	s->tx_bcast_bytes = be64_to_cpu(fwstats.tx_bcast_bytes);
1233 	s->tx_bcast_frames = be64_to_cpu(fwstats.tx_bcast_frames);
1234 	s->tx_mcast_bytes = be64_to_cpu(fwstats.tx_mcast_bytes);
1235 	s->tx_mcast_frames = be64_to_cpu(fwstats.tx_mcast_frames);
1236 	s->tx_ucast_bytes = be64_to_cpu(fwstats.tx_ucast_bytes);
1237 	s->tx_ucast_frames = be64_to_cpu(fwstats.tx_ucast_frames);
1238 	s->tx_drop_frames = be64_to_cpu(fwstats.tx_drop_frames);
1239 	s->tx_offload_bytes = be64_to_cpu(fwstats.tx_offload_bytes);
1240 	s->tx_offload_frames = be64_to_cpu(fwstats.tx_offload_frames);
1241 
1242 	s->rx_bcast_bytes = be64_to_cpu(fwstats.rx_bcast_bytes);
1243 	s->rx_bcast_frames = be64_to_cpu(fwstats.rx_bcast_frames);
1244 	s->rx_mcast_bytes = be64_to_cpu(fwstats.rx_mcast_bytes);
1245 	s->rx_mcast_frames = be64_to_cpu(fwstats.rx_mcast_frames);
1246 	s->rx_ucast_bytes = be64_to_cpu(fwstats.rx_ucast_bytes);
1247 	s->rx_ucast_frames = be64_to_cpu(fwstats.rx_ucast_frames);
1248 
1249 	s->rx_err_frames = be64_to_cpu(fwstats.rx_err_frames);
1250 
1251 	return 0;
1252 }
1253 
1254 /**
1255  *	t4vf_iq_free - free an ingress queue and its free lists
1256  *	@adapter: the adapter
1257  *	@iqtype: the ingress queue type (FW_IQ_TYPE_FL_INT_CAP, etc.)
1258  *	@iqid: ingress queue ID
1259  *	@fl0id: FL0 queue ID or 0xffff if no attached FL0
1260  *	@fl1id: FL1 queue ID or 0xffff if no attached FL1
1261  *
1262  *	Frees an ingress queue and its associated free lists, if any.
1263  */
t4vf_iq_free(struct adapter * adapter,unsigned int iqtype,unsigned int iqid,unsigned int fl0id,unsigned int fl1id)1264 int t4vf_iq_free(struct adapter *adapter, unsigned int iqtype,
1265 		 unsigned int iqid, unsigned int fl0id, unsigned int fl1id)
1266 {
1267 	struct fw_iq_cmd cmd;
1268 
1269 	memset(&cmd, 0, sizeof(cmd));
1270 	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_IQ_CMD) |
1271 				    FW_CMD_REQUEST |
1272 				    FW_CMD_EXEC);
1273 	cmd.alloc_to_len16 = cpu_to_be32(FW_IQ_CMD_FREE |
1274 					 FW_LEN16(cmd));
1275 	cmd.type_to_iqandstindex =
1276 		cpu_to_be32(FW_IQ_CMD_TYPE(iqtype));
1277 
1278 	cmd.iqid = cpu_to_be16(iqid);
1279 	cmd.fl0id = cpu_to_be16(fl0id);
1280 	cmd.fl1id = cpu_to_be16(fl1id);
1281 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
1282 }
1283 
1284 /**
1285  *	t4vf_eth_eq_free - free an Ethernet egress queue
1286  *	@adapter: the adapter
1287  *	@eqid: egress queue ID
1288  *
1289  *	Frees an Ethernet egress queue.
1290  */
t4vf_eth_eq_free(struct adapter * adapter,unsigned int eqid)1291 int t4vf_eth_eq_free(struct adapter *adapter, unsigned int eqid)
1292 {
1293 	struct fw_eq_eth_cmd cmd;
1294 
1295 	memset(&cmd, 0, sizeof(cmd));
1296 	cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_EQ_ETH_CMD) |
1297 				    FW_CMD_REQUEST |
1298 				    FW_CMD_EXEC);
1299 	cmd.alloc_to_len16 = cpu_to_be32(FW_EQ_ETH_CMD_FREE |
1300 					 FW_LEN16(cmd));
1301 	cmd.eqid_pkd = cpu_to_be32(FW_EQ_ETH_CMD_EQID(eqid));
1302 	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
1303 }
1304 
1305 /**
1306  *	t4vf_handle_fw_rpl - process a firmware reply message
1307  *	@adapter: the adapter
1308  *	@rpl: start of the firmware message
1309  *
1310  *	Processes a firmware message, such as link state change messages.
1311  */
t4vf_handle_fw_rpl(struct adapter * adapter,const __be64 * rpl)1312 int t4vf_handle_fw_rpl(struct adapter *adapter, const __be64 *rpl)
1313 {
1314 	const struct fw_cmd_hdr *cmd_hdr = (const struct fw_cmd_hdr *)rpl;
1315 	u8 opcode = FW_CMD_OP_GET(be32_to_cpu(cmd_hdr->hi));
1316 
1317 	switch (opcode) {
1318 	case FW_PORT_CMD: {
1319 		/*
1320 		 * Link/module state change message.
1321 		 */
1322 		const struct fw_port_cmd *port_cmd =
1323 			(const struct fw_port_cmd *)rpl;
1324 		u32 word;
1325 		int action, port_id, link_ok, speed, fc, pidx;
1326 
1327 		/*
1328 		 * Extract various fields from port status change message.
1329 		 */
1330 		action = FW_PORT_CMD_ACTION_GET(
1331 			be32_to_cpu(port_cmd->action_to_len16));
1332 		if (action != FW_PORT_ACTION_GET_PORT_INFO) {
1333 			dev_err(adapter->pdev_dev,
1334 				"Unknown firmware PORT reply action %x\n",
1335 				action);
1336 			break;
1337 		}
1338 
1339 		port_id = FW_PORT_CMD_PORTID_GET(
1340 			be32_to_cpu(port_cmd->op_to_portid));
1341 
1342 		word = be32_to_cpu(port_cmd->u.info.lstatus_to_modtype);
1343 		link_ok = (word & FW_PORT_CMD_LSTATUS) != 0;
1344 		speed = 0;
1345 		fc = 0;
1346 		if (word & FW_PORT_CMD_RXPAUSE)
1347 			fc |= PAUSE_RX;
1348 		if (word & FW_PORT_CMD_TXPAUSE)
1349 			fc |= PAUSE_TX;
1350 		if (word & FW_PORT_CMD_LSPEED(FW_PORT_CAP_SPEED_100M))
1351 			speed = SPEED_100;
1352 		else if (word & FW_PORT_CMD_LSPEED(FW_PORT_CAP_SPEED_1G))
1353 			speed = SPEED_1000;
1354 		else if (word & FW_PORT_CMD_LSPEED(FW_PORT_CAP_SPEED_10G))
1355 			speed = SPEED_10000;
1356 
1357 		/*
1358 		 * Scan all of our "ports" (Virtual Interfaces) looking for
1359 		 * those bound to the physical port which has changed.  If
1360 		 * our recorded state doesn't match the current state,
1361 		 * signal that change to the OS code.
1362 		 */
1363 		for_each_port(adapter, pidx) {
1364 			struct port_info *pi = adap2pinfo(adapter, pidx);
1365 			struct link_config *lc;
1366 
1367 			if (pi->port_id != port_id)
1368 				continue;
1369 
1370 			lc = &pi->link_cfg;
1371 			if (link_ok != lc->link_ok || speed != lc->speed ||
1372 			    fc != lc->fc) {
1373 				/* something changed */
1374 				lc->link_ok = link_ok;
1375 				lc->speed = speed;
1376 				lc->fc = fc;
1377 				t4vf_os_link_changed(adapter, pidx, link_ok);
1378 			}
1379 		}
1380 		break;
1381 	}
1382 
1383 	default:
1384 		dev_err(adapter->pdev_dev, "Unknown firmware reply %X\n",
1385 			opcode);
1386 	}
1387 	return 0;
1388 }
1389