Merge branch 'next-spi' of git://git.secretlab.ca/git/linux-2.6
[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_query_params - query FW or device parameters
330  *      @adapter: the adapter
331  *      @nparams: the number of parameters
332  *      @params: the parameter names
333  *      @vals: the parameter values
334  *
335  *      Reads the values of firmware or device parameters.  Up to 7 parameters
336  *      can be queried at once.
337  */
338 int t4vf_query_params(struct adapter *adapter, unsigned int nparams,
339                       const u32 *params, u32 *vals)
340 {
341         int i, ret;
342         struct fw_params_cmd cmd, rpl;
343         struct fw_params_param *p;
344         size_t len16;
345
346         if (nparams > 7)
347                 return -EINVAL;
348
349         memset(&cmd, 0, sizeof(cmd));
350         cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_PARAMS_CMD) |
351                                     FW_CMD_REQUEST |
352                                     FW_CMD_READ);
353         len16 = DIV_ROUND_UP(offsetof(struct fw_params_cmd,
354                                       param[nparams].mnem), 16);
355         cmd.retval_len16 = cpu_to_be32(FW_CMD_LEN16(len16));
356         for (i = 0, p = &cmd.param[0]; i < nparams; i++, p++)
357                 p->mnem = htonl(*params++);
358
359         ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
360         if (ret == 0)
361                 for (i = 0, p = &rpl.param[0]; i < nparams; i++, p++)
362                         *vals++ = be32_to_cpu(p->val);
363         return ret;
364 }
365
366 /**
367  *      t4vf_set_params - sets FW or device parameters
368  *      @adapter: the adapter
369  *      @nparams: the number of parameters
370  *      @params: the parameter names
371  *      @vals: the parameter values
372  *
373  *      Sets the values of firmware or device parameters.  Up to 7 parameters
374  *      can be specified at once.
375  */
376 int t4vf_set_params(struct adapter *adapter, unsigned int nparams,
377                     const u32 *params, const u32 *vals)
378 {
379         int i;
380         struct fw_params_cmd cmd;
381         struct fw_params_param *p;
382         size_t len16;
383
384         if (nparams > 7)
385                 return -EINVAL;
386
387         memset(&cmd, 0, sizeof(cmd));
388         cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_PARAMS_CMD) |
389                                     FW_CMD_REQUEST |
390                                     FW_CMD_WRITE);
391         len16 = DIV_ROUND_UP(offsetof(struct fw_params_cmd,
392                                       param[nparams]), 16);
393         cmd.retval_len16 = cpu_to_be32(FW_CMD_LEN16(len16));
394         for (i = 0, p = &cmd.param[0]; i < nparams; i++, p++) {
395                 p->mnem = cpu_to_be32(*params++);
396                 p->val = cpu_to_be32(*vals++);
397         }
398
399         return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
400 }
401
402 /**
403  *      t4vf_get_sge_params - retrieve adapter Scatter gather Engine parameters
404  *      @adapter: the adapter
405  *
406  *      Retrieves various core SGE parameters in the form of hardware SGE
407  *      register values.  The caller is responsible for decoding these as
408  *      needed.  The SGE parameters are stored in @adapter->params.sge.
409  */
410 int t4vf_get_sge_params(struct adapter *adapter)
411 {
412         struct sge_params *sge_params = &adapter->params.sge;
413         u32 params[7], vals[7];
414         int v;
415
416         params[0] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
417                      FW_PARAMS_PARAM_XYZ(SGE_CONTROL));
418         params[1] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
419                      FW_PARAMS_PARAM_XYZ(SGE_HOST_PAGE_SIZE));
420         params[2] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
421                      FW_PARAMS_PARAM_XYZ(SGE_FL_BUFFER_SIZE0));
422         params[3] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
423                      FW_PARAMS_PARAM_XYZ(SGE_FL_BUFFER_SIZE1));
424         params[4] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
425                      FW_PARAMS_PARAM_XYZ(SGE_TIMER_VALUE_0_AND_1));
426         params[5] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
427                      FW_PARAMS_PARAM_XYZ(SGE_TIMER_VALUE_2_AND_3));
428         params[6] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
429                      FW_PARAMS_PARAM_XYZ(SGE_TIMER_VALUE_4_AND_5));
430         v = t4vf_query_params(adapter, 7, params, vals);
431         if (v)
432                 return v;
433         sge_params->sge_control = vals[0];
434         sge_params->sge_host_page_size = vals[1];
435         sge_params->sge_fl_buffer_size[0] = vals[2];
436         sge_params->sge_fl_buffer_size[1] = vals[3];
437         sge_params->sge_timer_value_0_and_1 = vals[4];
438         sge_params->sge_timer_value_2_and_3 = vals[5];
439         sge_params->sge_timer_value_4_and_5 = vals[6];
440
441         params[0] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
442                      FW_PARAMS_PARAM_XYZ(SGE_INGRESS_RX_THRESHOLD));
443         v = t4vf_query_params(adapter, 1, params, vals);
444         if (v)
445                 return v;
446         sge_params->sge_ingress_rx_threshold = vals[0];
447
448         return 0;
449 }
450
451 /**
452  *      t4vf_get_vpd_params - retrieve device VPD paremeters
453  *      @adapter: the adapter
454  *
455  *      Retrives various device Vital Product Data parameters.  The parameters
456  *      are stored in @adapter->params.vpd.
457  */
458 int t4vf_get_vpd_params(struct adapter *adapter)
459 {
460         struct vpd_params *vpd_params = &adapter->params.vpd;
461         u32 params[7], vals[7];
462         int v;
463
464         params[0] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
465                      FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_CCLK));
466         v = t4vf_query_params(adapter, 1, params, vals);
467         if (v)
468                 return v;
469         vpd_params->cclk = vals[0];
470
471         return 0;
472 }
473
474 /**
475  *      t4vf_get_dev_params - retrieve device paremeters
476  *      @adapter: the adapter
477  *
478  *      Retrives various device parameters.  The parameters are stored in
479  *      @adapter->params.dev.
480  */
481 int t4vf_get_dev_params(struct adapter *adapter)
482 {
483         struct dev_params *dev_params = &adapter->params.dev;
484         u32 params[7], vals[7];
485         int v;
486
487         params[0] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
488                      FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_FWREV));
489         params[1] = (FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
490                      FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_TPREV));
491         v = t4vf_query_params(adapter, 2, params, vals);
492         if (v)
493                 return v;
494         dev_params->fwrev = vals[0];
495         dev_params->tprev = vals[1];
496
497         return 0;
498 }
499
500 /**
501  *      t4vf_get_rss_glb_config - retrieve adapter RSS Global Configuration
502  *      @adapter: the adapter
503  *
504  *      Retrieves global RSS mode and parameters with which we have to live
505  *      and stores them in the @adapter's RSS parameters.
506  */
507 int t4vf_get_rss_glb_config(struct adapter *adapter)
508 {
509         struct rss_params *rss = &adapter->params.rss;
510         struct fw_rss_glb_config_cmd cmd, rpl;
511         int v;
512
513         /*
514          * Execute an RSS Global Configuration read command to retrieve
515          * our RSS configuration.
516          */
517         memset(&cmd, 0, sizeof(cmd));
518         cmd.op_to_write = cpu_to_be32(FW_CMD_OP(FW_RSS_GLB_CONFIG_CMD) |
519                                       FW_CMD_REQUEST |
520                                       FW_CMD_READ);
521         cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
522         v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
523         if (v)
524                 return v;
525
526         /*
527          * Transate the big-endian RSS Global Configuration into our
528          * cpu-endian format based on the RSS mode.  We also do first level
529          * filtering at this point to weed out modes which don't support
530          * VF Drivers ...
531          */
532         rss->mode = FW_RSS_GLB_CONFIG_CMD_MODE_GET(
533                         be32_to_cpu(rpl.u.manual.mode_pkd));
534         switch (rss->mode) {
535         case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
536                 u32 word = be32_to_cpu(
537                                 rpl.u.basicvirtual.synmapen_to_hashtoeplitz);
538
539                 rss->u.basicvirtual.synmapen =
540                         ((word & FW_RSS_GLB_CONFIG_CMD_SYNMAPEN) != 0);
541                 rss->u.basicvirtual.syn4tupenipv6 =
542                         ((word & FW_RSS_GLB_CONFIG_CMD_SYN4TUPENIPV6) != 0);
543                 rss->u.basicvirtual.syn2tupenipv6 =
544                         ((word & FW_RSS_GLB_CONFIG_CMD_SYN2TUPENIPV6) != 0);
545                 rss->u.basicvirtual.syn4tupenipv4 =
546                         ((word & FW_RSS_GLB_CONFIG_CMD_SYN4TUPENIPV4) != 0);
547                 rss->u.basicvirtual.syn2tupenipv4 =
548                         ((word & FW_RSS_GLB_CONFIG_CMD_SYN2TUPENIPV4) != 0);
549
550                 rss->u.basicvirtual.ofdmapen =
551                         ((word & FW_RSS_GLB_CONFIG_CMD_OFDMAPEN) != 0);
552
553                 rss->u.basicvirtual.tnlmapen =
554                         ((word & FW_RSS_GLB_CONFIG_CMD_TNLMAPEN) != 0);
555                 rss->u.basicvirtual.tnlalllookup =
556                         ((word  & FW_RSS_GLB_CONFIG_CMD_TNLALLLKP) != 0);
557
558                 rss->u.basicvirtual.hashtoeplitz =
559                         ((word & FW_RSS_GLB_CONFIG_CMD_HASHTOEPLITZ) != 0);
560
561                 /* we need at least Tunnel Map Enable to be set */
562                 if (!rss->u.basicvirtual.tnlmapen)
563                         return -EINVAL;
564                 break;
565         }
566
567         default:
568                 /* all unknown/unsupported RSS modes result in an error */
569                 return -EINVAL;
570         }
571
572         return 0;
573 }
574
575 /**
576  *      t4vf_get_vfres - retrieve VF resource limits
577  *      @adapter: the adapter
578  *
579  *      Retrieves configured resource limits and capabilities for a virtual
580  *      function.  The results are stored in @adapter->vfres.
581  */
582 int t4vf_get_vfres(struct adapter *adapter)
583 {
584         struct vf_resources *vfres = &adapter->params.vfres;
585         struct fw_pfvf_cmd cmd, rpl;
586         int v;
587         u32 word;
588
589         /*
590          * Execute PFVF Read command to get VF resource limits; bail out early
591          * with error on command failure.
592          */
593         memset(&cmd, 0, sizeof(cmd));
594         cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_PFVF_CMD) |
595                                     FW_CMD_REQUEST |
596                                     FW_CMD_READ);
597         cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
598         v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
599         if (v)
600                 return v;
601
602         /*
603          * Extract VF resource limits and return success.
604          */
605         word = be32_to_cpu(rpl.niqflint_niq);
606         vfres->niqflint = FW_PFVF_CMD_NIQFLINT_GET(word);
607         vfres->niq = FW_PFVF_CMD_NIQ_GET(word);
608
609         word = be32_to_cpu(rpl.type_to_neq);
610         vfres->neq = FW_PFVF_CMD_NEQ_GET(word);
611         vfres->pmask = FW_PFVF_CMD_PMASK_GET(word);
612
613         word = be32_to_cpu(rpl.tc_to_nexactf);
614         vfres->tc = FW_PFVF_CMD_TC_GET(word);
615         vfres->nvi = FW_PFVF_CMD_NVI_GET(word);
616         vfres->nexactf = FW_PFVF_CMD_NEXACTF_GET(word);
617
618         word = be32_to_cpu(rpl.r_caps_to_nethctrl);
619         vfres->r_caps = FW_PFVF_CMD_R_CAPS_GET(word);
620         vfres->wx_caps = FW_PFVF_CMD_WX_CAPS_GET(word);
621         vfres->nethctrl = FW_PFVF_CMD_NETHCTRL_GET(word);
622
623         return 0;
624 }
625
626 /**
627  *      t4vf_read_rss_vi_config - read a VI's RSS configuration
628  *      @adapter: the adapter
629  *      @viid: Virtual Interface ID
630  *      @config: pointer to host-native VI RSS Configuration buffer
631  *
632  *      Reads the Virtual Interface's RSS configuration information and
633  *      translates it into CPU-native format.
634  */
635 int t4vf_read_rss_vi_config(struct adapter *adapter, unsigned int viid,
636                             union rss_vi_config *config)
637 {
638         struct fw_rss_vi_config_cmd cmd, rpl;
639         int v;
640
641         memset(&cmd, 0, sizeof(cmd));
642         cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_RSS_VI_CONFIG_CMD) |
643                                      FW_CMD_REQUEST |
644                                      FW_CMD_READ |
645                                      FW_RSS_VI_CONFIG_CMD_VIID(viid));
646         cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
647         v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
648         if (v)
649                 return v;
650
651         switch (adapter->params.rss.mode) {
652         case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
653                 u32 word = be32_to_cpu(rpl.u.basicvirtual.defaultq_to_udpen);
654
655                 config->basicvirtual.ip6fourtupen =
656                         ((word & FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN) != 0);
657                 config->basicvirtual.ip6twotupen =
658                         ((word & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN) != 0);
659                 config->basicvirtual.ip4fourtupen =
660                         ((word & FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN) != 0);
661                 config->basicvirtual.ip4twotupen =
662                         ((word & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN) != 0);
663                 config->basicvirtual.udpen =
664                         ((word & FW_RSS_VI_CONFIG_CMD_UDPEN) != 0);
665                 config->basicvirtual.defaultq =
666                         FW_RSS_VI_CONFIG_CMD_DEFAULTQ_GET(word);
667                 break;
668         }
669
670         default:
671                 return -EINVAL;
672         }
673
674         return 0;
675 }
676
677 /**
678  *      t4vf_write_rss_vi_config - write a VI's RSS configuration
679  *      @adapter: the adapter
680  *      @viid: Virtual Interface ID
681  *      @config: pointer to host-native VI RSS Configuration buffer
682  *
683  *      Write the Virtual Interface's RSS configuration information
684  *      (translating it into firmware-native format before writing).
685  */
686 int t4vf_write_rss_vi_config(struct adapter *adapter, unsigned int viid,
687                              union rss_vi_config *config)
688 {
689         struct fw_rss_vi_config_cmd cmd, rpl;
690
691         memset(&cmd, 0, sizeof(cmd));
692         cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_RSS_VI_CONFIG_CMD) |
693                                      FW_CMD_REQUEST |
694                                      FW_CMD_WRITE |
695                                      FW_RSS_VI_CONFIG_CMD_VIID(viid));
696         cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
697         switch (adapter->params.rss.mode) {
698         case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
699                 u32 word = 0;
700
701                 if (config->basicvirtual.ip6fourtupen)
702                         word |= FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN;
703                 if (config->basicvirtual.ip6twotupen)
704                         word |= FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN;
705                 if (config->basicvirtual.ip4fourtupen)
706                         word |= FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN;
707                 if (config->basicvirtual.ip4twotupen)
708                         word |= FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN;
709                 if (config->basicvirtual.udpen)
710                         word |= FW_RSS_VI_CONFIG_CMD_UDPEN;
711                 word |= FW_RSS_VI_CONFIG_CMD_DEFAULTQ(
712                                 config->basicvirtual.defaultq);
713                 cmd.u.basicvirtual.defaultq_to_udpen = cpu_to_be32(word);
714                 break;
715         }
716
717         default:
718                 return -EINVAL;
719         }
720
721         return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
722 }
723
724 /**
725  *      t4vf_config_rss_range - configure a portion of the RSS mapping table
726  *      @adapter: the adapter
727  *      @viid: Virtual Interface of RSS Table Slice
728  *      @start: starting entry in the table to write
729  *      @n: how many table entries to write
730  *      @rspq: values for the "Response Queue" (Ingress Queue) lookup table
731  *      @nrspq: number of values in @rspq
732  *
733  *      Programs the selected part of the VI's RSS mapping table with the
734  *      provided values.  If @nrspq < @n the supplied values are used repeatedly
735  *      until the full table range is populated.
736  *
737  *      The caller must ensure the values in @rspq are in the range 0..1023.
738  */
739 int t4vf_config_rss_range(struct adapter *adapter, unsigned int viid,
740                           int start, int n, const u16 *rspq, int nrspq)
741 {
742         const u16 *rsp = rspq;
743         const u16 *rsp_end = rspq+nrspq;
744         struct fw_rss_ind_tbl_cmd cmd;
745
746         /*
747          * Initialize firmware command template to write the RSS table.
748          */
749         memset(&cmd, 0, sizeof(cmd));
750         cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_RSS_IND_TBL_CMD) |
751                                      FW_CMD_REQUEST |
752                                      FW_CMD_WRITE |
753                                      FW_RSS_IND_TBL_CMD_VIID(viid));
754         cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
755
756         /*
757          * Each firmware RSS command can accommodate up to 32 RSS Ingress
758          * Queue Identifiers.  These Ingress Queue IDs are packed three to
759          * a 32-bit word as 10-bit values with the upper remaining 2 bits
760          * reserved.
761          */
762         while (n > 0) {
763                 __be32 *qp = &cmd.iq0_to_iq2;
764                 int nq = min(n, 32);
765                 int ret;
766
767                 /*
768                  * Set up the firmware RSS command header to send the next
769                  * "nq" Ingress Queue IDs to the firmware.
770                  */
771                 cmd.niqid = cpu_to_be16(nq);
772                 cmd.startidx = cpu_to_be16(start);
773
774                 /*
775                  * "nq" more done for the start of the next loop.
776                  */
777                 start += nq;
778                 n -= nq;
779
780                 /*
781                  * While there are still Ingress Queue IDs to stuff into the
782                  * current firmware RSS command, retrieve them from the
783                  * Ingress Queue ID array and insert them into the command.
784                  */
785                 while (nq > 0) {
786                         /*
787                          * Grab up to the next 3 Ingress Queue IDs (wrapping
788                          * around the Ingress Queue ID array if necessary) and
789                          * insert them into the firmware RSS command at the
790                          * current 3-tuple position within the commad.
791                          */
792                         u16 qbuf[3];
793                         u16 *qbp = qbuf;
794                         int nqbuf = min(3, nq);
795
796                         nq -= nqbuf;
797                         qbuf[0] = qbuf[1] = qbuf[2] = 0;
798                         while (nqbuf) {
799                                 nqbuf--;
800                                 *qbp++ = *rsp++;
801                                 if (rsp >= rsp_end)
802                                         rsp = rspq;
803                         }
804                         *qp++ = cpu_to_be32(FW_RSS_IND_TBL_CMD_IQ0(qbuf[0]) |
805                                             FW_RSS_IND_TBL_CMD_IQ1(qbuf[1]) |
806                                             FW_RSS_IND_TBL_CMD_IQ2(qbuf[2]));
807                 }
808
809                 /*
810                  * Send this portion of the RRS table update to the firmware;
811                  * bail out on any errors.
812                  */
813                 ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
814                 if (ret)
815                         return ret;
816         }
817         return 0;
818 }
819
820 /**
821  *      t4vf_alloc_vi - allocate a virtual interface on a port
822  *      @adapter: the adapter
823  *      @port_id: physical port associated with the VI
824  *
825  *      Allocate a new Virtual Interface and bind it to the indicated
826  *      physical port.  Return the new Virtual Interface Identifier on
827  *      success, or a [negative] error number on failure.
828  */
829 int t4vf_alloc_vi(struct adapter *adapter, int port_id)
830 {
831         struct fw_vi_cmd cmd, rpl;
832         int v;
833
834         /*
835          * Execute a VI command to allocate Virtual Interface and return its
836          * VIID.
837          */
838         memset(&cmd, 0, sizeof(cmd));
839         cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_VI_CMD) |
840                                     FW_CMD_REQUEST |
841                                     FW_CMD_WRITE |
842                                     FW_CMD_EXEC);
843         cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(cmd) |
844                                          FW_VI_CMD_ALLOC);
845         cmd.portid_pkd = FW_VI_CMD_PORTID(port_id);
846         v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
847         if (v)
848                 return v;
849
850         return FW_VI_CMD_VIID_GET(be16_to_cpu(rpl.type_viid));
851 }
852
853 /**
854  *      t4vf_free_vi -- free a virtual interface
855  *      @adapter: the adapter
856  *      @viid: the virtual interface identifier
857  *
858  *      Free a previously allocated Virtual Interface.  Return an error on
859  *      failure.
860  */
861 int t4vf_free_vi(struct adapter *adapter, int viid)
862 {
863         struct fw_vi_cmd cmd;
864
865         /*
866          * Execute a VI command to free the Virtual Interface.
867          */
868         memset(&cmd, 0, sizeof(cmd));
869         cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_VI_CMD) |
870                                     FW_CMD_REQUEST |
871                                     FW_CMD_EXEC);
872         cmd.alloc_to_len16 = cpu_to_be32(FW_LEN16(cmd) |
873                                          FW_VI_CMD_FREE);
874         cmd.type_viid = cpu_to_be16(FW_VI_CMD_VIID(viid));
875         return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
876 }
877
878 /**
879  *      t4vf_enable_vi - enable/disable a virtual interface
880  *      @adapter: the adapter
881  *      @viid: the Virtual Interface ID
882  *      @rx_en: 1=enable Rx, 0=disable Rx
883  *      @tx_en: 1=enable Tx, 0=disable Tx
884  *
885  *      Enables/disables a virtual interface.
886  */
887 int t4vf_enable_vi(struct adapter *adapter, unsigned int viid,
888                    bool rx_en, bool tx_en)
889 {
890         struct fw_vi_enable_cmd cmd;
891
892         memset(&cmd, 0, sizeof(cmd));
893         cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_ENABLE_CMD) |
894                                      FW_CMD_REQUEST |
895                                      FW_CMD_EXEC |
896                                      FW_VI_ENABLE_CMD_VIID(viid));
897         cmd.ien_to_len16 = cpu_to_be32(FW_VI_ENABLE_CMD_IEN(rx_en) |
898                                        FW_VI_ENABLE_CMD_EEN(tx_en) |
899                                        FW_LEN16(cmd));
900         return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
901 }
902
903 /**
904  *      t4vf_identify_port - identify a VI's port by blinking its LED
905  *      @adapter: the adapter
906  *      @viid: the Virtual Interface ID
907  *      @nblinks: how many times to blink LED at 2.5 Hz
908  *
909  *      Identifies a VI's port by blinking its LED.
910  */
911 int t4vf_identify_port(struct adapter *adapter, unsigned int viid,
912                        unsigned int nblinks)
913 {
914         struct fw_vi_enable_cmd cmd;
915
916         memset(&cmd, 0, sizeof(cmd));
917         cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_ENABLE_CMD) |
918                                      FW_CMD_REQUEST |
919                                      FW_CMD_EXEC |
920                                      FW_VI_ENABLE_CMD_VIID(viid));
921         cmd.ien_to_len16 = cpu_to_be32(FW_VI_ENABLE_CMD_LED |
922                                        FW_LEN16(cmd));
923         cmd.blinkdur = cpu_to_be16(nblinks);
924         return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
925 }
926
927 /**
928  *      t4vf_set_rxmode - set Rx properties of a virtual interface
929  *      @adapter: the adapter
930  *      @viid: the VI id
931  *      @mtu: the new MTU or -1 for no change
932  *      @promisc: 1 to enable promiscuous mode, 0 to disable it, -1 no change
933  *      @all_multi: 1 to enable all-multi mode, 0 to disable it, -1 no change
934  *      @bcast: 1 to enable broadcast Rx, 0 to disable it, -1 no change
935  *      @vlanex: 1 to enable hardware VLAN Tag extraction, 0 to disable it,
936  *              -1 no change
937  *
938  *      Sets Rx properties of a virtual interface.
939  */
940 int t4vf_set_rxmode(struct adapter *adapter, unsigned int viid,
941                     int mtu, int promisc, int all_multi, int bcast, int vlanex,
942                     bool sleep_ok)
943 {
944         struct fw_vi_rxmode_cmd cmd;
945
946         /* convert to FW values */
947         if (mtu < 0)
948                 mtu = FW_VI_RXMODE_CMD_MTU_MASK;
949         if (promisc < 0)
950                 promisc = FW_VI_RXMODE_CMD_PROMISCEN_MASK;
951         if (all_multi < 0)
952                 all_multi = FW_VI_RXMODE_CMD_ALLMULTIEN_MASK;
953         if (bcast < 0)
954                 bcast = FW_VI_RXMODE_CMD_BROADCASTEN_MASK;
955         if (vlanex < 0)
956                 vlanex = FW_VI_RXMODE_CMD_VLANEXEN_MASK;
957
958         memset(&cmd, 0, sizeof(cmd));
959         cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_RXMODE_CMD) |
960                                      FW_CMD_REQUEST |
961                                      FW_CMD_WRITE |
962                                      FW_VI_RXMODE_CMD_VIID(viid));
963         cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
964         cmd.mtu_to_vlanexen =
965                 cpu_to_be32(FW_VI_RXMODE_CMD_MTU(mtu) |
966                             FW_VI_RXMODE_CMD_PROMISCEN(promisc) |
967                             FW_VI_RXMODE_CMD_ALLMULTIEN(all_multi) |
968                             FW_VI_RXMODE_CMD_BROADCASTEN(bcast) |
969                             FW_VI_RXMODE_CMD_VLANEXEN(vlanex));
970         return t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), NULL, sleep_ok);
971 }
972
973 /**
974  *      t4vf_alloc_mac_filt - allocates exact-match filters for MAC addresses
975  *      @adapter: the adapter
976  *      @viid: the Virtual Interface Identifier
977  *      @free: if true any existing filters for this VI id are first removed
978  *      @naddr: the number of MAC addresses to allocate filters for (up to 7)
979  *      @addr: the MAC address(es)
980  *      @idx: where to store the index of each allocated filter
981  *      @hash: pointer to hash address filter bitmap
982  *      @sleep_ok: call is allowed to sleep
983  *
984  *      Allocates an exact-match filter for each of the supplied addresses and
985  *      sets it to the corresponding address.  If @idx is not %NULL it should
986  *      have at least @naddr entries, each of which will be set to the index of
987  *      the filter allocated for the corresponding MAC address.  If a filter
988  *      could not be allocated for an address its index is set to 0xffff.
989  *      If @hash is not %NULL addresses that fail to allocate an exact filter
990  *      are hashed and update the hash filter bitmap pointed at by @hash.
991  *
992  *      Returns a negative error number or the number of filters allocated.
993  */
994 int t4vf_alloc_mac_filt(struct adapter *adapter, unsigned int viid, bool free,
995                         unsigned int naddr, const u8 **addr, u16 *idx,
996                         u64 *hash, bool sleep_ok)
997 {
998         int i, ret;
999         struct fw_vi_mac_cmd cmd, rpl;
1000         struct fw_vi_mac_exact *p;
1001         size_t len16;
1002
1003         if (naddr > ARRAY_SIZE(cmd.u.exact))
1004                 return -EINVAL;
1005         len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
1006                                       u.exact[naddr]), 16);
1007
1008         memset(&cmd, 0, sizeof(cmd));
1009         cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_MAC_CMD) |
1010                                      FW_CMD_REQUEST |
1011                                      FW_CMD_WRITE |
1012                                      (free ? FW_CMD_EXEC : 0) |
1013                                      FW_VI_MAC_CMD_VIID(viid));
1014         cmd.freemacs_to_len16 = cpu_to_be32(FW_VI_MAC_CMD_FREEMACS(free) |
1015                                             FW_CMD_LEN16(len16));
1016
1017         for (i = 0, p = cmd.u.exact; i < naddr; i++, p++) {
1018                 p->valid_to_idx =
1019                         cpu_to_be16(FW_VI_MAC_CMD_VALID |
1020                                     FW_VI_MAC_CMD_IDX(FW_VI_MAC_ADD_MAC));
1021                 memcpy(p->macaddr, addr[i], sizeof(p->macaddr));
1022         }
1023
1024         ret = t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), &rpl, sleep_ok);
1025         if (ret)
1026                 return ret;
1027
1028         for (i = 0, p = rpl.u.exact; i < naddr; i++, p++) {
1029                 u16 index = FW_VI_MAC_CMD_IDX_GET(be16_to_cpu(p->valid_to_idx));
1030
1031                 if (idx)
1032                         idx[i] = (index >= FW_CLS_TCAM_NUM_ENTRIES
1033                                   ? 0xffff
1034                                   : index);
1035                 if (index < FW_CLS_TCAM_NUM_ENTRIES)
1036                         ret++;
1037                 else if (hash)
1038                         *hash |= (1 << hash_mac_addr(addr[i]));
1039         }
1040         return ret;
1041 }
1042
1043 /**
1044  *      t4vf_change_mac - modifies the exact-match filter for a MAC address
1045  *      @adapter: the adapter
1046  *      @viid: the Virtual Interface ID
1047  *      @idx: index of existing filter for old value of MAC address, or -1
1048  *      @addr: the new MAC address value
1049  *      @persist: if idx < 0, the new MAC allocation should be persistent
1050  *
1051  *      Modifies an exact-match filter and sets it to the new MAC address.
1052  *      Note that in general it is not possible to modify the value of a given
1053  *      filter so the generic way to modify an address filter is to free the
1054  *      one being used by the old address value and allocate a new filter for
1055  *      the new address value.  @idx can be -1 if the address is a new
1056  *      addition.
1057  *
1058  *      Returns a negative error number or the index of the filter with the new
1059  *      MAC value.
1060  */
1061 int t4vf_change_mac(struct adapter *adapter, unsigned int viid,
1062                     int idx, const u8 *addr, bool persist)
1063 {
1064         int ret;
1065         struct fw_vi_mac_cmd cmd, rpl;
1066         struct fw_vi_mac_exact *p = &cmd.u.exact[0];
1067         size_t len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
1068                                              u.exact[1]), 16);
1069
1070         /*
1071          * If this is a new allocation, determine whether it should be
1072          * persistent (across a "freemacs" operation) or not.
1073          */
1074         if (idx < 0)
1075                 idx = persist ? FW_VI_MAC_ADD_PERSIST_MAC : FW_VI_MAC_ADD_MAC;
1076
1077         memset(&cmd, 0, sizeof(cmd));
1078         cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_MAC_CMD) |
1079                                      FW_CMD_REQUEST |
1080                                      FW_CMD_WRITE |
1081                                      FW_VI_MAC_CMD_VIID(viid));
1082         cmd.freemacs_to_len16 = cpu_to_be32(FW_CMD_LEN16(len16));
1083         p->valid_to_idx = cpu_to_be16(FW_VI_MAC_CMD_VALID |
1084                                       FW_VI_MAC_CMD_IDX(idx));
1085         memcpy(p->macaddr, addr, sizeof(p->macaddr));
1086
1087         ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
1088         if (ret == 0) {
1089                 p = &rpl.u.exact[0];
1090                 ret = FW_VI_MAC_CMD_IDX_GET(be16_to_cpu(p->valid_to_idx));
1091                 if (ret >= FW_CLS_TCAM_NUM_ENTRIES)
1092                         ret = -ENOMEM;
1093         }
1094         return ret;
1095 }
1096
1097 /**
1098  *      t4vf_set_addr_hash - program the MAC inexact-match hash filter
1099  *      @adapter: the adapter
1100  *      @viid: the Virtual Interface Identifier
1101  *      @ucast: whether the hash filter should also match unicast addresses
1102  *      @vec: the value to be written to the hash filter
1103  *      @sleep_ok: call is allowed to sleep
1104  *
1105  *      Sets the 64-bit inexact-match hash filter for a virtual interface.
1106  */
1107 int t4vf_set_addr_hash(struct adapter *adapter, unsigned int viid,
1108                        bool ucast, u64 vec, bool sleep_ok)
1109 {
1110         struct fw_vi_mac_cmd cmd;
1111         size_t len16 = DIV_ROUND_UP(offsetof(struct fw_vi_mac_cmd,
1112                                              u.exact[0]), 16);
1113
1114         memset(&cmd, 0, sizeof(cmd));
1115         cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_MAC_CMD) |
1116                                      FW_CMD_REQUEST |
1117                                      FW_CMD_WRITE |
1118                                      FW_VI_ENABLE_CMD_VIID(viid));
1119         cmd.freemacs_to_len16 = cpu_to_be32(FW_VI_MAC_CMD_HASHVECEN |
1120                                             FW_VI_MAC_CMD_HASHUNIEN(ucast) |
1121                                             FW_CMD_LEN16(len16));
1122         cmd.u.hash.hashvec = cpu_to_be64(vec);
1123         return t4vf_wr_mbox_core(adapter, &cmd, sizeof(cmd), NULL, sleep_ok);
1124 }
1125
1126 /**
1127  *      t4vf_get_port_stats - collect "port" statistics
1128  *      @adapter: the adapter
1129  *      @pidx: the port index
1130  *      @s: the stats structure to fill
1131  *
1132  *      Collect statistics for the "port"'s Virtual Interface.
1133  */
1134 int t4vf_get_port_stats(struct adapter *adapter, int pidx,
1135                         struct t4vf_port_stats *s)
1136 {
1137         struct port_info *pi = adap2pinfo(adapter, pidx);
1138         struct fw_vi_stats_vf fwstats;
1139         unsigned int rem = VI_VF_NUM_STATS;
1140         __be64 *fwsp = (__be64 *)&fwstats;
1141
1142         /*
1143          * Grab the Virtual Interface statistics a chunk at a time via mailbox
1144          * commands.  We could use a Work Request and get all of them at once
1145          * but that's an asynchronous interface which is awkward to use.
1146          */
1147         while (rem) {
1148                 unsigned int ix = VI_VF_NUM_STATS - rem;
1149                 unsigned int nstats = min(6U, rem);
1150                 struct fw_vi_stats_cmd cmd, rpl;
1151                 size_t len = (offsetof(struct fw_vi_stats_cmd, u) +
1152                               sizeof(struct fw_vi_stats_ctl));
1153                 size_t len16 = DIV_ROUND_UP(len, 16);
1154                 int ret;
1155
1156                 memset(&cmd, 0, sizeof(cmd));
1157                 cmd.op_to_viid = cpu_to_be32(FW_CMD_OP(FW_VI_STATS_CMD) |
1158                                              FW_VI_STATS_CMD_VIID(pi->viid) |
1159                                              FW_CMD_REQUEST |
1160                                              FW_CMD_READ);
1161                 cmd.retval_len16 = cpu_to_be32(FW_CMD_LEN16(len16));
1162                 cmd.u.ctl.nstats_ix =
1163                         cpu_to_be16(FW_VI_STATS_CMD_IX(ix) |
1164                                     FW_VI_STATS_CMD_NSTATS(nstats));
1165                 ret = t4vf_wr_mbox_ns(adapter, &cmd, len, &rpl);
1166                 if (ret)
1167                         return ret;
1168
1169                 memcpy(fwsp, &rpl.u.ctl.stat0, sizeof(__be64) * nstats);
1170
1171                 rem -= nstats;
1172                 fwsp += nstats;
1173         }
1174
1175         /*
1176          * Translate firmware statistics into host native statistics.
1177          */
1178         s->tx_bcast_bytes = be64_to_cpu(fwstats.tx_bcast_bytes);
1179         s->tx_bcast_frames = be64_to_cpu(fwstats.tx_bcast_frames);
1180         s->tx_mcast_bytes = be64_to_cpu(fwstats.tx_mcast_bytes);
1181         s->tx_mcast_frames = be64_to_cpu(fwstats.tx_mcast_frames);
1182         s->tx_ucast_bytes = be64_to_cpu(fwstats.tx_ucast_bytes);
1183         s->tx_ucast_frames = be64_to_cpu(fwstats.tx_ucast_frames);
1184         s->tx_drop_frames = be64_to_cpu(fwstats.tx_drop_frames);
1185         s->tx_offload_bytes = be64_to_cpu(fwstats.tx_offload_bytes);
1186         s->tx_offload_frames = be64_to_cpu(fwstats.tx_offload_frames);
1187
1188         s->rx_bcast_bytes = be64_to_cpu(fwstats.rx_bcast_bytes);
1189         s->rx_bcast_frames = be64_to_cpu(fwstats.rx_bcast_frames);
1190         s->rx_mcast_bytes = be64_to_cpu(fwstats.rx_mcast_bytes);
1191         s->rx_mcast_frames = be64_to_cpu(fwstats.rx_mcast_frames);
1192         s->rx_ucast_bytes = be64_to_cpu(fwstats.rx_ucast_bytes);
1193         s->rx_ucast_frames = be64_to_cpu(fwstats.rx_ucast_frames);
1194
1195         s->rx_err_frames = be64_to_cpu(fwstats.rx_err_frames);
1196
1197         return 0;
1198 }
1199
1200 /**
1201  *      t4vf_iq_free - free an ingress queue and its free lists
1202  *      @adapter: the adapter
1203  *      @iqtype: the ingress queue type (FW_IQ_TYPE_FL_INT_CAP, etc.)
1204  *      @iqid: ingress queue ID
1205  *      @fl0id: FL0 queue ID or 0xffff if no attached FL0
1206  *      @fl1id: FL1 queue ID or 0xffff if no attached FL1
1207  *
1208  *      Frees an ingress queue and its associated free lists, if any.
1209  */
1210 int t4vf_iq_free(struct adapter *adapter, unsigned int iqtype,
1211                  unsigned int iqid, unsigned int fl0id, unsigned int fl1id)
1212 {
1213         struct fw_iq_cmd cmd;
1214
1215         memset(&cmd, 0, sizeof(cmd));
1216         cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_IQ_CMD) |
1217                                     FW_CMD_REQUEST |
1218                                     FW_CMD_EXEC);
1219         cmd.alloc_to_len16 = cpu_to_be32(FW_IQ_CMD_FREE |
1220                                          FW_LEN16(cmd));
1221         cmd.type_to_iqandstindex =
1222                 cpu_to_be32(FW_IQ_CMD_TYPE(iqtype));
1223
1224         cmd.iqid = cpu_to_be16(iqid);
1225         cmd.fl0id = cpu_to_be16(fl0id);
1226         cmd.fl1id = cpu_to_be16(fl1id);
1227         return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
1228 }
1229
1230 /**
1231  *      t4vf_eth_eq_free - free an Ethernet egress queue
1232  *      @adapter: the adapter
1233  *      @eqid: egress queue ID
1234  *
1235  *      Frees an Ethernet egress queue.
1236  */
1237 int t4vf_eth_eq_free(struct adapter *adapter, unsigned int eqid)
1238 {
1239         struct fw_eq_eth_cmd cmd;
1240
1241         memset(&cmd, 0, sizeof(cmd));
1242         cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP(FW_EQ_ETH_CMD) |
1243                                     FW_CMD_REQUEST |
1244                                     FW_CMD_EXEC);
1245         cmd.alloc_to_len16 = cpu_to_be32(FW_EQ_ETH_CMD_FREE |
1246                                          FW_LEN16(cmd));
1247         cmd.eqid_pkd = cpu_to_be32(FW_EQ_ETH_CMD_EQID(eqid));
1248         return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
1249 }
1250
1251 /**
1252  *      t4vf_handle_fw_rpl - process a firmware reply message
1253  *      @adapter: the adapter
1254  *      @rpl: start of the firmware message
1255  *
1256  *      Processes a firmware message, such as link state change messages.
1257  */
1258 int t4vf_handle_fw_rpl(struct adapter *adapter, const __be64 *rpl)
1259 {
1260         struct fw_cmd_hdr *cmd_hdr = (struct fw_cmd_hdr *)rpl;
1261         u8 opcode = FW_CMD_OP_GET(be32_to_cpu(cmd_hdr->hi));
1262
1263         switch (opcode) {
1264         case FW_PORT_CMD: {
1265                 /*
1266                  * Link/module state change message.
1267                  */
1268                 const struct fw_port_cmd *port_cmd = (void *)rpl;
1269                 u32 word;
1270                 int action, port_id, link_ok, speed, fc, pidx;
1271
1272                 /*
1273                  * Extract various fields from port status change message.
1274                  */
1275                 action = FW_PORT_CMD_ACTION_GET(
1276                         be32_to_cpu(port_cmd->action_to_len16));
1277                 if (action != FW_PORT_ACTION_GET_PORT_INFO) {
1278                         dev_err(adapter->pdev_dev,
1279                                 "Unknown firmware PORT reply action %x\n",
1280                                 action);
1281                         break;
1282                 }
1283
1284                 port_id = FW_PORT_CMD_PORTID_GET(
1285                         be32_to_cpu(port_cmd->op_to_portid));
1286
1287                 word = be32_to_cpu(port_cmd->u.info.lstatus_to_modtype);
1288                 link_ok = (word & FW_PORT_CMD_LSTATUS) != 0;
1289                 speed = 0;
1290                 fc = 0;
1291                 if (word & FW_PORT_CMD_RXPAUSE)
1292                         fc |= PAUSE_RX;
1293                 if (word & FW_PORT_CMD_TXPAUSE)
1294                         fc |= PAUSE_TX;
1295                 if (word & FW_PORT_CMD_LSPEED(FW_PORT_CAP_SPEED_100M))
1296                         speed = SPEED_100;
1297                 else if (word & FW_PORT_CMD_LSPEED(FW_PORT_CAP_SPEED_1G))
1298                         speed = SPEED_1000;
1299                 else if (word & FW_PORT_CMD_LSPEED(FW_PORT_CAP_SPEED_10G))
1300                         speed = SPEED_10000;
1301
1302                 /*
1303                  * Scan all of our "ports" (Virtual Interfaces) looking for
1304                  * those bound to the physical port which has changed.  If
1305                  * our recorded state doesn't match the current state,
1306                  * signal that change to the OS code.
1307                  */
1308                 for_each_port(adapter, pidx) {
1309                         struct port_info *pi = adap2pinfo(adapter, pidx);
1310                         struct link_config *lc;
1311
1312                         if (pi->port_id != port_id)
1313                                 continue;
1314
1315                         lc = &pi->link_cfg;
1316                         if (link_ok != lc->link_ok || speed != lc->speed ||
1317                             fc != lc->fc) {
1318                                 /* something changed */
1319                                 lc->link_ok = link_ok;
1320                                 lc->speed = speed;
1321                                 lc->fc = fc;
1322                                 t4vf_os_link_changed(adapter, pidx, link_ok);
1323                         }
1324                 }
1325                 break;
1326         }
1327
1328         default:
1329                 dev_err(adapter->pdev_dev, "Unknown firmware reply %X\n",
1330                         opcode);
1331         }
1332         return 0;
1333 }