New upstream version 17.11.1
[deb_dpdk.git] / drivers / net / szedata2 / rte_eth_szedata2.c
index 483d789..45aebed 100644 (file)
@@ -45,6 +45,7 @@
 
 #include <rte_mbuf.h>
 #include <rte_ethdev.h>
+#include <rte_ethdev_pci.h>
 #include <rte_malloc.h>
 #include <rte_memcpy.h>
 #include <rte_kvargs.h>
@@ -52,6 +53,7 @@
 #include <rte_atomic.h>
 
 #include "rte_eth_szedata2.h"
+#include "szedata2_iobuf.h"
 
 #define RTE_ETH_SZEDATA2_MAX_RX_QUEUES 32
 #define RTE_ETH_SZEDATA2_MAX_TX_QUEUES 32
  */
 #define RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED 8
 
-#define RTE_SZEDATA2_DRIVER_NAME rte_szedata2_pmd
-#define RTE_SZEDATA2_PCI_DRIVER_NAME "rte_szedata2_pmd"
+#define RTE_SZEDATA2_DRIVER_NAME net_szedata2
 
 #define SZEDATA2_DEV_PATH_FMT "/dev/szedataII%u"
 
 struct szedata2_rx_queue {
        struct szedata *sze;
        uint8_t rx_channel;
-       uint8_t in_port;
+       uint16_t in_port;
        struct rte_mempool *mb_pool;
        volatile uint64_t rx_pkts;
        volatile uint64_t rx_bytes;
@@ -91,6 +92,7 @@ struct pmd_internals {
        uint16_t max_rx_queues;
        uint16_t max_tx_queues;
        char sze_dev[PATH_MAX];
+       struct rte_mem_resource *pci_rsc;
 };
 
 static struct ether_addr eth_addr = {
@@ -680,7 +682,7 @@ eth_szedata2_tx(void *queue,
        uint32_t hwpkt_len;
        uint32_t unlock_size;
        uint32_t rem_len;
-       uint8_t mbuf_segs;
+       uint16_t mbuf_segs;
        uint16_t pkt_left = nb_pkts;
 
        if (sze_q->sze == NULL || nb_pkts == 0)
@@ -1030,6 +1032,7 @@ eth_dev_info(struct rte_eth_dev *dev,
                struct rte_eth_dev_info *dev_info)
 {
        struct pmd_internals *internals = dev->data->dev_private;
+       dev_info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
        dev_info->if_index = 0;
        dev_info->max_mac_addrs = 1;
        dev_info->max_rx_pktlen = (uint32_t)-1;
@@ -1039,7 +1042,7 @@ eth_dev_info(struct rte_eth_dev *dev,
        dev_info->speed_capa = ETH_LINK_SPEED_100G;
 }
 
-static void
+static int
 eth_stats_get(struct rte_eth_dev *dev,
                struct rte_eth_stats *stats)
 {
@@ -1074,6 +1077,8 @@ eth_stats_get(struct rte_eth_dev *dev,
        stats->ibytes = rx_total_bytes;
        stats->obytes = tx_total_bytes;
        stats->oerrors = tx_err_total;
+
+       return 0;
 }
 
 static void
@@ -1137,6 +1142,33 @@ eth_dev_close(struct rte_eth_dev *dev)
        dev->data->nb_tx_queues = 0;
 }
 
+/**
+ * Function takes value from first IBUF status register.
+ * Values in IBUF and OBUF should be same.
+ *
+ * @param internals
+ *     Pointer to device private structure.
+ * @return
+ *     Link speed constant.
+ */
+static inline enum szedata2_link_speed
+get_link_speed(const struct pmd_internals *internals)
+{
+       const volatile struct szedata2_ibuf *ibuf =
+               ibuf_ptr_by_index(internals->pci_rsc, 0);
+       uint32_t speed = (szedata2_read32(&ibuf->ibuf_st) & 0x70) >> 4;
+       switch (speed) {
+       case 0x03:
+               return SZEDATA2_LINK_SPEED_10G;
+       case 0x04:
+               return SZEDATA2_LINK_SPEED_40G;
+       case 0x05:
+               return SZEDATA2_LINK_SPEED_100G;
+       default:
+               return SZEDATA2_LINK_SPEED_DEFAULT;
+       }
+}
+
 static int
 eth_link_update(struct rte_eth_dev *dev,
                int wait_to_complete __rte_unused)
@@ -1144,11 +1176,13 @@ eth_link_update(struct rte_eth_dev *dev,
        struct rte_eth_link link;
        struct rte_eth_link *link_ptr = &link;
        struct rte_eth_link *dev_link = &dev->data->dev_link;
-       volatile struct szedata2_cgmii_ibuf *ibuf = SZEDATA2_PCI_RESOURCE_PTR(
-                       dev, SZEDATA2_CGMII_IBUF_BASE_OFF,
-                       volatile struct szedata2_cgmii_ibuf *);
+       struct pmd_internals *internals = (struct pmd_internals *)
+               dev->data->dev_private;
+       const volatile struct szedata2_ibuf *ibuf;
+       uint32_t i;
+       bool link_is_up = false;
 
-       switch (cgmii_link_speed(ibuf)) {
+       switch (get_link_speed(internals)) {
        case SZEDATA2_LINK_SPEED_10G:
                link.link_speed = ETH_SPEED_NUM_10G;
                break;
@@ -1166,10 +1200,21 @@ eth_link_update(struct rte_eth_dev *dev,
        /* szedata2 uses only full duplex */
        link.link_duplex = ETH_LINK_FULL_DUPLEX;
 
-       link.link_status = (cgmii_ibuf_is_enabled(ibuf) &&
-                       cgmii_ibuf_is_link_up(ibuf)) ? ETH_LINK_UP : ETH_LINK_DOWN;
+       for (i = 0; i < szedata2_ibuf_count; i++) {
+               ibuf = ibuf_ptr_by_index(internals->pci_rsc, i);
+               /*
+                * Link is considered up if at least one ibuf is enabled
+                * and up.
+                */
+               if (ibuf_is_enabled(ibuf) && ibuf_is_link_up(ibuf)) {
+                       link_is_up = true;
+                       break;
+               }
+       }
+
+       link.link_status = (link_is_up) ? ETH_LINK_UP : ETH_LINK_DOWN;
 
-       link.link_autoneg = ETH_LINK_SPEED_FIXED;
+       link.link_autoneg = ETH_LINK_FIXED;
 
        rte_atomic64_cmpset((uint64_t *)dev_link, *(uint64_t *)dev_link,
                        *(uint64_t *)link_ptr);
@@ -1180,30 +1225,28 @@ eth_link_update(struct rte_eth_dev *dev,
 static int
 eth_dev_set_link_up(struct rte_eth_dev *dev)
 {
-       volatile struct szedata2_cgmii_ibuf *ibuf = SZEDATA2_PCI_RESOURCE_PTR(
-                       dev, SZEDATA2_CGMII_IBUF_BASE_OFF,
-                       volatile struct szedata2_cgmii_ibuf *);
-       volatile struct szedata2_cgmii_obuf *obuf = SZEDATA2_PCI_RESOURCE_PTR(
-                       dev, SZEDATA2_CGMII_OBUF_BASE_OFF,
-                       volatile struct szedata2_cgmii_obuf *);
-
-       cgmii_ibuf_enable(ibuf);
-       cgmii_obuf_enable(obuf);
+       struct pmd_internals *internals = (struct pmd_internals *)
+               dev->data->dev_private;
+       uint32_t i;
+
+       for (i = 0; i < szedata2_ibuf_count; i++)
+               ibuf_enable(ibuf_ptr_by_index(internals->pci_rsc, i));
+       for (i = 0; i < szedata2_obuf_count; i++)
+               obuf_enable(obuf_ptr_by_index(internals->pci_rsc, i));
        return 0;
 }
 
 static int
 eth_dev_set_link_down(struct rte_eth_dev *dev)
 {
-       volatile struct szedata2_cgmii_ibuf *ibuf = SZEDATA2_PCI_RESOURCE_PTR(
-                       dev, SZEDATA2_CGMII_IBUF_BASE_OFF,
-                       volatile struct szedata2_cgmii_ibuf *);
-       volatile struct szedata2_cgmii_obuf *obuf = SZEDATA2_PCI_RESOURCE_PTR(
-                       dev, SZEDATA2_CGMII_OBUF_BASE_OFF,
-                       volatile struct szedata2_cgmii_obuf *);
-
-       cgmii_ibuf_disable(ibuf);
-       cgmii_obuf_disable(obuf);
+       struct pmd_internals *internals = (struct pmd_internals *)
+               dev->data->dev_private;
+       uint32_t i;
+
+       for (i = 0; i < szedata2_ibuf_count; i++)
+               ibuf_disable(ibuf_ptr_by_index(internals->pci_rsc, i));
+       for (i = 0; i < szedata2_obuf_count; i++)
+               obuf_disable(obuf_ptr_by_index(internals->pci_rsc, i));
        return 0;
 }
 
@@ -1281,37 +1324,53 @@ eth_mac_addr_set(struct rte_eth_dev *dev __rte_unused,
 static void
 eth_promiscuous_enable(struct rte_eth_dev *dev)
 {
-       volatile struct szedata2_cgmii_ibuf *ibuf = SZEDATA2_PCI_RESOURCE_PTR(
-                       dev, SZEDATA2_CGMII_IBUF_BASE_OFF,
-                       volatile struct szedata2_cgmii_ibuf *);
-       cgmii_ibuf_mac_mode_write(ibuf, SZEDATA2_MAC_CHMODE_PROMISC);
+       struct pmd_internals *internals = (struct pmd_internals *)
+               dev->data->dev_private;
+       uint32_t i;
+
+       for (i = 0; i < szedata2_ibuf_count; i++) {
+               ibuf_mac_mode_write(ibuf_ptr_by_index(internals->pci_rsc, i),
+                               SZEDATA2_MAC_CHMODE_PROMISC);
+       }
 }
 
 static void
 eth_promiscuous_disable(struct rte_eth_dev *dev)
 {
-       volatile struct szedata2_cgmii_ibuf *ibuf = SZEDATA2_PCI_RESOURCE_PTR(
-                       dev, SZEDATA2_CGMII_IBUF_BASE_OFF,
-                       volatile struct szedata2_cgmii_ibuf *);
-       cgmii_ibuf_mac_mode_write(ibuf, SZEDATA2_MAC_CHMODE_ONLY_VALID);
+       struct pmd_internals *internals = (struct pmd_internals *)
+               dev->data->dev_private;
+       uint32_t i;
+
+       for (i = 0; i < szedata2_ibuf_count; i++) {
+               ibuf_mac_mode_write(ibuf_ptr_by_index(internals->pci_rsc, i),
+                               SZEDATA2_MAC_CHMODE_ONLY_VALID);
+       }
 }
 
 static void
 eth_allmulticast_enable(struct rte_eth_dev *dev)
 {
-       volatile struct szedata2_cgmii_ibuf *ibuf = SZEDATA2_PCI_RESOURCE_PTR(
-                       dev, SZEDATA2_CGMII_IBUF_BASE_OFF,
-                       volatile struct szedata2_cgmii_ibuf *);
-       cgmii_ibuf_mac_mode_write(ibuf, SZEDATA2_MAC_CHMODE_ALL_MULTICAST);
+       struct pmd_internals *internals = (struct pmd_internals *)
+               dev->data->dev_private;
+       uint32_t i;
+
+       for (i = 0; i < szedata2_ibuf_count; i++) {
+               ibuf_mac_mode_write(ibuf_ptr_by_index(internals->pci_rsc, i),
+                               SZEDATA2_MAC_CHMODE_ALL_MULTICAST);
+       }
 }
 
 static void
 eth_allmulticast_disable(struct rte_eth_dev *dev)
 {
-       volatile struct szedata2_cgmii_ibuf *ibuf = SZEDATA2_PCI_RESOURCE_PTR(
-                       dev, SZEDATA2_CGMII_IBUF_BASE_OFF,
-                       volatile struct szedata2_cgmii_ibuf *);
-       cgmii_ibuf_mac_mode_write(ibuf, SZEDATA2_MAC_CHMODE_ONLY_VALID);
+       struct pmd_internals *internals = (struct pmd_internals *)
+               dev->data->dev_private;
+       uint32_t i;
+
+       for (i = 0; i < szedata2_ibuf_count; i++) {
+               ibuf_mac_mode_write(ibuf_ptr_by_index(internals->pci_rsc, i),
+                               SZEDATA2_MAC_CHMODE_ONLY_VALID);
+       }
 }
 
 static const struct eth_dev_ops ops = {
@@ -1349,7 +1408,7 @@ static const struct eth_dev_ops ops = {
  *          -1 on error
  */
 static int
-get_szedata2_index(struct rte_eth_dev *dev, uint32_t *index)
+get_szedata2_index(const struct rte_pci_addr *pcislot_addr, uint32_t *index)
 {
        DIR *dir;
        struct dirent *entry;
@@ -1357,7 +1416,6 @@ get_szedata2_index(struct rte_eth_dev *dev, uint32_t *index)
        uint32_t tmp_index;
        FILE *fd;
        char pcislot_path[PATH_MAX];
-       struct rte_pci_addr pcislot_addr = dev->pci_dev->addr;
        uint32_t domain;
        uint32_t bus;
        uint32_t devid;
@@ -1392,10 +1450,10 @@ get_szedata2_index(struct rte_eth_dev *dev, uint32_t *index)
                if (ret != 4)
                        continue;
 
-               if (pcislot_addr.domain == domain &&
-                               pcislot_addr.bus == bus &&
-                               pcislot_addr.devid == devid &&
-                               pcislot_addr.function == function) {
+               if (pcislot_addr->domain == domain &&
+                               pcislot_addr->bus == bus &&
+                               pcislot_addr->devid == devid &&
+                               pcislot_addr->function == function) {
                        *index = tmp_index;
                        closedir(dir);
                        return 0;
@@ -1415,9 +1473,10 @@ rte_szedata2_eth_dev_init(struct rte_eth_dev *dev)
        struct szedata *szedata_temp;
        int ret;
        uint32_t szedata2_index;
-       struct rte_pci_addr *pci_addr = &dev->pci_dev->addr;
-       struct rte_pci_resource *pci_rsc =
-               &dev->pci_dev->mem_resource[PCI_RESOURCE_NUMBER];
+       struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
+       struct rte_pci_addr *pci_addr = &pci_dev->addr;
+       struct rte_mem_resource *pci_rsc =
+               &pci_dev->mem_resource[PCI_RESOURCE_NUMBER];
        char rsc_filename[PATH_MAX];
        void *pci_resource_ptr = NULL;
        int fd;
@@ -1427,7 +1486,7 @@ rte_szedata2_eth_dev_init(struct rte_eth_dev *dev)
                        pci_addr->function);
 
        /* Get index of szedata2 device file and create path to device file */
-       ret = get_szedata2_index(dev, &szedata2_index);
+       ret = get_szedata2_index(pci_addr, &szedata2_index);
        if (ret != 0) {
                RTE_LOG(ERR, PMD, "Failed to get szedata2 device index!\n");
                return -ENODEV;
@@ -1471,17 +1530,17 @@ rte_szedata2_eth_dev_init(struct rte_eth_dev *dev)
        /* Set function callbacks for Ethernet API */
        dev->dev_ops = &ops;
 
-       rte_eth_copy_pci_info(dev, dev->pci_dev);
+       rte_eth_copy_pci_info(dev, pci_dev);
 
-       /* mmap pci resource0 file to rte_pci_resource structure */
-       if (dev->pci_dev->mem_resource[PCI_RESOURCE_NUMBER].phys_addr ==
+       /* mmap pci resource0 file to rte_mem_resource structure */
+       if (pci_dev->mem_resource[PCI_RESOURCE_NUMBER].phys_addr ==
                        0) {
                RTE_LOG(ERR, PMD, "Missing resource%u file\n",
                                PCI_RESOURCE_NUMBER);
                return -EINVAL;
        }
        snprintf(rsc_filename, PATH_MAX,
-               "%s/" PCI_PRI_FMT "/resource%u", pci_get_sysfs_path(),
+               "%s/" PCI_PRI_FMT "/resource%u", rte_pci_get_sysfs_path(),
                pci_addr->domain, pci_addr->bus,
                pci_addr->devid, pci_addr->function, PCI_RESOURCE_NUMBER);
        fd = open(rsc_filename, O_RDWR);
@@ -1491,16 +1550,16 @@ rte_szedata2_eth_dev_init(struct rte_eth_dev *dev)
        }
 
        pci_resource_ptr = mmap(0,
-                       dev->pci_dev->mem_resource[PCI_RESOURCE_NUMBER].len,
+                       pci_dev->mem_resource[PCI_RESOURCE_NUMBER].len,
                        PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
        close(fd);
-       if (pci_resource_ptr == NULL) {
+       if (pci_resource_ptr == MAP_FAILED) {
                RTE_LOG(ERR, PMD, "Could not mmap file %s (fd = %d)\n",
                                rsc_filename, fd);
                return -EINVAL;
        }
-       dev->pci_dev->mem_resource[PCI_RESOURCE_NUMBER].addr =
-               pci_resource_ptr;
+       pci_dev->mem_resource[PCI_RESOURCE_NUMBER].addr = pci_resource_ptr;
+       internals->pci_rsc = pci_rsc;
 
        RTE_LOG(DEBUG, PMD, "resource%u phys_addr = 0x%llx len = %llu "
                        "virt addr = %llx\n", PCI_RESOURCE_NUMBER,
@@ -1516,8 +1575,8 @@ rte_szedata2_eth_dev_init(struct rte_eth_dev *dev)
                        RTE_CACHE_LINE_SIZE);
        if (data->mac_addrs == NULL) {
                RTE_LOG(ERR, PMD, "Could not alloc space for MAC address!\n");
-               munmap(dev->pci_dev->mem_resource[PCI_RESOURCE_NUMBER].addr,
-                       dev->pci_dev->mem_resource[PCI_RESOURCE_NUMBER].len);
+               munmap(pci_dev->mem_resource[PCI_RESOURCE_NUMBER].addr,
+                      pci_dev->mem_resource[PCI_RESOURCE_NUMBER].len);
                return -EINVAL;
        }
 
@@ -1537,12 +1596,13 @@ rte_szedata2_eth_dev_init(struct rte_eth_dev *dev)
 static int
 rte_szedata2_eth_dev_uninit(struct rte_eth_dev *dev)
 {
-       struct rte_pci_addr *pci_addr = &dev->pci_dev->addr;
+       struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
+       struct rte_pci_addr *pci_addr = &pci_dev->addr;
 
        rte_free(dev->data->mac_addrs);
        dev->data->mac_addrs = NULL;
-       munmap(dev->pci_dev->mem_resource[PCI_RESOURCE_NUMBER].addr,
-               dev->pci_dev->mem_resource[PCI_RESOURCE_NUMBER].len);
+       munmap(pci_dev->mem_resource[PCI_RESOURCE_NUMBER].addr,
+              pci_dev->mem_resource[PCI_RESOURCE_NUMBER].len);
 
        RTE_LOG(INFO, PMD, "szedata2 device ("
                        PCI_PRI_FMT ") successfully uninitialized\n",
@@ -1570,35 +1630,26 @@ static const struct rte_pci_id rte_szedata2_pci_id_table[] = {
        }
 };
 
-static struct eth_driver szedata2_eth_driver = {
-       .pci_drv = {
-               .name     = RTE_SZEDATA2_PCI_DRIVER_NAME,
-               .id_table = rte_szedata2_pci_id_table,
-       },
-       .eth_dev_init     = rte_szedata2_eth_dev_init,
-       .eth_dev_uninit   = rte_szedata2_eth_dev_uninit,
-       .dev_private_size = sizeof(struct pmd_internals),
-};
-
-static int
-rte_szedata2_init(const char *name __rte_unused,
-               const char *args __rte_unused)
+static int szedata2_eth_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
+       struct rte_pci_device *pci_dev)
 {
-       rte_eth_driver_register(&szedata2_eth_driver);
-       return 0;
+       return rte_eth_dev_pci_generic_probe(pci_dev,
+               sizeof(struct pmd_internals), rte_szedata2_eth_dev_init);
 }
 
-static int
-rte_szedata2_uninit(const char *name __rte_unused)
+static int szedata2_eth_pci_remove(struct rte_pci_device *pci_dev)
 {
-       return 0;
+       return rte_eth_dev_pci_generic_remove(pci_dev,
+               rte_szedata2_eth_dev_uninit);
 }
 
-static struct rte_driver rte_szedata2_driver = {
-       .type = PMD_PDEV,
-       .init = rte_szedata2_init,
-       .uninit = rte_szedata2_uninit,
+static struct rte_pci_driver szedata2_eth_driver = {
+       .id_table = rte_szedata2_pci_id_table,
+       .probe = szedata2_eth_pci_probe,
+       .remove = szedata2_eth_pci_remove,
 };
 
-PMD_REGISTER_DRIVER(rte_szedata2_driver, RTE_SZEDATA2_DRIVER_NAME);
-DRIVER_REGISTER_PCI_TABLE(RTE_SZEDATA2_DRIVER_NAME, rte_szedata2_pci_id_table);
+RTE_PMD_REGISTER_PCI(RTE_SZEDATA2_DRIVER_NAME, szedata2_eth_driver);
+RTE_PMD_REGISTER_PCI_TABLE(RTE_SZEDATA2_DRIVER_NAME, rte_szedata2_pci_id_table);
+RTE_PMD_REGISTER_KMOD_DEP(RTE_SZEDATA2_DRIVER_NAME,
+       "* combo6core & combov3 & szedata2 & szedata2_cv3");