xen: allow determining DOM0 at runtime
[dpdk.git] / lib / librte_ether / rte_ethdev.c
index 073ffe9..0e41387 100644 (file)
@@ -427,7 +427,7 @@ rte_eth_dev_socket_id(uint8_t port_id)
 {
        if (!rte_eth_dev_is_valid_port(port_id))
                return -1;
-       return rte_eth_devices[port_id].pci_dev->numa_node;
+       return rte_eth_devices[port_id].data->numa_node;
 }
 
 uint8_t
@@ -444,32 +444,6 @@ rte_eth_dev_get_device_type(uint8_t port_id)
        return rte_eth_devices[port_id].dev_type;
 }
 
-static int
-rte_eth_dev_save(struct rte_eth_dev *devs, size_t size)
-{
-       if ((devs == NULL) ||
-           (size != sizeof(struct rte_eth_dev) * RTE_MAX_ETHPORTS))
-               return -EINVAL;
-
-       /* save current rte_eth_devices */
-       memcpy(devs, rte_eth_devices, size);
-       return 0;
-}
-
-static int
-rte_eth_dev_get_changed_port(struct rte_eth_dev *devs, uint8_t *port_id)
-{
-       if ((devs == NULL) || (port_id == NULL))
-               return -EINVAL;
-
-       /* check which port was attached or detached */
-       for (*port_id = 0; *port_id < RTE_MAX_ETHPORTS; (*port_id)++, devs++) {
-               if (rte_eth_devices[*port_id].attached ^ devs->attached)
-                       return 0;
-       }
-       return -ENODEV;
-}
-
 static int
 rte_eth_dev_get_addr_by_port(uint8_t port_id, struct rte_pci_addr *addr)
 {
@@ -503,60 +477,100 @@ rte_eth_dev_get_name_by_port(uint8_t port_id, char *name)
        return 0;
 }
 
