remove extra parentheses in return statement
[dpdk.git] / drivers / net / cxgbe / cxgbe_main.c
index 6f3a6db..649b941 100644 (file)
 #include "t4_msg.h"
 #include "cxgbe.h"
 
+/*
+ * Response queue handler for the FW event queue.
+ */
+static int fwevtq_handler(struct sge_rspq *q, const __be64 *rsp,
+                         __rte_unused const struct pkt_gl *gl)
+{
+       u8 opcode = ((const struct rss_header *)rsp)->opcode;
+
+       rsp++;                                          /* skip RSS header */
+
+       /*
+        * FW can send EGR_UPDATEs encapsulated in a CPL_FW4_MSG.
+        */
+       if (unlikely(opcode == CPL_FW4_MSG &&
+                    ((const struct cpl_fw4_msg *)rsp)->type ==
+                     FW_TYPE_RSSCPL)) {
+               rsp++;
+               opcode = ((const struct rss_header *)rsp)->opcode;
+               rsp++;
+               if (opcode != CPL_SGE_EGR_UPDATE) {
+                       dev_err(q->adapter, "unexpected FW4/CPL %#x on FW event queue\n",
+                               opcode);
+                       goto out;
+               }
+       }
+
+       if (likely(opcode == CPL_SGE_EGR_UPDATE)) {
+               /* do nothing */
+       } else if (opcode == CPL_FW6_MSG || opcode == CPL_FW4_MSG) {
+               const struct cpl_fw6_msg *msg = (const void *)rsp;
+
+               t4_handle_fw_rpl(q->adapter, msg->data);
+       } else {
+               dev_err(adapter, "unexpected CPL %#x on FW event queue\n",
+                       opcode);
+       }
+out:
+       return 0;
+}
+
+int setup_sge_fwevtq(struct adapter *adapter)
+{
+       struct sge *s = &adapter->sge;
+       int err = 0;
+       int msi_idx = 0;
+
+       err = t4_sge_alloc_rxq(adapter, &s->fw_evtq, true, adapter->eth_dev,
+                              msi_idx, NULL, fwevtq_handler, -1, NULL, 0,
+                              rte_socket_id());
+       return err;
+}
+
+static int closest_timer(const struct sge *s, int time)
+{
+       unsigned int i, match = 0;
+       int delta, min_delta = INT_MAX;
+
+       for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
+               delta = time - s->timer_val[i];
+               if (delta < 0)
+                       delta = -delta;
+               if (delta < min_delta) {
+                       min_delta = delta;
+                       match = i;
+               }
+       }
+       return match;
+}
+
+static int closest_thres(const struct sge *s, int thres)
+{
+       unsigned int i, match = 0;
+       int delta, min_delta = INT_MAX;
+
+       for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
+               delta = thres - s->counter_val[i];
+               if (delta < 0)
+                       delta = -delta;
+               if (delta < min_delta) {
+                       min_delta = delta;
+                       match = i;
+               }
+       }
+       return match;
+}
+
+/**
+ * cxgb4_set_rspq_intr_params - set a queue's interrupt holdoff parameters
+ * @q: the Rx queue
+ * @us: the hold-off time in us, or 0 to disable timer
+ * @cnt: the hold-off packet count, or 0 to disable counter
+ *
+ * Sets an Rx queue's interrupt hold-off time and packet count.  At least
+ * one of the two needs to be enabled for the queue to generate interrupts.
+ */
+int cxgb4_set_rspq_intr_params(struct sge_rspq *q, unsigned int us,
+                              unsigned int cnt)
+{
+       struct adapter *adap = q->adapter;
+       unsigned int timer_val;
+
+       if (cnt) {
+               int err;
+               u32 v, new_idx;
+
+               new_idx = closest_thres(&adap->sge, cnt);
+               if (q->desc && q->pktcnt_idx != new_idx) {
+                       /* the queue has already been created, update it */
+                       v = V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
+                           V_FW_PARAMS_PARAM_X(
+                           FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
+                           V_FW_PARAMS_PARAM_YZ(q->cntxt_id);
+                       err = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
+                                           &v, &new_idx);
+                       if (err)
+                               return err;
+               }
+               q->pktcnt_idx = new_idx;
+       }
+
+       timer_val = (us == 0) ? X_TIMERREG_RESTART_COUNTER :
+                               closest_timer(&adap->sge, us);
+
+       if ((us | cnt) == 0)
+               q->intr_params = V_QINTR_TIMER_IDX(X_TIMERREG_UPDATE_CIDX);
+       else
+               q->intr_params = V_QINTR_TIMER_IDX(timer_val) |
+                                V_QINTR_CNT_EN(cnt > 0);
+       return 0;
+}
+
+static inline bool is_x_1g_port(const struct link_config *lc)
+{
+       return (lc->supported & FW_PORT_CAP_SPEED_1G) != 0;
+}
+
+static inline bool is_x_10g_port(const struct link_config *lc)
+{
+       return ((lc->supported & FW_PORT_CAP_SPEED_10G) != 0 ||
+               (lc->supported & FW_PORT_CAP_SPEED_40G) != 0 ||
+               (lc->supported & FW_PORT_CAP_SPEED_100G) != 0);
+}
+
+inline void init_rspq(struct adapter *adap, struct sge_rspq *q,
+                     unsigned int us, unsigned int cnt,
+                     unsigned int size, unsigned int iqe_size)
+{
+       q->adapter = adap;
+       cxgb4_set_rspq_intr_params(q, us, cnt);
+       q->iqe_len = iqe_size;
+       q->size = size;
+}
+
+int cfg_queue_count(struct rte_eth_dev *eth_dev)
+{
+       struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
+       struct adapter *adap = pi->adapter;
+       struct sge *s = &adap->sge;
+       unsigned int max_queues = s->max_ethqsets / adap->params.nports;
+
+       if ((eth_dev->data->nb_rx_queues < 1) ||
+           (eth_dev->data->nb_tx_queues < 1))
+               return -EINVAL;
+
+       if ((eth_dev->data->nb_rx_queues > max_queues) ||
+           (eth_dev->data->nb_tx_queues > max_queues))
+               return -EINVAL;
+
+       if (eth_dev->data->nb_rx_queues > pi->rss_size)
+               return -EINVAL;
+
+       /* We must configure RSS, since config has changed*/
+       pi->flags &= ~PORT_RSS_DONE;
+
+       pi->n_rx_qsets = eth_dev->data->nb_rx_queues;
+       pi->n_tx_qsets = eth_dev->data->nb_tx_queues;
+
+       return 0;
+}
+
+void cfg_queues(struct rte_eth_dev *eth_dev)
+{
+       struct rte_config *config = rte_eal_get_configuration();
+       struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
+       struct adapter *adap = pi->adapter;
+       struct sge *s = &adap->sge;
+       unsigned int i, nb_ports = 0, qidx = 0;
+       unsigned int q_per_port = 0;
+
+       if (!(adap->flags & CFG_QUEUES)) {
+               for_each_port(adap, i) {
+                       struct port_info *tpi = adap2pinfo(adap, i);
+
+                       nb_ports += (is_x_10g_port(&tpi->link_cfg)) ||
+                                    is_x_1g_port(&tpi->link_cfg) ? 1 : 0;
+               }
+
+               /*
+                * We default up to # of cores queues per 1G/10G port.
+                */
+               if (nb_ports)
+                       q_per_port = (MAX_ETH_QSETS -
+                                    (adap->params.nports - nb_ports)) /
+                                    nb_ports;
+
+               if (q_per_port > config->lcore_count)
+                       q_per_port = config->lcore_count;
+
+               for_each_port(adap, i) {
+                       struct port_info *pi = adap2pinfo(adap, i);
+
+                       pi->first_qset = qidx;
+
+                       /* Initially n_rx_qsets == n_tx_qsets */
+                       pi->n_rx_qsets = (is_x_10g_port(&pi->link_cfg) ||
+                                         is_x_1g_port(&pi->link_cfg)) ?
+                                         q_per_port : 1;
+                       pi->n_tx_qsets = pi->n_rx_qsets;
+
+                       if (pi->n_rx_qsets > pi->rss_size)
+                               pi->n_rx_qsets = pi->rss_size;
+
+                       qidx += pi->n_rx_qsets;
+               }
+
+               s->max_ethqsets = qidx;
+
+               for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
+                       struct sge_eth_rxq *r = &s->ethrxq[i];
+
+                       init_rspq(adap, &r->rspq, 0, 0, 1024, 64);
+                       r->usembufs = 1;
+                       r->fl.size = (r->usembufs ? 1024 : 72);
+               }
+
+               for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
+                       s->ethtxq[i].q.size = 1024;
+
+               init_rspq(adap, &adap->sge.fw_evtq, 0, 0, 1024, 64);
+               adap->flags |= CFG_QUEUES;
+       }
+}
+
+void cxgbe_stats_get(struct port_info *pi, struct port_stats *stats)
+{
+       t4_get_port_stats_offset(pi->adapter, pi->tx_chan, stats,
+                                &pi->stats_base);
+}
+
+void cxgbe_stats_reset(struct port_info *pi)
+{
+       t4_clr_port_stats(pi->adapter, pi->tx_chan);
+}
+
 static void setup_memwin(struct adapter *adap)
 {
        u32 mem_win0_base;
@@ -89,6 +343,25 @@ static void setup_memwin(struct adapter *adap)
                                        MEMWIN_NIC));
 }
 
