net/cxgbe: update link speeds and port modules
[dpdk.git] / drivers / net / cxgbe / cxgbe_main.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2014-2017 Chelsio Communications.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Chelsio Communications nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <sys/queue.h>
35 #include <stdio.h>
36 #include <errno.h>
37 #include <stdint.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <stdarg.h>
41 #include <inttypes.h>
42 #include <netinet/in.h>
43
44 #include <rte_byteorder.h>
45 #include <rte_common.h>
46 #include <rte_cycles.h>
47 #include <rte_interrupts.h>
48 #include <rte_log.h>
49 #include <rte_debug.h>
50 #include <rte_pci.h>
51 #include <rte_atomic.h>
52 #include <rte_branch_prediction.h>
53 #include <rte_memory.h>
54 #include <rte_memzone.h>
55 #include <rte_tailq.h>
56 #include <rte_eal.h>
57 #include <rte_alarm.h>
58 #include <rte_ether.h>
59 #include <rte_ethdev.h>
60 #include <rte_ethdev_pci.h>
61 #include <rte_atomic.h>
62 #include <rte_malloc.h>
63 #include <rte_random.h>
64 #include <rte_dev.h>
65
66 #include "common.h"
67 #include "t4_regs.h"
68 #include "t4_msg.h"
69 #include "cxgbe.h"
70
71 /*
72  * Response queue handler for the FW event queue.
73  */
74 static int fwevtq_handler(struct sge_rspq *q, const __be64 *rsp,
75                           __rte_unused const struct pkt_gl *gl)
76 {
77         u8 opcode = ((const struct rss_header *)rsp)->opcode;
78
79         rsp++;                                          /* skip RSS header */
80
81         /*
82          * FW can send EGR_UPDATEs encapsulated in a CPL_FW4_MSG.
83          */
84         if (unlikely(opcode == CPL_FW4_MSG &&
85                      ((const struct cpl_fw4_msg *)rsp)->type ==
86                       FW_TYPE_RSSCPL)) {
87                 rsp++;
88                 opcode = ((const struct rss_header *)rsp)->opcode;
89                 rsp++;
90                 if (opcode != CPL_SGE_EGR_UPDATE) {
91                         dev_err(q->adapter, "unexpected FW4/CPL %#x on FW event queue\n",
92                                 opcode);
93                         goto out;
94                 }
95         }
96
97         if (likely(opcode == CPL_SGE_EGR_UPDATE)) {
98                 /* do nothing */
99         } else if (opcode == CPL_FW6_MSG || opcode == CPL_FW4_MSG) {
100                 const struct cpl_fw6_msg *msg = (const void *)rsp;
101
102                 t4_handle_fw_rpl(q->adapter, msg->data);
103         } else {
104                 dev_err(adapter, "unexpected CPL %#x on FW event queue\n",
105                         opcode);
106         }
107 out:
108         return 0;
109 }
110
111 int setup_sge_fwevtq(struct adapter *adapter)
112 {
113         struct sge *s = &adapter->sge;
114         int err = 0;
115         int msi_idx = 0;
116
117         err = t4_sge_alloc_rxq(adapter, &s->fw_evtq, true, adapter->eth_dev,
118                                msi_idx, NULL, fwevtq_handler, -1, NULL, 0,
119                                rte_socket_id());
120         return err;
121 }
122
123 static int closest_timer(const struct sge *s, int time)
124 {
125         unsigned int i, match = 0;
126         int delta, min_delta = INT_MAX;
127
128         for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
129                 delta = time - s->timer_val[i];
130                 if (delta < 0)
131                         delta = -delta;
132                 if (delta < min_delta) {
133                         min_delta = delta;
134                         match = i;
135                 }
136         }
137         return match;
138 }
139
140 static int closest_thres(const struct sge *s, int thres)
141 {
142         unsigned int i, match = 0;
143         int delta, min_delta = INT_MAX;
144
145         for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
146                 delta = thres - s->counter_val[i];
147                 if (delta < 0)
148                         delta = -delta;
149                 if (delta < min_delta) {
150                         min_delta = delta;
151                         match = i;
152                 }
153         }
154         return match;
155 }
156
157 /**
158  * cxgb4_set_rspq_intr_params - set a queue's interrupt holdoff parameters
159  * @q: the Rx queue
160  * @us: the hold-off time in us, or 0 to disable timer
161  * @cnt: the hold-off packet count, or 0 to disable counter
162  *
163  * Sets an Rx queue's interrupt hold-off time and packet count.  At least
164  * one of the two needs to be enabled for the queue to generate interrupts.
165  */
166 int cxgb4_set_rspq_intr_params(struct sge_rspq *q, unsigned int us,
167                                unsigned int cnt)
168 {
169         struct adapter *adap = q->adapter;
170         unsigned int timer_val;
171
172         if (cnt) {
173                 int err;
174                 u32 v, new_idx;
175
176                 new_idx = closest_thres(&adap->sge, cnt);
177                 if (q->desc && q->pktcnt_idx != new_idx) {
178                         /* the queue has already been created, update it */
179                         v = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
180                             V_FW_PARAMS_PARAM_X(
181                             FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
182                             V_FW_PARAMS_PARAM_YZ(q->cntxt_id);
183                         err = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
184                                             &v, &new_idx);
185                         if (err)
186                                 return err;
187                 }
188                 q->pktcnt_idx = new_idx;
189         }
190
191         timer_val = (us == 0) ? X_TIMERREG_RESTART_COUNTER :
192                                 closest_timer(&adap->sge, us);
193
194         if ((us | cnt) == 0)
195                 q->intr_params = V_QINTR_TIMER_IDX(X_TIMERREG_UPDATE_CIDX);
196         else
197                 q->intr_params = V_QINTR_TIMER_IDX(timer_val) |
198                                  V_QINTR_CNT_EN(cnt > 0);
199         return 0;
200 }
201
202 static inline bool is_x_1g_port(const struct link_config *lc)
203 {
204         return (lc->supported & FW_PORT_CAP_SPEED_1G) != 0;
205 }
206
207 static inline bool is_x_10g_port(const struct link_config *lc)
208 {
209         unsigned int speeds, high_speeds;
210
211         speeds = V_FW_PORT_CAP_SPEED(G_FW_PORT_CAP_SPEED(lc->supported));
212         high_speeds = speeds & ~(FW_PORT_CAP_SPEED_100M | FW_PORT_CAP_SPEED_1G);
213
214         return high_speeds != 0;
215 }
216
217 inline void init_rspq(struct adapter *adap, struct sge_rspq *q,
218                       unsigned int us, unsigned int cnt,
219                       unsigned int size, unsigned int iqe_size)
220 {
221         q->adapter = adap;
222         cxgb4_set_rspq_intr_params(q, us, cnt);
223         q->iqe_len = iqe_size;
224         q->size = size;
225 }
226
227 int cfg_queue_count(struct rte_eth_dev *eth_dev)
228 {
229         struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
230         struct adapter *adap = pi->adapter;
231         struct sge *s = &adap->sge;
232         unsigned int max_queues = s->max_ethqsets / adap->params.nports;
233
234         if ((eth_dev->data->nb_rx_queues < 1) ||
235             (eth_dev->data->nb_tx_queues < 1))
236                 return -EINVAL;
237
238         if ((eth_dev->data->nb_rx_queues > max_queues) ||
239             (eth_dev->data->nb_tx_queues > max_queues))
240                 return -EINVAL;
241
242         if (eth_dev->data->nb_rx_queues > pi->rss_size)
243                 return -EINVAL;
244
245         /* We must configure RSS, since config has changed*/
246         pi->flags &= ~PORT_RSS_DONE;
247
248         pi->n_rx_qsets = eth_dev->data->nb_rx_queues;
249         pi->n_tx_qsets = eth_dev->data->nb_tx_queues;
250
251         return 0;
252 }
253
254 void cfg_queues(struct rte_eth_dev *eth_dev)
255 {
256         struct rte_config *config = rte_eal_get_configuration();
257         struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
258         struct adapter *adap = pi->adapter;
259         struct sge *s = &adap->sge;
260         unsigned int i, nb_ports = 0, qidx = 0;
261         unsigned int q_per_port = 0;
262
263         if (!(adap->flags & CFG_QUEUES)) {
264                 for_each_port(adap, i) {
265                         struct port_info *tpi = adap2pinfo(adap, i);
266
267                         nb_ports += (is_x_10g_port(&tpi->link_cfg)) ||
268                                      is_x_1g_port(&tpi->link_cfg) ? 1 : 0;
269                 }
270
271                 /*
272                  * We default up to # of cores queues per 1G/10G port.
273                  */
274                 if (nb_ports)
275                         q_per_port = (MAX_ETH_QSETS -
276                                      (adap->params.nports - nb_ports)) /
277                                      nb_ports;
278
279                 if (q_per_port > config->lcore_count)
280                         q_per_port = config->lcore_count;
281
282                 for_each_port(adap, i) {
283                         struct port_info *pi = adap2pinfo(adap, i);
284
285                         pi->first_qset = qidx;
286
287                         /* Initially n_rx_qsets == n_tx_qsets */
288                         pi->n_rx_qsets = (is_x_10g_port(&pi->link_cfg) ||
289                                           is_x_1g_port(&pi->link_cfg)) ?
290                                           q_per_port : 1;
291                         pi->n_tx_qsets = pi->n_rx_qsets;
292
293                         if (pi->n_rx_qsets > pi->rss_size)
294                                 pi->n_rx_qsets = pi->rss_size;
295
296                         qidx += pi->n_rx_qsets;
297                 }
298
299                 s->max_ethqsets = qidx;
300
301                 for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
302                         struct sge_eth_rxq *r = &s->ethrxq[i];
303
304                         init_rspq(adap, &r->rspq, 0, 0, 1024, 64);
305                         r->usembufs = 1;
306                         r->fl.size = (r->usembufs ? 1024 : 72);
307                 }
308
309                 for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
310                         s->ethtxq[i].q.size = 1024;
311
312                 init_rspq(adap, &adap->sge.fw_evtq, 0, 0, 1024, 64);
313                 adap->flags |= CFG_QUEUES;
314         }
315 }
316
317 void cxgbe_stats_get(struct port_info *pi, struct port_stats *stats)
318 {
319         t4_get_port_stats_offset(pi->adapter, pi->tx_chan, stats,
320                                  &pi->stats_base);
321 }
322
323 void cxgbe_stats_reset(struct port_info *pi)
324 {
325         t4_clr_port_stats(pi->adapter, pi->tx_chan);
326 }
327
328 static void setup_memwin(struct adapter *adap)
329 {
330         u32 mem_win0_base;
331
332         /* For T5, only relative offset inside the PCIe BAR is passed */
333         mem_win0_base = MEMWIN0_BASE;
334
335         /*
336          * Set up memory window for accessing adapter memory ranges.  (Read
337          * back MA register to ensure that changes propagate before we attempt
338          * to use the new values.)
339          */
340         t4_write_reg(adap,
341                      PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN,
342                                          MEMWIN_NIC),
343                      mem_win0_base | V_BIR(0) |
344                      V_WINDOW(ilog2(MEMWIN0_APERTURE) - X_WINDOW_SHIFT));
345         t4_read_reg(adap,
346                     PCIE_MEM_ACCESS_REG(A_PCIE_MEM_ACCESS_BASE_WIN,
347                                         MEMWIN_NIC));
348 }
349
350 static int init_rss(struct adapter *adap)
351 {
352         unsigned int i;
353         int err;
354
355         err = t4_init_rss_mode(adap, adap->mbox);
356         if (err)
357                 return err;
358
359         for_each_port(adap, i) {
360                 struct port_info *pi = adap2pinfo(adap, i);
361
362                 pi->rss = rte_zmalloc(NULL, pi->rss_size * sizeof(u16), 0);
363                 if (!pi->rss)
364                         return -ENOMEM;
365         }
366         return 0;
367 }
368
369 static void print_port_info(struct adapter *adap)
370 {
371         int i;
372         char buf[80];
373         struct rte_pci_addr *loc = &adap->pdev->addr;
374
375         for_each_port(adap, i) {
376                 const struct port_info *pi = &adap->port[i];
377                 char *bufp = buf;
378
379                 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_100M)
380                         bufp += sprintf(bufp, "100M/");
381                 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_1G)
382                         bufp += sprintf(bufp, "1G/");
383                 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_10G)
384                         bufp += sprintf(bufp, "10G/");
385                 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_25G)
386                         bufp += sprintf(bufp, "25G/");
387                 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_40G)
388                         bufp += sprintf(bufp, "40G/");
389                 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_100G)
390                         bufp += sprintf(bufp, "100G/");
391                 if (bufp != buf)
392                         --bufp;
393                 sprintf(bufp, "BASE-%s",
394                         t4_get_port_type_description(
395                                         (enum fw_port_type)pi->port_type));
396
397                 dev_info(adap,
398                          " " PCI_PRI_FMT " Chelsio rev %d %s %s\n",
399                          loc->domain, loc->bus, loc->devid, loc->function,
400                          CHELSIO_CHIP_RELEASE(adap->params.chip), buf,
401                          (adap->flags & USING_MSIX) ? " MSI-X" :
402                          (adap->flags & USING_MSI) ? " MSI" : "");
403         }
404 }
405
406 /*
407  * Tweak configuration based on system architecture, etc.  Most of these have
408  * defaults assigned to them by Firmware Configuration Files (if we're using
409  * them) but need to be explicitly set if we're using hard-coded
410  * initialization. So these are essentially common tweaks/settings for
411  * Configuration Files and hard-coded initialization ...
412  */
413 static int adap_init0_tweaks(struct adapter *adapter)
414 {
415         u8 rx_dma_offset;
416
417         /*
418          * Fix up various Host-Dependent Parameters like Page Size, Cache
419          * Line Size, etc.  The firmware default is for a 4KB Page Size and
420          * 64B Cache Line Size ...
421          */
422         t4_fixup_host_params_compat(adapter, CXGBE_PAGE_SIZE, L1_CACHE_BYTES,
423                                     T5_LAST_REV);
424
425         /*
426          * Keep the chip default offset to deliver Ingress packets into our
427          * DMA buffers to zero
428          */
429         rx_dma_offset = 0;
430         t4_set_reg_field(adapter, A_SGE_CONTROL, V_PKTSHIFT(M_PKTSHIFT),
431                          V_PKTSHIFT(rx_dma_offset));
432
433         t4_set_reg_field(adapter, A_SGE_FLM_CFG,
434                          V_CREDITCNT(M_CREDITCNT) | M_CREDITCNTPACKING,
435                          V_CREDITCNT(3) | V_CREDITCNTPACKING(1));
436
437         t4_set_reg_field(adapter, A_SGE_CONTROL2, V_IDMAARBROUNDROBIN(1U),
438                          V_IDMAARBROUNDROBIN(1U));
439
440         /*
441          * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
442          * adds the pseudo header itself.
443          */
444         t4_tp_wr_bits_indirect(adapter, A_TP_INGRESS_CONFIG,
445                                F_CSUM_HAS_PSEUDO_HDR, 0);
446
447         return 0;
448 }
449
450 /*
451  * Attempt to initialize the adapter via a Firmware Configuration File.
452  */
453 static int adap_init0_config(struct adapter *adapter, int reset)
454 {
455         struct fw_caps_config_cmd caps_cmd;
456         unsigned long mtype = 0, maddr = 0;
457         u32 finiver, finicsum, cfcsum;
458         int ret;
459         int config_issued = 0;
460         int cfg_addr;
461         char config_name[20];
462
463         /*
464          * Reset device if necessary.
465          */
466         if (reset) {
467                 ret = t4_fw_reset(adapter, adapter->mbox,
468                                   F_PIORSTMODE | F_PIORST);
469                 if (ret < 0) {
470                         dev_warn(adapter, "Firmware reset failed, error %d\n",
471                                  -ret);
472                         goto bye;
473                 }
474         }
475
476         cfg_addr = t4_flash_cfg_addr(adapter);
477         if (cfg_addr < 0) {
478                 ret = cfg_addr;
479                 dev_warn(adapter, "Finding address for firmware config file in flash failed, error %d\n",
480                          -ret);
481                 goto bye;
482         }
483
484         strcpy(config_name, "On Flash");
485         mtype = FW_MEMTYPE_CF_FLASH;
486         maddr = cfg_addr;
487
488         /*
489          * Issue a Capability Configuration command to the firmware to get it
490          * to parse the Configuration File.  We don't use t4_fw_config_file()
491          * because we want the ability to modify various features after we've
492          * processed the configuration file ...
493          */
494         memset(&caps_cmd, 0, sizeof(caps_cmd));
495         caps_cmd.op_to_write = cpu_to_be32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
496                                            F_FW_CMD_REQUEST | F_FW_CMD_READ);
497         caps_cmd.cfvalid_to_len16 =
498                 cpu_to_be32(F_FW_CAPS_CONFIG_CMD_CFVALID |
499                             V_FW_CAPS_CONFIG_CMD_MEMTYPE_CF(mtype) |
500                             V_FW_CAPS_CONFIG_CMD_MEMADDR64K_CF(maddr >> 16) |
501                             FW_LEN16(caps_cmd));
502         ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
503                          &caps_cmd);
504         /*
505          * If the CAPS_CONFIG failed with an ENOENT (for a Firmware
506          * Configuration File in FLASH), our last gasp effort is to use the
507          * Firmware Configuration File which is embedded in the firmware.  A
508          * very few early versions of the firmware didn't have one embedded
509          * but we can ignore those.
510          */
511         if (ret == -ENOENT) {
512                 dev_info(adapter, "%s: Going for embedded config in firmware..\n",
513                          __func__);
514
515                 memset(&caps_cmd, 0, sizeof(caps_cmd));
516                 caps_cmd.op_to_write =
517                         cpu_to_be32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
518                                     F_FW_CMD_REQUEST | F_FW_CMD_READ);
519                 caps_cmd.cfvalid_to_len16 = cpu_to_be32(FW_LEN16(caps_cmd));
520                 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd,
521                                  sizeof(caps_cmd), &caps_cmd);
522                 strcpy(config_name, "Firmware Default");
523         }
524
525         config_issued = 1;
526         if (ret < 0)
527                 goto bye;
528
529         finiver = be32_to_cpu(caps_cmd.finiver);
530         finicsum = be32_to_cpu(caps_cmd.finicsum);
531         cfcsum = be32_to_cpu(caps_cmd.cfcsum);
532         if (finicsum != cfcsum)
533                 dev_warn(adapter, "Configuration File checksum mismatch: [fini] csum=%#x, computed csum=%#x\n",
534                          finicsum, cfcsum);
535
536         /*
537          * If we're a pure NIC driver then disable all offloading facilities.
538          * This will allow the firmware to optimize aspects of the hardware
539          * configuration which will result in improved performance.
540          */
541         caps_cmd.niccaps &= cpu_to_be16(~(FW_CAPS_CONFIG_NIC_HASHFILTER |
542                                           FW_CAPS_CONFIG_NIC_ETHOFLD));
543         caps_cmd.toecaps = 0;
544         caps_cmd.iscsicaps = 0;
545         caps_cmd.rdmacaps = 0;
546         caps_cmd.fcoecaps = 0;
547
548         /*
549          * And now tell the firmware to use the configuration we just loaded.
550          */
551         caps_cmd.op_to_write = cpu_to_be32(V_FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
552                                            F_FW_CMD_REQUEST | F_FW_CMD_WRITE);
553         caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
554         ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
555                          NULL);
556         if (ret < 0) {
557                 dev_warn(adapter, "Unable to finalize Firmware Capabilities %d\n",
558                          -ret);
559                 goto bye;
560         }
561
562         /*
563          * Tweak configuration based on system architecture, etc.
564          */
565         ret = adap_init0_tweaks(adapter);
566         if (ret < 0) {
567                 dev_warn(adapter, "Unable to do init0-tweaks %d\n", -ret);
568                 goto bye;
569         }
570
571         /*
572          * And finally tell the firmware to initialize itself using the
573          * parameters from the Configuration File.
574          */
575         ret = t4_fw_initialize(adapter, adapter->mbox);
576         if (ret < 0) {
577                 dev_warn(adapter, "Initializing Firmware failed, error %d\n",
578                          -ret);
579                 goto bye;
580         }
581
582         /*
583          * Return successfully and note that we're operating with parameters
584          * not supplied by the driver, rather than from hard-wired
585          * initialization constants burried in the driver.
586          */
587         dev_info(adapter,
588                  "Successfully configured using Firmware Configuration File \"%s\", version %#x, computed checksum %#x\n",
589                  config_name, finiver, cfcsum);
590
591         return 0;
592
593         /*
594          * Something bad happened.  Return the error ...  (If the "error"
595          * is that there's no Configuration File on the adapter we don't
596          * want to issue a warning since this is fairly common.)
597          */
598 bye:
599         if (config_issued && ret != -ENOENT)
600                 dev_warn(adapter, "\"%s\" configuration file error %d\n",
601                          config_name, -ret);
602
603         dev_debug(adapter, "%s: returning ret = %d ..\n", __func__, ret);
604         return ret;
605 }
606
607 static int adap_init0(struct adapter *adap)
608 {
609         int ret = 0;
610         u32 v, port_vec;
611         enum dev_state state;
612         u32 params[7], val[7];
613         int reset = 1;
614         int mbox = adap->mbox;
615
616         /*
617          * Contact FW, advertising Master capability.
618          */
619         ret = t4_fw_hello(adap, adap->mbox, adap->mbox, MASTER_MAY, &state);
620         if (ret < 0) {
621                 dev_err(adap, "%s: could not connect to FW, error %d\n",
622                         __func__, -ret);
623                 goto bye;
624         }
625
626         CXGBE_DEBUG_MBOX(adap, "%s: adap->mbox = %d; ret = %d\n", __func__,
627                          adap->mbox, ret);
628
629         if (ret == mbox)
630                 adap->flags |= MASTER_PF;
631
632         if (state == DEV_STATE_INIT) {
633                 /*
634                  * Force halt and reset FW because a previous instance may have
635                  * exited abnormally without properly shutting down
636                  */
637                 ret = t4_fw_halt(adap, adap->mbox, reset);
638                 if (ret < 0) {
639                         dev_err(adap, "Failed to halt. Exit.\n");
640                         goto bye;
641                 }
642
643                 ret = t4_fw_restart(adap, adap->mbox, reset);
644                 if (ret < 0) {
645                         dev_err(adap, "Failed to restart. Exit.\n");
646                         goto bye;
647                 }
648                 state = (enum dev_state)((unsigned)state & ~DEV_STATE_INIT);
649         }
650
651         t4_get_fw_version(adap, &adap->params.fw_vers);
652         t4_get_tp_version(adap, &adap->params.tp_vers);
653
654         dev_info(adap, "fw: %u.%u.%u.%u, TP: %u.%u.%u.%u\n",
655                  G_FW_HDR_FW_VER_MAJOR(adap->params.fw_vers),
656                  G_FW_HDR_FW_VER_MINOR(adap->params.fw_vers),
657                  G_FW_HDR_FW_VER_MICRO(adap->params.fw_vers),
658                  G_FW_HDR_FW_VER_BUILD(adap->params.fw_vers),
659                  G_FW_HDR_FW_VER_MAJOR(adap->params.tp_vers),
660                  G_FW_HDR_FW_VER_MINOR(adap->params.tp_vers),
661                  G_FW_HDR_FW_VER_MICRO(adap->params.tp_vers),
662                  G_FW_HDR_FW_VER_BUILD(adap->params.tp_vers));
663
664         ret = t4_get_core_clock(adap, &adap->params.vpd);
665         if (ret < 0) {
666                 dev_err(adap, "%s: could not get core clock, error %d\n",
667                         __func__, -ret);
668                 goto bye;
669         }
670
671         /*
672          * If the firmware is initialized already (and we're not forcing a
673          * master initialization), note that we're living with existing
674          * adapter parameters.  Otherwise, it's time to try initializing the
675          * adapter ...
676          */
677         if (state == DEV_STATE_INIT) {
678                 dev_info(adap, "Coming up as %s: Adapter already initialized\n",
679                          adap->flags & MASTER_PF ? "MASTER" : "SLAVE");
680         } else {
681                 dev_info(adap, "Coming up as MASTER: Initializing adapter\n");
682
683                 ret = adap_init0_config(adap, reset);
684                 if (ret == -ENOENT) {
685                         dev_err(adap,
686                                 "No Configuration File present on adapter. Using hard-wired configuration parameters.\n");
687                         goto bye;
688                 }
689         }
690         if (ret < 0) {
691                 dev_err(adap, "could not initialize adapter, error %d\n", -ret);
692                 goto bye;
693         }
694
695         /* Find out what ports are available to us. */
696         v = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) |
697             V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_PORTVEC);
698         ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &v, &port_vec);
699         if (ret < 0) {
700                 dev_err(adap, "%s: failure in t4_query_params; error = %d\n",
701                         __func__, ret);
702                 goto bye;
703         }
704
705         adap->params.nports = hweight32(port_vec);
706         adap->params.portvec = port_vec;
707
708         dev_debug(adap, "%s: adap->params.nports = %u\n", __func__,
709                   adap->params.nports);
710
711         /*
712          * Give the SGE code a chance to pull in anything that it needs ...
713          * Note that this must be called after we retrieve our VPD parameters
714          * in order to know how to convert core ticks to seconds, etc.
715          */
716         ret = t4_sge_init(adap);
717         if (ret < 0) {
718                 dev_err(adap, "t4_sge_init failed with error %d\n",
719                         -ret);
720                 goto bye;
721         }
722
723         /*
724          * Grab some of our basic fundamental operating parameters.
725          */
726 #define FW_PARAM_DEV(param) \
727         (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) | \
728          V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_##param))
729
730 #define FW_PARAM_PFVF(param) \
731         (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_PFVF) | \
732          V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_PFVF_##param) |  \
733          V_FW_PARAMS_PARAM_Y(0) | \
734          V_FW_PARAMS_PARAM_Z(0))
735
736         /* If we're running on newer firmware, let it know that we're
737          * prepared to deal with encapsulated CPL messages.  Older
738          * firmware won't understand this and we'll just get
739          * unencapsulated messages ...
740          */
741         params[0] = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
742         val[0] = 1;
743         (void)t4_set_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
744
745         /*
746          * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
747          * capability.  Earlier versions of the firmware didn't have the
748          * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
749          * permission to use ULPTX MEMWRITE DSGL.
750          */
751         if (is_t4(adap->params.chip)) {
752                 adap->params.ulptx_memwrite_dsgl = false;
753         } else {
754                 params[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
755                 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
756                                       1, params, val);
757                 adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
758         }
759
760         /*
761          * The MTU/MSS Table is initialized by now, so load their values.  If
762          * we're initializing the adapter, then we'll make any modifications
763          * we want to the MTU/MSS Table and also initialize the congestion
764          * parameters.
765          */
766         t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
767         if (state != DEV_STATE_INIT) {
768                 int i;
769
770                 /*
771                  * The default MTU Table contains values 1492 and 1500.
772                  * However, for TCP, it's better to have two values which are
773                  * a multiple of 8 +/- 4 bytes apart near this popular MTU.
774                  * This allows us to have a TCP Data Payload which is a
775                  * multiple of 8 regardless of what combination of TCP Options
776                  * are in use (always a multiple of 4 bytes) which is
777                  * important for performance reasons.  For instance, if no
778                  * options are in use, then we have a 20-byte IP header and a
779                  * 20-byte TCP header.  In this case, a 1500-byte MSS would
780                  * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
781                  * which is not a multiple of 8.  So using an MSS of 1488 in
782                  * this case results in a TCP Data Payload of 1448 bytes which
783                  * is a multiple of 8.  On the other hand, if 12-byte TCP Time
784                  * Stamps have been negotiated, then an MTU of 1500 bytes
785                  * results in a TCP Data Payload of 1448 bytes which, as
786                  * above, is a multiple of 8 bytes ...
787                  */
788                 for (i = 0; i < NMTUS; i++)
789                         if (adap->params.mtus[i] == 1492) {
790                                 adap->params.mtus[i] = 1488;
791                                 break;
792                         }
793
794                 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
795                              adap->params.b_wnd);
796         }
797         t4_init_sge_params(adap);
798         t4_init_tp_params(adap);
799
800         adap->params.drv_memwin = MEMWIN_NIC;
801         adap->flags |= FW_OK;
802         dev_debug(adap, "%s: returning zero..\n", __func__);
803         return 0;
804
805         /*
806          * Something bad happened.  If a command timed out or failed with EIO
807          * FW does not operate within its spec or something catastrophic
808          * happened to HW/FW, stop issuing commands.
809          */
810 bye:
811         if (ret != -ETIMEDOUT && ret != -EIO)
812                 t4_fw_bye(adap, adap->mbox);
813         return ret;
814 }
815
816 /**
817  * t4_os_portmod_changed - handle port module changes
818  * @adap: the adapter associated with the module change
819  * @port_id: the port index whose module status has changed
820  *
821  * This is the OS-dependent handler for port module changes.  It is
822  * invoked when a port module is removed or inserted for any OS-specific
823  * processing.
824  */
825 void t4_os_portmod_changed(const struct adapter *adap, int port_id)
826 {
827         static const char * const mod_str[] = {
828                 NULL, "LR", "SR", "ER", "passive DA", "active DA", "LRM"
829         };
830
831         const struct port_info *pi = &adap->port[port_id];
832
833         if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
834                 dev_info(adap, "Port%d: port module unplugged\n", pi->port_id);
835         else if (pi->mod_type < ARRAY_SIZE(mod_str))
836                 dev_info(adap, "Port%d: %s port module inserted\n", pi->port_id,
837                          mod_str[pi->mod_type]);
838         else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
839                 dev_info(adap, "Port%d: unsupported port module inserted\n",
840                          pi->port_id);
841         else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
842                 dev_info(adap, "Port%d: unknown port module inserted\n",
843                          pi->port_id);
844         else if (pi->mod_type == FW_PORT_MOD_TYPE_ERROR)
845                 dev_info(adap, "Port%d: transceiver module error\n",
846                          pi->port_id);
847         else
848                 dev_info(adap, "Port%d: unknown module type %d inserted\n",
849                          pi->port_id, pi->mod_type);
850 }
851
852 /**
853  * link_start - enable a port
854  * @dev: the port to enable
855  *
856  * Performs the MAC and PHY actions needed to enable a port.
857  */
858 int link_start(struct port_info *pi)
859 {
860         struct adapter *adapter = pi->adapter;
861         int ret;
862         unsigned int mtu;
863
864         mtu = pi->eth_dev->data->dev_conf.rxmode.max_rx_pkt_len -
865               (ETHER_HDR_LEN + ETHER_CRC_LEN);
866
867         /*
868          * We do not set address filters and promiscuity here, the stack does
869          * that step explicitly.
870          */
871         ret = t4_set_rxmode(adapter, adapter->mbox, pi->viid, mtu, -1, -1,
872                             -1, 1, true);
873         if (ret == 0) {
874                 ret = t4_change_mac(adapter, adapter->mbox, pi->viid,
875                                     pi->xact_addr_filt,
876                                     (u8 *)&pi->eth_dev->data->mac_addrs[0],
877                                     true, true);
878                 if (ret >= 0) {
879                         pi->xact_addr_filt = ret;
880                         ret = 0;
881                 }
882         }
883         if (ret == 0)
884                 ret = t4_link_l1cfg(adapter, adapter->mbox, pi->tx_chan,
885                                     &pi->link_cfg);
886         if (ret == 0) {
887                 /*
888                  * Enabling a Virtual Interface can result in an interrupt
889                  * during the processing of the VI Enable command and, in some
890                  * paths, result in an attempt to issue another command in the
891                  * interrupt context.  Thus, we disable interrupts during the
892                  * course of the VI Enable command ...
893                  */
894                 ret = t4_enable_vi_params(adapter, adapter->mbox, pi->viid,
895                                           true, true, false);
896         }
897         return ret;
898 }
899
900 /**
901  * cxgb4_write_rss - write the RSS table for a given port
902  * @pi: the port
903  * @queues: array of queue indices for RSS
904  *
905  * Sets up the portion of the HW RSS table for the port's VI to distribute
906  * packets to the Rx queues in @queues.
907  */
908 int cxgb4_write_rss(const struct port_info *pi, const u16 *queues)
909 {
910         u16 *rss;
911         int i, err;
912         struct adapter *adapter = pi->adapter;
913         const struct sge_eth_rxq *rxq;
914
915         /*  Should never be called before setting up sge eth rx queues */
916         BUG_ON(!(adapter->flags & FULL_INIT_DONE));
917
918         rxq = &adapter->sge.ethrxq[pi->first_qset];
919         rss = rte_zmalloc(NULL, pi->rss_size * sizeof(u16), 0);
920         if (!rss)
921                 return -ENOMEM;
922
923         /* map the queue indices to queue ids */
924         for (i = 0; i < pi->rss_size; i++, queues++)
925                 rss[i] = rxq[*queues].rspq.abs_id;
926
927         err = t4_config_rss_range(adapter, adapter->pf, pi->viid, 0,
928                                   pi->rss_size, rss, pi->rss_size);
929         /*
930          * If Tunnel All Lookup isn't specified in the global RSS
931          * Configuration, then we need to specify a default Ingress
932          * Queue for any ingress packets which aren't hashed.  We'll
933          * use our first ingress queue ...
934          */
935         if (!err)
936                 err = t4_config_vi_rss(adapter, adapter->mbox, pi->viid,
937                                        F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN |
938                                        F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN |
939                                        F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
940                                        F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN |
941                                        F_FW_RSS_VI_CONFIG_CMD_UDPEN,
942                                        rss[0]);
943         rte_free(rss);
944         return err;
945 }
946
947 /**
948  * setup_rss - configure RSS
949  * @adapter: the adapter
950  *
951  * Sets up RSS to distribute packets to multiple receive queues.  We
952  * configure the RSS CPU lookup table to distribute to the number of HW
953  * receive queues, and the response queue lookup table to narrow that
954  * down to the response queues actually configured for each port.
955  * We always configure the RSS mapping for all ports since the mapping
956  * table has plenty of entries.
957  */
958 int setup_rss(struct port_info *pi)
959 {
960         int j, err;
961         struct adapter *adapter = pi->adapter;
962
963         dev_debug(adapter, "%s:  pi->rss_size = %u; pi->n_rx_qsets = %u\n",
964                   __func__, pi->rss_size, pi->n_rx_qsets);
965
966         if (!(pi->flags & PORT_RSS_DONE)) {
967                 if (adapter->flags & FULL_INIT_DONE) {
968                         /* Fill default values with equal distribution */
969                         for (j = 0; j < pi->rss_size; j++)
970                                 pi->rss[j] = j % pi->n_rx_qsets;
971
972                         err = cxgb4_write_rss(pi, pi->rss);
973                         if (err)
974                                 return err;
975                         pi->flags |= PORT_RSS_DONE;
976                 }
977         }
978         return 0;
979 }
980
981 /*
982  * Enable NAPI scheduling and interrupt generation for all Rx queues.
983  */
984 static void enable_rx(struct adapter *adap)
985 {
986         struct sge *s = &adap->sge;
987         struct sge_rspq *q = &s->fw_evtq;
988         int i, j;
989
990         /* 0-increment GTS to start the timer and enable interrupts */
991         t4_write_reg(adap, MYPF_REG(A_SGE_PF_GTS),
992                      V_SEINTARM(q->intr_params) |
993                      V_INGRESSQID(q->cntxt_id));
994
995         for_each_port(adap, i) {
996                 const struct port_info *pi = &adap->port[i];
997                 struct rte_eth_dev *eth_dev = pi->eth_dev;
998
999                 for (j = 0; j < eth_dev->data->nb_rx_queues; j++) {
1000                         q = eth_dev->data->rx_queues[j];
1001
1002                         /*
1003                          * 0-increment GTS to start the timer and enable
1004                          * interrupts
1005                          */
1006                         t4_write_reg(adap, MYPF_REG(A_SGE_PF_GTS),
1007                                      V_SEINTARM(q->intr_params) |
1008                                      V_INGRESSQID(q->cntxt_id));
1009                 }
1010         }
1011 }
1012
1013 /**
1014  * cxgb_up - enable the adapter
1015  * @adap: adapter being enabled
1016  *
1017  * Called when the first port is enabled, this function performs the
1018  * actions necessary to make an adapter operational, such as completing
1019  * the initialization of HW modules, and enabling interrupts.
1020  */
1021 int cxgbe_up(struct adapter *adap)
1022 {
1023         enable_rx(adap);
1024         t4_sge_tx_monitor_start(adap);
1025         t4_intr_enable(adap);
1026         adap->flags |= FULL_INIT_DONE;
1027
1028         /* TODO: deadman watchdog ?? */
1029         return 0;
1030 }
1031
1032 /*
1033  * Close the port
1034  */
1035 int cxgbe_down(struct port_info *pi)
1036 {
1037         struct adapter *adapter = pi->adapter;
1038         int err = 0;
1039
1040         err = t4_enable_vi(adapter, adapter->mbox, pi->viid, false, false);
1041         if (err) {
1042                 dev_err(adapter, "%s: disable_vi failed: %d\n", __func__, err);
1043                 return err;
1044         }
1045
1046         t4_reset_link_config(adapter, pi->port_id);
1047         return 0;
1048 }
1049
1050 /*
1051  * Release resources when all the ports have been stopped.
1052  */
1053 void cxgbe_close(struct adapter *adapter)
1054 {
1055         struct port_info *pi;
1056         int i;
1057
1058         if (adapter->flags & FULL_INIT_DONE) {
1059                 t4_intr_disable(adapter);
1060                 t4_sge_tx_monitor_stop(adapter);
1061                 t4_free_sge_resources(adapter);
1062                 for_each_port(adapter, i) {
1063                         pi = adap2pinfo(adapter, i);
1064                         if (pi->viid != 0)
1065                                 t4_free_vi(adapter, adapter->mbox,
1066                                            adapter->pf, 0, pi->viid);
1067                         rte_free(pi->eth_dev->data->mac_addrs);
1068                 }
1069                 adapter->flags &= ~FULL_INIT_DONE;
1070         }
1071
1072         if (adapter->flags & FW_OK)
1073                 t4_fw_bye(adapter, adapter->mbox);
1074 }
1075
1076 int cxgbe_probe(struct adapter *adapter)
1077 {
1078         struct port_info *pi;
1079         int chip;
1080         int func, i;
1081         int err = 0;
1082         u32 whoami;
1083
1084         whoami = t4_read_reg(adapter, A_PL_WHOAMI);
1085         chip = t4_get_chip_type(adapter,
1086                         CHELSIO_PCI_ID_VER(adapter->pdev->id.device_id));
1087         if (chip < 0)
1088                 return chip;
1089
1090         func = CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5 ?
1091                G_SOURCEPF(whoami) : G_T6_SOURCEPF(whoami);
1092
1093         adapter->mbox = func;
1094         adapter->pf = func;
1095
1096         t4_os_lock_init(&adapter->mbox_lock);
1097         TAILQ_INIT(&adapter->mbox_list);
1098
1099         err = t4_prep_adapter(adapter);
1100         if (err)
1101                 return err;
1102
1103         setup_memwin(adapter);
1104         err = adap_init0(adapter);
1105         if (err) {
1106                 dev_err(adapter, "%s: Adapter initialization failed, error %d\n",
1107                         __func__, err);
1108                 goto out_free;
1109         }
1110
1111         if (!is_t4(adapter->params.chip)) {
1112                 /*
1113                  * The userspace doorbell BAR is split evenly into doorbell
1114                  * regions, each associated with an egress queue.  If this
1115                  * per-queue region is large enough (at least UDBS_SEG_SIZE)
1116                  * then it can be used to submit a tx work request with an
1117                  * implied doorbell.  Enable write combining on the BAR if
1118                  * there is room for such work requests.
1119                  */
1120                 int s_qpp, qpp, num_seg;
1121
1122                 s_qpp = (S_QUEUESPERPAGEPF0 +
1123                         (S_QUEUESPERPAGEPF1 - S_QUEUESPERPAGEPF0) *
1124                         adapter->pf);
1125                 qpp = 1 << ((t4_read_reg(adapter,
1126                                 A_SGE_EGRESS_QUEUES_PER_PAGE_PF) >> s_qpp)
1127                                 & M_QUEUESPERPAGEPF0);
1128                 num_seg = CXGBE_PAGE_SIZE / UDBS_SEG_SIZE;
1129                 if (qpp > num_seg)
1130                         dev_warn(adapter, "Incorrect SGE EGRESS QUEUES_PER_PAGE configuration, continuing in debug mode\n");
1131
1132                 adapter->bar2 = (void *)adapter->pdev->mem_resource[2].addr;
1133                 if (!adapter->bar2) {
1134                         dev_err(adapter, "cannot map device bar2 region\n");
1135                         err = -ENOMEM;
1136                         goto out_free;
1137                 }
1138                 t4_write_reg(adapter, A_SGE_STAT_CFG, V_STATSOURCE_T5(7) |
1139                              V_STATMODE(0));
1140         }
1141
1142         for_each_port(adapter, i) {
1143                 char name[RTE_ETH_NAME_MAX_LEN];
1144                 struct rte_eth_dev_data *data = NULL;
1145                 const unsigned int numa_node = rte_socket_id();
1146
1147                 pi = &adapter->port[i];
1148                 pi->adapter = adapter;
1149                 pi->xact_addr_filt = -1;
1150                 pi->port_id = i;
1151
1152                 snprintf(name, sizeof(name), "cxgbe%d",
1153                          adapter->eth_dev->data->port_id + i);
1154
1155                 if (i == 0) {
1156                         /* First port is already allocated by DPDK */
1157                         pi->eth_dev = adapter->eth_dev;
1158                         goto allocate_mac;
1159                 }
1160
1161                 /*
1162                  * now do all data allocation - for eth_dev structure,
1163                  * and internal (private) data for the remaining ports
1164                  */
1165
1166                 /* reserve an ethdev entry */
1167                 pi->eth_dev = rte_eth_dev_allocate(name);
1168                 if (!pi->eth_dev)
1169                         goto out_free;
1170
1171                 data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node);
1172                 if (!data)
1173                         goto out_free;
1174
1175                 data->port_id = adapter->eth_dev->data->port_id + i;
1176
1177                 pi->eth_dev->data = data;
1178
1179 allocate_mac:
1180                 pi->eth_dev->device = &adapter->pdev->device;
1181                 pi->eth_dev->data->dev_private = pi;
1182                 pi->eth_dev->dev_ops = adapter->eth_dev->dev_ops;
1183                 pi->eth_dev->tx_pkt_burst = adapter->eth_dev->tx_pkt_burst;
1184                 pi->eth_dev->rx_pkt_burst = adapter->eth_dev->rx_pkt_burst;
1185
1186                 rte_eth_copy_pci_info(pi->eth_dev, adapter->pdev);
1187
1188                 pi->eth_dev->data->mac_addrs = rte_zmalloc(name,
1189                                                            ETHER_ADDR_LEN, 0);
1190                 if (!pi->eth_dev->data->mac_addrs) {
1191                         dev_err(adapter, "%s: Mem allocation failed for storing mac addr, aborting\n",
1192                                 __func__);
1193                         err = -1;
1194                         goto out_free;
1195                 }
1196         }
1197
1198         if (adapter->flags & FW_OK) {
1199                 err = t4_port_init(adapter, adapter->mbox, adapter->pf, 0);
1200                 if (err) {
1201                         dev_err(adapter, "%s: t4_port_init failed with err %d\n",
1202                                 __func__, err);
1203                         goto out_free;
1204                 }
1205         }
1206
1207         cfg_queues(adapter->eth_dev);
1208
1209         print_port_info(adapter);
1210
1211         err = init_rss(adapter);
1212         if (err)
1213                 goto out_free;
1214
1215         return 0;
1216
1217 out_free:
1218         for_each_port(adapter, i) {
1219                 pi = adap2pinfo(adapter, i);
1220                 if (pi->viid != 0)
1221                         t4_free_vi(adapter, adapter->mbox, adapter->pf,
1222                                    0, pi->viid);
1223                 /* Skip first port since it'll be de-allocated by DPDK */
1224                 if (i == 0)
1225                         continue;
1226                 if (pi->eth_dev->data)
1227                         rte_free(pi->eth_dev->data);
1228         }
1229
1230         if (adapter->flags & FW_OK)
1231                 t4_fw_bye(adapter, adapter->mbox);
1232         return -err;
1233 }