net/ixgbe/base: update annotations
[dpdk.git] / lib / librte_ether / rte_ethdev.c
index 68b0318..394aa26 100644 (file)
@@ -1,34 +1,5 @@
-/*-
- *   BSD LICENSE
- *
- *   Copyright(c) 2010-2017 Intel Corporation. All rights reserved.
- *   All rights reserved.
- *
- *   Redistribution and use in source and binary forms, with or without
- *   modification, are permitted provided that the following conditions
- *   are met:
- *
- *     * Redistributions of source code must retain the above copyright
- *       notice, this list of conditions and the following disclaimer.
- *     * Redistributions in binary form must reproduce the above copyright
- *       notice, this list of conditions and the following disclaimer in
- *       the documentation and/or other materials provided with the
- *       distribution.
- *     * Neither the name of Intel Corporation nor the names of its
- *       contributors may be used to endorse or promote products derived
- *       from this software without specific prior written permission.
- *
- *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2010-2017 Intel Corporation
  */
 
 #include <sys/types.h>
@@ -47,7 +18,6 @@
 #include <rte_log.h>
 #include <rte_debug.h>
 #include <rte_interrupts.h>
-#include <rte_pci.h>
 #include <rte_memory.h>
 #include <rte_memcpy.h>
 #include <rte_memzone.h>
