net/txgbe: support VF VLAN
[dpdk.git] / drivers / net / txgbe / txgbe_ethdev_vf.c
index 6a5180e..b157310 100644 (file)
 #include "txgbe_ethdev.h"
 #include "txgbe_rxtx.h"
 
+static int txgbevf_dev_xstats_get(struct rte_eth_dev *dev,
+                                 struct rte_eth_xstat *xstats, unsigned int n);
 static int txgbevf_dev_info_get(struct rte_eth_dev *dev,
                                 struct rte_eth_dev_info *dev_info);
+static int  txgbevf_dev_configure(struct rte_eth_dev *dev);
 static int txgbevf_dev_link_update(struct rte_eth_dev *dev,
                                   int wait_to_complete);
 static int txgbevf_dev_close(struct rte_eth_dev *dev);
 static void txgbevf_intr_disable(struct rte_eth_dev *dev);
 static void txgbevf_intr_enable(struct rte_eth_dev *dev);
+static int txgbevf_dev_stats_reset(struct rte_eth_dev *dev);
+static int txgbevf_vlan_offload_config(struct rte_eth_dev *dev, int mask);
+static void txgbevf_set_vfta_all(struct rte_eth_dev *dev, bool on);
 static void txgbevf_configure_msix(struct rte_eth_dev *dev);
 static void txgbevf_remove_mac_addr(struct rte_eth_dev *dev, uint32_t index);
 static void txgbevf_dev_interrupt_handler(void *param);