+static int
+rte_eth_dev_get_port_by_name(const char *name, uint8_t *port_id)
+{
+       int i;
+
+       if (name == NULL) {
+               PMD_DEBUG_TRACE("Null pointer is specified\n");
+               return -EINVAL;
+       }
+
+       *port_id = RTE_MAX_ETHPORTS;
+
+       for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
+
+               if (!strncmp(name,
+                       rte_eth_dev_data[i].name, strlen(name))) {
+
+                       *port_id = i;
+
+                       return 0;
+               }
+       }
+       return -ENODEV;
+}
+
+static int
+rte_eth_dev_get_port_by_addr(const struct rte_pci_addr *addr, uint8_t *port_id)
+{
+       int i;
+       struct rte_pci_device *pci_dev = NULL;
+
+       if (addr == NULL) {
+               PMD_DEBUG_TRACE("Null pointer is specified\n");
+               return -EINVAL;
+       }
+
+       *port_id = RTE_MAX_ETHPORTS;
+
+       for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
+
+               pci_dev = rte_eth_devices[i].pci_dev;
+
+               if (pci_dev &&
+                       !rte_eal_compare_pci_addr(&pci_dev->addr, addr)) {
+
+                       *port_id = i;
+
+                       return 0;
+               }
+       }
+       return -ENODEV;
+}
+
 static int
 rte_eth_dev_is_detachable(uint8_t port_id)
 {
-       uint32_t drv_flags;
+       uint32_t dev_flags;
 
        if (!rte_eth_dev_is_valid_port(port_id)) {
                PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
                return -EINVAL;
        }
 
-       if (rte_eth_devices[port_id].dev_type == RTE_ETH_DEV_PCI) {
-               switch (rte_eth_devices[port_id].pci_dev->kdrv) {
-               case RTE_KDRV_IGB_UIO:
-               case RTE_KDRV_UIO_GENERIC:
-               case RTE_KDRV_NIC_UIO:
-                       break;
-               case RTE_KDRV_VFIO:
-               default:
-                       return -ENOTSUP;
-               }
+       switch (rte_eth_devices[port_id].data->kdrv) {
+       case RTE_KDRV_IGB_UIO:
+       case RTE_KDRV_UIO_GENERIC:
+       case RTE_KDRV_NIC_UIO:
+       case RTE_KDRV_NONE:
+               break;
+       case RTE_KDRV_VFIO:
+       default:
+               return -ENOTSUP;
        }
-
-       drv_flags = rte_eth_devices[port_id].driver->pci_drv.drv_flags;
-       return !(drv_flags & RTE_PCI_DRV_DETACHABLE);
+       dev_flags = rte_eth_devices[port_id].data->dev_flags;
+       return !(dev_flags & RTE_ETH_DEV_DETACHABLE);
 }
 
 /* attach the new physical device, then store port_id of the device */
 static int
 rte_eth_dev_attach_pdev(struct rte_pci_addr *addr, uint8_t *port_id)
 {
-       uint8_t new_port_id;
-       struct rte_eth_dev devs[RTE_MAX_ETHPORTS];
-
        if ((addr == NULL) || (port_id == NULL))
                goto err;
 
-       /* save current port status */
-       if (rte_eth_dev_save(devs, sizeof(devs)))
-               goto err;
        /* re-construct pci_device_list */
        if (rte_eal_pci_scan())
                goto err;
-       /* invoke probe func of the driver can handle the new device.
-        * TODO:
-        * rte_eal_pci_probe_one() should return port_id.
-        * And rte_eth_dev_save() and rte_eth_dev_get_changed_port()
-        * should be removed. */
+       /* Invoke probe func of the driver can handle the new device. */
        if (rte_eal_pci_probe_one(addr))
                goto err;
-       /* get port_id enabled by above procedures */
-       if (rte_eth_dev_get_changed_port(devs, &new_port_id))
+
+       if (rte_eth_dev_get_port_by_addr(addr, port_id))
                goto err;
 
-       *port_id = new_port_id;
        return 0;
 err:
        RTE_LOG(ERR, EAL, "Driver, cannot attach the device\n");
@@ -603,8 +617,6 @@ static int
 rte_eth_dev_attach_vdev(const char *vdevargs, uint8_t *port_id)
 {
        char *name = NULL, *args = NULL;
-       uint8_t new_port_id;
-       struct rte_eth_dev devs[RTE_MAX_ETHPORTS];
        int ret = -1;
 
        if ((vdevargs == NULL) || (port_id == NULL))
@@ -614,22 +626,18 @@ rte_eth_dev_attach_vdev(const char *vdevargs, uint8_t *port_id)
        if (rte_eal_parse_devargs_str(vdevargs, &name, &args))
                goto end;
 
-       /* save current port status */
-       if (rte_eth_dev_save(devs, sizeof(devs)))
-               goto end;
        /* walk around dev_driver_list to find the driver of the device,
-        * then invoke probe function o the driver.
-        * TODO:
-        * rte_eal_vdev_init() should return port_id,
-        * And rte_eth_dev_save() and rte_eth_dev_get_changed_port()
-        * should be removed. */
+        * then invoke probe function of the driver.
+        * rte_eal_vdev_init() updates port_id allocated after
+        * initialization.
+        */
        if (rte_eal_vdev_init(name, args))
                goto end;
-       /* get port_id enabled by above procedures */
-       if (rte_eth_dev_get_changed_port(devs, &new_port_id))
+
+       if (rte_eth_dev_get_port_by_name(name, port_id))
                goto end;
+
        ret = 0;
-       *port_id = new_port_id;
 end:
        if (name)
                free(name);
@@ -770,6 +778,13 @@ rte_eth_dev_rx_queue_start(uint8_t port_id, uint16_t rx_queue_id)
 
        FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_start, -ENOTSUP);
 
+       if (dev->data->rx_queue_state[rx_queue_id] != RTE_ETH_QUEUE_STATE_STOPPED) {
+               PMD_DEBUG_TRACE("Queue %" PRIu16" of device with port_id=%" PRIu8
+                       " already started\n",
+                       rx_queue_id, port_id);
+               return 0;
+       }
+
        return dev->dev_ops->rx_queue_start(dev, rx_queue_id);
 
 }
@@ -793,6 +808,13 @@ rte_eth_dev_rx_queue_stop(uint8_t port_id, uint16_t rx_queue_id)
 
        FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_stop, -ENOTSUP);
 
