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