@@ -51,6 +57,28 @@ static const struct rte_eth_desc_lim tx_desc_lim = {
 
 static const struct eth_dev_ops txgbevf_eth_dev_ops;
 
+static const struct rte_txgbe_xstats_name_off rte_txgbevf_stats_strings[] = {
+       {"rx_multicast_packets_0",
+                       offsetof(struct txgbevf_hw_stats, qp[0].vfmprc)},
+       {"rx_multicast_packets_1",
+                       offsetof(struct txgbevf_hw_stats, qp[1].vfmprc)},
+       {"rx_multicast_packets_2",
+                       offsetof(struct txgbevf_hw_stats, qp[2].vfmprc)},
+       {"rx_multicast_packets_3",
+                       offsetof(struct txgbevf_hw_stats, qp[3].vfmprc)},
+       {"rx_multicast_packets_4",
+                       offsetof(struct txgbevf_hw_stats, qp[4].vfmprc)},
+       {"rx_multicast_packets_5",
+                       offsetof(struct txgbevf_hw_stats, qp[5].vfmprc)},
+       {"rx_multicast_packets_6",
+                       offsetof(struct txgbevf_hw_stats, qp[6].vfmprc)},
+       {"rx_multicast_packets_7",
+                       offsetof(struct txgbevf_hw_stats, qp[7].vfmprc)}
+};
+
+#define TXGBEVF_NB_XSTATS (sizeof(rte_txgbevf_stats_strings) / \
+               sizeof(rte_txgbevf_stats_strings[0]))
+
 /*
  * Negotiate mailbox API version with the PF.
  * After reset API version is always set to the basic one (txgbe_mbox_api_10).
@@ -104,12 +132,18 @@ eth_txgbevf_dev_init(struct rte_eth_dev *eth_dev)
        struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
        struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
        struct txgbe_hw *hw = TXGBE_DEV_HW(eth_dev);
+       struct txgbe_vfta *shadow_vfta = TXGBE_DEV_VFTA(eth_dev);
+       struct txgbe_hwstrip *hwstrip = TXGBE_DEV_HWSTRIP(eth_dev);
        struct rte_ether_addr *perm_addr =
                        (struct rte_ether_addr *)hw->mac.perm_addr;
 
        PMD_INIT_FUNC_TRACE();
 
        eth_dev->dev_ops = &txgbevf_eth_dev_ops;
+       eth_dev->rx_descriptor_status = txgbe_dev_rx_descriptor_status;
+       eth_dev->tx_descriptor_status = txgbe_dev_tx_descriptor_status;
+       eth_dev->rx_pkt_burst = &txgbe_recv_pkts;
+       eth_dev->tx_pkt_burst = &txgbe_xmit_pkts;
 
        /* for secondary processes, we don't initialise any further as primary
         * has already done this work. Only check we don't need a different
@@ -143,6 +177,12 @@ eth_txgbevf_dev_init(struct rte_eth_dev *eth_dev)
        hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
        hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
 
+       /* initialize the vfta */
+       memset(shadow_vfta, 0, sizeof(*shadow_vfta));
+
+       /* initialize the hw strip bitmap*/
+       memset(hwstrip, 0, sizeof(*hwstrip));
+
        /* Initialize the shared code (base driver) */
        err = txgbe_init_shared_code(hw);
        if (err != 0) {
@@ -154,6 +194,9 @@ eth_txgbevf_dev_init(struct rte_eth_dev *eth_dev)
        /* init_mailbox_params */
        hw->mbx.init_params(hw);
 
+       /* Reset the hw statistics */
+       txgbevf_dev_stats_reset(eth_dev);
+
        /* Disable the interrupts for VF */
        txgbevf_intr_disable(eth_dev);
 
@@ -270,6 +313,131 @@ static struct rte_pci_driver rte_txgbevf_pmd = {
        .remove = eth_txgbevf_pci_remove,
 };
 
+static int txgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
+       struct rte_eth_xstat_name *xstats_names, unsigned int limit)
+{
+       unsigned int i;
+
+       if (limit < TXGBEVF_NB_XSTATS && xstats_names != NULL)
+               return -ENOMEM;
+
+       if (xstats_names != NULL)
+               for (i = 0; i < TXGBEVF_NB_XSTATS; i++)
+                       snprintf(xstats_names[i].name,
+                               sizeof(xstats_names[i].name),
+                               "%s", rte_txgbevf_stats_strings[i].name);
+       return TXGBEVF_NB_XSTATS;
+}
+
+static void
+txgbevf_update_stats(struct rte_eth_dev *dev)
+{
+       struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
+       struct txgbevf_hw_stats *hw_stats = (struct txgbevf_hw_stats *)
+                         TXGBE_DEV_STATS(dev);
+       unsigned int i;
+
+       for (i = 0; i < dev->data->nb_rx_queues; i++) {
+               /* Good Rx packet, include VF loopback */
+               TXGBE_UPDCNT32(TXGBE_QPRXPKT(i),
+               hw_stats->qp[i].last_vfgprc, hw_stats->qp[i].vfgprc);
+
+               /* Good Rx octets, include VF loopback */
+               TXGBE_UPDCNT36(TXGBE_QPRXOCTL(i),
+               hw_stats->qp[i].last_vfgorc, hw_stats->qp[i].vfgorc);
+
+               /* Rx Multicst Packet */
+               TXGBE_UPDCNT32(TXGBE_QPRXMPKT(i),
+               hw_stats->qp[i].last_vfmprc, hw_stats->qp[i].vfmprc);
+       }
+       hw->rx_loaded = 0;
+
+       for (i = 0; i < dev->data->nb_tx_queues; i++) {
+               /* Good Tx packet, include VF loopback */
+               TXGBE_UPDCNT32(TXGBE_QPTXPKT(i),
+               hw_stats->qp[i].last_vfgptc, hw_stats->qp[i].vfgptc);
+
+               /* Good Tx octets, include VF loopback */
+               TXGBE_UPDCNT36(TXGBE_QPTXOCTL(i),
+               hw_stats->qp[i].last_vfgotc, hw_stats->qp[i].vfgotc);
+       }
+       hw->offset_loaded = 0;
+}
+
+static int
+txgbevf_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
+                      unsigned int n)
+{
+       struct txgbevf_hw_stats *hw_stats = (struct txgbevf_hw_stats *)
+                       TXGBE_DEV_STATS(dev);
+       unsigned int i;
+
+       if (n < TXGBEVF_NB_XSTATS)
+               return TXGBEVF_NB_XSTATS;
+
+       txgbevf_update_stats(dev);
+
+       if (!xstats)
+               return 0;
+
+       /* Extended stats */
+       for (i = 0; i < TXGBEVF_NB_XSTATS; i++) {
+               xstats[i].id = i;
+               xstats[i].value = *(uint64_t *)(((char *)hw_stats) +
+                       rte_txgbevf_stats_strings[i].offset);
+       }
+
+       return TXGBEVF_NB_XSTATS;
+}
+
+static int
+txgbevf_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
+{
+       struct txgbevf_hw_stats *hw_stats = (struct txgbevf_hw_stats *)
+                         TXGBE_DEV_STATS(dev);
+       uint32_t i;
+
+       txgbevf_update_stats(dev);
+
+       if (stats == NULL)
+               return -EINVAL;
+
+       stats->ipackets = 0;
+       stats->ibytes = 0;
+       stats->opackets = 0;
+       stats->obytes = 0;
+
+       for (i = 0; i < 8; i++) {
+               stats->ipackets += hw_stats->qp[i].vfgprc;
+               stats->ibytes += hw_stats->qp[i].vfgorc;
+               stats->opackets += hw_stats->qp[i].vfgptc;
+               stats->obytes += hw_stats->qp[i].vfgotc;
+       }
+
+       return 0;
+}
+
+static int
+txgbevf_dev_stats_reset(struct rte_eth_dev *dev)
+{
+       struct txgbevf_hw_stats *hw_stats = (struct txgbevf_hw_stats *)
+                       TXGBE_DEV_STATS(dev);
+       uint32_t i;
+
+       /* Sync HW register to the last stats */
+       txgbevf_dev_stats_get(dev, NULL);
+
+       /* reset HW current stats*/
+       for (i = 0; i < 8; i++) {
+               hw_stats->qp[i].vfgprc = 0;
+               hw_stats->qp[i].vfgorc = 0;
+               hw_stats->qp[i].vfgptc = 0;
+               hw_stats->qp[i].vfgotc = 0;
+       }
+
+       return 0;
+}
+
 static int
 txgbevf_dev_info_get(struct rte_eth_dev *dev,
                     struct rte_eth_dev_info *dev_info)