+       if (dev->data->rx_queue_state[rx_queue_id] == RTE_ETH_QUEUE_STATE_STOPPED) {
+               PMD_DEBUG_TRACE("Queue %" PRIu16" of device with port_id=%" PRIu8
+                       " already stopped\n",
+                       rx_queue_id, port_id);
+               return 0;
+       }
+
        return dev->dev_ops->rx_queue_stop(dev, rx_queue_id);
 
 }
@@ -816,6 +838,13 @@ rte_eth_dev_tx_queue_start(uint8_t port_id, uint16_t tx_queue_id)
 
        FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_start, -ENOTSUP);
 
+       if (dev->data->tx_queue_state[tx_queue_id] != RTE_ETH_QUEUE_STATE_STOPPED) {
+               PMD_DEBUG_TRACE("Queue %" PRIu16" of device with port_id=%" PRIu8
+                       " already started\n",
+                       tx_queue_id, port_id);
+               return 0;
+       }
+
        return dev->dev_ops->tx_queue_start(dev, tx_queue_id);
 
 }
@@ -839,6 +868,13 @@ rte_eth_dev_tx_queue_stop(uint8_t port_id, uint16_t tx_queue_id)
 
        FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_stop, -ENOTSUP);
 
+       if (dev->data->tx_queue_state[tx_queue_id] == RTE_ETH_QUEUE_STATE_STOPPED) {
+               PMD_DEBUG_TRACE("Queue %" PRIu16" of device with port_id=%" PRIu8
+                       " already stopped\n",
+                       tx_queue_id, port_id);
+               return 0;
+       }
+
        return dev->dev_ops->tx_queue_stop(dev, tx_queue_id);
 
 }
@@ -955,14 +991,11 @@ rte_eth_dev_configure(uint8_t port_id, uint16_t nb_rx_q, uint16_t nb_tx_q,
         * If link state interrupt is enabled, check that the
         * device supports it.
         */
-       if (dev_conf->intr_conf.lsc == 1) {
-               const struct rte_pci_driver *pci_drv = &dev->driver->pci_drv;
-
-               if (!(pci_drv->drv_flags & RTE_PCI_DRV_INTR_LSC)) {
+       if ((dev_conf->intr_conf.lsc == 1) &&
+               (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC))) {
                        PMD_DEBUG_TRACE("driver %s does not support lsc\n",
-                                       pci_drv->name);
+                                       dev->data->drv_name);
                        return -EINVAL;
-               }
        }
 
        /*
@@ -1104,7 +1137,7 @@ rte_eth_dev_start(uint8_t port_id)
 
        rte_eth_dev_config_restore(port_id);
 
-       if (dev->data->dev_conf.intr_conf.lsc != 0) {
+       if (dev->data->dev_conf.intr_conf.lsc == 0) {
                FUNC_PTR_OR_ERR_RET(*dev->dev_ops->link_update, -ENOTSUP);
                (*dev->dev_ops->link_update)(dev, 0);
        }
@@ -1482,7 +1515,8 @@ rte_eth_xstats_get(uint8_t port_id, struct rte_eth_xstats *xstats,
        dev = &rte_eth_devices[port_id];
 
        /* Return generic statistics */
