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