@@ -363,6 +531,43 @@ txgbevf_intr_enable(struct rte_eth_dev *dev)
        intr->mask_misc = 0;
 }
 
+static int
+txgbevf_dev_configure(struct rte_eth_dev *dev)
+{
+       struct rte_eth_conf *conf = &dev->data->dev_conf;
+       struct txgbe_adapter *adapter = TXGBE_DEV_ADAPTER(dev);
+
+       PMD_INIT_LOG(DEBUG, "Configured Virtual Function port id: %d",
+                    dev->data->port_id);
+
+       if (dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG)
+               dev->data->dev_conf.rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH;
+
+       /*
+        * VF has no ability to enable/disable HW CRC
+        * Keep the persistent behavior the same as Host PF
+        */
+#ifndef RTE_LIBRTE_TXGBE_PF_DISABLE_STRIP_CRC
+       if (conf->rxmode.offloads & DEV_RX_OFFLOAD_KEEP_CRC) {
+               PMD_INIT_LOG(NOTICE, "VF can't disable HW CRC Strip");
+               conf->rxmode.offloads &= ~DEV_RX_OFFLOAD_KEEP_CRC;
+       }
+#else
+       if (!(conf->rxmode.offloads & DEV_RX_OFFLOAD_KEEP_CRC)) {
+               PMD_INIT_LOG(NOTICE, "VF can't enable HW CRC Strip");
+               conf->rxmode.offloads |= DEV_RX_OFFLOAD_KEEP_CRC;
+       }
+#endif
+
+       /*
+        * Initialize to TRUE. If any of Rx queues doesn't meet the bulk
+        * allocation or vector Rx preconditions we will reset it.
+        */
+       adapter->rx_bulk_alloc_allowed = true;
+
+       return 0;
+}
+
 static int
 txgbevf_dev_close(struct rte_eth_dev *dev)
 {
@@ -384,6 +589,9 @@ txgbevf_dev_close(struct rte_eth_dev *dev)
         **/
        txgbevf_remove_mac_addr(dev, 0);
 
+       dev->rx_pkt_burst = NULL;
+       dev->tx_pkt_burst = NULL;
+
        /* Disable the interrupts for VF */
        txgbevf_intr_disable(dev);
 
@@ -397,6 +605,110 @@ txgbevf_dev_close(struct rte_eth_dev *dev)
        return 0;
 }
 
