blob: dc30d2852850941da1b865e5285f201846669069 [file] [log] [blame]
Casey Leedom16f8bd42010-06-25 12:12:54 +00001/*
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
Casey Leedom16f8bd42010-06-25 12:12:54 +000036#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/t4fw_api.h"
43
44/*
45 * Wait for the device to become ready (signified by our "who am I" register
46 * returning a value other than all 1's). Return an error if it doesn't
47 * become ready ...
48 */
Bill Pembertond289f862012-12-03 09:23:03 -050049int t4vf_wait_dev_ready(struct adapter *adapter)
Casey Leedom16f8bd42010-06-25 12:12:54 +000050{
51 const u32 whoami = T4VF_PL_BASE_ADDR + PL_VF_WHOAMI;
52 const u32 notready1 = 0xffffffff;
53 const u32 notready2 = 0xeeeeeeee;
54 u32 val;
55
56 val = t4_read_reg(adapter, whoami);
57 if (val != notready1 && val != notready2)
58 return 0;
59 msleep(500);
60 val = t4_read_reg(adapter, whoami);
61 if (val != notready1 && val != notready2)
62 return 0;
63 else
64 return -EIO;
65}
66
67/*
68 * Get the reply to a mailbox command and store it in @rpl in big-endian order
69 * (since the firmware data structures are specified in a big-endian layout).
70 */
71static void get_mbox_rpl(struct adapter *adapter, __be64 *rpl, int size,
72 u32 mbox_data)
73{
74 for ( ; size; size -= 8, mbox_data += 8)
75 *rpl++ = cpu_to_be64(t4_read_reg64(adapter, mbox_data));
76}
77
78/*
79 * Dump contents of mailbox with a leading tag.
80 */
81static void dump_mbox(struct adapter *adapter, const char *tag, u32 mbox_data)
82{
83 dev_err(adapter->pdev_dev,
84 "mbox %s: %llx %llx %llx %llx %llx %llx %llx %llx\n", tag,
85 (unsigned long long)t4_read_reg64(adapter, mbox_data + 0),
86 (unsigned long long)t4_read_reg64(adapter, mbox_data + 8),
87 (unsigned long long)t4_read_reg64(adapter, mbox_data + 16),
88 (unsigned long long)t4_read_reg64(adapter, mbox_data + 24),
89 (unsigned long long)t4_read_reg64(adapter, mbox_data + 32),
90 (unsigned long long)t4_read_reg64(adapter, mbox_data + 40),
91 (unsigned long long)t4_read_reg64(adapter, mbox_data + 48),
92 (unsigned long long)t4_read_reg64(adapter, mbox_data + 56));
93}
94
95/**
96 * t4vf_wr_mbox_core - send a command to FW through the mailbox
97 * @adapter: the adapter
98 * @cmd: the command to write
99 * @size: command length in bytes
100 * @rpl: where to optionally store the reply
101 * @sleep_ok: if true we may sleep while awaiting command completion
102 *
103 * Sends the given command to FW through the mailbox and waits for the
104 * FW to execute the command. If @rpl is not %NULL it is used to store
105 * the FW's reply to the command. The command and its optional reply
106 * are of the same length. FW can take up to 500 ms to respond.
107 * @sleep_ok determines whether we may sleep while awaiting the response.
108 * If sleeping is allowed we use progressive backoff otherwise we spin.
109 *
110 * The return value is 0 on success or a negative errno on failure. A
111 * failure can happen either because we are not able to execute the
112 * command or FW executes it but signals an error. In the latter case
113 * the return value is the error code indicated by FW (negated).
114 */
115int t4vf_wr_mbox_core(struct adapter *adapter, const void *cmd, int size,
116 void *rpl, bool sleep_ok)
117{
Joe Perches215faf92010-12-21 02:16:10 -0800118 static const int delay[] = {
Casey Leedom16f8bd42010-06-25 12:12:54 +0000119 1, 1, 3, 5, 10, 10, 20, 50, 100
120 };
121
122 u32 v;
123 int i, ms, delay_idx;
124 const __be64 *p;
125 u32 mbox_data = T4VF_MBDATA_BASE_ADDR;
126 u32 mbox_ctl = T4VF_CIM_BASE_ADDR + CIM_VF_EXT_MAILBOX_CTRL;
127
128 /*
129 * Commands must be multiples of 16 bytes in length and may not be
130 * larger than the size of the Mailbox Data register array.
131 */
132 if ((size % 16) != 0 ||
133 size > NUM_CIM_VF_MAILBOX_DATA_INSTANCES * 4)
134 return -EINVAL;
135
136 /*
137 * Loop trying to get ownership of the mailbox. Return an error
138 * if we can't gain ownership.
139 */
140 v = MBOWNER_GET(t4_read_reg(adapter, mbox_ctl));
141 for (i = 0; v == MBOX_OWNER_NONE && i < 3; i++)
142 v = MBOWNER_GET(t4_read_reg(adapter, mbox_ctl));
143 if (v != MBOX_OWNER_DRV)
144 return v == MBOX_OWNER_FW ? -EBUSY : -ETIMEDOUT;
145
146 /*
147 * Write the command array into the Mailbox Data register array and
148 * transfer ownership of the mailbox to the firmware.
Casey Leedom80ce3f672011-01-10 13:53:43 -0800149 *
150 * For the VFs, the Mailbox Data "registers" are actually backed by
151 * T4's "MA" interface rather than PL Registers (as is the case for
152 * the PFs). Because these are in different coherency domains, the
153 * write to the VF's PL-register-backed Mailbox Control can race in
154 * front of the writes to the MA-backed VF Mailbox Data "registers".
155 * So we need to do a read-back on at least one byte of the VF Mailbox
156 * Data registers before doing the write to the VF Mailbox Control
157 * register.
Casey Leedom16f8bd42010-06-25 12:12:54 +0000158 */
159 for (i = 0, p = cmd; i < size; i += 8)
160 t4_write_reg64(adapter, mbox_data + i, be64_to_cpu(*p++));
Casey Leedom80ce3f672011-01-10 13:53:43 -0800161 t4_read_reg(adapter, mbox_data); /* flush write */
162
Casey Leedom16f8bd42010-06-25 12:12:54 +0000163 t4_write_reg(adapter, mbox_ctl,
164 MBMSGVALID | MBOWNER(MBOX_OWNER_FW));
165 t4_read_reg(adapter, mbox_ctl); /* flush write */
166
167 /*
168 * Spin waiting for firmware to acknowledge processing our command.
169 */
170 delay_idx = 0;
171 ms = delay[0];
172
Casey Leedom05507692011-02-14 12:56:25 +0000173 for (i = 0; i < FW_CMD_MAX_TIMEOUT; i += ms) {
Casey Leedom16f8bd42010-06-25 12:12:54 +0000174 if (sleep_ok) {
175 ms = delay[delay_idx];
Casey Leedom024e6292010-07-19 17:51:46 -0700176 if (delay_idx < ARRAY_SIZE(delay) - 1)
Casey Leedom16f8bd42010-06-25 12:12:54 +0000177 delay_idx++;
178 msleep(ms);
179 } else
180 mdelay(ms);
181
182 /*
183 * If we're the owner, see if this is the reply we wanted.
184 */
185 v = t4_read_reg(adapter, mbox_ctl);
186 if (MBOWNER_GET(v) == MBOX_OWNER_DRV) {
187 /*
188 * If the Message Valid bit isn't on, revoke ownership
189 * of the mailbox and continue waiting for our reply.
190 */
191 if ((v & MBMSGVALID) == 0) {
192 t4_write_reg(adapter, mbox_ctl,
193 MBOWNER(MBOX_OWNER_NONE));
194 continue;
195 }
196
197 /*
198 * We now have our reply. Extract the command return
199 * value, copy the reply back to our caller's buffer
200 * (if specified) and revoke ownership of the mailbox.
201 * We return the (negated) firmware command return
202 * code (this depends on FW_SUCCESS == 0).
203 */
204
205 /* return value in low-order little-endian word */
206 v = t4_read_reg(adapter, mbox_data);
207 if (FW_CMD_RETVAL_GET(v))
208 dump_mbox(adapter, "FW Error", mbox_data);
209
210 if (rpl) {
211 /* request bit in high-order BE word */
212 WARN_ON((be32_to_cpu(*(const u32 *)cmd)
213 & FW_CMD_REQUEST) == 0);
214 get_mbox_rpl(adapter, rpl, size, mbox_data);
215 WARN_ON((be32_to_cpu(*(u32 *)rpl)
216 & FW_CMD_REQUEST) != 0);
217 }
218 t4_write_reg(adapter, mbox_ctl,
219 MBOWNER(MBOX_OWNER_NONE));
220 return -FW_CMD_RETVAL_GET(v);
221 }
222 }
223
224 /*
225 * We timed out. Return the error ...
226 */
227 dump_mbox(adapter, "FW Timeout", mbox_data);
228 return -ETIMEDOUT;
229}
230
231/**
232 * hash_mac_addr - return the hash value of a MAC address
233 * @addr: the 48-bit Ethernet MAC address
234 *
235 * Hashes a MAC address according to the hash function used by hardware
236 * inexact (hash) address matching.
237 */
238static int hash_mac_addr(const u8 *addr)
239{
240 u32 a = ((u32)addr[0] << 16) | ((u32)addr[1] << 8) | addr[2];
241 u32 b = ((u32)addr[3] << 16) | ((u32)addr[4] << 8) | addr[5];
242 a ^= b;
243 a ^= (a >> 12);
244 a ^= (a >> 6);
245 return a & 0x3f;
246}
247
248/**
249 * init_link_config - initialize a link's SW state
250 * @lc: structure holding the link state
251 * @caps: link capabilities
252 *
253 * Initializes the SW state maintained for each link, including the link's
254 * capabilities and default speed/flow-control/autonegotiation settings.
255 */
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +0000256static void init_link_config(struct link_config *lc, unsigned int caps)
Casey Leedom16f8bd42010-06-25 12:12:54 +0000257{
258 lc->supported = caps;
259 lc->requested_speed = 0;
260 lc->speed = 0;
261 lc->requested_fc = lc->fc = PAUSE_RX | PAUSE_TX;
262 if (lc->supported & SUPPORTED_Autoneg) {
263 lc->advertising = lc->supported;
264 lc->autoneg = AUTONEG_ENABLE;
265 lc->requested_fc |= PAUSE_AUTONEG;
266 } else {
267 lc->advertising = 0;
268 lc->autoneg = AUTONEG_DISABLE;
269 }
270}
271
272/**
273 * t4vf_port_init - initialize port hardware/software state
274 * @adapter: the adapter
275 * @pidx: the adapter port index
276 */
Bill Pembertond289f862012-12-03 09:23:03 -0500277int t4vf_port_init(struct adapter *adapter, int pidx)
Casey Leedom16f8bd42010-06-25 12:12:54 +0000278{
279 struct port_info *pi = adap2pinfo(adapter, pidx);
280 struct fw_vi_cmd vi_cmd, vi_rpl;
281 struct fw_port_cmd port_cmd, port_rpl;
282 int v;
283 u32 word;
284
285 /*
286 * Execute a VI Read command to get our Virtual Interface information
287 * like MAC address, etc.
288 */
289 memset(&vi_cmd, 0, sizeof(vi_cmd));
290 vi_cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_VI_CMD) |
291 FW_CMD_REQUEST |
292 FW_CMD_READ);
293 vi_cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(vi_cmd));
294 vi_cmd.type_viid = cpu_to_be16(FW_VI_CMD_VIID(pi->viid));
295 v = t4vf_wr_mbox(adapter, &vi_cmd, sizeof(vi_cmd), &vi_rpl);
296 if (v)
297 return v;
298
299 BUG_ON(pi->port_id != FW_VI_CMD_PORTID_GET(vi_rpl.portid_pkd));
300 pi->rss_size = FW_VI_CMD_RSSSIZE_GET(be16_to_cpu(vi_rpl.rsssize_pkd));
301 t4_os_set_hw_addr(adapter, pidx, vi_rpl.mac);
302
303 /*
304 * If we don't have read access to our port information, we're done
305 * now. Otherwise, execute a PORT Read command to get it ...
306 */
307 if (!(adapter->params.vfres.r_caps & FW_CMD_CAP_PORT))
308 return 0;
309
310 memset(&port_cmd, 0, sizeof(port_cmd));
311 port_cmd.op_to_portid = cpu_to_be32(FW_CMD_OP(FW_PORT_CMD) |
312 FW_CMD_REQUEST |
313 FW_CMD_READ |
314 FW_PORT_CMD_PORTID(pi->port_id));
315 port_cmd.action_to_len16 =
316 cpu_to_be32(FW_PORT_CMD_ACTION(FW_PORT_ACTION_GET_PORT_INFO) |
317 FW_LEN16(port_cmd));
318 v = t4vf_wr_mbox(adapter, &port_cmd, sizeof(port_cmd), &port_rpl);
319 if (v)
320 return v;
321
322 v = 0;
323 word = be16_to_cpu(port_rpl.u.info.pcap);
324 if (word & FW_PORT_CAP_SPEED_100M)
325 v |= SUPPORTED_100baseT_Full;
326 if (word & FW_PORT_CAP_SPEED_1G)
327 v |= SUPPORTED_1000baseT_Full;
328 if (word & FW_PORT_CAP_SPEED_10G)
329 v |= SUPPORTED_10000baseT_Full;
Hariprasad Shenai897d55d2014-10-09 05:48:46 +0530330 if (word & FW_PORT_CAP_SPEED_40G)
331 v |= SUPPORTED_40000baseSR4_Full;
Casey Leedom16f8bd42010-06-25 12:12:54 +0000332 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/**
Casey Leedome68e6132010-11-11 09:06:53 +0000340 * 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 */
347int 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/**
Casey Leedom16f8bd42010-06-25 12:12:54 +0000359 * 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 */
stephen hemmingerde5b8672013-12-18 14:16:47 -0800368static int t4vf_query_params(struct adapter *adapter, unsigned int nparams,
369 const u32 *params, u32 *vals)
Casey Leedom16f8bd42010-06-25 12:12:54 +0000370{
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 */
406int 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 */
440int 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
Hariprasad Shenaice8f4072014-11-07 17:06:30 +0530471 /* T4 uses a single control field to specify both the PCIe Padding and
472 * Packing Boundary. T5 introduced the ability to specify these
473 * separately with the Padding Boundary in SGE_CONTROL and and Packing
474 * Boundary in SGE_CONTROL2. So for T5 and later we need to grab
475 * SGE_CONTROL in order to determine how ingress packet data will be
476 * laid out in Packed Buffer Mode. Unfortunately, older versions of
477 * the firmware won't let us retrieve SGE_CONTROL2 so if we get a
478 * failure grabbing it we throw an error since we can't figure out the
479 * right value.
480 */
481 if (!is_t4(adapter->params.chip)) {
482 params[0] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
483 FW_PARAMS_PARAM_XYZ(SGE_CONTROL2_A));
484 v = t4vf_query_params(adapter, 1, params, vals);
485 if (v != FW_SUCCESS) {
486 dev_err(adapter->pdev_dev,
487 "Unable to get SGE Control2; "
488 "probably old firmware.\n");
489 return v;
490 }
491 sge_params->sge_control2 = vals[0];
492 }
493
Casey Leedom16f8bd42010-06-25 12:12:54 +0000494 params[0] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
495 FW_PARAMS_PARAM_XYZ(SGE_INGRESS_RX_THRESHOLD));
496 v = t4vf_query_params(adapter, 1, params, vals);
497 if (v)
498 return v;
499 sge_params->sge_ingress_rx_threshold = vals[0];
500
501 return 0;
502}
503
504/**
505 * t4vf_get_vpd_params - retrieve device VPD paremeters
506 * @adapter: the adapter
507 *
508 * Retrives various device Vital Product Data parameters. The parameters
509 * are stored in @adapter->params.vpd.
510 */
511int t4vf_get_vpd_params(struct adapter *adapter)
512{
513 struct vpd_params *vpd_params = &adapter->params.vpd;
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_CCLK));
519 v = t4vf_query_params(adapter, 1, params, vals);
520 if (v)
521 return v;
522 vpd_params->cclk = vals[0];
523
524 return 0;
525}
526
527/**
528 * t4vf_get_dev_params - retrieve device paremeters
529 * @adapter: the adapter
530 *
531 * Retrives various device parameters. The parameters are stored in
532 * @adapter->params.dev.
533 */
534int t4vf_get_dev_params(struct adapter *adapter)
535{
536 struct dev_params *dev_params = &adapter->params.dev;
537 u32 params[7], vals[7];
538 int v;
539
540 params[0] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
541 FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_FWREV));
542 params[1] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
543 FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_TPREV));
544 v = t4vf_query_params(adapter, 2, params, vals);
545 if (v)
546 return v;
547 dev_params->fwrev = vals[0];
548 dev_params->tprev = vals[1];
549
550 return 0;
551}
552
553/**
554 * t4vf_get_rss_glb_config - retrieve adapter RSS Global Configuration
555 * @adapter: the adapter
556 *
557 * Retrieves global RSS mode and parameters with which we have to live
558 * and stores them in the @adapter's RSS parameters.
559 */
560int t4vf_get_rss_glb_config(struct adapter *adapter)
561{
562 struct rss_params *rss = &adapter->params.rss;
563 struct fw_rss_glb_config_cmd cmd, rpl;
564 int v;
565
566 /*
567 * Execute an RSS Global Configuration read command to retrieve
568 * our RSS configuration.
569 */
570 memset(&cmd, 0, sizeof(cmd));
571 cmd.op_to_write = cpu_to_be32(FW_CMD_OP(FW_RSS_GLB_CONFIG_CMD) |
572 FW_CMD_REQUEST |
573 FW_CMD_READ);
574 cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
575 v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
576 if (v)
577 return v;
578
579 /*
580 * Transate the big-endian RSS Global Configuration into our
581 * cpu-endian format based on the RSS mode. We also do first level
582 * filtering at this point to weed out modes which don't support
583 * VF Drivers ...
584 */
585 rss->mode = FW_RSS_GLB_CONFIG_CMD_MODE_GET(
586 be32_to_cpu(rpl.u.manual.mode_pkd));
587 switch (rss->mode) {
588 case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
589 u32 word = be32_to_cpu(
590 rpl.u.basicvirtual.synmapen_to_hashtoeplitz);
591
592 rss->u.basicvirtual.synmapen =
593 ((word & FW_RSS_GLB_CONFIG_CMD_SYNMAPEN) != 0);
594 rss->u.basicvirtual.syn4tupenipv6 =
595 ((word & FW_RSS_GLB_CONFIG_CMD_SYN4TUPENIPV6) != 0);
596 rss->u.basicvirtual.syn2tupenipv6 =
597 ((word & FW_RSS_GLB_CONFIG_CMD_SYN2TUPENIPV6) != 0);
598 rss->u.basicvirtual.syn4tupenipv4 =
599 ((word & FW_RSS_GLB_CONFIG_CMD_SYN4TUPENIPV4) != 0);
600 rss->u.basicvirtual.syn2tupenipv4 =
601 ((word & FW_RSS_GLB_CONFIG_CMD_SYN2TUPENIPV4) != 0);
602
603 rss->u.basicvirtual.ofdmapen =
604 ((word & FW_RSS_GLB_CONFIG_CMD_OFDMAPEN) != 0);
605
606 rss->u.basicvirtual.tnlmapen =
607 ((word & FW_RSS_GLB_CONFIG_CMD_TNLMAPEN) != 0);
608 rss->u.basicvirtual.tnlalllookup =
609 ((word & FW_RSS_GLB_CONFIG_CMD_TNLALLLKP) != 0);
610
611 rss->u.basicvirtual.hashtoeplitz =
612 ((word & FW_RSS_GLB_CONFIG_CMD_HASHTOEPLITZ) != 0);
613
614 /* we need at least Tunnel Map Enable to be set */
615 if (!rss->u.basicvirtual.tnlmapen)
616 return -EINVAL;
617 break;
618 }
619
620 default:
621 /* all unknown/unsupported RSS modes result in an error */
622 return -EINVAL;
623 }
624
625 return 0;
626}
627
628/**
629 * t4vf_get_vfres - retrieve VF resource limits
630 * @adapter: the adapter
631 *
632 * Retrieves configured resource limits and capabilities for a virtual
633 * function. The results are stored in @adapter->vfres.
634 */
635int t4vf_get_vfres(struct adapter *adapter)
636{
637 struct vf_resources *vfres = &adapter->params.vfres;
638 struct fw_pfvf_cmd cmd, rpl;
639 int v;
640 u32 word;
641
642 /*
643 * Execute PFVF Read command to get VF resource limits; bail out early
644 * with error on command failure.
645 */
646 memset(&cmd, 0, sizeof(cmd));
647 cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_PFVF_CMD) |
648 FW_CMD_REQUEST |
649 FW_CMD_READ);
650 cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
651 v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
652 if (v)
653 return v;
654
655 /*
656 * Extract VF resource limits and return success.
657 */
658 word = be32_to_cpu(rpl.niqflint_niq);
659 vfres->niqflint = FW_PFVF_CMD_NIQFLINT_GET(word);
660 vfres->niq = FW_PFVF_CMD_NIQ_GET(word);
661
662 word = be32_to_cpu(rpl.type_to_neq);
663 vfres->neq = FW_PFVF_CMD_NEQ_GET(word);
664 vfres->pmask = FW_PFVF_CMD_PMASK_GET(word);
665
666 word = be32_to_cpu(rpl.tc_to_nexactf);
667 vfres->tc = FW_PFVF_CMD_TC_GET(word);
668 vfres->nvi = FW_PFVF_CMD_NVI_GET(word);
669 vfres->nexactf = FW_PFVF_CMD_NEXACTF_GET(word);
670
671 word = be32_to_cpu(rpl.r_caps_to_nethctrl);
672 vfres->r_caps = FW_PFVF_CMD_R_CAPS_GET(word);
673 vfres->wx_caps = FW_PFVF_CMD_WX_CAPS_GET(word);
674 vfres->nethctrl = FW_PFVF_CMD_NETHCTRL_GET(word);
675
676 return 0;
677}
678
679/**
680 * t4vf_read_rss_vi_config - read a VI's RSS configuration
681 * @adapter: the adapter
682 * @viid: Virtual Interface ID
683 * @config: pointer to host-native VI RSS Configuration buffer
684 *
685 * Reads the Virtual Interface's RSS configuration information and
686 * translates it into CPU-native format.
687 */
688int t4vf_read_rss_vi_config(struct adapter *adapter, unsigned int viid,
689 union rss_vi_config *config)
690{
691 struct fw_rss_vi_config_cmd cmd, rpl;
692 int v;
693
694 memset(&cmd, 0, sizeof(cmd));
695 cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_RSS_VI_CONFIG_CMD) |
696 FW_CMD_REQUEST |
697 FW_CMD_READ |
698 FW_RSS_VI_CONFIG_CMD_VIID(viid));
699 cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
700 v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
701 if (v)
702 return v;
703
704 switch (adapter->params.rss.mode) {
705 case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
706 u32 word = be32_to_cpu(rpl.u.basicvirtual.defaultq_to_udpen);
707
708 config->basicvirtual.ip6fourtupen =
709 ((word & FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN) != 0);
710 config->basicvirtual.ip6twotupen =
711 ((word & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN) != 0);
712 config->basicvirtual.ip4fourtupen =
713 ((word & FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN) != 0);
714 config->basicvirtual.ip4twotupen =
715 ((word & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN) != 0);
716 config->basicvirtual.udpen =
717 ((word & FW_RSS_VI_CONFIG_CMD_UDPEN) != 0);
718 config->basicvirtual.defaultq =
719 FW_RSS_VI_CONFIG_CMD_DEFAULTQ_GET(word);
720 break;
721 }
722
723 default:
724 return -EINVAL;
725 }
726
727 return 0;
728}
729
730/**
731 * t4vf_write_rss_vi_config - write a VI's RSS configuration
732 * @adapter: the adapter
733 * @viid: Virtual Interface ID
734 * @config: pointer to host-native VI RSS Configuration buffer
735 *
736 * Write the Virtual Interface's RSS configuration information
737 * (translating it into firmware-native format before writing).
738 */
739int t4vf_write_rss_vi_config(struct adapter *adapter, unsigned int viid,
740 union rss_vi_config *config)
741{
742 struct fw_rss_vi_config_cmd cmd, rpl;
743
744 memset(&cmd, 0, sizeof(cmd));
745 cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_RSS_VI_CONFIG_CMD) |
746 FW_CMD_REQUEST |
747 FW_CMD_WRITE |
748 FW_RSS_VI_CONFIG_CMD_VIID(viid));
749 cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
750 switch (adapter->params.rss.mode) {
751 case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
752 u32 word = 0;
753
754 if (config->basicvirtual.ip6fourtupen)
755 word |= FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
756 if (config->basicvirtual.ip6twotupen)
757 word |= FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN;
758 if (config->basicvirtual.ip4fourtupen)
759 word |= FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
760 if (config->basicvirtual.ip4twotupen)
761 word |= FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN;
762 if (config->basicvirtual.udpen)
763 word |= FW_RSS_VI_CONFIG_CMD_UDPEN;
764 word |= FW_RSS_VI_CONFIG_CMD_DEFAULTQ(
765 config->basicvirtual.defaultq);
766 cmd.u.basicvirtual.defaultq_to_udpen = cpu_to_be32(word);
767 break;
768 }
769
770 default:
771 return -EINVAL;
772 }
773
774 return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
775}
776
777/**
778 * t4vf_config_rss_range - configure a portion of the RSS mapping table
779 * @adapter: the adapter
780 * @viid: Virtual Interface of RSS Table Slice
781 * @start: starting entry in the table to write
782 * @n: how many table entries to write
783 * @rspq: values for the "Response Queue" (Ingress Queue) lookup table
784 * @nrspq: number of values in @rspq
785 *
786 * Programs the selected part of the VI's RSS mapping table with the
787 * provided values. If @nrspq < @n the supplied values are used repeatedly
788 * until the full table range is populated.
789 *
790 * The caller must ensure the values in @rspq are in the range 0..1023.
791 */
792int t4vf_config_rss_range(struct adapter *adapter, unsigned int viid,
793 int start, int n, const u16 *rspq, int nrspq)
794{
795 const u16 *rsp = rspq;
796 const u16 *rsp_end = rspq+nrspq;
797 struct fw_rss_ind_tbl_cmd cmd;
798
799 /*
800 * Initialize firmware command template to write the RSS table.
801 */
802 memset(&cmd, 0, sizeof(cmd));
803 cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_RSS_IND_TBL_CMD) |
804 FW_CMD_REQUEST |
805 FW_CMD_WRITE |
806 FW_RSS_IND_TBL_CMD_VIID(viid));
807 cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
808
809 /*
810 * Each firmware RSS command can accommodate up to 32 RSS Ingress
811 * Queue Identifiers. These Ingress Queue IDs are packed three to
812 * a 32-bit word as 10-bit values with the upper remaining 2 bits
813 * reserved.
814 */
815 while (n > 0) {
816 __be32 *qp = &cmd.iq0_to_iq2;
817 int nq = min(n, 32);
818 int ret;
819
820 /*
821 * Set up the firmware RSS command header to send the next
822 * "nq" Ingress Queue IDs to the firmware.
823 */
824 cmd.niqid = cpu_to_be16(nq);
825 cmd.startidx = cpu_to_be16(start);
826
827 /*
828 * "nq" more done for the start of the next loop.
829 */
830 start += nq;
831 n -= nq;
832
833 /*
834 * While there are still Ingress Queue IDs to stuff into the
835 * current firmware RSS command, retrieve them from the
836 * Ingress Queue ID array and insert them into the command.
837 */
838 while (nq > 0) {
839 /*
840 * Grab up to the next 3 Ingress Queue IDs (wrapping
841 * around the Ingress Queue ID array if necessary) and
842 * insert them into the firmware RSS command at the
843 * current 3-tuple position within the commad.
844 */
845 u16 qbuf[3];
846 u16 *qbp = qbuf;
847 int nqbuf = min(3, nq);
848
849 nq -= nqbuf;
850 qbuf[0] = qbuf[1] = qbuf[2] = 0;
851 while (nqbuf) {
852 nqbuf--;
853 *qbp++ = *rsp++;
854 if (rsp >= rsp_end)
855 rsp = rspq;
856 }
857 *qp++ = cpu_to_be32(FW_RSS_IND_TBL_CMD_IQ0(qbuf[0]) |
858 FW_RSS_IND_TBL_CMD_IQ1(qbuf[1]) |
859 FW_RSS_IND_TBL_CMD_IQ2(qbuf[2]));
860 }
861
862 /*
863 * Send this portion of the RRS table update to the firmware;
864 * bail out on any errors.
865 */
866 ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
867 if (ret)
868 return ret;
869 }
870 return 0;
871}
872
873/**
874 * t4vf_alloc_vi - allocate a virtual interface on a port
875 * @adapter: the adapter
876 * @port_id: physical port associated with the VI
877 *
878 * Allocate a new Virtual Interface and bind it to the indicated
879 * physical port. Return the new Virtual Interface Identifier on
880 * success, or a [negative] error number on failure.
881 */
882int t4vf_alloc_vi(struct adapter *adapter, int port_id)
883{
884 struct fw_vi_cmd cmd, rpl;
885 int v;
886
887 /*
888 * Execute a VI command to allocate Virtual Interface and return its
889 * VIID.
890 */
891 memset(&cmd, 0, sizeof(cmd));
892 cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_VI_CMD) |
893 FW_CMD_REQUEST |
894 FW_CMD_WRITE |
895 FW_CMD_EXEC);
896 cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(cmd) |
897 FW_VI_CMD_ALLOC);
898 cmd.portid_pkd = FW_VI_CMD_PORTID(port_id);
899 v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
900 if (v)
901 return v;
902
903 return FW_VI_CMD_VIID_GET(be16_to_cpu(rpl.type_viid));
904}
905
906/**
907 * t4vf_free_vi -- free a virtual interface
908 * @adapter: the adapter
909 * @viid: the virtual interface identifier
910 *
911 * Free a previously allocated Virtual Interface. Return an error on
912 * failure.
913 */
914int t4vf_free_vi(struct adapter *adapter, int viid)
915{
916 struct fw_vi_cmd cmd;
917
918 /*
919 * Execute a VI command to free the Virtual Interface.
920 */
921 memset(&cmd, 0, sizeof(cmd));
922 cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_VI_CMD) |
923 FW_CMD_REQUEST |
924 FW_CMD_EXEC);
925 cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(cmd) |
926 FW_VI_CMD_FREE);
927 cmd.type_viid = cpu_to_be16(FW_VI_CMD_VIID(viid));
928 return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
929}
930
931/**
932 * t4vf_enable_vi - enable/disable a virtual interface
933 * @adapter: the adapter
934 * @viid: the Virtual Interface ID
935 * @rx_en: 1=enable Rx, 0=disable Rx
936 * @tx_en: 1=enable Tx, 0=disable Tx
937 *
938 * Enables/disables a virtual interface.
939 */
940int t4vf_enable_vi(struct adapter *adapter, unsigned int viid,
941 bool rx_en, bool tx_en)
942{
943 struct fw_vi_enable_cmd cmd;
944
945 memset(&cmd, 0, sizeof(cmd));
946 cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_ENABLE_CMD) |
947 FW_CMD_REQUEST |
948 FW_CMD_EXEC |
949 FW_VI_ENABLE_CMD_VIID(viid));
950 cmd.ien_to_len16 = cpu_to_be32(FW_VI_ENABLE_CMD_IEN(rx_en) |
951 FW_VI_ENABLE_CMD_EEN(tx_en) |
952 FW_LEN16(cmd));
953 return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
954}
955
956/**
957 * t4vf_identify_port - identify a VI's port by blinking its LED
958 * @adapter: the adapter
959 * @viid: the Virtual Interface ID
960 * @nblinks: how many times to blink LED at 2.5 Hz
961 *
962 * Identifies a VI's port by blinking its LED.
963 */
964int t4vf_identify_port(struct adapter *adapter, unsigned int viid,
965 unsigned int nblinks)
966{
967 struct fw_vi_enable_cmd cmd;
968
969 memset(&cmd, 0, sizeof(cmd));
970 cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_ENABLE_CMD) |
971 FW_CMD_REQUEST |
972 FW_CMD_EXEC |
973 FW_VI_ENABLE_CMD_VIID(viid));
974 cmd.ien_to_len16 = cpu_to_be32(FW_VI_ENABLE_CMD_LED |
975 FW_LEN16(cmd));
976 cmd.blinkdur = cpu_to_be16(nblinks);
977 return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
978}
979
980/**
981 * t4vf_set_rxmode - set Rx properties of a virtual interface
982 * @adapter: the adapter
983 * @viid: the VI id
984 * @mtu: the new MTU or -1 for no change
985 * @promisc: 1 to enable promiscuous mode, 0 to disable it, -1 no change
986 * @all_multi: 1 to enable all-multi mode, 0 to disable it, -1 no change
987 * @bcast: 1 to enable broadcast Rx, 0 to disable it, -1 no change
988 * @vlanex: 1 to enable hardware VLAN Tag extraction, 0 to disable it,
989 * -1 no change
990 *
991 * Sets Rx properties of a virtual interface.
992 */
993int t4vf_set_rxmode(struct adapter *adapter, unsigned int viid,
994 int mtu, int promisc, int all_multi, int bcast, int vlanex,
995 bool sleep_ok)
996{
997 struct fw_vi_rxmode_cmd cmd;
998
999 /* convert to FW values */
1000 if (mtu < 0)
1001 mtu = FW_VI_RXMODE_CMD_MTU_MASK;
1002 if (promisc < 0)
1003 promisc = FW_VI_RXMODE_CMD_PROMISCEN_MASK;
1004 if (all_multi < 0)
1005 all_multi = FW_VI_RXMODE_CMD_ALLMULTIEN_MASK;
1006 if (bcast < 0)
1007 bcast = FW_VI_RXMODE_CMD_BROADCASTEN_MASK;
1008 if (vlanex < 0)
1009 vlanex = FW_VI_RXMODE_CMD_VLANEXEN_MASK;
1010
1011 memset(&cmd, 0, sizeof(cmd));
1012 cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_RXMODE_CMD) |
1013 FW_CMD_REQUEST |
1014 FW_CMD_WRITE |
1015 FW_VI_RXMODE_CMD_VIID(viid));
1016 cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
1017 cmd.mtu_to_vlanexen =
1018 cpu_to_be32(FW_VI_RXMODE_CMD_MTU(mtu) |
1019 FW_VI_RXMODE_CMD_PROMISCEN(promisc) |
1020 FW_VI_RXMODE_CMD_ALLMULTIEN(all_multi) |
1021 FW_VI_RXMODE_CMD_BROADCASTEN(bcast) |
1022 FW_VI_RXMODE_CMD_VLANEXEN(vlanex));
1023 return t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), NULL, sleep_ok);
1024}
1025
1026/**
1027 * t4vf_alloc_mac_filt - allocates exact-match filters for MAC addresses
1028 * @adapter: the adapter
1029 * @viid: the Virtual Interface Identifier
1030 * @free: if true any existing filters for this VI id are first removed
1031 * @naddr: the number of MAC addresses to allocate filters for (up to 7)
1032 * @addr: the MAC address(es)
1033 * @idx: where to store the index of each allocated filter
1034 * @hash: pointer to hash address filter bitmap
1035 * @sleep_ok: call is allowed to sleep
1036 *
1037 * Allocates an exact-match filter for each of the supplied addresses and
1038 * sets it to the corresponding address. If @idx is not %NULL it should
1039 * have at least @naddr entries, each of which will be set to the index of
1040 * the filter allocated for the corresponding MAC address. If a filter
1041 * could not be allocated for an address its index is set to 0xffff.
1042 * If @hash is not %NULL addresses that fail to allocate an exact filter
1043 * are hashed and update the hash filter bitmap pointed at by @hash.
1044 *
1045 * Returns a negative error number or the number of filters allocated.
1046 */
1047int t4vf_alloc_mac_filt(struct adapter *adapter, unsigned int viid, bool free,
1048 unsigned int naddr, const u8 **addr, u16 *idx,
1049 u64 *hash, bool sleep_ok)
1050{
Casey Leedom42eb59d2010-11-24 12:23:57 +00001051 int offset, ret = 0;
1052 unsigned nfilters = 0;
1053 unsigned int rem = naddr;
Casey Leedom16f8bd42010-06-25 12:12:54 +00001054 struct fw_vi_mac_cmd cmd, rpl;
Hariprasad Shenai70ee3662013-12-03 17:05:57 +05301055 unsigned int max_naddr = is_t4(adapter->params.chip) ?
Santosh Rastapur622c62b2013-03-14 05:08:57 +00001056 NUM_MPS_CLS_SRAM_L_INSTANCES :
1057 NUM_MPS_T5_CLS_SRAM_L_INSTANCES;
Casey Leedom16f8bd42010-06-25 12:12:54 +00001058
Santosh Rastapur622c62b2013-03-14 05:08:57 +00001059 if (naddr > max_naddr)
Casey Leedom16f8bd42010-06-25 12:12:54 +00001060 return -EINVAL;
Casey Leedom16f8bd42010-06-25 12:12:54 +00001061
Casey Leedom42eb59d2010-11-24 12:23:57 +00001062 for (offset = 0; offset < naddr; /**/) {
1063 unsigned int fw_naddr = (rem < ARRAY_SIZE(cmd.u.exact)
1064 ? rem
1065 : ARRAY_SIZE(cmd.u.exact));
1066 size_t len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
1067 u.exact[fw_naddr]), 16);
1068 struct fw_vi_mac_exact *p;
1069 int i;
Casey Leedom16f8bd42010-06-25 12:12:54 +00001070
Casey Leedom42eb59d2010-11-24 12:23:57 +00001071 memset(&cmd, 0, sizeof(cmd));
1072 cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_MAC_CMD) |
1073 FW_CMD_REQUEST |
1074 FW_CMD_WRITE |
1075 (free ? FW_CMD_EXEC : 0) |
1076 FW_VI_MAC_CMD_VIID(viid));
1077 cmd.freemacs_to_len16 =
1078 cpu_to_be32(FW_VI_MAC_CMD_FREEMACS(free) |
1079 FW_CMD_LEN16(len16));
1080
1081 for (i = 0, p = cmd.u.exact; i < fw_naddr; i++, p++) {
1082 p->valid_to_idx = cpu_to_be16(
1083 FW_VI_MAC_CMD_VALID |
1084 FW_VI_MAC_CMD_IDX(FW_VI_MAC_ADD_MAC));
1085 memcpy(p->macaddr, addr[offset+i], sizeof(p->macaddr));
1086 }
1087
1088
1089 ret = t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), &rpl,
1090 sleep_ok);
1091 if (ret && ret != -ENOMEM)
1092 break;
1093
1094 for (i = 0, p = rpl.u.exact; i < fw_naddr; i++, p++) {
1095 u16 index = FW_VI_MAC_CMD_IDX_GET(
1096 be16_to_cpu(p->valid_to_idx));
1097
1098 if (idx)
1099 idx[offset+i] =
Santosh Rastapur622c62b2013-03-14 05:08:57 +00001100 (index >= max_naddr
Casey Leedom42eb59d2010-11-24 12:23:57 +00001101 ? 0xffff
1102 : index);
Santosh Rastapur622c62b2013-03-14 05:08:57 +00001103 if (index < max_naddr)
Casey Leedom42eb59d2010-11-24 12:23:57 +00001104 nfilters++;
1105 else if (hash)
1106 *hash |= (1ULL << hash_mac_addr(addr[offset+i]));
1107 }
1108
1109 free = false;
1110 offset += fw_naddr;
1111 rem -= fw_naddr;
Casey Leedom16f8bd42010-06-25 12:12:54 +00001112 }
1113
Casey Leedom42eb59d2010-11-24 12:23:57 +00001114 /*
1115 * If there were no errors or we merely ran out of room in our MAC
1116 * address arena, return the number of filters actually written.
1117 */
1118 if (ret == 0 || ret == -ENOMEM)
1119 ret = nfilters;
Casey Leedom16f8bd42010-06-25 12:12:54 +00001120 return ret;
1121}
1122
1123/**
1124 * t4vf_change_mac - modifies the exact-match filter for a MAC address
1125 * @adapter: the adapter
1126 * @viid: the Virtual Interface ID
1127 * @idx: index of existing filter for old value of MAC address, or -1
1128 * @addr: the new MAC address value
1129 * @persist: if idx < 0, the new MAC allocation should be persistent
1130 *
1131 * Modifies an exact-match filter and sets it to the new MAC address.
1132 * Note that in general it is not possible to modify the value of a given
1133 * filter so the generic way to modify an address filter is to free the
1134 * one being used by the old address value and allocate a new filter for
1135 * the new address value. @idx can be -1 if the address is a new
1136 * addition.
1137 *
1138 * Returns a negative error number or the index of the filter with the new
1139 * MAC value.
1140 */
1141int t4vf_change_mac(struct adapter *adapter, unsigned int viid,
1142 int idx, const u8 *addr, bool persist)
1143{
1144 int ret;
1145 struct fw_vi_mac_cmd cmd, rpl;
1146 struct fw_vi_mac_exact *p = &cmd.u.exact[0];
1147 size_t len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
1148 u.exact[1]), 16);
Hariprasad Shenai70ee3662013-12-03 17:05:57 +05301149 unsigned int max_naddr = is_t4(adapter->params.chip) ?
Santosh Rastapur622c62b2013-03-14 05:08:57 +00001150 NUM_MPS_CLS_SRAM_L_INSTANCES :
1151 NUM_MPS_T5_CLS_SRAM_L_INSTANCES;
Casey Leedom16f8bd42010-06-25 12:12:54 +00001152
1153 /*
1154 * If this is a new allocation, determine whether it should be
1155 * persistent (across a "freemacs" operation) or not.
1156 */
1157 if (idx < 0)
1158 idx = persist ? FW_VI_MAC_ADD_PERSIST_MAC : FW_VI_MAC_ADD_MAC;
1159
1160 memset(&cmd, 0, sizeof(cmd));
1161 cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_MAC_CMD) |
1162 FW_CMD_REQUEST |
1163 FW_CMD_WRITE |
1164 FW_VI_MAC_CMD_VIID(viid));
1165 cmd.freemacs_to_len16 = cpu_to_be32(FW_CMD_LEN16(len16));
1166 p->valid_to_idx = cpu_to_be16(FW_VI_MAC_CMD_VALID |
1167 FW_VI_MAC_CMD_IDX(idx));
1168 memcpy(p->macaddr, addr, sizeof(p->macaddr));
1169
1170 ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
1171 if (ret == 0) {
1172 p = &rpl.u.exact[0];
1173 ret = FW_VI_MAC_CMD_IDX_GET(be16_to_cpu(p->valid_to_idx));
Santosh Rastapur622c62b2013-03-14 05:08:57 +00001174 if (ret >= max_naddr)
Casey Leedom16f8bd42010-06-25 12:12:54 +00001175 ret = -ENOMEM;
1176 }
1177 return ret;
1178}
1179
1180/**
1181 * t4vf_set_addr_hash - program the MAC inexact-match hash filter
1182 * @adapter: the adapter
1183 * @viid: the Virtual Interface Identifier
1184 * @ucast: whether the hash filter should also match unicast addresses
1185 * @vec: the value to be written to the hash filter
1186 * @sleep_ok: call is allowed to sleep
1187 *
1188 * Sets the 64-bit inexact-match hash filter for a virtual interface.
1189 */
1190int t4vf_set_addr_hash(struct adapter *adapter, unsigned int viid,
1191 bool ucast, u64 vec, bool sleep_ok)
1192{
1193 struct fw_vi_mac_cmd cmd;
1194 size_t len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
1195 u.exact[0]), 16);
1196
1197 memset(&cmd, 0, sizeof(cmd));
1198 cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_MAC_CMD) |
1199 FW_CMD_REQUEST |
1200 FW_CMD_WRITE |
1201 FW_VI_ENABLE_CMD_VIID(viid));
1202 cmd.freemacs_to_len16 = cpu_to_be32(FW_VI_MAC_CMD_HASHVECEN |
1203 FW_VI_MAC_CMD_HASHUNIEN(ucast) |
1204 FW_CMD_LEN16(len16));
1205 cmd.u.hash.hashvec = cpu_to_be64(vec);
1206 return t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), NULL, sleep_ok);
1207}
1208
1209/**
1210 * t4vf_get_port_stats - collect "port" statistics
1211 * @adapter: the adapter
1212 * @pidx: the port index
1213 * @s: the stats structure to fill
1214 *
1215 * Collect statistics for the "port"'s Virtual Interface.
1216 */
1217int t4vf_get_port_stats(struct adapter *adapter, int pidx,
1218 struct t4vf_port_stats *s)
1219{
1220 struct port_info *pi = adap2pinfo(adapter, pidx);
1221 struct fw_vi_stats_vf fwstats;
1222 unsigned int rem = VI_VF_NUM_STATS;
1223 __be64 *fwsp = (__be64 *)&fwstats;
1224
1225 /*
1226 * Grab the Virtual Interface statistics a chunk at a time via mailbox
1227 * commands. We could use a Work Request and get all of them at once
1228 * but that's an asynchronous interface which is awkward to use.
1229 */
1230 while (rem) {
1231 unsigned int ix = VI_VF_NUM_STATS - rem;
1232 unsigned int nstats = min(6U, rem);
1233 struct fw_vi_stats_cmd cmd, rpl;
1234 size_t len = (offsetof(struct fw_vi_stats_cmd, u) +
1235 sizeof(struct fw_vi_stats_ctl));
1236 size_t len16 = DIV_ROUND_UP(len, 16);
1237 int ret;
1238
1239 memset(&cmd, 0, sizeof(cmd));
1240 cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_STATS_CMD) |
1241 FW_VI_STATS_CMD_VIID(pi->viid) |
1242 FW_CMD_REQUEST |
1243 FW_CMD_READ);
1244 cmd.retval_len16 = cpu_to_be32(FW_CMD_LEN16(len16));
1245 cmd.u.ctl.nstats_ix =
1246 cpu_to_be16(FW_VI_STATS_CMD_IX(ix) |
1247 FW_VI_STATS_CMD_NSTATS(nstats));
1248 ret = t4vf_wr_mbox_ns(adapter, &cmd, len, &rpl);
1249 if (ret)
1250 return ret;
1251
1252 memcpy(fwsp, &rpl.u.ctl.stat0, sizeof(__be64) * nstats);
1253
1254 rem -= nstats;
1255 fwsp += nstats;
1256 }
1257
1258 /*
1259 * Translate firmware statistics into host native statistics.
1260 */
1261 s->tx_bcast_bytes = be64_to_cpu(fwstats.tx_bcast_bytes);
1262 s->tx_bcast_frames = be64_to_cpu(fwstats.tx_bcast_frames);
1263 s->tx_mcast_bytes = be64_to_cpu(fwstats.tx_mcast_bytes);
1264 s->tx_mcast_frames = be64_to_cpu(fwstats.tx_mcast_frames);
1265 s->tx_ucast_bytes = be64_to_cpu(fwstats.tx_ucast_bytes);
1266 s->tx_ucast_frames = be64_to_cpu(fwstats.tx_ucast_frames);
1267 s->tx_drop_frames = be64_to_cpu(fwstats.tx_drop_frames);
1268 s->tx_offload_bytes = be64_to_cpu(fwstats.tx_offload_bytes);
1269 s->tx_offload_frames = be64_to_cpu(fwstats.tx_offload_frames);
1270
1271 s->rx_bcast_bytes = be64_to_cpu(fwstats.rx_bcast_bytes);
1272 s->rx_bcast_frames = be64_to_cpu(fwstats.rx_bcast_frames);
1273 s->rx_mcast_bytes = be64_to_cpu(fwstats.rx_mcast_bytes);
1274 s->rx_mcast_frames = be64_to_cpu(fwstats.rx_mcast_frames);
1275 s->rx_ucast_bytes = be64_to_cpu(fwstats.rx_ucast_bytes);
1276 s->rx_ucast_frames = be64_to_cpu(fwstats.rx_ucast_frames);
1277
1278 s->rx_err_frames = be64_to_cpu(fwstats.rx_err_frames);
1279
1280 return 0;
1281}
1282
1283/**
1284 * t4vf_iq_free - free an ingress queue and its free lists
1285 * @adapter: the adapter
1286 * @iqtype: the ingress queue type (FW_IQ_TYPE_FL_INT_CAP, etc.)
1287 * @iqid: ingress queue ID
1288 * @fl0id: FL0 queue ID or 0xffff if no attached FL0
1289 * @fl1id: FL1 queue ID or 0xffff if no attached FL1
1290 *
1291 * Frees an ingress queue and its associated free lists, if any.
1292 */
1293int t4vf_iq_free(struct adapter *adapter, unsigned int iqtype,
1294 unsigned int iqid, unsigned int fl0id, unsigned int fl1id)
1295{
1296 struct fw_iq_cmd cmd;
1297
1298 memset(&cmd, 0, sizeof(cmd));
1299 cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_IQ_CMD) |
1300 FW_CMD_REQUEST |
1301 FW_CMD_EXEC);
1302 cmd.alloc_to_len16 = cpu_to_be32(FW_IQ_CMD_FREE |
1303 FW_LEN16(cmd));
1304 cmd.type_to_iqandstindex =
1305 cpu_to_be32(FW_IQ_CMD_TYPE(iqtype));
1306
1307 cmd.iqid = cpu_to_be16(iqid);
1308 cmd.fl0id = cpu_to_be16(fl0id);
1309 cmd.fl1id = cpu_to_be16(fl1id);
1310 return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
1311}
1312
1313/**
1314 * t4vf_eth_eq_free - free an Ethernet egress queue
1315 * @adapter: the adapter
1316 * @eqid: egress queue ID
1317 *
1318 * Frees an Ethernet egress queue.
1319 */
1320int t4vf_eth_eq_free(struct adapter *adapter, unsigned int eqid)
1321{
1322 struct fw_eq_eth_cmd cmd;
1323
1324 memset(&cmd, 0, sizeof(cmd));
1325 cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_EQ_ETH_CMD) |
1326 FW_CMD_REQUEST |
1327 FW_CMD_EXEC);
1328 cmd.alloc_to_len16 = cpu_to_be32(FW_EQ_ETH_CMD_FREE |
1329 FW_LEN16(cmd));
1330 cmd.eqid_pkd = cpu_to_be32(FW_EQ_ETH_CMD_EQID(eqid));
1331 return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
1332}
1333
1334/**
1335 * t4vf_handle_fw_rpl - process a firmware reply message
1336 * @adapter: the adapter
1337 * @rpl: start of the firmware message
1338 *
1339 * Processes a firmware message, such as link state change messages.
1340 */
1341int t4vf_handle_fw_rpl(struct adapter *adapter, const __be64 *rpl)
1342{
Casey Leedomcaedda32010-11-11 09:30:40 +00001343 const struct fw_cmd_hdr *cmd_hdr = (const struct fw_cmd_hdr *)rpl;
Casey Leedom16f8bd42010-06-25 12:12:54 +00001344 u8 opcode = FW_CMD_OP_GET(be32_to_cpu(cmd_hdr->hi));
1345
1346 switch (opcode) {
1347 case FW_PORT_CMD: {
1348 /*
1349 * Link/module state change message.
1350 */
Casey Leedomcaedda32010-11-11 09:30:40 +00001351 const struct fw_port_cmd *port_cmd =
1352 (const struct fw_port_cmd *)rpl;
Casey Leedom16f8bd42010-06-25 12:12:54 +00001353 u32 word;
1354 int action, port_id, link_ok, speed, fc, pidx;
1355
1356 /*
1357 * Extract various fields from port status change message.
1358 */
1359 action = FW_PORT_CMD_ACTION_GET(
1360 be32_to_cpu(port_cmd->action_to_len16));
1361 if (action != FW_PORT_ACTION_GET_PORT_INFO) {
1362 dev_err(adapter->pdev_dev,
1363 "Unknown firmware PORT reply action %x\n",
1364 action);
1365 break;
1366 }
1367
1368 port_id = FW_PORT_CMD_PORTID_GET(
1369 be32_to_cpu(port_cmd->op_to_portid));
1370
1371 word = be32_to_cpu(port_cmd->u.info.lstatus_to_modtype);
1372 link_ok = (word & FW_PORT_CMD_LSTATUS) != 0;
1373 speed = 0;
1374 fc = 0;
1375 if (word & FW_PORT_CMD_RXPAUSE)
1376 fc |= PAUSE_RX;
1377 if (word & FW_PORT_CMD_TXPAUSE)
1378 fc |= PAUSE_TX;
1379 if (word & FW_PORT_CMD_LSPEED(FW_PORT_CAP_SPEED_100M))
Hariprasad Shenai897d55d2014-10-09 05:48:46 +05301380 speed = 100;
Casey Leedom16f8bd42010-06-25 12:12:54 +00001381 else if (word & FW_PORT_CMD_LSPEED(FW_PORT_CAP_SPEED_1G))
Hariprasad Shenai897d55d2014-10-09 05:48:46 +05301382 speed = 1000;
Casey Leedom16f8bd42010-06-25 12:12:54 +00001383 else if (word & FW_PORT_CMD_LSPEED(FW_PORT_CAP_SPEED_10G))
Hariprasad Shenai897d55d2014-10-09 05:48:46 +05301384 speed = 10000;
1385 else if (word & FW_PORT_CMD_LSPEED(FW_PORT_CAP_SPEED_40G))
1386 speed = 40000;
Casey Leedom16f8bd42010-06-25 12:12:54 +00001387
1388 /*
1389 * Scan all of our "ports" (Virtual Interfaces) looking for
1390 * those bound to the physical port which has changed. If
1391 * our recorded state doesn't match the current state,
1392 * signal that change to the OS code.
1393 */
1394 for_each_port(adapter, pidx) {
1395 struct port_info *pi = adap2pinfo(adapter, pidx);
1396 struct link_config *lc;
1397
1398 if (pi->port_id != port_id)
1399 continue;
1400
1401 lc = &pi->link_cfg;
1402 if (link_ok != lc->link_ok || speed != lc->speed ||
1403 fc != lc->fc) {
1404 /* something changed */
1405 lc->link_ok = link_ok;
1406 lc->speed = speed;
1407 lc->fc = fc;
1408 t4vf_os_link_changed(adapter, pidx, link_ok);
1409 }
1410 }
1411 break;
1412 }
1413
1414 default:
1415 dev_err(adapter->pdev_dev, "Unknown firmware reply %X\n",
1416 opcode);
1417 }
1418 return 0;
1419}