@@ -94,6 +64,7 @@ static const struct rte_eth_xstats_name_off rte_stats_strings[] = {
        {"tx_good_packets", offsetof(struct rte_eth_stats, opackets)},
        {"rx_good_bytes", offsetof(struct rte_eth_stats, ibytes)},
        {"tx_good_bytes", offsetof(struct rte_eth_stats, obytes)},
+       {"rx_missed_errors", offsetof(struct rte_eth_stats, imissed)},
        {"rx_errors", offsetof(struct rte_eth_stats, ierrors)},
        {"tx_errors", offsetof(struct rte_eth_stats, oerrors)},
        {"rx_mbuf_allocation_errors", offsetof(struct rte_eth_stats,
@@ -363,21 +334,6 @@ rte_eth_dev_get_port_by_name(const char *name, uint16_t *port_id)
        return -ENODEV;
 }
 
-static int
-rte_eth_dev_is_detachable(uint16_t port_id)
-{
-       uint32_t dev_flags;
-
-       RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
-
-       dev_flags = rte_eth_devices[port_id].data->dev_flags;
-       if ((dev_flags & RTE_ETH_DEV_DETACHABLE) &&
-               (!(dev_flags & RTE_ETH_DEV_BONDED_SLAVE)))
-               return 0;
-       else
-               return 1;
-}
-
 /* attach the new device, then store port_id of the device */
 int
 rte_eth_dev_attach(const char *devargs, uint16_t *port_id)
@@ -428,16 +384,23 @@ err:
 int
 rte_eth_dev_detach(uint16_t port_id, char *name)
 {
+       uint32_t dev_flags;
        int ret = -1;
 
+       RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
+
        if (name == NULL) {
                ret = -EINVAL;
                goto err;
        }
 
-       /* FIXME: move this to eal, once device flags are relocated there */
-       if (rte_eth_dev_is_detachable(port_id))
+       dev_flags = rte_eth_devices[port_id].data->dev_flags;
+       if (dev_flags & RTE_ETH_DEV_BONDED_SLAVE) {
+               RTE_LOG(ERR, EAL, "Port %" PRIu16 " is bonded, cannot detach\n",
+                       port_id);
+               ret = -ENOTSUP;
                goto err;
+       }
 
        snprintf(name, sizeof(rte_eth_devices[port_id].data->name),
                 "%s", rte_eth_devices[port_id].data->name);
@@ -819,7 +782,7 @@ rte_eth_dev_configure(uint16_t port_id, uint16_t nb_rx_q, uint16_t nb_tx_q,
         * Convert between the offloads API to enable PMDs to support
         * only one of them.
         */
-       if ((dev_conf->rxmode.ignore_offload_bitfield == 0)) {
+       if (dev_conf->rxmode.ignore_offload_bitfield == 0) {
                rte_eth_convert_rx_offload_bitfield(
                                &dev_conf->rxmode, &local_conf.rxmode.offloads);
        } else {
@@ -1563,6 +1526,22 @@ rte_eth_stats_reset(uint16_t port_id)
        return 0;
 }
 
+static inline int
+get_xstats_basic_count(struct rte_eth_dev *dev)
+{
+       uint16_t nb_rxqs, nb_txqs;
+       int count;
+
+       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);
+
+       count = RTE_NB_STATS;
+       count += nb_rxqs * RTE_NB_RXQ_STATS;
+       count += nb_txqs * RTE_NB_TXQ_STATS;
+
+       return count;
+}
+
 static int
 get_xstats_count(uint16_t port_id)
 {
@@ -1584,11 +1563,9 @@ get_xstats_count(uint16_t port_id)
        } else
                count = 0;
 
-       count += RTE_NB_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;
+
+       count += get_xstats_basic_count(dev);
+
        return count;
 }
 
@@ -1636,6 +1613,45 @@ rte_eth_xstats_get_id_by_name(uint16_t port_id, const char *xstat_name,
        return -EINVAL;
 }
 
+/* retrieve basic stats names */
+static int
+rte_eth_basic_stats_get_names(struct rte_eth_dev *dev,
+       struct rte_eth_xstat_name *xstats_names)
+{
+       int cnt_used_entries = 0;
+       uint32_t idx, id_queue;
+       uint16_t num_q;
+
+       for (idx = 0; idx < RTE_NB_STATS; idx++) {
+               snprintf(xstats_names[cnt_used_entries].name,
+                       sizeof(xstats_names[0].name),
+                       "%s", rte_stats_strings[idx].name);
+               cnt_used_entries++;
+       }
+       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),
+                               "rx_q%u%s",
+                               id_queue, rte_rxq_stats_strings[idx].name);
+                       cnt_used_entries++;
+               }
+
+       }
+       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),
+                               "tx_q%u%s",
+                               id_queue, rte_txq_stats_strings[idx].name);
+                       cnt_used_entries++;
+               }
+       }
+       return cnt_used_entries;
+}
+
 /* retrieve ethdev extended statistics names */
 int
 rte_eth_xstats_get_names_by_id(uint16_t port_id,
@@ -1643,14 +1659,23 @@ rte_eth_xstats_get_names_by_id(uint16_t port_id,
        uint64_t *ids)
 {
        struct rte_eth_xstat_name *xstats_names_copy;
+       unsigned int no_basic_stat_requested = 1;
+       unsigned int no_ext_stat_requested = 1;
        unsigned int expected_entries;
+       unsigned int basic_count;
        struct rte_eth_dev *dev;
        unsigned int i;
+       int ret;
 
        RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
-       expected_entries = get_xstats_count(port_id);
        dev = &rte_eth_devices[port_id];
 
+       basic_count = get_xstats_basic_count(dev);
+       ret = get_xstats_count(port_id);
+       if (ret < 0)
+               return ret;
+       expected_entries = (unsigned int)ret;
+
        /* Return max number of stats if no ids given */
        if (!ids) {
                if (!xstats_names)
@@ -1662,9 +1687,26 @@ rte_eth_xstats_get_names_by_id(uint16_t port_id,
        if (ids && !xstats_names)
                return -EINVAL;
 
-       if (dev->dev_ops->xstats_get_names_by_id != NULL)
-               return (*dev->dev_ops->xstats_get_names_by_id)(
-                               dev, xstats_names, ids, size);
+       if (ids && dev->dev_ops->xstats_get_names_by_id != NULL && size > 0) {
+               uint64_t ids_copy[size];
+
+               for (i = 0; i < size; i++) {
+                       if (ids[i] < basic_count) {
+                               no_basic_stat_requested = 0;
+                               break;
+                       }
+
+                       /*
+                        * Convert ids to xstats ids that PMD knows.
+                        * ids known by user are basic + extended stats.
+                        */
+                       ids_copy[i] = ids[i] - basic_count;
+               }
+
+               if (no_basic_stat_requested)
+                       return (*dev->dev_ops->xstats_get_names_by_id)(dev,
+                                       xstats_names, ids_copy, size);
+       }
 
        /* Retrieve all stats */
        if (!ids) {
@@ -1684,8 +1726,22 @@ rte_eth_xstats_get_names_by_id(uint16_t port_id,
                return -ENOMEM;
        }
 
+       if (ids) {
+               for (i = 0; i < size; i++) {
+                       if (ids[i] > basic_count) {
+                               no_ext_stat_requested = 0;
+                               break;
+                       }
+               }
+       }
+
        /* Fill xstats_names_copy structure */
-       rte_eth_xstats_get_names(port_id, xstats_names_copy, expected_entries);
+       if (ids && no_ext_stat_requested) {
+               rte_eth_basic_stats_get_names(dev, xstats_names_copy);
+       } else {
+               rte_eth_xstats_get_names(port_id, xstats_names_copy,
+                       expected_entries);
+       }
 
        /* Filter stats */
        for (i = 0; i < size; i++) {
@@ -1710,8 +1766,6 @@ rte_eth_xstats_get_names(uint16_t port_id,
        int cnt_used_entries;
        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 ||
@@ -1720,35 +1774,9 @@ rte_eth_xstats_get_names(uint16_t port_id,
 
        /* port_id checked in get_xstats_count() */
        dev = &rte_eth_devices[port_id];
-       cnt_used_entries = 0;
-
-       for (idx = 0; idx < RTE_NB_STATS; idx++) {
-               snprintf(xstats_names[cnt_used_entries].name,
-                       sizeof(xstats_names[0].name),
-                       "%s", rte_stats_strings[idx].name);
-               cnt_used_entries++;
-       }
-       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),
-                               "rx_q%u%s",
-                               id_queue, rte_rxq_stats_strings[idx].name);
-                       cnt_used_entries++;
-               }
 
-       }
-       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),
-                               "tx_q%u%s",
-                               id_queue, rte_txq_stats_strings[idx].name);
-                       cnt_used_entries++;
-               }
-       }
+       cnt_used_entries = rte_eth_basic_stats_get_names(
+               dev, xstats_names);
 
        if (dev->dev_ops->xstats_get_names != NULL) {
                /* If there are any driver-specific xstats, append them
@@ -1766,20 +1794,73 @@ rte_eth_xstats_get_names(uint16_t port_id,
        return cnt_used_entries;
 }
 
+
+static int
+rte_eth_basic_stats_get(uint16_t port_id, struct rte_eth_xstat *xstats)
+{
+       struct rte_eth_dev *dev;
+       struct rte_eth_stats eth_stats;
+       unsigned int count = 0, i, q;
+       uint64_t val, *stats_ptr;
+       uint16_t nb_rxqs, nb_txqs;
+
+       rte_eth_stats_get(port_id, &eth_stats);
+       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);
+
+       /* global stats */
+       for (i = 0; i < RTE_NB_STATS; i++) {
+               stats_ptr = RTE_PTR_ADD(&eth_stats,
+                                       rte_stats_strings[i].offset);
+               val = *stats_ptr;
+               xstats[count++].value = val;
+       }
+
+       /* per-rxq stats */
+       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 +
+                                       q * sizeof(uint64_t));
+                       val = *stats_ptr;
+                       xstats[count++].value = val;
+               }
+       }
+
+       /* per-txq stats */
+       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 +
+                                       q * sizeof(uint64_t));
+                       val = *stats_ptr;
+                       xstats[count++].value = val;
+               }
+       }
+       return count;
+}
+
 /* retrieve ethdev extended statistics */
 int
 rte_eth_xstats_get_by_id(uint16_t port_id, const uint64_t *ids,
                         uint64_t *values, unsigned int size)
 {
+       unsigned int no_basic_stat_requested = 1;
+       unsigned int no_ext_stat_requested = 1;
        unsigned int num_xstats_filled;
+       unsigned int basic_count;
        uint16_t expected_entries;
        struct rte_eth_dev *dev;
        unsigned int i;
+       int ret;
 
        RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
        expected_entries = get_xstats_count(port_id);
        struct rte_eth_xstat xstats[expected_entries];
        dev = &rte_eth_devices[port_id];
+       basic_count = get_xstats_basic_count(dev);
 
        /* Return max number of stats if no ids given */
        if (!ids) {
@@ -1792,13 +1873,46 @@ rte_eth_xstats_get_by_id(uint16_t port_id, const uint64_t *ids,
        if (ids && !values)
                return -EINVAL;
 
-       if (dev->dev_ops->xstats_get_by_id != NULL)
-               return (*dev->dev_ops->xstats_get_by_id)(dev, ids, values,
-                       size);
+       if (ids && dev->dev_ops->xstats_get_by_id != NULL && size) {
+               unsigned int basic_count = get_xstats_basic_count(dev);
+               uint64_t ids_copy[size];
+
+               for (i = 0; i < size; i++) {
+                       if (ids[i] < basic_count) {
+                               no_basic_stat_requested = 0;
+                               break;
+                       }
+
+                       /*
+                        * Convert ids to xstats ids that PMD knows.
+                        * ids known by user are basic + extended stats.
+                        */
+                       ids_copy[i] = ids[i] - basic_count;
+               }
+
+               if (no_basic_stat_requested)
+                       return (*dev->dev_ops->xstats_get_by_id)(dev, ids_copy,
+                                       values, size);
+       }
+
+       if (ids) {
+               for (i = 0; i < size; i++) {
+                       if (ids[i] > basic_count) {
+                               no_ext_stat_requested = 0;
+                               break;
+                       }
+               }
+       }
 
        /* Fill the xstats structure */
-       num_xstats_filled = rte_eth_xstats_get(port_id, xstats,
-               expected_entries);
+       if (ids && no_ext_stat_requested)
+               ret = rte_eth_basic_stats_get(port_id, xstats);
+       else
+               ret = rte_eth_xstats_get(port_id, xstats, expected_entries);
+
+       if (ret < 0)
+               return ret;
+       num_xstats_filled = (unsigned int)ret;
 
        /* Return all stats */
        if (!ids) {
@@ -1822,11 +1936,9 @@ int
 rte_eth_xstats_get(uint16_t port_id, struct rte_eth_xstat *xstats,
        unsigned int n)
 {
-       struct rte_eth_stats eth_stats;
        struct rte_eth_dev *dev;
-       unsigned int count = 0, i, q;
+       unsigned int count = 0, i;
        signed int xcount = 0;
-       uint64_t val, *stats_ptr;
        uint16_t nb_rxqs, nb_txqs;
 
        RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
@@ -1857,38 +1969,7 @@ rte_eth_xstats_get(uint16_t port_id, struct rte_eth_xstat *xstats,
                return count + xcount;
 
        /* now fill the xstats structure */
-       count = 0;
-       rte_eth_stats_get(port_id, &eth_stats);
-
-       /* global stats */
-       for (i = 0; i < RTE_NB_STATS; i++) {
-               stats_ptr = RTE_PTR_ADD(&eth_stats,
-                                       rte_stats_strings[i].offset);
-               val = *stats_ptr;
-               xstats[count++].value = val;
-       }
-
-       /* per-rxq stats */
-       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 +
-                                       q * sizeof(uint64_t));
-                       val = *stats_ptr;
-                       xstats[count++].value = val;
-               }
-       }
-
-       /* per-txq stats */
-       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 +
-                                       q * sizeof(uint64_t));
-                       val = *stats_ptr;
-                       xstats[count++].value = val;
-               }
-       }
+       count = rte_eth_basic_stats_get(port_id, xstats);
 
        for (i = 0; i < count; i++)
                xstats[i].id = i;