+static int init_rss(struct adapter *adap)
+{
+       unsigned int i;
+       int err;
+
+       err = t4_init_rss_mode(adap, adap->mbox);
+       if (err)
+               return err;
+
+       for_each_port(adap, i) {
+               struct port_info *pi = adap2pinfo(adap, i);
+
+               pi->rss = rte_zmalloc(NULL, pi->rss_size, 0);
+               if (!pi->rss)
+                       return -ENOMEM;
+       }
+       return 0;
+}
+
 static void print_port_info(struct adapter *adap)
 {
        int i;
@@ -110,7 +383,8 @@ static void print_port_info(struct adapter *adap)
                if (bufp != buf)
                        --bufp;
                sprintf(bufp, "BASE-%s",
-                       t4_get_port_type_description(pi->port_type));
+                       t4_get_port_type_description(
+                                       (enum fw_port_type)pi->port_type));
 
                dev_info(adap,
                         " " PCI_PRI_FMT " Chelsio rev %d %s %s\n",
@@ -137,7 +411,7 @@ static int adap_init0_tweaks(struct adapter *adapter)
         * Line Size, etc.  The firmware default is for a 4KB Page Size and
         * 64B Cache Line Size ...
         */
-       t4_fixup_host_params_compat(adapter, PAGE_SIZE, L1_CACHE_BYTES,
+       t4_fixup_host_params_compat(adapter, CXGBE_PAGE_SIZE, L1_CACHE_BYTES,
                                    T5_LAST_REV);
 
        /*
@@ -148,6 +422,13 @@ static int adap_init0_tweaks(struct adapter *adapter)
        t4_set_reg_field(adapter, A_SGE_CONTROL, V_PKTSHIFT(M_PKTSHIFT),
                         V_PKTSHIFT(rx_dma_offset));
 
+       t4_set_reg_field(adapter, A_SGE_FLM_CFG,
+                        V_CREDITCNT(M_CREDITCNT) | M_CREDITCNTPACKING,
+                        V_CREDITCNT(3) | V_CREDITCNTPACKING(1));
+
+       t4_set_reg_field(adapter, A_SGE_CONTROL2, V_IDMAARBROUNDROBIN(1U),
+                        V_IDMAARBROUNDROBIN(1U));
+
        /*
         * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
         * adds the pseudo header itself.
@@ -356,7 +637,7 @@ static int adap_init0(struct adapter *adap)
                        dev_err(adap, "Failed to restart. Exit.\n");
                        goto bye;
                }
-               state &= ~DEV_STATE_INIT;
+               state = (enum dev_state)((unsigned)state & ~DEV_STATE_INIT);
        }
 
        t4_get_fw_version(adap, &adap->params.fw_vers);
@@ -564,6 +845,227 @@ void t4_os_portmod_changed(const struct adapter *adap, int port_id)
                         pi->port_id, pi->mod_type);
 }
 
+/**
+ * link_start - enable a port
+ * @dev: the port to enable
+ *
+ * Performs the MAC and PHY actions needed to enable a port.
+ */
+int link_start(struct port_info *pi)
+{
+       struct adapter *adapter = pi->adapter;
+       int ret;
+       unsigned int mtu = pi->eth_dev->data->dev_conf.rxmode.max_rx_pkt_len;
+
+       /*
+        * We do not set address filters and promiscuity here, the stack does
+        * that step explicitly.
+        */
+       ret = t4_set_rxmode(adapter, adapter->mbox, pi->viid, mtu, -1, -1,
+                           -1, 1, true);
+       if (ret == 0) {
+               ret = t4_change_mac(adapter, adapter->mbox, pi->viid,
+                                   pi->xact_addr_filt,
+                                   (u8 *)&pi->eth_dev->data->mac_addrs[0],
+                                   true, true);
+               if (ret >= 0) {
+                       pi->xact_addr_filt = ret;
+                       ret = 0;
+               }
+       }
+       if (ret == 0)
+               ret = t4_link_l1cfg(adapter, adapter->mbox, pi->tx_chan,
+                                   &pi->link_cfg);
+       if (ret == 0) {
+               /*
+                * Enabling a Virtual Interface can result in an interrupt
+                * during the processing of the VI Enable command and, in some
+                * paths, result in an attempt to issue another command in the
+                * interrupt context.  Thus, we disable interrupts during the
+                * course of the VI Enable command ...
+                */
+               ret = t4_enable_vi_params(adapter, adapter->mbox, pi->viid,
+                                         true, true, false);
+       }
+       return ret;
+}
+
+/**
+ * cxgb4_write_rss - write the RSS table for a given port
+ * @pi: the port
+ * @queues: array of queue indices for RSS
+ *
+ * Sets up the portion of the HW RSS table for the port's VI to distribute
+ * packets to the Rx queues in @queues.
+ */
+int cxgb4_write_rss(const struct port_info *pi, const u16 *queues)
+{
+       u16 *rss;
+       int i, err;
+       struct adapter *adapter = pi->adapter;
+       const struct sge_eth_rxq *rxq;
+
+       /*  Should never be called before setting up sge eth rx queues */
+       BUG_ON(!(adapter->flags & FULL_INIT_DONE));
+
+       rxq = &adapter->sge.ethrxq[pi->first_qset];
+       rss = rte_zmalloc(NULL, pi->rss_size * sizeof(u16), 0);
+       if (!rss)
+               return -ENOMEM;
+
+       /* map the queue indices to queue ids */
+       for (i = 0; i < pi->rss_size; i++, queues++)
+               rss[i] = rxq[*queues].rspq.abs_id;
+
+       err = t4_config_rss_range(adapter, adapter->pf, pi->viid, 0,
+                                 pi->rss_size, rss, pi->rss_size);
+       /*
+        * If Tunnel All Lookup isn't specified in the global RSS
+        * Configuration, then we need to specify a default Ingress
+        * Queue for any ingress packets which aren't hashed.  We'll
+        * use our first ingress queue ...
+        */
+       if (!err)
+               err = t4_config_vi_rss(adapter, adapter->mbox, pi->viid,
+                                      F_FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN |
+                                      F_FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN |
+                                      F_FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN |
+                                      F_FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN |
+                                      F_FW_RSS_VI_CONFIG_CMD_UDPEN,
+                                      rss[0]);
+       rte_free(rss);
+       return err;
+}
+
+/**
+ * setup_rss - configure RSS
+ * @adapter: the adapter
+ *
+ * Sets up RSS to distribute packets to multiple receive queues.  We
+ * configure the RSS CPU lookup table to distribute to the number of HW
+ * receive queues, and the response queue lookup table to narrow that
+ * down to the response queues actually configured for each port.
+ * We always configure the RSS mapping for all ports since the mapping
+ * table has plenty of entries.
+ */
+int setup_rss(struct port_info *pi)
+{
+       int j, err;
+       struct adapter *adapter = pi->adapter;
+
+       dev_debug(adapter, "%s:  pi->rss_size = %u; pi->n_rx_qsets = %u\n",
+                 __func__, pi->rss_size, pi->n_rx_qsets);
+
+       if (!pi->flags & PORT_RSS_DONE) {
+               if (adapter->flags & FULL_INIT_DONE) {
+                       /* Fill default values with equal distribution */
+                       for (j = 0; j < pi->rss_size; j++)
+                               pi->rss[j] = j % pi->n_rx_qsets;
+
+                       err = cxgb4_write_rss(pi, pi->rss);
+                       if (err)
+                               return err;
+                       pi->flags |= PORT_RSS_DONE;
+               }
+       }
+       return 0;
+}
+
+/*
+ * Enable NAPI scheduling and interrupt generation for all Rx queues.
+ */
+static void enable_rx(struct adapter *adap)
+{
+       struct sge *s = &adap->sge;
+       struct sge_rspq *q = &s->fw_evtq;
+       int i, j;
+
+       /* 0-increment GTS to start the timer and enable interrupts */
+       t4_write_reg(adap, MYPF_REG(A_SGE_PF_GTS),
+                    V_SEINTARM(q->intr_params) |
+                    V_INGRESSQID(q->cntxt_id));
+
+       for_each_port(adap, i) {
+               const struct port_info *pi = &adap->port[i];
+               struct rte_eth_dev *eth_dev = pi->eth_dev;
+
+               for (j = 0; j < eth_dev->data->nb_rx_queues; j++) {
+                       q = eth_dev->data->rx_queues[j];
+
+                       /*
+                        * 0-increment GTS to start the timer and enable
+                        * interrupts
+                        */
+                       t4_write_reg(adap, MYPF_REG(A_SGE_PF_GTS),
+                                    V_SEINTARM(q->intr_params) |
+                                    V_INGRESSQID(q->cntxt_id));
+               }
+       }
+}
+
+/**
+ * cxgb_up - enable the adapter
+ * @adap: adapter being enabled
+ *
+ * Called when the first port is enabled, this function performs the
+ * actions necessary to make an adapter operational, such as completing
+ * the initialization of HW modules, and enabling interrupts.
+ */
+int cxgbe_up(struct adapter *adap)
+{
+       enable_rx(adap);
+       t4_sge_tx_monitor_start(adap);
+       t4_intr_enable(adap);
+       adap->flags |= FULL_INIT_DONE;
+
+       /* TODO: deadman watchdog ?? */
+       return 0;
+}
+
+/*
+ * Close the port
+ */
+int cxgbe_down(struct port_info *pi)
+{
+       struct adapter *adapter = pi->adapter;
+       int err = 0;
+
+       err = t4_enable_vi(adapter, adapter->mbox, pi->viid, false, false);
+       if (err) {
+               dev_err(adapter, "%s: disable_vi failed: %d\n", __func__, err);
+               return err;
+       }
+
+       t4_reset_link_config(adapter, pi->port_id);
+       return 0;
+}
+
+/*
+ * Release resources when all the ports have been stopped.
+ */
+void cxgbe_close(struct adapter *adapter)
+{
+       struct port_info *pi;
+       int i;
+
+       if (adapter->flags & FULL_INIT_DONE) {
+               t4_intr_disable(adapter);
+               t4_sge_tx_monitor_stop(adapter);
+               t4_free_sge_resources(adapter);
+               for_each_port(adapter, i) {
+                       pi = adap2pinfo(adapter, i);
+                       if (pi->viid != 0)
+                               t4_free_vi(adapter, adapter->mbox,
+                                          adapter->pf, 0, pi->viid);
+                       rte_free(pi->eth_dev->data->mac_addrs);
+               }
+               adapter->flags &= ~FULL_INIT_DONE;
+       }
+
+       if (adapter->flags & FW_OK)
+               t4_fw_bye(adapter, adapter->mbox);
+}
+
 int cxgbe_probe(struct adapter *adapter)
 {
        struct port_info *pi;
@@ -606,7 +1108,7 @@ int cxgbe_probe(struct adapter *adapter)
                qpp = 1 << ((t4_read_reg(adapter,
                                A_SGE_EGRESS_QUEUES_PER_PAGE_PF) >> s_qpp)
                                & M_QUEUESPERPAGEPF0);
-               num_seg = PAGE_SIZE / UDBS_SEG_SIZE;
+               num_seg = CXGBE_PAGE_SIZE / UDBS_SEG_SIZE;
                if (qpp > num_seg)
                        dev_warn(adapter, "Incorrect SGE EGRESS QUEUES_PER_PAGE configuration, continuing in debug mode\n");
 
@@ -662,6 +1164,8 @@ allocate_mac:
                pi->eth_dev->data->dev_private = pi;
                pi->eth_dev->driver = adapter->eth_dev->driver;
                pi->eth_dev->dev_ops = adapter->eth_dev->dev_ops;
+               pi->eth_dev->tx_pkt_burst = adapter->eth_dev->tx_pkt_burst;
+               pi->eth_dev->rx_pkt_burst = adapter->eth_dev->rx_pkt_burst;
                TAILQ_INIT(&pi->eth_dev->link_intr_cbs);
 
                pi->eth_dev->data->mac_addrs = rte_zmalloc(name,
@@ -683,8 +1187,14 @@ allocate_mac:
                }
        }
 
+       cfg_queues(adapter->eth_dev);
+
        print_port_info(adapter);
 
+       err = init_rss(adapter);
+       if (err)
+               goto out_free;
+
        return 0;
 
 out_free: