xref: /dpdk/lib/ethdev/rte_ethdev.c (revision dd4e429c)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2010-2017 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #include <ctype.h>
699a2dd95SBruce Richardson #include <errno.h>
799a2dd95SBruce Richardson #include <inttypes.h>
899a2dd95SBruce Richardson #include <stdbool.h>
999a2dd95SBruce Richardson #include <stdint.h>
1099a2dd95SBruce Richardson #include <stdlib.h>
1199a2dd95SBruce Richardson #include <string.h>
1299a2dd95SBruce Richardson #include <sys/queue.h>
1399a2dd95SBruce Richardson 
1499a2dd95SBruce Richardson #include <rte_byteorder.h>
1599a2dd95SBruce Richardson #include <rte_log.h>
1699a2dd95SBruce Richardson #include <rte_debug.h>
1799a2dd95SBruce Richardson #include <rte_interrupts.h>
1899a2dd95SBruce Richardson #include <rte_memory.h>
1999a2dd95SBruce Richardson #include <rte_memcpy.h>
2099a2dd95SBruce Richardson #include <rte_memzone.h>
2199a2dd95SBruce Richardson #include <rte_launch.h>
2299a2dd95SBruce Richardson #include <rte_eal.h>
2399a2dd95SBruce Richardson #include <rte_per_lcore.h>
2499a2dd95SBruce Richardson #include <rte_lcore.h>
2599a2dd95SBruce Richardson #include <rte_branch_prediction.h>
2699a2dd95SBruce Richardson #include <rte_common.h>
2799a2dd95SBruce Richardson #include <rte_mempool.h>
2899a2dd95SBruce Richardson #include <rte_malloc.h>
2999a2dd95SBruce Richardson #include <rte_mbuf.h>
3099a2dd95SBruce Richardson #include <rte_errno.h>
3199a2dd95SBruce Richardson #include <rte_spinlock.h>
3299a2dd95SBruce Richardson #include <rte_string_fns.h>
3399a2dd95SBruce Richardson #include <rte_kvargs.h>
3499a2dd95SBruce Richardson #include <rte_class.h>
3599a2dd95SBruce Richardson #include <rte_ether.h>
3699a2dd95SBruce Richardson #include <rte_telemetry.h>
3799a2dd95SBruce Richardson 
3899a2dd95SBruce Richardson #include "rte_ethdev_trace.h"
3999a2dd95SBruce Richardson #include "rte_ethdev.h"
4099a2dd95SBruce Richardson #include "ethdev_driver.h"
4199a2dd95SBruce Richardson #include "ethdev_profile.h"
4299a2dd95SBruce Richardson #include "ethdev_private.h"
4399a2dd95SBruce Richardson 
4499a2dd95SBruce Richardson static const char *MZ_RTE_ETH_DEV_DATA = "rte_eth_dev_data";
4599a2dd95SBruce Richardson struct rte_eth_dev rte_eth_devices[RTE_MAX_ETHPORTS];
4699a2dd95SBruce Richardson 
47c87d435aSKonstantin Ananyev /* public fast-path API */
48c87d435aSKonstantin Ananyev struct rte_eth_fp_ops rte_eth_fp_ops[RTE_MAX_ETHPORTS];
49c87d435aSKonstantin Ananyev 
5099a2dd95SBruce Richardson /* spinlock for eth device callbacks */
5199a2dd95SBruce Richardson static rte_spinlock_t eth_dev_cb_lock = RTE_SPINLOCK_INITIALIZER;
5299a2dd95SBruce Richardson 
5399a2dd95SBruce Richardson /* spinlock for add/remove rx callbacks */
5499a2dd95SBruce Richardson static rte_spinlock_t eth_dev_rx_cb_lock = RTE_SPINLOCK_INITIALIZER;
5599a2dd95SBruce Richardson 
5699a2dd95SBruce Richardson /* spinlock for add/remove tx callbacks */
5799a2dd95SBruce Richardson static rte_spinlock_t eth_dev_tx_cb_lock = RTE_SPINLOCK_INITIALIZER;
5899a2dd95SBruce Richardson 
5999a2dd95SBruce Richardson /* spinlock for shared data allocation */
6099a2dd95SBruce Richardson static rte_spinlock_t eth_dev_shared_data_lock = RTE_SPINLOCK_INITIALIZER;
6199a2dd95SBruce Richardson 
6299a2dd95SBruce Richardson /* store statistics names and its offset in stats structure  */
6399a2dd95SBruce Richardson struct rte_eth_xstats_name_off {
6499a2dd95SBruce Richardson 	char name[RTE_ETH_XSTATS_NAME_SIZE];
6599a2dd95SBruce Richardson 	unsigned offset;
6699a2dd95SBruce Richardson };
6799a2dd95SBruce Richardson 
6899a2dd95SBruce Richardson /* Shared memory between primary and secondary processes. */
6999a2dd95SBruce Richardson static struct {
7099a2dd95SBruce Richardson 	uint64_t next_owner_id;
7199a2dd95SBruce Richardson 	rte_spinlock_t ownership_lock;
7299a2dd95SBruce Richardson 	struct rte_eth_dev_data data[RTE_MAX_ETHPORTS];
7399a2dd95SBruce Richardson } *eth_dev_shared_data;
7499a2dd95SBruce Richardson 
7599a2dd95SBruce Richardson static const struct rte_eth_xstats_name_off eth_dev_stats_strings[] = {
7699a2dd95SBruce Richardson 	{"rx_good_packets", offsetof(struct rte_eth_stats, ipackets)},
7799a2dd95SBruce Richardson 	{"tx_good_packets", offsetof(struct rte_eth_stats, opackets)},
7899a2dd95SBruce Richardson 	{"rx_good_bytes", offsetof(struct rte_eth_stats, ibytes)},
7999a2dd95SBruce Richardson 	{"tx_good_bytes", offsetof(struct rte_eth_stats, obytes)},
8099a2dd95SBruce Richardson 	{"rx_missed_errors", offsetof(struct rte_eth_stats, imissed)},
8199a2dd95SBruce Richardson 	{"rx_errors", offsetof(struct rte_eth_stats, ierrors)},
8299a2dd95SBruce Richardson 	{"tx_errors", offsetof(struct rte_eth_stats, oerrors)},
8399a2dd95SBruce Richardson 	{"rx_mbuf_allocation_errors", offsetof(struct rte_eth_stats,
8499a2dd95SBruce Richardson 		rx_nombuf)},
8599a2dd95SBruce Richardson };
8699a2dd95SBruce Richardson 
8799a2dd95SBruce Richardson #define RTE_NB_STATS RTE_DIM(eth_dev_stats_strings)
8899a2dd95SBruce Richardson 
8999a2dd95SBruce Richardson static const struct rte_eth_xstats_name_off eth_dev_rxq_stats_strings[] = {
9099a2dd95SBruce Richardson 	{"packets", offsetof(struct rte_eth_stats, q_ipackets)},
9199a2dd95SBruce Richardson 	{"bytes", offsetof(struct rte_eth_stats, q_ibytes)},
9299a2dd95SBruce Richardson 	{"errors", offsetof(struct rte_eth_stats, q_errors)},
9399a2dd95SBruce Richardson };
9499a2dd95SBruce Richardson 
9599a2dd95SBruce Richardson #define RTE_NB_RXQ_STATS RTE_DIM(eth_dev_rxq_stats_strings)
9699a2dd95SBruce Richardson 
9799a2dd95SBruce Richardson static const struct rte_eth_xstats_name_off eth_dev_txq_stats_strings[] = {
9899a2dd95SBruce Richardson 	{"packets", offsetof(struct rte_eth_stats, q_opackets)},
9999a2dd95SBruce Richardson 	{"bytes", offsetof(struct rte_eth_stats, q_obytes)},
10099a2dd95SBruce Richardson };
10199a2dd95SBruce Richardson #define RTE_NB_TXQ_STATS RTE_DIM(eth_dev_txq_stats_strings)
10299a2dd95SBruce Richardson 
10399a2dd95SBruce Richardson #define RTE_RX_OFFLOAD_BIT2STR(_name)	\
10499a2dd95SBruce Richardson 	{ DEV_RX_OFFLOAD_##_name, #_name }
10599a2dd95SBruce Richardson 
10699a2dd95SBruce Richardson #define RTE_ETH_RX_OFFLOAD_BIT2STR(_name)	\
10799a2dd95SBruce Richardson 	{ RTE_ETH_RX_OFFLOAD_##_name, #_name }
10899a2dd95SBruce Richardson 
10999a2dd95SBruce Richardson static const struct {
11099a2dd95SBruce Richardson 	uint64_t offload;
11199a2dd95SBruce Richardson 	const char *name;
11299a2dd95SBruce Richardson } eth_dev_rx_offload_names[] = {
11399a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(VLAN_STRIP),
11499a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(IPV4_CKSUM),
11599a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(UDP_CKSUM),
11699a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(TCP_CKSUM),
11799a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(TCP_LRO),
11899a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(QINQ_STRIP),
11999a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(OUTER_IPV4_CKSUM),
12099a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(MACSEC_STRIP),
12199a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(HEADER_SPLIT),
12299a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(VLAN_FILTER),
12399a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(VLAN_EXTEND),
12499a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(JUMBO_FRAME),
12599a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(SCATTER),
12699a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(TIMESTAMP),
12799a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(SECURITY),
12899a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(KEEP_CRC),
12999a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(SCTP_CKSUM),
13099a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(OUTER_UDP_CKSUM),
13199a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(RSS_HASH),
13299a2dd95SBruce Richardson 	RTE_ETH_RX_OFFLOAD_BIT2STR(BUFFER_SPLIT),
13399a2dd95SBruce Richardson };
13499a2dd95SBruce Richardson 
13599a2dd95SBruce Richardson #undef RTE_RX_OFFLOAD_BIT2STR
13699a2dd95SBruce Richardson #undef RTE_ETH_RX_OFFLOAD_BIT2STR
13799a2dd95SBruce Richardson 
13899a2dd95SBruce Richardson #define RTE_TX_OFFLOAD_BIT2STR(_name)	\
13999a2dd95SBruce Richardson 	{ DEV_TX_OFFLOAD_##_name, #_name }
14099a2dd95SBruce Richardson 
14199a2dd95SBruce Richardson static const struct {
14299a2dd95SBruce Richardson 	uint64_t offload;
14399a2dd95SBruce Richardson 	const char *name;
14499a2dd95SBruce Richardson } eth_dev_tx_offload_names[] = {
14599a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(VLAN_INSERT),
14699a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(IPV4_CKSUM),
14799a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(UDP_CKSUM),
14899a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(TCP_CKSUM),
14999a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(SCTP_CKSUM),
15099a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(TCP_TSO),
15199a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(UDP_TSO),
15299a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(OUTER_IPV4_CKSUM),
15399a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(QINQ_INSERT),
15499a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(VXLAN_TNL_TSO),
15599a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(GRE_TNL_TSO),
15699a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(IPIP_TNL_TSO),
15799a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(GENEVE_TNL_TSO),
15899a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(MACSEC_INSERT),
15999a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(MT_LOCKFREE),
16099a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(MULTI_SEGS),
16199a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(MBUF_FAST_FREE),
16299a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(SECURITY),
16399a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(UDP_TNL_TSO),
16499a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(IP_TNL_TSO),
16599a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(OUTER_UDP_CKSUM),
16699a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(SEND_ON_TIMESTAMP),
16799a2dd95SBruce Richardson };
16899a2dd95SBruce Richardson 
16999a2dd95SBruce Richardson #undef RTE_TX_OFFLOAD_BIT2STR
17099a2dd95SBruce Richardson 
17199a2dd95SBruce Richardson /**
17299a2dd95SBruce Richardson  * The user application callback description.
17399a2dd95SBruce Richardson  *
17499a2dd95SBruce Richardson  * It contains callback address to be registered by user application,
17599a2dd95SBruce Richardson  * the pointer to the parameters for callback, and the event type.
17699a2dd95SBruce Richardson  */
17799a2dd95SBruce Richardson struct rte_eth_dev_callback {
17899a2dd95SBruce Richardson 	TAILQ_ENTRY(rte_eth_dev_callback) next; /**< Callbacks list */
17999a2dd95SBruce Richardson 	rte_eth_dev_cb_fn cb_fn;                /**< Callback address */
18099a2dd95SBruce Richardson 	void *cb_arg;                           /**< Parameter for callback */
18199a2dd95SBruce Richardson 	void *ret_param;                        /**< Return parameter */
18299a2dd95SBruce Richardson 	enum rte_eth_event_type event;          /**< Interrupt event type */
18399a2dd95SBruce Richardson 	uint32_t active;                        /**< Callback is executing */
18499a2dd95SBruce Richardson };
18599a2dd95SBruce Richardson 
18699a2dd95SBruce Richardson enum {
18799a2dd95SBruce Richardson 	STAT_QMAP_TX = 0,
18899a2dd95SBruce Richardson 	STAT_QMAP_RX
18999a2dd95SBruce Richardson };
19099a2dd95SBruce Richardson 
19199a2dd95SBruce Richardson int
19299a2dd95SBruce Richardson rte_eth_iterator_init(struct rte_dev_iterator *iter, const char *devargs_str)
19399a2dd95SBruce Richardson {
19499a2dd95SBruce Richardson 	int ret;
19599a2dd95SBruce Richardson 	struct rte_devargs devargs;
19699a2dd95SBruce Richardson 	const char *bus_param_key;
19799a2dd95SBruce Richardson 	char *bus_str = NULL;
19899a2dd95SBruce Richardson 	char *cls_str = NULL;
19999a2dd95SBruce Richardson 	int str_size;
20099a2dd95SBruce Richardson 
20153ef1b34SMin Hu (Connor) 	if (iter == NULL) {
20253ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot initialize NULL iterator\n");
20353ef1b34SMin Hu (Connor) 		return -EINVAL;
20453ef1b34SMin Hu (Connor) 	}
20553ef1b34SMin Hu (Connor) 
20653ef1b34SMin Hu (Connor) 	if (devargs_str == NULL) {
20753ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
20853ef1b34SMin Hu (Connor) 			"Cannot initialize iterator from NULL device description string\n");
20953ef1b34SMin Hu (Connor) 		return -EINVAL;
21053ef1b34SMin Hu (Connor) 	}
21153ef1b34SMin Hu (Connor) 
21299a2dd95SBruce Richardson 	memset(iter, 0, sizeof(*iter));
21399a2dd95SBruce Richardson 	memset(&devargs, 0, sizeof(devargs));
21499a2dd95SBruce Richardson 
21599a2dd95SBruce Richardson 	/*
21699a2dd95SBruce Richardson 	 * The devargs string may use various syntaxes:
21799a2dd95SBruce Richardson 	 *   - 0000:08:00.0,representor=[1-3]
21899a2dd95SBruce Richardson 	 *   - pci:0000:06:00.0,representor=[0,5]
21999a2dd95SBruce Richardson 	 *   - class=eth,mac=00:11:22:33:44:55
22099a2dd95SBruce Richardson 	 *   - bus=X,paramX=x/class=Y,paramY=y/driver=Z,paramZ=z
22199a2dd95SBruce Richardson 	 */
22299a2dd95SBruce Richardson 
22399a2dd95SBruce Richardson 	/*
22499a2dd95SBruce Richardson 	 * Handle pure class filter (i.e. without any bus-level argument),
22599a2dd95SBruce Richardson 	 * from future new syntax.
22699a2dd95SBruce Richardson 	 * rte_devargs_parse() is not yet supporting the new syntax,
22799a2dd95SBruce Richardson 	 * that's why this simple case is temporarily parsed here.
22899a2dd95SBruce Richardson 	 */
22999a2dd95SBruce Richardson #define iter_anybus_str "class=eth,"
23099a2dd95SBruce Richardson 	if (strncmp(devargs_str, iter_anybus_str,
23199a2dd95SBruce Richardson 			strlen(iter_anybus_str)) == 0) {
23299a2dd95SBruce Richardson 		iter->cls_str = devargs_str + strlen(iter_anybus_str);
23399a2dd95SBruce Richardson 		goto end;
23499a2dd95SBruce Richardson 	}
23599a2dd95SBruce Richardson 
23699a2dd95SBruce Richardson 	/* Split bus, device and parameters. */
23799a2dd95SBruce Richardson 	ret = rte_devargs_parse(&devargs, devargs_str);
23899a2dd95SBruce Richardson 	if (ret != 0)
23999a2dd95SBruce Richardson 		goto error;
24099a2dd95SBruce Richardson 
24199a2dd95SBruce Richardson 	/*
24299a2dd95SBruce Richardson 	 * Assume parameters of old syntax can match only at ethdev level.
24399a2dd95SBruce Richardson 	 * Extra parameters will be ignored, thanks to "+" prefix.
24499a2dd95SBruce Richardson 	 */
24599a2dd95SBruce Richardson 	str_size = strlen(devargs.args) + 2;
24699a2dd95SBruce Richardson 	cls_str = malloc(str_size);
24799a2dd95SBruce Richardson 	if (cls_str == NULL) {
24899a2dd95SBruce Richardson 		ret = -ENOMEM;
24999a2dd95SBruce Richardson 		goto error;
25099a2dd95SBruce Richardson 	}
25199a2dd95SBruce Richardson 	ret = snprintf(cls_str, str_size, "+%s", devargs.args);
25299a2dd95SBruce Richardson 	if (ret != str_size - 1) {
25399a2dd95SBruce Richardson 		ret = -EINVAL;
25499a2dd95SBruce Richardson 		goto error;
25599a2dd95SBruce Richardson 	}
25699a2dd95SBruce Richardson 	iter->cls_str = cls_str;
25799a2dd95SBruce Richardson 
25899a2dd95SBruce Richardson 	iter->bus = devargs.bus;
25999a2dd95SBruce Richardson 	if (iter->bus->dev_iterate == NULL) {
26099a2dd95SBruce Richardson 		ret = -ENOTSUP;
26199a2dd95SBruce Richardson 		goto error;
26299a2dd95SBruce Richardson 	}
26399a2dd95SBruce Richardson 
26499a2dd95SBruce Richardson 	/* Convert bus args to new syntax for use with new API dev_iterate. */
265a956adb2SHemant Agrawal 	if ((strcmp(iter->bus->name, "vdev") == 0) ||
266a956adb2SHemant Agrawal 		(strcmp(iter->bus->name, "fslmc") == 0) ||
267a956adb2SHemant Agrawal 		(strcmp(iter->bus->name, "dpaa_bus") == 0)) {
26899a2dd95SBruce Richardson 		bus_param_key = "name";
26999a2dd95SBruce Richardson 	} else if (strcmp(iter->bus->name, "pci") == 0) {
27099a2dd95SBruce Richardson 		bus_param_key = "addr";
27199a2dd95SBruce Richardson 	} else {
27299a2dd95SBruce Richardson 		ret = -ENOTSUP;
27399a2dd95SBruce Richardson 		goto error;
27499a2dd95SBruce Richardson 	}
27599a2dd95SBruce Richardson 	str_size = strlen(bus_param_key) + strlen(devargs.name) + 2;
27699a2dd95SBruce Richardson 	bus_str = malloc(str_size);
27799a2dd95SBruce Richardson 	if (bus_str == NULL) {
27899a2dd95SBruce Richardson 		ret = -ENOMEM;
27999a2dd95SBruce Richardson 		goto error;
28099a2dd95SBruce Richardson 	}
28199a2dd95SBruce Richardson 	ret = snprintf(bus_str, str_size, "%s=%s",
28299a2dd95SBruce Richardson 			bus_param_key, devargs.name);
28399a2dd95SBruce Richardson 	if (ret != str_size - 1) {
28499a2dd95SBruce Richardson 		ret = -EINVAL;
28599a2dd95SBruce Richardson 		goto error;
28699a2dd95SBruce Richardson 	}
28799a2dd95SBruce Richardson 	iter->bus_str = bus_str;
28899a2dd95SBruce Richardson 
28999a2dd95SBruce Richardson end:
29099a2dd95SBruce Richardson 	iter->cls = rte_class_find_by_name("eth");
29199a2dd95SBruce Richardson 	rte_devargs_reset(&devargs);
29299a2dd95SBruce Richardson 	return 0;
29399a2dd95SBruce Richardson 
29499a2dd95SBruce Richardson error:
29599a2dd95SBruce Richardson 	if (ret == -ENOTSUP)
29699a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Bus %s does not support iterating.\n",
29799a2dd95SBruce Richardson 				iter->bus->name);
29899a2dd95SBruce Richardson 	rte_devargs_reset(&devargs);
29999a2dd95SBruce Richardson 	free(bus_str);
30099a2dd95SBruce Richardson 	free(cls_str);
30199a2dd95SBruce Richardson 	return ret;
30299a2dd95SBruce Richardson }
30399a2dd95SBruce Richardson 
30499a2dd95SBruce Richardson uint16_t
30599a2dd95SBruce Richardson rte_eth_iterator_next(struct rte_dev_iterator *iter)
30699a2dd95SBruce Richardson {
30753ef1b34SMin Hu (Connor) 	if (iter == NULL) {
30853ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
30953ef1b34SMin Hu (Connor) 			"Cannot get next device from NULL iterator\n");
31053ef1b34SMin Hu (Connor) 		return RTE_MAX_ETHPORTS;
31153ef1b34SMin Hu (Connor) 	}
31253ef1b34SMin Hu (Connor) 
31399a2dd95SBruce Richardson 	if (iter->cls == NULL) /* invalid ethdev iterator */
31499a2dd95SBruce Richardson 		return RTE_MAX_ETHPORTS;
31599a2dd95SBruce Richardson 
31699a2dd95SBruce Richardson 	do { /* loop to try all matching rte_device */
31799a2dd95SBruce Richardson 		/* If not pure ethdev filter and */
31899a2dd95SBruce Richardson 		if (iter->bus != NULL &&
31999a2dd95SBruce Richardson 				/* not in middle of rte_eth_dev iteration, */
32099a2dd95SBruce Richardson 				iter->class_device == NULL) {
32199a2dd95SBruce Richardson 			/* get next rte_device to try. */
32299a2dd95SBruce Richardson 			iter->device = iter->bus->dev_iterate(
32399a2dd95SBruce Richardson 					iter->device, iter->bus_str, iter);
32499a2dd95SBruce Richardson 			if (iter->device == NULL)
32599a2dd95SBruce Richardson 				break; /* no more rte_device candidate */
32699a2dd95SBruce Richardson 		}
32799a2dd95SBruce Richardson 		/* A device is matching bus part, need to check ethdev part. */
32899a2dd95SBruce Richardson 		iter->class_device = iter->cls->dev_iterate(
32999a2dd95SBruce Richardson 				iter->class_device, iter->cls_str, iter);
33099a2dd95SBruce Richardson 		if (iter->class_device != NULL)
33199a2dd95SBruce Richardson 			return eth_dev_to_id(iter->class_device); /* match */
33299a2dd95SBruce Richardson 	} while (iter->bus != NULL); /* need to try next rte_device */
33399a2dd95SBruce Richardson 
33499a2dd95SBruce Richardson 	/* No more ethdev port to iterate. */
33599a2dd95SBruce Richardson 	rte_eth_iterator_cleanup(iter);
33699a2dd95SBruce Richardson 	return RTE_MAX_ETHPORTS;
33799a2dd95SBruce Richardson }
33899a2dd95SBruce Richardson 
33999a2dd95SBruce Richardson void
34099a2dd95SBruce Richardson rte_eth_iterator_cleanup(struct rte_dev_iterator *iter)
34199a2dd95SBruce Richardson {
34253ef1b34SMin Hu (Connor) 	if (iter == NULL) {
34353ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot do clean up from NULL iterator\n");
34453ef1b34SMin Hu (Connor) 		return;
34553ef1b34SMin Hu (Connor) 	}
34653ef1b34SMin Hu (Connor) 
34799a2dd95SBruce Richardson 	if (iter->bus_str == NULL)
34899a2dd95SBruce Richardson 		return; /* nothing to free in pure class filter */
34999a2dd95SBruce Richardson 	free(RTE_CAST_FIELD(iter, bus_str, char *)); /* workaround const */
35099a2dd95SBruce Richardson 	free(RTE_CAST_FIELD(iter, cls_str, char *)); /* workaround const */
35199a2dd95SBruce Richardson 	memset(iter, 0, sizeof(*iter));
35299a2dd95SBruce Richardson }
35399a2dd95SBruce Richardson 
35499a2dd95SBruce Richardson uint16_t
35599a2dd95SBruce Richardson rte_eth_find_next(uint16_t port_id)
35699a2dd95SBruce Richardson {
35799a2dd95SBruce Richardson 	while (port_id < RTE_MAX_ETHPORTS &&
35899a2dd95SBruce Richardson 			rte_eth_devices[port_id].state == RTE_ETH_DEV_UNUSED)
35999a2dd95SBruce Richardson 		port_id++;
36099a2dd95SBruce Richardson 
36199a2dd95SBruce Richardson 	if (port_id >= RTE_MAX_ETHPORTS)
36299a2dd95SBruce Richardson 		return RTE_MAX_ETHPORTS;
36399a2dd95SBruce Richardson 
36499a2dd95SBruce Richardson 	return port_id;
36599a2dd95SBruce Richardson }
36699a2dd95SBruce Richardson 
36799a2dd95SBruce Richardson /*
36899a2dd95SBruce Richardson  * Macro to iterate over all valid ports for internal usage.
36999a2dd95SBruce Richardson  * Note: RTE_ETH_FOREACH_DEV is different because filtering owned ports.
37099a2dd95SBruce Richardson  */
37199a2dd95SBruce Richardson #define RTE_ETH_FOREACH_VALID_DEV(port_id) \
37299a2dd95SBruce Richardson 	for (port_id = rte_eth_find_next(0); \
37399a2dd95SBruce Richardson 	     port_id < RTE_MAX_ETHPORTS; \
37499a2dd95SBruce Richardson 	     port_id = rte_eth_find_next(port_id + 1))
37599a2dd95SBruce Richardson 
37699a2dd95SBruce Richardson uint16_t
37799a2dd95SBruce Richardson rte_eth_find_next_of(uint16_t port_id, const struct rte_device *parent)
37899a2dd95SBruce Richardson {
37999a2dd95SBruce Richardson 	port_id = rte_eth_find_next(port_id);
38099a2dd95SBruce Richardson 	while (port_id < RTE_MAX_ETHPORTS &&
38199a2dd95SBruce Richardson 			rte_eth_devices[port_id].device != parent)
38299a2dd95SBruce Richardson 		port_id = rte_eth_find_next(port_id + 1);
38399a2dd95SBruce Richardson 
38499a2dd95SBruce Richardson 	return port_id;
38599a2dd95SBruce Richardson }
38699a2dd95SBruce Richardson 
38799a2dd95SBruce Richardson uint16_t
38899a2dd95SBruce Richardson rte_eth_find_next_sibling(uint16_t port_id, uint16_t ref_port_id)
38999a2dd95SBruce Richardson {
39099a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(ref_port_id, RTE_MAX_ETHPORTS);
39199a2dd95SBruce Richardson 	return rte_eth_find_next_of(port_id,
39299a2dd95SBruce Richardson 			rte_eth_devices[ref_port_id].device);
39399a2dd95SBruce Richardson }
39499a2dd95SBruce Richardson 
39599a2dd95SBruce Richardson static void
39699a2dd95SBruce Richardson eth_dev_shared_data_prepare(void)
39799a2dd95SBruce Richardson {
39899a2dd95SBruce Richardson 	const unsigned flags = 0;
39999a2dd95SBruce Richardson 	const struct rte_memzone *mz;
40099a2dd95SBruce Richardson 
40199a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_shared_data_lock);
40299a2dd95SBruce Richardson 
40399a2dd95SBruce Richardson 	if (eth_dev_shared_data == NULL) {
40499a2dd95SBruce Richardson 		if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
40599a2dd95SBruce Richardson 			/* Allocate port data and ownership shared memory. */
40699a2dd95SBruce Richardson 			mz = rte_memzone_reserve(MZ_RTE_ETH_DEV_DATA,
40799a2dd95SBruce Richardson 					sizeof(*eth_dev_shared_data),
40899a2dd95SBruce Richardson 					rte_socket_id(), flags);
40999a2dd95SBruce Richardson 		} else
41099a2dd95SBruce Richardson 			mz = rte_memzone_lookup(MZ_RTE_ETH_DEV_DATA);
41199a2dd95SBruce Richardson 		if (mz == NULL)
41299a2dd95SBruce Richardson 			rte_panic("Cannot allocate ethdev shared data\n");
41399a2dd95SBruce Richardson 
41499a2dd95SBruce Richardson 		eth_dev_shared_data = mz->addr;
41599a2dd95SBruce Richardson 		if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
41699a2dd95SBruce Richardson 			eth_dev_shared_data->next_owner_id =
41799a2dd95SBruce Richardson 					RTE_ETH_DEV_NO_OWNER + 1;
41899a2dd95SBruce Richardson 			rte_spinlock_init(&eth_dev_shared_data->ownership_lock);
41999a2dd95SBruce Richardson 			memset(eth_dev_shared_data->data, 0,
42099a2dd95SBruce Richardson 			       sizeof(eth_dev_shared_data->data));
42199a2dd95SBruce Richardson 		}
42299a2dd95SBruce Richardson 	}
42399a2dd95SBruce Richardson 
42499a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_shared_data_lock);
42599a2dd95SBruce Richardson }
42699a2dd95SBruce Richardson 
42799a2dd95SBruce Richardson static bool
42899a2dd95SBruce Richardson eth_dev_is_allocated(const struct rte_eth_dev *ethdev)
42999a2dd95SBruce Richardson {
43099a2dd95SBruce Richardson 	return ethdev->data->name[0] != '\0';
43199a2dd95SBruce Richardson }
43299a2dd95SBruce Richardson 
43399a2dd95SBruce Richardson static struct rte_eth_dev *
43499a2dd95SBruce Richardson eth_dev_allocated(const char *name)
43599a2dd95SBruce Richardson {
43699a2dd95SBruce Richardson 	uint16_t i;
43799a2dd95SBruce Richardson 
43899a2dd95SBruce Richardson 	RTE_BUILD_BUG_ON(RTE_MAX_ETHPORTS >= UINT16_MAX);
43999a2dd95SBruce Richardson 
44099a2dd95SBruce Richardson 	for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
44199a2dd95SBruce Richardson 		if (rte_eth_devices[i].data != NULL &&
44299a2dd95SBruce Richardson 		    strcmp(rte_eth_devices[i].data->name, name) == 0)
44399a2dd95SBruce Richardson 			return &rte_eth_devices[i];
44499a2dd95SBruce Richardson 	}
44599a2dd95SBruce Richardson 	return NULL;
44699a2dd95SBruce Richardson }
44799a2dd95SBruce Richardson 
44899a2dd95SBruce Richardson struct rte_eth_dev *
44999a2dd95SBruce Richardson rte_eth_dev_allocated(const char *name)
45099a2dd95SBruce Richardson {
45199a2dd95SBruce Richardson 	struct rte_eth_dev *ethdev;
45299a2dd95SBruce Richardson 
45399a2dd95SBruce Richardson 	eth_dev_shared_data_prepare();
45499a2dd95SBruce Richardson 
45599a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_shared_data->ownership_lock);
45699a2dd95SBruce Richardson 
45799a2dd95SBruce Richardson 	ethdev = eth_dev_allocated(name);
45899a2dd95SBruce Richardson 
45999a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_shared_data->ownership_lock);
46099a2dd95SBruce Richardson 
46199a2dd95SBruce Richardson 	return ethdev;
46299a2dd95SBruce Richardson }
46399a2dd95SBruce Richardson 
46499a2dd95SBruce Richardson static uint16_t
46599a2dd95SBruce Richardson eth_dev_find_free_port(void)
46699a2dd95SBruce Richardson {
46799a2dd95SBruce Richardson 	uint16_t i;
46899a2dd95SBruce Richardson 
46999a2dd95SBruce Richardson 	for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
47099a2dd95SBruce Richardson 		/* Using shared name field to find a free port. */
47199a2dd95SBruce Richardson 		if (eth_dev_shared_data->data[i].name[0] == '\0') {
47299a2dd95SBruce Richardson 			RTE_ASSERT(rte_eth_devices[i].state ==
47399a2dd95SBruce Richardson 				   RTE_ETH_DEV_UNUSED);
47499a2dd95SBruce Richardson 			return i;
47599a2dd95SBruce Richardson 		}
47699a2dd95SBruce Richardson 	}
47799a2dd95SBruce Richardson 	return RTE_MAX_ETHPORTS;
47899a2dd95SBruce Richardson }
47999a2dd95SBruce Richardson 
48099a2dd95SBruce Richardson static struct rte_eth_dev *
48199a2dd95SBruce Richardson eth_dev_get(uint16_t port_id)
48299a2dd95SBruce Richardson {
48399a2dd95SBruce Richardson 	struct rte_eth_dev *eth_dev = &rte_eth_devices[port_id];
48499a2dd95SBruce Richardson 
48599a2dd95SBruce Richardson 	eth_dev->data = &eth_dev_shared_data->data[port_id];
48699a2dd95SBruce Richardson 
48799a2dd95SBruce Richardson 	return eth_dev;
48899a2dd95SBruce Richardson }
48999a2dd95SBruce Richardson 
49099a2dd95SBruce Richardson struct rte_eth_dev *
49199a2dd95SBruce Richardson rte_eth_dev_allocate(const char *name)
49299a2dd95SBruce Richardson {
49399a2dd95SBruce Richardson 	uint16_t port_id;
49499a2dd95SBruce Richardson 	struct rte_eth_dev *eth_dev = NULL;
49599a2dd95SBruce Richardson 	size_t name_len;
49699a2dd95SBruce Richardson 
49799a2dd95SBruce Richardson 	name_len = strnlen(name, RTE_ETH_NAME_MAX_LEN);
49899a2dd95SBruce Richardson 	if (name_len == 0) {
49999a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Zero length Ethernet device name\n");
50099a2dd95SBruce Richardson 		return NULL;
50199a2dd95SBruce Richardson 	}
50299a2dd95SBruce Richardson 
50399a2dd95SBruce Richardson 	if (name_len >= RTE_ETH_NAME_MAX_LEN) {
50499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Ethernet device name is too long\n");
50599a2dd95SBruce Richardson 		return NULL;
50699a2dd95SBruce Richardson 	}
50799a2dd95SBruce Richardson 
50899a2dd95SBruce Richardson 	eth_dev_shared_data_prepare();
50999a2dd95SBruce Richardson 
51099a2dd95SBruce Richardson 	/* Synchronize port creation between primary and secondary threads. */
51199a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_shared_data->ownership_lock);
51299a2dd95SBruce Richardson 
51399a2dd95SBruce Richardson 	if (eth_dev_allocated(name) != NULL) {
51499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
51599a2dd95SBruce Richardson 			"Ethernet device with name %s already allocated\n",
51699a2dd95SBruce Richardson 			name);
51799a2dd95SBruce Richardson 		goto unlock;
51899a2dd95SBruce Richardson 	}
51999a2dd95SBruce Richardson 
52099a2dd95SBruce Richardson 	port_id = eth_dev_find_free_port();
52199a2dd95SBruce Richardson 	if (port_id == RTE_MAX_ETHPORTS) {
52299a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
52399a2dd95SBruce Richardson 			"Reached maximum number of Ethernet ports\n");
52499a2dd95SBruce Richardson 		goto unlock;
52599a2dd95SBruce Richardson 	}
52699a2dd95SBruce Richardson 
52799a2dd95SBruce Richardson 	eth_dev = eth_dev_get(port_id);
52899a2dd95SBruce Richardson 	strlcpy(eth_dev->data->name, name, sizeof(eth_dev->data->name));
52999a2dd95SBruce Richardson 	eth_dev->data->port_id = port_id;
530ff4e52efSViacheslav Galaktionov 	eth_dev->data->backer_port_id = RTE_MAX_ETHPORTS;
53199a2dd95SBruce Richardson 	eth_dev->data->mtu = RTE_ETHER_MTU;
53299a2dd95SBruce Richardson 	pthread_mutex_init(&eth_dev->data->flow_ops_mutex, NULL);
53399a2dd95SBruce Richardson 
53499a2dd95SBruce Richardson unlock:
53599a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_shared_data->ownership_lock);
53699a2dd95SBruce Richardson 
53799a2dd95SBruce Richardson 	return eth_dev;
53899a2dd95SBruce Richardson }
53999a2dd95SBruce Richardson 
54099a2dd95SBruce Richardson /*
54199a2dd95SBruce Richardson  * Attach to a port already registered by the primary process, which
54299a2dd95SBruce Richardson  * makes sure that the same device would have the same port id both
54399a2dd95SBruce Richardson  * in the primary and secondary process.
54499a2dd95SBruce Richardson  */
54599a2dd95SBruce Richardson struct rte_eth_dev *
54699a2dd95SBruce Richardson rte_eth_dev_attach_secondary(const char *name)
54799a2dd95SBruce Richardson {
54899a2dd95SBruce Richardson 	uint16_t i;
54999a2dd95SBruce Richardson 	struct rte_eth_dev *eth_dev = NULL;
55099a2dd95SBruce Richardson 
55199a2dd95SBruce Richardson 	eth_dev_shared_data_prepare();
55299a2dd95SBruce Richardson 
55399a2dd95SBruce Richardson 	/* Synchronize port attachment to primary port creation and release. */
55499a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_shared_data->ownership_lock);
55599a2dd95SBruce Richardson 
55699a2dd95SBruce Richardson 	for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
55799a2dd95SBruce Richardson 		if (strcmp(eth_dev_shared_data->data[i].name, name) == 0)
55899a2dd95SBruce Richardson 			break;
55999a2dd95SBruce Richardson 	}
56099a2dd95SBruce Richardson 	if (i == RTE_MAX_ETHPORTS) {
56199a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
56299a2dd95SBruce Richardson 			"Device %s is not driven by the primary process\n",
56399a2dd95SBruce Richardson 			name);
56499a2dd95SBruce Richardson 	} else {
56599a2dd95SBruce Richardson 		eth_dev = eth_dev_get(i);
56699a2dd95SBruce Richardson 		RTE_ASSERT(eth_dev->data->port_id == i);
56799a2dd95SBruce Richardson 	}
56899a2dd95SBruce Richardson 
56999a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_shared_data->ownership_lock);
57099a2dd95SBruce Richardson 	return eth_dev;
57199a2dd95SBruce Richardson }
57299a2dd95SBruce Richardson 
57399a2dd95SBruce Richardson int
57499a2dd95SBruce Richardson rte_eth_dev_release_port(struct rte_eth_dev *eth_dev)
57599a2dd95SBruce Richardson {
57699a2dd95SBruce Richardson 	if (eth_dev == NULL)
57799a2dd95SBruce Richardson 		return -EINVAL;
57899a2dd95SBruce Richardson 
57999a2dd95SBruce Richardson 	eth_dev_shared_data_prepare();
58099a2dd95SBruce Richardson 
58199a2dd95SBruce Richardson 	if (eth_dev->state != RTE_ETH_DEV_UNUSED)
58299a2dd95SBruce Richardson 		rte_eth_dev_callback_process(eth_dev,
58399a2dd95SBruce Richardson 				RTE_ETH_EVENT_DESTROY, NULL);
58499a2dd95SBruce Richardson 
585c87d435aSKonstantin Ananyev 	eth_dev_fp_ops_reset(rte_eth_fp_ops + eth_dev->data->port_id);
586c87d435aSKonstantin Ananyev 
58799a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_shared_data->ownership_lock);
58899a2dd95SBruce Richardson 
58999a2dd95SBruce Richardson 	eth_dev->state = RTE_ETH_DEV_UNUSED;
59099a2dd95SBruce Richardson 	eth_dev->device = NULL;
59199a2dd95SBruce Richardson 	eth_dev->process_private = NULL;
59299a2dd95SBruce Richardson 	eth_dev->intr_handle = NULL;
59399a2dd95SBruce Richardson 	eth_dev->rx_pkt_burst = NULL;
59499a2dd95SBruce Richardson 	eth_dev->tx_pkt_burst = NULL;
59599a2dd95SBruce Richardson 	eth_dev->tx_pkt_prepare = NULL;
59699a2dd95SBruce Richardson 	eth_dev->rx_queue_count = NULL;
59799a2dd95SBruce Richardson 	eth_dev->rx_descriptor_status = NULL;
59899a2dd95SBruce Richardson 	eth_dev->tx_descriptor_status = NULL;
59999a2dd95SBruce Richardson 	eth_dev->dev_ops = NULL;
60099a2dd95SBruce Richardson 
60199a2dd95SBruce Richardson 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
60299a2dd95SBruce Richardson 		rte_free(eth_dev->data->rx_queues);
60399a2dd95SBruce Richardson 		rte_free(eth_dev->data->tx_queues);
60499a2dd95SBruce Richardson 		rte_free(eth_dev->data->mac_addrs);
60599a2dd95SBruce Richardson 		rte_free(eth_dev->data->hash_mac_addrs);
60699a2dd95SBruce Richardson 		rte_free(eth_dev->data->dev_private);
60799a2dd95SBruce Richardson 		pthread_mutex_destroy(&eth_dev->data->flow_ops_mutex);
60899a2dd95SBruce Richardson 		memset(eth_dev->data, 0, sizeof(struct rte_eth_dev_data));
60999a2dd95SBruce Richardson 	}
61099a2dd95SBruce Richardson 
61199a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_shared_data->ownership_lock);
61299a2dd95SBruce Richardson 
61399a2dd95SBruce Richardson 	return 0;
61499a2dd95SBruce Richardson }
61599a2dd95SBruce Richardson 
61699a2dd95SBruce Richardson int
61799a2dd95SBruce Richardson rte_eth_dev_is_valid_port(uint16_t port_id)
61899a2dd95SBruce Richardson {
61999a2dd95SBruce Richardson 	if (port_id >= RTE_MAX_ETHPORTS ||
62099a2dd95SBruce Richardson 	    (rte_eth_devices[port_id].state == RTE_ETH_DEV_UNUSED))
62199a2dd95SBruce Richardson 		return 0;
62299a2dd95SBruce Richardson 	else
62399a2dd95SBruce Richardson 		return 1;
62499a2dd95SBruce Richardson }
62599a2dd95SBruce Richardson 
62699a2dd95SBruce Richardson static int
62799a2dd95SBruce Richardson eth_is_valid_owner_id(uint64_t owner_id)
62899a2dd95SBruce Richardson {
62999a2dd95SBruce Richardson 	if (owner_id == RTE_ETH_DEV_NO_OWNER ||
63099a2dd95SBruce Richardson 	    eth_dev_shared_data->next_owner_id <= owner_id)
63199a2dd95SBruce Richardson 		return 0;
63299a2dd95SBruce Richardson 	return 1;
63399a2dd95SBruce Richardson }
63499a2dd95SBruce Richardson 
63599a2dd95SBruce Richardson uint64_t
63699a2dd95SBruce Richardson rte_eth_find_next_owned_by(uint16_t port_id, const uint64_t owner_id)
63799a2dd95SBruce Richardson {
63899a2dd95SBruce Richardson 	port_id = rte_eth_find_next(port_id);
63999a2dd95SBruce Richardson 	while (port_id < RTE_MAX_ETHPORTS &&
64099a2dd95SBruce Richardson 			rte_eth_devices[port_id].data->owner.id != owner_id)
64199a2dd95SBruce Richardson 		port_id = rte_eth_find_next(port_id + 1);
64299a2dd95SBruce Richardson 
64399a2dd95SBruce Richardson 	return port_id;
64499a2dd95SBruce Richardson }
64599a2dd95SBruce Richardson 
64699a2dd95SBruce Richardson int
64799a2dd95SBruce Richardson rte_eth_dev_owner_new(uint64_t *owner_id)
64899a2dd95SBruce Richardson {
64953ef1b34SMin Hu (Connor) 	if (owner_id == NULL) {
65053ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot get new owner ID to NULL\n");
65153ef1b34SMin Hu (Connor) 		return -EINVAL;
65253ef1b34SMin Hu (Connor) 	}
65353ef1b34SMin Hu (Connor) 
65499a2dd95SBruce Richardson 	eth_dev_shared_data_prepare();
65599a2dd95SBruce Richardson 
65699a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_shared_data->ownership_lock);
65799a2dd95SBruce Richardson 
65899a2dd95SBruce Richardson 	*owner_id = eth_dev_shared_data->next_owner_id++;
65999a2dd95SBruce Richardson 
66099a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_shared_data->ownership_lock);
66199a2dd95SBruce Richardson 	return 0;
66299a2dd95SBruce Richardson }
66399a2dd95SBruce Richardson 
66499a2dd95SBruce Richardson static int
66599a2dd95SBruce Richardson eth_dev_owner_set(const uint16_t port_id, const uint64_t old_owner_id,
66699a2dd95SBruce Richardson 		       const struct rte_eth_dev_owner *new_owner)
66799a2dd95SBruce Richardson {
66899a2dd95SBruce Richardson 	struct rte_eth_dev *ethdev = &rte_eth_devices[port_id];
66999a2dd95SBruce Richardson 	struct rte_eth_dev_owner *port_owner;
67099a2dd95SBruce Richardson 
67199a2dd95SBruce Richardson 	if (port_id >= RTE_MAX_ETHPORTS || !eth_dev_is_allocated(ethdev)) {
67299a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Port id %"PRIu16" is not allocated\n",
67399a2dd95SBruce Richardson 			port_id);
67499a2dd95SBruce Richardson 		return -ENODEV;
67599a2dd95SBruce Richardson 	}
67699a2dd95SBruce Richardson 
67753ef1b34SMin Hu (Connor) 	if (new_owner == NULL) {
67853ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
67953ef1b34SMin Hu (Connor) 			"Cannot set ethdev port %u owner from NULL owner\n",
68053ef1b34SMin Hu (Connor) 			port_id);
68153ef1b34SMin Hu (Connor) 		return -EINVAL;
68253ef1b34SMin Hu (Connor) 	}
68353ef1b34SMin Hu (Connor) 
68499a2dd95SBruce Richardson 	if (!eth_is_valid_owner_id(new_owner->id) &&
68599a2dd95SBruce Richardson 	    !eth_is_valid_owner_id(old_owner_id)) {
68699a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
68799a2dd95SBruce Richardson 			"Invalid owner old_id=%016"PRIx64" new_id=%016"PRIx64"\n",
68899a2dd95SBruce Richardson 		       old_owner_id, new_owner->id);
68999a2dd95SBruce Richardson 		return -EINVAL;
69099a2dd95SBruce Richardson 	}
69199a2dd95SBruce Richardson 
69299a2dd95SBruce Richardson 	port_owner = &rte_eth_devices[port_id].data->owner;
69399a2dd95SBruce Richardson 	if (port_owner->id != old_owner_id) {
69499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
69599a2dd95SBruce Richardson 			"Cannot set owner to port %u already owned by %s_%016"PRIX64"\n",
69699a2dd95SBruce Richardson 			port_id, port_owner->name, port_owner->id);
69799a2dd95SBruce Richardson 		return -EPERM;
69899a2dd95SBruce Richardson 	}
69999a2dd95SBruce Richardson 
70099a2dd95SBruce Richardson 	/* can not truncate (same structure) */
70199a2dd95SBruce Richardson 	strlcpy(port_owner->name, new_owner->name, RTE_ETH_MAX_OWNER_NAME_LEN);
70299a2dd95SBruce Richardson 
70399a2dd95SBruce Richardson 	port_owner->id = new_owner->id;
70499a2dd95SBruce Richardson 
70599a2dd95SBruce Richardson 	RTE_ETHDEV_LOG(DEBUG, "Port %u owner is %s_%016"PRIx64"\n",
70699a2dd95SBruce Richardson 		port_id, new_owner->name, new_owner->id);
70799a2dd95SBruce Richardson 
70899a2dd95SBruce Richardson 	return 0;
70999a2dd95SBruce Richardson }
71099a2dd95SBruce Richardson 
71199a2dd95SBruce Richardson int
71299a2dd95SBruce Richardson rte_eth_dev_owner_set(const uint16_t port_id,
71399a2dd95SBruce Richardson 		      const struct rte_eth_dev_owner *owner)
71499a2dd95SBruce Richardson {
71599a2dd95SBruce Richardson 	int ret;
71699a2dd95SBruce Richardson 
71799a2dd95SBruce Richardson 	eth_dev_shared_data_prepare();
71899a2dd95SBruce Richardson 
71999a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_shared_data->ownership_lock);
72099a2dd95SBruce Richardson 
72199a2dd95SBruce Richardson 	ret = eth_dev_owner_set(port_id, RTE_ETH_DEV_NO_OWNER, owner);
72299a2dd95SBruce Richardson 
72399a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_shared_data->ownership_lock);
72499a2dd95SBruce Richardson 	return ret;
72599a2dd95SBruce Richardson }
72699a2dd95SBruce Richardson 
72799a2dd95SBruce Richardson int
72899a2dd95SBruce Richardson rte_eth_dev_owner_unset(const uint16_t port_id, const uint64_t owner_id)
72999a2dd95SBruce Richardson {
73099a2dd95SBruce Richardson 	const struct rte_eth_dev_owner new_owner = (struct rte_eth_dev_owner)
73199a2dd95SBruce Richardson 			{.id = RTE_ETH_DEV_NO_OWNER, .name = ""};
73299a2dd95SBruce Richardson 	int ret;
73399a2dd95SBruce Richardson 
73499a2dd95SBruce Richardson 	eth_dev_shared_data_prepare();
73599a2dd95SBruce Richardson 
73699a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_shared_data->ownership_lock);
73799a2dd95SBruce Richardson 
73899a2dd95SBruce Richardson 	ret = eth_dev_owner_set(port_id, owner_id, &new_owner);
73999a2dd95SBruce Richardson 
74099a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_shared_data->ownership_lock);
74199a2dd95SBruce Richardson 	return ret;
74299a2dd95SBruce Richardson }
74399a2dd95SBruce Richardson 
74499a2dd95SBruce Richardson int
74599a2dd95SBruce Richardson rte_eth_dev_owner_delete(const uint64_t owner_id)
74699a2dd95SBruce Richardson {
74799a2dd95SBruce Richardson 	uint16_t port_id;
74899a2dd95SBruce Richardson 	int ret = 0;
74999a2dd95SBruce Richardson 
75099a2dd95SBruce Richardson 	eth_dev_shared_data_prepare();
75199a2dd95SBruce Richardson 
75299a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_shared_data->ownership_lock);
75399a2dd95SBruce Richardson 
75499a2dd95SBruce Richardson 	if (eth_is_valid_owner_id(owner_id)) {
75599a2dd95SBruce Richardson 		for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++)
75699a2dd95SBruce Richardson 			if (rte_eth_devices[port_id].data->owner.id == owner_id)
75799a2dd95SBruce Richardson 				memset(&rte_eth_devices[port_id].data->owner, 0,
75899a2dd95SBruce Richardson 				       sizeof(struct rte_eth_dev_owner));
75999a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(NOTICE,
76099a2dd95SBruce Richardson 			"All port owners owned by %016"PRIx64" identifier have removed\n",
76199a2dd95SBruce Richardson 			owner_id);
76299a2dd95SBruce Richardson 	} else {
76399a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
76499a2dd95SBruce Richardson 			       "Invalid owner id=%016"PRIx64"\n",
76599a2dd95SBruce Richardson 			       owner_id);
76699a2dd95SBruce Richardson 		ret = -EINVAL;
76799a2dd95SBruce Richardson 	}
76899a2dd95SBruce Richardson 
76999a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_shared_data->ownership_lock);
77099a2dd95SBruce Richardson 
77199a2dd95SBruce Richardson 	return ret;
77299a2dd95SBruce Richardson }
77399a2dd95SBruce Richardson 
77499a2dd95SBruce Richardson int
77599a2dd95SBruce Richardson rte_eth_dev_owner_get(const uint16_t port_id, struct rte_eth_dev_owner *owner)
77699a2dd95SBruce Richardson {
77753ef1b34SMin Hu (Connor) 	struct rte_eth_dev *ethdev;
77853ef1b34SMin Hu (Connor) 
77953ef1b34SMin Hu (Connor) 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
78053ef1b34SMin Hu (Connor) 	ethdev = &rte_eth_devices[port_id];
78153ef1b34SMin Hu (Connor) 
78253ef1b34SMin Hu (Connor) 	if (!eth_dev_is_allocated(ethdev)) {
78353ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Port id %"PRIu16" is not allocated\n",
78453ef1b34SMin Hu (Connor) 			port_id);
78553ef1b34SMin Hu (Connor) 		return -ENODEV;
78653ef1b34SMin Hu (Connor) 	}
78753ef1b34SMin Hu (Connor) 
78853ef1b34SMin Hu (Connor) 	if (owner == NULL) {
78953ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot get ethdev port %u owner to NULL\n",
79053ef1b34SMin Hu (Connor) 			port_id);
79153ef1b34SMin Hu (Connor) 		return -EINVAL;
79253ef1b34SMin Hu (Connor) 	}
79399a2dd95SBruce Richardson 
79499a2dd95SBruce Richardson 	eth_dev_shared_data_prepare();
79599a2dd95SBruce Richardson 
79699a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_shared_data->ownership_lock);
79799a2dd95SBruce Richardson 	rte_memcpy(owner, &ethdev->data->owner, sizeof(*owner));
79899a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_shared_data->ownership_lock);
79953ef1b34SMin Hu (Connor) 
80053ef1b34SMin Hu (Connor) 	return 0;
80199a2dd95SBruce Richardson }
80299a2dd95SBruce Richardson 
80399a2dd95SBruce Richardson int
80499a2dd95SBruce Richardson rte_eth_dev_socket_id(uint16_t port_id)
80599a2dd95SBruce Richardson {
80699a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -1);
80799a2dd95SBruce Richardson 	return rte_eth_devices[port_id].data->numa_node;
80899a2dd95SBruce Richardson }
80999a2dd95SBruce Richardson 
81099a2dd95SBruce Richardson void *
81199a2dd95SBruce Richardson rte_eth_dev_get_sec_ctx(uint16_t port_id)
81299a2dd95SBruce Richardson {
81399a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, NULL);
81499a2dd95SBruce Richardson 	return rte_eth_devices[port_id].security_ctx;
81599a2dd95SBruce Richardson }
81699a2dd95SBruce Richardson 
81799a2dd95SBruce Richardson uint16_t
81899a2dd95SBruce Richardson rte_eth_dev_count_avail(void)
81999a2dd95SBruce Richardson {
82099a2dd95SBruce Richardson 	uint16_t p;
82199a2dd95SBruce Richardson 	uint16_t count;
82299a2dd95SBruce Richardson 
82399a2dd95SBruce Richardson 	count = 0;
82499a2dd95SBruce Richardson 
82599a2dd95SBruce Richardson 	RTE_ETH_FOREACH_DEV(p)
82699a2dd95SBruce Richardson 		count++;
82799a2dd95SBruce Richardson 
82899a2dd95SBruce Richardson 	return count;
82999a2dd95SBruce Richardson }
83099a2dd95SBruce Richardson 
83199a2dd95SBruce Richardson uint16_t
83299a2dd95SBruce Richardson rte_eth_dev_count_total(void)
83399a2dd95SBruce Richardson {
83499a2dd95SBruce Richardson 	uint16_t port, count = 0;
83599a2dd95SBruce Richardson 
83699a2dd95SBruce Richardson 	RTE_ETH_FOREACH_VALID_DEV(port)
83799a2dd95SBruce Richardson 		count++;
83899a2dd95SBruce Richardson 
83999a2dd95SBruce Richardson 	return count;
84099a2dd95SBruce Richardson }
84199a2dd95SBruce Richardson 
84299a2dd95SBruce Richardson int
84399a2dd95SBruce Richardson rte_eth_dev_get_name_by_port(uint16_t port_id, char *name)
84499a2dd95SBruce Richardson {
84599a2dd95SBruce Richardson 	char *tmp;
84699a2dd95SBruce Richardson 
84799a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
84899a2dd95SBruce Richardson 
84999a2dd95SBruce Richardson 	if (name == NULL) {
85053ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot get ethdev port %u name to NULL\n",
85153ef1b34SMin Hu (Connor) 			port_id);
85299a2dd95SBruce Richardson 		return -EINVAL;
85399a2dd95SBruce Richardson 	}
85499a2dd95SBruce Richardson 
85599a2dd95SBruce Richardson 	/* shouldn't check 'rte_eth_devices[i].data',
85699a2dd95SBruce Richardson 	 * because it might be overwritten by VDEV PMD */
85799a2dd95SBruce Richardson 	tmp = eth_dev_shared_data->data[port_id].name;
85899a2dd95SBruce Richardson 	strcpy(name, tmp);
85999a2dd95SBruce Richardson 	return 0;
86099a2dd95SBruce Richardson }
86199a2dd95SBruce Richardson 
86299a2dd95SBruce Richardson int
86399a2dd95SBruce Richardson rte_eth_dev_get_port_by_name(const char *name, uint16_t *port_id)
86499a2dd95SBruce Richardson {
86599a2dd95SBruce Richardson 	uint16_t pid;
86699a2dd95SBruce Richardson 
86799a2dd95SBruce Richardson 	if (name == NULL) {
86853ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot get port ID from NULL name");
86953ef1b34SMin Hu (Connor) 		return -EINVAL;
87053ef1b34SMin Hu (Connor) 	}
87153ef1b34SMin Hu (Connor) 
87253ef1b34SMin Hu (Connor) 	if (port_id == NULL) {
87353ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
87453ef1b34SMin Hu (Connor) 			"Cannot get port ID to NULL for %s\n", name);
87599a2dd95SBruce Richardson 		return -EINVAL;
87699a2dd95SBruce Richardson 	}
87799a2dd95SBruce Richardson 
87899a2dd95SBruce Richardson 	RTE_ETH_FOREACH_VALID_DEV(pid)
87999a2dd95SBruce Richardson 		if (!strcmp(name, eth_dev_shared_data->data[pid].name)) {
88099a2dd95SBruce Richardson 			*port_id = pid;
88199a2dd95SBruce Richardson 			return 0;
88299a2dd95SBruce Richardson 		}
88399a2dd95SBruce Richardson 
88499a2dd95SBruce Richardson 	return -ENODEV;
88599a2dd95SBruce Richardson }
88699a2dd95SBruce Richardson 
88799a2dd95SBruce Richardson static int
88899a2dd95SBruce Richardson eth_err(uint16_t port_id, int ret)
88999a2dd95SBruce Richardson {
89099a2dd95SBruce Richardson 	if (ret == 0)
89199a2dd95SBruce Richardson 		return 0;
89299a2dd95SBruce Richardson 	if (rte_eth_dev_is_removed(port_id))
89399a2dd95SBruce Richardson 		return -EIO;
89499a2dd95SBruce Richardson 	return ret;
89599a2dd95SBruce Richardson }
89699a2dd95SBruce Richardson 
89749ed3224SXueming Li static void
89849ed3224SXueming Li eth_dev_rxq_release(struct rte_eth_dev *dev, uint16_t qid)
89949ed3224SXueming Li {
90049ed3224SXueming Li 	void **rxq = dev->data->rx_queues;
90149ed3224SXueming Li 
90249ed3224SXueming Li 	if (rxq[qid] == NULL)
90349ed3224SXueming Li 		return;
90449ed3224SXueming Li 
90549ed3224SXueming Li 	if (dev->dev_ops->rx_queue_release != NULL)
9067483341aSXueming Li 		(*dev->dev_ops->rx_queue_release)(dev, qid);
90749ed3224SXueming Li 	rxq[qid] = NULL;
90849ed3224SXueming Li }
90949ed3224SXueming Li 
91049ed3224SXueming Li static void
91149ed3224SXueming Li eth_dev_txq_release(struct rte_eth_dev *dev, uint16_t qid)
91249ed3224SXueming Li {
91349ed3224SXueming Li 	void **txq = dev->data->tx_queues;
91449ed3224SXueming Li 
91549ed3224SXueming Li 	if (txq[qid] == NULL)
91649ed3224SXueming Li 		return;
91749ed3224SXueming Li 
91849ed3224SXueming Li 	if (dev->dev_ops->tx_queue_release != NULL)
9197483341aSXueming Li 		(*dev->dev_ops->tx_queue_release)(dev, qid);
92049ed3224SXueming Li 	txq[qid] = NULL;
92149ed3224SXueming Li }
92249ed3224SXueming Li 
92399a2dd95SBruce Richardson static int
92499a2dd95SBruce Richardson eth_dev_rx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
92599a2dd95SBruce Richardson {
92699a2dd95SBruce Richardson 	uint16_t old_nb_queues = dev->data->nb_rx_queues;
92799a2dd95SBruce Richardson 	unsigned i;
92899a2dd95SBruce Richardson 
92999a2dd95SBruce Richardson 	if (dev->data->rx_queues == NULL && nb_queues != 0) { /* first time configuration */
93099a2dd95SBruce Richardson 		dev->data->rx_queues = rte_zmalloc("ethdev->rx_queues",
931c024496aSKonstantin Ananyev 				sizeof(dev->data->rx_queues[0]) *
932c024496aSKonstantin Ananyev 				RTE_MAX_QUEUES_PER_PORT,
93399a2dd95SBruce Richardson 				RTE_CACHE_LINE_SIZE);
93499a2dd95SBruce Richardson 		if (dev->data->rx_queues == NULL) {
93599a2dd95SBruce Richardson 			dev->data->nb_rx_queues = 0;
93699a2dd95SBruce Richardson 			return -(ENOMEM);
93799a2dd95SBruce Richardson 		}
93899a2dd95SBruce Richardson 	} else if (dev->data->rx_queues != NULL && nb_queues != 0) { /* re-configure */
93949ed3224SXueming Li 		for (i = nb_queues; i < old_nb_queues; i++)
94049ed3224SXueming Li 			eth_dev_rxq_release(dev, i);
94199a2dd95SBruce Richardson 
94299a2dd95SBruce Richardson 	} else if (dev->data->rx_queues != NULL && nb_queues == 0) {
94399a2dd95SBruce Richardson 		for (i = nb_queues; i < old_nb_queues; i++)
94449ed3224SXueming Li 			eth_dev_rxq_release(dev, i);
94599a2dd95SBruce Richardson 
94699a2dd95SBruce Richardson 		rte_free(dev->data->rx_queues);
94799a2dd95SBruce Richardson 		dev->data->rx_queues = NULL;
94899a2dd95SBruce Richardson 	}
94999a2dd95SBruce Richardson 	dev->data->nb_rx_queues = nb_queues;
95099a2dd95SBruce Richardson 	return 0;
95199a2dd95SBruce Richardson }
95299a2dd95SBruce Richardson 
95399a2dd95SBruce Richardson static int
95499a2dd95SBruce Richardson eth_dev_validate_rx_queue(const struct rte_eth_dev *dev, uint16_t rx_queue_id)
95599a2dd95SBruce Richardson {
95699a2dd95SBruce Richardson 	uint16_t port_id;
95799a2dd95SBruce Richardson 
95899a2dd95SBruce Richardson 	if (rx_queue_id >= dev->data->nb_rx_queues) {
95999a2dd95SBruce Richardson 		port_id = dev->data->port_id;
96099a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
96199a2dd95SBruce Richardson 			       "Invalid Rx queue_id=%u of device with port_id=%u\n",
96299a2dd95SBruce Richardson 			       rx_queue_id, port_id);
96399a2dd95SBruce Richardson 		return -EINVAL;
96499a2dd95SBruce Richardson 	}
96599a2dd95SBruce Richardson 
96699a2dd95SBruce Richardson 	if (dev->data->rx_queues[rx_queue_id] == NULL) {
96799a2dd95SBruce Richardson 		port_id = dev->data->port_id;
96899a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
96999a2dd95SBruce Richardson 			       "Queue %u of device with port_id=%u has not been setup\n",
97099a2dd95SBruce Richardson 			       rx_queue_id, port_id);
97199a2dd95SBruce Richardson 		return -EINVAL;
97299a2dd95SBruce Richardson 	}
97399a2dd95SBruce Richardson 
97499a2dd95SBruce Richardson 	return 0;
97599a2dd95SBruce Richardson }
97699a2dd95SBruce Richardson 
97799a2dd95SBruce Richardson static int
97899a2dd95SBruce Richardson eth_dev_validate_tx_queue(const struct rte_eth_dev *dev, uint16_t tx_queue_id)
97999a2dd95SBruce Richardson {
98099a2dd95SBruce Richardson 	uint16_t port_id;
98199a2dd95SBruce Richardson 
98299a2dd95SBruce Richardson 	if (tx_queue_id >= dev->data->nb_tx_queues) {
98399a2dd95SBruce Richardson 		port_id = dev->data->port_id;
98499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
98599a2dd95SBruce Richardson 			       "Invalid Tx queue_id=%u of device with port_id=%u\n",
98699a2dd95SBruce Richardson 			       tx_queue_id, port_id);
98799a2dd95SBruce Richardson 		return -EINVAL;
98899a2dd95SBruce Richardson 	}
98999a2dd95SBruce Richardson 
99099a2dd95SBruce Richardson 	if (dev->data->tx_queues[tx_queue_id] == NULL) {
99199a2dd95SBruce Richardson 		port_id = dev->data->port_id;
99299a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
99399a2dd95SBruce Richardson 			       "Queue %u of device with port_id=%u has not been setup\n",
99499a2dd95SBruce Richardson 			       tx_queue_id, port_id);
99599a2dd95SBruce Richardson 		return -EINVAL;
99699a2dd95SBruce Richardson 	}
99799a2dd95SBruce Richardson 
99899a2dd95SBruce Richardson 	return 0;
99999a2dd95SBruce Richardson }
100099a2dd95SBruce Richardson 
100199a2dd95SBruce Richardson int
100299a2dd95SBruce Richardson rte_eth_dev_rx_queue_start(uint16_t port_id, uint16_t rx_queue_id)
100399a2dd95SBruce Richardson {
100499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
100599a2dd95SBruce Richardson 	int ret;
100699a2dd95SBruce Richardson 
100799a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
100899a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
100953ef1b34SMin Hu (Connor) 
101099a2dd95SBruce Richardson 	if (!dev->data->dev_started) {
101199a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
101299a2dd95SBruce Richardson 			"Port %u must be started before start any queue\n",
101399a2dd95SBruce Richardson 			port_id);
101499a2dd95SBruce Richardson 		return -EINVAL;
101599a2dd95SBruce Richardson 	}
101699a2dd95SBruce Richardson 
101799a2dd95SBruce Richardson 	ret = eth_dev_validate_rx_queue(dev, rx_queue_id);
101899a2dd95SBruce Richardson 	if (ret != 0)
101999a2dd95SBruce Richardson 		return ret;
102099a2dd95SBruce Richardson 
102199a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_start, -ENOTSUP);
102299a2dd95SBruce Richardson 
102399a2dd95SBruce Richardson 	if (rte_eth_dev_is_rx_hairpin_queue(dev, rx_queue_id)) {
102499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(INFO,
102599a2dd95SBruce Richardson 			"Can't start Rx hairpin queue %"PRIu16" of device with port_id=%"PRIu16"\n",
102699a2dd95SBruce Richardson 			rx_queue_id, port_id);
102799a2dd95SBruce Richardson 		return -EINVAL;
102899a2dd95SBruce Richardson 	}
102999a2dd95SBruce Richardson 
103099a2dd95SBruce Richardson 	if (dev->data->rx_queue_state[rx_queue_id] != RTE_ETH_QUEUE_STATE_STOPPED) {
103199a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(INFO,
103299a2dd95SBruce Richardson 			"Queue %"PRIu16" of device with port_id=%"PRIu16" already started\n",
103399a2dd95SBruce Richardson 			rx_queue_id, port_id);
103499a2dd95SBruce Richardson 		return 0;
103599a2dd95SBruce Richardson 	}
103699a2dd95SBruce Richardson 
103753ef1b34SMin Hu (Connor) 	return eth_err(port_id, dev->dev_ops->rx_queue_start(dev, rx_queue_id));
103899a2dd95SBruce Richardson }
103999a2dd95SBruce Richardson 
104099a2dd95SBruce Richardson int
104199a2dd95SBruce Richardson rte_eth_dev_rx_queue_stop(uint16_t port_id, uint16_t rx_queue_id)
104299a2dd95SBruce Richardson {
104399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
104499a2dd95SBruce Richardson 	int ret;
104599a2dd95SBruce Richardson 
104699a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
104799a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
104899a2dd95SBruce Richardson 
104999a2dd95SBruce Richardson 	ret = eth_dev_validate_rx_queue(dev, rx_queue_id);
105099a2dd95SBruce Richardson 	if (ret != 0)
105199a2dd95SBruce Richardson 		return ret;
105299a2dd95SBruce Richardson 
105399a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_stop, -ENOTSUP);
105499a2dd95SBruce Richardson 
105599a2dd95SBruce Richardson 	if (rte_eth_dev_is_rx_hairpin_queue(dev, rx_queue_id)) {
105699a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(INFO,
105799a2dd95SBruce Richardson 			"Can't stop Rx hairpin queue %"PRIu16" of device with port_id=%"PRIu16"\n",
105899a2dd95SBruce Richardson 			rx_queue_id, port_id);
105999a2dd95SBruce Richardson 		return -EINVAL;
106099a2dd95SBruce Richardson 	}
106199a2dd95SBruce Richardson 
106299a2dd95SBruce Richardson 	if (dev->data->rx_queue_state[rx_queue_id] == RTE_ETH_QUEUE_STATE_STOPPED) {
106399a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(INFO,
106499a2dd95SBruce Richardson 			"Queue %"PRIu16" of device with port_id=%"PRIu16" already stopped\n",
106599a2dd95SBruce Richardson 			rx_queue_id, port_id);
106699a2dd95SBruce Richardson 		return 0;
106799a2dd95SBruce Richardson 	}
106899a2dd95SBruce Richardson 
106999a2dd95SBruce Richardson 	return eth_err(port_id, dev->dev_ops->rx_queue_stop(dev, rx_queue_id));
107099a2dd95SBruce Richardson }
107199a2dd95SBruce Richardson 
107299a2dd95SBruce Richardson int
107399a2dd95SBruce Richardson rte_eth_dev_tx_queue_start(uint16_t port_id, uint16_t tx_queue_id)
107499a2dd95SBruce Richardson {
107599a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
107699a2dd95SBruce Richardson 	int ret;
107799a2dd95SBruce Richardson 
107899a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
107999a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
108053ef1b34SMin Hu (Connor) 
108199a2dd95SBruce Richardson 	if (!dev->data->dev_started) {
108299a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
108399a2dd95SBruce Richardson 			"Port %u must be started before start any queue\n",
108499a2dd95SBruce Richardson 			port_id);
108599a2dd95SBruce Richardson 		return -EINVAL;
108699a2dd95SBruce Richardson 	}
108799a2dd95SBruce Richardson 
108899a2dd95SBruce Richardson 	ret = eth_dev_validate_tx_queue(dev, tx_queue_id);
108999a2dd95SBruce Richardson 	if (ret != 0)
109099a2dd95SBruce Richardson 		return ret;
109199a2dd95SBruce Richardson 
109299a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_start, -ENOTSUP);
109399a2dd95SBruce Richardson 
109499a2dd95SBruce Richardson 	if (rte_eth_dev_is_tx_hairpin_queue(dev, tx_queue_id)) {
109599a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(INFO,
109699a2dd95SBruce Richardson 			"Can't start Tx hairpin queue %"PRIu16" of device with port_id=%"PRIu16"\n",
109799a2dd95SBruce Richardson 			tx_queue_id, port_id);
109899a2dd95SBruce Richardson 		return -EINVAL;
109999a2dd95SBruce Richardson 	}
110099a2dd95SBruce Richardson 
110199a2dd95SBruce Richardson 	if (dev->data->tx_queue_state[tx_queue_id] != RTE_ETH_QUEUE_STATE_STOPPED) {
110299a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(INFO,
110399a2dd95SBruce Richardson 			"Queue %"PRIu16" of device with port_id=%"PRIu16" already started\n",
110499a2dd95SBruce Richardson 			tx_queue_id, port_id);
110599a2dd95SBruce Richardson 		return 0;
110699a2dd95SBruce Richardson 	}
110799a2dd95SBruce Richardson 
110899a2dd95SBruce Richardson 	return eth_err(port_id, dev->dev_ops->tx_queue_start(dev, tx_queue_id));
110999a2dd95SBruce Richardson }
111099a2dd95SBruce Richardson 
111199a2dd95SBruce Richardson int
111299a2dd95SBruce Richardson rte_eth_dev_tx_queue_stop(uint16_t port_id, uint16_t tx_queue_id)
111399a2dd95SBruce Richardson {
111499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
111599a2dd95SBruce Richardson 	int ret;
111699a2dd95SBruce Richardson 
111799a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
111899a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
111999a2dd95SBruce Richardson 
112099a2dd95SBruce Richardson 	ret = eth_dev_validate_tx_queue(dev, tx_queue_id);
112199a2dd95SBruce Richardson 	if (ret != 0)
112299a2dd95SBruce Richardson 		return ret;
112399a2dd95SBruce Richardson 
112499a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_stop, -ENOTSUP);
112599a2dd95SBruce Richardson 
112699a2dd95SBruce Richardson 	if (rte_eth_dev_is_tx_hairpin_queue(dev, tx_queue_id)) {
112799a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(INFO,
112899a2dd95SBruce Richardson 			"Can't stop Tx hairpin queue %"PRIu16" of device with port_id=%"PRIu16"\n",
112999a2dd95SBruce Richardson 			tx_queue_id, port_id);
113099a2dd95SBruce Richardson 		return -EINVAL;
113199a2dd95SBruce Richardson 	}
113299a2dd95SBruce Richardson 
113399a2dd95SBruce Richardson 	if (dev->data->tx_queue_state[tx_queue_id] == RTE_ETH_QUEUE_STATE_STOPPED) {
113499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(INFO,
113599a2dd95SBruce Richardson 			"Queue %"PRIu16" of device with port_id=%"PRIu16" already stopped\n",
113699a2dd95SBruce Richardson 			tx_queue_id, port_id);
113799a2dd95SBruce Richardson 		return 0;
113899a2dd95SBruce Richardson 	}
113999a2dd95SBruce Richardson 
114099a2dd95SBruce Richardson 	return eth_err(port_id, dev->dev_ops->tx_queue_stop(dev, tx_queue_id));
114199a2dd95SBruce Richardson }
114299a2dd95SBruce Richardson 
114399a2dd95SBruce Richardson static int
114499a2dd95SBruce Richardson eth_dev_tx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
114599a2dd95SBruce Richardson {
114699a2dd95SBruce Richardson 	uint16_t old_nb_queues = dev->data->nb_tx_queues;
114799a2dd95SBruce Richardson 	unsigned i;
114899a2dd95SBruce Richardson 
114999a2dd95SBruce Richardson 	if (dev->data->tx_queues == NULL && nb_queues != 0) { /* first time configuration */
115099a2dd95SBruce Richardson 		dev->data->tx_queues = rte_zmalloc("ethdev->tx_queues",
1151c024496aSKonstantin Ananyev 				sizeof(dev->data->tx_queues[0]) *
1152c024496aSKonstantin Ananyev 				RTE_MAX_QUEUES_PER_PORT,
115399a2dd95SBruce Richardson 				RTE_CACHE_LINE_SIZE);
115499a2dd95SBruce Richardson 		if (dev->data->tx_queues == NULL) {
115599a2dd95SBruce Richardson 			dev->data->nb_tx_queues = 0;
115699a2dd95SBruce Richardson 			return -(ENOMEM);
115799a2dd95SBruce Richardson 		}
115899a2dd95SBruce Richardson 	} else if (dev->data->tx_queues != NULL && nb_queues != 0) { /* re-configure */
115949ed3224SXueming Li 		for (i = nb_queues; i < old_nb_queues; i++)
116049ed3224SXueming Li 			eth_dev_txq_release(dev, i);
116199a2dd95SBruce Richardson 
116299a2dd95SBruce Richardson 	} else if (dev->data->tx_queues != NULL && nb_queues == 0) {
116399a2dd95SBruce Richardson 		for (i = nb_queues; i < old_nb_queues; i++)
116449ed3224SXueming Li 			eth_dev_txq_release(dev, i);
116599a2dd95SBruce Richardson 
116699a2dd95SBruce Richardson 		rte_free(dev->data->tx_queues);
116799a2dd95SBruce Richardson 		dev->data->tx_queues = NULL;
116899a2dd95SBruce Richardson 	}
116999a2dd95SBruce Richardson 	dev->data->nb_tx_queues = nb_queues;
117099a2dd95SBruce Richardson 	return 0;
117199a2dd95SBruce Richardson }
117299a2dd95SBruce Richardson 
117399a2dd95SBruce Richardson uint32_t
117499a2dd95SBruce Richardson rte_eth_speed_bitflag(uint32_t speed, int duplex)
117599a2dd95SBruce Richardson {
117699a2dd95SBruce Richardson 	switch (speed) {
117799a2dd95SBruce Richardson 	case ETH_SPEED_NUM_10M:
117899a2dd95SBruce Richardson 		return duplex ? ETH_LINK_SPEED_10M : ETH_LINK_SPEED_10M_HD;
117999a2dd95SBruce Richardson 	case ETH_SPEED_NUM_100M:
118099a2dd95SBruce Richardson 		return duplex ? ETH_LINK_SPEED_100M : ETH_LINK_SPEED_100M_HD;
118199a2dd95SBruce Richardson 	case ETH_SPEED_NUM_1G:
118299a2dd95SBruce Richardson 		return ETH_LINK_SPEED_1G;
118399a2dd95SBruce Richardson 	case ETH_SPEED_NUM_2_5G:
118499a2dd95SBruce Richardson 		return ETH_LINK_SPEED_2_5G;
118599a2dd95SBruce Richardson 	case ETH_SPEED_NUM_5G:
118699a2dd95SBruce Richardson 		return ETH_LINK_SPEED_5G;
118799a2dd95SBruce Richardson 	case ETH_SPEED_NUM_10G:
118899a2dd95SBruce Richardson 		return ETH_LINK_SPEED_10G;
118999a2dd95SBruce Richardson 	case ETH_SPEED_NUM_20G:
119099a2dd95SBruce Richardson 		return ETH_LINK_SPEED_20G;
119199a2dd95SBruce Richardson 	case ETH_SPEED_NUM_25G:
119299a2dd95SBruce Richardson 		return ETH_LINK_SPEED_25G;
119399a2dd95SBruce Richardson 	case ETH_SPEED_NUM_40G:
119499a2dd95SBruce Richardson 		return ETH_LINK_SPEED_40G;
119599a2dd95SBruce Richardson 	case ETH_SPEED_NUM_50G:
119699a2dd95SBruce Richardson 		return ETH_LINK_SPEED_50G;
119799a2dd95SBruce Richardson 	case ETH_SPEED_NUM_56G:
119899a2dd95SBruce Richardson 		return ETH_LINK_SPEED_56G;
119999a2dd95SBruce Richardson 	case ETH_SPEED_NUM_100G:
120099a2dd95SBruce Richardson 		return ETH_LINK_SPEED_100G;
120199a2dd95SBruce Richardson 	case ETH_SPEED_NUM_200G:
120299a2dd95SBruce Richardson 		return ETH_LINK_SPEED_200G;
120399a2dd95SBruce Richardson 	default:
120499a2dd95SBruce Richardson 		return 0;
120599a2dd95SBruce Richardson 	}
120699a2dd95SBruce Richardson }
120799a2dd95SBruce Richardson 
120899a2dd95SBruce Richardson const char *
120999a2dd95SBruce Richardson rte_eth_dev_rx_offload_name(uint64_t offload)
121099a2dd95SBruce Richardson {
121199a2dd95SBruce Richardson 	const char *name = "UNKNOWN";
121299a2dd95SBruce Richardson 	unsigned int i;
121399a2dd95SBruce Richardson 
121499a2dd95SBruce Richardson 	for (i = 0; i < RTE_DIM(eth_dev_rx_offload_names); ++i) {
121599a2dd95SBruce Richardson 		if (offload == eth_dev_rx_offload_names[i].offload) {
121699a2dd95SBruce Richardson 			name = eth_dev_rx_offload_names[i].name;
121799a2dd95SBruce Richardson 			break;
121899a2dd95SBruce Richardson 		}
121999a2dd95SBruce Richardson 	}
122099a2dd95SBruce Richardson 
122199a2dd95SBruce Richardson 	return name;
122299a2dd95SBruce Richardson }
122399a2dd95SBruce Richardson 
122499a2dd95SBruce Richardson const char *
122599a2dd95SBruce Richardson rte_eth_dev_tx_offload_name(uint64_t offload)
122699a2dd95SBruce Richardson {
122799a2dd95SBruce Richardson 	const char *name = "UNKNOWN";
122899a2dd95SBruce Richardson 	unsigned int i;
122999a2dd95SBruce Richardson 
123099a2dd95SBruce Richardson 	for (i = 0; i < RTE_DIM(eth_dev_tx_offload_names); ++i) {
123199a2dd95SBruce Richardson 		if (offload == eth_dev_tx_offload_names[i].offload) {
123299a2dd95SBruce Richardson 			name = eth_dev_tx_offload_names[i].name;
123399a2dd95SBruce Richardson 			break;
123499a2dd95SBruce Richardson 		}
123599a2dd95SBruce Richardson 	}
123699a2dd95SBruce Richardson 
123799a2dd95SBruce Richardson 	return name;
123899a2dd95SBruce Richardson }
123999a2dd95SBruce Richardson 
124099a2dd95SBruce Richardson static inline int
124199a2dd95SBruce Richardson eth_dev_check_lro_pkt_size(uint16_t port_id, uint32_t config_size,
124299a2dd95SBruce Richardson 		   uint32_t max_rx_pkt_len, uint32_t dev_info_size)
124399a2dd95SBruce Richardson {
124499a2dd95SBruce Richardson 	int ret = 0;
124599a2dd95SBruce Richardson 
124699a2dd95SBruce Richardson 	if (dev_info_size == 0) {
124799a2dd95SBruce Richardson 		if (config_size != max_rx_pkt_len) {
124899a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR, "Ethdev port_id=%d max_lro_pkt_size"
124999a2dd95SBruce Richardson 				       " %u != %u is not allowed\n",
125099a2dd95SBruce Richardson 				       port_id, config_size, max_rx_pkt_len);
125199a2dd95SBruce Richardson 			ret = -EINVAL;
125299a2dd95SBruce Richardson 		}
125399a2dd95SBruce Richardson 	} else if (config_size > dev_info_size) {
125499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Ethdev port_id=%d max_lro_pkt_size %u "
125599a2dd95SBruce Richardson 			       "> max allowed value %u\n", port_id, config_size,
125699a2dd95SBruce Richardson 			       dev_info_size);
125799a2dd95SBruce Richardson 		ret = -EINVAL;
125899a2dd95SBruce Richardson 	} else if (config_size < RTE_ETHER_MIN_LEN) {
125999a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Ethdev port_id=%d max_lro_pkt_size %u "
126099a2dd95SBruce Richardson 			       "< min allowed value %u\n", port_id, config_size,
126199a2dd95SBruce Richardson 			       (unsigned int)RTE_ETHER_MIN_LEN);
126299a2dd95SBruce Richardson 		ret = -EINVAL;
126399a2dd95SBruce Richardson 	}
126499a2dd95SBruce Richardson 	return ret;
126599a2dd95SBruce Richardson }
126699a2dd95SBruce Richardson 
126799a2dd95SBruce Richardson /*
126899a2dd95SBruce Richardson  * Validate offloads that are requested through rte_eth_dev_configure against
126999a2dd95SBruce Richardson  * the offloads successfully set by the ethernet device.
127099a2dd95SBruce Richardson  *
127199a2dd95SBruce Richardson  * @param port_id
127299a2dd95SBruce Richardson  *   The port identifier of the Ethernet device.
127399a2dd95SBruce Richardson  * @param req_offloads
127499a2dd95SBruce Richardson  *   The offloads that have been requested through `rte_eth_dev_configure`.
127599a2dd95SBruce Richardson  * @param set_offloads
127699a2dd95SBruce Richardson  *   The offloads successfully set by the ethernet device.
127799a2dd95SBruce Richardson  * @param offload_type
127899a2dd95SBruce Richardson  *   The offload type i.e. Rx/Tx string.
127999a2dd95SBruce Richardson  * @param offload_name
128099a2dd95SBruce Richardson  *   The function that prints the offload name.
128199a2dd95SBruce Richardson  * @return
128299a2dd95SBruce Richardson  *   - (0) if validation successful.
128399a2dd95SBruce Richardson  *   - (-EINVAL) if requested offload has been silently disabled.
128499a2dd95SBruce Richardson  *
128599a2dd95SBruce Richardson  */
128699a2dd95SBruce Richardson static int
128799a2dd95SBruce Richardson eth_dev_validate_offloads(uint16_t port_id, uint64_t req_offloads,
128899a2dd95SBruce Richardson 		  uint64_t set_offloads, const char *offload_type,
128999a2dd95SBruce Richardson 		  const char *(*offload_name)(uint64_t))
129099a2dd95SBruce Richardson {
129199a2dd95SBruce Richardson 	uint64_t offloads_diff = req_offloads ^ set_offloads;
129299a2dd95SBruce Richardson 	uint64_t offload;
129399a2dd95SBruce Richardson 	int ret = 0;
129499a2dd95SBruce Richardson 
129599a2dd95SBruce Richardson 	while (offloads_diff != 0) {
129699a2dd95SBruce Richardson 		/* Check if any offload is requested but not enabled. */
129799a2dd95SBruce Richardson 		offload = 1ULL << __builtin_ctzll(offloads_diff);
129899a2dd95SBruce Richardson 		if (offload & req_offloads) {
129999a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
130099a2dd95SBruce Richardson 				"Port %u failed to enable %s offload %s\n",
130199a2dd95SBruce Richardson 				port_id, offload_type, offload_name(offload));
130299a2dd95SBruce Richardson 			ret = -EINVAL;
130399a2dd95SBruce Richardson 		}
130499a2dd95SBruce Richardson 
130599a2dd95SBruce Richardson 		/* Check if offload couldn't be disabled. */
130699a2dd95SBruce Richardson 		if (offload & set_offloads) {
130799a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(DEBUG,
130899a2dd95SBruce Richardson 				"Port %u %s offload %s is not requested but enabled\n",
130999a2dd95SBruce Richardson 				port_id, offload_type, offload_name(offload));
131099a2dd95SBruce Richardson 		}
131199a2dd95SBruce Richardson 
131299a2dd95SBruce Richardson 		offloads_diff &= ~offload;
131399a2dd95SBruce Richardson 	}
131499a2dd95SBruce Richardson 
131599a2dd95SBruce Richardson 	return ret;
131699a2dd95SBruce Richardson }
131799a2dd95SBruce Richardson 
13181bb4a528SFerruh Yigit static uint32_t
13191bb4a528SFerruh Yigit eth_dev_get_overhead_len(uint32_t max_rx_pktlen, uint16_t max_mtu)
13201bb4a528SFerruh Yigit {
13211bb4a528SFerruh Yigit 	uint32_t overhead_len;
13221bb4a528SFerruh Yigit 
13231bb4a528SFerruh Yigit 	if (max_mtu != UINT16_MAX && max_rx_pktlen > max_mtu)
13241bb4a528SFerruh Yigit 		overhead_len = max_rx_pktlen - max_mtu;
13251bb4a528SFerruh Yigit 	else
13261bb4a528SFerruh Yigit 		overhead_len = RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN;
13271bb4a528SFerruh Yigit 
13281bb4a528SFerruh Yigit 	return overhead_len;
13291bb4a528SFerruh Yigit }
13301bb4a528SFerruh Yigit 
133199a2dd95SBruce Richardson int
133299a2dd95SBruce Richardson rte_eth_dev_configure(uint16_t port_id, uint16_t nb_rx_q, uint16_t nb_tx_q,
133399a2dd95SBruce Richardson 		      const struct rte_eth_conf *dev_conf)
133499a2dd95SBruce Richardson {
133599a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
133699a2dd95SBruce Richardson 	struct rte_eth_dev_info dev_info;
133799a2dd95SBruce Richardson 	struct rte_eth_conf orig_conf;
13381bb4a528SFerruh Yigit 	uint32_t max_rx_pktlen;
13391bb4a528SFerruh Yigit 	uint32_t overhead_len;
134099a2dd95SBruce Richardson 	int diag;
134199a2dd95SBruce Richardson 	int ret;
134299a2dd95SBruce Richardson 	uint16_t old_mtu;
134399a2dd95SBruce Richardson 
134499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
134599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
134699a2dd95SBruce Richardson 
134753ef1b34SMin Hu (Connor) 	if (dev_conf == NULL) {
134853ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
134953ef1b34SMin Hu (Connor) 			"Cannot configure ethdev port %u from NULL config\n",
135053ef1b34SMin Hu (Connor) 			port_id);
135153ef1b34SMin Hu (Connor) 		return -EINVAL;
135253ef1b34SMin Hu (Connor) 	}
135353ef1b34SMin Hu (Connor) 
135499a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_configure, -ENOTSUP);
135599a2dd95SBruce Richardson 
135699a2dd95SBruce Richardson 	if (dev->data->dev_started) {
135799a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
135899a2dd95SBruce Richardson 			"Port %u must be stopped to allow configuration\n",
135999a2dd95SBruce Richardson 			port_id);
136099a2dd95SBruce Richardson 		return -EBUSY;
136199a2dd95SBruce Richardson 	}
136299a2dd95SBruce Richardson 
136302edbfabSHuisong Li 	/*
136402edbfabSHuisong Li 	 * Ensure that "dev_configured" is always 0 each time prepare to do
136502edbfabSHuisong Li 	 * dev_configure() to avoid any non-anticipated behaviour.
136602edbfabSHuisong Li 	 * And set to 1 when dev_configure() is executed successfully.
136702edbfabSHuisong Li 	 */
136802edbfabSHuisong Li 	dev->data->dev_configured = 0;
136902edbfabSHuisong Li 
137099a2dd95SBruce Richardson 	 /* Store original config, as rollback required on failure */
137199a2dd95SBruce Richardson 	memcpy(&orig_conf, &dev->data->dev_conf, sizeof(dev->data->dev_conf));
137299a2dd95SBruce Richardson 
137399a2dd95SBruce Richardson 	/*
137499a2dd95SBruce Richardson 	 * Copy the dev_conf parameter into the dev structure.
137599a2dd95SBruce Richardson 	 * rte_eth_dev_info_get() requires dev_conf, copy it before dev_info get
137699a2dd95SBruce Richardson 	 */
137799a2dd95SBruce Richardson 	if (dev_conf != &dev->data->dev_conf)
137899a2dd95SBruce Richardson 		memcpy(&dev->data->dev_conf, dev_conf,
137999a2dd95SBruce Richardson 		       sizeof(dev->data->dev_conf));
138099a2dd95SBruce Richardson 
138199a2dd95SBruce Richardson 	/* Backup mtu for rollback */
138299a2dd95SBruce Richardson 	old_mtu = dev->data->mtu;
138399a2dd95SBruce Richardson 
138499a2dd95SBruce Richardson 	ret = rte_eth_dev_info_get(port_id, &dev_info);
138599a2dd95SBruce Richardson 	if (ret != 0)
138699a2dd95SBruce Richardson 		goto rollback;
138799a2dd95SBruce Richardson 
138899a2dd95SBruce Richardson 	/* Get the real Ethernet overhead length */
13891bb4a528SFerruh Yigit 	overhead_len = eth_dev_get_overhead_len(dev_info.max_rx_pktlen,
13901bb4a528SFerruh Yigit 			dev_info.max_mtu);
139199a2dd95SBruce Richardson 
139299a2dd95SBruce Richardson 	/* If number of queues specified by application for both Rx and Tx is
139399a2dd95SBruce Richardson 	 * zero, use driver preferred values. This cannot be done individually
139499a2dd95SBruce Richardson 	 * as it is valid for either Tx or Rx (but not both) to be zero.
139599a2dd95SBruce Richardson 	 * If driver does not provide any preferred valued, fall back on
139699a2dd95SBruce Richardson 	 * EAL defaults.
139799a2dd95SBruce Richardson 	 */
139899a2dd95SBruce Richardson 	if (nb_rx_q == 0 && nb_tx_q == 0) {
139999a2dd95SBruce Richardson 		nb_rx_q = dev_info.default_rxportconf.nb_queues;
140099a2dd95SBruce Richardson 		if (nb_rx_q == 0)
140199a2dd95SBruce Richardson 			nb_rx_q = RTE_ETH_DEV_FALLBACK_RX_NBQUEUES;
140299a2dd95SBruce Richardson 		nb_tx_q = dev_info.default_txportconf.nb_queues;
140399a2dd95SBruce Richardson 		if (nb_tx_q == 0)
140499a2dd95SBruce Richardson 			nb_tx_q = RTE_ETH_DEV_FALLBACK_TX_NBQUEUES;
140599a2dd95SBruce Richardson 	}
140699a2dd95SBruce Richardson 
140799a2dd95SBruce Richardson 	if (nb_rx_q > RTE_MAX_QUEUES_PER_PORT) {
140899a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
140999a2dd95SBruce Richardson 			"Number of RX queues requested (%u) is greater than max supported(%d)\n",
141099a2dd95SBruce Richardson 			nb_rx_q, RTE_MAX_QUEUES_PER_PORT);
141199a2dd95SBruce Richardson 		ret = -EINVAL;
141299a2dd95SBruce Richardson 		goto rollback;
141399a2dd95SBruce Richardson 	}
141499a2dd95SBruce Richardson 
141599a2dd95SBruce Richardson 	if (nb_tx_q > RTE_MAX_QUEUES_PER_PORT) {
141699a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
141799a2dd95SBruce Richardson 			"Number of TX queues requested (%u) is greater than max supported(%d)\n",
141899a2dd95SBruce Richardson 			nb_tx_q, RTE_MAX_QUEUES_PER_PORT);
141999a2dd95SBruce Richardson 		ret = -EINVAL;
142099a2dd95SBruce Richardson 		goto rollback;
142199a2dd95SBruce Richardson 	}
142299a2dd95SBruce Richardson 
142399a2dd95SBruce Richardson 	/*
142499a2dd95SBruce Richardson 	 * Check that the numbers of RX and TX queues are not greater
142599a2dd95SBruce Richardson 	 * than the maximum number of RX and TX queues supported by the
142699a2dd95SBruce Richardson 	 * configured device.
142799a2dd95SBruce Richardson 	 */
142899a2dd95SBruce Richardson 	if (nb_rx_q > dev_info.max_rx_queues) {
142999a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Ethdev port_id=%u nb_rx_queues=%u > %u\n",
143099a2dd95SBruce Richardson 			port_id, nb_rx_q, dev_info.max_rx_queues);
143199a2dd95SBruce Richardson 		ret = -EINVAL;
143299a2dd95SBruce Richardson 		goto rollback;
143399a2dd95SBruce Richardson 	}
143499a2dd95SBruce Richardson 
143599a2dd95SBruce Richardson 	if (nb_tx_q > dev_info.max_tx_queues) {
143699a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Ethdev port_id=%u nb_tx_queues=%u > %u\n",
143799a2dd95SBruce Richardson 			port_id, nb_tx_q, dev_info.max_tx_queues);
143899a2dd95SBruce Richardson 		ret = -EINVAL;
143999a2dd95SBruce Richardson 		goto rollback;
144099a2dd95SBruce Richardson 	}
144199a2dd95SBruce Richardson 
144299a2dd95SBruce Richardson 	/* Check that the device supports requested interrupts */
144399a2dd95SBruce Richardson 	if ((dev_conf->intr_conf.lsc == 1) &&
144499a2dd95SBruce Richardson 			(!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC))) {
144599a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Driver %s does not support lsc\n",
144699a2dd95SBruce Richardson 			dev->device->driver->name);
144799a2dd95SBruce Richardson 		ret = -EINVAL;
144899a2dd95SBruce Richardson 		goto rollback;
144999a2dd95SBruce Richardson 	}
145099a2dd95SBruce Richardson 	if ((dev_conf->intr_conf.rmv == 1) &&
145199a2dd95SBruce Richardson 			(!(dev->data->dev_flags & RTE_ETH_DEV_INTR_RMV))) {
145299a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Driver %s does not support rmv\n",
145399a2dd95SBruce Richardson 			dev->device->driver->name);
145499a2dd95SBruce Richardson 		ret = -EINVAL;
145599a2dd95SBruce Richardson 		goto rollback;
145699a2dd95SBruce Richardson 	}
145799a2dd95SBruce Richardson 
145899a2dd95SBruce Richardson 	/*
14591bb4a528SFerruh Yigit 	 * Check that the maximum RX packet length is supported by the
14601bb4a528SFerruh Yigit 	 * configured device.
146199a2dd95SBruce Richardson 	 */
14621bb4a528SFerruh Yigit 	if (dev_conf->rxmode.mtu == 0)
14631bb4a528SFerruh Yigit 		dev->data->dev_conf.rxmode.mtu = RTE_ETHER_MTU;
14641bb4a528SFerruh Yigit 	max_rx_pktlen = dev->data->dev_conf.rxmode.mtu + overhead_len;
14651bb4a528SFerruh Yigit 	if (max_rx_pktlen > dev_info.max_rx_pktlen) {
146699a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
14671bb4a528SFerruh Yigit 			"Ethdev port_id=%u max_rx_pktlen %u > max valid value %u\n",
14681bb4a528SFerruh Yigit 			port_id, max_rx_pktlen, dev_info.max_rx_pktlen);
146999a2dd95SBruce Richardson 		ret = -EINVAL;
147099a2dd95SBruce Richardson 		goto rollback;
14711bb4a528SFerruh Yigit 	} else if (max_rx_pktlen < RTE_ETHER_MIN_LEN) {
147299a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
14731bb4a528SFerruh Yigit 			"Ethdev port_id=%u max_rx_pktlen %u < min valid value %u\n",
14741bb4a528SFerruh Yigit 			port_id, max_rx_pktlen, RTE_ETHER_MIN_LEN);
147599a2dd95SBruce Richardson 		ret = -EINVAL;
147699a2dd95SBruce Richardson 		goto rollback;
147799a2dd95SBruce Richardson 	}
147899a2dd95SBruce Richardson 
14791bb4a528SFerruh Yigit 	if ((dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) == 0) {
14801bb4a528SFerruh Yigit 		if (dev->data->dev_conf.rxmode.mtu < RTE_ETHER_MIN_MTU ||
14811bb4a528SFerruh Yigit 				dev->data->dev_conf.rxmode.mtu > RTE_ETHER_MTU)
148299a2dd95SBruce Richardson 			/* Use default value */
14831bb4a528SFerruh Yigit 			dev->data->dev_conf.rxmode.mtu = RTE_ETHER_MTU;
148499a2dd95SBruce Richardson 	}
148599a2dd95SBruce Richardson 
14861bb4a528SFerruh Yigit 	dev->data->mtu = dev->data->dev_conf.rxmode.mtu;
14871bb4a528SFerruh Yigit 
148899a2dd95SBruce Richardson 	/*
148999a2dd95SBruce Richardson 	 * If LRO is enabled, check that the maximum aggregated packet
149099a2dd95SBruce Richardson 	 * size is supported by the configured device.
149199a2dd95SBruce Richardson 	 */
149299a2dd95SBruce Richardson 	if (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_TCP_LRO) {
149399a2dd95SBruce Richardson 		if (dev_conf->rxmode.max_lro_pkt_size == 0)
14941bb4a528SFerruh Yigit 			dev->data->dev_conf.rxmode.max_lro_pkt_size = max_rx_pktlen;
149599a2dd95SBruce Richardson 		ret = eth_dev_check_lro_pkt_size(port_id,
149699a2dd95SBruce Richardson 				dev->data->dev_conf.rxmode.max_lro_pkt_size,
14971bb4a528SFerruh Yigit 				max_rx_pktlen,
149899a2dd95SBruce Richardson 				dev_info.max_lro_pkt_size);
149999a2dd95SBruce Richardson 		if (ret != 0)
150099a2dd95SBruce Richardson 			goto rollback;
150199a2dd95SBruce Richardson 	}
150299a2dd95SBruce Richardson 
150399a2dd95SBruce Richardson 	/* Any requested offloading must be within its device capabilities */
150499a2dd95SBruce Richardson 	if ((dev_conf->rxmode.offloads & dev_info.rx_offload_capa) !=
150599a2dd95SBruce Richardson 	     dev_conf->rxmode.offloads) {
150699a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
150799a2dd95SBruce Richardson 			"Ethdev port_id=%u requested Rx offloads 0x%"PRIx64" doesn't match Rx offloads "
150899a2dd95SBruce Richardson 			"capabilities 0x%"PRIx64" in %s()\n",
150999a2dd95SBruce Richardson 			port_id, dev_conf->rxmode.offloads,
151099a2dd95SBruce Richardson 			dev_info.rx_offload_capa,
151199a2dd95SBruce Richardson 			__func__);
151299a2dd95SBruce Richardson 		ret = -EINVAL;
151399a2dd95SBruce Richardson 		goto rollback;
151499a2dd95SBruce Richardson 	}
151599a2dd95SBruce Richardson 	if ((dev_conf->txmode.offloads & dev_info.tx_offload_capa) !=
151699a2dd95SBruce Richardson 	     dev_conf->txmode.offloads) {
151799a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
151899a2dd95SBruce Richardson 			"Ethdev port_id=%u requested Tx offloads 0x%"PRIx64" doesn't match Tx offloads "
151999a2dd95SBruce Richardson 			"capabilities 0x%"PRIx64" in %s()\n",
152099a2dd95SBruce Richardson 			port_id, dev_conf->txmode.offloads,
152199a2dd95SBruce Richardson 			dev_info.tx_offload_capa,
152299a2dd95SBruce Richardson 			__func__);
152399a2dd95SBruce Richardson 		ret = -EINVAL;
152499a2dd95SBruce Richardson 		goto rollback;
152599a2dd95SBruce Richardson 	}
152699a2dd95SBruce Richardson 
152799a2dd95SBruce Richardson 	dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf =
152899a2dd95SBruce Richardson 		rte_eth_rss_hf_refine(dev_conf->rx_adv_conf.rss_conf.rss_hf);
152999a2dd95SBruce Richardson 
153099a2dd95SBruce Richardson 	/* Check that device supports requested rss hash functions. */
153199a2dd95SBruce Richardson 	if ((dev_info.flow_type_rss_offloads |
153299a2dd95SBruce Richardson 	     dev_conf->rx_adv_conf.rss_conf.rss_hf) !=
153399a2dd95SBruce Richardson 	    dev_info.flow_type_rss_offloads) {
153499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
153599a2dd95SBruce Richardson 			"Ethdev port_id=%u invalid rss_hf: 0x%"PRIx64", valid value: 0x%"PRIx64"\n",
153699a2dd95SBruce Richardson 			port_id, dev_conf->rx_adv_conf.rss_conf.rss_hf,
153799a2dd95SBruce Richardson 			dev_info.flow_type_rss_offloads);
153899a2dd95SBruce Richardson 		ret = -EINVAL;
153999a2dd95SBruce Richardson 		goto rollback;
154099a2dd95SBruce Richardson 	}
154199a2dd95SBruce Richardson 
154299a2dd95SBruce Richardson 	/* Check if Rx RSS distribution is disabled but RSS hash is enabled. */
154399a2dd95SBruce Richardson 	if (((dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) == 0) &&
154499a2dd95SBruce Richardson 	    (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_RSS_HASH)) {
154599a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
154699a2dd95SBruce Richardson 			"Ethdev port_id=%u config invalid Rx mq_mode without RSS but %s offload is requested\n",
154799a2dd95SBruce Richardson 			port_id,
154899a2dd95SBruce Richardson 			rte_eth_dev_rx_offload_name(DEV_RX_OFFLOAD_RSS_HASH));
154999a2dd95SBruce Richardson 		ret = -EINVAL;
155099a2dd95SBruce Richardson 		goto rollback;
155199a2dd95SBruce Richardson 	}
155299a2dd95SBruce Richardson 
155399a2dd95SBruce Richardson 	/*
155499a2dd95SBruce Richardson 	 * Setup new number of RX/TX queues and reconfigure device.
155599a2dd95SBruce Richardson 	 */
155699a2dd95SBruce Richardson 	diag = eth_dev_rx_queue_config(dev, nb_rx_q);
155799a2dd95SBruce Richardson 	if (diag != 0) {
155899a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
155999a2dd95SBruce Richardson 			"Port%u eth_dev_rx_queue_config = %d\n",
156099a2dd95SBruce Richardson 			port_id, diag);
156199a2dd95SBruce Richardson 		ret = diag;
156299a2dd95SBruce Richardson 		goto rollback;
156399a2dd95SBruce Richardson 	}
156499a2dd95SBruce Richardson 
156599a2dd95SBruce Richardson 	diag = eth_dev_tx_queue_config(dev, nb_tx_q);
156699a2dd95SBruce Richardson 	if (diag != 0) {
156799a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
156899a2dd95SBruce Richardson 			"Port%u eth_dev_tx_queue_config = %d\n",
156999a2dd95SBruce Richardson 			port_id, diag);
157099a2dd95SBruce Richardson 		eth_dev_rx_queue_config(dev, 0);
157199a2dd95SBruce Richardson 		ret = diag;
157299a2dd95SBruce Richardson 		goto rollback;
157399a2dd95SBruce Richardson 	}
157499a2dd95SBruce Richardson 
157599a2dd95SBruce Richardson 	diag = (*dev->dev_ops->dev_configure)(dev);
157699a2dd95SBruce Richardson 	if (diag != 0) {
157799a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Port%u dev_configure = %d\n",
157899a2dd95SBruce Richardson 			port_id, diag);
157999a2dd95SBruce Richardson 		ret = eth_err(port_id, diag);
158099a2dd95SBruce Richardson 		goto reset_queues;
158199a2dd95SBruce Richardson 	}
158299a2dd95SBruce Richardson 
158399a2dd95SBruce Richardson 	/* Initialize Rx profiling if enabled at compilation time. */
158499a2dd95SBruce Richardson 	diag = __rte_eth_dev_profile_init(port_id, dev);
158599a2dd95SBruce Richardson 	if (diag != 0) {
158699a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Port%u __rte_eth_dev_profile_init = %d\n",
158799a2dd95SBruce Richardson 			port_id, diag);
158899a2dd95SBruce Richardson 		ret = eth_err(port_id, diag);
158999a2dd95SBruce Richardson 		goto reset_queues;
159099a2dd95SBruce Richardson 	}
159199a2dd95SBruce Richardson 
159299a2dd95SBruce Richardson 	/* Validate Rx offloads. */
159399a2dd95SBruce Richardson 	diag = eth_dev_validate_offloads(port_id,
159499a2dd95SBruce Richardson 			dev_conf->rxmode.offloads,
159599a2dd95SBruce Richardson 			dev->data->dev_conf.rxmode.offloads, "Rx",
159699a2dd95SBruce Richardson 			rte_eth_dev_rx_offload_name);
159799a2dd95SBruce Richardson 	if (diag != 0) {
159899a2dd95SBruce Richardson 		ret = diag;
159999a2dd95SBruce Richardson 		goto reset_queues;
160099a2dd95SBruce Richardson 	}
160199a2dd95SBruce Richardson 
160299a2dd95SBruce Richardson 	/* Validate Tx offloads. */
160399a2dd95SBruce Richardson 	diag = eth_dev_validate_offloads(port_id,
160499a2dd95SBruce Richardson 			dev_conf->txmode.offloads,
160599a2dd95SBruce Richardson 			dev->data->dev_conf.txmode.offloads, "Tx",
160699a2dd95SBruce Richardson 			rte_eth_dev_tx_offload_name);
160799a2dd95SBruce Richardson 	if (diag != 0) {
160899a2dd95SBruce Richardson 		ret = diag;
160999a2dd95SBruce Richardson 		goto reset_queues;
161099a2dd95SBruce Richardson 	}
161199a2dd95SBruce Richardson 
161202edbfabSHuisong Li 	dev->data->dev_configured = 1;
161399a2dd95SBruce Richardson 	rte_ethdev_trace_configure(port_id, nb_rx_q, nb_tx_q, dev_conf, 0);
161499a2dd95SBruce Richardson 	return 0;
161599a2dd95SBruce Richardson reset_queues:
161699a2dd95SBruce Richardson 	eth_dev_rx_queue_config(dev, 0);
161799a2dd95SBruce Richardson 	eth_dev_tx_queue_config(dev, 0);
161899a2dd95SBruce Richardson rollback:
161999a2dd95SBruce Richardson 	memcpy(&dev->data->dev_conf, &orig_conf, sizeof(dev->data->dev_conf));
162099a2dd95SBruce Richardson 	if (old_mtu != dev->data->mtu)
162199a2dd95SBruce Richardson 		dev->data->mtu = old_mtu;
162299a2dd95SBruce Richardson 
162399a2dd95SBruce Richardson 	rte_ethdev_trace_configure(port_id, nb_rx_q, nb_tx_q, dev_conf, ret);
162499a2dd95SBruce Richardson 	return ret;
162599a2dd95SBruce Richardson }
162699a2dd95SBruce Richardson 
162799a2dd95SBruce Richardson void
162899a2dd95SBruce Richardson rte_eth_dev_internal_reset(struct rte_eth_dev *dev)
162999a2dd95SBruce Richardson {
163099a2dd95SBruce Richardson 	if (dev->data->dev_started) {
163199a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Port %u must be stopped to allow reset\n",
163299a2dd95SBruce Richardson 			dev->data->port_id);
163399a2dd95SBruce Richardson 		return;
163499a2dd95SBruce Richardson 	}
163599a2dd95SBruce Richardson 
163699a2dd95SBruce Richardson 	eth_dev_rx_queue_config(dev, 0);
163799a2dd95SBruce Richardson 	eth_dev_tx_queue_config(dev, 0);
163899a2dd95SBruce Richardson 
163999a2dd95SBruce Richardson 	memset(&dev->data->dev_conf, 0, sizeof(dev->data->dev_conf));
164099a2dd95SBruce Richardson }
164199a2dd95SBruce Richardson 
164299a2dd95SBruce Richardson static void
164399a2dd95SBruce Richardson eth_dev_mac_restore(struct rte_eth_dev *dev,
164499a2dd95SBruce Richardson 			struct rte_eth_dev_info *dev_info)
164599a2dd95SBruce Richardson {
164699a2dd95SBruce Richardson 	struct rte_ether_addr *addr;
164799a2dd95SBruce Richardson 	uint16_t i;
164899a2dd95SBruce Richardson 	uint32_t pool = 0;
164999a2dd95SBruce Richardson 	uint64_t pool_mask;
165099a2dd95SBruce Richardson 
165199a2dd95SBruce Richardson 	/* replay MAC address configuration including default MAC */
165299a2dd95SBruce Richardson 	addr = &dev->data->mac_addrs[0];
165399a2dd95SBruce Richardson 	if (*dev->dev_ops->mac_addr_set != NULL)
165499a2dd95SBruce Richardson 		(*dev->dev_ops->mac_addr_set)(dev, addr);
165599a2dd95SBruce Richardson 	else if (*dev->dev_ops->mac_addr_add != NULL)
165699a2dd95SBruce Richardson 		(*dev->dev_ops->mac_addr_add)(dev, addr, 0, pool);
165799a2dd95SBruce Richardson 
165899a2dd95SBruce Richardson 	if (*dev->dev_ops->mac_addr_add != NULL) {
165999a2dd95SBruce Richardson 		for (i = 1; i < dev_info->max_mac_addrs; i++) {
166099a2dd95SBruce Richardson 			addr = &dev->data->mac_addrs[i];
166199a2dd95SBruce Richardson 
166299a2dd95SBruce Richardson 			/* skip zero address */
166399a2dd95SBruce Richardson 			if (rte_is_zero_ether_addr(addr))
166499a2dd95SBruce Richardson 				continue;
166599a2dd95SBruce Richardson 
166699a2dd95SBruce Richardson 			pool = 0;
166799a2dd95SBruce Richardson 			pool_mask = dev->data->mac_pool_sel[i];
166899a2dd95SBruce Richardson 
166999a2dd95SBruce Richardson 			do {
167099a2dd95SBruce Richardson 				if (pool_mask & 1ULL)
167199a2dd95SBruce Richardson 					(*dev->dev_ops->mac_addr_add)(dev,
167299a2dd95SBruce Richardson 						addr, i, pool);
167399a2dd95SBruce Richardson 				pool_mask >>= 1;
167499a2dd95SBruce Richardson 				pool++;
167599a2dd95SBruce Richardson 			} while (pool_mask);
167699a2dd95SBruce Richardson 		}
167799a2dd95SBruce Richardson 	}
167899a2dd95SBruce Richardson }
167999a2dd95SBruce Richardson 
168099a2dd95SBruce Richardson static int
168199a2dd95SBruce Richardson eth_dev_config_restore(struct rte_eth_dev *dev,
168299a2dd95SBruce Richardson 		struct rte_eth_dev_info *dev_info, uint16_t port_id)
168399a2dd95SBruce Richardson {
168499a2dd95SBruce Richardson 	int ret;
168599a2dd95SBruce Richardson 
168699a2dd95SBruce Richardson 	if (!(*dev_info->dev_flags & RTE_ETH_DEV_NOLIVE_MAC_ADDR))
168799a2dd95SBruce Richardson 		eth_dev_mac_restore(dev, dev_info);
168899a2dd95SBruce Richardson 
168999a2dd95SBruce Richardson 	/* replay promiscuous configuration */
169099a2dd95SBruce Richardson 	/*
169199a2dd95SBruce Richardson 	 * use callbacks directly since we don't need port_id check and
169299a2dd95SBruce Richardson 	 * would like to bypass the same value set
169399a2dd95SBruce Richardson 	 */
169499a2dd95SBruce Richardson 	if (rte_eth_promiscuous_get(port_id) == 1 &&
169599a2dd95SBruce Richardson 	    *dev->dev_ops->promiscuous_enable != NULL) {
169699a2dd95SBruce Richardson 		ret = eth_err(port_id,
169799a2dd95SBruce Richardson 			      (*dev->dev_ops->promiscuous_enable)(dev));
169899a2dd95SBruce Richardson 		if (ret != 0 && ret != -ENOTSUP) {
169999a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
170099a2dd95SBruce Richardson 				"Failed to enable promiscuous mode for device (port %u): %s\n",
170199a2dd95SBruce Richardson 				port_id, rte_strerror(-ret));
170299a2dd95SBruce Richardson 			return ret;
170399a2dd95SBruce Richardson 		}
170499a2dd95SBruce Richardson 	} else if (rte_eth_promiscuous_get(port_id) == 0 &&
170599a2dd95SBruce Richardson 		   *dev->dev_ops->promiscuous_disable != NULL) {
170699a2dd95SBruce Richardson 		ret = eth_err(port_id,
170799a2dd95SBruce Richardson 			      (*dev->dev_ops->promiscuous_disable)(dev));
170899a2dd95SBruce Richardson 		if (ret != 0 && ret != -ENOTSUP) {
170999a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
171099a2dd95SBruce Richardson 				"Failed to disable promiscuous mode for device (port %u): %s\n",
171199a2dd95SBruce Richardson 				port_id, rte_strerror(-ret));
171299a2dd95SBruce Richardson 			return ret;
171399a2dd95SBruce Richardson 		}
171499a2dd95SBruce Richardson 	}
171599a2dd95SBruce Richardson 
171699a2dd95SBruce Richardson 	/* replay all multicast configuration */
171799a2dd95SBruce Richardson 	/*
171899a2dd95SBruce Richardson 	 * use callbacks directly since we don't need port_id check and
171999a2dd95SBruce Richardson 	 * would like to bypass the same value set
172099a2dd95SBruce Richardson 	 */
172199a2dd95SBruce Richardson 	if (rte_eth_allmulticast_get(port_id) == 1 &&
172299a2dd95SBruce Richardson 	    *dev->dev_ops->allmulticast_enable != NULL) {
172399a2dd95SBruce Richardson 		ret = eth_err(port_id,
172499a2dd95SBruce Richardson 			      (*dev->dev_ops->allmulticast_enable)(dev));
172599a2dd95SBruce Richardson 		if (ret != 0 && ret != -ENOTSUP) {
172699a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
172799a2dd95SBruce Richardson 				"Failed to enable allmulticast mode for device (port %u): %s\n",
172899a2dd95SBruce Richardson 				port_id, rte_strerror(-ret));
172999a2dd95SBruce Richardson 			return ret;
173099a2dd95SBruce Richardson 		}
173199a2dd95SBruce Richardson 	} else if (rte_eth_allmulticast_get(port_id) == 0 &&
173299a2dd95SBruce Richardson 		   *dev->dev_ops->allmulticast_disable != NULL) {
173399a2dd95SBruce Richardson 		ret = eth_err(port_id,
173499a2dd95SBruce Richardson 			      (*dev->dev_ops->allmulticast_disable)(dev));
173599a2dd95SBruce Richardson 		if (ret != 0 && ret != -ENOTSUP) {
173699a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
173799a2dd95SBruce Richardson 				"Failed to disable allmulticast mode for device (port %u): %s\n",
173899a2dd95SBruce Richardson 				port_id, rte_strerror(-ret));
173999a2dd95SBruce Richardson 			return ret;
174099a2dd95SBruce Richardson 		}
174199a2dd95SBruce Richardson 	}
174299a2dd95SBruce Richardson 
174399a2dd95SBruce Richardson 	return 0;
174499a2dd95SBruce Richardson }
174599a2dd95SBruce Richardson 
174699a2dd95SBruce Richardson int
174799a2dd95SBruce Richardson rte_eth_dev_start(uint16_t port_id)
174899a2dd95SBruce Richardson {
174999a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
175099a2dd95SBruce Richardson 	struct rte_eth_dev_info dev_info;
175199a2dd95SBruce Richardson 	int diag;
175299a2dd95SBruce Richardson 	int ret, ret_stop;
175399a2dd95SBruce Richardson 
175499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
175599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
175699a2dd95SBruce Richardson 
175799a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
175899a2dd95SBruce Richardson 
175902edbfabSHuisong Li 	if (dev->data->dev_configured == 0) {
176002edbfabSHuisong Li 		RTE_ETHDEV_LOG(INFO,
176102edbfabSHuisong Li 			"Device with port_id=%"PRIu16" is not configured.\n",
176202edbfabSHuisong Li 			port_id);
176302edbfabSHuisong Li 		return -EINVAL;
176402edbfabSHuisong Li 	}
176502edbfabSHuisong Li 
176699a2dd95SBruce Richardson 	if (dev->data->dev_started != 0) {
176799a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(INFO,
176899a2dd95SBruce Richardson 			"Device with port_id=%"PRIu16" already started\n",
176999a2dd95SBruce Richardson 			port_id);
177099a2dd95SBruce Richardson 		return 0;
177199a2dd95SBruce Richardson 	}
177299a2dd95SBruce Richardson 
177399a2dd95SBruce Richardson 	ret = rte_eth_dev_info_get(port_id, &dev_info);
177499a2dd95SBruce Richardson 	if (ret != 0)
177599a2dd95SBruce Richardson 		return ret;
177699a2dd95SBruce Richardson 
177799a2dd95SBruce Richardson 	/* Lets restore MAC now if device does not support live change */
177899a2dd95SBruce Richardson 	if (*dev_info.dev_flags & RTE_ETH_DEV_NOLIVE_MAC_ADDR)
177999a2dd95SBruce Richardson 		eth_dev_mac_restore(dev, &dev_info);
178099a2dd95SBruce Richardson 
178199a2dd95SBruce Richardson 	diag = (*dev->dev_ops->dev_start)(dev);
178299a2dd95SBruce Richardson 	if (diag == 0)
178399a2dd95SBruce Richardson 		dev->data->dev_started = 1;
178499a2dd95SBruce Richardson 	else
178599a2dd95SBruce Richardson 		return eth_err(port_id, diag);
178699a2dd95SBruce Richardson 
178799a2dd95SBruce Richardson 	ret = eth_dev_config_restore(dev, &dev_info, port_id);
178899a2dd95SBruce Richardson 	if (ret != 0) {
178999a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
179099a2dd95SBruce Richardson 			"Error during restoring configuration for device (port %u): %s\n",
179199a2dd95SBruce Richardson 			port_id, rte_strerror(-ret));
179299a2dd95SBruce Richardson 		ret_stop = rte_eth_dev_stop(port_id);
179399a2dd95SBruce Richardson 		if (ret_stop != 0) {
179499a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
179599a2dd95SBruce Richardson 				"Failed to stop device (port %u): %s\n",
179699a2dd95SBruce Richardson 				port_id, rte_strerror(-ret_stop));
179799a2dd95SBruce Richardson 		}
179899a2dd95SBruce Richardson 
179999a2dd95SBruce Richardson 		return ret;
180099a2dd95SBruce Richardson 	}
180199a2dd95SBruce Richardson 
180299a2dd95SBruce Richardson 	if (dev->data->dev_conf.intr_conf.lsc == 0) {
180399a2dd95SBruce Richardson 		RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->link_update, -ENOTSUP);
180499a2dd95SBruce Richardson 		(*dev->dev_ops->link_update)(dev, 0);
180599a2dd95SBruce Richardson 	}
180699a2dd95SBruce Richardson 
1807c87d435aSKonstantin Ananyev 	/* expose selection of PMD fast-path functions */
1808c87d435aSKonstantin Ananyev 	eth_dev_fp_ops_setup(rte_eth_fp_ops + port_id, dev);
1809c87d435aSKonstantin Ananyev 
181099a2dd95SBruce Richardson 	rte_ethdev_trace_start(port_id);
181199a2dd95SBruce Richardson 	return 0;
181299a2dd95SBruce Richardson }
181399a2dd95SBruce Richardson 
181499a2dd95SBruce Richardson int
181599a2dd95SBruce Richardson rte_eth_dev_stop(uint16_t port_id)
181699a2dd95SBruce Richardson {
181799a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
181899a2dd95SBruce Richardson 	int ret;
181999a2dd95SBruce Richardson 
182099a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
182199a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
182299a2dd95SBruce Richardson 
182399a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_stop, -ENOTSUP);
182499a2dd95SBruce Richardson 
182599a2dd95SBruce Richardson 	if (dev->data->dev_started == 0) {
182699a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(INFO,
182799a2dd95SBruce Richardson 			"Device with port_id=%"PRIu16" already stopped\n",
182899a2dd95SBruce Richardson 			port_id);
182999a2dd95SBruce Richardson 		return 0;
183099a2dd95SBruce Richardson 	}
183199a2dd95SBruce Richardson 
1832c87d435aSKonstantin Ananyev 	/* point fast-path functions to dummy ones */
1833c87d435aSKonstantin Ananyev 	eth_dev_fp_ops_reset(rte_eth_fp_ops + port_id);
1834c87d435aSKonstantin Ananyev 
183599a2dd95SBruce Richardson 	dev->data->dev_started = 0;
183699a2dd95SBruce Richardson 	ret = (*dev->dev_ops->dev_stop)(dev);
183799a2dd95SBruce Richardson 	rte_ethdev_trace_stop(port_id, ret);
183899a2dd95SBruce Richardson 
183999a2dd95SBruce Richardson 	return ret;
184099a2dd95SBruce Richardson }
184199a2dd95SBruce Richardson 
184299a2dd95SBruce Richardson int
184399a2dd95SBruce Richardson rte_eth_dev_set_link_up(uint16_t port_id)
184499a2dd95SBruce Richardson {
184599a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
184699a2dd95SBruce Richardson 
184799a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
184899a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
184999a2dd95SBruce Richardson 
185099a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_set_link_up, -ENOTSUP);
185199a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->dev_set_link_up)(dev));
185299a2dd95SBruce Richardson }
185399a2dd95SBruce Richardson 
185499a2dd95SBruce Richardson int
185599a2dd95SBruce Richardson rte_eth_dev_set_link_down(uint16_t port_id)
185699a2dd95SBruce Richardson {
185799a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
185899a2dd95SBruce Richardson 
185999a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
186099a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
186199a2dd95SBruce Richardson 
186299a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_set_link_down, -ENOTSUP);
186399a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->dev_set_link_down)(dev));
186499a2dd95SBruce Richardson }
186599a2dd95SBruce Richardson 
186699a2dd95SBruce Richardson int
186799a2dd95SBruce Richardson rte_eth_dev_close(uint16_t port_id)
186899a2dd95SBruce Richardson {
186999a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
187099a2dd95SBruce Richardson 	int firsterr, binerr;
187199a2dd95SBruce Richardson 	int *lasterr = &firsterr;
187299a2dd95SBruce Richardson 
187399a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
187499a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
187599a2dd95SBruce Richardson 
187699a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
187799a2dd95SBruce Richardson 	*lasterr = (*dev->dev_ops->dev_close)(dev);
187899a2dd95SBruce Richardson 	if (*lasterr != 0)
187999a2dd95SBruce Richardson 		lasterr = &binerr;
188099a2dd95SBruce Richardson 
188199a2dd95SBruce Richardson 	rte_ethdev_trace_close(port_id);
188299a2dd95SBruce Richardson 	*lasterr = rte_eth_dev_release_port(dev);
188399a2dd95SBruce Richardson 
188499a2dd95SBruce Richardson 	return firsterr;
188599a2dd95SBruce Richardson }
188699a2dd95SBruce Richardson 
188799a2dd95SBruce Richardson int
188899a2dd95SBruce Richardson rte_eth_dev_reset(uint16_t port_id)
188999a2dd95SBruce Richardson {
189099a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
189199a2dd95SBruce Richardson 	int ret;
189299a2dd95SBruce Richardson 
189399a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
189499a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
189599a2dd95SBruce Richardson 
189699a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_reset, -ENOTSUP);
189799a2dd95SBruce Richardson 
189899a2dd95SBruce Richardson 	ret = rte_eth_dev_stop(port_id);
189999a2dd95SBruce Richardson 	if (ret != 0) {
190099a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
190199a2dd95SBruce Richardson 			"Failed to stop device (port %u) before reset: %s - ignore\n",
190299a2dd95SBruce Richardson 			port_id, rte_strerror(-ret));
190399a2dd95SBruce Richardson 	}
190499a2dd95SBruce Richardson 	ret = dev->dev_ops->dev_reset(dev);
190599a2dd95SBruce Richardson 
190699a2dd95SBruce Richardson 	return eth_err(port_id, ret);
190799a2dd95SBruce Richardson }
190899a2dd95SBruce Richardson 
190999a2dd95SBruce Richardson int
191099a2dd95SBruce Richardson rte_eth_dev_is_removed(uint16_t port_id)
191199a2dd95SBruce Richardson {
191299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
191399a2dd95SBruce Richardson 	int ret;
191499a2dd95SBruce Richardson 
191599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, 0);
191699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
191799a2dd95SBruce Richardson 
191899a2dd95SBruce Richardson 	if (dev->state == RTE_ETH_DEV_REMOVED)
191999a2dd95SBruce Richardson 		return 1;
192099a2dd95SBruce Richardson 
192199a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->is_removed, 0);
192299a2dd95SBruce Richardson 
192399a2dd95SBruce Richardson 	ret = dev->dev_ops->is_removed(dev);
192499a2dd95SBruce Richardson 	if (ret != 0)
192599a2dd95SBruce Richardson 		/* Device is physically removed. */
192699a2dd95SBruce Richardson 		dev->state = RTE_ETH_DEV_REMOVED;
192799a2dd95SBruce Richardson 
192899a2dd95SBruce Richardson 	return ret;
192999a2dd95SBruce Richardson }
193099a2dd95SBruce Richardson 
193199a2dd95SBruce Richardson static int
193299a2dd95SBruce Richardson rte_eth_rx_queue_check_split(const struct rte_eth_rxseg_split *rx_seg,
193399a2dd95SBruce Richardson 			     uint16_t n_seg, uint32_t *mbp_buf_size,
193499a2dd95SBruce Richardson 			     const struct rte_eth_dev_info *dev_info)
193599a2dd95SBruce Richardson {
193699a2dd95SBruce Richardson 	const struct rte_eth_rxseg_capa *seg_capa = &dev_info->rx_seg_capa;
193799a2dd95SBruce Richardson 	struct rte_mempool *mp_first;
193899a2dd95SBruce Richardson 	uint32_t offset_mask;
193999a2dd95SBruce Richardson 	uint16_t seg_idx;
194099a2dd95SBruce Richardson 
194199a2dd95SBruce Richardson 	if (n_seg > seg_capa->max_nseg) {
194299a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
194399a2dd95SBruce Richardson 			       "Requested Rx segments %u exceed supported %u\n",
194499a2dd95SBruce Richardson 			       n_seg, seg_capa->max_nseg);
194599a2dd95SBruce Richardson 		return -EINVAL;
194699a2dd95SBruce Richardson 	}
194799a2dd95SBruce Richardson 	/*
194899a2dd95SBruce Richardson 	 * Check the sizes and offsets against buffer sizes
194999a2dd95SBruce Richardson 	 * for each segment specified in extended configuration.
195099a2dd95SBruce Richardson 	 */
195199a2dd95SBruce Richardson 	mp_first = rx_seg[0].mp;
195299a2dd95SBruce Richardson 	offset_mask = (1u << seg_capa->offset_align_log2) - 1;
195399a2dd95SBruce Richardson 	for (seg_idx = 0; seg_idx < n_seg; seg_idx++) {
195499a2dd95SBruce Richardson 		struct rte_mempool *mpl = rx_seg[seg_idx].mp;
195599a2dd95SBruce Richardson 		uint32_t length = rx_seg[seg_idx].length;
195699a2dd95SBruce Richardson 		uint32_t offset = rx_seg[seg_idx].offset;
195799a2dd95SBruce Richardson 
195899a2dd95SBruce Richardson 		if (mpl == NULL) {
195999a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR, "null mempool pointer\n");
196099a2dd95SBruce Richardson 			return -EINVAL;
196199a2dd95SBruce Richardson 		}
196299a2dd95SBruce Richardson 		if (seg_idx != 0 && mp_first != mpl &&
196399a2dd95SBruce Richardson 		    seg_capa->multi_pools == 0) {
196499a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR, "Receiving to multiple pools is not supported\n");
196599a2dd95SBruce Richardson 			return -ENOTSUP;
196699a2dd95SBruce Richardson 		}
196799a2dd95SBruce Richardson 		if (offset != 0) {
196899a2dd95SBruce Richardson 			if (seg_capa->offset_allowed == 0) {
196999a2dd95SBruce Richardson 				RTE_ETHDEV_LOG(ERR, "Rx segmentation with offset is not supported\n");
197099a2dd95SBruce Richardson 				return -ENOTSUP;
197199a2dd95SBruce Richardson 			}
197299a2dd95SBruce Richardson 			if (offset & offset_mask) {
197399a2dd95SBruce Richardson 				RTE_ETHDEV_LOG(ERR, "Rx segmentation invalid offset alignment %u, %u\n",
197499a2dd95SBruce Richardson 					       offset,
197599a2dd95SBruce Richardson 					       seg_capa->offset_align_log2);
197699a2dd95SBruce Richardson 				return -EINVAL;
197799a2dd95SBruce Richardson 			}
197899a2dd95SBruce Richardson 		}
197999a2dd95SBruce Richardson 		if (mpl->private_data_size <
198099a2dd95SBruce Richardson 			sizeof(struct rte_pktmbuf_pool_private)) {
198199a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
198299a2dd95SBruce Richardson 				       "%s private_data_size %u < %u\n",
198399a2dd95SBruce Richardson 				       mpl->name, mpl->private_data_size,
198499a2dd95SBruce Richardson 				       (unsigned int)sizeof
198599a2dd95SBruce Richardson 					(struct rte_pktmbuf_pool_private));
198699a2dd95SBruce Richardson 			return -ENOSPC;
198799a2dd95SBruce Richardson 		}
198899a2dd95SBruce Richardson 		offset += seg_idx != 0 ? 0 : RTE_PKTMBUF_HEADROOM;
198999a2dd95SBruce Richardson 		*mbp_buf_size = rte_pktmbuf_data_room_size(mpl);
199099a2dd95SBruce Richardson 		length = length != 0 ? length : *mbp_buf_size;
199199a2dd95SBruce Richardson 		if (*mbp_buf_size < length + offset) {
199299a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
199399a2dd95SBruce Richardson 				       "%s mbuf_data_room_size %u < %u (segment length=%u + segment offset=%u)\n",
199499a2dd95SBruce Richardson 				       mpl->name, *mbp_buf_size,
199599a2dd95SBruce Richardson 				       length + offset, length, offset);
199699a2dd95SBruce Richardson 			return -EINVAL;
199799a2dd95SBruce Richardson 		}
199899a2dd95SBruce Richardson 	}
199999a2dd95SBruce Richardson 	return 0;
200099a2dd95SBruce Richardson }
200199a2dd95SBruce Richardson 
200299a2dd95SBruce Richardson int
200399a2dd95SBruce Richardson rte_eth_rx_queue_setup(uint16_t port_id, uint16_t rx_queue_id,
200499a2dd95SBruce Richardson 		       uint16_t nb_rx_desc, unsigned int socket_id,
200599a2dd95SBruce Richardson 		       const struct rte_eth_rxconf *rx_conf,
200699a2dd95SBruce Richardson 		       struct rte_mempool *mp)
200799a2dd95SBruce Richardson {
200899a2dd95SBruce Richardson 	int ret;
200999a2dd95SBruce Richardson 	uint32_t mbp_buf_size;
201099a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
201199a2dd95SBruce Richardson 	struct rte_eth_dev_info dev_info;
201299a2dd95SBruce Richardson 	struct rte_eth_rxconf local_conf;
201399a2dd95SBruce Richardson 
201499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
201599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
201653ef1b34SMin Hu (Connor) 
201799a2dd95SBruce Richardson 	if (rx_queue_id >= dev->data->nb_rx_queues) {
201899a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid RX queue_id=%u\n", rx_queue_id);
201999a2dd95SBruce Richardson 		return -EINVAL;
202099a2dd95SBruce Richardson 	}
202199a2dd95SBruce Richardson 
202299a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_setup, -ENOTSUP);
202399a2dd95SBruce Richardson 
202499a2dd95SBruce Richardson 	ret = rte_eth_dev_info_get(port_id, &dev_info);
202599a2dd95SBruce Richardson 	if (ret != 0)
202699a2dd95SBruce Richardson 		return ret;
202799a2dd95SBruce Richardson 
202899a2dd95SBruce Richardson 	if (mp != NULL) {
202999a2dd95SBruce Richardson 		/* Single pool configuration check. */
203099a2dd95SBruce Richardson 		if (rx_conf != NULL && rx_conf->rx_nseg != 0) {
203199a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
203299a2dd95SBruce Richardson 				       "Ambiguous segment configuration\n");
203399a2dd95SBruce Richardson 			return -EINVAL;
203499a2dd95SBruce Richardson 		}
203599a2dd95SBruce Richardson 		/*
203699a2dd95SBruce Richardson 		 * Check the size of the mbuf data buffer, this value
203799a2dd95SBruce Richardson 		 * must be provided in the private data of the memory pool.
203899a2dd95SBruce Richardson 		 * First check that the memory pool(s) has a valid private data.
203999a2dd95SBruce Richardson 		 */
204099a2dd95SBruce Richardson 		if (mp->private_data_size <
204199a2dd95SBruce Richardson 				sizeof(struct rte_pktmbuf_pool_private)) {
204299a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR, "%s private_data_size %u < %u\n",
204399a2dd95SBruce Richardson 				mp->name, mp->private_data_size,
204499a2dd95SBruce Richardson 				(unsigned int)
204599a2dd95SBruce Richardson 				sizeof(struct rte_pktmbuf_pool_private));
204699a2dd95SBruce Richardson 			return -ENOSPC;
204799a2dd95SBruce Richardson 		}
204899a2dd95SBruce Richardson 		mbp_buf_size = rte_pktmbuf_data_room_size(mp);
204999a2dd95SBruce Richardson 		if (mbp_buf_size < dev_info.min_rx_bufsize +
205099a2dd95SBruce Richardson 				   RTE_PKTMBUF_HEADROOM) {
205199a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
205299a2dd95SBruce Richardson 				       "%s mbuf_data_room_size %u < %u (RTE_PKTMBUF_HEADROOM=%u + min_rx_bufsize(dev)=%u)\n",
205399a2dd95SBruce Richardson 				       mp->name, mbp_buf_size,
205499a2dd95SBruce Richardson 				       RTE_PKTMBUF_HEADROOM +
205599a2dd95SBruce Richardson 				       dev_info.min_rx_bufsize,
205699a2dd95SBruce Richardson 				       RTE_PKTMBUF_HEADROOM,
205799a2dd95SBruce Richardson 				       dev_info.min_rx_bufsize);
205899a2dd95SBruce Richardson 			return -EINVAL;
205999a2dd95SBruce Richardson 		}
206099a2dd95SBruce Richardson 	} else {
206199a2dd95SBruce Richardson 		const struct rte_eth_rxseg_split *rx_seg;
206299a2dd95SBruce Richardson 		uint16_t n_seg;
206399a2dd95SBruce Richardson 
206499a2dd95SBruce Richardson 		/* Extended multi-segment configuration check. */
206599a2dd95SBruce Richardson 		if (rx_conf == NULL || rx_conf->rx_seg == NULL || rx_conf->rx_nseg == 0) {
206699a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
206799a2dd95SBruce Richardson 				       "Memory pool is null and no extended configuration provided\n");
206899a2dd95SBruce Richardson 			return -EINVAL;
206999a2dd95SBruce Richardson 		}
207099a2dd95SBruce Richardson 
207199a2dd95SBruce Richardson 		rx_seg = (const struct rte_eth_rxseg_split *)rx_conf->rx_seg;
207299a2dd95SBruce Richardson 		n_seg = rx_conf->rx_nseg;
207399a2dd95SBruce Richardson 
207499a2dd95SBruce Richardson 		if (rx_conf->offloads & RTE_ETH_RX_OFFLOAD_BUFFER_SPLIT) {
207599a2dd95SBruce Richardson 			ret = rte_eth_rx_queue_check_split(rx_seg, n_seg,
207699a2dd95SBruce Richardson 							   &mbp_buf_size,
207799a2dd95SBruce Richardson 							   &dev_info);
207899a2dd95SBruce Richardson 			if (ret != 0)
207999a2dd95SBruce Richardson 				return ret;
208099a2dd95SBruce Richardson 		} else {
208199a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR, "No Rx segmentation offload configured\n");
208299a2dd95SBruce Richardson 			return -EINVAL;
208399a2dd95SBruce Richardson 		}
208499a2dd95SBruce Richardson 	}
208599a2dd95SBruce Richardson 
208699a2dd95SBruce Richardson 	/* Use default specified by driver, if nb_rx_desc is zero */
208799a2dd95SBruce Richardson 	if (nb_rx_desc == 0) {
208899a2dd95SBruce Richardson 		nb_rx_desc = dev_info.default_rxportconf.ring_size;
208999a2dd95SBruce Richardson 		/* If driver default is also zero, fall back on EAL default */
209099a2dd95SBruce Richardson 		if (nb_rx_desc == 0)
209199a2dd95SBruce Richardson 			nb_rx_desc = RTE_ETH_DEV_FALLBACK_RX_RINGSIZE;
209299a2dd95SBruce Richardson 	}
209399a2dd95SBruce Richardson 
209499a2dd95SBruce Richardson 	if (nb_rx_desc > dev_info.rx_desc_lim.nb_max ||
209599a2dd95SBruce Richardson 			nb_rx_desc < dev_info.rx_desc_lim.nb_min ||
209699a2dd95SBruce Richardson 			nb_rx_desc % dev_info.rx_desc_lim.nb_align != 0) {
209799a2dd95SBruce Richardson 
209899a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
209999a2dd95SBruce Richardson 			"Invalid value for nb_rx_desc(=%hu), should be: <= %hu, >= %hu, and a product of %hu\n",
210099a2dd95SBruce Richardson 			nb_rx_desc, dev_info.rx_desc_lim.nb_max,
210199a2dd95SBruce Richardson 			dev_info.rx_desc_lim.nb_min,
210299a2dd95SBruce Richardson 			dev_info.rx_desc_lim.nb_align);
210399a2dd95SBruce Richardson 		return -EINVAL;
210499a2dd95SBruce Richardson 	}
210599a2dd95SBruce Richardson 
210699a2dd95SBruce Richardson 	if (dev->data->dev_started &&
210799a2dd95SBruce Richardson 		!(dev_info.dev_capa &
210899a2dd95SBruce Richardson 			RTE_ETH_DEV_CAPA_RUNTIME_RX_QUEUE_SETUP))
210999a2dd95SBruce Richardson 		return -EBUSY;
211099a2dd95SBruce Richardson 
211199a2dd95SBruce Richardson 	if (dev->data->dev_started &&
211299a2dd95SBruce Richardson 		(dev->data->rx_queue_state[rx_queue_id] !=
211399a2dd95SBruce Richardson 			RTE_ETH_QUEUE_STATE_STOPPED))
211499a2dd95SBruce Richardson 		return -EBUSY;
211599a2dd95SBruce Richardson 
211649ed3224SXueming Li 	eth_dev_rxq_release(dev, rx_queue_id);
211799a2dd95SBruce Richardson 
211899a2dd95SBruce Richardson 	if (rx_conf == NULL)
211999a2dd95SBruce Richardson 		rx_conf = &dev_info.default_rxconf;
212099a2dd95SBruce Richardson 
212199a2dd95SBruce Richardson 	local_conf = *rx_conf;
212299a2dd95SBruce Richardson 
212399a2dd95SBruce Richardson 	/*
212499a2dd95SBruce Richardson 	 * If an offloading has already been enabled in
212599a2dd95SBruce Richardson 	 * rte_eth_dev_configure(), it has been enabled on all queues,
212699a2dd95SBruce Richardson 	 * so there is no need to enable it in this queue again.
212799a2dd95SBruce Richardson 	 * The local_conf.offloads input to underlying PMD only carries
212899a2dd95SBruce Richardson 	 * those offloadings which are only enabled on this queue and
212999a2dd95SBruce Richardson 	 * not enabled on all queues.
213099a2dd95SBruce Richardson 	 */
213199a2dd95SBruce Richardson 	local_conf.offloads &= ~dev->data->dev_conf.rxmode.offloads;
213299a2dd95SBruce Richardson 
213399a2dd95SBruce Richardson 	/*
213499a2dd95SBruce Richardson 	 * New added offloadings for this queue are those not enabled in
213599a2dd95SBruce Richardson 	 * rte_eth_dev_configure() and they must be per-queue type.
213699a2dd95SBruce Richardson 	 * A pure per-port offloading can't be enabled on a queue while
213799a2dd95SBruce Richardson 	 * disabled on another queue. A pure per-port offloading can't
213899a2dd95SBruce Richardson 	 * be enabled for any queue as new added one if it hasn't been
213999a2dd95SBruce Richardson 	 * enabled in rte_eth_dev_configure().
214099a2dd95SBruce Richardson 	 */
214199a2dd95SBruce Richardson 	if ((local_conf.offloads & dev_info.rx_queue_offload_capa) !=
214299a2dd95SBruce Richardson 	     local_conf.offloads) {
214399a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
214499a2dd95SBruce Richardson 			"Ethdev port_id=%d rx_queue_id=%d, new added offloads 0x%"PRIx64" must be "
214599a2dd95SBruce Richardson 			"within per-queue offload capabilities 0x%"PRIx64" in %s()\n",
214699a2dd95SBruce Richardson 			port_id, rx_queue_id, local_conf.offloads,
214799a2dd95SBruce Richardson 			dev_info.rx_queue_offload_capa,
214899a2dd95SBruce Richardson 			__func__);
214999a2dd95SBruce Richardson 		return -EINVAL;
215099a2dd95SBruce Richardson 	}
215199a2dd95SBruce Richardson 
215299a2dd95SBruce Richardson 	/*
215399a2dd95SBruce Richardson 	 * If LRO is enabled, check that the maximum aggregated packet
215499a2dd95SBruce Richardson 	 * size is supported by the configured device.
215599a2dd95SBruce Richardson 	 */
21561bb4a528SFerruh Yigit 	/* Get the real Ethernet overhead length */
215799a2dd95SBruce Richardson 	if (local_conf.offloads & DEV_RX_OFFLOAD_TCP_LRO) {
21581bb4a528SFerruh Yigit 		uint32_t overhead_len;
21591bb4a528SFerruh Yigit 		uint32_t max_rx_pktlen;
21601bb4a528SFerruh Yigit 		int ret;
21611bb4a528SFerruh Yigit 
21621bb4a528SFerruh Yigit 		overhead_len = eth_dev_get_overhead_len(dev_info.max_rx_pktlen,
21631bb4a528SFerruh Yigit 				dev_info.max_mtu);
21641bb4a528SFerruh Yigit 		max_rx_pktlen = dev->data->mtu + overhead_len;
216599a2dd95SBruce Richardson 		if (dev->data->dev_conf.rxmode.max_lro_pkt_size == 0)
21661bb4a528SFerruh Yigit 			dev->data->dev_conf.rxmode.max_lro_pkt_size = max_rx_pktlen;
21671bb4a528SFerruh Yigit 		ret = eth_dev_check_lro_pkt_size(port_id,
216899a2dd95SBruce Richardson 				dev->data->dev_conf.rxmode.max_lro_pkt_size,
21691bb4a528SFerruh Yigit 				max_rx_pktlen,
217099a2dd95SBruce Richardson 				dev_info.max_lro_pkt_size);
217199a2dd95SBruce Richardson 		if (ret != 0)
217299a2dd95SBruce Richardson 			return ret;
217399a2dd95SBruce Richardson 	}
217499a2dd95SBruce Richardson 
217599a2dd95SBruce Richardson 	ret = (*dev->dev_ops->rx_queue_setup)(dev, rx_queue_id, nb_rx_desc,
217699a2dd95SBruce Richardson 					      socket_id, &local_conf, mp);
217799a2dd95SBruce Richardson 	if (!ret) {
217899a2dd95SBruce Richardson 		if (!dev->data->min_rx_buf_size ||
217999a2dd95SBruce Richardson 		    dev->data->min_rx_buf_size > mbp_buf_size)
218099a2dd95SBruce Richardson 			dev->data->min_rx_buf_size = mbp_buf_size;
218199a2dd95SBruce Richardson 	}
218299a2dd95SBruce Richardson 
218399a2dd95SBruce Richardson 	rte_ethdev_trace_rxq_setup(port_id, rx_queue_id, nb_rx_desc, mp,
218499a2dd95SBruce Richardson 		rx_conf, ret);
218599a2dd95SBruce Richardson 	return eth_err(port_id, ret);
218699a2dd95SBruce Richardson }
218799a2dd95SBruce Richardson 
218899a2dd95SBruce Richardson int
218999a2dd95SBruce Richardson rte_eth_rx_hairpin_queue_setup(uint16_t port_id, uint16_t rx_queue_id,
219099a2dd95SBruce Richardson 			       uint16_t nb_rx_desc,
219199a2dd95SBruce Richardson 			       const struct rte_eth_hairpin_conf *conf)
219299a2dd95SBruce Richardson {
219399a2dd95SBruce Richardson 	int ret;
219499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
219599a2dd95SBruce Richardson 	struct rte_eth_hairpin_cap cap;
219699a2dd95SBruce Richardson 	int i;
219799a2dd95SBruce Richardson 	int count;
219899a2dd95SBruce Richardson 
219999a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
220099a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
220153ef1b34SMin Hu (Connor) 
220299a2dd95SBruce Richardson 	if (rx_queue_id >= dev->data->nb_rx_queues) {
220399a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid RX queue_id=%u\n", rx_queue_id);
220499a2dd95SBruce Richardson 		return -EINVAL;
220599a2dd95SBruce Richardson 	}
220653ef1b34SMin Hu (Connor) 
220753ef1b34SMin Hu (Connor) 	if (conf == NULL) {
220853ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
220953ef1b34SMin Hu (Connor) 			"Cannot setup ethdev port %u Rx hairpin queue from NULL config\n",
221053ef1b34SMin Hu (Connor) 			port_id);
221153ef1b34SMin Hu (Connor) 		return -EINVAL;
221253ef1b34SMin Hu (Connor) 	}
221353ef1b34SMin Hu (Connor) 
221499a2dd95SBruce Richardson 	ret = rte_eth_dev_hairpin_capability_get(port_id, &cap);
221599a2dd95SBruce Richardson 	if (ret != 0)
221699a2dd95SBruce Richardson 		return ret;
221799a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_hairpin_queue_setup,
221899a2dd95SBruce Richardson 				-ENOTSUP);
221999a2dd95SBruce Richardson 	/* if nb_rx_desc is zero use max number of desc from the driver. */
222099a2dd95SBruce Richardson 	if (nb_rx_desc == 0)
222199a2dd95SBruce Richardson 		nb_rx_desc = cap.max_nb_desc;
222299a2dd95SBruce Richardson 	if (nb_rx_desc > cap.max_nb_desc) {
222399a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
222499a2dd95SBruce Richardson 			"Invalid value for nb_rx_desc(=%hu), should be: <= %hu",
222599a2dd95SBruce Richardson 			nb_rx_desc, cap.max_nb_desc);
222699a2dd95SBruce Richardson 		return -EINVAL;
222799a2dd95SBruce Richardson 	}
222899a2dd95SBruce Richardson 	if (conf->peer_count > cap.max_rx_2_tx) {
222999a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
223099a2dd95SBruce Richardson 			"Invalid value for number of peers for Rx queue(=%u), should be: <= %hu",
223199a2dd95SBruce Richardson 			conf->peer_count, cap.max_rx_2_tx);
223299a2dd95SBruce Richardson 		return -EINVAL;
223399a2dd95SBruce Richardson 	}
223499a2dd95SBruce Richardson 	if (conf->peer_count == 0) {
223599a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
223699a2dd95SBruce Richardson 			"Invalid value for number of peers for Rx queue(=%u), should be: > 0",
223799a2dd95SBruce Richardson 			conf->peer_count);
223899a2dd95SBruce Richardson 		return -EINVAL;
223999a2dd95SBruce Richardson 	}
224099a2dd95SBruce Richardson 	for (i = 0, count = 0; i < dev->data->nb_rx_queues &&
224199a2dd95SBruce Richardson 	     cap.max_nb_queues != UINT16_MAX; i++) {
224299a2dd95SBruce Richardson 		if (i == rx_queue_id || rte_eth_dev_is_rx_hairpin_queue(dev, i))
224399a2dd95SBruce Richardson 			count++;
224499a2dd95SBruce Richardson 	}
224599a2dd95SBruce Richardson 	if (count > cap.max_nb_queues) {
224699a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "To many Rx hairpin queues max is %d",
224799a2dd95SBruce Richardson 		cap.max_nb_queues);
224899a2dd95SBruce Richardson 		return -EINVAL;
224999a2dd95SBruce Richardson 	}
225099a2dd95SBruce Richardson 	if (dev->data->dev_started)
225199a2dd95SBruce Richardson 		return -EBUSY;
225249ed3224SXueming Li 	eth_dev_rxq_release(dev, rx_queue_id);
225399a2dd95SBruce Richardson 	ret = (*dev->dev_ops->rx_hairpin_queue_setup)(dev, rx_queue_id,
225499a2dd95SBruce Richardson 						      nb_rx_desc, conf);
225599a2dd95SBruce Richardson 	if (ret == 0)
225699a2dd95SBruce Richardson 		dev->data->rx_queue_state[rx_queue_id] =
225799a2dd95SBruce Richardson 			RTE_ETH_QUEUE_STATE_HAIRPIN;
225899a2dd95SBruce Richardson 	return eth_err(port_id, ret);
225999a2dd95SBruce Richardson }
226099a2dd95SBruce Richardson 
226199a2dd95SBruce Richardson int
226299a2dd95SBruce Richardson rte_eth_tx_queue_setup(uint16_t port_id, uint16_t tx_queue_id,
226399a2dd95SBruce Richardson 		       uint16_t nb_tx_desc, unsigned int socket_id,
226499a2dd95SBruce Richardson 		       const struct rte_eth_txconf *tx_conf)
226599a2dd95SBruce Richardson {
226699a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
226799a2dd95SBruce Richardson 	struct rte_eth_dev_info dev_info;
226899a2dd95SBruce Richardson 	struct rte_eth_txconf local_conf;
226999a2dd95SBruce Richardson 	int ret;
227099a2dd95SBruce Richardson 
227199a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
227299a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
227353ef1b34SMin Hu (Connor) 
227499a2dd95SBruce Richardson 	if (tx_queue_id >= dev->data->nb_tx_queues) {
227599a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid TX queue_id=%u\n", tx_queue_id);
227699a2dd95SBruce Richardson 		return -EINVAL;
227799a2dd95SBruce Richardson 	}
227899a2dd95SBruce Richardson 
227999a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_setup, -ENOTSUP);
228099a2dd95SBruce Richardson 
228199a2dd95SBruce Richardson 	ret = rte_eth_dev_info_get(port_id, &dev_info);
228299a2dd95SBruce Richardson 	if (ret != 0)
228399a2dd95SBruce Richardson 		return ret;
228499a2dd95SBruce Richardson 
228599a2dd95SBruce Richardson 	/* Use default specified by driver, if nb_tx_desc is zero */
228699a2dd95SBruce Richardson 	if (nb_tx_desc == 0) {
228799a2dd95SBruce Richardson 		nb_tx_desc = dev_info.default_txportconf.ring_size;
228899a2dd95SBruce Richardson 		/* If driver default is zero, fall back on EAL default */
228999a2dd95SBruce Richardson 		if (nb_tx_desc == 0)
229099a2dd95SBruce Richardson 			nb_tx_desc = RTE_ETH_DEV_FALLBACK_TX_RINGSIZE;
229199a2dd95SBruce Richardson 	}
229299a2dd95SBruce Richardson 	if (nb_tx_desc > dev_info.tx_desc_lim.nb_max ||
229399a2dd95SBruce Richardson 	    nb_tx_desc < dev_info.tx_desc_lim.nb_min ||
229499a2dd95SBruce Richardson 	    nb_tx_desc % dev_info.tx_desc_lim.nb_align != 0) {
229599a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
229699a2dd95SBruce Richardson 			"Invalid value for nb_tx_desc(=%hu), should be: <= %hu, >= %hu, and a product of %hu\n",
229799a2dd95SBruce Richardson 			nb_tx_desc, dev_info.tx_desc_lim.nb_max,
229899a2dd95SBruce Richardson 			dev_info.tx_desc_lim.nb_min,
229999a2dd95SBruce Richardson 			dev_info.tx_desc_lim.nb_align);
230099a2dd95SBruce Richardson 		return -EINVAL;
230199a2dd95SBruce Richardson 	}
230299a2dd95SBruce Richardson 
230399a2dd95SBruce Richardson 	if (dev->data->dev_started &&
230499a2dd95SBruce Richardson 		!(dev_info.dev_capa &
230599a2dd95SBruce Richardson 			RTE_ETH_DEV_CAPA_RUNTIME_TX_QUEUE_SETUP))
230699a2dd95SBruce Richardson 		return -EBUSY;
230799a2dd95SBruce Richardson 
230899a2dd95SBruce Richardson 	if (dev->data->dev_started &&
230999a2dd95SBruce Richardson 		(dev->data->tx_queue_state[tx_queue_id] !=
231099a2dd95SBruce Richardson 			RTE_ETH_QUEUE_STATE_STOPPED))
231199a2dd95SBruce Richardson 		return -EBUSY;
231299a2dd95SBruce Richardson 
231349ed3224SXueming Li 	eth_dev_txq_release(dev, tx_queue_id);
231499a2dd95SBruce Richardson 
231599a2dd95SBruce Richardson 	if (tx_conf == NULL)
231699a2dd95SBruce Richardson 		tx_conf = &dev_info.default_txconf;
231799a2dd95SBruce Richardson 
231899a2dd95SBruce Richardson 	local_conf = *tx_conf;
231999a2dd95SBruce Richardson 
232099a2dd95SBruce Richardson 	/*
232199a2dd95SBruce Richardson 	 * If an offloading has already been enabled in
232299a2dd95SBruce Richardson 	 * rte_eth_dev_configure(), it has been enabled on all queues,
232399a2dd95SBruce Richardson 	 * so there is no need to enable it in this queue again.
232499a2dd95SBruce Richardson 	 * The local_conf.offloads input to underlying PMD only carries
232599a2dd95SBruce Richardson 	 * those offloadings which are only enabled on this queue and
232699a2dd95SBruce Richardson 	 * not enabled on all queues.
232799a2dd95SBruce Richardson 	 */
232899a2dd95SBruce Richardson 	local_conf.offloads &= ~dev->data->dev_conf.txmode.offloads;
232999a2dd95SBruce Richardson 
233099a2dd95SBruce Richardson 	/*
233199a2dd95SBruce Richardson 	 * New added offloadings for this queue are those not enabled in
233299a2dd95SBruce Richardson 	 * rte_eth_dev_configure() and they must be per-queue type.
233399a2dd95SBruce Richardson 	 * A pure per-port offloading can't be enabled on a queue while
233499a2dd95SBruce Richardson 	 * disabled on another queue. A pure per-port offloading can't
233599a2dd95SBruce Richardson 	 * be enabled for any queue as new added one if it hasn't been
233699a2dd95SBruce Richardson 	 * enabled in rte_eth_dev_configure().
233799a2dd95SBruce Richardson 	 */
233899a2dd95SBruce Richardson 	if ((local_conf.offloads & dev_info.tx_queue_offload_capa) !=
233999a2dd95SBruce Richardson 	     local_conf.offloads) {
234099a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
234199a2dd95SBruce Richardson 			"Ethdev port_id=%d tx_queue_id=%d, new added offloads 0x%"PRIx64" must be "
234299a2dd95SBruce Richardson 			"within per-queue offload capabilities 0x%"PRIx64" in %s()\n",
234399a2dd95SBruce Richardson 			port_id, tx_queue_id, local_conf.offloads,
234499a2dd95SBruce Richardson 			dev_info.tx_queue_offload_capa,
234599a2dd95SBruce Richardson 			__func__);
234699a2dd95SBruce Richardson 		return -EINVAL;
234799a2dd95SBruce Richardson 	}
234899a2dd95SBruce Richardson 
234999a2dd95SBruce Richardson 	rte_ethdev_trace_txq_setup(port_id, tx_queue_id, nb_tx_desc, tx_conf);
235099a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->tx_queue_setup)(dev,
235199a2dd95SBruce Richardson 		       tx_queue_id, nb_tx_desc, socket_id, &local_conf));
235299a2dd95SBruce Richardson }
235399a2dd95SBruce Richardson 
235499a2dd95SBruce Richardson int
235599a2dd95SBruce Richardson rte_eth_tx_hairpin_queue_setup(uint16_t port_id, uint16_t tx_queue_id,
235699a2dd95SBruce Richardson 			       uint16_t nb_tx_desc,
235799a2dd95SBruce Richardson 			       const struct rte_eth_hairpin_conf *conf)
235899a2dd95SBruce Richardson {
235999a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
236099a2dd95SBruce Richardson 	struct rte_eth_hairpin_cap cap;
236199a2dd95SBruce Richardson 	int i;
236299a2dd95SBruce Richardson 	int count;
236399a2dd95SBruce Richardson 	int ret;
236499a2dd95SBruce Richardson 
236599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
236699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
236753ef1b34SMin Hu (Connor) 
236899a2dd95SBruce Richardson 	if (tx_queue_id >= dev->data->nb_tx_queues) {
236999a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid TX queue_id=%u\n", tx_queue_id);
237099a2dd95SBruce Richardson 		return -EINVAL;
237199a2dd95SBruce Richardson 	}
237253ef1b34SMin Hu (Connor) 
237353ef1b34SMin Hu (Connor) 	if (conf == NULL) {
237453ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
237553ef1b34SMin Hu (Connor) 			"Cannot setup ethdev port %u Tx hairpin queue from NULL config\n",
237653ef1b34SMin Hu (Connor) 			port_id);
237753ef1b34SMin Hu (Connor) 		return -EINVAL;
237853ef1b34SMin Hu (Connor) 	}
237953ef1b34SMin Hu (Connor) 
238099a2dd95SBruce Richardson 	ret = rte_eth_dev_hairpin_capability_get(port_id, &cap);
238199a2dd95SBruce Richardson 	if (ret != 0)
238299a2dd95SBruce Richardson 		return ret;
238399a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_hairpin_queue_setup,
238499a2dd95SBruce Richardson 				-ENOTSUP);
238599a2dd95SBruce Richardson 	/* if nb_rx_desc is zero use max number of desc from the driver. */
238699a2dd95SBruce Richardson 	if (nb_tx_desc == 0)
238799a2dd95SBruce Richardson 		nb_tx_desc = cap.max_nb_desc;
238899a2dd95SBruce Richardson 	if (nb_tx_desc > cap.max_nb_desc) {
238999a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
239099a2dd95SBruce Richardson 			"Invalid value for nb_tx_desc(=%hu), should be: <= %hu",
239199a2dd95SBruce Richardson 			nb_tx_desc, cap.max_nb_desc);
239299a2dd95SBruce Richardson 		return -EINVAL;
239399a2dd95SBruce Richardson 	}
239499a2dd95SBruce Richardson 	if (conf->peer_count > cap.max_tx_2_rx) {
239599a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
239699a2dd95SBruce Richardson 			"Invalid value for number of peers for Tx queue(=%u), should be: <= %hu",
239799a2dd95SBruce Richardson 			conf->peer_count, cap.max_tx_2_rx);
239899a2dd95SBruce Richardson 		return -EINVAL;
239999a2dd95SBruce Richardson 	}
240099a2dd95SBruce Richardson 	if (conf->peer_count == 0) {
240199a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
240299a2dd95SBruce Richardson 			"Invalid value for number of peers for Tx queue(=%u), should be: > 0",
240399a2dd95SBruce Richardson 			conf->peer_count);
240499a2dd95SBruce Richardson 		return -EINVAL;
240599a2dd95SBruce Richardson 	}
240699a2dd95SBruce Richardson 	for (i = 0, count = 0; i < dev->data->nb_tx_queues &&
240799a2dd95SBruce Richardson 	     cap.max_nb_queues != UINT16_MAX; i++) {
240899a2dd95SBruce Richardson 		if (i == tx_queue_id || rte_eth_dev_is_tx_hairpin_queue(dev, i))
240999a2dd95SBruce Richardson 			count++;
241099a2dd95SBruce Richardson 	}
241199a2dd95SBruce Richardson 	if (count > cap.max_nb_queues) {
241299a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "To many Tx hairpin queues max is %d",
241399a2dd95SBruce Richardson 		cap.max_nb_queues);
241499a2dd95SBruce Richardson 		return -EINVAL;
241599a2dd95SBruce Richardson 	}
241699a2dd95SBruce Richardson 	if (dev->data->dev_started)
241799a2dd95SBruce Richardson 		return -EBUSY;
241849ed3224SXueming Li 	eth_dev_txq_release(dev, tx_queue_id);
241999a2dd95SBruce Richardson 	ret = (*dev->dev_ops->tx_hairpin_queue_setup)
242099a2dd95SBruce Richardson 		(dev, tx_queue_id, nb_tx_desc, conf);
242199a2dd95SBruce Richardson 	if (ret == 0)
242299a2dd95SBruce Richardson 		dev->data->tx_queue_state[tx_queue_id] =
242399a2dd95SBruce Richardson 			RTE_ETH_QUEUE_STATE_HAIRPIN;
242499a2dd95SBruce Richardson 	return eth_err(port_id, ret);
242599a2dd95SBruce Richardson }
242699a2dd95SBruce Richardson 
242799a2dd95SBruce Richardson int
242899a2dd95SBruce Richardson rte_eth_hairpin_bind(uint16_t tx_port, uint16_t rx_port)
242999a2dd95SBruce Richardson {
243099a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
243199a2dd95SBruce Richardson 	int ret;
243299a2dd95SBruce Richardson 
243399a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(tx_port, -ENODEV);
243499a2dd95SBruce Richardson 	dev = &rte_eth_devices[tx_port];
243553ef1b34SMin Hu (Connor) 
243699a2dd95SBruce Richardson 	if (dev->data->dev_started == 0) {
243799a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Tx port %d is not started\n", tx_port);
243899a2dd95SBruce Richardson 		return -EBUSY;
243999a2dd95SBruce Richardson 	}
244099a2dd95SBruce Richardson 
244199a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->hairpin_bind, -ENOTSUP);
244299a2dd95SBruce Richardson 	ret = (*dev->dev_ops->hairpin_bind)(dev, rx_port);
244399a2dd95SBruce Richardson 	if (ret != 0)
244499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Failed to bind hairpin Tx %d"
244599a2dd95SBruce Richardson 			       " to Rx %d (%d - all ports)\n",
244699a2dd95SBruce Richardson 			       tx_port, rx_port, RTE_MAX_ETHPORTS);
244799a2dd95SBruce Richardson 
244899a2dd95SBruce Richardson 	return ret;
244999a2dd95SBruce Richardson }
245099a2dd95SBruce Richardson 
245199a2dd95SBruce Richardson int
245299a2dd95SBruce Richardson rte_eth_hairpin_unbind(uint16_t tx_port, uint16_t rx_port)
245399a2dd95SBruce Richardson {
245499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
245599a2dd95SBruce Richardson 	int ret;
245699a2dd95SBruce Richardson 
245799a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(tx_port, -ENODEV);
245899a2dd95SBruce Richardson 	dev = &rte_eth_devices[tx_port];
245953ef1b34SMin Hu (Connor) 
246099a2dd95SBruce Richardson 	if (dev->data->dev_started == 0) {
246199a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Tx port %d is already stopped\n", tx_port);
246299a2dd95SBruce Richardson 		return -EBUSY;
246399a2dd95SBruce Richardson 	}
246499a2dd95SBruce Richardson 
246599a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->hairpin_unbind, -ENOTSUP);
246699a2dd95SBruce Richardson 	ret = (*dev->dev_ops->hairpin_unbind)(dev, rx_port);
246799a2dd95SBruce Richardson 	if (ret != 0)
246899a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Failed to unbind hairpin Tx %d"
246999a2dd95SBruce Richardson 			       " from Rx %d (%d - all ports)\n",
247099a2dd95SBruce Richardson 			       tx_port, rx_port, RTE_MAX_ETHPORTS);
247199a2dd95SBruce Richardson 
247299a2dd95SBruce Richardson 	return ret;
247399a2dd95SBruce Richardson }
247499a2dd95SBruce Richardson 
247599a2dd95SBruce Richardson int
247699a2dd95SBruce Richardson rte_eth_hairpin_get_peer_ports(uint16_t port_id, uint16_t *peer_ports,
247799a2dd95SBruce Richardson 			       size_t len, uint32_t direction)
247899a2dd95SBruce Richardson {
247999a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
248099a2dd95SBruce Richardson 	int ret;
248199a2dd95SBruce Richardson 
248299a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
248399a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
248453ef1b34SMin Hu (Connor) 
248553ef1b34SMin Hu (Connor) 	if (peer_ports == NULL) {
248653ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
248753ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u hairpin peer ports to NULL\n",
248853ef1b34SMin Hu (Connor) 			port_id);
248953ef1b34SMin Hu (Connor) 		return -EINVAL;
249053ef1b34SMin Hu (Connor) 	}
249153ef1b34SMin Hu (Connor) 
249253ef1b34SMin Hu (Connor) 	if (len == 0) {
249353ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
249453ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u hairpin peer ports to array with zero size\n",
249553ef1b34SMin Hu (Connor) 			port_id);
249653ef1b34SMin Hu (Connor) 		return -EINVAL;
249753ef1b34SMin Hu (Connor) 	}
249853ef1b34SMin Hu (Connor) 
249999a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->hairpin_get_peer_ports,
250099a2dd95SBruce Richardson 				-ENOTSUP);
250199a2dd95SBruce Richardson 
250299a2dd95SBruce Richardson 	ret = (*dev->dev_ops->hairpin_get_peer_ports)(dev, peer_ports,
250399a2dd95SBruce Richardson 						      len, direction);
250499a2dd95SBruce Richardson 	if (ret < 0)
250599a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Failed to get %d hairpin peer %s ports\n",
250699a2dd95SBruce Richardson 			       port_id, direction ? "Rx" : "Tx");
250799a2dd95SBruce Richardson 
250899a2dd95SBruce Richardson 	return ret;
250999a2dd95SBruce Richardson }
251099a2dd95SBruce Richardson 
251199a2dd95SBruce Richardson void
251299a2dd95SBruce Richardson rte_eth_tx_buffer_drop_callback(struct rte_mbuf **pkts, uint16_t unsent,
251399a2dd95SBruce Richardson 		void *userdata __rte_unused)
251499a2dd95SBruce Richardson {
251599a2dd95SBruce Richardson 	rte_pktmbuf_free_bulk(pkts, unsent);
251699a2dd95SBruce Richardson }
251799a2dd95SBruce Richardson 
251899a2dd95SBruce Richardson void
251999a2dd95SBruce Richardson rte_eth_tx_buffer_count_callback(struct rte_mbuf **pkts, uint16_t unsent,
252099a2dd95SBruce Richardson 		void *userdata)
252199a2dd95SBruce Richardson {
252299a2dd95SBruce Richardson 	uint64_t *count = userdata;
252399a2dd95SBruce Richardson 
252499a2dd95SBruce Richardson 	rte_pktmbuf_free_bulk(pkts, unsent);
252599a2dd95SBruce Richardson 	*count += unsent;
252699a2dd95SBruce Richardson }
252799a2dd95SBruce Richardson 
252899a2dd95SBruce Richardson int
252999a2dd95SBruce Richardson rte_eth_tx_buffer_set_err_callback(struct rte_eth_dev_tx_buffer *buffer,
253099a2dd95SBruce Richardson 		buffer_tx_error_fn cbfn, void *userdata)
253199a2dd95SBruce Richardson {
253253ef1b34SMin Hu (Connor) 	if (buffer == NULL) {
253353ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
253453ef1b34SMin Hu (Connor) 			"Cannot set Tx buffer error callback to NULL buffer\n");
253553ef1b34SMin Hu (Connor) 		return -EINVAL;
253653ef1b34SMin Hu (Connor) 	}
253753ef1b34SMin Hu (Connor) 
253899a2dd95SBruce Richardson 	buffer->error_callback = cbfn;
253999a2dd95SBruce Richardson 	buffer->error_userdata = userdata;
254099a2dd95SBruce Richardson 	return 0;
254199a2dd95SBruce Richardson }
254299a2dd95SBruce Richardson 
254399a2dd95SBruce Richardson int
254499a2dd95SBruce Richardson rte_eth_tx_buffer_init(struct rte_eth_dev_tx_buffer *buffer, uint16_t size)
254599a2dd95SBruce Richardson {
254699a2dd95SBruce Richardson 	int ret = 0;
254799a2dd95SBruce Richardson 
254853ef1b34SMin Hu (Connor) 	if (buffer == NULL) {
254953ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot initialize NULL buffer\n");
255099a2dd95SBruce Richardson 		return -EINVAL;
255153ef1b34SMin Hu (Connor) 	}
255299a2dd95SBruce Richardson 
255399a2dd95SBruce Richardson 	buffer->size = size;
255499a2dd95SBruce Richardson 	if (buffer->error_callback == NULL) {
255599a2dd95SBruce Richardson 		ret = rte_eth_tx_buffer_set_err_callback(
255699a2dd95SBruce Richardson 			buffer, rte_eth_tx_buffer_drop_callback, NULL);
255799a2dd95SBruce Richardson 	}
255899a2dd95SBruce Richardson 
255999a2dd95SBruce Richardson 	return ret;
256099a2dd95SBruce Richardson }
256199a2dd95SBruce Richardson 
256299a2dd95SBruce Richardson int
256399a2dd95SBruce Richardson rte_eth_tx_done_cleanup(uint16_t port_id, uint16_t queue_id, uint32_t free_cnt)
256499a2dd95SBruce Richardson {
256553ef1b34SMin Hu (Connor) 	struct rte_eth_dev *dev;
256699a2dd95SBruce Richardson 	int ret;
256799a2dd95SBruce Richardson 
256899a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
256953ef1b34SMin Hu (Connor) 	dev = &rte_eth_devices[port_id];
257053ef1b34SMin Hu (Connor) 
257199a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_done_cleanup, -ENOTSUP);
257299a2dd95SBruce Richardson 
257399a2dd95SBruce Richardson 	/* Call driver to free pending mbufs. */
257499a2dd95SBruce Richardson 	ret = (*dev->dev_ops->tx_done_cleanup)(dev->data->tx_queues[queue_id],
257599a2dd95SBruce Richardson 					       free_cnt);
257699a2dd95SBruce Richardson 	return eth_err(port_id, ret);
257799a2dd95SBruce Richardson }
257899a2dd95SBruce Richardson 
257999a2dd95SBruce Richardson int
258099a2dd95SBruce Richardson rte_eth_promiscuous_enable(uint16_t port_id)
258199a2dd95SBruce Richardson {
258299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
258399a2dd95SBruce Richardson 	int diag = 0;
258499a2dd95SBruce Richardson 
258599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
258699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
258799a2dd95SBruce Richardson 
258899a2dd95SBruce Richardson 	if (dev->data->promiscuous == 1)
258999a2dd95SBruce Richardson 		return 0;
259099a2dd95SBruce Richardson 
259199a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->promiscuous_enable, -ENOTSUP);
259299a2dd95SBruce Richardson 
259399a2dd95SBruce Richardson 	diag = (*dev->dev_ops->promiscuous_enable)(dev);
259499a2dd95SBruce Richardson 	dev->data->promiscuous = (diag == 0) ? 1 : 0;
259599a2dd95SBruce Richardson 
259699a2dd95SBruce Richardson 	return eth_err(port_id, diag);
259799a2dd95SBruce Richardson }
259899a2dd95SBruce Richardson 
259999a2dd95SBruce Richardson int
260099a2dd95SBruce Richardson rte_eth_promiscuous_disable(uint16_t port_id)
260199a2dd95SBruce Richardson {
260299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
260399a2dd95SBruce Richardson 	int diag = 0;
260499a2dd95SBruce Richardson 
260599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
260699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
260799a2dd95SBruce Richardson 
260899a2dd95SBruce Richardson 	if (dev->data->promiscuous == 0)
260999a2dd95SBruce Richardson 		return 0;
261099a2dd95SBruce Richardson 
261199a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->promiscuous_disable, -ENOTSUP);
261299a2dd95SBruce Richardson 
261399a2dd95SBruce Richardson 	dev->data->promiscuous = 0;
261499a2dd95SBruce Richardson 	diag = (*dev->dev_ops->promiscuous_disable)(dev);
261599a2dd95SBruce Richardson 	if (diag != 0)
261699a2dd95SBruce Richardson 		dev->data->promiscuous = 1;
261799a2dd95SBruce Richardson 
261899a2dd95SBruce Richardson 	return eth_err(port_id, diag);
261999a2dd95SBruce Richardson }
262099a2dd95SBruce Richardson 
262199a2dd95SBruce Richardson int
262299a2dd95SBruce Richardson rte_eth_promiscuous_get(uint16_t port_id)
262399a2dd95SBruce Richardson {
262499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
262599a2dd95SBruce Richardson 
262699a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
262799a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
262853ef1b34SMin Hu (Connor) 
262999a2dd95SBruce Richardson 	return dev->data->promiscuous;
263099a2dd95SBruce Richardson }
263199a2dd95SBruce Richardson 
263299a2dd95SBruce Richardson int
263399a2dd95SBruce Richardson rte_eth_allmulticast_enable(uint16_t port_id)
263499a2dd95SBruce Richardson {
263599a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
263699a2dd95SBruce Richardson 	int diag;
263799a2dd95SBruce Richardson 
263899a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
263999a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
264099a2dd95SBruce Richardson 
264199a2dd95SBruce Richardson 	if (dev->data->all_multicast == 1)
264299a2dd95SBruce Richardson 		return 0;
264399a2dd95SBruce Richardson 
264499a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->allmulticast_enable, -ENOTSUP);
264599a2dd95SBruce Richardson 	diag = (*dev->dev_ops->allmulticast_enable)(dev);
264699a2dd95SBruce Richardson 	dev->data->all_multicast = (diag == 0) ? 1 : 0;
264799a2dd95SBruce Richardson 
264899a2dd95SBruce Richardson 	return eth_err(port_id, diag);
264999a2dd95SBruce Richardson }
265099a2dd95SBruce Richardson 
265199a2dd95SBruce Richardson int
265299a2dd95SBruce Richardson rte_eth_allmulticast_disable(uint16_t port_id)
265399a2dd95SBruce Richardson {
265499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
265599a2dd95SBruce Richardson 	int diag;
265699a2dd95SBruce Richardson 
265799a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
265899a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
265999a2dd95SBruce Richardson 
266099a2dd95SBruce Richardson 	if (dev->data->all_multicast == 0)
266199a2dd95SBruce Richardson 		return 0;
266299a2dd95SBruce Richardson 
266399a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->allmulticast_disable, -ENOTSUP);
266499a2dd95SBruce Richardson 	dev->data->all_multicast = 0;
266599a2dd95SBruce Richardson 	diag = (*dev->dev_ops->allmulticast_disable)(dev);
266699a2dd95SBruce Richardson 	if (diag != 0)
266799a2dd95SBruce Richardson 		dev->data->all_multicast = 1;
266899a2dd95SBruce Richardson 
266999a2dd95SBruce Richardson 	return eth_err(port_id, diag);
267099a2dd95SBruce Richardson }
267199a2dd95SBruce Richardson 
267299a2dd95SBruce Richardson int
267399a2dd95SBruce Richardson rte_eth_allmulticast_get(uint16_t port_id)
267499a2dd95SBruce Richardson {
267599a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
267699a2dd95SBruce Richardson 
267799a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
267899a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
267953ef1b34SMin Hu (Connor) 
268099a2dd95SBruce Richardson 	return dev->data->all_multicast;
268199a2dd95SBruce Richardson }
268299a2dd95SBruce Richardson 
268399a2dd95SBruce Richardson int
268499a2dd95SBruce Richardson rte_eth_link_get(uint16_t port_id, struct rte_eth_link *eth_link)
268599a2dd95SBruce Richardson {
268699a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
268799a2dd95SBruce Richardson 
268899a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
268999a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
269099a2dd95SBruce Richardson 
269153ef1b34SMin Hu (Connor) 	if (eth_link == NULL) {
269253ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot get ethdev port %u link to NULL\n",
269353ef1b34SMin Hu (Connor) 			port_id);
269453ef1b34SMin Hu (Connor) 		return -EINVAL;
269553ef1b34SMin Hu (Connor) 	}
269653ef1b34SMin Hu (Connor) 
269753ef1b34SMin Hu (Connor) 	if (dev->data->dev_conf.intr_conf.lsc && dev->data->dev_started)
269899a2dd95SBruce Richardson 		rte_eth_linkstatus_get(dev, eth_link);
269999a2dd95SBruce Richardson 	else {
270099a2dd95SBruce Richardson 		RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->link_update, -ENOTSUP);
270199a2dd95SBruce Richardson 		(*dev->dev_ops->link_update)(dev, 1);
270299a2dd95SBruce Richardson 		*eth_link = dev->data->dev_link;
270399a2dd95SBruce Richardson 	}
270499a2dd95SBruce Richardson 
270599a2dd95SBruce Richardson 	return 0;
270699a2dd95SBruce Richardson }
270799a2dd95SBruce Richardson 
270899a2dd95SBruce Richardson int
270999a2dd95SBruce Richardson rte_eth_link_get_nowait(uint16_t port_id, struct rte_eth_link *eth_link)
271099a2dd95SBruce Richardson {
271199a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
271299a2dd95SBruce Richardson 
271399a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
271499a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
271599a2dd95SBruce Richardson 
271653ef1b34SMin Hu (Connor) 	if (eth_link == NULL) {
271753ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot get ethdev port %u link to NULL\n",
271853ef1b34SMin Hu (Connor) 			port_id);
271953ef1b34SMin Hu (Connor) 		return -EINVAL;
272053ef1b34SMin Hu (Connor) 	}
272153ef1b34SMin Hu (Connor) 
272253ef1b34SMin Hu (Connor) 	if (dev->data->dev_conf.intr_conf.lsc && dev->data->dev_started)
272399a2dd95SBruce Richardson 		rte_eth_linkstatus_get(dev, eth_link);
272499a2dd95SBruce Richardson 	else {
272599a2dd95SBruce Richardson 		RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->link_update, -ENOTSUP);
272699a2dd95SBruce Richardson 		(*dev->dev_ops->link_update)(dev, 0);
272799a2dd95SBruce Richardson 		*eth_link = dev->data->dev_link;
272899a2dd95SBruce Richardson 	}
272999a2dd95SBruce Richardson 
273099a2dd95SBruce Richardson 	return 0;
273199a2dd95SBruce Richardson }
273299a2dd95SBruce Richardson 
273399a2dd95SBruce Richardson const char *
273499a2dd95SBruce Richardson rte_eth_link_speed_to_str(uint32_t link_speed)
273599a2dd95SBruce Richardson {
273699a2dd95SBruce Richardson 	switch (link_speed) {
273799a2dd95SBruce Richardson 	case ETH_SPEED_NUM_NONE: return "None";
273899a2dd95SBruce Richardson 	case ETH_SPEED_NUM_10M:  return "10 Mbps";
273999a2dd95SBruce Richardson 	case ETH_SPEED_NUM_100M: return "100 Mbps";
274099a2dd95SBruce Richardson 	case ETH_SPEED_NUM_1G:   return "1 Gbps";
274199a2dd95SBruce Richardson 	case ETH_SPEED_NUM_2_5G: return "2.5 Gbps";
274299a2dd95SBruce Richardson 	case ETH_SPEED_NUM_5G:   return "5 Gbps";
274399a2dd95SBruce Richardson 	case ETH_SPEED_NUM_10G:  return "10 Gbps";
274499a2dd95SBruce Richardson 	case ETH_SPEED_NUM_20G:  return "20 Gbps";
274599a2dd95SBruce Richardson 	case ETH_SPEED_NUM_25G:  return "25 Gbps";
274699a2dd95SBruce Richardson 	case ETH_SPEED_NUM_40G:  return "40 Gbps";
274799a2dd95SBruce Richardson 	case ETH_SPEED_NUM_50G:  return "50 Gbps";
274899a2dd95SBruce Richardson 	case ETH_SPEED_NUM_56G:  return "56 Gbps";
274999a2dd95SBruce Richardson 	case ETH_SPEED_NUM_100G: return "100 Gbps";
275099a2dd95SBruce Richardson 	case ETH_SPEED_NUM_200G: return "200 Gbps";
275199a2dd95SBruce Richardson 	case ETH_SPEED_NUM_UNKNOWN: return "Unknown";
275299a2dd95SBruce Richardson 	default: return "Invalid";
275399a2dd95SBruce Richardson 	}
275499a2dd95SBruce Richardson }
275599a2dd95SBruce Richardson 
275699a2dd95SBruce Richardson int
275799a2dd95SBruce Richardson rte_eth_link_to_str(char *str, size_t len, const struct rte_eth_link *eth_link)
275899a2dd95SBruce Richardson {
275953ef1b34SMin Hu (Connor) 	if (str == NULL) {
276053ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot convert link to NULL string\n");
276153ef1b34SMin Hu (Connor) 		return -EINVAL;
276253ef1b34SMin Hu (Connor) 	}
276353ef1b34SMin Hu (Connor) 
276453ef1b34SMin Hu (Connor) 	if (len == 0) {
276553ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
276653ef1b34SMin Hu (Connor) 			"Cannot convert link to string with zero size\n");
276753ef1b34SMin Hu (Connor) 		return -EINVAL;
276853ef1b34SMin Hu (Connor) 	}
276953ef1b34SMin Hu (Connor) 
277053ef1b34SMin Hu (Connor) 	if (eth_link == NULL) {
277153ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot convert to string from NULL link\n");
277253ef1b34SMin Hu (Connor) 		return -EINVAL;
277353ef1b34SMin Hu (Connor) 	}
277453ef1b34SMin Hu (Connor) 
277599a2dd95SBruce Richardson 	if (eth_link->link_status == ETH_LINK_DOWN)
277699a2dd95SBruce Richardson 		return snprintf(str, len, "Link down");
277799a2dd95SBruce Richardson 	else
277899a2dd95SBruce Richardson 		return snprintf(str, len, "Link up at %s %s %s",
277999a2dd95SBruce Richardson 			rte_eth_link_speed_to_str(eth_link->link_speed),
278099a2dd95SBruce Richardson 			(eth_link->link_duplex == ETH_LINK_FULL_DUPLEX) ?
278199a2dd95SBruce Richardson 			"FDX" : "HDX",
278299a2dd95SBruce Richardson 			(eth_link->link_autoneg == ETH_LINK_AUTONEG) ?
278399a2dd95SBruce Richardson 			"Autoneg" : "Fixed");
278499a2dd95SBruce Richardson }
278599a2dd95SBruce Richardson 
278699a2dd95SBruce Richardson int
278799a2dd95SBruce Richardson rte_eth_stats_get(uint16_t port_id, struct rte_eth_stats *stats)
278899a2dd95SBruce Richardson {
278999a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
279099a2dd95SBruce Richardson 
279199a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
279299a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
279353ef1b34SMin Hu (Connor) 
279453ef1b34SMin Hu (Connor) 	if (stats == NULL) {
279553ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot get ethdev port %u stats to NULL\n",
279653ef1b34SMin Hu (Connor) 			port_id);
279753ef1b34SMin Hu (Connor) 		return -EINVAL;
279853ef1b34SMin Hu (Connor) 	}
279953ef1b34SMin Hu (Connor) 
280099a2dd95SBruce Richardson 	memset(stats, 0, sizeof(*stats));
280199a2dd95SBruce Richardson 
280299a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
280399a2dd95SBruce Richardson 	stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
280499a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->stats_get)(dev, stats));
280599a2dd95SBruce Richardson }
280699a2dd95SBruce Richardson 
280799a2dd95SBruce Richardson int
280899a2dd95SBruce Richardson rte_eth_stats_reset(uint16_t port_id)
280999a2dd95SBruce Richardson {
281099a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
281199a2dd95SBruce Richardson 	int ret;
281299a2dd95SBruce Richardson 
281399a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
281499a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
281599a2dd95SBruce Richardson 
281699a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_reset, -ENOTSUP);
281799a2dd95SBruce Richardson 	ret = (*dev->dev_ops->stats_reset)(dev);
281899a2dd95SBruce Richardson 	if (ret != 0)
281999a2dd95SBruce Richardson 		return eth_err(port_id, ret);
282099a2dd95SBruce Richardson 
282199a2dd95SBruce Richardson 	dev->data->rx_mbuf_alloc_failed = 0;
282299a2dd95SBruce Richardson 
282399a2dd95SBruce Richardson 	return 0;
282499a2dd95SBruce Richardson }
282599a2dd95SBruce Richardson 
282699a2dd95SBruce Richardson static inline int
282799a2dd95SBruce Richardson eth_dev_get_xstats_basic_count(struct rte_eth_dev *dev)
282899a2dd95SBruce Richardson {
282999a2dd95SBruce Richardson 	uint16_t nb_rxqs, nb_txqs;
283099a2dd95SBruce Richardson 	int count;
283199a2dd95SBruce Richardson 
283299a2dd95SBruce Richardson 	nb_rxqs = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
283399a2dd95SBruce Richardson 	nb_txqs = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
283499a2dd95SBruce Richardson 
283599a2dd95SBruce Richardson 	count = RTE_NB_STATS;
283699a2dd95SBruce Richardson 	if (dev->data->dev_flags & RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS) {
283799a2dd95SBruce Richardson 		count += nb_rxqs * RTE_NB_RXQ_STATS;
283899a2dd95SBruce Richardson 		count += nb_txqs * RTE_NB_TXQ_STATS;
283999a2dd95SBruce Richardson 	}
284099a2dd95SBruce Richardson 
284199a2dd95SBruce Richardson 	return count;
284299a2dd95SBruce Richardson }
284399a2dd95SBruce Richardson 
284499a2dd95SBruce Richardson static int
284599a2dd95SBruce Richardson eth_dev_get_xstats_count(uint16_t port_id)
284699a2dd95SBruce Richardson {
284799a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
284899a2dd95SBruce Richardson 	int count;
284999a2dd95SBruce Richardson 
285099a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
285199a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
285299a2dd95SBruce Richardson 	if (dev->dev_ops->xstats_get_names != NULL) {
285399a2dd95SBruce Richardson 		count = (*dev->dev_ops->xstats_get_names)(dev, NULL, 0);
285499a2dd95SBruce Richardson 		if (count < 0)
285599a2dd95SBruce Richardson 			return eth_err(port_id, count);
285699a2dd95SBruce Richardson 	} else
285799a2dd95SBruce Richardson 		count = 0;
285899a2dd95SBruce Richardson 
285999a2dd95SBruce Richardson 
286099a2dd95SBruce Richardson 	count += eth_dev_get_xstats_basic_count(dev);
286199a2dd95SBruce Richardson 
286299a2dd95SBruce Richardson 	return count;
286399a2dd95SBruce Richardson }
286499a2dd95SBruce Richardson 
286599a2dd95SBruce Richardson int
286699a2dd95SBruce Richardson rte_eth_xstats_get_id_by_name(uint16_t port_id, const char *xstat_name,
286799a2dd95SBruce Richardson 		uint64_t *id)
286899a2dd95SBruce Richardson {
286999a2dd95SBruce Richardson 	int cnt_xstats, idx_xstat;
287099a2dd95SBruce Richardson 
287199a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
287299a2dd95SBruce Richardson 
287353ef1b34SMin Hu (Connor) 	if (xstat_name == NULL) {
287453ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
287553ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u xstats ID from NULL xstat name\n",
287653ef1b34SMin Hu (Connor) 			port_id);
287799a2dd95SBruce Richardson 		return -ENOMEM;
287899a2dd95SBruce Richardson 	}
287999a2dd95SBruce Richardson 
288053ef1b34SMin Hu (Connor) 	if (id == NULL) {
288153ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
288253ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u xstats ID to NULL\n",
288353ef1b34SMin Hu (Connor) 			port_id);
288499a2dd95SBruce Richardson 		return -ENOMEM;
288599a2dd95SBruce Richardson 	}
288699a2dd95SBruce Richardson 
288799a2dd95SBruce Richardson 	/* Get count */
288899a2dd95SBruce Richardson 	cnt_xstats = rte_eth_xstats_get_names_by_id(port_id, NULL, 0, NULL);
288999a2dd95SBruce Richardson 	if (cnt_xstats  < 0) {
289099a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Cannot get count of xstats\n");
289199a2dd95SBruce Richardson 		return -ENODEV;
289299a2dd95SBruce Richardson 	}
289399a2dd95SBruce Richardson 
289499a2dd95SBruce Richardson 	/* Get id-name lookup table */
289599a2dd95SBruce Richardson 	struct rte_eth_xstat_name xstats_names[cnt_xstats];
289699a2dd95SBruce Richardson 
289799a2dd95SBruce Richardson 	if (cnt_xstats != rte_eth_xstats_get_names_by_id(
289899a2dd95SBruce Richardson 			port_id, xstats_names, cnt_xstats, NULL)) {
289999a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Cannot get xstats lookup\n");
290099a2dd95SBruce Richardson 		return -1;
290199a2dd95SBruce Richardson 	}
290299a2dd95SBruce Richardson 
290399a2dd95SBruce Richardson 	for (idx_xstat = 0; idx_xstat < cnt_xstats; idx_xstat++) {
290499a2dd95SBruce Richardson 		if (!strcmp(xstats_names[idx_xstat].name, xstat_name)) {
290599a2dd95SBruce Richardson 			*id = idx_xstat;
290699a2dd95SBruce Richardson 			return 0;
290799a2dd95SBruce Richardson 		};
290899a2dd95SBruce Richardson 	}
290999a2dd95SBruce Richardson 
291099a2dd95SBruce Richardson 	return -EINVAL;
291199a2dd95SBruce Richardson }
291299a2dd95SBruce Richardson 
291399a2dd95SBruce Richardson /* retrieve basic stats names */
291499a2dd95SBruce Richardson static int
291599a2dd95SBruce Richardson eth_basic_stats_get_names(struct rte_eth_dev *dev,
291699a2dd95SBruce Richardson 	struct rte_eth_xstat_name *xstats_names)
291799a2dd95SBruce Richardson {
291899a2dd95SBruce Richardson 	int cnt_used_entries = 0;
291999a2dd95SBruce Richardson 	uint32_t idx, id_queue;
292099a2dd95SBruce Richardson 	uint16_t num_q;
292199a2dd95SBruce Richardson 
292299a2dd95SBruce Richardson 	for (idx = 0; idx < RTE_NB_STATS; idx++) {
292399a2dd95SBruce Richardson 		strlcpy(xstats_names[cnt_used_entries].name,
292499a2dd95SBruce Richardson 			eth_dev_stats_strings[idx].name,
292599a2dd95SBruce Richardson 			sizeof(xstats_names[0].name));
292699a2dd95SBruce Richardson 		cnt_used_entries++;
292799a2dd95SBruce Richardson 	}
292899a2dd95SBruce Richardson 
292999a2dd95SBruce Richardson 	if ((dev->data->dev_flags & RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS) == 0)
293099a2dd95SBruce Richardson 		return cnt_used_entries;
293199a2dd95SBruce Richardson 
293299a2dd95SBruce Richardson 	num_q = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
293399a2dd95SBruce Richardson 	for (id_queue = 0; id_queue < num_q; id_queue++) {
293499a2dd95SBruce Richardson 		for (idx = 0; idx < RTE_NB_RXQ_STATS; idx++) {
293599a2dd95SBruce Richardson 			snprintf(xstats_names[cnt_used_entries].name,
293699a2dd95SBruce Richardson 				sizeof(xstats_names[0].name),
293799a2dd95SBruce Richardson 				"rx_q%u_%s",
293899a2dd95SBruce Richardson 				id_queue, eth_dev_rxq_stats_strings[idx].name);
293999a2dd95SBruce Richardson 			cnt_used_entries++;
294099a2dd95SBruce Richardson 		}
294199a2dd95SBruce Richardson 
294299a2dd95SBruce Richardson 	}
294399a2dd95SBruce Richardson 	num_q = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
294499a2dd95SBruce Richardson 	for (id_queue = 0; id_queue < num_q; id_queue++) {
294599a2dd95SBruce Richardson 		for (idx = 0; idx < RTE_NB_TXQ_STATS; idx++) {
294699a2dd95SBruce Richardson 			snprintf(xstats_names[cnt_used_entries].name,
294799a2dd95SBruce Richardson 				sizeof(xstats_names[0].name),
294899a2dd95SBruce Richardson 				"tx_q%u_%s",
294999a2dd95SBruce Richardson 				id_queue, eth_dev_txq_stats_strings[idx].name);
295099a2dd95SBruce Richardson 			cnt_used_entries++;
295199a2dd95SBruce Richardson 		}
295299a2dd95SBruce Richardson 	}
295399a2dd95SBruce Richardson 	return cnt_used_entries;
295499a2dd95SBruce Richardson }
295599a2dd95SBruce Richardson 
295699a2dd95SBruce Richardson /* retrieve ethdev extended statistics names */
295799a2dd95SBruce Richardson int
295899a2dd95SBruce Richardson rte_eth_xstats_get_names_by_id(uint16_t port_id,
295999a2dd95SBruce Richardson 	struct rte_eth_xstat_name *xstats_names, unsigned int size,
296099a2dd95SBruce Richardson 	uint64_t *ids)
296199a2dd95SBruce Richardson {
296299a2dd95SBruce Richardson 	struct rte_eth_xstat_name *xstats_names_copy;
296399a2dd95SBruce Richardson 	unsigned int no_basic_stat_requested = 1;
296499a2dd95SBruce Richardson 	unsigned int no_ext_stat_requested = 1;
296599a2dd95SBruce Richardson 	unsigned int expected_entries;
296699a2dd95SBruce Richardson 	unsigned int basic_count;
296799a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
296899a2dd95SBruce Richardson 	unsigned int i;
296999a2dd95SBruce Richardson 	int ret;
297099a2dd95SBruce Richardson 
297199a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
297299a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
297399a2dd95SBruce Richardson 
297499a2dd95SBruce Richardson 	basic_count = eth_dev_get_xstats_basic_count(dev);
297599a2dd95SBruce Richardson 	ret = eth_dev_get_xstats_count(port_id);
297699a2dd95SBruce Richardson 	if (ret < 0)
297799a2dd95SBruce Richardson 		return ret;
297899a2dd95SBruce Richardson 	expected_entries = (unsigned int)ret;
297999a2dd95SBruce Richardson 
298099a2dd95SBruce Richardson 	/* Return max number of stats if no ids given */
298199a2dd95SBruce Richardson 	if (!ids) {
298299a2dd95SBruce Richardson 		if (!xstats_names)
298399a2dd95SBruce Richardson 			return expected_entries;
298499a2dd95SBruce Richardson 		else if (xstats_names && size < expected_entries)
298599a2dd95SBruce Richardson 			return expected_entries;
298699a2dd95SBruce Richardson 	}
298799a2dd95SBruce Richardson 
298899a2dd95SBruce Richardson 	if (ids && !xstats_names)
298999a2dd95SBruce Richardson 		return -EINVAL;
299099a2dd95SBruce Richardson 
299199a2dd95SBruce Richardson 	if (ids && dev->dev_ops->xstats_get_names_by_id != NULL && size > 0) {
299299a2dd95SBruce Richardson 		uint64_t ids_copy[size];
299399a2dd95SBruce Richardson 
299499a2dd95SBruce Richardson 		for (i = 0; i < size; i++) {
299599a2dd95SBruce Richardson 			if (ids[i] < basic_count) {
299699a2dd95SBruce Richardson 				no_basic_stat_requested = 0;
299799a2dd95SBruce Richardson 				break;
299899a2dd95SBruce Richardson 			}
299999a2dd95SBruce Richardson 
300099a2dd95SBruce Richardson 			/*
300199a2dd95SBruce Richardson 			 * Convert ids to xstats ids that PMD knows.
300299a2dd95SBruce Richardson 			 * ids known by user are basic + extended stats.
300399a2dd95SBruce Richardson 			 */
300499a2dd95SBruce Richardson 			ids_copy[i] = ids[i] - basic_count;
300599a2dd95SBruce Richardson 		}
300699a2dd95SBruce Richardson 
300799a2dd95SBruce Richardson 		if (no_basic_stat_requested)
300899a2dd95SBruce Richardson 			return (*dev->dev_ops->xstats_get_names_by_id)(dev,
30098c9f976fSAndrew Rybchenko 					ids_copy, xstats_names, size);
301099a2dd95SBruce Richardson 	}
301199a2dd95SBruce Richardson 
301299a2dd95SBruce Richardson 	/* Retrieve all stats */
301399a2dd95SBruce Richardson 	if (!ids) {
301499a2dd95SBruce Richardson 		int num_stats = rte_eth_xstats_get_names(port_id, xstats_names,
301599a2dd95SBruce Richardson 				expected_entries);
301699a2dd95SBruce Richardson 		if (num_stats < 0 || num_stats > (int)expected_entries)
301799a2dd95SBruce Richardson 			return num_stats;
301899a2dd95SBruce Richardson 		else
301999a2dd95SBruce Richardson 			return expected_entries;
302099a2dd95SBruce Richardson 	}
302199a2dd95SBruce Richardson 
302299a2dd95SBruce Richardson 	xstats_names_copy = calloc(expected_entries,
302399a2dd95SBruce Richardson 		sizeof(struct rte_eth_xstat_name));
302499a2dd95SBruce Richardson 
302599a2dd95SBruce Richardson 	if (!xstats_names_copy) {
302699a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Can't allocate memory\n");
302799a2dd95SBruce Richardson 		return -ENOMEM;
302899a2dd95SBruce Richardson 	}
302999a2dd95SBruce Richardson 
303099a2dd95SBruce Richardson 	if (ids) {
303199a2dd95SBruce Richardson 		for (i = 0; i < size; i++) {
303299a2dd95SBruce Richardson 			if (ids[i] >= basic_count) {
303399a2dd95SBruce Richardson 				no_ext_stat_requested = 0;
303499a2dd95SBruce Richardson 				break;
303599a2dd95SBruce Richardson 			}
303699a2dd95SBruce Richardson 		}
303799a2dd95SBruce Richardson 	}
303899a2dd95SBruce Richardson 
303999a2dd95SBruce Richardson 	/* Fill xstats_names_copy structure */
304099a2dd95SBruce Richardson 	if (ids && no_ext_stat_requested) {
304199a2dd95SBruce Richardson 		eth_basic_stats_get_names(dev, xstats_names_copy);
304299a2dd95SBruce Richardson 	} else {
304399a2dd95SBruce Richardson 		ret = rte_eth_xstats_get_names(port_id, xstats_names_copy,
304499a2dd95SBruce Richardson 			expected_entries);
304599a2dd95SBruce Richardson 		if (ret < 0) {
304699a2dd95SBruce Richardson 			free(xstats_names_copy);
304799a2dd95SBruce Richardson 			return ret;
304899a2dd95SBruce Richardson 		}
304999a2dd95SBruce Richardson 	}
305099a2dd95SBruce Richardson 
305199a2dd95SBruce Richardson 	/* Filter stats */
305299a2dd95SBruce Richardson 	for (i = 0; i < size; i++) {
305399a2dd95SBruce Richardson 		if (ids[i] >= expected_entries) {
305499a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR, "Id value isn't valid\n");
305599a2dd95SBruce Richardson 			free(xstats_names_copy);
305699a2dd95SBruce Richardson 			return -1;
305799a2dd95SBruce Richardson 		}
305899a2dd95SBruce Richardson 		xstats_names[i] = xstats_names_copy[ids[i]];
305999a2dd95SBruce Richardson 	}
306099a2dd95SBruce Richardson 
306199a2dd95SBruce Richardson 	free(xstats_names_copy);
306299a2dd95SBruce Richardson 	return size;
306399a2dd95SBruce Richardson }
306499a2dd95SBruce Richardson 
306599a2dd95SBruce Richardson int
306699a2dd95SBruce Richardson rte_eth_xstats_get_names(uint16_t port_id,
306799a2dd95SBruce Richardson 	struct rte_eth_xstat_name *xstats_names,
306899a2dd95SBruce Richardson 	unsigned int size)
306999a2dd95SBruce Richardson {
307099a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
307199a2dd95SBruce Richardson 	int cnt_used_entries;
307299a2dd95SBruce Richardson 	int cnt_expected_entries;
307399a2dd95SBruce Richardson 	int cnt_driver_entries;
307499a2dd95SBruce Richardson 
307599a2dd95SBruce Richardson 	cnt_expected_entries = eth_dev_get_xstats_count(port_id);
307699a2dd95SBruce Richardson 	if (xstats_names == NULL || cnt_expected_entries < 0 ||
307799a2dd95SBruce Richardson 			(int)size < cnt_expected_entries)
307899a2dd95SBruce Richardson 		return cnt_expected_entries;
307999a2dd95SBruce Richardson 
308099a2dd95SBruce Richardson 	/* port_id checked in eth_dev_get_xstats_count() */
308199a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
308299a2dd95SBruce Richardson 
308399a2dd95SBruce Richardson 	cnt_used_entries = eth_basic_stats_get_names(dev, xstats_names);
308499a2dd95SBruce Richardson 
308599a2dd95SBruce Richardson 	if (dev->dev_ops->xstats_get_names != NULL) {
308699a2dd95SBruce Richardson 		/* If there are any driver-specific xstats, append them
308799a2dd95SBruce Richardson 		 * to end of list.
308899a2dd95SBruce Richardson 		 */
308999a2dd95SBruce Richardson 		cnt_driver_entries = (*dev->dev_ops->xstats_get_names)(
309099a2dd95SBruce Richardson 			dev,
309199a2dd95SBruce Richardson 			xstats_names + cnt_used_entries,
309299a2dd95SBruce Richardson 			size - cnt_used_entries);
309399a2dd95SBruce Richardson 		if (cnt_driver_entries < 0)
309499a2dd95SBruce Richardson 			return eth_err(port_id, cnt_driver_entries);
309599a2dd95SBruce Richardson 		cnt_used_entries += cnt_driver_entries;
309699a2dd95SBruce Richardson 	}
309799a2dd95SBruce Richardson 
309899a2dd95SBruce Richardson 	return cnt_used_entries;
309999a2dd95SBruce Richardson }
310099a2dd95SBruce Richardson 
310199a2dd95SBruce Richardson 
310299a2dd95SBruce Richardson static int
310399a2dd95SBruce Richardson eth_basic_stats_get(uint16_t port_id, struct rte_eth_xstat *xstats)
310499a2dd95SBruce Richardson {
310599a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
310699a2dd95SBruce Richardson 	struct rte_eth_stats eth_stats;
310799a2dd95SBruce Richardson 	unsigned int count = 0, i, q;
310899a2dd95SBruce Richardson 	uint64_t val, *stats_ptr;
310999a2dd95SBruce Richardson 	uint16_t nb_rxqs, nb_txqs;
311099a2dd95SBruce Richardson 	int ret;
311199a2dd95SBruce Richardson 
311299a2dd95SBruce Richardson 	ret = rte_eth_stats_get(port_id, &eth_stats);
311399a2dd95SBruce Richardson 	if (ret < 0)
311499a2dd95SBruce Richardson 		return ret;
311599a2dd95SBruce Richardson 
311699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
311799a2dd95SBruce Richardson 
311899a2dd95SBruce Richardson 	nb_rxqs = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
311999a2dd95SBruce Richardson 	nb_txqs = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
312099a2dd95SBruce Richardson 
312199a2dd95SBruce Richardson 	/* global stats */
312299a2dd95SBruce Richardson 	for (i = 0; i < RTE_NB_STATS; i++) {
312399a2dd95SBruce Richardson 		stats_ptr = RTE_PTR_ADD(&eth_stats,
312499a2dd95SBruce Richardson 					eth_dev_stats_strings[i].offset);
312599a2dd95SBruce Richardson 		val = *stats_ptr;
312699a2dd95SBruce Richardson 		xstats[count++].value = val;
312799a2dd95SBruce Richardson 	}
312899a2dd95SBruce Richardson 
312999a2dd95SBruce Richardson 	if ((dev->data->dev_flags & RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS) == 0)
313099a2dd95SBruce Richardson 		return count;
313199a2dd95SBruce Richardson 
313299a2dd95SBruce Richardson 	/* per-rxq stats */
313399a2dd95SBruce Richardson 	for (q = 0; q < nb_rxqs; q++) {
313499a2dd95SBruce Richardson 		for (i = 0; i < RTE_NB_RXQ_STATS; i++) {
313599a2dd95SBruce Richardson 			stats_ptr = RTE_PTR_ADD(&eth_stats,
313699a2dd95SBruce Richardson 					eth_dev_rxq_stats_strings[i].offset +
313799a2dd95SBruce Richardson 					q * sizeof(uint64_t));
313899a2dd95SBruce Richardson 			val = *stats_ptr;
313999a2dd95SBruce Richardson 			xstats[count++].value = val;
314099a2dd95SBruce Richardson 		}
314199a2dd95SBruce Richardson 	}
314299a2dd95SBruce Richardson 
314399a2dd95SBruce Richardson 	/* per-txq stats */
314499a2dd95SBruce Richardson 	for (q = 0; q < nb_txqs; q++) {
314599a2dd95SBruce Richardson 		for (i = 0; i < RTE_NB_TXQ_STATS; i++) {
314699a2dd95SBruce Richardson 			stats_ptr = RTE_PTR_ADD(&eth_stats,
314799a2dd95SBruce Richardson 					eth_dev_txq_stats_strings[i].offset +
314899a2dd95SBruce Richardson 					q * sizeof(uint64_t));
314999a2dd95SBruce Richardson 			val = *stats_ptr;
315099a2dd95SBruce Richardson 			xstats[count++].value = val;
315199a2dd95SBruce Richardson 		}
315299a2dd95SBruce Richardson 	}
315399a2dd95SBruce Richardson 	return count;
315499a2dd95SBruce Richardson }
315599a2dd95SBruce Richardson 
315699a2dd95SBruce Richardson /* retrieve ethdev extended statistics */
315799a2dd95SBruce Richardson int
315899a2dd95SBruce Richardson rte_eth_xstats_get_by_id(uint16_t port_id, const uint64_t *ids,
315999a2dd95SBruce Richardson 			 uint64_t *values, unsigned int size)
316099a2dd95SBruce Richardson {
316199a2dd95SBruce Richardson 	unsigned int no_basic_stat_requested = 1;
316299a2dd95SBruce Richardson 	unsigned int no_ext_stat_requested = 1;
316399a2dd95SBruce Richardson 	unsigned int num_xstats_filled;
316499a2dd95SBruce Richardson 	unsigned int basic_count;
316599a2dd95SBruce Richardson 	uint16_t expected_entries;
316699a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
316799a2dd95SBruce Richardson 	unsigned int i;
316899a2dd95SBruce Richardson 	int ret;
316999a2dd95SBruce Richardson 
317099a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
317153ef1b34SMin Hu (Connor) 	dev = &rte_eth_devices[port_id];
317253ef1b34SMin Hu (Connor) 
317399a2dd95SBruce Richardson 	ret = eth_dev_get_xstats_count(port_id);
317499a2dd95SBruce Richardson 	if (ret < 0)
317599a2dd95SBruce Richardson 		return ret;
317699a2dd95SBruce Richardson 	expected_entries = (uint16_t)ret;
317799a2dd95SBruce Richardson 	struct rte_eth_xstat xstats[expected_entries];
317899a2dd95SBruce Richardson 	basic_count = eth_dev_get_xstats_basic_count(dev);
317999a2dd95SBruce Richardson 
318099a2dd95SBruce Richardson 	/* Return max number of stats if no ids given */
318199a2dd95SBruce Richardson 	if (!ids) {
318299a2dd95SBruce Richardson 		if (!values)
318399a2dd95SBruce Richardson 			return expected_entries;
318499a2dd95SBruce Richardson 		else if (values && size < expected_entries)
318599a2dd95SBruce Richardson 			return expected_entries;
318699a2dd95SBruce Richardson 	}
318799a2dd95SBruce Richardson 
318899a2dd95SBruce Richardson 	if (ids && !values)
318999a2dd95SBruce Richardson 		return -EINVAL;
319099a2dd95SBruce Richardson 
319199a2dd95SBruce Richardson 	if (ids && dev->dev_ops->xstats_get_by_id != NULL && size) {
319299a2dd95SBruce Richardson 		unsigned int basic_count = eth_dev_get_xstats_basic_count(dev);
319399a2dd95SBruce Richardson 		uint64_t ids_copy[size];
319499a2dd95SBruce Richardson 
319599a2dd95SBruce Richardson 		for (i = 0; i < size; i++) {
319699a2dd95SBruce Richardson 			if (ids[i] < basic_count) {
319799a2dd95SBruce Richardson 				no_basic_stat_requested = 0;
319899a2dd95SBruce Richardson 				break;
319999a2dd95SBruce Richardson 			}
320099a2dd95SBruce Richardson 
320199a2dd95SBruce Richardson 			/*
320299a2dd95SBruce Richardson 			 * Convert ids to xstats ids that PMD knows.
320399a2dd95SBruce Richardson 			 * ids known by user are basic + extended stats.
320499a2dd95SBruce Richardson 			 */
320599a2dd95SBruce Richardson 			ids_copy[i] = ids[i] - basic_count;
320699a2dd95SBruce Richardson 		}
320799a2dd95SBruce Richardson 
320899a2dd95SBruce Richardson 		if (no_basic_stat_requested)
320999a2dd95SBruce Richardson 			return (*dev->dev_ops->xstats_get_by_id)(dev, ids_copy,
321099a2dd95SBruce Richardson 					values, size);
321199a2dd95SBruce Richardson 	}
321299a2dd95SBruce Richardson 
321399a2dd95SBruce Richardson 	if (ids) {
321499a2dd95SBruce Richardson 		for (i = 0; i < size; i++) {
321599a2dd95SBruce Richardson 			if (ids[i] >= basic_count) {
321699a2dd95SBruce Richardson 				no_ext_stat_requested = 0;
321799a2dd95SBruce Richardson 				break;
321899a2dd95SBruce Richardson 			}
321999a2dd95SBruce Richardson 		}
322099a2dd95SBruce Richardson 	}
322199a2dd95SBruce Richardson 
322299a2dd95SBruce Richardson 	/* Fill the xstats structure */
322399a2dd95SBruce Richardson 	if (ids && no_ext_stat_requested)
322499a2dd95SBruce Richardson 		ret = eth_basic_stats_get(port_id, xstats);
322599a2dd95SBruce Richardson 	else
322699a2dd95SBruce Richardson 		ret = rte_eth_xstats_get(port_id, xstats, expected_entries);
322799a2dd95SBruce Richardson 
322899a2dd95SBruce Richardson 	if (ret < 0)
322999a2dd95SBruce Richardson 		return ret;
323099a2dd95SBruce Richardson 	num_xstats_filled = (unsigned int)ret;
323199a2dd95SBruce Richardson 
323299a2dd95SBruce Richardson 	/* Return all stats */
323399a2dd95SBruce Richardson 	if (!ids) {
323499a2dd95SBruce Richardson 		for (i = 0; i < num_xstats_filled; i++)
323599a2dd95SBruce Richardson 			values[i] = xstats[i].value;
323699a2dd95SBruce Richardson 		return expected_entries;
323799a2dd95SBruce Richardson 	}
323899a2dd95SBruce Richardson 
323999a2dd95SBruce Richardson 	/* Filter stats */
324099a2dd95SBruce Richardson 	for (i = 0; i < size; i++) {
324199a2dd95SBruce Richardson 		if (ids[i] >= expected_entries) {
324299a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR, "Id value isn't valid\n");
324399a2dd95SBruce Richardson 			return -1;
324499a2dd95SBruce Richardson 		}
324599a2dd95SBruce Richardson 		values[i] = xstats[ids[i]].value;
324699a2dd95SBruce Richardson 	}
324799a2dd95SBruce Richardson 	return size;
324899a2dd95SBruce Richardson }
324999a2dd95SBruce Richardson 
325099a2dd95SBruce Richardson int
325199a2dd95SBruce Richardson rte_eth_xstats_get(uint16_t port_id, struct rte_eth_xstat *xstats,
325299a2dd95SBruce Richardson 	unsigned int n)
325399a2dd95SBruce Richardson {
325499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
325599a2dd95SBruce Richardson 	unsigned int count = 0, i;
325699a2dd95SBruce Richardson 	signed int xcount = 0;
325799a2dd95SBruce Richardson 	uint16_t nb_rxqs, nb_txqs;
325899a2dd95SBruce Richardson 	int ret;
325999a2dd95SBruce Richardson 
326099a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
326199a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
326299a2dd95SBruce Richardson 
326399a2dd95SBruce Richardson 	nb_rxqs = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
326499a2dd95SBruce Richardson 	nb_txqs = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
326599a2dd95SBruce Richardson 
326699a2dd95SBruce Richardson 	/* Return generic statistics */
326799a2dd95SBruce Richardson 	count = RTE_NB_STATS;
326899a2dd95SBruce Richardson 	if (dev->data->dev_flags & RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS)
326999a2dd95SBruce Richardson 		count += (nb_rxqs * RTE_NB_RXQ_STATS) + (nb_txqs * RTE_NB_TXQ_STATS);
327099a2dd95SBruce Richardson 
327199a2dd95SBruce Richardson 	/* implemented by the driver */
327299a2dd95SBruce Richardson 	if (dev->dev_ops->xstats_get != NULL) {
327399a2dd95SBruce Richardson 		/* Retrieve the xstats from the driver at the end of the
327499a2dd95SBruce Richardson 		 * xstats struct.
327599a2dd95SBruce Richardson 		 */
327699a2dd95SBruce Richardson 		xcount = (*dev->dev_ops->xstats_get)(dev,
327799a2dd95SBruce Richardson 				     xstats ? xstats + count : NULL,
327899a2dd95SBruce Richardson 				     (n > count) ? n - count : 0);
327999a2dd95SBruce Richardson 
328099a2dd95SBruce Richardson 		if (xcount < 0)
328199a2dd95SBruce Richardson 			return eth_err(port_id, xcount);
328299a2dd95SBruce Richardson 	}
328399a2dd95SBruce Richardson 
328499a2dd95SBruce Richardson 	if (n < count + xcount || xstats == NULL)
328599a2dd95SBruce Richardson 		return count + xcount;
328699a2dd95SBruce Richardson 
328799a2dd95SBruce Richardson 	/* now fill the xstats structure */
328899a2dd95SBruce Richardson 	ret = eth_basic_stats_get(port_id, xstats);
328999a2dd95SBruce Richardson 	if (ret < 0)
329099a2dd95SBruce Richardson 		return ret;
329199a2dd95SBruce Richardson 	count = ret;
329299a2dd95SBruce Richardson 
329399a2dd95SBruce Richardson 	for (i = 0; i < count; i++)
329499a2dd95SBruce Richardson 		xstats[i].id = i;
329599a2dd95SBruce Richardson 	/* add an offset to driver-specific stats */
329699a2dd95SBruce Richardson 	for ( ; i < count + xcount; i++)
329799a2dd95SBruce Richardson 		xstats[i].id += count;
329899a2dd95SBruce Richardson 
329999a2dd95SBruce Richardson 	return count + xcount;
330099a2dd95SBruce Richardson }
330199a2dd95SBruce Richardson 
330299a2dd95SBruce Richardson /* reset ethdev extended statistics */
330399a2dd95SBruce Richardson int
330499a2dd95SBruce Richardson rte_eth_xstats_reset(uint16_t port_id)
330599a2dd95SBruce Richardson {
330699a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
330799a2dd95SBruce Richardson 
330899a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
330999a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
331099a2dd95SBruce Richardson 
331199a2dd95SBruce Richardson 	/* implemented by the driver */
331299a2dd95SBruce Richardson 	if (dev->dev_ops->xstats_reset != NULL)
331399a2dd95SBruce Richardson 		return eth_err(port_id, (*dev->dev_ops->xstats_reset)(dev));
331499a2dd95SBruce Richardson 
331599a2dd95SBruce Richardson 	/* fallback to default */
331699a2dd95SBruce Richardson 	return rte_eth_stats_reset(port_id);
331799a2dd95SBruce Richardson }
331899a2dd95SBruce Richardson 
331999a2dd95SBruce Richardson static int
332099a2dd95SBruce Richardson eth_dev_set_queue_stats_mapping(uint16_t port_id, uint16_t queue_id,
332199a2dd95SBruce Richardson 		uint8_t stat_idx, uint8_t is_rx)
332299a2dd95SBruce Richardson {
332399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
332499a2dd95SBruce Richardson 
332599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
332699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
332799a2dd95SBruce Richardson 
332899a2dd95SBruce Richardson 	if (is_rx && (queue_id >= dev->data->nb_rx_queues))
332999a2dd95SBruce Richardson 		return -EINVAL;
333099a2dd95SBruce Richardson 
333199a2dd95SBruce Richardson 	if (!is_rx && (queue_id >= dev->data->nb_tx_queues))
333299a2dd95SBruce Richardson 		return -EINVAL;
333399a2dd95SBruce Richardson 
333499a2dd95SBruce Richardson 	if (stat_idx >= RTE_ETHDEV_QUEUE_STAT_CNTRS)
333599a2dd95SBruce Richardson 		return -EINVAL;
333699a2dd95SBruce Richardson 
333753ef1b34SMin Hu (Connor) 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_stats_mapping_set, -ENOTSUP);
333853ef1b34SMin Hu (Connor) 	return (*dev->dev_ops->queue_stats_mapping_set) (dev, queue_id, stat_idx, is_rx);
333999a2dd95SBruce Richardson }
334099a2dd95SBruce Richardson 
334199a2dd95SBruce Richardson int
334299a2dd95SBruce Richardson rte_eth_dev_set_tx_queue_stats_mapping(uint16_t port_id, uint16_t tx_queue_id,
334399a2dd95SBruce Richardson 		uint8_t stat_idx)
334499a2dd95SBruce Richardson {
334599a2dd95SBruce Richardson 	return eth_err(port_id, eth_dev_set_queue_stats_mapping(port_id,
334699a2dd95SBruce Richardson 						tx_queue_id,
334799a2dd95SBruce Richardson 						stat_idx, STAT_QMAP_TX));
334899a2dd95SBruce Richardson }
334999a2dd95SBruce Richardson 
335099a2dd95SBruce Richardson int
335199a2dd95SBruce Richardson rte_eth_dev_set_rx_queue_stats_mapping(uint16_t port_id, uint16_t rx_queue_id,
335299a2dd95SBruce Richardson 		uint8_t stat_idx)
335399a2dd95SBruce Richardson {
335499a2dd95SBruce Richardson 	return eth_err(port_id, eth_dev_set_queue_stats_mapping(port_id,
335599a2dd95SBruce Richardson 						rx_queue_id,
335699a2dd95SBruce Richardson 						stat_idx, STAT_QMAP_RX));
335799a2dd95SBruce Richardson }
335899a2dd95SBruce Richardson 
335999a2dd95SBruce Richardson int
336099a2dd95SBruce Richardson rte_eth_dev_fw_version_get(uint16_t port_id, char *fw_version, size_t fw_size)
336199a2dd95SBruce Richardson {
336299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
336399a2dd95SBruce Richardson 
336499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
336599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
336699a2dd95SBruce Richardson 
336753ef1b34SMin Hu (Connor) 	if (fw_version == NULL && fw_size > 0) {
336853ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
336953ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u FW version to NULL when string size is non zero\n",
337053ef1b34SMin Hu (Connor) 			port_id);
337153ef1b34SMin Hu (Connor) 		return -EINVAL;
337253ef1b34SMin Hu (Connor) 	}
337353ef1b34SMin Hu (Connor) 
337499a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fw_version_get, -ENOTSUP);
337599a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->fw_version_get)(dev,
337699a2dd95SBruce Richardson 							fw_version, fw_size));
337799a2dd95SBruce Richardson }
337899a2dd95SBruce Richardson 
337999a2dd95SBruce Richardson int
338099a2dd95SBruce Richardson rte_eth_dev_info_get(uint16_t port_id, struct rte_eth_dev_info *dev_info)
338199a2dd95SBruce Richardson {
338299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
338399a2dd95SBruce Richardson 	const struct rte_eth_desc_lim lim = {
338499a2dd95SBruce Richardson 		.nb_max = UINT16_MAX,
338599a2dd95SBruce Richardson 		.nb_min = 0,
338699a2dd95SBruce Richardson 		.nb_align = 1,
338799a2dd95SBruce Richardson 		.nb_seg_max = UINT16_MAX,
338899a2dd95SBruce Richardson 		.nb_mtu_seg_max = UINT16_MAX,
338999a2dd95SBruce Richardson 	};
339099a2dd95SBruce Richardson 	int diag;
339199a2dd95SBruce Richardson 
339253ef1b34SMin Hu (Connor) 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
339353ef1b34SMin Hu (Connor) 	dev = &rte_eth_devices[port_id];
339453ef1b34SMin Hu (Connor) 
339553ef1b34SMin Hu (Connor) 	if (dev_info == NULL) {
339653ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot get ethdev port %u info to NULL\n",
339753ef1b34SMin Hu (Connor) 			port_id);
339853ef1b34SMin Hu (Connor) 		return -EINVAL;
339953ef1b34SMin Hu (Connor) 	}
340053ef1b34SMin Hu (Connor) 
340199a2dd95SBruce Richardson 	/*
340299a2dd95SBruce Richardson 	 * Init dev_info before port_id check since caller does not have
340399a2dd95SBruce Richardson 	 * return status and does not know if get is successful or not.
340499a2dd95SBruce Richardson 	 */
340599a2dd95SBruce Richardson 	memset(dev_info, 0, sizeof(struct rte_eth_dev_info));
340699a2dd95SBruce Richardson 	dev_info->switch_info.domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
340799a2dd95SBruce Richardson 
340899a2dd95SBruce Richardson 	dev_info->rx_desc_lim = lim;
340999a2dd95SBruce Richardson 	dev_info->tx_desc_lim = lim;
341099a2dd95SBruce Richardson 	dev_info->device = dev->device;
341199a2dd95SBruce Richardson 	dev_info->min_mtu = RTE_ETHER_MIN_MTU;
341299a2dd95SBruce Richardson 	dev_info->max_mtu = UINT16_MAX;
341399a2dd95SBruce Richardson 
341499a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
341599a2dd95SBruce Richardson 	diag = (*dev->dev_ops->dev_infos_get)(dev, dev_info);
341699a2dd95SBruce Richardson 	if (diag != 0) {
341799a2dd95SBruce Richardson 		/* Cleanup already filled in device information */
341899a2dd95SBruce Richardson 		memset(dev_info, 0, sizeof(struct rte_eth_dev_info));
341999a2dd95SBruce Richardson 		return eth_err(port_id, diag);
342099a2dd95SBruce Richardson 	}
342199a2dd95SBruce Richardson 
342299a2dd95SBruce Richardson 	/* Maximum number of queues should be <= RTE_MAX_QUEUES_PER_PORT */
342399a2dd95SBruce Richardson 	dev_info->max_rx_queues = RTE_MIN(dev_info->max_rx_queues,
342499a2dd95SBruce Richardson 			RTE_MAX_QUEUES_PER_PORT);
342599a2dd95SBruce Richardson 	dev_info->max_tx_queues = RTE_MIN(dev_info->max_tx_queues,
342699a2dd95SBruce Richardson 			RTE_MAX_QUEUES_PER_PORT);
342799a2dd95SBruce Richardson 
342899a2dd95SBruce Richardson 	dev_info->driver_name = dev->device->driver->name;
342999a2dd95SBruce Richardson 	dev_info->nb_rx_queues = dev->data->nb_rx_queues;
343099a2dd95SBruce Richardson 	dev_info->nb_tx_queues = dev->data->nb_tx_queues;
343199a2dd95SBruce Richardson 
343299a2dd95SBruce Richardson 	dev_info->dev_flags = &dev->data->dev_flags;
343399a2dd95SBruce Richardson 
343499a2dd95SBruce Richardson 	return 0;
343599a2dd95SBruce Richardson }
343699a2dd95SBruce Richardson 
343799a2dd95SBruce Richardson int
3438632be327SJie Wang rte_eth_dev_conf_get(uint16_t port_id, struct rte_eth_conf *dev_conf)
3439632be327SJie Wang {
3440632be327SJie Wang 	struct rte_eth_dev *dev;
3441632be327SJie Wang 
3442632be327SJie Wang 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3443632be327SJie Wang 	dev = &rte_eth_devices[port_id];
3444632be327SJie Wang 
3445632be327SJie Wang 	if (dev_conf == NULL) {
3446632be327SJie Wang 		RTE_ETHDEV_LOG(ERR,
3447632be327SJie Wang 			"Cannot get ethdev port %u configuration to NULL\n",
3448632be327SJie Wang 			port_id);
3449632be327SJie Wang 		return -EINVAL;
3450632be327SJie Wang 	}
3451632be327SJie Wang 
3452632be327SJie Wang 	memcpy(dev_conf, &dev->data->dev_conf, sizeof(struct rte_eth_conf));
3453632be327SJie Wang 
3454632be327SJie Wang 	return 0;
3455632be327SJie Wang }
3456632be327SJie Wang 
3457632be327SJie Wang int
345899a2dd95SBruce Richardson rte_eth_dev_get_supported_ptypes(uint16_t port_id, uint32_t ptype_mask,
345999a2dd95SBruce Richardson 				 uint32_t *ptypes, int num)
346099a2dd95SBruce Richardson {
346199a2dd95SBruce Richardson 	int i, j;
346299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
346399a2dd95SBruce Richardson 	const uint32_t *all_ptypes;
346499a2dd95SBruce Richardson 
346599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
346699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
346753ef1b34SMin Hu (Connor) 
346853ef1b34SMin Hu (Connor) 	if (ptypes == NULL && num > 0) {
346953ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
347053ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u supported packet types to NULL when array size is non zero\n",
347153ef1b34SMin Hu (Connor) 			port_id);
347253ef1b34SMin Hu (Connor) 		return -EINVAL;
347353ef1b34SMin Hu (Connor) 	}
347453ef1b34SMin Hu (Connor) 
347599a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_supported_ptypes_get, 0);
347699a2dd95SBruce Richardson 	all_ptypes = (*dev->dev_ops->dev_supported_ptypes_get)(dev);
347799a2dd95SBruce Richardson 
347899a2dd95SBruce Richardson 	if (!all_ptypes)
347999a2dd95SBruce Richardson 		return 0;
348099a2dd95SBruce Richardson 
348199a2dd95SBruce Richardson 	for (i = 0, j = 0; all_ptypes[i] != RTE_PTYPE_UNKNOWN; ++i)
348299a2dd95SBruce Richardson 		if (all_ptypes[i] & ptype_mask) {
348399a2dd95SBruce Richardson 			if (j < num)
348499a2dd95SBruce Richardson 				ptypes[j] = all_ptypes[i];
348599a2dd95SBruce Richardson 			j++;
348699a2dd95SBruce Richardson 		}
348799a2dd95SBruce Richardson 
348899a2dd95SBruce Richardson 	return j;
348999a2dd95SBruce Richardson }
349099a2dd95SBruce Richardson 
349199a2dd95SBruce Richardson int
349299a2dd95SBruce Richardson rte_eth_dev_set_ptypes(uint16_t port_id, uint32_t ptype_mask,
349399a2dd95SBruce Richardson 				 uint32_t *set_ptypes, unsigned int num)
349499a2dd95SBruce Richardson {
349599a2dd95SBruce Richardson 	const uint32_t valid_ptype_masks[] = {
349699a2dd95SBruce Richardson 		RTE_PTYPE_L2_MASK,
349799a2dd95SBruce Richardson 		RTE_PTYPE_L3_MASK,
349899a2dd95SBruce Richardson 		RTE_PTYPE_L4_MASK,
349999a2dd95SBruce Richardson 		RTE_PTYPE_TUNNEL_MASK,
350099a2dd95SBruce Richardson 		RTE_PTYPE_INNER_L2_MASK,
350199a2dd95SBruce Richardson 		RTE_PTYPE_INNER_L3_MASK,
350299a2dd95SBruce Richardson 		RTE_PTYPE_INNER_L4_MASK,
350399a2dd95SBruce Richardson 	};
350499a2dd95SBruce Richardson 	const uint32_t *all_ptypes;
350599a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
350699a2dd95SBruce Richardson 	uint32_t unused_mask;
350799a2dd95SBruce Richardson 	unsigned int i, j;
350899a2dd95SBruce Richardson 	int ret;
350999a2dd95SBruce Richardson 
351099a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
351199a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
351299a2dd95SBruce Richardson 
351353ef1b34SMin Hu (Connor) 	if (num > 0 && set_ptypes == NULL) {
351453ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
351553ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u set packet types to NULL when array size is non zero\n",
351653ef1b34SMin Hu (Connor) 			port_id);
351799a2dd95SBruce Richardson 		return -EINVAL;
351853ef1b34SMin Hu (Connor) 	}
351999a2dd95SBruce Richardson 
352099a2dd95SBruce Richardson 	if (*dev->dev_ops->dev_supported_ptypes_get == NULL ||
352199a2dd95SBruce Richardson 			*dev->dev_ops->dev_ptypes_set == NULL) {
352299a2dd95SBruce Richardson 		ret = 0;
352399a2dd95SBruce Richardson 		goto ptype_unknown;
352499a2dd95SBruce Richardson 	}
352599a2dd95SBruce Richardson 
352699a2dd95SBruce Richardson 	if (ptype_mask == 0) {
352799a2dd95SBruce Richardson 		ret = (*dev->dev_ops->dev_ptypes_set)(dev,
352899a2dd95SBruce Richardson 				ptype_mask);
352999a2dd95SBruce Richardson 		goto ptype_unknown;
353099a2dd95SBruce Richardson 	}
353199a2dd95SBruce Richardson 
353299a2dd95SBruce Richardson 	unused_mask = ptype_mask;
353399a2dd95SBruce Richardson 	for (i = 0; i < RTE_DIM(valid_ptype_masks); i++) {
353499a2dd95SBruce Richardson 		uint32_t mask = ptype_mask & valid_ptype_masks[i];
353599a2dd95SBruce Richardson 		if (mask && mask != valid_ptype_masks[i]) {
353699a2dd95SBruce Richardson 			ret = -EINVAL;
353799a2dd95SBruce Richardson 			goto ptype_unknown;
353899a2dd95SBruce Richardson 		}
353999a2dd95SBruce Richardson 		unused_mask &= ~valid_ptype_masks[i];
354099a2dd95SBruce Richardson 	}
354199a2dd95SBruce Richardson 
354299a2dd95SBruce Richardson 	if (unused_mask) {
354399a2dd95SBruce Richardson 		ret = -EINVAL;
354499a2dd95SBruce Richardson 		goto ptype_unknown;
354599a2dd95SBruce Richardson 	}
354699a2dd95SBruce Richardson 
354799a2dd95SBruce Richardson 	all_ptypes = (*dev->dev_ops->dev_supported_ptypes_get)(dev);
354899a2dd95SBruce Richardson 	if (all_ptypes == NULL) {
354999a2dd95SBruce Richardson 		ret = 0;
355099a2dd95SBruce Richardson 		goto ptype_unknown;
355199a2dd95SBruce Richardson 	}
355299a2dd95SBruce Richardson 
355399a2dd95SBruce Richardson 	/*
355499a2dd95SBruce Richardson 	 * Accommodate as many set_ptypes as possible. If the supplied
355599a2dd95SBruce Richardson 	 * set_ptypes array is insufficient fill it partially.
355699a2dd95SBruce Richardson 	 */
355799a2dd95SBruce Richardson 	for (i = 0, j = 0; set_ptypes != NULL &&
355899a2dd95SBruce Richardson 				(all_ptypes[i] != RTE_PTYPE_UNKNOWN); ++i) {
355999a2dd95SBruce Richardson 		if (ptype_mask & all_ptypes[i]) {
356099a2dd95SBruce Richardson 			if (j < num - 1) {
356199a2dd95SBruce Richardson 				set_ptypes[j] = all_ptypes[i];
356299a2dd95SBruce Richardson 				j++;
356399a2dd95SBruce Richardson 				continue;
356499a2dd95SBruce Richardson 			}
356599a2dd95SBruce Richardson 			break;
356699a2dd95SBruce Richardson 		}
356799a2dd95SBruce Richardson 	}
356899a2dd95SBruce Richardson 
356999a2dd95SBruce Richardson 	if (set_ptypes != NULL && j < num)
357099a2dd95SBruce Richardson 		set_ptypes[j] = RTE_PTYPE_UNKNOWN;
357199a2dd95SBruce Richardson 
357299a2dd95SBruce Richardson 	return (*dev->dev_ops->dev_ptypes_set)(dev, ptype_mask);
357399a2dd95SBruce Richardson 
357499a2dd95SBruce Richardson ptype_unknown:
357599a2dd95SBruce Richardson 	if (num > 0)
357699a2dd95SBruce Richardson 		set_ptypes[0] = RTE_PTYPE_UNKNOWN;
357799a2dd95SBruce Richardson 
357899a2dd95SBruce Richardson 	return ret;
357999a2dd95SBruce Richardson }
358099a2dd95SBruce Richardson 
358199a2dd95SBruce Richardson int
358227a300e6SKonstantin Ananyev rte_eth_macaddrs_get(uint16_t port_id, struct rte_ether_addr *ma,
358327a300e6SKonstantin Ananyev 	unsigned int num)
358427a300e6SKonstantin Ananyev {
358527a300e6SKonstantin Ananyev 	int32_t ret;
358627a300e6SKonstantin Ananyev 	struct rte_eth_dev *dev;
358727a300e6SKonstantin Ananyev 	struct rte_eth_dev_info dev_info;
358827a300e6SKonstantin Ananyev 
358927a300e6SKonstantin Ananyev 	if (ma == NULL) {
359027a300e6SKonstantin Ananyev 		RTE_ETHDEV_LOG(ERR, "%s: invalid parameters\n", __func__);
359127a300e6SKonstantin Ananyev 		return -EINVAL;
359227a300e6SKonstantin Ananyev 	}
359327a300e6SKonstantin Ananyev 
359427a300e6SKonstantin Ananyev 	/* will check for us that port_id is a valid one */
359527a300e6SKonstantin Ananyev 	ret = rte_eth_dev_info_get(port_id, &dev_info);
359627a300e6SKonstantin Ananyev 	if (ret != 0)
359727a300e6SKonstantin Ananyev 		return ret;
359827a300e6SKonstantin Ananyev 
359927a300e6SKonstantin Ananyev 	dev = &rte_eth_devices[port_id];
360027a300e6SKonstantin Ananyev 	num = RTE_MIN(dev_info.max_mac_addrs, num);
360127a300e6SKonstantin Ananyev 	memcpy(ma, dev->data->mac_addrs, num * sizeof(ma[0]));
360227a300e6SKonstantin Ananyev 
360327a300e6SKonstantin Ananyev 	return num;
360427a300e6SKonstantin Ananyev }
360527a300e6SKonstantin Ananyev 
360627a300e6SKonstantin Ananyev int
360799a2dd95SBruce Richardson rte_eth_macaddr_get(uint16_t port_id, struct rte_ether_addr *mac_addr)
360899a2dd95SBruce Richardson {
360999a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
361099a2dd95SBruce Richardson 
361199a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
361299a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
361353ef1b34SMin Hu (Connor) 
361453ef1b34SMin Hu (Connor) 	if (mac_addr == NULL) {
361553ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
361653ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u MAC address to NULL\n",
361753ef1b34SMin Hu (Connor) 			port_id);
361853ef1b34SMin Hu (Connor) 		return -EINVAL;
361953ef1b34SMin Hu (Connor) 	}
362053ef1b34SMin Hu (Connor) 
362199a2dd95SBruce Richardson 	rte_ether_addr_copy(&dev->data->mac_addrs[0], mac_addr);
362299a2dd95SBruce Richardson 
362399a2dd95SBruce Richardson 	return 0;
362499a2dd95SBruce Richardson }
362599a2dd95SBruce Richardson 
362699a2dd95SBruce Richardson int
362799a2dd95SBruce Richardson rte_eth_dev_get_mtu(uint16_t port_id, uint16_t *mtu)
362899a2dd95SBruce Richardson {
362999a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
363099a2dd95SBruce Richardson 
363199a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
363299a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
363353ef1b34SMin Hu (Connor) 
363453ef1b34SMin Hu (Connor) 	if (mtu == NULL) {
363553ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot get ethdev port %u MTU to NULL\n",
363653ef1b34SMin Hu (Connor) 			port_id);
363753ef1b34SMin Hu (Connor) 		return -EINVAL;
363853ef1b34SMin Hu (Connor) 	}
363953ef1b34SMin Hu (Connor) 
364099a2dd95SBruce Richardson 	*mtu = dev->data->mtu;
364199a2dd95SBruce Richardson 	return 0;
364299a2dd95SBruce Richardson }
364399a2dd95SBruce Richardson 
364499a2dd95SBruce Richardson int
364599a2dd95SBruce Richardson rte_eth_dev_set_mtu(uint16_t port_id, uint16_t mtu)
364699a2dd95SBruce Richardson {
364799a2dd95SBruce Richardson 	int ret;
364899a2dd95SBruce Richardson 	struct rte_eth_dev_info dev_info;
364999a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
3650*dd4e429cSFerruh Yigit 	int is_jumbo_frame_capable = 0;
365199a2dd95SBruce Richardson 
365299a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
365399a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
365499a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mtu_set, -ENOTSUP);
365599a2dd95SBruce Richardson 
365699a2dd95SBruce Richardson 	/*
365799a2dd95SBruce Richardson 	 * Check if the device supports dev_infos_get, if it does not
365899a2dd95SBruce Richardson 	 * skip min_mtu/max_mtu validation here as this requires values
365999a2dd95SBruce Richardson 	 * that are populated within the call to rte_eth_dev_info_get()
366099a2dd95SBruce Richardson 	 * which relies on dev->dev_ops->dev_infos_get.
366199a2dd95SBruce Richardson 	 */
366299a2dd95SBruce Richardson 	if (*dev->dev_ops->dev_infos_get != NULL) {
366399a2dd95SBruce Richardson 		ret = rte_eth_dev_info_get(port_id, &dev_info);
366499a2dd95SBruce Richardson 		if (ret != 0)
366599a2dd95SBruce Richardson 			return ret;
366699a2dd95SBruce Richardson 
366799a2dd95SBruce Richardson 		if (mtu < dev_info.min_mtu || mtu > dev_info.max_mtu)
366899a2dd95SBruce Richardson 			return -EINVAL;
3669*dd4e429cSFerruh Yigit 
3670*dd4e429cSFerruh Yigit 		if ((dev_info.rx_offload_capa & DEV_RX_OFFLOAD_JUMBO_FRAME) != 0)
3671*dd4e429cSFerruh Yigit 			is_jumbo_frame_capable = 1;
367299a2dd95SBruce Richardson 	}
367399a2dd95SBruce Richardson 
3674*dd4e429cSFerruh Yigit 	if (mtu > RTE_ETHER_MTU && is_jumbo_frame_capable == 0)
3675*dd4e429cSFerruh Yigit 		return -EINVAL;
3676*dd4e429cSFerruh Yigit 
367799a2dd95SBruce Richardson 	ret = (*dev->dev_ops->mtu_set)(dev, mtu);
3678*dd4e429cSFerruh Yigit 	if (ret == 0) {
367999a2dd95SBruce Richardson 		dev->data->mtu = mtu;
368099a2dd95SBruce Richardson 
3681*dd4e429cSFerruh Yigit 		/* switch to jumbo mode if needed */
3682*dd4e429cSFerruh Yigit 		if (mtu > RTE_ETHER_MTU)
3683*dd4e429cSFerruh Yigit 			dev->data->dev_conf.rxmode.offloads |=
3684*dd4e429cSFerruh Yigit 				DEV_RX_OFFLOAD_JUMBO_FRAME;
3685*dd4e429cSFerruh Yigit 		else
3686*dd4e429cSFerruh Yigit 			dev->data->dev_conf.rxmode.offloads &=
3687*dd4e429cSFerruh Yigit 				~DEV_RX_OFFLOAD_JUMBO_FRAME;
3688*dd4e429cSFerruh Yigit 	}
3689*dd4e429cSFerruh Yigit 
369099a2dd95SBruce Richardson 	return eth_err(port_id, ret);
369199a2dd95SBruce Richardson }
369299a2dd95SBruce Richardson 
369399a2dd95SBruce Richardson int
369499a2dd95SBruce Richardson rte_eth_dev_vlan_filter(uint16_t port_id, uint16_t vlan_id, int on)
369599a2dd95SBruce Richardson {
369699a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
369799a2dd95SBruce Richardson 	int ret;
369899a2dd95SBruce Richardson 
369999a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
370099a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
370153ef1b34SMin Hu (Connor) 
370299a2dd95SBruce Richardson 	if (!(dev->data->dev_conf.rxmode.offloads &
370399a2dd95SBruce Richardson 	      DEV_RX_OFFLOAD_VLAN_FILTER)) {
370499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Port %u: vlan-filtering disabled\n",
370599a2dd95SBruce Richardson 			port_id);
370699a2dd95SBruce Richardson 		return -ENOSYS;
370799a2dd95SBruce Richardson 	}
370899a2dd95SBruce Richardson 
370999a2dd95SBruce Richardson 	if (vlan_id > 4095) {
371099a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Port_id=%u invalid vlan_id=%u > 4095\n",
371199a2dd95SBruce Richardson 			port_id, vlan_id);
371299a2dd95SBruce Richardson 		return -EINVAL;
371399a2dd95SBruce Richardson 	}
371499a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_filter_set, -ENOTSUP);
371599a2dd95SBruce Richardson 
371699a2dd95SBruce Richardson 	ret = (*dev->dev_ops->vlan_filter_set)(dev, vlan_id, on);
371799a2dd95SBruce Richardson 	if (ret == 0) {
371899a2dd95SBruce Richardson 		struct rte_vlan_filter_conf *vfc;
371999a2dd95SBruce Richardson 		int vidx;
372099a2dd95SBruce Richardson 		int vbit;
372199a2dd95SBruce Richardson 
372299a2dd95SBruce Richardson 		vfc = &dev->data->vlan_filter_conf;
372399a2dd95SBruce Richardson 		vidx = vlan_id / 64;
372499a2dd95SBruce Richardson 		vbit = vlan_id % 64;
372599a2dd95SBruce Richardson 
372699a2dd95SBruce Richardson 		if (on)
372799a2dd95SBruce Richardson 			vfc->ids[vidx] |= UINT64_C(1) << vbit;
372899a2dd95SBruce Richardson 		else
372999a2dd95SBruce Richardson 			vfc->ids[vidx] &= ~(UINT64_C(1) << vbit);
373099a2dd95SBruce Richardson 	}
373199a2dd95SBruce Richardson 
373299a2dd95SBruce Richardson 	return eth_err(port_id, ret);
373399a2dd95SBruce Richardson }
373499a2dd95SBruce Richardson 
373599a2dd95SBruce Richardson int
373699a2dd95SBruce Richardson rte_eth_dev_set_vlan_strip_on_queue(uint16_t port_id, uint16_t rx_queue_id,
373799a2dd95SBruce Richardson 				    int on)
373899a2dd95SBruce Richardson {
373999a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
374099a2dd95SBruce Richardson 
374199a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
374299a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
374353ef1b34SMin Hu (Connor) 
374499a2dd95SBruce Richardson 	if (rx_queue_id >= dev->data->nb_rx_queues) {
374599a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid rx_queue_id=%u\n", rx_queue_id);
374699a2dd95SBruce Richardson 		return -EINVAL;
374799a2dd95SBruce Richardson 	}
374899a2dd95SBruce Richardson 
374999a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_strip_queue_set, -ENOTSUP);
375099a2dd95SBruce Richardson 	(*dev->dev_ops->vlan_strip_queue_set)(dev, rx_queue_id, on);
375199a2dd95SBruce Richardson 
375299a2dd95SBruce Richardson 	return 0;
375399a2dd95SBruce Richardson }
375499a2dd95SBruce Richardson 
375599a2dd95SBruce Richardson int
375699a2dd95SBruce Richardson rte_eth_dev_set_vlan_ether_type(uint16_t port_id,
375799a2dd95SBruce Richardson 				enum rte_vlan_type vlan_type,
375899a2dd95SBruce Richardson 				uint16_t tpid)
375999a2dd95SBruce Richardson {
376099a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
376199a2dd95SBruce Richardson 
376299a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
376399a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
376499a2dd95SBruce Richardson 
376553ef1b34SMin Hu (Connor) 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_tpid_set, -ENOTSUP);
376699a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->vlan_tpid_set)(dev, vlan_type,
376799a2dd95SBruce Richardson 							       tpid));
376899a2dd95SBruce Richardson }
376999a2dd95SBruce Richardson 
377099a2dd95SBruce Richardson int
377199a2dd95SBruce Richardson rte_eth_dev_set_vlan_offload(uint16_t port_id, int offload_mask)
377299a2dd95SBruce Richardson {
377399a2dd95SBruce Richardson 	struct rte_eth_dev_info dev_info;
377499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
377599a2dd95SBruce Richardson 	int ret = 0;
377699a2dd95SBruce Richardson 	int mask = 0;
377799a2dd95SBruce Richardson 	int cur, org = 0;
377899a2dd95SBruce Richardson 	uint64_t orig_offloads;
377999a2dd95SBruce Richardson 	uint64_t dev_offloads;
378099a2dd95SBruce Richardson 	uint64_t new_offloads;
378199a2dd95SBruce Richardson 
378299a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
378399a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
378499a2dd95SBruce Richardson 
378599a2dd95SBruce Richardson 	/* save original values in case of failure */
378699a2dd95SBruce Richardson 	orig_offloads = dev->data->dev_conf.rxmode.offloads;
378799a2dd95SBruce Richardson 	dev_offloads = orig_offloads;
378899a2dd95SBruce Richardson 
378999a2dd95SBruce Richardson 	/* check which option changed by application */
379099a2dd95SBruce Richardson 	cur = !!(offload_mask & ETH_VLAN_STRIP_OFFLOAD);
379199a2dd95SBruce Richardson 	org = !!(dev_offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
379299a2dd95SBruce Richardson 	if (cur != org) {
379399a2dd95SBruce Richardson 		if (cur)
379499a2dd95SBruce Richardson 			dev_offloads |= DEV_RX_OFFLOAD_VLAN_STRIP;
379599a2dd95SBruce Richardson 		else
379699a2dd95SBruce Richardson 			dev_offloads &= ~DEV_RX_OFFLOAD_VLAN_STRIP;
379799a2dd95SBruce Richardson 		mask |= ETH_VLAN_STRIP_MASK;
379899a2dd95SBruce Richardson 	}
379999a2dd95SBruce Richardson 
380099a2dd95SBruce Richardson 	cur = !!(offload_mask & ETH_VLAN_FILTER_OFFLOAD);
380199a2dd95SBruce Richardson 	org = !!(dev_offloads & DEV_RX_OFFLOAD_VLAN_FILTER);
380299a2dd95SBruce Richardson 	if (cur != org) {
380399a2dd95SBruce Richardson 		if (cur)
380499a2dd95SBruce Richardson 			dev_offloads |= DEV_RX_OFFLOAD_VLAN_FILTER;
380599a2dd95SBruce Richardson 		else
380699a2dd95SBruce Richardson 			dev_offloads &= ~DEV_RX_OFFLOAD_VLAN_FILTER;
380799a2dd95SBruce Richardson 		mask |= ETH_VLAN_FILTER_MASK;
380899a2dd95SBruce Richardson 	}
380999a2dd95SBruce Richardson 
381099a2dd95SBruce Richardson 	cur = !!(offload_mask & ETH_VLAN_EXTEND_OFFLOAD);
381199a2dd95SBruce Richardson 	org = !!(dev_offloads & DEV_RX_OFFLOAD_VLAN_EXTEND);
381299a2dd95SBruce Richardson 	if (cur != org) {
381399a2dd95SBruce Richardson 		if (cur)
381499a2dd95SBruce Richardson 			dev_offloads |= DEV_RX_OFFLOAD_VLAN_EXTEND;
381599a2dd95SBruce Richardson 		else
381699a2dd95SBruce Richardson 			dev_offloads &= ~DEV_RX_OFFLOAD_VLAN_EXTEND;
381799a2dd95SBruce Richardson 		mask |= ETH_VLAN_EXTEND_MASK;
381899a2dd95SBruce Richardson 	}
381999a2dd95SBruce Richardson 
382099a2dd95SBruce Richardson 	cur = !!(offload_mask & ETH_QINQ_STRIP_OFFLOAD);
382199a2dd95SBruce Richardson 	org = !!(dev_offloads & DEV_RX_OFFLOAD_QINQ_STRIP);
382299a2dd95SBruce Richardson 	if (cur != org) {
382399a2dd95SBruce Richardson 		if (cur)
382499a2dd95SBruce Richardson 			dev_offloads |= DEV_RX_OFFLOAD_QINQ_STRIP;
382599a2dd95SBruce Richardson 		else
382699a2dd95SBruce Richardson 			dev_offloads &= ~DEV_RX_OFFLOAD_QINQ_STRIP;
382799a2dd95SBruce Richardson 		mask |= ETH_QINQ_STRIP_MASK;
382899a2dd95SBruce Richardson 	}
382999a2dd95SBruce Richardson 
383099a2dd95SBruce Richardson 	/*no change*/
383199a2dd95SBruce Richardson 	if (mask == 0)
383299a2dd95SBruce Richardson 		return ret;
383399a2dd95SBruce Richardson 
383499a2dd95SBruce Richardson 	ret = rte_eth_dev_info_get(port_id, &dev_info);
383599a2dd95SBruce Richardson 	if (ret != 0)
383699a2dd95SBruce Richardson 		return ret;
383799a2dd95SBruce Richardson 
383899a2dd95SBruce Richardson 	/* Rx VLAN offloading must be within its device capabilities */
383999a2dd95SBruce Richardson 	if ((dev_offloads & dev_info.rx_offload_capa) != dev_offloads) {
384099a2dd95SBruce Richardson 		new_offloads = dev_offloads & ~orig_offloads;
384199a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
384299a2dd95SBruce Richardson 			"Ethdev port_id=%u requested new added VLAN offloads "
384399a2dd95SBruce Richardson 			"0x%" PRIx64 " must be within Rx offloads capabilities "
384499a2dd95SBruce Richardson 			"0x%" PRIx64 " in %s()\n",
384599a2dd95SBruce Richardson 			port_id, new_offloads, dev_info.rx_offload_capa,
384699a2dd95SBruce Richardson 			__func__);
384799a2dd95SBruce Richardson 		return -EINVAL;
384899a2dd95SBruce Richardson 	}
384999a2dd95SBruce Richardson 
385099a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_offload_set, -ENOTSUP);
385199a2dd95SBruce Richardson 	dev->data->dev_conf.rxmode.offloads = dev_offloads;
385299a2dd95SBruce Richardson 	ret = (*dev->dev_ops->vlan_offload_set)(dev, mask);
385399a2dd95SBruce Richardson 	if (ret) {
385499a2dd95SBruce Richardson 		/* hit an error restore  original values */
385599a2dd95SBruce Richardson 		dev->data->dev_conf.rxmode.offloads = orig_offloads;
385699a2dd95SBruce Richardson 	}
385799a2dd95SBruce Richardson 
385899a2dd95SBruce Richardson 	return eth_err(port_id, ret);
385999a2dd95SBruce Richardson }
386099a2dd95SBruce Richardson 
386199a2dd95SBruce Richardson int
386299a2dd95SBruce Richardson rte_eth_dev_get_vlan_offload(uint16_t port_id)
386399a2dd95SBruce Richardson {
386499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
386599a2dd95SBruce Richardson 	uint64_t *dev_offloads;
386699a2dd95SBruce Richardson 	int ret = 0;
386799a2dd95SBruce Richardson 
386899a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
386999a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
387099a2dd95SBruce Richardson 	dev_offloads = &dev->data->dev_conf.rxmode.offloads;
387199a2dd95SBruce Richardson 
387299a2dd95SBruce Richardson 	if (*dev_offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
387399a2dd95SBruce Richardson 		ret |= ETH_VLAN_STRIP_OFFLOAD;
387499a2dd95SBruce Richardson 
387599a2dd95SBruce Richardson 	if (*dev_offloads & DEV_RX_OFFLOAD_VLAN_FILTER)
387699a2dd95SBruce Richardson 		ret |= ETH_VLAN_FILTER_OFFLOAD;
387799a2dd95SBruce Richardson 
387899a2dd95SBruce Richardson 	if (*dev_offloads & DEV_RX_OFFLOAD_VLAN_EXTEND)
387999a2dd95SBruce Richardson 		ret |= ETH_VLAN_EXTEND_OFFLOAD;
388099a2dd95SBruce Richardson 
388199a2dd95SBruce Richardson 	if (*dev_offloads & DEV_RX_OFFLOAD_QINQ_STRIP)
388299a2dd95SBruce Richardson 		ret |= ETH_QINQ_STRIP_OFFLOAD;
388399a2dd95SBruce Richardson 
388499a2dd95SBruce Richardson 	return ret;
388599a2dd95SBruce Richardson }
388699a2dd95SBruce Richardson 
388799a2dd95SBruce Richardson int
388899a2dd95SBruce Richardson rte_eth_dev_set_vlan_pvid(uint16_t port_id, uint16_t pvid, int on)
388999a2dd95SBruce Richardson {
389099a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
389199a2dd95SBruce Richardson 
389299a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
389399a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
389499a2dd95SBruce Richardson 
389553ef1b34SMin Hu (Connor) 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_pvid_set, -ENOTSUP);
389699a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->vlan_pvid_set)(dev, pvid, on));
389799a2dd95SBruce Richardson }
389899a2dd95SBruce Richardson 
389999a2dd95SBruce Richardson int
390099a2dd95SBruce Richardson rte_eth_dev_flow_ctrl_get(uint16_t port_id, struct rte_eth_fc_conf *fc_conf)
390199a2dd95SBruce Richardson {
390299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
390399a2dd95SBruce Richardson 
390499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
390599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
390653ef1b34SMin Hu (Connor) 
390753ef1b34SMin Hu (Connor) 	if (fc_conf == NULL) {
390853ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
390953ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u flow control config to NULL\n",
391053ef1b34SMin Hu (Connor) 			port_id);
391153ef1b34SMin Hu (Connor) 		return -EINVAL;
391253ef1b34SMin Hu (Connor) 	}
391353ef1b34SMin Hu (Connor) 
391499a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_get, -ENOTSUP);
391599a2dd95SBruce Richardson 	memset(fc_conf, 0, sizeof(*fc_conf));
391699a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->flow_ctrl_get)(dev, fc_conf));
391799a2dd95SBruce Richardson }
391899a2dd95SBruce Richardson 
391999a2dd95SBruce Richardson int
392099a2dd95SBruce Richardson rte_eth_dev_flow_ctrl_set(uint16_t port_id, struct rte_eth_fc_conf *fc_conf)
392199a2dd95SBruce Richardson {
392299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
392399a2dd95SBruce Richardson 
392499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
392553ef1b34SMin Hu (Connor) 	dev = &rte_eth_devices[port_id];
392653ef1b34SMin Hu (Connor) 
392753ef1b34SMin Hu (Connor) 	if (fc_conf == NULL) {
392853ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
392953ef1b34SMin Hu (Connor) 			"Cannot set ethdev port %u flow control from NULL config\n",
393053ef1b34SMin Hu (Connor) 			port_id);
393153ef1b34SMin Hu (Connor) 		return -EINVAL;
393253ef1b34SMin Hu (Connor) 	}
393353ef1b34SMin Hu (Connor) 
393499a2dd95SBruce Richardson 	if ((fc_conf->send_xon != 0) && (fc_conf->send_xon != 1)) {
393599a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid send_xon, only 0/1 allowed\n");
393699a2dd95SBruce Richardson 		return -EINVAL;
393799a2dd95SBruce Richardson 	}
393899a2dd95SBruce Richardson 
393999a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_set, -ENOTSUP);
394099a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->flow_ctrl_set)(dev, fc_conf));
394199a2dd95SBruce Richardson }
394299a2dd95SBruce Richardson 
394399a2dd95SBruce Richardson int
394499a2dd95SBruce Richardson rte_eth_dev_priority_flow_ctrl_set(uint16_t port_id,
394599a2dd95SBruce Richardson 				   struct rte_eth_pfc_conf *pfc_conf)
394699a2dd95SBruce Richardson {
394799a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
394899a2dd95SBruce Richardson 
394999a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
395053ef1b34SMin Hu (Connor) 	dev = &rte_eth_devices[port_id];
395153ef1b34SMin Hu (Connor) 
395253ef1b34SMin Hu (Connor) 	if (pfc_conf == NULL) {
395353ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
395453ef1b34SMin Hu (Connor) 			"Cannot set ethdev port %u priority flow control from NULL config\n",
395553ef1b34SMin Hu (Connor) 			port_id);
395653ef1b34SMin Hu (Connor) 		return -EINVAL;
395753ef1b34SMin Hu (Connor) 	}
395853ef1b34SMin Hu (Connor) 
395999a2dd95SBruce Richardson 	if (pfc_conf->priority > (ETH_DCB_NUM_USER_PRIORITIES - 1)) {
396099a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid priority, only 0-7 allowed\n");
396199a2dd95SBruce Richardson 		return -EINVAL;
396299a2dd95SBruce Richardson 	}
396399a2dd95SBruce Richardson 
396499a2dd95SBruce Richardson 	/* High water, low water validation are device specific */
396599a2dd95SBruce Richardson 	if  (*dev->dev_ops->priority_flow_ctrl_set)
396699a2dd95SBruce Richardson 		return eth_err(port_id, (*dev->dev_ops->priority_flow_ctrl_set)
396799a2dd95SBruce Richardson 					(dev, pfc_conf));
396899a2dd95SBruce Richardson 	return -ENOTSUP;
396999a2dd95SBruce Richardson }
397099a2dd95SBruce Richardson 
397199a2dd95SBruce Richardson static int
397299a2dd95SBruce Richardson eth_check_reta_mask(struct rte_eth_rss_reta_entry64 *reta_conf,
397399a2dd95SBruce Richardson 			uint16_t reta_size)
397499a2dd95SBruce Richardson {
397599a2dd95SBruce Richardson 	uint16_t i, num;
397699a2dd95SBruce Richardson 
397799a2dd95SBruce Richardson 	num = (reta_size + RTE_RETA_GROUP_SIZE - 1) / RTE_RETA_GROUP_SIZE;
397899a2dd95SBruce Richardson 	for (i = 0; i < num; i++) {
397999a2dd95SBruce Richardson 		if (reta_conf[i].mask)
398099a2dd95SBruce Richardson 			return 0;
398199a2dd95SBruce Richardson 	}
398299a2dd95SBruce Richardson 
398399a2dd95SBruce Richardson 	return -EINVAL;
398499a2dd95SBruce Richardson }
398599a2dd95SBruce Richardson 
398699a2dd95SBruce Richardson static int
398799a2dd95SBruce Richardson eth_check_reta_entry(struct rte_eth_rss_reta_entry64 *reta_conf,
398899a2dd95SBruce Richardson 			 uint16_t reta_size,
398999a2dd95SBruce Richardson 			 uint16_t max_rxq)
399099a2dd95SBruce Richardson {
399199a2dd95SBruce Richardson 	uint16_t i, idx, shift;
399299a2dd95SBruce Richardson 
399399a2dd95SBruce Richardson 	if (max_rxq == 0) {
399499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "No receive queue is available\n");
399599a2dd95SBruce Richardson 		return -EINVAL;
399699a2dd95SBruce Richardson 	}
399799a2dd95SBruce Richardson 
399899a2dd95SBruce Richardson 	for (i = 0; i < reta_size; i++) {
399999a2dd95SBruce Richardson 		idx = i / RTE_RETA_GROUP_SIZE;
400099a2dd95SBruce Richardson 		shift = i % RTE_RETA_GROUP_SIZE;
400199a2dd95SBruce Richardson 		if ((reta_conf[idx].mask & (1ULL << shift)) &&
400299a2dd95SBruce Richardson 			(reta_conf[idx].reta[shift] >= max_rxq)) {
400399a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
400499a2dd95SBruce Richardson 				"reta_conf[%u]->reta[%u]: %u exceeds the maximum rxq index: %u\n",
400599a2dd95SBruce Richardson 				idx, shift,
400699a2dd95SBruce Richardson 				reta_conf[idx].reta[shift], max_rxq);
400799a2dd95SBruce Richardson 			return -EINVAL;
400899a2dd95SBruce Richardson 		}
400999a2dd95SBruce Richardson 	}
401099a2dd95SBruce Richardson 
401199a2dd95SBruce Richardson 	return 0;
401299a2dd95SBruce Richardson }
401399a2dd95SBruce Richardson 
401499a2dd95SBruce Richardson int
401599a2dd95SBruce Richardson rte_eth_dev_rss_reta_update(uint16_t port_id,
401699a2dd95SBruce Richardson 			    struct rte_eth_rss_reta_entry64 *reta_conf,
401799a2dd95SBruce Richardson 			    uint16_t reta_size)
401899a2dd95SBruce Richardson {
401999a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
402099a2dd95SBruce Richardson 	int ret;
402199a2dd95SBruce Richardson 
402299a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
402353ef1b34SMin Hu (Connor) 	dev = &rte_eth_devices[port_id];
402453ef1b34SMin Hu (Connor) 
402553ef1b34SMin Hu (Connor) 	if (reta_conf == NULL) {
402653ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
402753ef1b34SMin Hu (Connor) 			"Cannot update ethdev port %u RSS RETA to NULL\n",
402853ef1b34SMin Hu (Connor) 			port_id);
402953ef1b34SMin Hu (Connor) 		return -EINVAL;
403053ef1b34SMin Hu (Connor) 	}
403153ef1b34SMin Hu (Connor) 
403253ef1b34SMin Hu (Connor) 	if (reta_size == 0) {
403353ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
403453ef1b34SMin Hu (Connor) 			"Cannot update ethdev port %u RSS RETA with zero size\n",
403553ef1b34SMin Hu (Connor) 			port_id);
403653ef1b34SMin Hu (Connor) 		return -EINVAL;
403753ef1b34SMin Hu (Connor) 	}
403853ef1b34SMin Hu (Connor) 
403999a2dd95SBruce Richardson 	/* Check mask bits */
404099a2dd95SBruce Richardson 	ret = eth_check_reta_mask(reta_conf, reta_size);
404199a2dd95SBruce Richardson 	if (ret < 0)
404299a2dd95SBruce Richardson 		return ret;
404399a2dd95SBruce Richardson 
404499a2dd95SBruce Richardson 	/* Check entry value */
404599a2dd95SBruce Richardson 	ret = eth_check_reta_entry(reta_conf, reta_size,
404699a2dd95SBruce Richardson 				dev->data->nb_rx_queues);
404799a2dd95SBruce Richardson 	if (ret < 0)
404899a2dd95SBruce Richardson 		return ret;
404999a2dd95SBruce Richardson 
405099a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->reta_update, -ENOTSUP);
405199a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->reta_update)(dev, reta_conf,
405299a2dd95SBruce Richardson 							     reta_size));
405399a2dd95SBruce Richardson }
405499a2dd95SBruce Richardson 
405599a2dd95SBruce Richardson int
405699a2dd95SBruce Richardson rte_eth_dev_rss_reta_query(uint16_t port_id,
405799a2dd95SBruce Richardson 			   struct rte_eth_rss_reta_entry64 *reta_conf,
405899a2dd95SBruce Richardson 			   uint16_t reta_size)
405999a2dd95SBruce Richardson {
406099a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
406199a2dd95SBruce Richardson 	int ret;
406299a2dd95SBruce Richardson 
406399a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
406453ef1b34SMin Hu (Connor) 	dev = &rte_eth_devices[port_id];
406553ef1b34SMin Hu (Connor) 
406653ef1b34SMin Hu (Connor) 	if (reta_conf == NULL) {
406753ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
406853ef1b34SMin Hu (Connor) 			"Cannot query ethdev port %u RSS RETA from NULL config\n",
406953ef1b34SMin Hu (Connor) 			port_id);
407053ef1b34SMin Hu (Connor) 		return -EINVAL;
407153ef1b34SMin Hu (Connor) 	}
407299a2dd95SBruce Richardson 
407399a2dd95SBruce Richardson 	/* Check mask bits */
407499a2dd95SBruce Richardson 	ret = eth_check_reta_mask(reta_conf, reta_size);
407599a2dd95SBruce Richardson 	if (ret < 0)
407699a2dd95SBruce Richardson 		return ret;
407799a2dd95SBruce Richardson 
407899a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->reta_query, -ENOTSUP);
407999a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->reta_query)(dev, reta_conf,
408099a2dd95SBruce Richardson 							    reta_size));
408199a2dd95SBruce Richardson }
408299a2dd95SBruce Richardson 
408399a2dd95SBruce Richardson int
408499a2dd95SBruce Richardson rte_eth_dev_rss_hash_update(uint16_t port_id,
408599a2dd95SBruce Richardson 			    struct rte_eth_rss_conf *rss_conf)
408699a2dd95SBruce Richardson {
408799a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
408899a2dd95SBruce Richardson 	struct rte_eth_dev_info dev_info = { .flow_type_rss_offloads = 0, };
408999a2dd95SBruce Richardson 	int ret;
409099a2dd95SBruce Richardson 
409199a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
409253ef1b34SMin Hu (Connor) 	dev = &rte_eth_devices[port_id];
409353ef1b34SMin Hu (Connor) 
409453ef1b34SMin Hu (Connor) 	if (rss_conf == NULL) {
409553ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
409653ef1b34SMin Hu (Connor) 			"Cannot update ethdev port %u RSS hash from NULL config\n",
409753ef1b34SMin Hu (Connor) 			port_id);
409853ef1b34SMin Hu (Connor) 		return -EINVAL;
409953ef1b34SMin Hu (Connor) 	}
410099a2dd95SBruce Richardson 
410199a2dd95SBruce Richardson 	ret = rte_eth_dev_info_get(port_id, &dev_info);
410299a2dd95SBruce Richardson 	if (ret != 0)
410399a2dd95SBruce Richardson 		return ret;
410499a2dd95SBruce Richardson 
410599a2dd95SBruce Richardson 	rss_conf->rss_hf = rte_eth_rss_hf_refine(rss_conf->rss_hf);
410699a2dd95SBruce Richardson 	if ((dev_info.flow_type_rss_offloads | rss_conf->rss_hf) !=
410799a2dd95SBruce Richardson 	    dev_info.flow_type_rss_offloads) {
410899a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
410999a2dd95SBruce Richardson 			"Ethdev port_id=%u invalid rss_hf: 0x%"PRIx64", valid value: 0x%"PRIx64"\n",
411099a2dd95SBruce Richardson 			port_id, rss_conf->rss_hf,
411199a2dd95SBruce Richardson 			dev_info.flow_type_rss_offloads);
411299a2dd95SBruce Richardson 		return -EINVAL;
411399a2dd95SBruce Richardson 	}
411499a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rss_hash_update, -ENOTSUP);
411599a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->rss_hash_update)(dev,
411699a2dd95SBruce Richardson 								 rss_conf));
411799a2dd95SBruce Richardson }
411899a2dd95SBruce Richardson 
411999a2dd95SBruce Richardson int
412099a2dd95SBruce Richardson rte_eth_dev_rss_hash_conf_get(uint16_t port_id,
412199a2dd95SBruce Richardson 			      struct rte_eth_rss_conf *rss_conf)
412299a2dd95SBruce Richardson {
412399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
412499a2dd95SBruce Richardson 
412599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
412699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
412753ef1b34SMin Hu (Connor) 
412853ef1b34SMin Hu (Connor) 	if (rss_conf == NULL) {
412953ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
413053ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u RSS hash config to NULL\n",
413153ef1b34SMin Hu (Connor) 			port_id);
413253ef1b34SMin Hu (Connor) 		return -EINVAL;
413353ef1b34SMin Hu (Connor) 	}
413453ef1b34SMin Hu (Connor) 
413599a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rss_hash_conf_get, -ENOTSUP);
413699a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->rss_hash_conf_get)(dev,
413799a2dd95SBruce Richardson 								   rss_conf));
413899a2dd95SBruce Richardson }
413999a2dd95SBruce Richardson 
414099a2dd95SBruce Richardson int
414199a2dd95SBruce Richardson rte_eth_dev_udp_tunnel_port_add(uint16_t port_id,
414299a2dd95SBruce Richardson 				struct rte_eth_udp_tunnel *udp_tunnel)
414399a2dd95SBruce Richardson {
414499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
414599a2dd95SBruce Richardson 
414699a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
414753ef1b34SMin Hu (Connor) 	dev = &rte_eth_devices[port_id];
414853ef1b34SMin Hu (Connor) 
414999a2dd95SBruce Richardson 	if (udp_tunnel == NULL) {
415053ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
415153ef1b34SMin Hu (Connor) 			"Cannot add ethdev port %u UDP tunnel port from NULL UDP tunnel\n",
415253ef1b34SMin Hu (Connor) 			port_id);
415399a2dd95SBruce Richardson 		return -EINVAL;
415499a2dd95SBruce Richardson 	}
415599a2dd95SBruce Richardson 
415699a2dd95SBruce Richardson 	if (udp_tunnel->prot_type >= RTE_TUNNEL_TYPE_MAX) {
415799a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid tunnel type\n");
415899a2dd95SBruce Richardson 		return -EINVAL;
415999a2dd95SBruce Richardson 	}
416099a2dd95SBruce Richardson 
416199a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->udp_tunnel_port_add, -ENOTSUP);
416299a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->udp_tunnel_port_add)(dev,
416399a2dd95SBruce Richardson 								udp_tunnel));
416499a2dd95SBruce Richardson }
416599a2dd95SBruce Richardson 
416699a2dd95SBruce Richardson int
416799a2dd95SBruce Richardson rte_eth_dev_udp_tunnel_port_delete(uint16_t port_id,
416899a2dd95SBruce Richardson 				   struct rte_eth_udp_tunnel *udp_tunnel)
416999a2dd95SBruce Richardson {
417099a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
417199a2dd95SBruce Richardson 
417299a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
417399a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
417499a2dd95SBruce Richardson 
417599a2dd95SBruce Richardson 	if (udp_tunnel == NULL) {
417653ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
417753ef1b34SMin Hu (Connor) 			"Cannot delete ethdev port %u UDP tunnel port from NULL UDP tunnel\n",
417853ef1b34SMin Hu (Connor) 			port_id);
417999a2dd95SBruce Richardson 		return -EINVAL;
418099a2dd95SBruce Richardson 	}
418199a2dd95SBruce Richardson 
418299a2dd95SBruce Richardson 	if (udp_tunnel->prot_type >= RTE_TUNNEL_TYPE_MAX) {
418399a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid tunnel type\n");
418499a2dd95SBruce Richardson 		return -EINVAL;
418599a2dd95SBruce Richardson 	}
418699a2dd95SBruce Richardson 
418799a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->udp_tunnel_port_del, -ENOTSUP);
418899a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->udp_tunnel_port_del)(dev,
418999a2dd95SBruce Richardson 								udp_tunnel));
419099a2dd95SBruce Richardson }
419199a2dd95SBruce Richardson 
419299a2dd95SBruce Richardson int
419399a2dd95SBruce Richardson rte_eth_led_on(uint16_t port_id)
419499a2dd95SBruce Richardson {
419599a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
419699a2dd95SBruce Richardson 
419799a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
419899a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
419953ef1b34SMin Hu (Connor) 
420099a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_on, -ENOTSUP);
420199a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->dev_led_on)(dev));
420299a2dd95SBruce Richardson }
420399a2dd95SBruce Richardson 
420499a2dd95SBruce Richardson int
420599a2dd95SBruce Richardson rte_eth_led_off(uint16_t port_id)
420699a2dd95SBruce Richardson {
420799a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
420899a2dd95SBruce Richardson 
420999a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
421099a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
421153ef1b34SMin Hu (Connor) 
421299a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_off, -ENOTSUP);
421399a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->dev_led_off)(dev));
421499a2dd95SBruce Richardson }
421599a2dd95SBruce Richardson 
421699a2dd95SBruce Richardson int
421799a2dd95SBruce Richardson rte_eth_fec_get_capability(uint16_t port_id,
421899a2dd95SBruce Richardson 			   struct rte_eth_fec_capa *speed_fec_capa,
421999a2dd95SBruce Richardson 			   unsigned int num)
422099a2dd95SBruce Richardson {
422199a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
422299a2dd95SBruce Richardson 	int ret;
422399a2dd95SBruce Richardson 
422499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
422599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
422653ef1b34SMin Hu (Connor) 
422753ef1b34SMin Hu (Connor) 	if (speed_fec_capa == NULL && num > 0) {
422853ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
422953ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u FEC capability to NULL when array size is non zero\n",
423053ef1b34SMin Hu (Connor) 			port_id);
423153ef1b34SMin Hu (Connor) 		return -EINVAL;
423253ef1b34SMin Hu (Connor) 	}
423353ef1b34SMin Hu (Connor) 
423499a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fec_get_capability, -ENOTSUP);
423599a2dd95SBruce Richardson 	ret = (*dev->dev_ops->fec_get_capability)(dev, speed_fec_capa, num);
423699a2dd95SBruce Richardson 
423799a2dd95SBruce Richardson 	return ret;
423899a2dd95SBruce Richardson }
423999a2dd95SBruce Richardson 
424099a2dd95SBruce Richardson int
424199a2dd95SBruce Richardson rte_eth_fec_get(uint16_t port_id, uint32_t *fec_capa)
424299a2dd95SBruce Richardson {
424399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
424499a2dd95SBruce Richardson 
424599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
424699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
424753ef1b34SMin Hu (Connor) 
424853ef1b34SMin Hu (Connor) 	if (fec_capa == NULL) {
424953ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
425053ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u current FEC mode to NULL\n",
425153ef1b34SMin Hu (Connor) 			port_id);
425253ef1b34SMin Hu (Connor) 		return -EINVAL;
425353ef1b34SMin Hu (Connor) 	}
425453ef1b34SMin Hu (Connor) 
425599a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fec_get, -ENOTSUP);
425699a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->fec_get)(dev, fec_capa));
425799a2dd95SBruce Richardson }
425899a2dd95SBruce Richardson 
425999a2dd95SBruce Richardson int
426099a2dd95SBruce Richardson rte_eth_fec_set(uint16_t port_id, uint32_t fec_capa)
426199a2dd95SBruce Richardson {
426299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
426399a2dd95SBruce Richardson 
426499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
426599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
426653ef1b34SMin Hu (Connor) 
426799a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fec_set, -ENOTSUP);
426899a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->fec_set)(dev, fec_capa));
426999a2dd95SBruce Richardson }
427099a2dd95SBruce Richardson 
427199a2dd95SBruce Richardson /*
427299a2dd95SBruce Richardson  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
427399a2dd95SBruce Richardson  * an empty spot.
427499a2dd95SBruce Richardson  */
427599a2dd95SBruce Richardson static int
427699a2dd95SBruce Richardson eth_dev_get_mac_addr_index(uint16_t port_id, const struct rte_ether_addr *addr)
427799a2dd95SBruce Richardson {
427899a2dd95SBruce Richardson 	struct rte_eth_dev_info dev_info;
427999a2dd95SBruce Richardson 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
428099a2dd95SBruce Richardson 	unsigned i;
428199a2dd95SBruce Richardson 	int ret;
428299a2dd95SBruce Richardson 
428399a2dd95SBruce Richardson 	ret = rte_eth_dev_info_get(port_id, &dev_info);
428499a2dd95SBruce Richardson 	if (ret != 0)
428599a2dd95SBruce Richardson 		return -1;
428699a2dd95SBruce Richardson 
428799a2dd95SBruce Richardson 	for (i = 0; i < dev_info.max_mac_addrs; i++)
428899a2dd95SBruce Richardson 		if (memcmp(addr, &dev->data->mac_addrs[i],
428999a2dd95SBruce Richardson 				RTE_ETHER_ADDR_LEN) == 0)
429099a2dd95SBruce Richardson 			return i;
429199a2dd95SBruce Richardson 
429299a2dd95SBruce Richardson 	return -1;
429399a2dd95SBruce Richardson }
429499a2dd95SBruce Richardson 
429599a2dd95SBruce Richardson static const struct rte_ether_addr null_mac_addr;
429699a2dd95SBruce Richardson 
429799a2dd95SBruce Richardson int
429899a2dd95SBruce Richardson rte_eth_dev_mac_addr_add(uint16_t port_id, struct rte_ether_addr *addr,
429999a2dd95SBruce Richardson 			uint32_t pool)
430099a2dd95SBruce Richardson {
430199a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
430299a2dd95SBruce Richardson 	int index;
430399a2dd95SBruce Richardson 	uint64_t pool_mask;
430499a2dd95SBruce Richardson 	int ret;
430599a2dd95SBruce Richardson 
430699a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
430799a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
430853ef1b34SMin Hu (Connor) 
430953ef1b34SMin Hu (Connor) 	if (addr == NULL) {
431053ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
431153ef1b34SMin Hu (Connor) 			"Cannot add ethdev port %u MAC address from NULL address\n",
431253ef1b34SMin Hu (Connor) 			port_id);
431353ef1b34SMin Hu (Connor) 		return -EINVAL;
431453ef1b34SMin Hu (Connor) 	}
431553ef1b34SMin Hu (Connor) 
431699a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_add, -ENOTSUP);
431799a2dd95SBruce Richardson 
431899a2dd95SBruce Richardson 	if (rte_is_zero_ether_addr(addr)) {
431999a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Port %u: Cannot add NULL MAC address\n",
432099a2dd95SBruce Richardson 			port_id);
432199a2dd95SBruce Richardson 		return -EINVAL;
432299a2dd95SBruce Richardson 	}
432399a2dd95SBruce Richardson 	if (pool >= ETH_64_POOLS) {
432499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Pool id must be 0-%d\n", ETH_64_POOLS - 1);
432599a2dd95SBruce Richardson 		return -EINVAL;
432699a2dd95SBruce Richardson 	}
432799a2dd95SBruce Richardson 
432899a2dd95SBruce Richardson 	index = eth_dev_get_mac_addr_index(port_id, addr);
432999a2dd95SBruce Richardson 	if (index < 0) {
433099a2dd95SBruce Richardson 		index = eth_dev_get_mac_addr_index(port_id, &null_mac_addr);
433199a2dd95SBruce Richardson 		if (index < 0) {
433299a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR, "Port %u: MAC address array full\n",
433399a2dd95SBruce Richardson 				port_id);
433499a2dd95SBruce Richardson 			return -ENOSPC;
433599a2dd95SBruce Richardson 		}
433699a2dd95SBruce Richardson 	} else {
433799a2dd95SBruce Richardson 		pool_mask = dev->data->mac_pool_sel[index];
433899a2dd95SBruce Richardson 
433999a2dd95SBruce Richardson 		/* Check if both MAC address and pool is already there, and do nothing */
434099a2dd95SBruce Richardson 		if (pool_mask & (1ULL << pool))
434199a2dd95SBruce Richardson 			return 0;
434299a2dd95SBruce Richardson 	}
434399a2dd95SBruce Richardson 
434499a2dd95SBruce Richardson 	/* Update NIC */
434599a2dd95SBruce Richardson 	ret = (*dev->dev_ops->mac_addr_add)(dev, addr, index, pool);
434699a2dd95SBruce Richardson 
434799a2dd95SBruce Richardson 	if (ret == 0) {
434899a2dd95SBruce Richardson 		/* Update address in NIC data structure */
434999a2dd95SBruce Richardson 		rte_ether_addr_copy(addr, &dev->data->mac_addrs[index]);
435099a2dd95SBruce Richardson 
435199a2dd95SBruce Richardson 		/* Update pool bitmap in NIC data structure */
435299a2dd95SBruce Richardson 		dev->data->mac_pool_sel[index] |= (1ULL << pool);
435399a2dd95SBruce Richardson 	}
435499a2dd95SBruce Richardson 
435599a2dd95SBruce Richardson 	return eth_err(port_id, ret);
435699a2dd95SBruce Richardson }
435799a2dd95SBruce Richardson 
435899a2dd95SBruce Richardson int
435999a2dd95SBruce Richardson rte_eth_dev_mac_addr_remove(uint16_t port_id, struct rte_ether_addr *addr)
436099a2dd95SBruce Richardson {
436199a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
436299a2dd95SBruce Richardson 	int index;
436399a2dd95SBruce Richardson 
436499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
436599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
436653ef1b34SMin Hu (Connor) 
436753ef1b34SMin Hu (Connor) 	if (addr == NULL) {
436853ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
436953ef1b34SMin Hu (Connor) 			"Cannot remove ethdev port %u MAC address from NULL address\n",
437053ef1b34SMin Hu (Connor) 			port_id);
437153ef1b34SMin Hu (Connor) 		return -EINVAL;
437253ef1b34SMin Hu (Connor) 	}
437353ef1b34SMin Hu (Connor) 
437499a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_remove, -ENOTSUP);
437599a2dd95SBruce Richardson 
437699a2dd95SBruce Richardson 	index = eth_dev_get_mac_addr_index(port_id, addr);
437799a2dd95SBruce Richardson 	if (index == 0) {
437899a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
437999a2dd95SBruce Richardson 			"Port %u: Cannot remove default MAC address\n",
438099a2dd95SBruce Richardson 			port_id);
438199a2dd95SBruce Richardson 		return -EADDRINUSE;
438299a2dd95SBruce Richardson 	} else if (index < 0)
438399a2dd95SBruce Richardson 		return 0;  /* Do nothing if address wasn't found */
438499a2dd95SBruce Richardson 
438599a2dd95SBruce Richardson 	/* Update NIC */
438699a2dd95SBruce Richardson 	(*dev->dev_ops->mac_addr_remove)(dev, index);
438799a2dd95SBruce Richardson 
438899a2dd95SBruce Richardson 	/* Update address in NIC data structure */
438999a2dd95SBruce Richardson 	rte_ether_addr_copy(&null_mac_addr, &dev->data->mac_addrs[index]);
439099a2dd95SBruce Richardson 
439199a2dd95SBruce Richardson 	/* reset pool bitmap */
439299a2dd95SBruce Richardson 	dev->data->mac_pool_sel[index] = 0;
439399a2dd95SBruce Richardson 
439499a2dd95SBruce Richardson 	return 0;
439599a2dd95SBruce Richardson }
439699a2dd95SBruce Richardson 
439799a2dd95SBruce Richardson int
439899a2dd95SBruce Richardson rte_eth_dev_default_mac_addr_set(uint16_t port_id, struct rte_ether_addr *addr)
439999a2dd95SBruce Richardson {
440099a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
440199a2dd95SBruce Richardson 	int ret;
440299a2dd95SBruce Richardson 
440399a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
440453ef1b34SMin Hu (Connor) 	dev = &rte_eth_devices[port_id];
440553ef1b34SMin Hu (Connor) 
440653ef1b34SMin Hu (Connor) 	if (addr == NULL) {
440753ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
440853ef1b34SMin Hu (Connor) 			"Cannot set ethdev port %u default MAC address from NULL address\n",
440953ef1b34SMin Hu (Connor) 			port_id);
441053ef1b34SMin Hu (Connor) 		return -EINVAL;
441153ef1b34SMin Hu (Connor) 	}
441299a2dd95SBruce Richardson 
441399a2dd95SBruce Richardson 	if (!rte_is_valid_assigned_ether_addr(addr))
441499a2dd95SBruce Richardson 		return -EINVAL;
441599a2dd95SBruce Richardson 
441699a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_set, -ENOTSUP);
441799a2dd95SBruce Richardson 
441899a2dd95SBruce Richardson 	ret = (*dev->dev_ops->mac_addr_set)(dev, addr);
441999a2dd95SBruce Richardson 	if (ret < 0)
442099a2dd95SBruce Richardson 		return ret;
442199a2dd95SBruce Richardson 
442299a2dd95SBruce Richardson 	/* Update default address in NIC data structure */
442399a2dd95SBruce Richardson 	rte_ether_addr_copy(addr, &dev->data->mac_addrs[0]);
442499a2dd95SBruce Richardson 
442599a2dd95SBruce Richardson 	return 0;
442699a2dd95SBruce Richardson }
442799a2dd95SBruce Richardson 
442899a2dd95SBruce Richardson 
442999a2dd95SBruce Richardson /*
443099a2dd95SBruce Richardson  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
443199a2dd95SBruce Richardson  * an empty spot.
443299a2dd95SBruce Richardson  */
443399a2dd95SBruce Richardson static int
443499a2dd95SBruce Richardson eth_dev_get_hash_mac_addr_index(uint16_t port_id,
443599a2dd95SBruce Richardson 		const struct rte_ether_addr *addr)
443699a2dd95SBruce Richardson {
443799a2dd95SBruce Richardson 	struct rte_eth_dev_info dev_info;
443899a2dd95SBruce Richardson 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
443999a2dd95SBruce Richardson 	unsigned i;
444099a2dd95SBruce Richardson 	int ret;
444199a2dd95SBruce Richardson 
444299a2dd95SBruce Richardson 	ret = rte_eth_dev_info_get(port_id, &dev_info);
444399a2dd95SBruce Richardson 	if (ret != 0)
444499a2dd95SBruce Richardson 		return -1;
444599a2dd95SBruce Richardson 
444699a2dd95SBruce Richardson 	if (!dev->data->hash_mac_addrs)
444799a2dd95SBruce Richardson 		return -1;
444899a2dd95SBruce Richardson 
444999a2dd95SBruce Richardson 	for (i = 0; i < dev_info.max_hash_mac_addrs; i++)
445099a2dd95SBruce Richardson 		if (memcmp(addr, &dev->data->hash_mac_addrs[i],
445199a2dd95SBruce Richardson 			RTE_ETHER_ADDR_LEN) == 0)
445299a2dd95SBruce Richardson 			return i;
445399a2dd95SBruce Richardson 
445499a2dd95SBruce Richardson 	return -1;
445599a2dd95SBruce Richardson }
445699a2dd95SBruce Richardson 
445799a2dd95SBruce Richardson int
445899a2dd95SBruce Richardson rte_eth_dev_uc_hash_table_set(uint16_t port_id, struct rte_ether_addr *addr,
445999a2dd95SBruce Richardson 				uint8_t on)
446099a2dd95SBruce Richardson {
446199a2dd95SBruce Richardson 	int index;
446299a2dd95SBruce Richardson 	int ret;
446399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
446499a2dd95SBruce Richardson 
446599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
446699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
446753ef1b34SMin Hu (Connor) 
446853ef1b34SMin Hu (Connor) 	if (addr == NULL) {
446953ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
447053ef1b34SMin Hu (Connor) 			"Cannot set ethdev port %u unicast hash table from NULL address\n",
447153ef1b34SMin Hu (Connor) 			port_id);
447253ef1b34SMin Hu (Connor) 		return -EINVAL;
447353ef1b34SMin Hu (Connor) 	}
447453ef1b34SMin Hu (Connor) 
447599a2dd95SBruce Richardson 	if (rte_is_zero_ether_addr(addr)) {
447699a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Port %u: Cannot add NULL MAC address\n",
447799a2dd95SBruce Richardson 			port_id);
447899a2dd95SBruce Richardson 		return -EINVAL;
447999a2dd95SBruce Richardson 	}
448099a2dd95SBruce Richardson 
448199a2dd95SBruce Richardson 	index = eth_dev_get_hash_mac_addr_index(port_id, addr);
448299a2dd95SBruce Richardson 	/* Check if it's already there, and do nothing */
448399a2dd95SBruce Richardson 	if ((index >= 0) && on)
448499a2dd95SBruce Richardson 		return 0;
448599a2dd95SBruce Richardson 
448699a2dd95SBruce Richardson 	if (index < 0) {
448799a2dd95SBruce Richardson 		if (!on) {
448899a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
448999a2dd95SBruce Richardson 				"Port %u: the MAC address was not set in UTA\n",
449099a2dd95SBruce Richardson 				port_id);
449199a2dd95SBruce Richardson 			return -EINVAL;
449299a2dd95SBruce Richardson 		}
449399a2dd95SBruce Richardson 
449499a2dd95SBruce Richardson 		index = eth_dev_get_hash_mac_addr_index(port_id, &null_mac_addr);
449599a2dd95SBruce Richardson 		if (index < 0) {
449699a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR, "Port %u: MAC address array full\n",
449799a2dd95SBruce Richardson 				port_id);
449899a2dd95SBruce Richardson 			return -ENOSPC;
449999a2dd95SBruce Richardson 		}
450099a2dd95SBruce Richardson 	}
450199a2dd95SBruce Richardson 
450299a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->uc_hash_table_set, -ENOTSUP);
450399a2dd95SBruce Richardson 	ret = (*dev->dev_ops->uc_hash_table_set)(dev, addr, on);
450499a2dd95SBruce Richardson 	if (ret == 0) {
450599a2dd95SBruce Richardson 		/* Update address in NIC data structure */
450699a2dd95SBruce Richardson 		if (on)
450799a2dd95SBruce Richardson 			rte_ether_addr_copy(addr,
450899a2dd95SBruce Richardson 					&dev->data->hash_mac_addrs[index]);
450999a2dd95SBruce Richardson 		else
451099a2dd95SBruce Richardson 			rte_ether_addr_copy(&null_mac_addr,
451199a2dd95SBruce Richardson 					&dev->data->hash_mac_addrs[index]);
451299a2dd95SBruce Richardson 	}
451399a2dd95SBruce Richardson 
451499a2dd95SBruce Richardson 	return eth_err(port_id, ret);
451599a2dd95SBruce Richardson }
451699a2dd95SBruce Richardson 
451799a2dd95SBruce Richardson int
451899a2dd95SBruce Richardson rte_eth_dev_uc_all_hash_table_set(uint16_t port_id, uint8_t on)
451999a2dd95SBruce Richardson {
452099a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
452199a2dd95SBruce Richardson 
452299a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
452399a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
452499a2dd95SBruce Richardson 
452599a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->uc_all_hash_table_set, -ENOTSUP);
452699a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->uc_all_hash_table_set)(dev,
452799a2dd95SBruce Richardson 								       on));
452899a2dd95SBruce Richardson }
452999a2dd95SBruce Richardson 
453099a2dd95SBruce Richardson int rte_eth_set_queue_rate_limit(uint16_t port_id, uint16_t queue_idx,
453199a2dd95SBruce Richardson 					uint16_t tx_rate)
453299a2dd95SBruce Richardson {
453399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
453499a2dd95SBruce Richardson 	struct rte_eth_dev_info dev_info;
453599a2dd95SBruce Richardson 	struct rte_eth_link link;
453699a2dd95SBruce Richardson 	int ret;
453799a2dd95SBruce Richardson 
453899a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
453953ef1b34SMin Hu (Connor) 	dev = &rte_eth_devices[port_id];
454099a2dd95SBruce Richardson 
454199a2dd95SBruce Richardson 	ret = rte_eth_dev_info_get(port_id, &dev_info);
454299a2dd95SBruce Richardson 	if (ret != 0)
454399a2dd95SBruce Richardson 		return ret;
454499a2dd95SBruce Richardson 
454599a2dd95SBruce Richardson 	link = dev->data->dev_link;
454699a2dd95SBruce Richardson 
454799a2dd95SBruce Richardson 	if (queue_idx > dev_info.max_tx_queues) {
454899a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
454999a2dd95SBruce Richardson 			"Set queue rate limit:port %u: invalid queue id=%u\n",
455099a2dd95SBruce Richardson 			port_id, queue_idx);
455199a2dd95SBruce Richardson 		return -EINVAL;
455299a2dd95SBruce Richardson 	}
455399a2dd95SBruce Richardson 
455499a2dd95SBruce Richardson 	if (tx_rate > link.link_speed) {
455599a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
455699a2dd95SBruce Richardson 			"Set queue rate limit:invalid tx_rate=%u, bigger than link speed= %d\n",
455799a2dd95SBruce Richardson 			tx_rate, link.link_speed);
455899a2dd95SBruce Richardson 		return -EINVAL;
455999a2dd95SBruce Richardson 	}
456099a2dd95SBruce Richardson 
456199a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_queue_rate_limit, -ENOTSUP);
456299a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->set_queue_rate_limit)(dev,
456399a2dd95SBruce Richardson 							queue_idx, tx_rate));
456499a2dd95SBruce Richardson }
456599a2dd95SBruce Richardson 
4566c87d435aSKonstantin Ananyev RTE_INIT(eth_dev_init_fp_ops)
4567c87d435aSKonstantin Ananyev {
4568c87d435aSKonstantin Ananyev 	uint32_t i;
4569c87d435aSKonstantin Ananyev 
4570c87d435aSKonstantin Ananyev 	for (i = 0; i != RTE_DIM(rte_eth_fp_ops); i++)
4571c87d435aSKonstantin Ananyev 		eth_dev_fp_ops_reset(rte_eth_fp_ops + i);
4572c87d435aSKonstantin Ananyev }
4573c87d435aSKonstantin Ananyev 
457499a2dd95SBruce Richardson RTE_INIT(eth_dev_init_cb_lists)
457599a2dd95SBruce Richardson {
457699a2dd95SBruce Richardson 	uint16_t i;
457799a2dd95SBruce Richardson 
457899a2dd95SBruce Richardson 	for (i = 0; i < RTE_MAX_ETHPORTS; i++)
457999a2dd95SBruce Richardson 		TAILQ_INIT(&rte_eth_devices[i].link_intr_cbs);
458099a2dd95SBruce Richardson }
458199a2dd95SBruce Richardson 
458299a2dd95SBruce Richardson int
458399a2dd95SBruce Richardson rte_eth_dev_callback_register(uint16_t port_id,
458499a2dd95SBruce Richardson 			enum rte_eth_event_type event,
458599a2dd95SBruce Richardson 			rte_eth_dev_cb_fn cb_fn, void *cb_arg)
458699a2dd95SBruce Richardson {
458799a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
458899a2dd95SBruce Richardson 	struct rte_eth_dev_callback *user_cb;
458999a2dd95SBruce Richardson 	uint16_t next_port;
459099a2dd95SBruce Richardson 	uint16_t last_port;
459199a2dd95SBruce Richardson 
459253ef1b34SMin Hu (Connor) 	if (cb_fn == NULL) {
459353ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
459453ef1b34SMin Hu (Connor) 			"Cannot register ethdev port %u callback from NULL\n",
459553ef1b34SMin Hu (Connor) 			port_id);
459699a2dd95SBruce Richardson 		return -EINVAL;
459753ef1b34SMin Hu (Connor) 	}
459899a2dd95SBruce Richardson 
459999a2dd95SBruce Richardson 	if (!rte_eth_dev_is_valid_port(port_id) && port_id != RTE_ETH_ALL) {
460099a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid port_id=%d\n", port_id);
460199a2dd95SBruce Richardson 		return -EINVAL;
460299a2dd95SBruce Richardson 	}
460399a2dd95SBruce Richardson 
460499a2dd95SBruce Richardson 	if (port_id == RTE_ETH_ALL) {
460599a2dd95SBruce Richardson 		next_port = 0;
460699a2dd95SBruce Richardson 		last_port = RTE_MAX_ETHPORTS - 1;
460799a2dd95SBruce Richardson 	} else {
460899a2dd95SBruce Richardson 		next_port = last_port = port_id;
460999a2dd95SBruce Richardson 	}
461099a2dd95SBruce Richardson 
461199a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_cb_lock);
461299a2dd95SBruce Richardson 
461399a2dd95SBruce Richardson 	do {
461499a2dd95SBruce Richardson 		dev = &rte_eth_devices[next_port];
461599a2dd95SBruce Richardson 
461699a2dd95SBruce Richardson 		TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
461799a2dd95SBruce Richardson 			if (user_cb->cb_fn == cb_fn &&
461899a2dd95SBruce Richardson 				user_cb->cb_arg == cb_arg &&
461999a2dd95SBruce Richardson 				user_cb->event == event) {
462099a2dd95SBruce Richardson 				break;
462199a2dd95SBruce Richardson 			}
462299a2dd95SBruce Richardson 		}
462399a2dd95SBruce Richardson 
462499a2dd95SBruce Richardson 		/* create a new callback. */
462599a2dd95SBruce Richardson 		if (user_cb == NULL) {
462699a2dd95SBruce Richardson 			user_cb = rte_zmalloc("INTR_USER_CALLBACK",
462799a2dd95SBruce Richardson 				sizeof(struct rte_eth_dev_callback), 0);
462899a2dd95SBruce Richardson 			if (user_cb != NULL) {
462999a2dd95SBruce Richardson 				user_cb->cb_fn = cb_fn;
463099a2dd95SBruce Richardson 				user_cb->cb_arg = cb_arg;
463199a2dd95SBruce Richardson 				user_cb->event = event;
463299a2dd95SBruce Richardson 				TAILQ_INSERT_TAIL(&(dev->link_intr_cbs),
463399a2dd95SBruce Richardson 						  user_cb, next);
463499a2dd95SBruce Richardson 			} else {
463599a2dd95SBruce Richardson 				rte_spinlock_unlock(&eth_dev_cb_lock);
463699a2dd95SBruce Richardson 				rte_eth_dev_callback_unregister(port_id, event,
463799a2dd95SBruce Richardson 								cb_fn, cb_arg);
463899a2dd95SBruce Richardson 				return -ENOMEM;
463999a2dd95SBruce Richardson 			}
464099a2dd95SBruce Richardson 
464199a2dd95SBruce Richardson 		}
464299a2dd95SBruce Richardson 	} while (++next_port <= last_port);
464399a2dd95SBruce Richardson 
464499a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_cb_lock);
464599a2dd95SBruce Richardson 	return 0;
464699a2dd95SBruce Richardson }
464799a2dd95SBruce Richardson 
464899a2dd95SBruce Richardson int
464999a2dd95SBruce Richardson rte_eth_dev_callback_unregister(uint16_t port_id,
465099a2dd95SBruce Richardson 			enum rte_eth_event_type event,
465199a2dd95SBruce Richardson 			rte_eth_dev_cb_fn cb_fn, void *cb_arg)
465299a2dd95SBruce Richardson {
465399a2dd95SBruce Richardson 	int ret;
465499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
465599a2dd95SBruce Richardson 	struct rte_eth_dev_callback *cb, *next;
465699a2dd95SBruce Richardson 	uint16_t next_port;
465799a2dd95SBruce Richardson 	uint16_t last_port;
465899a2dd95SBruce Richardson 
465953ef1b34SMin Hu (Connor) 	if (cb_fn == NULL) {
466053ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
466153ef1b34SMin Hu (Connor) 			"Cannot unregister ethdev port %u callback from NULL\n",
466253ef1b34SMin Hu (Connor) 			port_id);
466399a2dd95SBruce Richardson 		return -EINVAL;
466453ef1b34SMin Hu (Connor) 	}
466599a2dd95SBruce Richardson 
466699a2dd95SBruce Richardson 	if (!rte_eth_dev_is_valid_port(port_id) && port_id != RTE_ETH_ALL) {
466799a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid port_id=%d\n", port_id);
466899a2dd95SBruce Richardson 		return -EINVAL;
466999a2dd95SBruce Richardson 	}
467099a2dd95SBruce Richardson 
467199a2dd95SBruce Richardson 	if (port_id == RTE_ETH_ALL) {
467299a2dd95SBruce Richardson 		next_port = 0;
467399a2dd95SBruce Richardson 		last_port = RTE_MAX_ETHPORTS - 1;
467499a2dd95SBruce Richardson 	} else {
467599a2dd95SBruce Richardson 		next_port = last_port = port_id;
467699a2dd95SBruce Richardson 	}
467799a2dd95SBruce Richardson 
467899a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_cb_lock);
467999a2dd95SBruce Richardson 
468099a2dd95SBruce Richardson 	do {
468199a2dd95SBruce Richardson 		dev = &rte_eth_devices[next_port];
468299a2dd95SBruce Richardson 		ret = 0;
468399a2dd95SBruce Richardson 		for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL;
468499a2dd95SBruce Richardson 		     cb = next) {
468599a2dd95SBruce Richardson 
468699a2dd95SBruce Richardson 			next = TAILQ_NEXT(cb, next);
468799a2dd95SBruce Richardson 
468899a2dd95SBruce Richardson 			if (cb->cb_fn != cb_fn || cb->event != event ||
468999a2dd95SBruce Richardson 			    (cb_arg != (void *)-1 && cb->cb_arg != cb_arg))
469099a2dd95SBruce Richardson 				continue;
469199a2dd95SBruce Richardson 
469299a2dd95SBruce Richardson 			/*
469399a2dd95SBruce Richardson 			 * if this callback is not executing right now,
469499a2dd95SBruce Richardson 			 * then remove it.
469599a2dd95SBruce Richardson 			 */
469699a2dd95SBruce Richardson 			if (cb->active == 0) {
469799a2dd95SBruce Richardson 				TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
469899a2dd95SBruce Richardson 				rte_free(cb);
469999a2dd95SBruce Richardson 			} else {
470099a2dd95SBruce Richardson 				ret = -EAGAIN;
470199a2dd95SBruce Richardson 			}
470299a2dd95SBruce Richardson 		}
470399a2dd95SBruce Richardson 	} while (++next_port <= last_port);
470499a2dd95SBruce Richardson 
470599a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_cb_lock);
470699a2dd95SBruce Richardson 	return ret;
470799a2dd95SBruce Richardson }
470899a2dd95SBruce Richardson 
470999a2dd95SBruce Richardson int
471099a2dd95SBruce Richardson rte_eth_dev_callback_process(struct rte_eth_dev *dev,
471199a2dd95SBruce Richardson 	enum rte_eth_event_type event, void *ret_param)
471299a2dd95SBruce Richardson {
471399a2dd95SBruce Richardson 	struct rte_eth_dev_callback *cb_lst;
471499a2dd95SBruce Richardson 	struct rte_eth_dev_callback dev_cb;
471599a2dd95SBruce Richardson 	int rc = 0;
471699a2dd95SBruce Richardson 
471799a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_cb_lock);
471899a2dd95SBruce Richardson 	TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
471999a2dd95SBruce Richardson 		if (cb_lst->cb_fn == NULL || cb_lst->event != event)
472099a2dd95SBruce Richardson 			continue;
472199a2dd95SBruce Richardson 		dev_cb = *cb_lst;
472299a2dd95SBruce Richardson 		cb_lst->active = 1;
472399a2dd95SBruce Richardson 		if (ret_param != NULL)
472499a2dd95SBruce Richardson 			dev_cb.ret_param = ret_param;
472599a2dd95SBruce Richardson 
472699a2dd95SBruce Richardson 		rte_spinlock_unlock(&eth_dev_cb_lock);
472799a2dd95SBruce Richardson 		rc = dev_cb.cb_fn(dev->data->port_id, dev_cb.event,
472899a2dd95SBruce Richardson 				dev_cb.cb_arg, dev_cb.ret_param);
472999a2dd95SBruce Richardson 		rte_spinlock_lock(&eth_dev_cb_lock);
473099a2dd95SBruce Richardson 		cb_lst->active = 0;
473199a2dd95SBruce Richardson 	}
473299a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_cb_lock);
473399a2dd95SBruce Richardson 	return rc;
473499a2dd95SBruce Richardson }
473599a2dd95SBruce Richardson 
473699a2dd95SBruce Richardson void
473799a2dd95SBruce Richardson rte_eth_dev_probing_finish(struct rte_eth_dev *dev)
473899a2dd95SBruce Richardson {
473999a2dd95SBruce Richardson 	if (dev == NULL)
474099a2dd95SBruce Richardson 		return;
474199a2dd95SBruce Richardson 
4742c87d435aSKonstantin Ananyev 	/*
4743c87d435aSKonstantin Ananyev 	 * for secondary process, at that point we expect device
4744c87d435aSKonstantin Ananyev 	 * to be already 'usable', so shared data and all function pointers
4745c87d435aSKonstantin Ananyev 	 * for fast-path devops have to be setup properly inside rte_eth_dev.
4746c87d435aSKonstantin Ananyev 	 */
4747c87d435aSKonstantin Ananyev 	if (rte_eal_process_type() == RTE_PROC_SECONDARY)
4748c87d435aSKonstantin Ananyev 		eth_dev_fp_ops_setup(rte_eth_fp_ops + dev->data->port_id, dev);
4749c87d435aSKonstantin Ananyev 
475099a2dd95SBruce Richardson 	rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_NEW, NULL);
475199a2dd95SBruce Richardson 
475299a2dd95SBruce Richardson 	dev->state = RTE_ETH_DEV_ATTACHED;
475399a2dd95SBruce Richardson }
475499a2dd95SBruce Richardson 
475599a2dd95SBruce Richardson int
475699a2dd95SBruce Richardson rte_eth_dev_rx_intr_ctl(uint16_t port_id, int epfd, int op, void *data)
475799a2dd95SBruce Richardson {
475899a2dd95SBruce Richardson 	uint32_t vec;
475999a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
476099a2dd95SBruce Richardson 	struct rte_intr_handle *intr_handle;
476199a2dd95SBruce Richardson 	uint16_t qid;
476299a2dd95SBruce Richardson 	int rc;
476399a2dd95SBruce Richardson 
476499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
476599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
476699a2dd95SBruce Richardson 
476799a2dd95SBruce Richardson 	if (!dev->intr_handle) {
476899a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "RX Intr handle unset\n");
476999a2dd95SBruce Richardson 		return -ENOTSUP;
477099a2dd95SBruce Richardson 	}
477199a2dd95SBruce Richardson 
477299a2dd95SBruce Richardson 	intr_handle = dev->intr_handle;
477399a2dd95SBruce Richardson 	if (!intr_handle->intr_vec) {
477499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "RX Intr vector unset\n");
477599a2dd95SBruce Richardson 		return -EPERM;
477699a2dd95SBruce Richardson 	}
477799a2dd95SBruce Richardson 
477899a2dd95SBruce Richardson 	for (qid = 0; qid < dev->data->nb_rx_queues; qid++) {
477999a2dd95SBruce Richardson 		vec = intr_handle->intr_vec[qid];
478099a2dd95SBruce Richardson 		rc = rte_intr_rx_ctl(intr_handle, epfd, op, vec, data);
478199a2dd95SBruce Richardson 		if (rc && rc != -EEXIST) {
478299a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
478399a2dd95SBruce Richardson 				"p %u q %u rx ctl error op %d epfd %d vec %u\n",
478499a2dd95SBruce Richardson 				port_id, qid, op, epfd, vec);
478599a2dd95SBruce Richardson 		}
478699a2dd95SBruce Richardson 	}
478799a2dd95SBruce Richardson 
478899a2dd95SBruce Richardson 	return 0;
478999a2dd95SBruce Richardson }
479099a2dd95SBruce Richardson 
479199a2dd95SBruce Richardson int
479299a2dd95SBruce Richardson rte_eth_dev_rx_intr_ctl_q_get_fd(uint16_t port_id, uint16_t queue_id)
479399a2dd95SBruce Richardson {
479499a2dd95SBruce Richardson 	struct rte_intr_handle *intr_handle;
479599a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
479699a2dd95SBruce Richardson 	unsigned int efd_idx;
479799a2dd95SBruce Richardson 	uint32_t vec;
479899a2dd95SBruce Richardson 	int fd;
479999a2dd95SBruce Richardson 
480099a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -1);
480199a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
480299a2dd95SBruce Richardson 
480399a2dd95SBruce Richardson 	if (queue_id >= dev->data->nb_rx_queues) {
480499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid RX queue_id=%u\n", queue_id);
480599a2dd95SBruce Richardson 		return -1;
480699a2dd95SBruce Richardson 	}
480799a2dd95SBruce Richardson 
480899a2dd95SBruce Richardson 	if (!dev->intr_handle) {
480999a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "RX Intr handle unset\n");
481099a2dd95SBruce Richardson 		return -1;
481199a2dd95SBruce Richardson 	}
481299a2dd95SBruce Richardson 
481399a2dd95SBruce Richardson 	intr_handle = dev->intr_handle;
481499a2dd95SBruce Richardson 	if (!intr_handle->intr_vec) {
481599a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "RX Intr vector unset\n");
481699a2dd95SBruce Richardson 		return -1;
481799a2dd95SBruce Richardson 	}
481899a2dd95SBruce Richardson 
481999a2dd95SBruce Richardson 	vec = intr_handle->intr_vec[queue_id];
482099a2dd95SBruce Richardson 	efd_idx = (vec >= RTE_INTR_VEC_RXTX_OFFSET) ?
482199a2dd95SBruce Richardson 		(vec - RTE_INTR_VEC_RXTX_OFFSET) : vec;
482299a2dd95SBruce Richardson 	fd = intr_handle->efds[efd_idx];
482399a2dd95SBruce Richardson 
482499a2dd95SBruce Richardson 	return fd;
482599a2dd95SBruce Richardson }
482699a2dd95SBruce Richardson 
482799a2dd95SBruce Richardson static inline int
482899a2dd95SBruce Richardson eth_dev_dma_mzone_name(char *name, size_t len, uint16_t port_id, uint16_t queue_id,
482999a2dd95SBruce Richardson 		const char *ring_name)
483099a2dd95SBruce Richardson {
483199a2dd95SBruce Richardson 	return snprintf(name, len, "eth_p%d_q%d_%s",
483299a2dd95SBruce Richardson 			port_id, queue_id, ring_name);
483399a2dd95SBruce Richardson }
483499a2dd95SBruce Richardson 
483599a2dd95SBruce Richardson const struct rte_memzone *
483699a2dd95SBruce Richardson rte_eth_dma_zone_reserve(const struct rte_eth_dev *dev, const char *ring_name,
483799a2dd95SBruce Richardson 			 uint16_t queue_id, size_t size, unsigned align,
483899a2dd95SBruce Richardson 			 int socket_id)
483999a2dd95SBruce Richardson {
484099a2dd95SBruce Richardson 	char z_name[RTE_MEMZONE_NAMESIZE];
484199a2dd95SBruce Richardson 	const struct rte_memzone *mz;
484299a2dd95SBruce Richardson 	int rc;
484399a2dd95SBruce Richardson 
484499a2dd95SBruce Richardson 	rc = eth_dev_dma_mzone_name(z_name, sizeof(z_name), dev->data->port_id,
484599a2dd95SBruce Richardson 			queue_id, ring_name);
484699a2dd95SBruce Richardson 	if (rc >= RTE_MEMZONE_NAMESIZE) {
484799a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "ring name too long\n");
484899a2dd95SBruce Richardson 		rte_errno = ENAMETOOLONG;
484999a2dd95SBruce Richardson 		return NULL;
485099a2dd95SBruce Richardson 	}
485199a2dd95SBruce Richardson 
485299a2dd95SBruce Richardson 	mz = rte_memzone_lookup(z_name);
485399a2dd95SBruce Richardson 	if (mz) {
485499a2dd95SBruce Richardson 		if ((socket_id != SOCKET_ID_ANY && socket_id != mz->socket_id) ||
485599a2dd95SBruce Richardson 				size > mz->len ||
485699a2dd95SBruce Richardson 				((uintptr_t)mz->addr & (align - 1)) != 0) {
485799a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
485899a2dd95SBruce Richardson 				"memzone %s does not justify the requested attributes\n",
485999a2dd95SBruce Richardson 				mz->name);
486099a2dd95SBruce Richardson 			return NULL;
486199a2dd95SBruce Richardson 		}
486299a2dd95SBruce Richardson 
486399a2dd95SBruce Richardson 		return mz;
486499a2dd95SBruce Richardson 	}
486599a2dd95SBruce Richardson 
486699a2dd95SBruce Richardson 	return rte_memzone_reserve_aligned(z_name, size, socket_id,
486799a2dd95SBruce Richardson 			RTE_MEMZONE_IOVA_CONTIG, align);
486899a2dd95SBruce Richardson }
486999a2dd95SBruce Richardson 
487099a2dd95SBruce Richardson int
487199a2dd95SBruce Richardson rte_eth_dma_zone_free(const struct rte_eth_dev *dev, const char *ring_name,
487299a2dd95SBruce Richardson 		uint16_t queue_id)
487399a2dd95SBruce Richardson {
487499a2dd95SBruce Richardson 	char z_name[RTE_MEMZONE_NAMESIZE];
487599a2dd95SBruce Richardson 	const struct rte_memzone *mz;
487699a2dd95SBruce Richardson 	int rc = 0;
487799a2dd95SBruce Richardson 
487899a2dd95SBruce Richardson 	rc = eth_dev_dma_mzone_name(z_name, sizeof(z_name), dev->data->port_id,
487999a2dd95SBruce Richardson 			queue_id, ring_name);
488099a2dd95SBruce Richardson 	if (rc >= RTE_MEMZONE_NAMESIZE) {
488199a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "ring name too long\n");
488299a2dd95SBruce Richardson 		return -ENAMETOOLONG;
488399a2dd95SBruce Richardson 	}
488499a2dd95SBruce Richardson 
488599a2dd95SBruce Richardson 	mz = rte_memzone_lookup(z_name);
488699a2dd95SBruce Richardson 	if (mz)
488799a2dd95SBruce Richardson 		rc = rte_memzone_free(mz);
488899a2dd95SBruce Richardson 	else
488999a2dd95SBruce Richardson 		rc = -ENOENT;
489099a2dd95SBruce Richardson 
489199a2dd95SBruce Richardson 	return rc;
489299a2dd95SBruce Richardson }
489399a2dd95SBruce Richardson 
489499a2dd95SBruce Richardson int
489599a2dd95SBruce Richardson rte_eth_dev_create(struct rte_device *device, const char *name,
489699a2dd95SBruce Richardson 	size_t priv_data_size,
489799a2dd95SBruce Richardson 	ethdev_bus_specific_init ethdev_bus_specific_init,
489899a2dd95SBruce Richardson 	void *bus_init_params,
489999a2dd95SBruce Richardson 	ethdev_init_t ethdev_init, void *init_params)
490099a2dd95SBruce Richardson {
490199a2dd95SBruce Richardson 	struct rte_eth_dev *ethdev;
490299a2dd95SBruce Richardson 	int retval;
490399a2dd95SBruce Richardson 
490499a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*ethdev_init, -EINVAL);
490599a2dd95SBruce Richardson 
490699a2dd95SBruce Richardson 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
490799a2dd95SBruce Richardson 		ethdev = rte_eth_dev_allocate(name);
490899a2dd95SBruce Richardson 		if (!ethdev)
490999a2dd95SBruce Richardson 			return -ENODEV;
491099a2dd95SBruce Richardson 
491199a2dd95SBruce Richardson 		if (priv_data_size) {
491299a2dd95SBruce Richardson 			ethdev->data->dev_private = rte_zmalloc_socket(
491399a2dd95SBruce Richardson 				name, priv_data_size, RTE_CACHE_LINE_SIZE,
491499a2dd95SBruce Richardson 				device->numa_node);
491599a2dd95SBruce Richardson 
491699a2dd95SBruce Richardson 			if (!ethdev->data->dev_private) {
491799a2dd95SBruce Richardson 				RTE_ETHDEV_LOG(ERR,
491899a2dd95SBruce Richardson 					"failed to allocate private data\n");
491999a2dd95SBruce Richardson 				retval = -ENOMEM;
492099a2dd95SBruce Richardson 				goto probe_failed;
492199a2dd95SBruce Richardson 			}
492299a2dd95SBruce Richardson 		}
492399a2dd95SBruce Richardson 	} else {
492499a2dd95SBruce Richardson 		ethdev = rte_eth_dev_attach_secondary(name);
492599a2dd95SBruce Richardson 		if (!ethdev) {
492699a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
492799a2dd95SBruce Richardson 				"secondary process attach failed, ethdev doesn't exist\n");
492899a2dd95SBruce Richardson 			return  -ENODEV;
492999a2dd95SBruce Richardson 		}
493099a2dd95SBruce Richardson 	}
493199a2dd95SBruce Richardson 
493299a2dd95SBruce Richardson 	ethdev->device = device;
493399a2dd95SBruce Richardson 
493499a2dd95SBruce Richardson 	if (ethdev_bus_specific_init) {
493599a2dd95SBruce Richardson 		retval = ethdev_bus_specific_init(ethdev, bus_init_params);
493699a2dd95SBruce Richardson 		if (retval) {
493799a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
493899a2dd95SBruce Richardson 				"ethdev bus specific initialisation failed\n");
493999a2dd95SBruce Richardson 			goto probe_failed;
494099a2dd95SBruce Richardson 		}
494199a2dd95SBruce Richardson 	}
494299a2dd95SBruce Richardson 
494399a2dd95SBruce Richardson 	retval = ethdev_init(ethdev, init_params);
494499a2dd95SBruce Richardson 	if (retval) {
494599a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "ethdev initialisation failed\n");
494699a2dd95SBruce Richardson 		goto probe_failed;
494799a2dd95SBruce Richardson 	}
494899a2dd95SBruce Richardson 
494999a2dd95SBruce Richardson 	rte_eth_dev_probing_finish(ethdev);
495099a2dd95SBruce Richardson 
495199a2dd95SBruce Richardson 	return retval;
495299a2dd95SBruce Richardson 
495399a2dd95SBruce Richardson probe_failed:
495499a2dd95SBruce Richardson 	rte_eth_dev_release_port(ethdev);
495599a2dd95SBruce Richardson 	return retval;
495699a2dd95SBruce Richardson }
495799a2dd95SBruce Richardson 
495899a2dd95SBruce Richardson int
495999a2dd95SBruce Richardson rte_eth_dev_destroy(struct rte_eth_dev *ethdev,
496099a2dd95SBruce Richardson 	ethdev_uninit_t ethdev_uninit)
496199a2dd95SBruce Richardson {
496299a2dd95SBruce Richardson 	int ret;
496399a2dd95SBruce Richardson 
496499a2dd95SBruce Richardson 	ethdev = rte_eth_dev_allocated(ethdev->data->name);
496599a2dd95SBruce Richardson 	if (!ethdev)
496699a2dd95SBruce Richardson 		return -ENODEV;
496799a2dd95SBruce Richardson 
496899a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*ethdev_uninit, -EINVAL);
496999a2dd95SBruce Richardson 
497099a2dd95SBruce Richardson 	ret = ethdev_uninit(ethdev);
497199a2dd95SBruce Richardson 	if (ret)
497299a2dd95SBruce Richardson 		return ret;
497399a2dd95SBruce Richardson 
497499a2dd95SBruce Richardson 	return rte_eth_dev_release_port(ethdev);
497599a2dd95SBruce Richardson }
497699a2dd95SBruce Richardson 
497799a2dd95SBruce Richardson int
497899a2dd95SBruce Richardson rte_eth_dev_rx_intr_ctl_q(uint16_t port_id, uint16_t queue_id,
497999a2dd95SBruce Richardson 			  int epfd, int op, void *data)
498099a2dd95SBruce Richardson {
498199a2dd95SBruce Richardson 	uint32_t vec;
498299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
498399a2dd95SBruce Richardson 	struct rte_intr_handle *intr_handle;
498499a2dd95SBruce Richardson 	int rc;
498599a2dd95SBruce Richardson 
498699a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
498799a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
498853ef1b34SMin Hu (Connor) 
498999a2dd95SBruce Richardson 	if (queue_id >= dev->data->nb_rx_queues) {
499099a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid RX queue_id=%u\n", queue_id);
499199a2dd95SBruce Richardson 		return -EINVAL;
499299a2dd95SBruce Richardson 	}
499399a2dd95SBruce Richardson 
499499a2dd95SBruce Richardson 	if (!dev->intr_handle) {
499599a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "RX Intr handle unset\n");
499699a2dd95SBruce Richardson 		return -ENOTSUP;
499799a2dd95SBruce Richardson 	}
499899a2dd95SBruce Richardson 
499999a2dd95SBruce Richardson 	intr_handle = dev->intr_handle;
500099a2dd95SBruce Richardson 	if (!intr_handle->intr_vec) {
500199a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "RX Intr vector unset\n");
500299a2dd95SBruce Richardson 		return -EPERM;
500399a2dd95SBruce Richardson 	}
500499a2dd95SBruce Richardson 
500599a2dd95SBruce Richardson 	vec = intr_handle->intr_vec[queue_id];
500699a2dd95SBruce Richardson 	rc = rte_intr_rx_ctl(intr_handle, epfd, op, vec, data);
500799a2dd95SBruce Richardson 	if (rc && rc != -EEXIST) {
500899a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
500999a2dd95SBruce Richardson 			"p %u q %u rx ctl error op %d epfd %d vec %u\n",
501099a2dd95SBruce Richardson 			port_id, queue_id, op, epfd, vec);
501199a2dd95SBruce Richardson 		return rc;
501299a2dd95SBruce Richardson 	}
501399a2dd95SBruce Richardson 
501499a2dd95SBruce Richardson 	return 0;
501599a2dd95SBruce Richardson }
501699a2dd95SBruce Richardson 
501799a2dd95SBruce Richardson int
501899a2dd95SBruce Richardson rte_eth_dev_rx_intr_enable(uint16_t port_id,
501999a2dd95SBruce Richardson 			   uint16_t queue_id)
502099a2dd95SBruce Richardson {
502199a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
502299a2dd95SBruce Richardson 	int ret;
502399a2dd95SBruce Richardson 
502499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
502599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
502699a2dd95SBruce Richardson 
502799a2dd95SBruce Richardson 	ret = eth_dev_validate_rx_queue(dev, queue_id);
502899a2dd95SBruce Richardson 	if (ret != 0)
502999a2dd95SBruce Richardson 		return ret;
503099a2dd95SBruce Richardson 
503199a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_intr_enable, -ENOTSUP);
503253ef1b34SMin Hu (Connor) 	return eth_err(port_id, (*dev->dev_ops->rx_queue_intr_enable)(dev, queue_id));
503399a2dd95SBruce Richardson }
503499a2dd95SBruce Richardson 
503599a2dd95SBruce Richardson int
503699a2dd95SBruce Richardson rte_eth_dev_rx_intr_disable(uint16_t port_id,
503799a2dd95SBruce Richardson 			    uint16_t queue_id)
503899a2dd95SBruce Richardson {
503999a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
504099a2dd95SBruce Richardson 	int ret;
504199a2dd95SBruce Richardson 
504299a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
504399a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
504499a2dd95SBruce Richardson 
504599a2dd95SBruce Richardson 	ret = eth_dev_validate_rx_queue(dev, queue_id);
504699a2dd95SBruce Richardson 	if (ret != 0)
504799a2dd95SBruce Richardson 		return ret;
504899a2dd95SBruce Richardson 
504999a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_intr_disable, -ENOTSUP);
505053ef1b34SMin Hu (Connor) 	return eth_err(port_id, (*dev->dev_ops->rx_queue_intr_disable)(dev, queue_id));
505199a2dd95SBruce Richardson }
505299a2dd95SBruce Richardson 
505399a2dd95SBruce Richardson 
505499a2dd95SBruce Richardson const struct rte_eth_rxtx_callback *
505599a2dd95SBruce Richardson rte_eth_add_rx_callback(uint16_t port_id, uint16_t queue_id,
505699a2dd95SBruce Richardson 		rte_rx_callback_fn fn, void *user_param)
505799a2dd95SBruce Richardson {
505899a2dd95SBruce Richardson #ifndef RTE_ETHDEV_RXTX_CALLBACKS
505999a2dd95SBruce Richardson 	rte_errno = ENOTSUP;
506099a2dd95SBruce Richardson 	return NULL;
506199a2dd95SBruce Richardson #endif
506299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
506399a2dd95SBruce Richardson 
506499a2dd95SBruce Richardson 	/* check input parameters */
506599a2dd95SBruce Richardson 	if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
506699a2dd95SBruce Richardson 		    queue_id >= rte_eth_devices[port_id].data->nb_rx_queues) {
506799a2dd95SBruce Richardson 		rte_errno = EINVAL;
506899a2dd95SBruce Richardson 		return NULL;
506999a2dd95SBruce Richardson 	}
507099a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
507199a2dd95SBruce Richardson 	if (rte_eth_dev_is_rx_hairpin_queue(dev, queue_id)) {
507299a2dd95SBruce Richardson 		rte_errno = EINVAL;
507399a2dd95SBruce Richardson 		return NULL;
507499a2dd95SBruce Richardson 	}
507599a2dd95SBruce Richardson 	struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
507699a2dd95SBruce Richardson 
507799a2dd95SBruce Richardson 	if (cb == NULL) {
507899a2dd95SBruce Richardson 		rte_errno = ENOMEM;
507999a2dd95SBruce Richardson 		return NULL;
508099a2dd95SBruce Richardson 	}
508199a2dd95SBruce Richardson 
508299a2dd95SBruce Richardson 	cb->fn.rx = fn;
508399a2dd95SBruce Richardson 	cb->param = user_param;
508499a2dd95SBruce Richardson 
508599a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_rx_cb_lock);
508699a2dd95SBruce Richardson 	/* Add the callbacks in fifo order. */
508799a2dd95SBruce Richardson 	struct rte_eth_rxtx_callback *tail =
508899a2dd95SBruce Richardson 		rte_eth_devices[port_id].post_rx_burst_cbs[queue_id];
508999a2dd95SBruce Richardson 
509099a2dd95SBruce Richardson 	if (!tail) {
509199a2dd95SBruce Richardson 		/* Stores to cb->fn and cb->param should complete before
509299a2dd95SBruce Richardson 		 * cb is visible to data plane.
509399a2dd95SBruce Richardson 		 */
509499a2dd95SBruce Richardson 		__atomic_store_n(
509599a2dd95SBruce Richardson 			&rte_eth_devices[port_id].post_rx_burst_cbs[queue_id],
509699a2dd95SBruce Richardson 			cb, __ATOMIC_RELEASE);
509799a2dd95SBruce Richardson 
509899a2dd95SBruce Richardson 	} else {
509999a2dd95SBruce Richardson 		while (tail->next)
510099a2dd95SBruce Richardson 			tail = tail->next;
510199a2dd95SBruce Richardson 		/* Stores to cb->fn and cb->param should complete before
510299a2dd95SBruce Richardson 		 * cb is visible to data plane.
510399a2dd95SBruce Richardson 		 */
510499a2dd95SBruce Richardson 		__atomic_store_n(&tail->next, cb, __ATOMIC_RELEASE);
510599a2dd95SBruce Richardson 	}
510699a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_rx_cb_lock);
510799a2dd95SBruce Richardson 
510899a2dd95SBruce Richardson 	return cb;
510999a2dd95SBruce Richardson }
511099a2dd95SBruce Richardson 
511199a2dd95SBruce Richardson const struct rte_eth_rxtx_callback *
511299a2dd95SBruce Richardson rte_eth_add_first_rx_callback(uint16_t port_id, uint16_t queue_id,
511399a2dd95SBruce Richardson 		rte_rx_callback_fn fn, void *user_param)
511499a2dd95SBruce Richardson {
511599a2dd95SBruce Richardson #ifndef RTE_ETHDEV_RXTX_CALLBACKS
511699a2dd95SBruce Richardson 	rte_errno = ENOTSUP;
511799a2dd95SBruce Richardson 	return NULL;
511899a2dd95SBruce Richardson #endif
511999a2dd95SBruce Richardson 	/* check input parameters */
512099a2dd95SBruce Richardson 	if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
512199a2dd95SBruce Richardson 		queue_id >= rte_eth_devices[port_id].data->nb_rx_queues) {
512299a2dd95SBruce Richardson 		rte_errno = EINVAL;
512399a2dd95SBruce Richardson 		return NULL;
512499a2dd95SBruce Richardson 	}
512599a2dd95SBruce Richardson 
512699a2dd95SBruce Richardson 	struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
512799a2dd95SBruce Richardson 
512899a2dd95SBruce Richardson 	if (cb == NULL) {
512999a2dd95SBruce Richardson 		rte_errno = ENOMEM;
513099a2dd95SBruce Richardson 		return NULL;
513199a2dd95SBruce Richardson 	}
513299a2dd95SBruce Richardson 
513399a2dd95SBruce Richardson 	cb->fn.rx = fn;
513499a2dd95SBruce Richardson 	cb->param = user_param;
513599a2dd95SBruce Richardson 
513699a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_rx_cb_lock);
513799a2dd95SBruce Richardson 	/* Add the callbacks at first position */
513899a2dd95SBruce Richardson 	cb->next = rte_eth_devices[port_id].post_rx_burst_cbs[queue_id];
513999a2dd95SBruce Richardson 	/* Stores to cb->fn, cb->param and cb->next should complete before
514099a2dd95SBruce Richardson 	 * cb is visible to data plane threads.
514199a2dd95SBruce Richardson 	 */
514299a2dd95SBruce Richardson 	__atomic_store_n(
514399a2dd95SBruce Richardson 		&rte_eth_devices[port_id].post_rx_burst_cbs[queue_id],
514499a2dd95SBruce Richardson 		cb, __ATOMIC_RELEASE);
514599a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_rx_cb_lock);
514699a2dd95SBruce Richardson 
514799a2dd95SBruce Richardson 	return cb;
514899a2dd95SBruce Richardson }
514999a2dd95SBruce Richardson 
515099a2dd95SBruce Richardson const struct rte_eth_rxtx_callback *
515199a2dd95SBruce Richardson rte_eth_add_tx_callback(uint16_t port_id, uint16_t queue_id,
515299a2dd95SBruce Richardson 		rte_tx_callback_fn fn, void *user_param)
515399a2dd95SBruce Richardson {
515499a2dd95SBruce Richardson #ifndef RTE_ETHDEV_RXTX_CALLBACKS
515599a2dd95SBruce Richardson 	rte_errno = ENOTSUP;
515699a2dd95SBruce Richardson 	return NULL;
515799a2dd95SBruce Richardson #endif
515899a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
515999a2dd95SBruce Richardson 
516099a2dd95SBruce Richardson 	/* check input parameters */
516199a2dd95SBruce Richardson 	if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
516299a2dd95SBruce Richardson 		    queue_id >= rte_eth_devices[port_id].data->nb_tx_queues) {
516399a2dd95SBruce Richardson 		rte_errno = EINVAL;
516499a2dd95SBruce Richardson 		return NULL;
516599a2dd95SBruce Richardson 	}
516699a2dd95SBruce Richardson 
516799a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
516899a2dd95SBruce Richardson 	if (rte_eth_dev_is_tx_hairpin_queue(dev, queue_id)) {
516999a2dd95SBruce Richardson 		rte_errno = EINVAL;
517099a2dd95SBruce Richardson 		return NULL;
517199a2dd95SBruce Richardson 	}
517299a2dd95SBruce Richardson 
517399a2dd95SBruce Richardson 	struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
517499a2dd95SBruce Richardson 
517599a2dd95SBruce Richardson 	if (cb == NULL) {
517699a2dd95SBruce Richardson 		rte_errno = ENOMEM;
517799a2dd95SBruce Richardson 		return NULL;
517899a2dd95SBruce Richardson 	}
517999a2dd95SBruce Richardson 
518099a2dd95SBruce Richardson 	cb->fn.tx = fn;
518199a2dd95SBruce Richardson 	cb->param = user_param;
518299a2dd95SBruce Richardson 
518399a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_tx_cb_lock);
518499a2dd95SBruce Richardson 	/* Add the callbacks in fifo order. */
518599a2dd95SBruce Richardson 	struct rte_eth_rxtx_callback *tail =
518699a2dd95SBruce Richardson 		rte_eth_devices[port_id].pre_tx_burst_cbs[queue_id];
518799a2dd95SBruce Richardson 
518899a2dd95SBruce Richardson 	if (!tail) {
518999a2dd95SBruce Richardson 		/* Stores to cb->fn and cb->param should complete before
519099a2dd95SBruce Richardson 		 * cb is visible to data plane.
519199a2dd95SBruce Richardson 		 */
519299a2dd95SBruce Richardson 		__atomic_store_n(
519399a2dd95SBruce Richardson 			&rte_eth_devices[port_id].pre_tx_burst_cbs[queue_id],
519499a2dd95SBruce Richardson 			cb, __ATOMIC_RELEASE);
519599a2dd95SBruce Richardson 
519699a2dd95SBruce Richardson 	} else {
519799a2dd95SBruce Richardson 		while (tail->next)
519899a2dd95SBruce Richardson 			tail = tail->next;
519999a2dd95SBruce Richardson 		/* Stores to cb->fn and cb->param should complete before
520099a2dd95SBruce Richardson 		 * cb is visible to data plane.
520199a2dd95SBruce Richardson 		 */
520299a2dd95SBruce Richardson 		__atomic_store_n(&tail->next, cb, __ATOMIC_RELEASE);
520399a2dd95SBruce Richardson 	}
520499a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_tx_cb_lock);
520599a2dd95SBruce Richardson 
520699a2dd95SBruce Richardson 	return cb;
520799a2dd95SBruce Richardson }
520899a2dd95SBruce Richardson 
520999a2dd95SBruce Richardson int
521099a2dd95SBruce Richardson rte_eth_remove_rx_callback(uint16_t port_id, uint16_t queue_id,
521199a2dd95SBruce Richardson 		const struct rte_eth_rxtx_callback *user_cb)
521299a2dd95SBruce Richardson {
521399a2dd95SBruce Richardson #ifndef RTE_ETHDEV_RXTX_CALLBACKS
521499a2dd95SBruce Richardson 	return -ENOTSUP;
521599a2dd95SBruce Richardson #endif
521699a2dd95SBruce Richardson 	/* Check input parameters. */
521799a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
521899a2dd95SBruce Richardson 	if (user_cb == NULL ||
521999a2dd95SBruce Richardson 			queue_id >= rte_eth_devices[port_id].data->nb_rx_queues)
522099a2dd95SBruce Richardson 		return -EINVAL;
522199a2dd95SBruce Richardson 
522299a2dd95SBruce Richardson 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
522399a2dd95SBruce Richardson 	struct rte_eth_rxtx_callback *cb;
522499a2dd95SBruce Richardson 	struct rte_eth_rxtx_callback **prev_cb;
522599a2dd95SBruce Richardson 	int ret = -EINVAL;
522699a2dd95SBruce Richardson 
522799a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_rx_cb_lock);
522899a2dd95SBruce Richardson 	prev_cb = &dev->post_rx_burst_cbs[queue_id];
522999a2dd95SBruce Richardson 	for (; *prev_cb != NULL; prev_cb = &cb->next) {
523099a2dd95SBruce Richardson 		cb = *prev_cb;
523199a2dd95SBruce Richardson 		if (cb == user_cb) {
523299a2dd95SBruce Richardson 			/* Remove the user cb from the callback list. */
523399a2dd95SBruce Richardson 			__atomic_store_n(prev_cb, cb->next, __ATOMIC_RELAXED);
523499a2dd95SBruce Richardson 			ret = 0;
523599a2dd95SBruce Richardson 			break;
523699a2dd95SBruce Richardson 		}
523799a2dd95SBruce Richardson 	}
523899a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_rx_cb_lock);
523999a2dd95SBruce Richardson 
524099a2dd95SBruce Richardson 	return ret;
524199a2dd95SBruce Richardson }
524299a2dd95SBruce Richardson 
524399a2dd95SBruce Richardson int
524499a2dd95SBruce Richardson rte_eth_remove_tx_callback(uint16_t port_id, uint16_t queue_id,
524599a2dd95SBruce Richardson 		const struct rte_eth_rxtx_callback *user_cb)
524699a2dd95SBruce Richardson {
524799a2dd95SBruce Richardson #ifndef RTE_ETHDEV_RXTX_CALLBACKS
524899a2dd95SBruce Richardson 	return -ENOTSUP;
524999a2dd95SBruce Richardson #endif
525099a2dd95SBruce Richardson 	/* Check input parameters. */
525199a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
525299a2dd95SBruce Richardson 	if (user_cb == NULL ||
525399a2dd95SBruce Richardson 			queue_id >= rte_eth_devices[port_id].data->nb_tx_queues)
525499a2dd95SBruce Richardson 		return -EINVAL;
525599a2dd95SBruce Richardson 
525699a2dd95SBruce Richardson 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
525799a2dd95SBruce Richardson 	int ret = -EINVAL;
525899a2dd95SBruce Richardson 	struct rte_eth_rxtx_callback *cb;
525999a2dd95SBruce Richardson 	struct rte_eth_rxtx_callback **prev_cb;
526099a2dd95SBruce Richardson 
526199a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_tx_cb_lock);
526299a2dd95SBruce Richardson 	prev_cb = &dev->pre_tx_burst_cbs[queue_id];
526399a2dd95SBruce Richardson 	for (; *prev_cb != NULL; prev_cb = &cb->next) {
526499a2dd95SBruce Richardson 		cb = *prev_cb;
526599a2dd95SBruce Richardson 		if (cb == user_cb) {
526699a2dd95SBruce Richardson 			/* Remove the user cb from the callback list. */
526799a2dd95SBruce Richardson 			__atomic_store_n(prev_cb, cb->next, __ATOMIC_RELAXED);
526899a2dd95SBruce Richardson 			ret = 0;
526999a2dd95SBruce Richardson 			break;
527099a2dd95SBruce Richardson 		}
527199a2dd95SBruce Richardson 	}
527299a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_tx_cb_lock);
527399a2dd95SBruce Richardson 
527499a2dd95SBruce Richardson 	return ret;
527599a2dd95SBruce Richardson }
527699a2dd95SBruce Richardson 
527799a2dd95SBruce Richardson int
527899a2dd95SBruce Richardson rte_eth_rx_queue_info_get(uint16_t port_id, uint16_t queue_id,
527999a2dd95SBruce Richardson 	struct rte_eth_rxq_info *qinfo)
528099a2dd95SBruce Richardson {
528199a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
528299a2dd95SBruce Richardson 
528399a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
528499a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
528553ef1b34SMin Hu (Connor) 
528699a2dd95SBruce Richardson 	if (queue_id >= dev->data->nb_rx_queues) {
528799a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid RX queue_id=%u\n", queue_id);
528899a2dd95SBruce Richardson 		return -EINVAL;
528999a2dd95SBruce Richardson 	}
529099a2dd95SBruce Richardson 
529153ef1b34SMin Hu (Connor) 	if (qinfo == NULL) {
529253ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot get ethdev port %u Rx queue %u info to NULL\n",
529353ef1b34SMin Hu (Connor) 			port_id, queue_id);
529453ef1b34SMin Hu (Connor) 		return -EINVAL;
529553ef1b34SMin Hu (Connor) 	}
529653ef1b34SMin Hu (Connor) 
529799a2dd95SBruce Richardson 	if (dev->data->rx_queues == NULL ||
529899a2dd95SBruce Richardson 			dev->data->rx_queues[queue_id] == NULL) {
529999a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
530099a2dd95SBruce Richardson 			       "Rx queue %"PRIu16" of device with port_id=%"
530199a2dd95SBruce Richardson 			       PRIu16" has not been setup\n",
530299a2dd95SBruce Richardson 			       queue_id, port_id);
530399a2dd95SBruce Richardson 		return -EINVAL;
530499a2dd95SBruce Richardson 	}
530599a2dd95SBruce Richardson 
530699a2dd95SBruce Richardson 	if (rte_eth_dev_is_rx_hairpin_queue(dev, queue_id)) {
530799a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(INFO,
530899a2dd95SBruce Richardson 			"Can't get hairpin Rx queue %"PRIu16" info of device with port_id=%"PRIu16"\n",
530999a2dd95SBruce Richardson 			queue_id, port_id);
531099a2dd95SBruce Richardson 		return -EINVAL;
531199a2dd95SBruce Richardson 	}
531299a2dd95SBruce Richardson 
531399a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rxq_info_get, -ENOTSUP);
531499a2dd95SBruce Richardson 
531599a2dd95SBruce Richardson 	memset(qinfo, 0, sizeof(*qinfo));
531699a2dd95SBruce Richardson 	dev->dev_ops->rxq_info_get(dev, queue_id, qinfo);
53179ad9ff47SLijun Ou 	qinfo->queue_state = dev->data->rx_queue_state[queue_id];
53189ad9ff47SLijun Ou 
531999a2dd95SBruce Richardson 	return 0;
532099a2dd95SBruce Richardson }
532199a2dd95SBruce Richardson 
532299a2dd95SBruce Richardson int
532399a2dd95SBruce Richardson rte_eth_tx_queue_info_get(uint16_t port_id, uint16_t queue_id,
532499a2dd95SBruce Richardson 	struct rte_eth_txq_info *qinfo)
532599a2dd95SBruce Richardson {
532699a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
532799a2dd95SBruce Richardson 
532899a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
532999a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
533053ef1b34SMin Hu (Connor) 
533199a2dd95SBruce Richardson 	if (queue_id >= dev->data->nb_tx_queues) {
533299a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid TX queue_id=%u\n", queue_id);
533399a2dd95SBruce Richardson 		return -EINVAL;
533499a2dd95SBruce Richardson 	}
533599a2dd95SBruce Richardson 
533653ef1b34SMin Hu (Connor) 	if (qinfo == NULL) {
533753ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot get ethdev port %u Tx queue %u info to NULL\n",
533853ef1b34SMin Hu (Connor) 			port_id, queue_id);
533953ef1b34SMin Hu (Connor) 		return -EINVAL;
534053ef1b34SMin Hu (Connor) 	}
534153ef1b34SMin Hu (Connor) 
534299a2dd95SBruce Richardson 	if (dev->data->tx_queues == NULL ||
534399a2dd95SBruce Richardson 			dev->data->tx_queues[queue_id] == NULL) {
534499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
534599a2dd95SBruce Richardson 			       "Tx queue %"PRIu16" of device with port_id=%"
534699a2dd95SBruce Richardson 			       PRIu16" has not been setup\n",
534799a2dd95SBruce Richardson 			       queue_id, port_id);
534899a2dd95SBruce Richardson 		return -EINVAL;
534999a2dd95SBruce Richardson 	}
535099a2dd95SBruce Richardson 
535199a2dd95SBruce Richardson 	if (rte_eth_dev_is_tx_hairpin_queue(dev, queue_id)) {
535299a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(INFO,
535399a2dd95SBruce Richardson 			"Can't get hairpin Tx queue %"PRIu16" info of device with port_id=%"PRIu16"\n",
535499a2dd95SBruce Richardson 			queue_id, port_id);
535599a2dd95SBruce Richardson 		return -EINVAL;
535699a2dd95SBruce Richardson 	}
535799a2dd95SBruce Richardson 
535899a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->txq_info_get, -ENOTSUP);
535999a2dd95SBruce Richardson 
536099a2dd95SBruce Richardson 	memset(qinfo, 0, sizeof(*qinfo));
536199a2dd95SBruce Richardson 	dev->dev_ops->txq_info_get(dev, queue_id, qinfo);
53629ad9ff47SLijun Ou 	qinfo->queue_state = dev->data->tx_queue_state[queue_id];
536399a2dd95SBruce Richardson 
536499a2dd95SBruce Richardson 	return 0;
536599a2dd95SBruce Richardson }
536699a2dd95SBruce Richardson 
536799a2dd95SBruce Richardson int
536899a2dd95SBruce Richardson rte_eth_rx_burst_mode_get(uint16_t port_id, uint16_t queue_id,
536999a2dd95SBruce Richardson 			  struct rte_eth_burst_mode *mode)
537099a2dd95SBruce Richardson {
537199a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
537299a2dd95SBruce Richardson 
537399a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
537499a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
537599a2dd95SBruce Richardson 
537699a2dd95SBruce Richardson 	if (queue_id >= dev->data->nb_rx_queues) {
537799a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid RX queue_id=%u\n", queue_id);
537899a2dd95SBruce Richardson 		return -EINVAL;
537999a2dd95SBruce Richardson 	}
538099a2dd95SBruce Richardson 
538153ef1b34SMin Hu (Connor) 	if (mode == NULL) {
538253ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
538353ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u Rx queue %u burst mode to NULL\n",
538453ef1b34SMin Hu (Connor) 			port_id, queue_id);
538553ef1b34SMin Hu (Connor) 		return -EINVAL;
538653ef1b34SMin Hu (Connor) 	}
538753ef1b34SMin Hu (Connor) 
538899a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_burst_mode_get, -ENOTSUP);
538999a2dd95SBruce Richardson 	memset(mode, 0, sizeof(*mode));
539099a2dd95SBruce Richardson 	return eth_err(port_id,
539199a2dd95SBruce Richardson 		       dev->dev_ops->rx_burst_mode_get(dev, queue_id, mode));
539299a2dd95SBruce Richardson }
539399a2dd95SBruce Richardson 
539499a2dd95SBruce Richardson int
539599a2dd95SBruce Richardson rte_eth_tx_burst_mode_get(uint16_t port_id, uint16_t queue_id,
539699a2dd95SBruce Richardson 			  struct rte_eth_burst_mode *mode)
539799a2dd95SBruce Richardson {
539899a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
539999a2dd95SBruce Richardson 
540099a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
540199a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
540299a2dd95SBruce Richardson 
540399a2dd95SBruce Richardson 	if (queue_id >= dev->data->nb_tx_queues) {
540499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid TX queue_id=%u\n", queue_id);
540599a2dd95SBruce Richardson 		return -EINVAL;
540699a2dd95SBruce Richardson 	}
540799a2dd95SBruce Richardson 
540853ef1b34SMin Hu (Connor) 	if (mode == NULL) {
540953ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
541053ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u Tx queue %u burst mode to NULL\n",
541153ef1b34SMin Hu (Connor) 			port_id, queue_id);
541253ef1b34SMin Hu (Connor) 		return -EINVAL;
541353ef1b34SMin Hu (Connor) 	}
541453ef1b34SMin Hu (Connor) 
541599a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_burst_mode_get, -ENOTSUP);
541699a2dd95SBruce Richardson 	memset(mode, 0, sizeof(*mode));
541799a2dd95SBruce Richardson 	return eth_err(port_id,
541899a2dd95SBruce Richardson 		       dev->dev_ops->tx_burst_mode_get(dev, queue_id, mode));
541999a2dd95SBruce Richardson }
542099a2dd95SBruce Richardson 
542199a2dd95SBruce Richardson int
542299a2dd95SBruce Richardson rte_eth_get_monitor_addr(uint16_t port_id, uint16_t queue_id,
542399a2dd95SBruce Richardson 		struct rte_power_monitor_cond *pmc)
542499a2dd95SBruce Richardson {
542599a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
542699a2dd95SBruce Richardson 
542799a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
542899a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
542999a2dd95SBruce Richardson 
543099a2dd95SBruce Richardson 	if (queue_id >= dev->data->nb_rx_queues) {
543199a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid Rx queue_id=%u\n", queue_id);
543299a2dd95SBruce Richardson 		return -EINVAL;
543399a2dd95SBruce Richardson 	}
543499a2dd95SBruce Richardson 
543599a2dd95SBruce Richardson 	if (pmc == NULL) {
543653ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
543753ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u Rx queue %u power monitor condition to NULL\n",
543853ef1b34SMin Hu (Connor) 			port_id, queue_id);
543999a2dd95SBruce Richardson 		return -EINVAL;
544099a2dd95SBruce Richardson 	}
544199a2dd95SBruce Richardson 
544253ef1b34SMin Hu (Connor) 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_monitor_addr, -ENOTSUP);
544399a2dd95SBruce Richardson 	return eth_err(port_id,
544453ef1b34SMin Hu (Connor) 		dev->dev_ops->get_monitor_addr(dev->data->rx_queues[queue_id], pmc));
544599a2dd95SBruce Richardson }
544699a2dd95SBruce Richardson 
544799a2dd95SBruce Richardson int
544899a2dd95SBruce Richardson rte_eth_dev_set_mc_addr_list(uint16_t port_id,
544999a2dd95SBruce Richardson 			     struct rte_ether_addr *mc_addr_set,
545099a2dd95SBruce Richardson 			     uint32_t nb_mc_addr)
545199a2dd95SBruce Richardson {
545299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
545399a2dd95SBruce Richardson 
545499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
545599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
545653ef1b34SMin Hu (Connor) 
545799a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_mc_addr_list, -ENOTSUP);
545899a2dd95SBruce Richardson 	return eth_err(port_id, dev->dev_ops->set_mc_addr_list(dev,
545999a2dd95SBruce Richardson 						mc_addr_set, nb_mc_addr));
546099a2dd95SBruce Richardson }
546199a2dd95SBruce Richardson 
546299a2dd95SBruce Richardson int
546399a2dd95SBruce Richardson rte_eth_timesync_enable(uint16_t port_id)
546499a2dd95SBruce Richardson {
546599a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
546699a2dd95SBruce Richardson 
546799a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
546899a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
546999a2dd95SBruce Richardson 
547099a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_enable, -ENOTSUP);
547199a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->timesync_enable)(dev));
547299a2dd95SBruce Richardson }
547399a2dd95SBruce Richardson 
547499a2dd95SBruce Richardson int
547599a2dd95SBruce Richardson rte_eth_timesync_disable(uint16_t port_id)
547699a2dd95SBruce Richardson {
547799a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
547899a2dd95SBruce Richardson 
547999a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
548099a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
548199a2dd95SBruce Richardson 
548299a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_disable, -ENOTSUP);
548399a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->timesync_disable)(dev));
548499a2dd95SBruce Richardson }
548599a2dd95SBruce Richardson 
548699a2dd95SBruce Richardson int
548799a2dd95SBruce Richardson rte_eth_timesync_read_rx_timestamp(uint16_t port_id, struct timespec *timestamp,
548899a2dd95SBruce Richardson 				   uint32_t flags)
548999a2dd95SBruce Richardson {
549099a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
549199a2dd95SBruce Richardson 
549299a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
549399a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
549499a2dd95SBruce Richardson 
549553ef1b34SMin Hu (Connor) 	if (timestamp == NULL) {
549653ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
549753ef1b34SMin Hu (Connor) 			"Cannot read ethdev port %u Rx timestamp to NULL\n",
549853ef1b34SMin Hu (Connor) 			port_id);
549953ef1b34SMin Hu (Connor) 		return -EINVAL;
550053ef1b34SMin Hu (Connor) 	}
550153ef1b34SMin Hu (Connor) 
550299a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_rx_timestamp, -ENOTSUP);
550399a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->timesync_read_rx_timestamp)
550499a2dd95SBruce Richardson 				(dev, timestamp, flags));
550599a2dd95SBruce Richardson }
550699a2dd95SBruce Richardson 
550799a2dd95SBruce Richardson int
550899a2dd95SBruce Richardson rte_eth_timesync_read_tx_timestamp(uint16_t port_id,
550999a2dd95SBruce Richardson 				   struct timespec *timestamp)
551099a2dd95SBruce Richardson {
551199a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
551299a2dd95SBruce Richardson 
551399a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
551499a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
551599a2dd95SBruce Richardson 
551653ef1b34SMin Hu (Connor) 	if (timestamp == NULL) {
551753ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
551853ef1b34SMin Hu (Connor) 			"Cannot read ethdev port %u Tx timestamp to NULL\n",
551953ef1b34SMin Hu (Connor) 			port_id);
552053ef1b34SMin Hu (Connor) 		return -EINVAL;
552153ef1b34SMin Hu (Connor) 	}
552253ef1b34SMin Hu (Connor) 
552399a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_tx_timestamp, -ENOTSUP);
552499a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->timesync_read_tx_timestamp)
552599a2dd95SBruce Richardson 				(dev, timestamp));
552699a2dd95SBruce Richardson }
552799a2dd95SBruce Richardson 
552899a2dd95SBruce Richardson int
552999a2dd95SBruce Richardson rte_eth_timesync_adjust_time(uint16_t port_id, int64_t delta)
553099a2dd95SBruce Richardson {
553199a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
553299a2dd95SBruce Richardson 
553399a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
553499a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
553599a2dd95SBruce Richardson 
553699a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_adjust_time, -ENOTSUP);
553753ef1b34SMin Hu (Connor) 	return eth_err(port_id, (*dev->dev_ops->timesync_adjust_time)(dev, delta));
553899a2dd95SBruce Richardson }
553999a2dd95SBruce Richardson 
554099a2dd95SBruce Richardson int
554199a2dd95SBruce Richardson rte_eth_timesync_read_time(uint16_t port_id, struct timespec *timestamp)
554299a2dd95SBruce Richardson {
554399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
554499a2dd95SBruce Richardson 
554599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
554699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
554799a2dd95SBruce Richardson 
554853ef1b34SMin Hu (Connor) 	if (timestamp == NULL) {
554953ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
555053ef1b34SMin Hu (Connor) 			"Cannot read ethdev port %u timesync time to NULL\n",
555153ef1b34SMin Hu (Connor) 			port_id);
555253ef1b34SMin Hu (Connor) 		return -EINVAL;
555353ef1b34SMin Hu (Connor) 	}
555453ef1b34SMin Hu (Connor) 
555599a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_time, -ENOTSUP);
555699a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->timesync_read_time)(dev,
555799a2dd95SBruce Richardson 								timestamp));
555899a2dd95SBruce Richardson }
555999a2dd95SBruce Richardson 
556099a2dd95SBruce Richardson int
556199a2dd95SBruce Richardson rte_eth_timesync_write_time(uint16_t port_id, const struct timespec *timestamp)
556299a2dd95SBruce Richardson {
556399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
556499a2dd95SBruce Richardson 
556599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
556699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
556799a2dd95SBruce Richardson 
556853ef1b34SMin Hu (Connor) 	if (timestamp == NULL) {
556953ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
557053ef1b34SMin Hu (Connor) 			"Cannot write ethdev port %u timesync from NULL time\n",
557153ef1b34SMin Hu (Connor) 			port_id);
557253ef1b34SMin Hu (Connor) 		return -EINVAL;
557353ef1b34SMin Hu (Connor) 	}
557453ef1b34SMin Hu (Connor) 
557599a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_write_time, -ENOTSUP);
557699a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->timesync_write_time)(dev,
557799a2dd95SBruce Richardson 								timestamp));
557899a2dd95SBruce Richardson }
557999a2dd95SBruce Richardson 
558099a2dd95SBruce Richardson int
558199a2dd95SBruce Richardson rte_eth_read_clock(uint16_t port_id, uint64_t *clock)
558299a2dd95SBruce Richardson {
558399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
558499a2dd95SBruce Richardson 
558599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
558699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
558799a2dd95SBruce Richardson 
558853ef1b34SMin Hu (Connor) 	if (clock == NULL) {
558953ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot read ethdev port %u clock to NULL\n",
559053ef1b34SMin Hu (Connor) 			port_id);
559153ef1b34SMin Hu (Connor) 		return -EINVAL;
559253ef1b34SMin Hu (Connor) 	}
559353ef1b34SMin Hu (Connor) 
559499a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->read_clock, -ENOTSUP);
559599a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->read_clock)(dev, clock));
559699a2dd95SBruce Richardson }
559799a2dd95SBruce Richardson 
559899a2dd95SBruce Richardson int
559999a2dd95SBruce Richardson rte_eth_dev_get_reg_info(uint16_t port_id, struct rte_dev_reg_info *info)
560099a2dd95SBruce Richardson {
560199a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
560299a2dd95SBruce Richardson 
560399a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
560499a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
560553ef1b34SMin Hu (Connor) 
560653ef1b34SMin Hu (Connor) 	if (info == NULL) {
560753ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
560853ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u register info to NULL\n",
560953ef1b34SMin Hu (Connor) 			port_id);
561053ef1b34SMin Hu (Connor) 		return -EINVAL;
561153ef1b34SMin Hu (Connor) 	}
561253ef1b34SMin Hu (Connor) 
561399a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_reg, -ENOTSUP);
561499a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->get_reg)(dev, info));
561599a2dd95SBruce Richardson }
561699a2dd95SBruce Richardson 
561799a2dd95SBruce Richardson int
561899a2dd95SBruce Richardson rte_eth_dev_get_eeprom_length(uint16_t port_id)
561999a2dd95SBruce Richardson {
562099a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
562199a2dd95SBruce Richardson 
562299a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
562399a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
562453ef1b34SMin Hu (Connor) 
562599a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_eeprom_length, -ENOTSUP);
562699a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->get_eeprom_length)(dev));
562799a2dd95SBruce Richardson }
562899a2dd95SBruce Richardson 
562999a2dd95SBruce Richardson int
563099a2dd95SBruce Richardson rte_eth_dev_get_eeprom(uint16_t port_id, struct rte_dev_eeprom_info *info)
563199a2dd95SBruce Richardson {
563299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
563399a2dd95SBruce Richardson 
563499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
563599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
563653ef1b34SMin Hu (Connor) 
563753ef1b34SMin Hu (Connor) 	if (info == NULL) {
563853ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
563953ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u EEPROM info to NULL\n",
564053ef1b34SMin Hu (Connor) 			port_id);
564153ef1b34SMin Hu (Connor) 		return -EINVAL;
564253ef1b34SMin Hu (Connor) 	}
564353ef1b34SMin Hu (Connor) 
564499a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_eeprom, -ENOTSUP);
564599a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->get_eeprom)(dev, info));
564699a2dd95SBruce Richardson }
564799a2dd95SBruce Richardson 
564899a2dd95SBruce Richardson int
564999a2dd95SBruce Richardson rte_eth_dev_set_eeprom(uint16_t port_id, struct rte_dev_eeprom_info *info)
565099a2dd95SBruce Richardson {
565199a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
565299a2dd95SBruce Richardson 
565399a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
565499a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
565553ef1b34SMin Hu (Connor) 
565653ef1b34SMin Hu (Connor) 	if (info == NULL) {
565753ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
565853ef1b34SMin Hu (Connor) 			"Cannot set ethdev port %u EEPROM from NULL info\n",
565953ef1b34SMin Hu (Connor) 			port_id);
566053ef1b34SMin Hu (Connor) 		return -EINVAL;
566153ef1b34SMin Hu (Connor) 	}
566253ef1b34SMin Hu (Connor) 
566399a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_eeprom, -ENOTSUP);
566499a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->set_eeprom)(dev, info));
566599a2dd95SBruce Richardson }
566699a2dd95SBruce Richardson 
566799a2dd95SBruce Richardson int
566899a2dd95SBruce Richardson rte_eth_dev_get_module_info(uint16_t port_id,
566999a2dd95SBruce Richardson 			    struct rte_eth_dev_module_info *modinfo)
567099a2dd95SBruce Richardson {
567199a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
567299a2dd95SBruce Richardson 
567399a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
567499a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
567553ef1b34SMin Hu (Connor) 
567653ef1b34SMin Hu (Connor) 	if (modinfo == NULL) {
567753ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
567853ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u EEPROM module info to NULL\n",
567953ef1b34SMin Hu (Connor) 			port_id);
568053ef1b34SMin Hu (Connor) 		return -EINVAL;
568153ef1b34SMin Hu (Connor) 	}
568253ef1b34SMin Hu (Connor) 
568399a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_module_info, -ENOTSUP);
568499a2dd95SBruce Richardson 	return (*dev->dev_ops->get_module_info)(dev, modinfo);
568599a2dd95SBruce Richardson }
568699a2dd95SBruce Richardson 
568799a2dd95SBruce Richardson int
568899a2dd95SBruce Richardson rte_eth_dev_get_module_eeprom(uint16_t port_id,
568999a2dd95SBruce Richardson 			      struct rte_dev_eeprom_info *info)
569099a2dd95SBruce Richardson {
569199a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
569299a2dd95SBruce Richardson 
569399a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
569499a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
569553ef1b34SMin Hu (Connor) 
569653ef1b34SMin Hu (Connor) 	if (info == NULL) {
569753ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
569853ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u module EEPROM info to NULL\n",
569953ef1b34SMin Hu (Connor) 			port_id);
570053ef1b34SMin Hu (Connor) 		return -EINVAL;
570153ef1b34SMin Hu (Connor) 	}
570253ef1b34SMin Hu (Connor) 
570353ef1b34SMin Hu (Connor) 	if (info->data == NULL) {
570453ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
570553ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u module EEPROM data to NULL\n",
570653ef1b34SMin Hu (Connor) 			port_id);
570753ef1b34SMin Hu (Connor) 		return -EINVAL;
570853ef1b34SMin Hu (Connor) 	}
570953ef1b34SMin Hu (Connor) 
571053ef1b34SMin Hu (Connor) 	if (info->length == 0) {
571153ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
571253ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u module EEPROM to data with zero size\n",
571353ef1b34SMin Hu (Connor) 			port_id);
571453ef1b34SMin Hu (Connor) 		return -EINVAL;
571553ef1b34SMin Hu (Connor) 	}
571653ef1b34SMin Hu (Connor) 
571799a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_module_eeprom, -ENOTSUP);
571899a2dd95SBruce Richardson 	return (*dev->dev_ops->get_module_eeprom)(dev, info);
571999a2dd95SBruce Richardson }
572099a2dd95SBruce Richardson 
572199a2dd95SBruce Richardson int
572299a2dd95SBruce Richardson rte_eth_dev_get_dcb_info(uint16_t port_id,
572399a2dd95SBruce Richardson 			     struct rte_eth_dcb_info *dcb_info)
572499a2dd95SBruce Richardson {
572599a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
572699a2dd95SBruce Richardson 
572799a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
572899a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
572953ef1b34SMin Hu (Connor) 
573053ef1b34SMin Hu (Connor) 	if (dcb_info == NULL) {
573153ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
573253ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u DCB info to NULL\n",
573353ef1b34SMin Hu (Connor) 			port_id);
573453ef1b34SMin Hu (Connor) 		return -EINVAL;
573553ef1b34SMin Hu (Connor) 	}
573653ef1b34SMin Hu (Connor) 
573799a2dd95SBruce Richardson 	memset(dcb_info, 0, sizeof(struct rte_eth_dcb_info));
573899a2dd95SBruce Richardson 
573999a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_dcb_info, -ENOTSUP);
574099a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->get_dcb_info)(dev, dcb_info));
574199a2dd95SBruce Richardson }
574299a2dd95SBruce Richardson 
574399a2dd95SBruce Richardson static void
574499a2dd95SBruce Richardson eth_dev_adjust_nb_desc(uint16_t *nb_desc,
574599a2dd95SBruce Richardson 		const struct rte_eth_desc_lim *desc_lim)
574699a2dd95SBruce Richardson {
574799a2dd95SBruce Richardson 	if (desc_lim->nb_align != 0)
574899a2dd95SBruce Richardson 		*nb_desc = RTE_ALIGN_CEIL(*nb_desc, desc_lim->nb_align);
574999a2dd95SBruce Richardson 
575099a2dd95SBruce Richardson 	if (desc_lim->nb_max != 0)
575199a2dd95SBruce Richardson 		*nb_desc = RTE_MIN(*nb_desc, desc_lim->nb_max);
575299a2dd95SBruce Richardson 
575399a2dd95SBruce Richardson 	*nb_desc = RTE_MAX(*nb_desc, desc_lim->nb_min);
575499a2dd95SBruce Richardson }
575599a2dd95SBruce Richardson 
575699a2dd95SBruce Richardson int
575799a2dd95SBruce Richardson rte_eth_dev_adjust_nb_rx_tx_desc(uint16_t port_id,
575899a2dd95SBruce Richardson 				 uint16_t *nb_rx_desc,
575999a2dd95SBruce Richardson 				 uint16_t *nb_tx_desc)
576099a2dd95SBruce Richardson {
576199a2dd95SBruce Richardson 	struct rte_eth_dev_info dev_info;
576299a2dd95SBruce Richardson 	int ret;
576399a2dd95SBruce Richardson 
576499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
576599a2dd95SBruce Richardson 
576699a2dd95SBruce Richardson 	ret = rte_eth_dev_info_get(port_id, &dev_info);
576799a2dd95SBruce Richardson 	if (ret != 0)
576899a2dd95SBruce Richardson 		return ret;
576999a2dd95SBruce Richardson 
577099a2dd95SBruce Richardson 	if (nb_rx_desc != NULL)
577199a2dd95SBruce Richardson 		eth_dev_adjust_nb_desc(nb_rx_desc, &dev_info.rx_desc_lim);
577299a2dd95SBruce Richardson 
577399a2dd95SBruce Richardson 	if (nb_tx_desc != NULL)
577499a2dd95SBruce Richardson 		eth_dev_adjust_nb_desc(nb_tx_desc, &dev_info.tx_desc_lim);
577599a2dd95SBruce Richardson 
577699a2dd95SBruce Richardson 	return 0;
577799a2dd95SBruce Richardson }
577899a2dd95SBruce Richardson 
577999a2dd95SBruce Richardson int
578099a2dd95SBruce Richardson rte_eth_dev_hairpin_capability_get(uint16_t port_id,
578199a2dd95SBruce Richardson 				   struct rte_eth_hairpin_cap *cap)
578299a2dd95SBruce Richardson {
578399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
578499a2dd95SBruce Richardson 
578599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
578699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
578753ef1b34SMin Hu (Connor) 
578853ef1b34SMin Hu (Connor) 	if (cap == NULL) {
578953ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
579053ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u hairpin capability to NULL\n",
579153ef1b34SMin Hu (Connor) 			port_id);
579253ef1b34SMin Hu (Connor) 		return -EINVAL;
579353ef1b34SMin Hu (Connor) 	}
579453ef1b34SMin Hu (Connor) 
579599a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->hairpin_cap_get, -ENOTSUP);
579699a2dd95SBruce Richardson 	memset(cap, 0, sizeof(*cap));
579799a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->hairpin_cap_get)(dev, cap));
579899a2dd95SBruce Richardson }
579999a2dd95SBruce Richardson 
580099a2dd95SBruce Richardson int
580199a2dd95SBruce Richardson rte_eth_dev_is_rx_hairpin_queue(struct rte_eth_dev *dev, uint16_t queue_id)
580299a2dd95SBruce Richardson {
580353ef1b34SMin Hu (Connor) 	if (dev->data->rx_queue_state[queue_id] == RTE_ETH_QUEUE_STATE_HAIRPIN)
580499a2dd95SBruce Richardson 		return 1;
580599a2dd95SBruce Richardson 	return 0;
580699a2dd95SBruce Richardson }
580799a2dd95SBruce Richardson 
580899a2dd95SBruce Richardson int
580999a2dd95SBruce Richardson rte_eth_dev_is_tx_hairpin_queue(struct rte_eth_dev *dev, uint16_t queue_id)
581099a2dd95SBruce Richardson {
581153ef1b34SMin Hu (Connor) 	if (dev->data->tx_queue_state[queue_id] == RTE_ETH_QUEUE_STATE_HAIRPIN)
581299a2dd95SBruce Richardson 		return 1;
581399a2dd95SBruce Richardson 	return 0;
581499a2dd95SBruce Richardson }
581599a2dd95SBruce Richardson 
581699a2dd95SBruce Richardson int
581799a2dd95SBruce Richardson rte_eth_dev_pool_ops_supported(uint16_t port_id, const char *pool)
581899a2dd95SBruce Richardson {
581999a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
582099a2dd95SBruce Richardson 
582199a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
582299a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
582399a2dd95SBruce Richardson 
582453ef1b34SMin Hu (Connor) 	if (pool == NULL) {
582553ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
582653ef1b34SMin Hu (Connor) 			"Cannot test ethdev port %u mempool operation from NULL pool\n",
582753ef1b34SMin Hu (Connor) 			port_id);
582853ef1b34SMin Hu (Connor) 		return -EINVAL;
582953ef1b34SMin Hu (Connor) 	}
583053ef1b34SMin Hu (Connor) 
583199a2dd95SBruce Richardson 	if (*dev->dev_ops->pool_ops_supported == NULL)
583299a2dd95SBruce Richardson 		return 1; /* all pools are supported */
583399a2dd95SBruce Richardson 
583499a2dd95SBruce Richardson 	return (*dev->dev_ops->pool_ops_supported)(dev, pool);
583599a2dd95SBruce Richardson }
583699a2dd95SBruce Richardson 
583799a2dd95SBruce Richardson /**
583899a2dd95SBruce Richardson  * A set of values to describe the possible states of a switch domain.
583999a2dd95SBruce Richardson  */
584099a2dd95SBruce Richardson enum rte_eth_switch_domain_state {
584199a2dd95SBruce Richardson 	RTE_ETH_SWITCH_DOMAIN_UNUSED = 0,
584299a2dd95SBruce Richardson 	RTE_ETH_SWITCH_DOMAIN_ALLOCATED
584399a2dd95SBruce Richardson };
584499a2dd95SBruce Richardson 
584599a2dd95SBruce Richardson /**
584699a2dd95SBruce Richardson  * Array of switch domains available for allocation. Array is sized to
584799a2dd95SBruce Richardson  * RTE_MAX_ETHPORTS elements as there cannot be more active switch domains than
584899a2dd95SBruce Richardson  * ethdev ports in a single process.
584999a2dd95SBruce Richardson  */
585099a2dd95SBruce Richardson static struct rte_eth_dev_switch {
585199a2dd95SBruce Richardson 	enum rte_eth_switch_domain_state state;
585299a2dd95SBruce Richardson } eth_dev_switch_domains[RTE_MAX_ETHPORTS];
585399a2dd95SBruce Richardson 
585499a2dd95SBruce Richardson int
585599a2dd95SBruce Richardson rte_eth_switch_domain_alloc(uint16_t *domain_id)
585699a2dd95SBruce Richardson {
585799a2dd95SBruce Richardson 	uint16_t i;
585899a2dd95SBruce Richardson 
585999a2dd95SBruce Richardson 	*domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
586099a2dd95SBruce Richardson 
586199a2dd95SBruce Richardson 	for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
586299a2dd95SBruce Richardson 		if (eth_dev_switch_domains[i].state ==
586399a2dd95SBruce Richardson 			RTE_ETH_SWITCH_DOMAIN_UNUSED) {
586499a2dd95SBruce Richardson 			eth_dev_switch_domains[i].state =
586599a2dd95SBruce Richardson 				RTE_ETH_SWITCH_DOMAIN_ALLOCATED;
586699a2dd95SBruce Richardson 			*domain_id = i;
586799a2dd95SBruce Richardson 			return 0;
586899a2dd95SBruce Richardson 		}
586999a2dd95SBruce Richardson 	}
587099a2dd95SBruce Richardson 
587199a2dd95SBruce Richardson 	return -ENOSPC;
587299a2dd95SBruce Richardson }
587399a2dd95SBruce Richardson 
587499a2dd95SBruce Richardson int
587599a2dd95SBruce Richardson rte_eth_switch_domain_free(uint16_t domain_id)
587699a2dd95SBruce Richardson {
587799a2dd95SBruce Richardson 	if (domain_id == RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID ||
587899a2dd95SBruce Richardson 		domain_id >= RTE_MAX_ETHPORTS)
587999a2dd95SBruce Richardson 		return -EINVAL;
588099a2dd95SBruce Richardson 
588199a2dd95SBruce Richardson 	if (eth_dev_switch_domains[domain_id].state !=
588299a2dd95SBruce Richardson 		RTE_ETH_SWITCH_DOMAIN_ALLOCATED)
588399a2dd95SBruce Richardson 		return -EINVAL;
588499a2dd95SBruce Richardson 
588599a2dd95SBruce Richardson 	eth_dev_switch_domains[domain_id].state = RTE_ETH_SWITCH_DOMAIN_UNUSED;
588699a2dd95SBruce Richardson 
588799a2dd95SBruce Richardson 	return 0;
588899a2dd95SBruce Richardson }
588999a2dd95SBruce Richardson 
589099a2dd95SBruce Richardson static int
589199a2dd95SBruce Richardson eth_dev_devargs_tokenise(struct rte_kvargs *arglist, const char *str_in)
589299a2dd95SBruce Richardson {
589399a2dd95SBruce Richardson 	int state;
589499a2dd95SBruce Richardson 	struct rte_kvargs_pair *pair;
589599a2dd95SBruce Richardson 	char *letter;
589699a2dd95SBruce Richardson 
589799a2dd95SBruce Richardson 	arglist->str = strdup(str_in);
589899a2dd95SBruce Richardson 	if (arglist->str == NULL)
589999a2dd95SBruce Richardson 		return -ENOMEM;
590099a2dd95SBruce Richardson 
590199a2dd95SBruce Richardson 	letter = arglist->str;
590299a2dd95SBruce Richardson 	state = 0;
590399a2dd95SBruce Richardson 	arglist->count = 0;
590499a2dd95SBruce Richardson 	pair = &arglist->pairs[0];
590599a2dd95SBruce Richardson 	while (1) {
590699a2dd95SBruce Richardson 		switch (state) {
590799a2dd95SBruce Richardson 		case 0: /* Initial */
590899a2dd95SBruce Richardson 			if (*letter == '=')
590999a2dd95SBruce Richardson 				return -EINVAL;
591099a2dd95SBruce Richardson 			else if (*letter == '\0')
591199a2dd95SBruce Richardson 				return 0;
591299a2dd95SBruce Richardson 
591399a2dd95SBruce Richardson 			state = 1;
591499a2dd95SBruce Richardson 			pair->key = letter;
591599a2dd95SBruce Richardson 			/* fall-thru */
591699a2dd95SBruce Richardson 
591799a2dd95SBruce Richardson 		case 1: /* Parsing key */
591899a2dd95SBruce Richardson 			if (*letter == '=') {
591999a2dd95SBruce Richardson 				*letter = '\0';
592099a2dd95SBruce Richardson 				pair->value = letter + 1;
592199a2dd95SBruce Richardson 				state = 2;
592299a2dd95SBruce Richardson 			} else if (*letter == ',' || *letter == '\0')
592399a2dd95SBruce Richardson 				return -EINVAL;
592499a2dd95SBruce Richardson 			break;
592599a2dd95SBruce Richardson 
592699a2dd95SBruce Richardson 
592799a2dd95SBruce Richardson 		case 2: /* Parsing value */
592899a2dd95SBruce Richardson 			if (*letter == '[')
592999a2dd95SBruce Richardson 				state = 3;
593099a2dd95SBruce Richardson 			else if (*letter == ',') {
593199a2dd95SBruce Richardson 				*letter = '\0';
593299a2dd95SBruce Richardson 				arglist->count++;
593399a2dd95SBruce Richardson 				pair = &arglist->pairs[arglist->count];
593499a2dd95SBruce Richardson 				state = 0;
593599a2dd95SBruce Richardson 			} else if (*letter == '\0') {
593699a2dd95SBruce Richardson 				letter--;
593799a2dd95SBruce Richardson 				arglist->count++;
593899a2dd95SBruce Richardson 				pair = &arglist->pairs[arglist->count];
593999a2dd95SBruce Richardson 				state = 0;
594099a2dd95SBruce Richardson 			}
594199a2dd95SBruce Richardson 			break;
594299a2dd95SBruce Richardson 
594399a2dd95SBruce Richardson 		case 3: /* Parsing list */
594499a2dd95SBruce Richardson 			if (*letter == ']')
594599a2dd95SBruce Richardson 				state = 2;
594699a2dd95SBruce Richardson 			else if (*letter == '\0')
594799a2dd95SBruce Richardson 				return -EINVAL;
594899a2dd95SBruce Richardson 			break;
594999a2dd95SBruce Richardson 		}
595099a2dd95SBruce Richardson 		letter++;
595199a2dd95SBruce Richardson 	}
595299a2dd95SBruce Richardson }
595399a2dd95SBruce Richardson 
595499a2dd95SBruce Richardson int
595599a2dd95SBruce Richardson rte_eth_devargs_parse(const char *dargs, struct rte_eth_devargs *eth_da)
595699a2dd95SBruce Richardson {
595799a2dd95SBruce Richardson 	struct rte_kvargs args;
595899a2dd95SBruce Richardson 	struct rte_kvargs_pair *pair;
595999a2dd95SBruce Richardson 	unsigned int i;
596099a2dd95SBruce Richardson 	int result = 0;
596199a2dd95SBruce Richardson 
596299a2dd95SBruce Richardson 	memset(eth_da, 0, sizeof(*eth_da));
596399a2dd95SBruce Richardson 
596499a2dd95SBruce Richardson 	result = eth_dev_devargs_tokenise(&args, dargs);
596599a2dd95SBruce Richardson 	if (result < 0)
596699a2dd95SBruce Richardson 		goto parse_cleanup;
596799a2dd95SBruce Richardson 
596899a2dd95SBruce Richardson 	for (i = 0; i < args.count; i++) {
596999a2dd95SBruce Richardson 		pair = &args.pairs[i];
597099a2dd95SBruce Richardson 		if (strcmp("representor", pair->key) == 0) {
597199a2dd95SBruce Richardson 			if (eth_da->type != RTE_ETH_REPRESENTOR_NONE) {
597299a2dd95SBruce Richardson 				RTE_LOG(ERR, EAL, "duplicated representor key: %s\n",
597399a2dd95SBruce Richardson 					dargs);
597499a2dd95SBruce Richardson 				result = -1;
597599a2dd95SBruce Richardson 				goto parse_cleanup;
597699a2dd95SBruce Richardson 			}
597799a2dd95SBruce Richardson 			result = rte_eth_devargs_parse_representor_ports(
597899a2dd95SBruce Richardson 					pair->value, eth_da);
597999a2dd95SBruce Richardson 			if (result < 0)
598099a2dd95SBruce Richardson 				goto parse_cleanup;
598199a2dd95SBruce Richardson 		}
598299a2dd95SBruce Richardson 	}
598399a2dd95SBruce Richardson 
598499a2dd95SBruce Richardson parse_cleanup:
598599a2dd95SBruce Richardson 	if (args.str)
598699a2dd95SBruce Richardson 		free(args.str);
598799a2dd95SBruce Richardson 
598899a2dd95SBruce Richardson 	return result;
598999a2dd95SBruce Richardson }
599099a2dd95SBruce Richardson 
599199a2dd95SBruce Richardson int
5992ff4e52efSViacheslav Galaktionov rte_eth_representor_id_get(uint16_t port_id,
599399a2dd95SBruce Richardson 			   enum rte_eth_representor_type type,
599499a2dd95SBruce Richardson 			   int controller, int pf, int representor_port,
599599a2dd95SBruce Richardson 			   uint16_t *repr_id)
599699a2dd95SBruce Richardson {
599710eaf41dSViacheslav Galaktionov 	int ret, n, count;
599810eaf41dSViacheslav Galaktionov 	uint32_t i;
599999a2dd95SBruce Richardson 	struct rte_eth_representor_info *info = NULL;
600099a2dd95SBruce Richardson 	size_t size;
600199a2dd95SBruce Richardson 
600299a2dd95SBruce Richardson 	if (type == RTE_ETH_REPRESENTOR_NONE)
600399a2dd95SBruce Richardson 		return 0;
600499a2dd95SBruce Richardson 	if (repr_id == NULL)
600599a2dd95SBruce Richardson 		return -EINVAL;
600699a2dd95SBruce Richardson 
600799a2dd95SBruce Richardson 	/* Get PMD representor range info. */
6008ff4e52efSViacheslav Galaktionov 	ret = rte_eth_representor_info_get(port_id, NULL);
600999a2dd95SBruce Richardson 	if (ret == -ENOTSUP && type == RTE_ETH_REPRESENTOR_VF &&
601099a2dd95SBruce Richardson 	    controller == -1 && pf == -1) {
601199a2dd95SBruce Richardson 		/* Direct mapping for legacy VF representor. */
601299a2dd95SBruce Richardson 		*repr_id = representor_port;
601399a2dd95SBruce Richardson 		return 0;
601499a2dd95SBruce Richardson 	} else if (ret < 0) {
601599a2dd95SBruce Richardson 		return ret;
601699a2dd95SBruce Richardson 	}
601799a2dd95SBruce Richardson 	n = ret;
601899a2dd95SBruce Richardson 	size = sizeof(*info) + n * sizeof(info->ranges[0]);
601999a2dd95SBruce Richardson 	info = calloc(1, size);
602099a2dd95SBruce Richardson 	if (info == NULL)
602199a2dd95SBruce Richardson 		return -ENOMEM;
602210eaf41dSViacheslav Galaktionov 	info->nb_ranges_alloc = n;
6023ff4e52efSViacheslav Galaktionov 	ret = rte_eth_representor_info_get(port_id, info);
602499a2dd95SBruce Richardson 	if (ret < 0)
602599a2dd95SBruce Richardson 		goto out;
602699a2dd95SBruce Richardson 
602799a2dd95SBruce Richardson 	/* Default controller and pf to caller. */
602899a2dd95SBruce Richardson 	if (controller == -1)
602999a2dd95SBruce Richardson 		controller = info->controller;
603099a2dd95SBruce Richardson 	if (pf == -1)
603199a2dd95SBruce Richardson 		pf = info->pf;
603299a2dd95SBruce Richardson 
603399a2dd95SBruce Richardson 	/* Locate representor ID. */
603499a2dd95SBruce Richardson 	ret = -ENOENT;
603510eaf41dSViacheslav Galaktionov 	for (i = 0; i < info->nb_ranges; ++i) {
603699a2dd95SBruce Richardson 		if (info->ranges[i].type != type)
603799a2dd95SBruce Richardson 			continue;
603899a2dd95SBruce Richardson 		if (info->ranges[i].controller != controller)
603999a2dd95SBruce Richardson 			continue;
604099a2dd95SBruce Richardson 		if (info->ranges[i].id_end < info->ranges[i].id_base) {
604199a2dd95SBruce Richardson 			RTE_LOG(WARNING, EAL, "Port %hu invalid representor ID Range %u - %u, entry %d\n",
6042ff4e52efSViacheslav Galaktionov 				port_id, info->ranges[i].id_base,
604399a2dd95SBruce Richardson 				info->ranges[i].id_end, i);
604499a2dd95SBruce Richardson 			continue;
604599a2dd95SBruce Richardson 
604699a2dd95SBruce Richardson 		}
604799a2dd95SBruce Richardson 		count = info->ranges[i].id_end - info->ranges[i].id_base + 1;
604899a2dd95SBruce Richardson 		switch (info->ranges[i].type) {
604999a2dd95SBruce Richardson 		case RTE_ETH_REPRESENTOR_PF:
605099a2dd95SBruce Richardson 			if (pf < info->ranges[i].pf ||
605199a2dd95SBruce Richardson 			    pf >= info->ranges[i].pf + count)
605299a2dd95SBruce Richardson 				continue;
605399a2dd95SBruce Richardson 			*repr_id = info->ranges[i].id_base +
605499a2dd95SBruce Richardson 				   (pf - info->ranges[i].pf);
605599a2dd95SBruce Richardson 			ret = 0;
605699a2dd95SBruce Richardson 			goto out;
605799a2dd95SBruce Richardson 		case RTE_ETH_REPRESENTOR_VF:
605899a2dd95SBruce Richardson 			if (info->ranges[i].pf != pf)
605999a2dd95SBruce Richardson 				continue;
606099a2dd95SBruce Richardson 			if (representor_port < info->ranges[i].vf ||
606199a2dd95SBruce Richardson 			    representor_port >= info->ranges[i].vf + count)
606299a2dd95SBruce Richardson 				continue;
606399a2dd95SBruce Richardson 			*repr_id = info->ranges[i].id_base +
606499a2dd95SBruce Richardson 				   (representor_port - info->ranges[i].vf);
606599a2dd95SBruce Richardson 			ret = 0;
606699a2dd95SBruce Richardson 			goto out;
606799a2dd95SBruce Richardson 		case RTE_ETH_REPRESENTOR_SF:
606899a2dd95SBruce Richardson 			if (info->ranges[i].pf != pf)
606999a2dd95SBruce Richardson 				continue;
607099a2dd95SBruce Richardson 			if (representor_port < info->ranges[i].sf ||
607199a2dd95SBruce Richardson 			    representor_port >= info->ranges[i].sf + count)
607299a2dd95SBruce Richardson 				continue;
607399a2dd95SBruce Richardson 			*repr_id = info->ranges[i].id_base +
607499a2dd95SBruce Richardson 			      (representor_port - info->ranges[i].sf);
607599a2dd95SBruce Richardson 			ret = 0;
607699a2dd95SBruce Richardson 			goto out;
607799a2dd95SBruce Richardson 		default:
607899a2dd95SBruce Richardson 			break;
607999a2dd95SBruce Richardson 		}
608099a2dd95SBruce Richardson 	}
608199a2dd95SBruce Richardson out:
608299a2dd95SBruce Richardson 	free(info);
608399a2dd95SBruce Richardson 	return ret;
608499a2dd95SBruce Richardson }
608599a2dd95SBruce Richardson 
608699a2dd95SBruce Richardson static int
608799a2dd95SBruce Richardson eth_dev_handle_port_list(const char *cmd __rte_unused,
608899a2dd95SBruce Richardson 		const char *params __rte_unused,
608999a2dd95SBruce Richardson 		struct rte_tel_data *d)
609099a2dd95SBruce Richardson {
609199a2dd95SBruce Richardson 	int port_id;
609299a2dd95SBruce Richardson 
609399a2dd95SBruce Richardson 	rte_tel_data_start_array(d, RTE_TEL_INT_VAL);
609499a2dd95SBruce Richardson 	RTE_ETH_FOREACH_DEV(port_id)
609599a2dd95SBruce Richardson 		rte_tel_data_add_array_int(d, port_id);
609699a2dd95SBruce Richardson 	return 0;
609799a2dd95SBruce Richardson }
609899a2dd95SBruce Richardson 
609999a2dd95SBruce Richardson static void
610099a2dd95SBruce Richardson eth_dev_add_port_queue_stats(struct rte_tel_data *d, uint64_t *q_stats,
610199a2dd95SBruce Richardson 		const char *stat_name)
610299a2dd95SBruce Richardson {
610399a2dd95SBruce Richardson 	int q;
610499a2dd95SBruce Richardson 	struct rte_tel_data *q_data = rte_tel_data_alloc();
610599a2dd95SBruce Richardson 	rte_tel_data_start_array(q_data, RTE_TEL_U64_VAL);
610699a2dd95SBruce Richardson 	for (q = 0; q < RTE_ETHDEV_QUEUE_STAT_CNTRS; q++)
610799a2dd95SBruce Richardson 		rte_tel_data_add_array_u64(q_data, q_stats[q]);
610899a2dd95SBruce Richardson 	rte_tel_data_add_dict_container(d, stat_name, q_data, 0);
610999a2dd95SBruce Richardson }
611099a2dd95SBruce Richardson 
611199a2dd95SBruce Richardson #define ADD_DICT_STAT(stats, s) rte_tel_data_add_dict_u64(d, #s, stats.s)
611299a2dd95SBruce Richardson 
611399a2dd95SBruce Richardson static int
611499a2dd95SBruce Richardson eth_dev_handle_port_stats(const char *cmd __rte_unused,
611599a2dd95SBruce Richardson 		const char *params,
611699a2dd95SBruce Richardson 		struct rte_tel_data *d)
611799a2dd95SBruce Richardson {
611899a2dd95SBruce Richardson 	struct rte_eth_stats stats;
611999a2dd95SBruce Richardson 	int port_id, ret;
612099a2dd95SBruce Richardson 
612199a2dd95SBruce Richardson 	if (params == NULL || strlen(params) == 0 || !isdigit(*params))
612299a2dd95SBruce Richardson 		return -1;
612399a2dd95SBruce Richardson 
612499a2dd95SBruce Richardson 	port_id = atoi(params);
612599a2dd95SBruce Richardson 	if (!rte_eth_dev_is_valid_port(port_id))
612699a2dd95SBruce Richardson 		return -1;
612799a2dd95SBruce Richardson 
612899a2dd95SBruce Richardson 	ret = rte_eth_stats_get(port_id, &stats);
612999a2dd95SBruce Richardson 	if (ret < 0)
613099a2dd95SBruce Richardson 		return -1;
613199a2dd95SBruce Richardson 
613299a2dd95SBruce Richardson 	rte_tel_data_start_dict(d);
613399a2dd95SBruce Richardson 	ADD_DICT_STAT(stats, ipackets);
613499a2dd95SBruce Richardson 	ADD_DICT_STAT(stats, opackets);
613599a2dd95SBruce Richardson 	ADD_DICT_STAT(stats, ibytes);
613699a2dd95SBruce Richardson 	ADD_DICT_STAT(stats, obytes);
613799a2dd95SBruce Richardson 	ADD_DICT_STAT(stats, imissed);
613899a2dd95SBruce Richardson 	ADD_DICT_STAT(stats, ierrors);
613999a2dd95SBruce Richardson 	ADD_DICT_STAT(stats, oerrors);
614099a2dd95SBruce Richardson 	ADD_DICT_STAT(stats, rx_nombuf);
614199a2dd95SBruce Richardson 	eth_dev_add_port_queue_stats(d, stats.q_ipackets, "q_ipackets");
614299a2dd95SBruce Richardson 	eth_dev_add_port_queue_stats(d, stats.q_opackets, "q_opackets");
614399a2dd95SBruce Richardson 	eth_dev_add_port_queue_stats(d, stats.q_ibytes, "q_ibytes");
614499a2dd95SBruce Richardson 	eth_dev_add_port_queue_stats(d, stats.q_obytes, "q_obytes");
614599a2dd95SBruce Richardson 	eth_dev_add_port_queue_stats(d, stats.q_errors, "q_errors");
614699a2dd95SBruce Richardson 
614799a2dd95SBruce Richardson 	return 0;
614899a2dd95SBruce Richardson }
614999a2dd95SBruce Richardson 
615099a2dd95SBruce Richardson static int
615199a2dd95SBruce Richardson eth_dev_handle_port_xstats(const char *cmd __rte_unused,
615299a2dd95SBruce Richardson 		const char *params,
615399a2dd95SBruce Richardson 		struct rte_tel_data *d)
615499a2dd95SBruce Richardson {
615599a2dd95SBruce Richardson 	struct rte_eth_xstat *eth_xstats;
615699a2dd95SBruce Richardson 	struct rte_eth_xstat_name *xstat_names;
615799a2dd95SBruce Richardson 	int port_id, num_xstats;
615899a2dd95SBruce Richardson 	int i, ret;
615999a2dd95SBruce Richardson 	char *end_param;
616099a2dd95SBruce Richardson 
616199a2dd95SBruce Richardson 	if (params == NULL || strlen(params) == 0 || !isdigit(*params))
616299a2dd95SBruce Richardson 		return -1;
616399a2dd95SBruce Richardson 
616499a2dd95SBruce Richardson 	port_id = strtoul(params, &end_param, 0);
616599a2dd95SBruce Richardson 	if (*end_param != '\0')
616699a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(NOTICE,
616799a2dd95SBruce Richardson 			"Extra parameters passed to ethdev telemetry command, ignoring");
616899a2dd95SBruce Richardson 	if (!rte_eth_dev_is_valid_port(port_id))
616999a2dd95SBruce Richardson 		return -1;
617099a2dd95SBruce Richardson 
617199a2dd95SBruce Richardson 	num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
617299a2dd95SBruce Richardson 	if (num_xstats < 0)
617399a2dd95SBruce Richardson 		return -1;
617499a2dd95SBruce Richardson 
617599a2dd95SBruce Richardson 	/* use one malloc for both names and stats */
617699a2dd95SBruce Richardson 	eth_xstats = malloc((sizeof(struct rte_eth_xstat) +
617799a2dd95SBruce Richardson 			sizeof(struct rte_eth_xstat_name)) * num_xstats);
617899a2dd95SBruce Richardson 	if (eth_xstats == NULL)
617999a2dd95SBruce Richardson 		return -1;
618099a2dd95SBruce Richardson 	xstat_names = (void *)&eth_xstats[num_xstats];
618199a2dd95SBruce Richardson 
618299a2dd95SBruce Richardson 	ret = rte_eth_xstats_get_names(port_id, xstat_names, num_xstats);
618399a2dd95SBruce Richardson 	if (ret < 0 || ret > num_xstats) {
618499a2dd95SBruce Richardson 		free(eth_xstats);
618599a2dd95SBruce Richardson 		return -1;
618699a2dd95SBruce Richardson 	}
618799a2dd95SBruce Richardson 
618899a2dd95SBruce Richardson 	ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats);
618999a2dd95SBruce Richardson 	if (ret < 0 || ret > num_xstats) {
619099a2dd95SBruce Richardson 		free(eth_xstats);
619199a2dd95SBruce Richardson 		return -1;
619299a2dd95SBruce Richardson 	}
619399a2dd95SBruce Richardson 
619499a2dd95SBruce Richardson 	rte_tel_data_start_dict(d);
619599a2dd95SBruce Richardson 	for (i = 0; i < num_xstats; i++)
619699a2dd95SBruce Richardson 		rte_tel_data_add_dict_u64(d, xstat_names[i].name,
619799a2dd95SBruce Richardson 				eth_xstats[i].value);
619899a2dd95SBruce Richardson 	return 0;
619999a2dd95SBruce Richardson }
620099a2dd95SBruce Richardson 
620199a2dd95SBruce Richardson static int
620299a2dd95SBruce Richardson eth_dev_handle_port_link_status(const char *cmd __rte_unused,
620399a2dd95SBruce Richardson 		const char *params,
620499a2dd95SBruce Richardson 		struct rte_tel_data *d)
620599a2dd95SBruce Richardson {
620699a2dd95SBruce Richardson 	static const char *status_str = "status";
620799a2dd95SBruce Richardson 	int ret, port_id;
620899a2dd95SBruce Richardson 	struct rte_eth_link link;
620999a2dd95SBruce Richardson 	char *end_param;
621099a2dd95SBruce Richardson 
621199a2dd95SBruce Richardson 	if (params == NULL || strlen(params) == 0 || !isdigit(*params))
621299a2dd95SBruce Richardson 		return -1;
621399a2dd95SBruce Richardson 
621499a2dd95SBruce Richardson 	port_id = strtoul(params, &end_param, 0);
621599a2dd95SBruce Richardson 	if (*end_param != '\0')
621699a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(NOTICE,
621799a2dd95SBruce Richardson 			"Extra parameters passed to ethdev telemetry command, ignoring");
621899a2dd95SBruce Richardson 	if (!rte_eth_dev_is_valid_port(port_id))
621999a2dd95SBruce Richardson 		return -1;
622099a2dd95SBruce Richardson 
622199a2dd95SBruce Richardson 	ret = rte_eth_link_get_nowait(port_id, &link);
622299a2dd95SBruce Richardson 	if (ret < 0)
622399a2dd95SBruce Richardson 		return -1;
622499a2dd95SBruce Richardson 
622599a2dd95SBruce Richardson 	rte_tel_data_start_dict(d);
622699a2dd95SBruce Richardson 	if (!link.link_status) {
622799a2dd95SBruce Richardson 		rte_tel_data_add_dict_string(d, status_str, "DOWN");
622899a2dd95SBruce Richardson 		return 0;
622999a2dd95SBruce Richardson 	}
623099a2dd95SBruce Richardson 	rte_tel_data_add_dict_string(d, status_str, "UP");
623199a2dd95SBruce Richardson 	rte_tel_data_add_dict_u64(d, "speed", link.link_speed);
623299a2dd95SBruce Richardson 	rte_tel_data_add_dict_string(d, "duplex",
623399a2dd95SBruce Richardson 			(link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
623499a2dd95SBruce Richardson 				"full-duplex" : "half-duplex");
623599a2dd95SBruce Richardson 	return 0;
623699a2dd95SBruce Richardson }
623799a2dd95SBruce Richardson 
623858b43c1dSGowrishankar Muthukrishnan static int
623958b43c1dSGowrishankar Muthukrishnan eth_dev_handle_port_info(const char *cmd __rte_unused,
624058b43c1dSGowrishankar Muthukrishnan 		const char *params,
624158b43c1dSGowrishankar Muthukrishnan 		struct rte_tel_data *d)
624258b43c1dSGowrishankar Muthukrishnan {
624358b43c1dSGowrishankar Muthukrishnan 	struct rte_tel_data *rxq_state, *txq_state;
624458b43c1dSGowrishankar Muthukrishnan 	char mac_addr[RTE_ETHER_ADDR_LEN];
624558b43c1dSGowrishankar Muthukrishnan 	struct rte_eth_dev *eth_dev;
624658b43c1dSGowrishankar Muthukrishnan 	char *end_param;
624758b43c1dSGowrishankar Muthukrishnan 	int port_id, i;
624858b43c1dSGowrishankar Muthukrishnan 
624958b43c1dSGowrishankar Muthukrishnan 	if (params == NULL || strlen(params) == 0 || !isdigit(*params))
625058b43c1dSGowrishankar Muthukrishnan 		return -1;
625158b43c1dSGowrishankar Muthukrishnan 
625258b43c1dSGowrishankar Muthukrishnan 	port_id = strtoul(params, &end_param, 0);
625358b43c1dSGowrishankar Muthukrishnan 	if (*end_param != '\0')
625458b43c1dSGowrishankar Muthukrishnan 		RTE_ETHDEV_LOG(NOTICE,
625558b43c1dSGowrishankar Muthukrishnan 			"Extra parameters passed to ethdev telemetry command, ignoring");
625658b43c1dSGowrishankar Muthukrishnan 
625758b43c1dSGowrishankar Muthukrishnan 	if (!rte_eth_dev_is_valid_port(port_id))
625858b43c1dSGowrishankar Muthukrishnan 		return -EINVAL;
625958b43c1dSGowrishankar Muthukrishnan 
626058b43c1dSGowrishankar Muthukrishnan 	eth_dev = &rte_eth_devices[port_id];
626158b43c1dSGowrishankar Muthukrishnan 	if (!eth_dev)
626258b43c1dSGowrishankar Muthukrishnan 		return -EINVAL;
626358b43c1dSGowrishankar Muthukrishnan 
626458b43c1dSGowrishankar Muthukrishnan 	rxq_state = rte_tel_data_alloc();
626558b43c1dSGowrishankar Muthukrishnan 	if (!rxq_state)
626658b43c1dSGowrishankar Muthukrishnan 		return -ENOMEM;
626758b43c1dSGowrishankar Muthukrishnan 
626858b43c1dSGowrishankar Muthukrishnan 	txq_state = rte_tel_data_alloc();
626958b43c1dSGowrishankar Muthukrishnan 	if (!txq_state)
627058b43c1dSGowrishankar Muthukrishnan 		return -ENOMEM;
627158b43c1dSGowrishankar Muthukrishnan 
627258b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_start_dict(d);
627358b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_string(d, "name", eth_dev->data->name);
627458b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "state", eth_dev->state);
627558b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "nb_rx_queues",
627658b43c1dSGowrishankar Muthukrishnan 			eth_dev->data->nb_rx_queues);
627758b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "nb_tx_queues",
627858b43c1dSGowrishankar Muthukrishnan 			eth_dev->data->nb_tx_queues);
627958b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "port_id", eth_dev->data->port_id);
628058b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "mtu", eth_dev->data->mtu);
628158b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "rx_mbuf_size_min",
628258b43c1dSGowrishankar Muthukrishnan 			eth_dev->data->min_rx_buf_size);
628358b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "rx_mbuf_alloc_fail",
628458b43c1dSGowrishankar Muthukrishnan 			eth_dev->data->rx_mbuf_alloc_failed);
628558b43c1dSGowrishankar Muthukrishnan 	snprintf(mac_addr, RTE_ETHER_ADDR_LEN, "%02x:%02x:%02x:%02x:%02x:%02x",
628658b43c1dSGowrishankar Muthukrishnan 			 eth_dev->data->mac_addrs->addr_bytes[0],
628758b43c1dSGowrishankar Muthukrishnan 			 eth_dev->data->mac_addrs->addr_bytes[1],
628858b43c1dSGowrishankar Muthukrishnan 			 eth_dev->data->mac_addrs->addr_bytes[2],
628958b43c1dSGowrishankar Muthukrishnan 			 eth_dev->data->mac_addrs->addr_bytes[3],
629058b43c1dSGowrishankar Muthukrishnan 			 eth_dev->data->mac_addrs->addr_bytes[4],
629158b43c1dSGowrishankar Muthukrishnan 			 eth_dev->data->mac_addrs->addr_bytes[5]);
629258b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_string(d, "mac_addr", mac_addr);
629358b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "promiscuous",
629458b43c1dSGowrishankar Muthukrishnan 			eth_dev->data->promiscuous);
629558b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "scattered_rx",
629658b43c1dSGowrishankar Muthukrishnan 			eth_dev->data->scattered_rx);
629758b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "all_multicast",
629858b43c1dSGowrishankar Muthukrishnan 			eth_dev->data->all_multicast);
629958b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "dev_started", eth_dev->data->dev_started);
630058b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "lro", eth_dev->data->lro);
630158b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "dev_configured",
630258b43c1dSGowrishankar Muthukrishnan 			eth_dev->data->dev_configured);
630358b43c1dSGowrishankar Muthukrishnan 
630458b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_start_array(rxq_state, RTE_TEL_INT_VAL);
630558b43c1dSGowrishankar Muthukrishnan 	for (i = 0; i < eth_dev->data->nb_rx_queues; i++)
630658b43c1dSGowrishankar Muthukrishnan 		rte_tel_data_add_array_int(rxq_state,
630758b43c1dSGowrishankar Muthukrishnan 				eth_dev->data->rx_queue_state[i]);
630858b43c1dSGowrishankar Muthukrishnan 
630958b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_start_array(txq_state, RTE_TEL_INT_VAL);
631058b43c1dSGowrishankar Muthukrishnan 	for (i = 0; i < eth_dev->data->nb_tx_queues; i++)
631158b43c1dSGowrishankar Muthukrishnan 		rte_tel_data_add_array_int(txq_state,
631258b43c1dSGowrishankar Muthukrishnan 				eth_dev->data->tx_queue_state[i]);
631358b43c1dSGowrishankar Muthukrishnan 
631458b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_container(d, "rxq_state", rxq_state, 0);
631558b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_container(d, "txq_state", txq_state, 0);
631658b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "numa_node", eth_dev->data->numa_node);
631758b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "dev_flags", eth_dev->data->dev_flags);
631858b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "rx_offloads",
631958b43c1dSGowrishankar Muthukrishnan 			eth_dev->data->dev_conf.rxmode.offloads);
632058b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "tx_offloads",
632158b43c1dSGowrishankar Muthukrishnan 			eth_dev->data->dev_conf.txmode.offloads);
632258b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "ethdev_rss_hf",
632358b43c1dSGowrishankar Muthukrishnan 			eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf);
632458b43c1dSGowrishankar Muthukrishnan 
632558b43c1dSGowrishankar Muthukrishnan 	return 0;
632658b43c1dSGowrishankar Muthukrishnan }
632758b43c1dSGowrishankar Muthukrishnan 
632899a2dd95SBruce Richardson int
632999a2dd95SBruce Richardson rte_eth_hairpin_queue_peer_update(uint16_t peer_port, uint16_t peer_queue,
633099a2dd95SBruce Richardson 				  struct rte_hairpin_peer_info *cur_info,
633199a2dd95SBruce Richardson 				  struct rte_hairpin_peer_info *peer_info,
633299a2dd95SBruce Richardson 				  uint32_t direction)
633399a2dd95SBruce Richardson {
633499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
633599a2dd95SBruce Richardson 
633699a2dd95SBruce Richardson 	/* Current queue information is not mandatory. */
633799a2dd95SBruce Richardson 	if (peer_info == NULL)
633899a2dd95SBruce Richardson 		return -EINVAL;
633999a2dd95SBruce Richardson 
634099a2dd95SBruce Richardson 	/* No need to check the validity again. */
634199a2dd95SBruce Richardson 	dev = &rte_eth_devices[peer_port];
634299a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->hairpin_queue_peer_update,
634399a2dd95SBruce Richardson 				-ENOTSUP);
634499a2dd95SBruce Richardson 
634599a2dd95SBruce Richardson 	return (*dev->dev_ops->hairpin_queue_peer_update)(dev, peer_queue,
634699a2dd95SBruce Richardson 					cur_info, peer_info, direction);
634799a2dd95SBruce Richardson }
634899a2dd95SBruce Richardson 
634999a2dd95SBruce Richardson int
635099a2dd95SBruce Richardson rte_eth_hairpin_queue_peer_bind(uint16_t cur_port, uint16_t cur_queue,
635199a2dd95SBruce Richardson 				struct rte_hairpin_peer_info *peer_info,
635299a2dd95SBruce Richardson 				uint32_t direction)
635399a2dd95SBruce Richardson {
635499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
635599a2dd95SBruce Richardson 
635699a2dd95SBruce Richardson 	if (peer_info == NULL)
635799a2dd95SBruce Richardson 		return -EINVAL;
635899a2dd95SBruce Richardson 
635999a2dd95SBruce Richardson 	/* No need to check the validity again. */
636099a2dd95SBruce Richardson 	dev = &rte_eth_devices[cur_port];
636199a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->hairpin_queue_peer_bind,
636299a2dd95SBruce Richardson 				-ENOTSUP);
636399a2dd95SBruce Richardson 
636499a2dd95SBruce Richardson 	return (*dev->dev_ops->hairpin_queue_peer_bind)(dev, cur_queue,
636599a2dd95SBruce Richardson 							peer_info, direction);
636699a2dd95SBruce Richardson }
636799a2dd95SBruce Richardson 
636899a2dd95SBruce Richardson int
636999a2dd95SBruce Richardson rte_eth_hairpin_queue_peer_unbind(uint16_t cur_port, uint16_t cur_queue,
637099a2dd95SBruce Richardson 				  uint32_t direction)
637199a2dd95SBruce Richardson {
637299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
637399a2dd95SBruce Richardson 
637499a2dd95SBruce Richardson 	/* No need to check the validity again. */
637599a2dd95SBruce Richardson 	dev = &rte_eth_devices[cur_port];
637699a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->hairpin_queue_peer_unbind,
637799a2dd95SBruce Richardson 				-ENOTSUP);
637899a2dd95SBruce Richardson 
637999a2dd95SBruce Richardson 	return (*dev->dev_ops->hairpin_queue_peer_unbind)(dev, cur_queue,
638099a2dd95SBruce Richardson 							  direction);
638199a2dd95SBruce Richardson }
638299a2dd95SBruce Richardson 
638399a2dd95SBruce Richardson int
638499a2dd95SBruce Richardson rte_eth_representor_info_get(uint16_t port_id,
638599a2dd95SBruce Richardson 			     struct rte_eth_representor_info *info)
638699a2dd95SBruce Richardson {
638799a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
638899a2dd95SBruce Richardson 
638999a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
639099a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
639199a2dd95SBruce Richardson 
639299a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->representor_info_get, -ENOTSUP);
639353ef1b34SMin Hu (Connor) 	return eth_err(port_id, (*dev->dev_ops->representor_info_get)(dev, info));
639499a2dd95SBruce Richardson }
639599a2dd95SBruce Richardson 
6396f6d8a6d3SIvan Malov int
6397f6d8a6d3SIvan Malov rte_eth_rx_metadata_negotiate(uint16_t port_id, uint64_t *features)
6398f6d8a6d3SIvan Malov {
6399f6d8a6d3SIvan Malov 	struct rte_eth_dev *dev;
6400f6d8a6d3SIvan Malov 
6401f6d8a6d3SIvan Malov 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
6402f6d8a6d3SIvan Malov 	dev = &rte_eth_devices[port_id];
6403f6d8a6d3SIvan Malov 
6404f6d8a6d3SIvan Malov 	if (dev->data->dev_configured != 0) {
6405f6d8a6d3SIvan Malov 		RTE_ETHDEV_LOG(ERR,
6406f6d8a6d3SIvan Malov 			"The port (id=%"PRIu16") is already configured\n",
6407f6d8a6d3SIvan Malov 			port_id);
6408f6d8a6d3SIvan Malov 		return -EBUSY;
6409f6d8a6d3SIvan Malov 	}
6410f6d8a6d3SIvan Malov 
6411f6d8a6d3SIvan Malov 	if (features == NULL) {
6412f6d8a6d3SIvan Malov 		RTE_ETHDEV_LOG(ERR, "Invalid features (NULL)\n");
6413f6d8a6d3SIvan Malov 		return -EINVAL;
6414f6d8a6d3SIvan Malov 	}
6415f6d8a6d3SIvan Malov 
6416f6d8a6d3SIvan Malov 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_metadata_negotiate, -ENOTSUP);
6417f6d8a6d3SIvan Malov 	return eth_err(port_id,
6418f6d8a6d3SIvan Malov 		       (*dev->dev_ops->rx_metadata_negotiate)(dev, features));
6419f6d8a6d3SIvan Malov }
6420f6d8a6d3SIvan Malov 
6421eeded204SDavid Marchand RTE_LOG_REGISTER_DEFAULT(rte_eth_dev_logtype, INFO);
642299a2dd95SBruce Richardson 
642399a2dd95SBruce Richardson RTE_INIT(ethdev_init_telemetry)
642499a2dd95SBruce Richardson {
642599a2dd95SBruce Richardson 	rte_telemetry_register_cmd("/ethdev/list", eth_dev_handle_port_list,
642699a2dd95SBruce Richardson 			"Returns list of available ethdev ports. Takes no parameters");
642799a2dd95SBruce Richardson 	rte_telemetry_register_cmd("/ethdev/stats", eth_dev_handle_port_stats,
642899a2dd95SBruce Richardson 			"Returns the common stats for a port. Parameters: int port_id");
642999a2dd95SBruce Richardson 	rte_telemetry_register_cmd("/ethdev/xstats", eth_dev_handle_port_xstats,
643099a2dd95SBruce Richardson 			"Returns the extended stats for a port. Parameters: int port_id");
643199a2dd95SBruce Richardson 	rte_telemetry_register_cmd("/ethdev/link_status",
643299a2dd95SBruce Richardson 			eth_dev_handle_port_link_status,
643399a2dd95SBruce Richardson 			"Returns the link status for a port. Parameters: int port_id");
643458b43c1dSGowrishankar Muthukrishnan 	rte_telemetry_register_cmd("/ethdev/info", eth_dev_handle_port_info,
643558b43c1dSGowrishankar Muthukrishnan 			"Returns the device info for a port. Parameters: int port_id");
643699a2dd95SBruce Richardson }
6437