ethdev: move info filling of PCI into drivers
[dpdk.git] / lib / librte_ether / rte_ethdev.c
index 0d9d9c1..8e02d2e 100644 (file)
@@ -212,8 +212,12 @@ rte_eth_dev_allocate(const char *name)
 
        eth_dev = &rte_eth_devices[port_id];
        eth_dev->data = &rte_eth_dev_data[port_id];
+       memset(eth_dev->data, 0, sizeof(*eth_dev->data));
        snprintf(eth_dev->data->name, sizeof(eth_dev->data->name), "%s", name);
        eth_dev->data->port_id = port_id;
+       eth_dev->data->mtu = ETHER_MTU;
+       TAILQ_INIT(&(eth_dev->link_intr_cbs));
+
        eth_dev->attached = DEV_ATTACHED;
        eth_dev_last_created_port = port_id;
        nb_ports++;
@@ -258,16 +262,8 @@ rte_eth_dev_pci_probe(struct rte_pci_driver *pci_drv,
                        rte_panic("Cannot allocate memzone for private port data\n");
        }
        eth_dev->pci_dev = pci_dev;
+       eth_dev->intr_handle = &pci_dev->intr_handle;
        eth_dev->driver = eth_drv;
-       eth_dev->data->rx_mbuf_alloc_failed = 0;
-
-       /* init user callbacks */
-       TAILQ_INIT(&(eth_dev->link_intr_cbs));
-
-       /*
-        * Set the default MTU.
-        */
-       eth_dev->data->mtu = ETHER_MTU;
 
        /* Invoke PMD device initialization function */
        diag = (*eth_drv->eth_dev_init)(eth_dev);
@@ -376,6 +372,9 @@ rte_eth_dev_get_port_by_name(const char *name, uint8_t *port_id)
                return -EINVAL;
        }
 
+       if (!nb_ports)
+               return -ENODEV;
+
        *port_id = RTE_MAX_ETHPORTS;
 
        for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
@@ -531,6 +530,9 @@ rte_eth_dev_rx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
 
                for (i = nb_queues; i < old_nb_queues; i++)
                        (*dev->dev_ops->rx_queue_release)(rxq[i]);
+
+               rte_free(dev->data->rx_queues);
+               dev->data->rx_queues = NULL;
        }
        dev->data->nb_rx_queues = nb_queues;
        return 0;
@@ -682,6 +684,9 @@ rte_eth_dev_tx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
 
                for (i = nb_queues; i < old_nb_queues; i++)
                        (*dev->dev_ops->tx_queue_release)(txq[i]);
+
+               rte_free(dev->data->tx_queues);
+               dev->data->tx_queues = NULL;
        }
        dev->data->nb_tx_queues = nb_queues;
        return 0;
@@ -852,6 +857,22 @@ rte_eth_dev_configure(uint8_t port_id, uint16_t nb_rx_q, uint16_t nb_tx_q,
        return 0;
 }
 
+void
+_rte_eth_dev_reset(struct rte_eth_dev *dev)
+{
+       if (dev->data->dev_started) {
+               RTE_PMD_DEBUG_TRACE(
+                       "port %d must be stopped to allow reset\n",
+                       dev->data->port_id);
+               return;
+       }
+
+       rte_eth_dev_rx_queue_config(dev, 0);
+       rte_eth_dev_tx_queue_config(dev, 0);
+
+       memset(&dev->data->dev_conf, 0, sizeof(dev->data->dev_conf));
+}
+
 static void
 rte_eth_dev_config_restore(uint8_t port_id)
 {
@@ -1010,6 +1031,7 @@ rte_eth_rx_queue_setup(uint8_t port_id, uint16_t rx_queue_id,
        uint32_t mbp_buf_size;
        struct rte_eth_dev *dev;
        struct rte_eth_dev_info dev_info;
+       void **rxq;
 
        RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
 
@@ -1068,6 +1090,14 @@ rte_eth_rx_queue_setup(uint8_t port_id, uint16_t rx_queue_id,
                return -EINVAL;
        }
 
+       rxq = dev->data->rx_queues;
+       if (rxq[rx_queue_id]) {
+               RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_release,
+                                       -ENOTSUP);
+               (*dev->dev_ops->rx_queue_release)(rxq[rx_queue_id]);
+               rxq[rx_queue_id] = NULL;
+       }
+
        if (rx_conf == NULL)
                rx_conf = &dev_info.default_rxconf;
 
@@ -1089,6 +1119,7 @@ rte_eth_tx_queue_setup(uint8_t port_id, uint16_t tx_queue_id,
 {
        struct rte_eth_dev *dev;
        struct rte_eth_dev_info dev_info;
+       void **txq;
 
        RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
 
@@ -1121,6 +1152,14 @@ rte_eth_tx_queue_setup(uint8_t port_id, uint16_t tx_queue_id,
                return -EINVAL;
        }
 
+       txq = dev->data->tx_queues;
+       if (txq[tx_queue_id]) {
+               RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_release,
+                                       -ENOTSUP);
+               (*dev->dev_ops->tx_queue_release)(txq[tx_queue_id]);
+               txq[tx_queue_id] = NULL;
+       }
+
        if (tx_conf == NULL)
                tx_conf = &dev_info.default_txconf;
 
@@ -1343,8 +1382,10 @@ get_xstats_count(uint8_t port_id)
        } else
                count = 0;
        count += RTE_NB_STATS;
-       count += dev->data->nb_rx_queues * RTE_NB_RXQ_STATS;
-       count += dev->data->nb_tx_queues * RTE_NB_TXQ_STATS;
+       count += RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS) *
+                RTE_NB_RXQ_STATS;
+       count += RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS) *
+                RTE_NB_TXQ_STATS;
        return count;
 }
 