-       count = RTE_NB_STATS;
+       count = RTE_NB_STATS + (dev->data->nb_rx_queues * RTE_NB_RXQ_STATS) +
+               (dev->data->nb_tx_queues * RTE_NB_TXQ_STATS);
 
        /* implemented by the driver */
        if (dev->dev_ops->xstats_get != NULL) {
@@ -1494,9 +1528,6 @@ rte_eth_xstats_get(uint8_t port_id, struct rte_eth_xstats *xstats,
 
                if (xcount < 0)
                        return xcount;
-       } else {
-               count += dev->data->nb_rx_queues * RTE_NB_RXQ_STATS;
-               count += dev->data->nb_tx_queues * RTE_NB_TXQ_STATS;
        }
 
        if (n < count + xcount)
@@ -1516,10 +1547,6 @@ rte_eth_xstats_get(uint8_t port_id, struct rte_eth_xstats *xstats,
                xstats[count++].value = val;
        }
 
-       /* if xstats_get() is implemented by the PMD, the Q stats are done */
-       if (dev->dev_ops->xstats_get != NULL)
-               return count + xcount;
-
        /* per-rxq stats */
        for (q = 0; q < dev->data->nb_rx_queues; q++) {
                for (i = 0; i < RTE_NB_RXQ_STATS; i++) {
@@ -1624,8 +1651,7 @@ rte_eth_dev_info_get(uint8_t port_id, struct rte_eth_dev_info *dev_info)
        FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
        (*dev->dev_ops->dev_infos_get)(dev, dev_info);
        dev_info->pci_dev = dev->pci_dev;
-       if (dev->driver)
-               dev_info->driver_name = dev->driver->pci_drv.name;
+       dev_info->driver_name = dev->data->drv_name;
 }
 
 void
@@ -2668,6 +2694,30 @@ rte_eth_dev_rx_intr_ctl(uint8_t port_id, int epfd, int op, void *data)
        return 0;
 }
 
+const struct rte_memzone *
+rte_eth_dma_zone_reserve(const struct rte_eth_dev *dev, const char *ring_name,
+                        uint16_t queue_id, size_t size, unsigned align,
+                        int socket_id)
+{
+       char z_name[RTE_MEMZONE_NAMESIZE];
+       const struct rte_memzone *mz;
+
+       snprintf(z_name, sizeof(z_name), "%s_%s_%d_%d",
+                dev->driver->pci_drv.name, ring_name,
+                dev->data->port_id, queue_id);
+
+       mz = rte_memzone_lookup(z_name);
+       if (mz)
+               return mz;
+
+       if (is_xen_dom0_supported())
+               return rte_memzone_reserve_bounded(z_name, size, socket_id,
+                                                  0, align, RTE_PGSIZE_2M);
+       else
+               return rte_memzone_reserve_aligned(z_name, size, socket_id,
+                                                  0, align);
+}
+
 int
 rte_eth_dev_rx_intr_ctl_q(uint8_t port_id, uint16_t queue_id,
                          int epfd, int op, void *data)
@@ -3237,3 +3287,23 @@ rte_eth_dev_get_dcb_info(uint8_t port_id,
        FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_dcb_info, -ENOTSUP);
        return (*dev->dev_ops->get_dcb_info)(dev, dcb_info);
 }
+
+void
+rte_eth_copy_pci_info(struct rte_eth_dev *eth_dev, struct rte_pci_device *pci_dev)
+{
+       if ((eth_dev == NULL) || (pci_dev == NULL)) {
+               PMD_DEBUG_TRACE("NULL pointer eth_dev=%p pci_dev=%p\n",
+                               eth_dev, pci_dev);
+               return;
+       }
+
+       eth_dev->data->dev_flags = 0;
+       if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
+               eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
+       if (pci_dev->driver->drv_flags & RTE_PCI_DRV_DETACHABLE)
+               eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
+
+       eth_dev->data->kdrv = pci_dev->kdrv;
+       eth_dev->data->numa_node = pci_dev->numa_node;
+       eth_dev->data->drv_name = pci_dev->driver->name;
+}