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