@@ -1358,6 +1399,7 @@ rte_eth_xstats_get_names(uint8_t port_id,
        int cnt_expected_entries;
        int cnt_driver_entries;
        uint32_t idx, id_queue;
+       uint16_t num_q;
 
        cnt_expected_entries = get_xstats_count(port_id);
        if (xstats_names == NULL || cnt_expected_entries < 0 ||
@@ -1374,7 +1416,8 @@ rte_eth_xstats_get_names(uint8_t port_id,
                        "%s", rte_stats_strings[idx].name);
                cnt_used_entries++;
        }
-       for (id_queue = 0; id_queue < dev->data->nb_rx_queues; id_queue++) {
+       num_q = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
+       for (id_queue = 0; id_queue < num_q; id_queue++) {
                for (idx = 0; idx < RTE_NB_RXQ_STATS; idx++) {
                        snprintf(xstats_names[cnt_used_entries].name,
                                sizeof(xstats_names[0].name),
@@ -1384,7 +1427,8 @@ rte_eth_xstats_get_names(uint8_t port_id,
                }
 
        }
-       for (id_queue = 0; id_queue < dev->data->nb_tx_queues; id_queue++) {
+       num_q = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
+       for (id_queue = 0; id_queue < num_q; id_queue++) {
                for (idx = 0; idx < RTE_NB_TXQ_STATS; idx++) {
                        snprintf(xstats_names[cnt_used_entries].name,
                                sizeof(xstats_names[0].name),
@@ -1420,14 +1464,18 @@ rte_eth_xstats_get(uint8_t port_id, struct rte_eth_xstat *xstats,
        unsigned count = 0, i, q;
        signed xcount = 0;
        uint64_t val, *stats_ptr;
+       uint16_t nb_rxqs, nb_txqs;
 
        RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
 
        dev = &rte_eth_devices[port_id];
 
+       nb_rxqs = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
+       nb_txqs = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
+
        /* Return generic statistics */
-       count = RTE_NB_STATS + (dev->data->nb_rx_queues * RTE_NB_RXQ_STATS) +
-               (dev->data->nb_tx_queues * RTE_NB_TXQ_STATS);
+       count = RTE_NB_STATS + (nb_rxqs * RTE_NB_RXQ_STATS) +
+               (nb_txqs * RTE_NB_TXQ_STATS);
 
        /* implemented by the driver */
        if (dev->dev_ops->xstats_get != NULL) {
@@ -1458,7 +1506,7 @@ rte_eth_xstats_get(uint8_t port_id, struct rte_eth_xstat *xstats,
        }
 
        /* per-rxq stats */
-       for (q = 0; q < dev->data->nb_rx_queues; q++) {
+       for (q = 0; q < nb_rxqs; q++) {
                for (i = 0; i < RTE_NB_RXQ_STATS; i++) {
                        stats_ptr = RTE_PTR_ADD(&eth_stats,
                                        rte_rxq_stats_strings[i].offset +
@@ -1469,7 +1517,7 @@ rte_eth_xstats_get(uint8_t port_id, struct rte_eth_xstat *xstats,
        }
 
        /* per-txq stats */
-       for (q = 0; q < dev->data->nb_tx_queues; q++) {
+       for (q = 0; q < nb_txqs; q++) {
                for (i = 0; i < RTE_NB_TXQ_STATS; i++) {
                        stats_ptr = RTE_PTR_ADD(&eth_stats,
                                        rte_txq_stats_strings[i].offset +
@@ -1556,7 +1604,6 @@ rte_eth_dev_info_get(uint8_t port_id, struct rte_eth_dev_info *dev_info)
 
        RTE_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;
        dev_info->driver_name = dev->data->drv_name;
        dev_info->nb_rx_queues = dev->data->nb_rx_queues;
        dev_info->nb_tx_queues = dev->data->nb_tx_queues;
@@ -2449,14 +2496,15 @@ rte_eth_dev_callback_register(uint8_t port_id,
        }
 
        /* create a new callback. */
-       if (user_cb == NULL)
+       if (user_cb == NULL) {
                user_cb = rte_zmalloc("INTR_USER_CALLBACK",
                                        sizeof(struct rte_eth_dev_callback), 0);
-       if (user_cb != NULL) {
-               user_cb->cb_fn = cb_fn;
-               user_cb->cb_arg = cb_arg;
-               user_cb->event = event;
-               TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
+               if (user_cb != NULL) {
+                       user_cb->cb_fn = cb_fn;
+                       user_cb->cb_arg = cb_arg;
+                       user_cb->event = event;
+                       TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
+               }
        }
 
        rte_spinlock_unlock(&rte_eth_dev_cb_lock);
@@ -2543,7 +2591,13 @@ rte_eth_dev_rx_intr_ctl(uint8_t port_id, int epfd, int op, void *data)
        RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
 
        dev = &rte_eth_devices[port_id];
-       intr_handle = &dev->pci_dev->intr_handle;
+
+       if (!dev->intr_handle) {
+               RTE_PMD_DEBUG_TRACE("RX Intr handle unset\n");
+               return -ENOTSUP;
+       }
+
+       intr_handle = dev->intr_handle;
        if (!intr_handle->intr_vec) {
                RTE_PMD_DEBUG_TRACE("RX Intr vector unset\n");
                return -EPERM;
@@ -2603,7 +2657,12 @@ rte_eth_dev_rx_intr_ctl_q(uint8_t port_id, uint16_t queue_id,
                return -EINVAL;
        }
 
-       intr_handle = &dev->pci_dev->intr_handle;
+       if (!dev->intr_handle) {
+               RTE_PMD_DEBUG_TRACE("RX Intr handle unset\n");
+               return -ENOTSUP;
+       }
+
+       intr_handle = dev->intr_handle;
        if (!intr_handle->intr_vec) {
                RTE_PMD_DEBUG_TRACE("RX Intr vector unset\n");
                return -EPERM;
@@ -3205,6 +3264,8 @@ rte_eth_copy_pci_info(struct rte_eth_dev *eth_dev, struct rte_pci_device *pci_de
                return;
        }
 
+       eth_dev->intr_handle = &pci_dev->intr_handle;
+
        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;