+static void txgbevf_set_vfta_all(struct rte_eth_dev *dev, bool on)
+{
+       struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
+       struct txgbe_vfta *shadow_vfta = TXGBE_DEV_VFTA(dev);
+       int i = 0, j = 0, vfta = 0, mask = 1;
+
+       for (i = 0; i < TXGBE_VFTA_SIZE; i++) {
+               vfta = shadow_vfta->vfta[i];
+               if (vfta) {
+                       mask = 1;
+                       for (j = 0; j < 32; j++) {
+                               if (vfta & mask)
+                                       txgbe_set_vfta(hw, (i << 5) + j, 0,
+                                                      on, false);
+                               mask <<= 1;
+                       }
+               }
+       }
+}
+
+static int
+txgbevf_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
+{
+       struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
+       struct txgbe_vfta *shadow_vfta = TXGBE_DEV_VFTA(dev);
+       uint32_t vid_idx = 0;
+       uint32_t vid_bit = 0;
+       int ret = 0;
+
+       PMD_INIT_FUNC_TRACE();
+
+       /* vind is not used in VF driver, set to 0, check txgbe_set_vfta_vf */
+       ret = hw->mac.set_vfta(hw, vlan_id, 0, !!on, false);
+       if (ret) {
+               PMD_INIT_LOG(ERR, "Unable to set VF vlan");
+               return ret;
+       }
+       vid_idx = (uint32_t)((vlan_id >> 5) & 0x7F);
+       vid_bit = (uint32_t)(1 << (vlan_id & 0x1F));
+
+       /* Save what we set and restore it after device reset */
+       if (on)
+               shadow_vfta->vfta[vid_idx] |= vid_bit;
+       else
+               shadow_vfta->vfta[vid_idx] &= ~vid_bit;
+
+       return 0;
+}
+
+static void
+txgbevf_vlan_strip_queue_set(struct rte_eth_dev *dev, uint16_t queue, int on)
+{
+       struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
+       uint32_t ctrl;
+
+       PMD_INIT_FUNC_TRACE();
+
+       if (queue >= hw->mac.max_rx_queues)
+               return;
+
+       ctrl = rd32(hw, TXGBE_RXCFG(queue));
+       txgbe_dev_save_rx_queue(hw, queue);
+       if (on)
+               ctrl |= TXGBE_RXCFG_VLAN;
+       else
+               ctrl &= ~TXGBE_RXCFG_VLAN;
+       wr32(hw, TXGBE_RXCFG(queue), 0);
+       msec_delay(100);
+       txgbe_dev_store_rx_queue(hw, queue);
+       wr32m(hw, TXGBE_RXCFG(queue),
+               TXGBE_RXCFG_VLAN | TXGBE_RXCFG_ENA, ctrl);
+
+       txgbe_vlan_hw_strip_bitmap_set(dev, queue, on);
+}
+
+static int
+txgbevf_vlan_offload_config(struct rte_eth_dev *dev, int mask)
+{
+       struct txgbe_rx_queue *rxq;
+       uint16_t i;
+       int on = 0;
+
+       /* VF function only support hw strip feature, others are not support */
+       if (mask & ETH_VLAN_STRIP_MASK) {
+               for (i = 0; i < dev->data->nb_rx_queues; i++) {
+                       rxq = dev->data->rx_queues[i];
+                       on = !!(rxq->offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
+                       txgbevf_vlan_strip_queue_set(dev, i, on);
+               }
+       }
+
+       return 0;
+}
+
+static int
+txgbevf_vlan_offload_set(struct rte_eth_dev *dev, int mask)
+{
+       txgbe_config_vlan_strip_on_all_queues(dev, mask);
+
+       txgbevf_vlan_offload_config(dev, mask);
+
+       return 0;
+}
+
 static int
 txgbevf_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
 {
@@ -660,8 +972,17 @@ txgbevf_dev_interrupt_handler(void *param)
  * operation have been implemented
  */
 static const struct eth_dev_ops txgbevf_eth_dev_ops = {
+       .dev_configure        = txgbevf_dev_configure,
        .link_update          = txgbevf_dev_link_update,
+       .stats_get            = txgbevf_dev_stats_get,
+       .xstats_get           = txgbevf_dev_xstats_get,
+       .stats_reset          = txgbevf_dev_stats_reset,
+       .xstats_reset         = txgbevf_dev_stats_reset,
+       .xstats_get_names     = txgbevf_dev_xstats_get_names,
        .dev_infos_get        = txgbevf_dev_info_get,
+       .vlan_filter_set      = txgbevf_vlan_filter_set,
+       .vlan_strip_queue_set = txgbevf_vlan_strip_queue_set,
+       .vlan_offload_set     = txgbevf_vlan_offload_set,
        .rx_queue_intr_enable = txgbevf_dev_rx_queue_intr_enable,
        .rx_queue_intr_disable = txgbevf_dev_rx_queue_intr_disable,
        .mac_addr_add         = txgbevf_add_mac_addr,