@@ -2133,10 +2214,14 @@ rte_eth_dev_set_vlan_offload(uint16_t port_id, int offload_mask)
        int ret = 0;
        int mask = 0;
        int cur, org = 0;
+       uint64_t orig_offloads;
 
        RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
        dev = &rte_eth_devices[port_id];
 
+       /* save original values in case of failure */
+       orig_offloads = dev->data->dev_conf.rxmode.offloads;
+
        /*check which option changed by application*/
        cur = !!(offload_mask & ETH_VLAN_STRIP_OFFLOAD);
        org = !!(dev->data->dev_conf.rxmode.offloads &
@@ -2189,7 +2274,13 @@ rte_eth_dev_set_vlan_offload(uint16_t port_id, int offload_mask)
         */
        rte_eth_convert_rx_offloads(dev->data->dev_conf.rxmode.offloads,
                                    &dev->data->dev_conf.rxmode);
-       (*dev->dev_ops->vlan_offload_set)(dev, mask);
+       ret = (*dev->dev_ops->vlan_offload_set)(dev, mask);
+       if (ret) {
+               /* hit an error restore  original values */
+               dev->data->dev_conf.rxmode.offloads = orig_offloads;
+               rte_eth_convert_rx_offloads(dev->data->dev_conf.rxmode.offloads,
+                                           &dev->data->dev_conf.rxmode);
+       }
 
        return ret;
 }
@@ -2632,7 +2723,7 @@ rte_eth_dev_uc_hash_table_set(uint16_t port_id, struct ether_addr *addr,
 
        index = get_hash_mac_addr_index(port_id, addr);
        /* Check if it's already there, and do nothing */
-       if ((index >= 0) && (on))
+       if ((index >= 0) && on)
                return 0;
 
        if (index < 0) {