xref: /dpdk/lib/ethdev/rte_ethdev.c (revision b7ade5d3)
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 
5309fd4227SAndrew Rybchenko /* spinlock for add/remove Rx callbacks */
5499a2dd95SBruce Richardson static rte_spinlock_t eth_dev_rx_cb_lock = RTE_SPINLOCK_INITIALIZER;
5599a2dd95SBruce Richardson 
5609fd4227SAndrew Rybchenko /* 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 	{ RTE_ETH_RX_OFFLOAD_##_name, #_name }
10599a2dd95SBruce Richardson 
10699a2dd95SBruce Richardson static const struct {
10799a2dd95SBruce Richardson 	uint64_t offload;
10899a2dd95SBruce Richardson 	const char *name;
10999a2dd95SBruce Richardson } eth_dev_rx_offload_names[] = {
11099a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(VLAN_STRIP),
11199a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(IPV4_CKSUM),
11299a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(UDP_CKSUM),
11399a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(TCP_CKSUM),
11499a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(TCP_LRO),
11599a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(QINQ_STRIP),
11699a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(OUTER_IPV4_CKSUM),
11799a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(MACSEC_STRIP),
11899a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(HEADER_SPLIT),
11999a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(VLAN_FILTER),
12099a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(VLAN_EXTEND),
12199a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(SCATTER),
12299a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(TIMESTAMP),
12399a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(SECURITY),
12499a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(KEEP_CRC),
12599a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(SCTP_CKSUM),
12699a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(OUTER_UDP_CKSUM),
12799a2dd95SBruce Richardson 	RTE_RX_OFFLOAD_BIT2STR(RSS_HASH),
128295968d1SFerruh Yigit 	RTE_RX_OFFLOAD_BIT2STR(BUFFER_SPLIT),
12999a2dd95SBruce Richardson };
13099a2dd95SBruce Richardson 
13199a2dd95SBruce Richardson #undef RTE_RX_OFFLOAD_BIT2STR
13299a2dd95SBruce Richardson #undef RTE_ETH_RX_OFFLOAD_BIT2STR
13399a2dd95SBruce Richardson 
13499a2dd95SBruce Richardson #define RTE_TX_OFFLOAD_BIT2STR(_name)	\
135295968d1SFerruh Yigit 	{ RTE_ETH_TX_OFFLOAD_##_name, #_name }
13699a2dd95SBruce Richardson 
13799a2dd95SBruce Richardson static const struct {
13899a2dd95SBruce Richardson 	uint64_t offload;
13999a2dd95SBruce Richardson 	const char *name;
14099a2dd95SBruce Richardson } eth_dev_tx_offload_names[] = {
14199a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(VLAN_INSERT),
14299a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(IPV4_CKSUM),
14399a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(UDP_CKSUM),
14499a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(TCP_CKSUM),
14599a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(SCTP_CKSUM),
14699a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(TCP_TSO),
14799a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(UDP_TSO),
14899a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(OUTER_IPV4_CKSUM),
14999a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(QINQ_INSERT),
15099a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(VXLAN_TNL_TSO),
15199a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(GRE_TNL_TSO),
15299a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(IPIP_TNL_TSO),
15399a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(GENEVE_TNL_TSO),
15499a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(MACSEC_INSERT),
15599a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(MT_LOCKFREE),
15699a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(MULTI_SEGS),
15799a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(MBUF_FAST_FREE),
15899a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(SECURITY),
15999a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(UDP_TNL_TSO),
16099a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(IP_TNL_TSO),
16199a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(OUTER_UDP_CKSUM),
16299a2dd95SBruce Richardson 	RTE_TX_OFFLOAD_BIT2STR(SEND_ON_TIMESTAMP),
16399a2dd95SBruce Richardson };
16499a2dd95SBruce Richardson 
16599a2dd95SBruce Richardson #undef RTE_TX_OFFLOAD_BIT2STR
16699a2dd95SBruce Richardson 
16793e441c9SXueming Li static const struct {
16893e441c9SXueming Li 	uint64_t offload;
16993e441c9SXueming Li 	const char *name;
17093e441c9SXueming Li } rte_eth_dev_capa_names[] = {
17193e441c9SXueming Li 	{RTE_ETH_DEV_CAPA_RUNTIME_RX_QUEUE_SETUP, "RUNTIME_RX_QUEUE_SETUP"},
17293e441c9SXueming Li 	{RTE_ETH_DEV_CAPA_RUNTIME_TX_QUEUE_SETUP, "RUNTIME_TX_QUEUE_SETUP"},
17393e441c9SXueming Li 	{RTE_ETH_DEV_CAPA_RXQ_SHARE, "RXQ_SHARE"},
17493e441c9SXueming Li };
17593e441c9SXueming Li 
17699a2dd95SBruce Richardson /**
17799a2dd95SBruce Richardson  * The user application callback description.
17899a2dd95SBruce Richardson  *
17999a2dd95SBruce Richardson  * It contains callback address to be registered by user application,
18099a2dd95SBruce Richardson  * the pointer to the parameters for callback, and the event type.
18199a2dd95SBruce Richardson  */
18299a2dd95SBruce Richardson struct rte_eth_dev_callback {
18399a2dd95SBruce Richardson 	TAILQ_ENTRY(rte_eth_dev_callback) next; /**< Callbacks list */
18499a2dd95SBruce Richardson 	rte_eth_dev_cb_fn cb_fn;                /**< Callback address */
18599a2dd95SBruce Richardson 	void *cb_arg;                           /**< Parameter for callback */
18699a2dd95SBruce Richardson 	void *ret_param;                        /**< Return parameter */
18799a2dd95SBruce Richardson 	enum rte_eth_event_type event;          /**< Interrupt event type */
18899a2dd95SBruce Richardson 	uint32_t active;                        /**< Callback is executing */
18999a2dd95SBruce Richardson };
19099a2dd95SBruce Richardson 
19199a2dd95SBruce Richardson enum {
19299a2dd95SBruce Richardson 	STAT_QMAP_TX = 0,
19399a2dd95SBruce Richardson 	STAT_QMAP_RX
19499a2dd95SBruce Richardson };
19599a2dd95SBruce Richardson 
19699a2dd95SBruce Richardson int
19799a2dd95SBruce Richardson rte_eth_iterator_init(struct rte_dev_iterator *iter, const char *devargs_str)
19899a2dd95SBruce Richardson {
19999a2dd95SBruce Richardson 	int ret;
20099a2dd95SBruce Richardson 	struct rte_devargs devargs;
20199a2dd95SBruce Richardson 	const char *bus_param_key;
20299a2dd95SBruce Richardson 	char *bus_str = NULL;
20399a2dd95SBruce Richardson 	char *cls_str = NULL;
20499a2dd95SBruce Richardson 	int str_size;
20599a2dd95SBruce Richardson 
20653ef1b34SMin Hu (Connor) 	if (iter == NULL) {
20753ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot initialize NULL iterator\n");
20853ef1b34SMin Hu (Connor) 		return -EINVAL;
20953ef1b34SMin Hu (Connor) 	}
21053ef1b34SMin Hu (Connor) 
21153ef1b34SMin Hu (Connor) 	if (devargs_str == NULL) {
21253ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
21353ef1b34SMin Hu (Connor) 			"Cannot initialize iterator from NULL device description string\n");
21453ef1b34SMin Hu (Connor) 		return -EINVAL;
21553ef1b34SMin Hu (Connor) 	}
21653ef1b34SMin Hu (Connor) 
21799a2dd95SBruce Richardson 	memset(iter, 0, sizeof(*iter));
21899a2dd95SBruce Richardson 	memset(&devargs, 0, sizeof(devargs));
21999a2dd95SBruce Richardson 
22099a2dd95SBruce Richardson 	/*
22199a2dd95SBruce Richardson 	 * The devargs string may use various syntaxes:
22299a2dd95SBruce Richardson 	 *   - 0000:08:00.0,representor=[1-3]
22399a2dd95SBruce Richardson 	 *   - pci:0000:06:00.0,representor=[0,5]
22499a2dd95SBruce Richardson 	 *   - class=eth,mac=00:11:22:33:44:55
22599a2dd95SBruce Richardson 	 *   - bus=X,paramX=x/class=Y,paramY=y/driver=Z,paramZ=z
22699a2dd95SBruce Richardson 	 */
22799a2dd95SBruce Richardson 
22899a2dd95SBruce Richardson 	/*
22999a2dd95SBruce Richardson 	 * Handle pure class filter (i.e. without any bus-level argument),
23099a2dd95SBruce Richardson 	 * from future new syntax.
23199a2dd95SBruce Richardson 	 * rte_devargs_parse() is not yet supporting the new syntax,
23299a2dd95SBruce Richardson 	 * that's why this simple case is temporarily parsed here.
23399a2dd95SBruce Richardson 	 */
23499a2dd95SBruce Richardson #define iter_anybus_str "class=eth,"
23599a2dd95SBruce Richardson 	if (strncmp(devargs_str, iter_anybus_str,
23699a2dd95SBruce Richardson 			strlen(iter_anybus_str)) == 0) {
23799a2dd95SBruce Richardson 		iter->cls_str = devargs_str + strlen(iter_anybus_str);
23899a2dd95SBruce Richardson 		goto end;
23999a2dd95SBruce Richardson 	}
24099a2dd95SBruce Richardson 
24199a2dd95SBruce Richardson 	/* Split bus, device and parameters. */
24299a2dd95SBruce Richardson 	ret = rte_devargs_parse(&devargs, devargs_str);
24399a2dd95SBruce Richardson 	if (ret != 0)
24499a2dd95SBruce Richardson 		goto error;
24599a2dd95SBruce Richardson 
24699a2dd95SBruce Richardson 	/*
24799a2dd95SBruce Richardson 	 * Assume parameters of old syntax can match only at ethdev level.
24899a2dd95SBruce Richardson 	 * Extra parameters will be ignored, thanks to "+" prefix.
24999a2dd95SBruce Richardson 	 */
25099a2dd95SBruce Richardson 	str_size = strlen(devargs.args) + 2;
25199a2dd95SBruce Richardson 	cls_str = malloc(str_size);
25299a2dd95SBruce Richardson 	if (cls_str == NULL) {
25399a2dd95SBruce Richardson 		ret = -ENOMEM;
25499a2dd95SBruce Richardson 		goto error;
25599a2dd95SBruce Richardson 	}
25699a2dd95SBruce Richardson 	ret = snprintf(cls_str, str_size, "+%s", devargs.args);
25799a2dd95SBruce Richardson 	if (ret != str_size - 1) {
25899a2dd95SBruce Richardson 		ret = -EINVAL;
25999a2dd95SBruce Richardson 		goto error;
26099a2dd95SBruce Richardson 	}
26199a2dd95SBruce Richardson 	iter->cls_str = cls_str;
26299a2dd95SBruce Richardson 
26399a2dd95SBruce Richardson 	iter->bus = devargs.bus;
26499a2dd95SBruce Richardson 	if (iter->bus->dev_iterate == NULL) {
26599a2dd95SBruce Richardson 		ret = -ENOTSUP;
26699a2dd95SBruce Richardson 		goto error;
26799a2dd95SBruce Richardson 	}
26899a2dd95SBruce Richardson 
26999a2dd95SBruce Richardson 	/* Convert bus args to new syntax for use with new API dev_iterate. */
270a956adb2SHemant Agrawal 	if ((strcmp(iter->bus->name, "vdev") == 0) ||
271a956adb2SHemant Agrawal 		(strcmp(iter->bus->name, "fslmc") == 0) ||
272a956adb2SHemant Agrawal 		(strcmp(iter->bus->name, "dpaa_bus") == 0)) {
27399a2dd95SBruce Richardson 		bus_param_key = "name";
27499a2dd95SBruce Richardson 	} else if (strcmp(iter->bus->name, "pci") == 0) {
27599a2dd95SBruce Richardson 		bus_param_key = "addr";
27699a2dd95SBruce Richardson 	} else {
27799a2dd95SBruce Richardson 		ret = -ENOTSUP;
27899a2dd95SBruce Richardson 		goto error;
27999a2dd95SBruce Richardson 	}
28099a2dd95SBruce Richardson 	str_size = strlen(bus_param_key) + strlen(devargs.name) + 2;
28199a2dd95SBruce Richardson 	bus_str = malloc(str_size);
28299a2dd95SBruce Richardson 	if (bus_str == NULL) {
28399a2dd95SBruce Richardson 		ret = -ENOMEM;
28499a2dd95SBruce Richardson 		goto error;
28599a2dd95SBruce Richardson 	}
28699a2dd95SBruce Richardson 	ret = snprintf(bus_str, str_size, "%s=%s",
28799a2dd95SBruce Richardson 			bus_param_key, devargs.name);
28899a2dd95SBruce Richardson 	if (ret != str_size - 1) {
28999a2dd95SBruce Richardson 		ret = -EINVAL;
29099a2dd95SBruce Richardson 		goto error;
29199a2dd95SBruce Richardson 	}
29299a2dd95SBruce Richardson 	iter->bus_str = bus_str;
29399a2dd95SBruce Richardson 
29499a2dd95SBruce Richardson end:
29599a2dd95SBruce Richardson 	iter->cls = rte_class_find_by_name("eth");
29699a2dd95SBruce Richardson 	rte_devargs_reset(&devargs);
29799a2dd95SBruce Richardson 	return 0;
29899a2dd95SBruce Richardson 
29999a2dd95SBruce Richardson error:
30099a2dd95SBruce Richardson 	if (ret == -ENOTSUP)
30199a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Bus %s does not support iterating.\n",
30299a2dd95SBruce Richardson 				iter->bus->name);
30399a2dd95SBruce Richardson 	rte_devargs_reset(&devargs);
30499a2dd95SBruce Richardson 	free(bus_str);
30599a2dd95SBruce Richardson 	free(cls_str);
30699a2dd95SBruce Richardson 	return ret;
30799a2dd95SBruce Richardson }
30899a2dd95SBruce Richardson 
30999a2dd95SBruce Richardson uint16_t
31099a2dd95SBruce Richardson rte_eth_iterator_next(struct rte_dev_iterator *iter)
31199a2dd95SBruce Richardson {
31253ef1b34SMin Hu (Connor) 	if (iter == NULL) {
31353ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
31453ef1b34SMin Hu (Connor) 			"Cannot get next device from NULL iterator\n");
31553ef1b34SMin Hu (Connor) 		return RTE_MAX_ETHPORTS;
31653ef1b34SMin Hu (Connor) 	}
31753ef1b34SMin Hu (Connor) 
31899a2dd95SBruce Richardson 	if (iter->cls == NULL) /* invalid ethdev iterator */
31999a2dd95SBruce Richardson 		return RTE_MAX_ETHPORTS;
32099a2dd95SBruce Richardson 
32199a2dd95SBruce Richardson 	do { /* loop to try all matching rte_device */
32299a2dd95SBruce Richardson 		/* If not pure ethdev filter and */
32399a2dd95SBruce Richardson 		if (iter->bus != NULL &&
32499a2dd95SBruce Richardson 				/* not in middle of rte_eth_dev iteration, */
32599a2dd95SBruce Richardson 				iter->class_device == NULL) {
32699a2dd95SBruce Richardson 			/* get next rte_device to try. */
32799a2dd95SBruce Richardson 			iter->device = iter->bus->dev_iterate(
32899a2dd95SBruce Richardson 					iter->device, iter->bus_str, iter);
32999a2dd95SBruce Richardson 			if (iter->device == NULL)
33099a2dd95SBruce Richardson 				break; /* no more rte_device candidate */
33199a2dd95SBruce Richardson 		}
33299a2dd95SBruce Richardson 		/* A device is matching bus part, need to check ethdev part. */
33399a2dd95SBruce Richardson 		iter->class_device = iter->cls->dev_iterate(
33499a2dd95SBruce Richardson 				iter->class_device, iter->cls_str, iter);
33599a2dd95SBruce Richardson 		if (iter->class_device != NULL)
33699a2dd95SBruce Richardson 			return eth_dev_to_id(iter->class_device); /* match */
33799a2dd95SBruce Richardson 	} while (iter->bus != NULL); /* need to try next rte_device */
33899a2dd95SBruce Richardson 
33999a2dd95SBruce Richardson 	/* No more ethdev port to iterate. */
34099a2dd95SBruce Richardson 	rte_eth_iterator_cleanup(iter);
34199a2dd95SBruce Richardson 	return RTE_MAX_ETHPORTS;
34299a2dd95SBruce Richardson }
34399a2dd95SBruce Richardson 
34499a2dd95SBruce Richardson void
34599a2dd95SBruce Richardson rte_eth_iterator_cleanup(struct rte_dev_iterator *iter)
34699a2dd95SBruce Richardson {
34753ef1b34SMin Hu (Connor) 	if (iter == NULL) {
34853ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot do clean up from NULL iterator\n");
34953ef1b34SMin Hu (Connor) 		return;
35053ef1b34SMin Hu (Connor) 	}
35153ef1b34SMin Hu (Connor) 
35299a2dd95SBruce Richardson 	if (iter->bus_str == NULL)
35399a2dd95SBruce Richardson 		return; /* nothing to free in pure class filter */
35499a2dd95SBruce Richardson 	free(RTE_CAST_FIELD(iter, bus_str, char *)); /* workaround const */
35599a2dd95SBruce Richardson 	free(RTE_CAST_FIELD(iter, cls_str, char *)); /* workaround const */
35699a2dd95SBruce Richardson 	memset(iter, 0, sizeof(*iter));
35799a2dd95SBruce Richardson }
35899a2dd95SBruce Richardson 
35999a2dd95SBruce Richardson uint16_t
36099a2dd95SBruce Richardson rte_eth_find_next(uint16_t port_id)
36199a2dd95SBruce Richardson {
36299a2dd95SBruce Richardson 	while (port_id < RTE_MAX_ETHPORTS &&
36399a2dd95SBruce Richardson 			rte_eth_devices[port_id].state == RTE_ETH_DEV_UNUSED)
36499a2dd95SBruce Richardson 		port_id++;
36599a2dd95SBruce Richardson 
36699a2dd95SBruce Richardson 	if (port_id >= RTE_MAX_ETHPORTS)
36799a2dd95SBruce Richardson 		return RTE_MAX_ETHPORTS;
36899a2dd95SBruce Richardson 
36999a2dd95SBruce Richardson 	return port_id;
37099a2dd95SBruce Richardson }
37199a2dd95SBruce Richardson 
37299a2dd95SBruce Richardson /*
37399a2dd95SBruce Richardson  * Macro to iterate over all valid ports for internal usage.
37499a2dd95SBruce Richardson  * Note: RTE_ETH_FOREACH_DEV is different because filtering owned ports.
37599a2dd95SBruce Richardson  */
37699a2dd95SBruce Richardson #define RTE_ETH_FOREACH_VALID_DEV(port_id) \
37799a2dd95SBruce Richardson 	for (port_id = rte_eth_find_next(0); \
37899a2dd95SBruce Richardson 	     port_id < RTE_MAX_ETHPORTS; \
37999a2dd95SBruce Richardson 	     port_id = rte_eth_find_next(port_id + 1))
38099a2dd95SBruce Richardson 
38199a2dd95SBruce Richardson uint16_t
38299a2dd95SBruce Richardson rte_eth_find_next_of(uint16_t port_id, const struct rte_device *parent)
38399a2dd95SBruce Richardson {
38499a2dd95SBruce Richardson 	port_id = rte_eth_find_next(port_id);
38599a2dd95SBruce Richardson 	while (port_id < RTE_MAX_ETHPORTS &&
38699a2dd95SBruce Richardson 			rte_eth_devices[port_id].device != parent)
38799a2dd95SBruce Richardson 		port_id = rte_eth_find_next(port_id + 1);
38899a2dd95SBruce Richardson 
38999a2dd95SBruce Richardson 	return port_id;
39099a2dd95SBruce Richardson }
39199a2dd95SBruce Richardson 
39299a2dd95SBruce Richardson uint16_t
39399a2dd95SBruce Richardson rte_eth_find_next_sibling(uint16_t port_id, uint16_t ref_port_id)
39499a2dd95SBruce Richardson {
39599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(ref_port_id, RTE_MAX_ETHPORTS);
39699a2dd95SBruce Richardson 	return rte_eth_find_next_of(port_id,
39799a2dd95SBruce Richardson 			rte_eth_devices[ref_port_id].device);
39899a2dd95SBruce Richardson }
39999a2dd95SBruce Richardson 
40099a2dd95SBruce Richardson static void
40199a2dd95SBruce Richardson eth_dev_shared_data_prepare(void)
40299a2dd95SBruce Richardson {
40399a2dd95SBruce Richardson 	const unsigned flags = 0;
40499a2dd95SBruce Richardson 	const struct rte_memzone *mz;
40599a2dd95SBruce Richardson 
40699a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_shared_data_lock);
40799a2dd95SBruce Richardson 
40899a2dd95SBruce Richardson 	if (eth_dev_shared_data == NULL) {
40999a2dd95SBruce Richardson 		if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
41099a2dd95SBruce Richardson 			/* Allocate port data and ownership shared memory. */
41199a2dd95SBruce Richardson 			mz = rte_memzone_reserve(MZ_RTE_ETH_DEV_DATA,
41299a2dd95SBruce Richardson 					sizeof(*eth_dev_shared_data),
41399a2dd95SBruce Richardson 					rte_socket_id(), flags);
41499a2dd95SBruce Richardson 		} else
41599a2dd95SBruce Richardson 			mz = rte_memzone_lookup(MZ_RTE_ETH_DEV_DATA);
41699a2dd95SBruce Richardson 		if (mz == NULL)
41799a2dd95SBruce Richardson 			rte_panic("Cannot allocate ethdev shared data\n");
41899a2dd95SBruce Richardson 
41999a2dd95SBruce Richardson 		eth_dev_shared_data = mz->addr;
42099a2dd95SBruce Richardson 		if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
42199a2dd95SBruce Richardson 			eth_dev_shared_data->next_owner_id =
42299a2dd95SBruce Richardson 					RTE_ETH_DEV_NO_OWNER + 1;
42399a2dd95SBruce Richardson 			rte_spinlock_init(&eth_dev_shared_data->ownership_lock);
42499a2dd95SBruce Richardson 			memset(eth_dev_shared_data->data, 0,
42599a2dd95SBruce Richardson 			       sizeof(eth_dev_shared_data->data));
42699a2dd95SBruce Richardson 		}
42799a2dd95SBruce Richardson 	}
42899a2dd95SBruce Richardson 
42999a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_shared_data_lock);
43099a2dd95SBruce Richardson }
43199a2dd95SBruce Richardson 
43299a2dd95SBruce Richardson static bool
43399a2dd95SBruce Richardson eth_dev_is_allocated(const struct rte_eth_dev *ethdev)
43499a2dd95SBruce Richardson {
43599a2dd95SBruce Richardson 	return ethdev->data->name[0] != '\0';
43699a2dd95SBruce Richardson }
43799a2dd95SBruce Richardson 
43899a2dd95SBruce Richardson static struct rte_eth_dev *
43999a2dd95SBruce Richardson eth_dev_allocated(const char *name)
44099a2dd95SBruce Richardson {
44199a2dd95SBruce Richardson 	uint16_t i;
44299a2dd95SBruce Richardson 
44399a2dd95SBruce Richardson 	RTE_BUILD_BUG_ON(RTE_MAX_ETHPORTS >= UINT16_MAX);
44499a2dd95SBruce Richardson 
44599a2dd95SBruce Richardson 	for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
44699a2dd95SBruce Richardson 		if (rte_eth_devices[i].data != NULL &&
44799a2dd95SBruce Richardson 		    strcmp(rte_eth_devices[i].data->name, name) == 0)
44899a2dd95SBruce Richardson 			return &rte_eth_devices[i];
44999a2dd95SBruce Richardson 	}
45099a2dd95SBruce Richardson 	return NULL;
45199a2dd95SBruce Richardson }
45299a2dd95SBruce Richardson 
45399a2dd95SBruce Richardson struct rte_eth_dev *
45499a2dd95SBruce Richardson rte_eth_dev_allocated(const char *name)
45599a2dd95SBruce Richardson {
45699a2dd95SBruce Richardson 	struct rte_eth_dev *ethdev;
45799a2dd95SBruce Richardson 
45899a2dd95SBruce Richardson 	eth_dev_shared_data_prepare();
45999a2dd95SBruce Richardson 
46099a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_shared_data->ownership_lock);
46199a2dd95SBruce Richardson 
46299a2dd95SBruce Richardson 	ethdev = eth_dev_allocated(name);
46399a2dd95SBruce Richardson 
46499a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_shared_data->ownership_lock);
46599a2dd95SBruce Richardson 
46699a2dd95SBruce Richardson 	return ethdev;
46799a2dd95SBruce Richardson }
46899a2dd95SBruce Richardson 
46999a2dd95SBruce Richardson static uint16_t
47099a2dd95SBruce Richardson eth_dev_find_free_port(void)
47199a2dd95SBruce Richardson {
47299a2dd95SBruce Richardson 	uint16_t i;
47399a2dd95SBruce Richardson 
47499a2dd95SBruce Richardson 	for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
47599a2dd95SBruce Richardson 		/* Using shared name field to find a free port. */
47699a2dd95SBruce Richardson 		if (eth_dev_shared_data->data[i].name[0] == '\0') {
47799a2dd95SBruce Richardson 			RTE_ASSERT(rte_eth_devices[i].state ==
47899a2dd95SBruce Richardson 				   RTE_ETH_DEV_UNUSED);
47999a2dd95SBruce Richardson 			return i;
48099a2dd95SBruce Richardson 		}
48199a2dd95SBruce Richardson 	}
48299a2dd95SBruce Richardson 	return RTE_MAX_ETHPORTS;
48399a2dd95SBruce Richardson }
48499a2dd95SBruce Richardson 
48599a2dd95SBruce Richardson static struct rte_eth_dev *
48699a2dd95SBruce Richardson eth_dev_get(uint16_t port_id)
48799a2dd95SBruce Richardson {
48899a2dd95SBruce Richardson 	struct rte_eth_dev *eth_dev = &rte_eth_devices[port_id];
48999a2dd95SBruce Richardson 
49099a2dd95SBruce Richardson 	eth_dev->data = &eth_dev_shared_data->data[port_id];
49199a2dd95SBruce Richardson 
49299a2dd95SBruce Richardson 	return eth_dev;
49399a2dd95SBruce Richardson }
49499a2dd95SBruce Richardson 
49599a2dd95SBruce Richardson struct rte_eth_dev *
49699a2dd95SBruce Richardson rte_eth_dev_allocate(const char *name)
49799a2dd95SBruce Richardson {
49899a2dd95SBruce Richardson 	uint16_t port_id;
49999a2dd95SBruce Richardson 	struct rte_eth_dev *eth_dev = NULL;
50099a2dd95SBruce Richardson 	size_t name_len;
50199a2dd95SBruce Richardson 
50299a2dd95SBruce Richardson 	name_len = strnlen(name, RTE_ETH_NAME_MAX_LEN);
50399a2dd95SBruce Richardson 	if (name_len == 0) {
50499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Zero length Ethernet device name\n");
50599a2dd95SBruce Richardson 		return NULL;
50699a2dd95SBruce Richardson 	}
50799a2dd95SBruce Richardson 
50899a2dd95SBruce Richardson 	if (name_len >= RTE_ETH_NAME_MAX_LEN) {
50999a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Ethernet device name is too long\n");
51099a2dd95SBruce Richardson 		return NULL;
51199a2dd95SBruce Richardson 	}
51299a2dd95SBruce Richardson 
51399a2dd95SBruce Richardson 	eth_dev_shared_data_prepare();
51499a2dd95SBruce Richardson 
51599a2dd95SBruce Richardson 	/* Synchronize port creation between primary and secondary threads. */
51699a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_shared_data->ownership_lock);
51799a2dd95SBruce Richardson 
51899a2dd95SBruce Richardson 	if (eth_dev_allocated(name) != NULL) {
51999a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
52099a2dd95SBruce Richardson 			"Ethernet device with name %s already allocated\n",
52199a2dd95SBruce Richardson 			name);
52299a2dd95SBruce Richardson 		goto unlock;
52399a2dd95SBruce Richardson 	}
52499a2dd95SBruce Richardson 
52599a2dd95SBruce Richardson 	port_id = eth_dev_find_free_port();
52699a2dd95SBruce Richardson 	if (port_id == RTE_MAX_ETHPORTS) {
52799a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
52899a2dd95SBruce Richardson 			"Reached maximum number of Ethernet ports\n");
52999a2dd95SBruce Richardson 		goto unlock;
53099a2dd95SBruce Richardson 	}
53199a2dd95SBruce Richardson 
53299a2dd95SBruce Richardson 	eth_dev = eth_dev_get(port_id);
53399a2dd95SBruce Richardson 	strlcpy(eth_dev->data->name, name, sizeof(eth_dev->data->name));
53499a2dd95SBruce Richardson 	eth_dev->data->port_id = port_id;
535ff4e52efSViacheslav Galaktionov 	eth_dev->data->backer_port_id = RTE_MAX_ETHPORTS;
53699a2dd95SBruce Richardson 	eth_dev->data->mtu = RTE_ETHER_MTU;
53799a2dd95SBruce Richardson 	pthread_mutex_init(&eth_dev->data->flow_ops_mutex, NULL);
53899a2dd95SBruce Richardson 
53999a2dd95SBruce Richardson unlock:
54099a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_shared_data->ownership_lock);
54199a2dd95SBruce Richardson 
54299a2dd95SBruce Richardson 	return eth_dev;
54399a2dd95SBruce Richardson }
54499a2dd95SBruce Richardson 
54599a2dd95SBruce Richardson /*
54699a2dd95SBruce Richardson  * Attach to a port already registered by the primary process, which
5475906be5aSAndrew Rybchenko  * makes sure that the same device would have the same port ID both
54899a2dd95SBruce Richardson  * in the primary and secondary process.
54999a2dd95SBruce Richardson  */
55099a2dd95SBruce Richardson struct rte_eth_dev *
55199a2dd95SBruce Richardson rte_eth_dev_attach_secondary(const char *name)
55299a2dd95SBruce Richardson {
55399a2dd95SBruce Richardson 	uint16_t i;
55499a2dd95SBruce Richardson 	struct rte_eth_dev *eth_dev = NULL;
55599a2dd95SBruce Richardson 
55699a2dd95SBruce Richardson 	eth_dev_shared_data_prepare();
55799a2dd95SBruce Richardson 
55899a2dd95SBruce Richardson 	/* Synchronize port attachment to primary port creation and release. */
55999a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_shared_data->ownership_lock);
56099a2dd95SBruce Richardson 
56199a2dd95SBruce Richardson 	for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
56299a2dd95SBruce Richardson 		if (strcmp(eth_dev_shared_data->data[i].name, name) == 0)
56399a2dd95SBruce Richardson 			break;
56499a2dd95SBruce Richardson 	}
56599a2dd95SBruce Richardson 	if (i == RTE_MAX_ETHPORTS) {
56699a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
56799a2dd95SBruce Richardson 			"Device %s is not driven by the primary process\n",
56899a2dd95SBruce Richardson 			name);
56999a2dd95SBruce Richardson 	} else {
57099a2dd95SBruce Richardson 		eth_dev = eth_dev_get(i);
57199a2dd95SBruce Richardson 		RTE_ASSERT(eth_dev->data->port_id == i);
57299a2dd95SBruce Richardson 	}
57399a2dd95SBruce Richardson 
57499a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_shared_data->ownership_lock);
57599a2dd95SBruce Richardson 	return eth_dev;
57699a2dd95SBruce Richardson }
57799a2dd95SBruce Richardson 
57899a2dd95SBruce Richardson int
57999a2dd95SBruce Richardson rte_eth_dev_release_port(struct rte_eth_dev *eth_dev)
58099a2dd95SBruce Richardson {
58199a2dd95SBruce Richardson 	if (eth_dev == NULL)
58299a2dd95SBruce Richardson 		return -EINVAL;
58399a2dd95SBruce Richardson 
58499a2dd95SBruce Richardson 	eth_dev_shared_data_prepare();
58599a2dd95SBruce Richardson 
58699a2dd95SBruce Richardson 	if (eth_dev->state != RTE_ETH_DEV_UNUSED)
58799a2dd95SBruce Richardson 		rte_eth_dev_callback_process(eth_dev,
58899a2dd95SBruce Richardson 				RTE_ETH_EVENT_DESTROY, NULL);
58999a2dd95SBruce Richardson 
590c87d435aSKonstantin Ananyev 	eth_dev_fp_ops_reset(rte_eth_fp_ops + eth_dev->data->port_id);
591c87d435aSKonstantin Ananyev 
59299a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_shared_data->ownership_lock);
59399a2dd95SBruce Richardson 
59499a2dd95SBruce Richardson 	eth_dev->state = RTE_ETH_DEV_UNUSED;
59599a2dd95SBruce Richardson 	eth_dev->device = NULL;
59699a2dd95SBruce Richardson 	eth_dev->process_private = NULL;
59799a2dd95SBruce Richardson 	eth_dev->intr_handle = NULL;
59899a2dd95SBruce Richardson 	eth_dev->rx_pkt_burst = NULL;
59999a2dd95SBruce Richardson 	eth_dev->tx_pkt_burst = NULL;
60099a2dd95SBruce Richardson 	eth_dev->tx_pkt_prepare = NULL;
60199a2dd95SBruce Richardson 	eth_dev->rx_queue_count = NULL;
60299a2dd95SBruce Richardson 	eth_dev->rx_descriptor_status = NULL;
60399a2dd95SBruce Richardson 	eth_dev->tx_descriptor_status = NULL;
60499a2dd95SBruce Richardson 	eth_dev->dev_ops = NULL;
60599a2dd95SBruce Richardson 
60699a2dd95SBruce Richardson 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
60799a2dd95SBruce Richardson 		rte_free(eth_dev->data->rx_queues);
60899a2dd95SBruce Richardson 		rte_free(eth_dev->data->tx_queues);
60999a2dd95SBruce Richardson 		rte_free(eth_dev->data->mac_addrs);
61099a2dd95SBruce Richardson 		rte_free(eth_dev->data->hash_mac_addrs);
61199a2dd95SBruce Richardson 		rte_free(eth_dev->data->dev_private);
61299a2dd95SBruce Richardson 		pthread_mutex_destroy(&eth_dev->data->flow_ops_mutex);
61399a2dd95SBruce Richardson 		memset(eth_dev->data, 0, sizeof(struct rte_eth_dev_data));
61499a2dd95SBruce Richardson 	}
61599a2dd95SBruce Richardson 
61699a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_shared_data->ownership_lock);
61799a2dd95SBruce Richardson 
61899a2dd95SBruce Richardson 	return 0;
61999a2dd95SBruce Richardson }
62099a2dd95SBruce Richardson 
62199a2dd95SBruce Richardson int
62299a2dd95SBruce Richardson rte_eth_dev_is_valid_port(uint16_t port_id)
62399a2dd95SBruce Richardson {
62499a2dd95SBruce Richardson 	if (port_id >= RTE_MAX_ETHPORTS ||
62599a2dd95SBruce Richardson 	    (rte_eth_devices[port_id].state == RTE_ETH_DEV_UNUSED))
62699a2dd95SBruce Richardson 		return 0;
62799a2dd95SBruce Richardson 	else
62899a2dd95SBruce Richardson 		return 1;
62999a2dd95SBruce Richardson }
63099a2dd95SBruce Richardson 
63199a2dd95SBruce Richardson static int
63299a2dd95SBruce Richardson eth_is_valid_owner_id(uint64_t owner_id)
63399a2dd95SBruce Richardson {
63499a2dd95SBruce Richardson 	if (owner_id == RTE_ETH_DEV_NO_OWNER ||
63599a2dd95SBruce Richardson 	    eth_dev_shared_data->next_owner_id <= owner_id)
63699a2dd95SBruce Richardson 		return 0;
63799a2dd95SBruce Richardson 	return 1;
63899a2dd95SBruce Richardson }
63999a2dd95SBruce Richardson 
64099a2dd95SBruce Richardson uint64_t
64199a2dd95SBruce Richardson rte_eth_find_next_owned_by(uint16_t port_id, const uint64_t owner_id)
64299a2dd95SBruce Richardson {
64399a2dd95SBruce Richardson 	port_id = rte_eth_find_next(port_id);
64499a2dd95SBruce Richardson 	while (port_id < RTE_MAX_ETHPORTS &&
64599a2dd95SBruce Richardson 			rte_eth_devices[port_id].data->owner.id != owner_id)
64699a2dd95SBruce Richardson 		port_id = rte_eth_find_next(port_id + 1);
64799a2dd95SBruce Richardson 
64899a2dd95SBruce Richardson 	return port_id;
64999a2dd95SBruce Richardson }
65099a2dd95SBruce Richardson 
65199a2dd95SBruce Richardson int
65299a2dd95SBruce Richardson rte_eth_dev_owner_new(uint64_t *owner_id)
65399a2dd95SBruce Richardson {
65453ef1b34SMin Hu (Connor) 	if (owner_id == NULL) {
65553ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot get new owner ID to NULL\n");
65653ef1b34SMin Hu (Connor) 		return -EINVAL;
65753ef1b34SMin Hu (Connor) 	}
65853ef1b34SMin Hu (Connor) 
65999a2dd95SBruce Richardson 	eth_dev_shared_data_prepare();
66099a2dd95SBruce Richardson 
66199a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_shared_data->ownership_lock);
66299a2dd95SBruce Richardson 
66399a2dd95SBruce Richardson 	*owner_id = eth_dev_shared_data->next_owner_id++;
66499a2dd95SBruce Richardson 
66599a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_shared_data->ownership_lock);
66699a2dd95SBruce Richardson 	return 0;
66799a2dd95SBruce Richardson }
66899a2dd95SBruce Richardson 
66999a2dd95SBruce Richardson static int
67099a2dd95SBruce Richardson eth_dev_owner_set(const uint16_t port_id, const uint64_t old_owner_id,
67199a2dd95SBruce Richardson 		       const struct rte_eth_dev_owner *new_owner)
67299a2dd95SBruce Richardson {
67399a2dd95SBruce Richardson 	struct rte_eth_dev *ethdev = &rte_eth_devices[port_id];
67499a2dd95SBruce Richardson 	struct rte_eth_dev_owner *port_owner;
67599a2dd95SBruce Richardson 
67699a2dd95SBruce Richardson 	if (port_id >= RTE_MAX_ETHPORTS || !eth_dev_is_allocated(ethdev)) {
6775906be5aSAndrew Rybchenko 		RTE_ETHDEV_LOG(ERR, "Port ID %"PRIu16" is not allocated\n",
67899a2dd95SBruce Richardson 			port_id);
67999a2dd95SBruce Richardson 		return -ENODEV;
68099a2dd95SBruce Richardson 	}
68199a2dd95SBruce Richardson 
68253ef1b34SMin Hu (Connor) 	if (new_owner == NULL) {
68353ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
68453ef1b34SMin Hu (Connor) 			"Cannot set ethdev port %u owner from NULL owner\n",
68553ef1b34SMin Hu (Connor) 			port_id);
68653ef1b34SMin Hu (Connor) 		return -EINVAL;
68753ef1b34SMin Hu (Connor) 	}
68853ef1b34SMin Hu (Connor) 
68999a2dd95SBruce Richardson 	if (!eth_is_valid_owner_id(new_owner->id) &&
69099a2dd95SBruce Richardson 	    !eth_is_valid_owner_id(old_owner_id)) {
69199a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
69299a2dd95SBruce Richardson 			"Invalid owner old_id=%016"PRIx64" new_id=%016"PRIx64"\n",
69399a2dd95SBruce Richardson 		       old_owner_id, new_owner->id);
69499a2dd95SBruce Richardson 		return -EINVAL;
69599a2dd95SBruce Richardson 	}
69699a2dd95SBruce Richardson 
69799a2dd95SBruce Richardson 	port_owner = &rte_eth_devices[port_id].data->owner;
69899a2dd95SBruce Richardson 	if (port_owner->id != old_owner_id) {
69999a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
70099a2dd95SBruce Richardson 			"Cannot set owner to port %u already owned by %s_%016"PRIX64"\n",
70199a2dd95SBruce Richardson 			port_id, port_owner->name, port_owner->id);
70299a2dd95SBruce Richardson 		return -EPERM;
70399a2dd95SBruce Richardson 	}
70499a2dd95SBruce Richardson 
70599a2dd95SBruce Richardson 	/* can not truncate (same structure) */
70699a2dd95SBruce Richardson 	strlcpy(port_owner->name, new_owner->name, RTE_ETH_MAX_OWNER_NAME_LEN);
70799a2dd95SBruce Richardson 
70899a2dd95SBruce Richardson 	port_owner->id = new_owner->id;
70999a2dd95SBruce Richardson 
71099a2dd95SBruce Richardson 	RTE_ETHDEV_LOG(DEBUG, "Port %u owner is %s_%016"PRIx64"\n",
71199a2dd95SBruce Richardson 		port_id, new_owner->name, new_owner->id);
71299a2dd95SBruce Richardson 
71399a2dd95SBruce Richardson 	return 0;
71499a2dd95SBruce Richardson }
71599a2dd95SBruce Richardson 
71699a2dd95SBruce Richardson int
71799a2dd95SBruce Richardson rte_eth_dev_owner_set(const uint16_t port_id,
71899a2dd95SBruce Richardson 		      const struct rte_eth_dev_owner *owner)
71999a2dd95SBruce Richardson {
72099a2dd95SBruce Richardson 	int ret;
72199a2dd95SBruce Richardson 
72299a2dd95SBruce Richardson 	eth_dev_shared_data_prepare();
72399a2dd95SBruce Richardson 
72499a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_shared_data->ownership_lock);
72599a2dd95SBruce Richardson 
72699a2dd95SBruce Richardson 	ret = eth_dev_owner_set(port_id, RTE_ETH_DEV_NO_OWNER, owner);
72799a2dd95SBruce Richardson 
72899a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_shared_data->ownership_lock);
72999a2dd95SBruce Richardson 	return ret;
73099a2dd95SBruce Richardson }
73199a2dd95SBruce Richardson 
73299a2dd95SBruce Richardson int
73399a2dd95SBruce Richardson rte_eth_dev_owner_unset(const uint16_t port_id, const uint64_t owner_id)
73499a2dd95SBruce Richardson {
73599a2dd95SBruce Richardson 	const struct rte_eth_dev_owner new_owner = (struct rte_eth_dev_owner)
73699a2dd95SBruce Richardson 			{.id = RTE_ETH_DEV_NO_OWNER, .name = ""};
73799a2dd95SBruce Richardson 	int ret;
73899a2dd95SBruce Richardson 
73999a2dd95SBruce Richardson 	eth_dev_shared_data_prepare();
74099a2dd95SBruce Richardson 
74199a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_shared_data->ownership_lock);
74299a2dd95SBruce Richardson 
74399a2dd95SBruce Richardson 	ret = eth_dev_owner_set(port_id, owner_id, &new_owner);
74499a2dd95SBruce Richardson 
74599a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_shared_data->ownership_lock);
74699a2dd95SBruce Richardson 	return ret;
74799a2dd95SBruce Richardson }
74899a2dd95SBruce Richardson 
74999a2dd95SBruce Richardson int
75099a2dd95SBruce Richardson rte_eth_dev_owner_delete(const uint64_t owner_id)
75199a2dd95SBruce Richardson {
75299a2dd95SBruce Richardson 	uint16_t port_id;
75399a2dd95SBruce Richardson 	int ret = 0;
75499a2dd95SBruce Richardson 
75599a2dd95SBruce Richardson 	eth_dev_shared_data_prepare();
75699a2dd95SBruce Richardson 
75799a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_shared_data->ownership_lock);
75899a2dd95SBruce Richardson 
75999a2dd95SBruce Richardson 	if (eth_is_valid_owner_id(owner_id)) {
760*b7ade5d3SFerruh Yigit 		for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
761*b7ade5d3SFerruh Yigit 			struct rte_eth_dev_data *data =
762*b7ade5d3SFerruh Yigit 				rte_eth_devices[port_id].data;
763*b7ade5d3SFerruh Yigit 			if (data != NULL && data->owner.id == owner_id)
764*b7ade5d3SFerruh Yigit 				memset(&data->owner, 0,
76599a2dd95SBruce Richardson 				       sizeof(struct rte_eth_dev_owner));
766*b7ade5d3SFerruh Yigit 		}
76799a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(NOTICE,
76899a2dd95SBruce Richardson 			"All port owners owned by %016"PRIx64" identifier have removed\n",
76999a2dd95SBruce Richardson 			owner_id);
77099a2dd95SBruce Richardson 	} else {
77199a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
7725906be5aSAndrew Rybchenko 			       "Invalid owner ID=%016"PRIx64"\n",
77399a2dd95SBruce Richardson 			       owner_id);
77499a2dd95SBruce Richardson 		ret = -EINVAL;
77599a2dd95SBruce Richardson 	}
77699a2dd95SBruce Richardson 
77799a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_shared_data->ownership_lock);
77899a2dd95SBruce Richardson 
77999a2dd95SBruce Richardson 	return ret;
78099a2dd95SBruce Richardson }
78199a2dd95SBruce Richardson 
78299a2dd95SBruce Richardson int
78399a2dd95SBruce Richardson rte_eth_dev_owner_get(const uint16_t port_id, struct rte_eth_dev_owner *owner)
78499a2dd95SBruce Richardson {
78553ef1b34SMin Hu (Connor) 	struct rte_eth_dev *ethdev;
78653ef1b34SMin Hu (Connor) 
78753ef1b34SMin Hu (Connor) 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
78853ef1b34SMin Hu (Connor) 	ethdev = &rte_eth_devices[port_id];
78953ef1b34SMin Hu (Connor) 
79053ef1b34SMin Hu (Connor) 	if (!eth_dev_is_allocated(ethdev)) {
7915906be5aSAndrew Rybchenko 		RTE_ETHDEV_LOG(ERR, "Port ID %"PRIu16" is not allocated\n",
79253ef1b34SMin Hu (Connor) 			port_id);
79353ef1b34SMin Hu (Connor) 		return -ENODEV;
79453ef1b34SMin Hu (Connor) 	}
79553ef1b34SMin Hu (Connor) 
79653ef1b34SMin Hu (Connor) 	if (owner == NULL) {
79753ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot get ethdev port %u owner to NULL\n",
79853ef1b34SMin Hu (Connor) 			port_id);
79953ef1b34SMin Hu (Connor) 		return -EINVAL;
80053ef1b34SMin Hu (Connor) 	}
80199a2dd95SBruce Richardson 
80299a2dd95SBruce Richardson 	eth_dev_shared_data_prepare();
80399a2dd95SBruce Richardson 
80499a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_shared_data->ownership_lock);
80599a2dd95SBruce Richardson 	rte_memcpy(owner, &ethdev->data->owner, sizeof(*owner));
80699a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_shared_data->ownership_lock);
80753ef1b34SMin Hu (Connor) 
80853ef1b34SMin Hu (Connor) 	return 0;
80999a2dd95SBruce Richardson }
81099a2dd95SBruce Richardson 
81199a2dd95SBruce Richardson int
81299a2dd95SBruce Richardson rte_eth_dev_socket_id(uint16_t port_id)
81399a2dd95SBruce Richardson {
81499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -1);
81599a2dd95SBruce Richardson 	return rte_eth_devices[port_id].data->numa_node;
81699a2dd95SBruce Richardson }
81799a2dd95SBruce Richardson 
81899a2dd95SBruce Richardson void *
81999a2dd95SBruce Richardson rte_eth_dev_get_sec_ctx(uint16_t port_id)
82099a2dd95SBruce Richardson {
82199a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, NULL);
82299a2dd95SBruce Richardson 	return rte_eth_devices[port_id].security_ctx;
82399a2dd95SBruce Richardson }
82499a2dd95SBruce Richardson 
82599a2dd95SBruce Richardson uint16_t
82699a2dd95SBruce Richardson rte_eth_dev_count_avail(void)
82799a2dd95SBruce Richardson {
82899a2dd95SBruce Richardson 	uint16_t p;
82999a2dd95SBruce Richardson 	uint16_t count;
83099a2dd95SBruce Richardson 
83199a2dd95SBruce Richardson 	count = 0;
83299a2dd95SBruce Richardson 
83399a2dd95SBruce Richardson 	RTE_ETH_FOREACH_DEV(p)
83499a2dd95SBruce Richardson 		count++;
83599a2dd95SBruce Richardson 
83699a2dd95SBruce Richardson 	return count;
83799a2dd95SBruce Richardson }
83899a2dd95SBruce Richardson 
83999a2dd95SBruce Richardson uint16_t
84099a2dd95SBruce Richardson rte_eth_dev_count_total(void)
84199a2dd95SBruce Richardson {
84299a2dd95SBruce Richardson 	uint16_t port, count = 0;
84399a2dd95SBruce Richardson 
84499a2dd95SBruce Richardson 	RTE_ETH_FOREACH_VALID_DEV(port)
84599a2dd95SBruce Richardson 		count++;
84699a2dd95SBruce Richardson 
84799a2dd95SBruce Richardson 	return count;
84899a2dd95SBruce Richardson }
84999a2dd95SBruce Richardson 
85099a2dd95SBruce Richardson int
85199a2dd95SBruce Richardson rte_eth_dev_get_name_by_port(uint16_t port_id, char *name)
85299a2dd95SBruce Richardson {
85399a2dd95SBruce Richardson 	char *tmp;
85499a2dd95SBruce Richardson 
85599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
85699a2dd95SBruce Richardson 
85799a2dd95SBruce Richardson 	if (name == NULL) {
85853ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot get ethdev port %u name to NULL\n",
85953ef1b34SMin Hu (Connor) 			port_id);
86099a2dd95SBruce Richardson 		return -EINVAL;
86199a2dd95SBruce Richardson 	}
86299a2dd95SBruce Richardson 
86399a2dd95SBruce Richardson 	/* shouldn't check 'rte_eth_devices[i].data',
86499a2dd95SBruce Richardson 	 * because it might be overwritten by VDEV PMD */
86599a2dd95SBruce Richardson 	tmp = eth_dev_shared_data->data[port_id].name;
86699a2dd95SBruce Richardson 	strcpy(name, tmp);
86799a2dd95SBruce Richardson 	return 0;
86899a2dd95SBruce Richardson }
86999a2dd95SBruce Richardson 
87099a2dd95SBruce Richardson int
87199a2dd95SBruce Richardson rte_eth_dev_get_port_by_name(const char *name, uint16_t *port_id)
87299a2dd95SBruce Richardson {
87399a2dd95SBruce Richardson 	uint16_t pid;
87499a2dd95SBruce Richardson 
87599a2dd95SBruce Richardson 	if (name == NULL) {
87653ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot get port ID from NULL name");
87753ef1b34SMin Hu (Connor) 		return -EINVAL;
87853ef1b34SMin Hu (Connor) 	}
87953ef1b34SMin Hu (Connor) 
88053ef1b34SMin Hu (Connor) 	if (port_id == NULL) {
88153ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
88253ef1b34SMin Hu (Connor) 			"Cannot get port ID to NULL for %s\n", name);
88399a2dd95SBruce Richardson 		return -EINVAL;
88499a2dd95SBruce Richardson 	}
88599a2dd95SBruce Richardson 
88699a2dd95SBruce Richardson 	RTE_ETH_FOREACH_VALID_DEV(pid)
88799a2dd95SBruce Richardson 		if (!strcmp(name, eth_dev_shared_data->data[pid].name)) {
88899a2dd95SBruce Richardson 			*port_id = pid;
88999a2dd95SBruce Richardson 			return 0;
89099a2dd95SBruce Richardson 		}
89199a2dd95SBruce Richardson 
89299a2dd95SBruce Richardson 	return -ENODEV;
89399a2dd95SBruce Richardson }
89499a2dd95SBruce Richardson 
89599a2dd95SBruce Richardson static int
89699a2dd95SBruce Richardson eth_err(uint16_t port_id, int ret)
89799a2dd95SBruce Richardson {
89899a2dd95SBruce Richardson 	if (ret == 0)
89999a2dd95SBruce Richardson 		return 0;
90099a2dd95SBruce Richardson 	if (rte_eth_dev_is_removed(port_id))
90199a2dd95SBruce Richardson 		return -EIO;
90299a2dd95SBruce Richardson 	return ret;
90399a2dd95SBruce Richardson }
90499a2dd95SBruce Richardson 
90549ed3224SXueming Li static void
90649ed3224SXueming Li eth_dev_rxq_release(struct rte_eth_dev *dev, uint16_t qid)
90749ed3224SXueming Li {
90849ed3224SXueming Li 	void **rxq = dev->data->rx_queues;
90949ed3224SXueming Li 
91049ed3224SXueming Li 	if (rxq[qid] == NULL)
91149ed3224SXueming Li 		return;
91249ed3224SXueming Li 
91349ed3224SXueming Li 	if (dev->dev_ops->rx_queue_release != NULL)
9147483341aSXueming Li 		(*dev->dev_ops->rx_queue_release)(dev, qid);
91549ed3224SXueming Li 	rxq[qid] = NULL;
91649ed3224SXueming Li }
91749ed3224SXueming Li 
91849ed3224SXueming Li static void
91949ed3224SXueming Li eth_dev_txq_release(struct rte_eth_dev *dev, uint16_t qid)
92049ed3224SXueming Li {
92149ed3224SXueming Li 	void **txq = dev->data->tx_queues;
92249ed3224SXueming Li 
92349ed3224SXueming Li 	if (txq[qid] == NULL)
92449ed3224SXueming Li 		return;
92549ed3224SXueming Li 
92649ed3224SXueming Li 	if (dev->dev_ops->tx_queue_release != NULL)
9277483341aSXueming Li 		(*dev->dev_ops->tx_queue_release)(dev, qid);
92849ed3224SXueming Li 	txq[qid] = NULL;
92949ed3224SXueming Li }
93049ed3224SXueming Li 
93199a2dd95SBruce Richardson static int
93299a2dd95SBruce Richardson eth_dev_rx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
93399a2dd95SBruce Richardson {
93499a2dd95SBruce Richardson 	uint16_t old_nb_queues = dev->data->nb_rx_queues;
93599a2dd95SBruce Richardson 	unsigned i;
93699a2dd95SBruce Richardson 
93799a2dd95SBruce Richardson 	if (dev->data->rx_queues == NULL && nb_queues != 0) { /* first time configuration */
93899a2dd95SBruce Richardson 		dev->data->rx_queues = rte_zmalloc("ethdev->rx_queues",
939c024496aSKonstantin Ananyev 				sizeof(dev->data->rx_queues[0]) *
940c024496aSKonstantin Ananyev 				RTE_MAX_QUEUES_PER_PORT,
94199a2dd95SBruce Richardson 				RTE_CACHE_LINE_SIZE);
94299a2dd95SBruce Richardson 		if (dev->data->rx_queues == NULL) {
94399a2dd95SBruce Richardson 			dev->data->nb_rx_queues = 0;
94499a2dd95SBruce Richardson 			return -(ENOMEM);
94599a2dd95SBruce Richardson 		}
94699a2dd95SBruce Richardson 	} else if (dev->data->rx_queues != NULL && nb_queues != 0) { /* re-configure */
94749ed3224SXueming Li 		for (i = nb_queues; i < old_nb_queues; i++)
94849ed3224SXueming Li 			eth_dev_rxq_release(dev, i);
94999a2dd95SBruce Richardson 
95099a2dd95SBruce Richardson 	} else if (dev->data->rx_queues != NULL && nb_queues == 0) {
95199a2dd95SBruce Richardson 		for (i = nb_queues; i < old_nb_queues; i++)
95249ed3224SXueming Li 			eth_dev_rxq_release(dev, i);
95399a2dd95SBruce Richardson 
95499a2dd95SBruce Richardson 		rte_free(dev->data->rx_queues);
95599a2dd95SBruce Richardson 		dev->data->rx_queues = NULL;
95699a2dd95SBruce Richardson 	}
95799a2dd95SBruce Richardson 	dev->data->nb_rx_queues = nb_queues;
95899a2dd95SBruce Richardson 	return 0;
95999a2dd95SBruce Richardson }
96099a2dd95SBruce Richardson 
96199a2dd95SBruce Richardson static int
96299a2dd95SBruce Richardson eth_dev_validate_rx_queue(const struct rte_eth_dev *dev, uint16_t rx_queue_id)
96399a2dd95SBruce Richardson {
96499a2dd95SBruce Richardson 	uint16_t port_id;
96599a2dd95SBruce Richardson 
96699a2dd95SBruce Richardson 	if (rx_queue_id >= dev->data->nb_rx_queues) {
96799a2dd95SBruce Richardson 		port_id = dev->data->port_id;
96899a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
96999a2dd95SBruce Richardson 			       "Invalid Rx queue_id=%u of device with port_id=%u\n",
97099a2dd95SBruce Richardson 			       rx_queue_id, port_id);
97199a2dd95SBruce Richardson 		return -EINVAL;
97299a2dd95SBruce Richardson 	}
97399a2dd95SBruce Richardson 
97499a2dd95SBruce Richardson 	if (dev->data->rx_queues[rx_queue_id] == NULL) {
97599a2dd95SBruce Richardson 		port_id = dev->data->port_id;
97699a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
97799a2dd95SBruce Richardson 			       "Queue %u of device with port_id=%u has not been setup\n",
97899a2dd95SBruce Richardson 			       rx_queue_id, port_id);
97999a2dd95SBruce Richardson 		return -EINVAL;
98099a2dd95SBruce Richardson 	}
98199a2dd95SBruce Richardson 
98299a2dd95SBruce Richardson 	return 0;
98399a2dd95SBruce Richardson }
98499a2dd95SBruce Richardson 
98599a2dd95SBruce Richardson static int
98699a2dd95SBruce Richardson eth_dev_validate_tx_queue(const struct rte_eth_dev *dev, uint16_t tx_queue_id)
98799a2dd95SBruce Richardson {
98899a2dd95SBruce Richardson 	uint16_t port_id;
98999a2dd95SBruce Richardson 
99099a2dd95SBruce Richardson 	if (tx_queue_id >= dev->data->nb_tx_queues) {
99199a2dd95SBruce Richardson 		port_id = dev->data->port_id;
99299a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
99399a2dd95SBruce Richardson 			       "Invalid Tx queue_id=%u of device with port_id=%u\n",
99499a2dd95SBruce Richardson 			       tx_queue_id, port_id);
99599a2dd95SBruce Richardson 		return -EINVAL;
99699a2dd95SBruce Richardson 	}
99799a2dd95SBruce Richardson 
99899a2dd95SBruce Richardson 	if (dev->data->tx_queues[tx_queue_id] == NULL) {
99999a2dd95SBruce Richardson 		port_id = dev->data->port_id;
100099a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
100199a2dd95SBruce Richardson 			       "Queue %u of device with port_id=%u has not been setup\n",
100299a2dd95SBruce Richardson 			       tx_queue_id, port_id);
100399a2dd95SBruce Richardson 		return -EINVAL;
100499a2dd95SBruce Richardson 	}
100599a2dd95SBruce Richardson 
100699a2dd95SBruce Richardson 	return 0;
100799a2dd95SBruce Richardson }
100899a2dd95SBruce Richardson 
100999a2dd95SBruce Richardson int
101099a2dd95SBruce Richardson rte_eth_dev_rx_queue_start(uint16_t port_id, uint16_t rx_queue_id)
101199a2dd95SBruce Richardson {
101299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
101399a2dd95SBruce Richardson 	int ret;
101499a2dd95SBruce Richardson 
101599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
101699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
101753ef1b34SMin Hu (Connor) 
101899a2dd95SBruce Richardson 	if (!dev->data->dev_started) {
101999a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
102099a2dd95SBruce Richardson 			"Port %u must be started before start any queue\n",
102199a2dd95SBruce Richardson 			port_id);
102299a2dd95SBruce Richardson 		return -EINVAL;
102399a2dd95SBruce Richardson 	}
102499a2dd95SBruce Richardson 
102599a2dd95SBruce Richardson 	ret = eth_dev_validate_rx_queue(dev, rx_queue_id);
102699a2dd95SBruce Richardson 	if (ret != 0)
102799a2dd95SBruce Richardson 		return ret;
102899a2dd95SBruce Richardson 
102999a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_start, -ENOTSUP);
103099a2dd95SBruce Richardson 
103199a2dd95SBruce Richardson 	if (rte_eth_dev_is_rx_hairpin_queue(dev, rx_queue_id)) {
103299a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(INFO,
103399a2dd95SBruce Richardson 			"Can't start Rx hairpin queue %"PRIu16" of device with port_id=%"PRIu16"\n",
103499a2dd95SBruce Richardson 			rx_queue_id, port_id);
103599a2dd95SBruce Richardson 		return -EINVAL;
103699a2dd95SBruce Richardson 	}
103799a2dd95SBruce Richardson 
103899a2dd95SBruce Richardson 	if (dev->data->rx_queue_state[rx_queue_id] != RTE_ETH_QUEUE_STATE_STOPPED) {
103999a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(INFO,
104099a2dd95SBruce Richardson 			"Queue %"PRIu16" of device with port_id=%"PRIu16" already started\n",
104199a2dd95SBruce Richardson 			rx_queue_id, port_id);
104299a2dd95SBruce Richardson 		return 0;
104399a2dd95SBruce Richardson 	}
104499a2dd95SBruce Richardson 
104553ef1b34SMin Hu (Connor) 	return eth_err(port_id, dev->dev_ops->rx_queue_start(dev, rx_queue_id));
104699a2dd95SBruce Richardson }
104799a2dd95SBruce Richardson 
104899a2dd95SBruce Richardson int
104999a2dd95SBruce Richardson rte_eth_dev_rx_queue_stop(uint16_t port_id, uint16_t rx_queue_id)
105099a2dd95SBruce Richardson {
105199a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
105299a2dd95SBruce Richardson 	int ret;
105399a2dd95SBruce Richardson 
105499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
105599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
105699a2dd95SBruce Richardson 
105799a2dd95SBruce Richardson 	ret = eth_dev_validate_rx_queue(dev, rx_queue_id);
105899a2dd95SBruce Richardson 	if (ret != 0)
105999a2dd95SBruce Richardson 		return ret;
106099a2dd95SBruce Richardson 
106199a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_stop, -ENOTSUP);
106299a2dd95SBruce Richardson 
106399a2dd95SBruce Richardson 	if (rte_eth_dev_is_rx_hairpin_queue(dev, rx_queue_id)) {
106499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(INFO,
106599a2dd95SBruce Richardson 			"Can't stop Rx hairpin queue %"PRIu16" of device with port_id=%"PRIu16"\n",
106699a2dd95SBruce Richardson 			rx_queue_id, port_id);
106799a2dd95SBruce Richardson 		return -EINVAL;
106899a2dd95SBruce Richardson 	}
106999a2dd95SBruce Richardson 
107099a2dd95SBruce Richardson 	if (dev->data->rx_queue_state[rx_queue_id] == RTE_ETH_QUEUE_STATE_STOPPED) {
107199a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(INFO,
107299a2dd95SBruce Richardson 			"Queue %"PRIu16" of device with port_id=%"PRIu16" already stopped\n",
107399a2dd95SBruce Richardson 			rx_queue_id, port_id);
107499a2dd95SBruce Richardson 		return 0;
107599a2dd95SBruce Richardson 	}
107699a2dd95SBruce Richardson 
107799a2dd95SBruce Richardson 	return eth_err(port_id, dev->dev_ops->rx_queue_stop(dev, rx_queue_id));
107899a2dd95SBruce Richardson }
107999a2dd95SBruce Richardson 
108099a2dd95SBruce Richardson int
108199a2dd95SBruce Richardson rte_eth_dev_tx_queue_start(uint16_t port_id, uint16_t tx_queue_id)
108299a2dd95SBruce Richardson {
108399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
108499a2dd95SBruce Richardson 	int ret;
108599a2dd95SBruce Richardson 
108699a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
108799a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
108853ef1b34SMin Hu (Connor) 
108999a2dd95SBruce Richardson 	if (!dev->data->dev_started) {
109099a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
109199a2dd95SBruce Richardson 			"Port %u must be started before start any queue\n",
109299a2dd95SBruce Richardson 			port_id);
109399a2dd95SBruce Richardson 		return -EINVAL;
109499a2dd95SBruce Richardson 	}
109599a2dd95SBruce Richardson 
109699a2dd95SBruce Richardson 	ret = eth_dev_validate_tx_queue(dev, tx_queue_id);
109799a2dd95SBruce Richardson 	if (ret != 0)
109899a2dd95SBruce Richardson 		return ret;
109999a2dd95SBruce Richardson 
110099a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_start, -ENOTSUP);
110199a2dd95SBruce Richardson 
110299a2dd95SBruce Richardson 	if (rte_eth_dev_is_tx_hairpin_queue(dev, tx_queue_id)) {
110399a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(INFO,
110499a2dd95SBruce Richardson 			"Can't start Tx hairpin queue %"PRIu16" of device with port_id=%"PRIu16"\n",
110599a2dd95SBruce Richardson 			tx_queue_id, port_id);
110699a2dd95SBruce Richardson 		return -EINVAL;
110799a2dd95SBruce Richardson 	}
110899a2dd95SBruce Richardson 
110999a2dd95SBruce Richardson 	if (dev->data->tx_queue_state[tx_queue_id] != RTE_ETH_QUEUE_STATE_STOPPED) {
111099a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(INFO,
111199a2dd95SBruce Richardson 			"Queue %"PRIu16" of device with port_id=%"PRIu16" already started\n",
111299a2dd95SBruce Richardson 			tx_queue_id, port_id);
111399a2dd95SBruce Richardson 		return 0;
111499a2dd95SBruce Richardson 	}
111599a2dd95SBruce Richardson 
111699a2dd95SBruce Richardson 	return eth_err(port_id, dev->dev_ops->tx_queue_start(dev, tx_queue_id));
111799a2dd95SBruce Richardson }
111899a2dd95SBruce Richardson 
111999a2dd95SBruce Richardson int
112099a2dd95SBruce Richardson rte_eth_dev_tx_queue_stop(uint16_t port_id, uint16_t tx_queue_id)
112199a2dd95SBruce Richardson {
112299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
112399a2dd95SBruce Richardson 	int ret;
112499a2dd95SBruce Richardson 
112599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
112699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
112799a2dd95SBruce Richardson 
112899a2dd95SBruce Richardson 	ret = eth_dev_validate_tx_queue(dev, tx_queue_id);
112999a2dd95SBruce Richardson 	if (ret != 0)
113099a2dd95SBruce Richardson 		return ret;
113199a2dd95SBruce Richardson 
113299a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_stop, -ENOTSUP);
113399a2dd95SBruce Richardson 
113499a2dd95SBruce Richardson 	if (rte_eth_dev_is_tx_hairpin_queue(dev, tx_queue_id)) {
113599a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(INFO,
113699a2dd95SBruce Richardson 			"Can't stop Tx hairpin queue %"PRIu16" of device with port_id=%"PRIu16"\n",
113799a2dd95SBruce Richardson 			tx_queue_id, port_id);
113899a2dd95SBruce Richardson 		return -EINVAL;
113999a2dd95SBruce Richardson 	}
114099a2dd95SBruce Richardson 
114199a2dd95SBruce Richardson 	if (dev->data->tx_queue_state[tx_queue_id] == RTE_ETH_QUEUE_STATE_STOPPED) {
114299a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(INFO,
114399a2dd95SBruce Richardson 			"Queue %"PRIu16" of device with port_id=%"PRIu16" already stopped\n",
114499a2dd95SBruce Richardson 			tx_queue_id, port_id);
114599a2dd95SBruce Richardson 		return 0;
114699a2dd95SBruce Richardson 	}
114799a2dd95SBruce Richardson 
114899a2dd95SBruce Richardson 	return eth_err(port_id, dev->dev_ops->tx_queue_stop(dev, tx_queue_id));
114999a2dd95SBruce Richardson }
115099a2dd95SBruce Richardson 
115199a2dd95SBruce Richardson static int
115299a2dd95SBruce Richardson eth_dev_tx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
115399a2dd95SBruce Richardson {
115499a2dd95SBruce Richardson 	uint16_t old_nb_queues = dev->data->nb_tx_queues;
115599a2dd95SBruce Richardson 	unsigned i;
115699a2dd95SBruce Richardson 
115799a2dd95SBruce Richardson 	if (dev->data->tx_queues == NULL && nb_queues != 0) { /* first time configuration */
115899a2dd95SBruce Richardson 		dev->data->tx_queues = rte_zmalloc("ethdev->tx_queues",
1159c024496aSKonstantin Ananyev 				sizeof(dev->data->tx_queues[0]) *
1160c024496aSKonstantin Ananyev 				RTE_MAX_QUEUES_PER_PORT,
116199a2dd95SBruce Richardson 				RTE_CACHE_LINE_SIZE);
116299a2dd95SBruce Richardson 		if (dev->data->tx_queues == NULL) {
116399a2dd95SBruce Richardson 			dev->data->nb_tx_queues = 0;
116499a2dd95SBruce Richardson 			return -(ENOMEM);
116599a2dd95SBruce Richardson 		}
116699a2dd95SBruce Richardson 	} else if (dev->data->tx_queues != NULL && nb_queues != 0) { /* re-configure */
116749ed3224SXueming Li 		for (i = nb_queues; i < old_nb_queues; i++)
116849ed3224SXueming Li 			eth_dev_txq_release(dev, i);
116999a2dd95SBruce Richardson 
117099a2dd95SBruce Richardson 	} else if (dev->data->tx_queues != NULL && nb_queues == 0) {
117199a2dd95SBruce Richardson 		for (i = nb_queues; i < old_nb_queues; i++)
117249ed3224SXueming Li 			eth_dev_txq_release(dev, i);
117399a2dd95SBruce Richardson 
117499a2dd95SBruce Richardson 		rte_free(dev->data->tx_queues);
117599a2dd95SBruce Richardson 		dev->data->tx_queues = NULL;
117699a2dd95SBruce Richardson 	}
117799a2dd95SBruce Richardson 	dev->data->nb_tx_queues = nb_queues;
117899a2dd95SBruce Richardson 	return 0;
117999a2dd95SBruce Richardson }
118099a2dd95SBruce Richardson 
118199a2dd95SBruce Richardson uint32_t
118299a2dd95SBruce Richardson rte_eth_speed_bitflag(uint32_t speed, int duplex)
118399a2dd95SBruce Richardson {
118499a2dd95SBruce Richardson 	switch (speed) {
1185295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_10M:
1186295968d1SFerruh Yigit 		return duplex ? RTE_ETH_LINK_SPEED_10M : RTE_ETH_LINK_SPEED_10M_HD;
1187295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_100M:
1188295968d1SFerruh Yigit 		return duplex ? RTE_ETH_LINK_SPEED_100M : RTE_ETH_LINK_SPEED_100M_HD;
1189295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_1G:
1190295968d1SFerruh Yigit 		return RTE_ETH_LINK_SPEED_1G;
1191295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_2_5G:
1192295968d1SFerruh Yigit 		return RTE_ETH_LINK_SPEED_2_5G;
1193295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_5G:
1194295968d1SFerruh Yigit 		return RTE_ETH_LINK_SPEED_5G;
1195295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_10G:
1196295968d1SFerruh Yigit 		return RTE_ETH_LINK_SPEED_10G;
1197295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_20G:
1198295968d1SFerruh Yigit 		return RTE_ETH_LINK_SPEED_20G;
1199295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_25G:
1200295968d1SFerruh Yigit 		return RTE_ETH_LINK_SPEED_25G;
1201295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_40G:
1202295968d1SFerruh Yigit 		return RTE_ETH_LINK_SPEED_40G;
1203295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_50G:
1204295968d1SFerruh Yigit 		return RTE_ETH_LINK_SPEED_50G;
1205295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_56G:
1206295968d1SFerruh Yigit 		return RTE_ETH_LINK_SPEED_56G;
1207295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_100G:
1208295968d1SFerruh Yigit 		return RTE_ETH_LINK_SPEED_100G;
1209295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_200G:
1210295968d1SFerruh Yigit 		return RTE_ETH_LINK_SPEED_200G;
121199a2dd95SBruce Richardson 	default:
121299a2dd95SBruce Richardson 		return 0;
121399a2dd95SBruce Richardson 	}
121499a2dd95SBruce Richardson }
121599a2dd95SBruce Richardson 
121699a2dd95SBruce Richardson const char *
121799a2dd95SBruce Richardson rte_eth_dev_rx_offload_name(uint64_t offload)
121899a2dd95SBruce Richardson {
121999a2dd95SBruce Richardson 	const char *name = "UNKNOWN";
122099a2dd95SBruce Richardson 	unsigned int i;
122199a2dd95SBruce Richardson 
122299a2dd95SBruce Richardson 	for (i = 0; i < RTE_DIM(eth_dev_rx_offload_names); ++i) {
122399a2dd95SBruce Richardson 		if (offload == eth_dev_rx_offload_names[i].offload) {
122499a2dd95SBruce Richardson 			name = eth_dev_rx_offload_names[i].name;
122599a2dd95SBruce Richardson 			break;
122699a2dd95SBruce Richardson 		}
122799a2dd95SBruce Richardson 	}
122899a2dd95SBruce Richardson 
122999a2dd95SBruce Richardson 	return name;
123099a2dd95SBruce Richardson }
123199a2dd95SBruce Richardson 
123299a2dd95SBruce Richardson const char *
123399a2dd95SBruce Richardson rte_eth_dev_tx_offload_name(uint64_t offload)
123499a2dd95SBruce Richardson {
123599a2dd95SBruce Richardson 	const char *name = "UNKNOWN";
123699a2dd95SBruce Richardson 	unsigned int i;
123799a2dd95SBruce Richardson 
123899a2dd95SBruce Richardson 	for (i = 0; i < RTE_DIM(eth_dev_tx_offload_names); ++i) {
123999a2dd95SBruce Richardson 		if (offload == eth_dev_tx_offload_names[i].offload) {
124099a2dd95SBruce Richardson 			name = eth_dev_tx_offload_names[i].name;
124199a2dd95SBruce Richardson 			break;
124299a2dd95SBruce Richardson 		}
124399a2dd95SBruce Richardson 	}
124499a2dd95SBruce Richardson 
124599a2dd95SBruce Richardson 	return name;
124699a2dd95SBruce Richardson }
124799a2dd95SBruce Richardson 
124893e441c9SXueming Li const char *
124993e441c9SXueming Li rte_eth_dev_capability_name(uint64_t capability)
125093e441c9SXueming Li {
125193e441c9SXueming Li 	const char *name = "UNKNOWN";
125293e441c9SXueming Li 	unsigned int i;
125393e441c9SXueming Li 
125493e441c9SXueming Li 	for (i = 0; i < RTE_DIM(rte_eth_dev_capa_names); ++i) {
125593e441c9SXueming Li 		if (capability == rte_eth_dev_capa_names[i].offload) {
125693e441c9SXueming Li 			name = rte_eth_dev_capa_names[i].name;
125793e441c9SXueming Li 			break;
125893e441c9SXueming Li 		}
125993e441c9SXueming Li 	}
126093e441c9SXueming Li 
126193e441c9SXueming Li 	return name;
126293e441c9SXueming Li }
126393e441c9SXueming Li 
126499a2dd95SBruce Richardson static inline int
126599a2dd95SBruce Richardson eth_dev_check_lro_pkt_size(uint16_t port_id, uint32_t config_size,
126699a2dd95SBruce Richardson 		   uint32_t max_rx_pkt_len, uint32_t dev_info_size)
126799a2dd95SBruce Richardson {
126899a2dd95SBruce Richardson 	int ret = 0;
126999a2dd95SBruce Richardson 
127099a2dd95SBruce Richardson 	if (dev_info_size == 0) {
127199a2dd95SBruce Richardson 		if (config_size != max_rx_pkt_len) {
127299a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR, "Ethdev port_id=%d max_lro_pkt_size"
127399a2dd95SBruce Richardson 				       " %u != %u is not allowed\n",
127499a2dd95SBruce Richardson 				       port_id, config_size, max_rx_pkt_len);
127599a2dd95SBruce Richardson 			ret = -EINVAL;
127699a2dd95SBruce Richardson 		}
127799a2dd95SBruce Richardson 	} else if (config_size > dev_info_size) {
127899a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Ethdev port_id=%d max_lro_pkt_size %u "
127999a2dd95SBruce Richardson 			       "> max allowed value %u\n", port_id, config_size,
128099a2dd95SBruce Richardson 			       dev_info_size);
128199a2dd95SBruce Richardson 		ret = -EINVAL;
128299a2dd95SBruce Richardson 	} else if (config_size < RTE_ETHER_MIN_LEN) {
128399a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Ethdev port_id=%d max_lro_pkt_size %u "
128499a2dd95SBruce Richardson 			       "< min allowed value %u\n", port_id, config_size,
128599a2dd95SBruce Richardson 			       (unsigned int)RTE_ETHER_MIN_LEN);
128699a2dd95SBruce Richardson 		ret = -EINVAL;
128799a2dd95SBruce Richardson 	}
128899a2dd95SBruce Richardson 	return ret;
128999a2dd95SBruce Richardson }
129099a2dd95SBruce Richardson 
129199a2dd95SBruce Richardson /*
129299a2dd95SBruce Richardson  * Validate offloads that are requested through rte_eth_dev_configure against
12930d9f56a8SAndrew Rybchenko  * the offloads successfully set by the Ethernet device.
129499a2dd95SBruce Richardson  *
129599a2dd95SBruce Richardson  * @param port_id
129699a2dd95SBruce Richardson  *   The port identifier of the Ethernet device.
129799a2dd95SBruce Richardson  * @param req_offloads
129899a2dd95SBruce Richardson  *   The offloads that have been requested through `rte_eth_dev_configure`.
129999a2dd95SBruce Richardson  * @param set_offloads
13000d9f56a8SAndrew Rybchenko  *   The offloads successfully set by the Ethernet device.
130199a2dd95SBruce Richardson  * @param offload_type
130299a2dd95SBruce Richardson  *   The offload type i.e. Rx/Tx string.
130399a2dd95SBruce Richardson  * @param offload_name
130499a2dd95SBruce Richardson  *   The function that prints the offload name.
130599a2dd95SBruce Richardson  * @return
130699a2dd95SBruce Richardson  *   - (0) if validation successful.
130799a2dd95SBruce Richardson  *   - (-EINVAL) if requested offload has been silently disabled.
130899a2dd95SBruce Richardson  *
130999a2dd95SBruce Richardson  */
131099a2dd95SBruce Richardson static int
131199a2dd95SBruce Richardson eth_dev_validate_offloads(uint16_t port_id, uint64_t req_offloads,
131299a2dd95SBruce Richardson 		  uint64_t set_offloads, const char *offload_type,
131399a2dd95SBruce Richardson 		  const char *(*offload_name)(uint64_t))
131499a2dd95SBruce Richardson {
131599a2dd95SBruce Richardson 	uint64_t offloads_diff = req_offloads ^ set_offloads;
131699a2dd95SBruce Richardson 	uint64_t offload;
131799a2dd95SBruce Richardson 	int ret = 0;
131899a2dd95SBruce Richardson 
131999a2dd95SBruce Richardson 	while (offloads_diff != 0) {
132099a2dd95SBruce Richardson 		/* Check if any offload is requested but not enabled. */
1321e1823e08SThomas Monjalon 		offload = RTE_BIT64(__builtin_ctzll(offloads_diff));
132299a2dd95SBruce Richardson 		if (offload & req_offloads) {
132399a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
132499a2dd95SBruce Richardson 				"Port %u failed to enable %s offload %s\n",
132599a2dd95SBruce Richardson 				port_id, offload_type, offload_name(offload));
132699a2dd95SBruce Richardson 			ret = -EINVAL;
132799a2dd95SBruce Richardson 		}
132899a2dd95SBruce Richardson 
132999a2dd95SBruce Richardson 		/* Check if offload couldn't be disabled. */
133099a2dd95SBruce Richardson 		if (offload & set_offloads) {
133199a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(DEBUG,
133299a2dd95SBruce Richardson 				"Port %u %s offload %s is not requested but enabled\n",
133399a2dd95SBruce Richardson 				port_id, offload_type, offload_name(offload));
133499a2dd95SBruce Richardson 		}
133599a2dd95SBruce Richardson 
133699a2dd95SBruce Richardson 		offloads_diff &= ~offload;
133799a2dd95SBruce Richardson 	}
133899a2dd95SBruce Richardson 
133999a2dd95SBruce Richardson 	return ret;
134099a2dd95SBruce Richardson }
134199a2dd95SBruce Richardson 
13421bb4a528SFerruh Yigit static uint32_t
13431bb4a528SFerruh Yigit eth_dev_get_overhead_len(uint32_t max_rx_pktlen, uint16_t max_mtu)
13441bb4a528SFerruh Yigit {
13451bb4a528SFerruh Yigit 	uint32_t overhead_len;
13461bb4a528SFerruh Yigit 
13471bb4a528SFerruh Yigit 	if (max_mtu != UINT16_MAX && max_rx_pktlen > max_mtu)
13481bb4a528SFerruh Yigit 		overhead_len = max_rx_pktlen - max_mtu;
13491bb4a528SFerruh Yigit 	else
13501bb4a528SFerruh Yigit 		overhead_len = RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN;
13511bb4a528SFerruh Yigit 
13521bb4a528SFerruh Yigit 	return overhead_len;
13531bb4a528SFerruh Yigit }
13541bb4a528SFerruh Yigit 
1355990912e6SFerruh Yigit /* rte_eth_dev_info_get() should be called prior to this function */
1356990912e6SFerruh Yigit static int
1357990912e6SFerruh Yigit eth_dev_validate_mtu(uint16_t port_id, struct rte_eth_dev_info *dev_info,
1358990912e6SFerruh Yigit 		uint16_t mtu)
1359990912e6SFerruh Yigit {
1360990912e6SFerruh Yigit 	uint32_t overhead_len;
1361990912e6SFerruh Yigit 	uint32_t frame_size;
1362990912e6SFerruh Yigit 
1363990912e6SFerruh Yigit 	if (mtu < dev_info->min_mtu) {
1364990912e6SFerruh Yigit 		RTE_ETHDEV_LOG(ERR,
1365990912e6SFerruh Yigit 			"MTU (%u) < device min MTU (%u) for port_id %u\n",
1366990912e6SFerruh Yigit 			mtu, dev_info->min_mtu, port_id);
1367990912e6SFerruh Yigit 		return -EINVAL;
1368990912e6SFerruh Yigit 	}
1369990912e6SFerruh Yigit 	if (mtu > dev_info->max_mtu) {
1370990912e6SFerruh Yigit 		RTE_ETHDEV_LOG(ERR,
1371990912e6SFerruh Yigit 			"MTU (%u) > device max MTU (%u) for port_id %u\n",
1372990912e6SFerruh Yigit 			mtu, dev_info->max_mtu, port_id);
1373990912e6SFerruh Yigit 		return -EINVAL;
1374990912e6SFerruh Yigit 	}
1375990912e6SFerruh Yigit 
1376990912e6SFerruh Yigit 	overhead_len = eth_dev_get_overhead_len(dev_info->max_rx_pktlen,
1377990912e6SFerruh Yigit 			dev_info->max_mtu);
1378990912e6SFerruh Yigit 	frame_size = mtu + overhead_len;
1379990912e6SFerruh Yigit 	if (frame_size < RTE_ETHER_MIN_LEN) {
1380990912e6SFerruh Yigit 		RTE_ETHDEV_LOG(ERR,
1381990912e6SFerruh Yigit 			"Frame size (%u) < min frame size (%u) for port_id %u\n",
1382990912e6SFerruh Yigit 			frame_size, RTE_ETHER_MIN_LEN, port_id);
1383990912e6SFerruh Yigit 		return -EINVAL;
1384990912e6SFerruh Yigit 	}
1385990912e6SFerruh Yigit 
1386990912e6SFerruh Yigit 	if (frame_size > dev_info->max_rx_pktlen) {
1387990912e6SFerruh Yigit 		RTE_ETHDEV_LOG(ERR,
1388990912e6SFerruh Yigit 			"Frame size (%u) > device max frame size (%u) for port_id %u\n",
1389990912e6SFerruh Yigit 			frame_size, dev_info->max_rx_pktlen, port_id);
1390990912e6SFerruh Yigit 		return -EINVAL;
1391990912e6SFerruh Yigit 	}
1392990912e6SFerruh Yigit 
1393990912e6SFerruh Yigit 	return 0;
1394990912e6SFerruh Yigit }
1395990912e6SFerruh Yigit 
139699a2dd95SBruce Richardson int
139799a2dd95SBruce Richardson rte_eth_dev_configure(uint16_t port_id, uint16_t nb_rx_q, uint16_t nb_tx_q,
139899a2dd95SBruce Richardson 		      const struct rte_eth_conf *dev_conf)
139999a2dd95SBruce Richardson {
140099a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
140199a2dd95SBruce Richardson 	struct rte_eth_dev_info dev_info;
140299a2dd95SBruce Richardson 	struct rte_eth_conf orig_conf;
140399a2dd95SBruce Richardson 	int diag;
140499a2dd95SBruce Richardson 	int ret;
140599a2dd95SBruce Richardson 	uint16_t old_mtu;
140699a2dd95SBruce Richardson 
140799a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
140899a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
140999a2dd95SBruce Richardson 
141053ef1b34SMin Hu (Connor) 	if (dev_conf == NULL) {
141153ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
141253ef1b34SMin Hu (Connor) 			"Cannot configure ethdev port %u from NULL config\n",
141353ef1b34SMin Hu (Connor) 			port_id);
141453ef1b34SMin Hu (Connor) 		return -EINVAL;
141553ef1b34SMin Hu (Connor) 	}
141653ef1b34SMin Hu (Connor) 
141799a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_configure, -ENOTSUP);
141899a2dd95SBruce Richardson 
141999a2dd95SBruce Richardson 	if (dev->data->dev_started) {
142099a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
142199a2dd95SBruce Richardson 			"Port %u must be stopped to allow configuration\n",
142299a2dd95SBruce Richardson 			port_id);
142399a2dd95SBruce Richardson 		return -EBUSY;
142499a2dd95SBruce Richardson 	}
142599a2dd95SBruce Richardson 
142602edbfabSHuisong Li 	/*
142702edbfabSHuisong Li 	 * Ensure that "dev_configured" is always 0 each time prepare to do
142802edbfabSHuisong Li 	 * dev_configure() to avoid any non-anticipated behaviour.
142902edbfabSHuisong Li 	 * And set to 1 when dev_configure() is executed successfully.
143002edbfabSHuisong Li 	 */
143102edbfabSHuisong Li 	dev->data->dev_configured = 0;
143202edbfabSHuisong Li 
143399a2dd95SBruce Richardson 	 /* Store original config, as rollback required on failure */
143499a2dd95SBruce Richardson 	memcpy(&orig_conf, &dev->data->dev_conf, sizeof(dev->data->dev_conf));
143599a2dd95SBruce Richardson 
143699a2dd95SBruce Richardson 	/*
143799a2dd95SBruce Richardson 	 * Copy the dev_conf parameter into the dev structure.
143899a2dd95SBruce Richardson 	 * rte_eth_dev_info_get() requires dev_conf, copy it before dev_info get
143999a2dd95SBruce Richardson 	 */
144099a2dd95SBruce Richardson 	if (dev_conf != &dev->data->dev_conf)
144199a2dd95SBruce Richardson 		memcpy(&dev->data->dev_conf, dev_conf,
144299a2dd95SBruce Richardson 		       sizeof(dev->data->dev_conf));
144399a2dd95SBruce Richardson 
144499a2dd95SBruce Richardson 	/* Backup mtu for rollback */
144599a2dd95SBruce Richardson 	old_mtu = dev->data->mtu;
144699a2dd95SBruce Richardson 
144799a2dd95SBruce Richardson 	ret = rte_eth_dev_info_get(port_id, &dev_info);
144899a2dd95SBruce Richardson 	if (ret != 0)
144999a2dd95SBruce Richardson 		goto rollback;
145099a2dd95SBruce Richardson 
145199a2dd95SBruce Richardson 	/* If number of queues specified by application for both Rx and Tx is
145299a2dd95SBruce Richardson 	 * zero, use driver preferred values. This cannot be done individually
145399a2dd95SBruce Richardson 	 * as it is valid for either Tx or Rx (but not both) to be zero.
145499a2dd95SBruce Richardson 	 * If driver does not provide any preferred valued, fall back on
145599a2dd95SBruce Richardson 	 * EAL defaults.
145699a2dd95SBruce Richardson 	 */
145799a2dd95SBruce Richardson 	if (nb_rx_q == 0 && nb_tx_q == 0) {
145899a2dd95SBruce Richardson 		nb_rx_q = dev_info.default_rxportconf.nb_queues;
145999a2dd95SBruce Richardson 		if (nb_rx_q == 0)
146099a2dd95SBruce Richardson 			nb_rx_q = RTE_ETH_DEV_FALLBACK_RX_NBQUEUES;
146199a2dd95SBruce Richardson 		nb_tx_q = dev_info.default_txportconf.nb_queues;
146299a2dd95SBruce Richardson 		if (nb_tx_q == 0)
146399a2dd95SBruce Richardson 			nb_tx_q = RTE_ETH_DEV_FALLBACK_TX_NBQUEUES;
146499a2dd95SBruce Richardson 	}
146599a2dd95SBruce Richardson 
146699a2dd95SBruce Richardson 	if (nb_rx_q > RTE_MAX_QUEUES_PER_PORT) {
146799a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
146809fd4227SAndrew Rybchenko 			"Number of Rx queues requested (%u) is greater than max supported(%d)\n",
146999a2dd95SBruce Richardson 			nb_rx_q, RTE_MAX_QUEUES_PER_PORT);
147099a2dd95SBruce Richardson 		ret = -EINVAL;
147199a2dd95SBruce Richardson 		goto rollback;
147299a2dd95SBruce Richardson 	}
147399a2dd95SBruce Richardson 
147499a2dd95SBruce Richardson 	if (nb_tx_q > RTE_MAX_QUEUES_PER_PORT) {
147599a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
147609fd4227SAndrew Rybchenko 			"Number of Tx queues requested (%u) is greater than max supported(%d)\n",
147799a2dd95SBruce Richardson 			nb_tx_q, RTE_MAX_QUEUES_PER_PORT);
147899a2dd95SBruce Richardson 		ret = -EINVAL;
147999a2dd95SBruce Richardson 		goto rollback;
148099a2dd95SBruce Richardson 	}
148199a2dd95SBruce Richardson 
148299a2dd95SBruce Richardson 	/*
148309fd4227SAndrew Rybchenko 	 * Check that the numbers of Rx and Tx queues are not greater
148409fd4227SAndrew Rybchenko 	 * than the maximum number of Rx and Tx queues supported by the
148599a2dd95SBruce Richardson 	 * configured device.
148699a2dd95SBruce Richardson 	 */
148799a2dd95SBruce Richardson 	if (nb_rx_q > dev_info.max_rx_queues) {
148899a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Ethdev port_id=%u nb_rx_queues=%u > %u\n",
148999a2dd95SBruce Richardson 			port_id, nb_rx_q, dev_info.max_rx_queues);
149099a2dd95SBruce Richardson 		ret = -EINVAL;
149199a2dd95SBruce Richardson 		goto rollback;
149299a2dd95SBruce Richardson 	}
149399a2dd95SBruce Richardson 
149499a2dd95SBruce Richardson 	if (nb_tx_q > dev_info.max_tx_queues) {
149599a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Ethdev port_id=%u nb_tx_queues=%u > %u\n",
149699a2dd95SBruce Richardson 			port_id, nb_tx_q, dev_info.max_tx_queues);
149799a2dd95SBruce Richardson 		ret = -EINVAL;
149899a2dd95SBruce Richardson 		goto rollback;
149999a2dd95SBruce Richardson 	}
150099a2dd95SBruce Richardson 
150199a2dd95SBruce Richardson 	/* Check that the device supports requested interrupts */
150299a2dd95SBruce Richardson 	if ((dev_conf->intr_conf.lsc == 1) &&
150399a2dd95SBruce Richardson 			(!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC))) {
150499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Driver %s does not support lsc\n",
150599a2dd95SBruce Richardson 			dev->device->driver->name);
150699a2dd95SBruce Richardson 		ret = -EINVAL;
150799a2dd95SBruce Richardson 		goto rollback;
150899a2dd95SBruce Richardson 	}
150999a2dd95SBruce Richardson 	if ((dev_conf->intr_conf.rmv == 1) &&
151099a2dd95SBruce Richardson 			(!(dev->data->dev_flags & RTE_ETH_DEV_INTR_RMV))) {
151199a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Driver %s does not support rmv\n",
151299a2dd95SBruce Richardson 			dev->device->driver->name);
151399a2dd95SBruce Richardson 		ret = -EINVAL;
151499a2dd95SBruce Richardson 		goto rollback;
151599a2dd95SBruce Richardson 	}
151699a2dd95SBruce Richardson 
15171bb4a528SFerruh Yigit 	if (dev_conf->rxmode.mtu == 0)
15181bb4a528SFerruh Yigit 		dev->data->dev_conf.rxmode.mtu = RTE_ETHER_MTU;
1519990912e6SFerruh Yigit 
1520990912e6SFerruh Yigit 	ret = eth_dev_validate_mtu(port_id, &dev_info,
1521990912e6SFerruh Yigit 			dev->data->dev_conf.rxmode.mtu);
1522990912e6SFerruh Yigit 	if (ret != 0)
152399a2dd95SBruce Richardson 		goto rollback;
152499a2dd95SBruce Richardson 
15251bb4a528SFerruh Yigit 	dev->data->mtu = dev->data->dev_conf.rxmode.mtu;
15261bb4a528SFerruh Yigit 
152799a2dd95SBruce Richardson 	/*
152899a2dd95SBruce Richardson 	 * If LRO is enabled, check that the maximum aggregated packet
152999a2dd95SBruce Richardson 	 * size is supported by the configured device.
153099a2dd95SBruce Richardson 	 */
1531295968d1SFerruh Yigit 	if (dev_conf->rxmode.offloads & RTE_ETH_RX_OFFLOAD_TCP_LRO) {
1532990912e6SFerruh Yigit 		uint32_t max_rx_pktlen;
1533990912e6SFerruh Yigit 		uint32_t overhead_len;
1534990912e6SFerruh Yigit 
1535990912e6SFerruh Yigit 		overhead_len = eth_dev_get_overhead_len(dev_info.max_rx_pktlen,
1536990912e6SFerruh Yigit 				dev_info.max_mtu);
1537990912e6SFerruh Yigit 		max_rx_pktlen = dev->data->dev_conf.rxmode.mtu + overhead_len;
153899a2dd95SBruce Richardson 		if (dev_conf->rxmode.max_lro_pkt_size == 0)
15391bb4a528SFerruh Yigit 			dev->data->dev_conf.rxmode.max_lro_pkt_size = max_rx_pktlen;
154099a2dd95SBruce Richardson 		ret = eth_dev_check_lro_pkt_size(port_id,
154199a2dd95SBruce Richardson 				dev->data->dev_conf.rxmode.max_lro_pkt_size,
15421bb4a528SFerruh Yigit 				max_rx_pktlen,
154399a2dd95SBruce Richardson 				dev_info.max_lro_pkt_size);
154499a2dd95SBruce Richardson 		if (ret != 0)
154599a2dd95SBruce Richardson 			goto rollback;
154699a2dd95SBruce Richardson 	}
154799a2dd95SBruce Richardson 
154899a2dd95SBruce Richardson 	/* Any requested offloading must be within its device capabilities */
154999a2dd95SBruce Richardson 	if ((dev_conf->rxmode.offloads & dev_info.rx_offload_capa) !=
155099a2dd95SBruce Richardson 	     dev_conf->rxmode.offloads) {
155199a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
155299a2dd95SBruce Richardson 			"Ethdev port_id=%u requested Rx offloads 0x%"PRIx64" doesn't match Rx offloads "
155399a2dd95SBruce Richardson 			"capabilities 0x%"PRIx64" in %s()\n",
155499a2dd95SBruce Richardson 			port_id, dev_conf->rxmode.offloads,
155599a2dd95SBruce Richardson 			dev_info.rx_offload_capa,
155699a2dd95SBruce Richardson 			__func__);
155799a2dd95SBruce Richardson 		ret = -EINVAL;
155899a2dd95SBruce Richardson 		goto rollback;
155999a2dd95SBruce Richardson 	}
156099a2dd95SBruce Richardson 	if ((dev_conf->txmode.offloads & dev_info.tx_offload_capa) !=
156199a2dd95SBruce Richardson 	     dev_conf->txmode.offloads) {
156299a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
156399a2dd95SBruce Richardson 			"Ethdev port_id=%u requested Tx offloads 0x%"PRIx64" doesn't match Tx offloads "
156499a2dd95SBruce Richardson 			"capabilities 0x%"PRIx64" in %s()\n",
156599a2dd95SBruce Richardson 			port_id, dev_conf->txmode.offloads,
156699a2dd95SBruce Richardson 			dev_info.tx_offload_capa,
156799a2dd95SBruce Richardson 			__func__);
156899a2dd95SBruce Richardson 		ret = -EINVAL;
156999a2dd95SBruce Richardson 		goto rollback;
157099a2dd95SBruce Richardson 	}
157199a2dd95SBruce Richardson 
157299a2dd95SBruce Richardson 	dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf =
157399a2dd95SBruce Richardson 		rte_eth_rss_hf_refine(dev_conf->rx_adv_conf.rss_conf.rss_hf);
157499a2dd95SBruce Richardson 
157599a2dd95SBruce Richardson 	/* Check that device supports requested rss hash functions. */
157699a2dd95SBruce Richardson 	if ((dev_info.flow_type_rss_offloads |
157799a2dd95SBruce Richardson 	     dev_conf->rx_adv_conf.rss_conf.rss_hf) !=
157899a2dd95SBruce Richardson 	    dev_info.flow_type_rss_offloads) {
157999a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
158099a2dd95SBruce Richardson 			"Ethdev port_id=%u invalid rss_hf: 0x%"PRIx64", valid value: 0x%"PRIx64"\n",
158199a2dd95SBruce Richardson 			port_id, dev_conf->rx_adv_conf.rss_conf.rss_hf,
158299a2dd95SBruce Richardson 			dev_info.flow_type_rss_offloads);
158399a2dd95SBruce Richardson 		ret = -EINVAL;
158499a2dd95SBruce Richardson 		goto rollback;
158599a2dd95SBruce Richardson 	}
158699a2dd95SBruce Richardson 
158799a2dd95SBruce Richardson 	/* Check if Rx RSS distribution is disabled but RSS hash is enabled. */
1588295968d1SFerruh Yigit 	if (((dev_conf->rxmode.mq_mode & RTE_ETH_MQ_RX_RSS_FLAG) == 0) &&
1589295968d1SFerruh Yigit 	    (dev_conf->rxmode.offloads & RTE_ETH_RX_OFFLOAD_RSS_HASH)) {
159099a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
159199a2dd95SBruce Richardson 			"Ethdev port_id=%u config invalid Rx mq_mode without RSS but %s offload is requested\n",
159299a2dd95SBruce Richardson 			port_id,
1593295968d1SFerruh Yigit 			rte_eth_dev_rx_offload_name(RTE_ETH_RX_OFFLOAD_RSS_HASH));
159499a2dd95SBruce Richardson 		ret = -EINVAL;
159599a2dd95SBruce Richardson 		goto rollback;
159699a2dd95SBruce Richardson 	}
159799a2dd95SBruce Richardson 
159899a2dd95SBruce Richardson 	/*
159909fd4227SAndrew Rybchenko 	 * Setup new number of Rx/Tx queues and reconfigure device.
160099a2dd95SBruce Richardson 	 */
160199a2dd95SBruce Richardson 	diag = eth_dev_rx_queue_config(dev, nb_rx_q);
160299a2dd95SBruce Richardson 	if (diag != 0) {
160399a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
160499a2dd95SBruce Richardson 			"Port%u eth_dev_rx_queue_config = %d\n",
160599a2dd95SBruce Richardson 			port_id, diag);
160699a2dd95SBruce Richardson 		ret = diag;
160799a2dd95SBruce Richardson 		goto rollback;
160899a2dd95SBruce Richardson 	}
160999a2dd95SBruce Richardson 
161099a2dd95SBruce Richardson 	diag = eth_dev_tx_queue_config(dev, nb_tx_q);
161199a2dd95SBruce Richardson 	if (diag != 0) {
161299a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
161399a2dd95SBruce Richardson 			"Port%u eth_dev_tx_queue_config = %d\n",
161499a2dd95SBruce Richardson 			port_id, diag);
161599a2dd95SBruce Richardson 		eth_dev_rx_queue_config(dev, 0);
161699a2dd95SBruce Richardson 		ret = diag;
161799a2dd95SBruce Richardson 		goto rollback;
161899a2dd95SBruce Richardson 	}
161999a2dd95SBruce Richardson 
162099a2dd95SBruce Richardson 	diag = (*dev->dev_ops->dev_configure)(dev);
162199a2dd95SBruce Richardson 	if (diag != 0) {
162299a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Port%u dev_configure = %d\n",
162399a2dd95SBruce Richardson 			port_id, diag);
162499a2dd95SBruce Richardson 		ret = eth_err(port_id, diag);
162599a2dd95SBruce Richardson 		goto reset_queues;
162699a2dd95SBruce Richardson 	}
162799a2dd95SBruce Richardson 
162899a2dd95SBruce Richardson 	/* Initialize Rx profiling if enabled at compilation time. */
162999a2dd95SBruce Richardson 	diag = __rte_eth_dev_profile_init(port_id, dev);
163099a2dd95SBruce Richardson 	if (diag != 0) {
163199a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Port%u __rte_eth_dev_profile_init = %d\n",
163299a2dd95SBruce Richardson 			port_id, diag);
163399a2dd95SBruce Richardson 		ret = eth_err(port_id, diag);
163499a2dd95SBruce Richardson 		goto reset_queues;
163599a2dd95SBruce Richardson 	}
163699a2dd95SBruce Richardson 
163799a2dd95SBruce Richardson 	/* Validate Rx offloads. */
163899a2dd95SBruce Richardson 	diag = eth_dev_validate_offloads(port_id,
163999a2dd95SBruce Richardson 			dev_conf->rxmode.offloads,
164099a2dd95SBruce Richardson 			dev->data->dev_conf.rxmode.offloads, "Rx",
164199a2dd95SBruce Richardson 			rte_eth_dev_rx_offload_name);
164299a2dd95SBruce Richardson 	if (diag != 0) {
164399a2dd95SBruce Richardson 		ret = diag;
164499a2dd95SBruce Richardson 		goto reset_queues;
164599a2dd95SBruce Richardson 	}
164699a2dd95SBruce Richardson 
164799a2dd95SBruce Richardson 	/* Validate Tx offloads. */
164899a2dd95SBruce Richardson 	diag = eth_dev_validate_offloads(port_id,
164999a2dd95SBruce Richardson 			dev_conf->txmode.offloads,
165099a2dd95SBruce Richardson 			dev->data->dev_conf.txmode.offloads, "Tx",
165199a2dd95SBruce Richardson 			rte_eth_dev_tx_offload_name);
165299a2dd95SBruce Richardson 	if (diag != 0) {
165399a2dd95SBruce Richardson 		ret = diag;
165499a2dd95SBruce Richardson 		goto reset_queues;
165599a2dd95SBruce Richardson 	}
165699a2dd95SBruce Richardson 
165702edbfabSHuisong Li 	dev->data->dev_configured = 1;
165899a2dd95SBruce Richardson 	rte_ethdev_trace_configure(port_id, nb_rx_q, nb_tx_q, dev_conf, 0);
165999a2dd95SBruce Richardson 	return 0;
166099a2dd95SBruce Richardson reset_queues:
166199a2dd95SBruce Richardson 	eth_dev_rx_queue_config(dev, 0);
166299a2dd95SBruce Richardson 	eth_dev_tx_queue_config(dev, 0);
166399a2dd95SBruce Richardson rollback:
166499a2dd95SBruce Richardson 	memcpy(&dev->data->dev_conf, &orig_conf, sizeof(dev->data->dev_conf));
166599a2dd95SBruce Richardson 	if (old_mtu != dev->data->mtu)
166699a2dd95SBruce Richardson 		dev->data->mtu = old_mtu;
166799a2dd95SBruce Richardson 
166899a2dd95SBruce Richardson 	rte_ethdev_trace_configure(port_id, nb_rx_q, nb_tx_q, dev_conf, ret);
166999a2dd95SBruce Richardson 	return ret;
167099a2dd95SBruce Richardson }
167199a2dd95SBruce Richardson 
167299a2dd95SBruce Richardson void
167399a2dd95SBruce Richardson rte_eth_dev_internal_reset(struct rte_eth_dev *dev)
167499a2dd95SBruce Richardson {
167599a2dd95SBruce Richardson 	if (dev->data->dev_started) {
167699a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Port %u must be stopped to allow reset\n",
167799a2dd95SBruce Richardson 			dev->data->port_id);
167899a2dd95SBruce Richardson 		return;
167999a2dd95SBruce Richardson 	}
168099a2dd95SBruce Richardson 
168199a2dd95SBruce Richardson 	eth_dev_rx_queue_config(dev, 0);
168299a2dd95SBruce Richardson 	eth_dev_tx_queue_config(dev, 0);
168399a2dd95SBruce Richardson 
168499a2dd95SBruce Richardson 	memset(&dev->data->dev_conf, 0, sizeof(dev->data->dev_conf));
168599a2dd95SBruce Richardson }
168699a2dd95SBruce Richardson 
168799a2dd95SBruce Richardson static void
168899a2dd95SBruce Richardson eth_dev_mac_restore(struct rte_eth_dev *dev,
168999a2dd95SBruce Richardson 			struct rte_eth_dev_info *dev_info)
169099a2dd95SBruce Richardson {
169199a2dd95SBruce Richardson 	struct rte_ether_addr *addr;
169299a2dd95SBruce Richardson 	uint16_t i;
169399a2dd95SBruce Richardson 	uint32_t pool = 0;
169499a2dd95SBruce Richardson 	uint64_t pool_mask;
169599a2dd95SBruce Richardson 
169699a2dd95SBruce Richardson 	/* replay MAC address configuration including default MAC */
169799a2dd95SBruce Richardson 	addr = &dev->data->mac_addrs[0];
169899a2dd95SBruce Richardson 	if (*dev->dev_ops->mac_addr_set != NULL)
169999a2dd95SBruce Richardson 		(*dev->dev_ops->mac_addr_set)(dev, addr);
170099a2dd95SBruce Richardson 	else if (*dev->dev_ops->mac_addr_add != NULL)
170199a2dd95SBruce Richardson 		(*dev->dev_ops->mac_addr_add)(dev, addr, 0, pool);
170299a2dd95SBruce Richardson 
170399a2dd95SBruce Richardson 	if (*dev->dev_ops->mac_addr_add != NULL) {
170499a2dd95SBruce Richardson 		for (i = 1; i < dev_info->max_mac_addrs; i++) {
170599a2dd95SBruce Richardson 			addr = &dev->data->mac_addrs[i];
170699a2dd95SBruce Richardson 
170799a2dd95SBruce Richardson 			/* skip zero address */
170899a2dd95SBruce Richardson 			if (rte_is_zero_ether_addr(addr))
170999a2dd95SBruce Richardson 				continue;
171099a2dd95SBruce Richardson 
171199a2dd95SBruce Richardson 			pool = 0;
171299a2dd95SBruce Richardson 			pool_mask = dev->data->mac_pool_sel[i];
171399a2dd95SBruce Richardson 
171499a2dd95SBruce Richardson 			do {
1715e1823e08SThomas Monjalon 				if (pool_mask & UINT64_C(1))
171699a2dd95SBruce Richardson 					(*dev->dev_ops->mac_addr_add)(dev,
171799a2dd95SBruce Richardson 						addr, i, pool);
171899a2dd95SBruce Richardson 				pool_mask >>= 1;
171999a2dd95SBruce Richardson 				pool++;
172099a2dd95SBruce Richardson 			} while (pool_mask);
172199a2dd95SBruce Richardson 		}
172299a2dd95SBruce Richardson 	}
172399a2dd95SBruce Richardson }
172499a2dd95SBruce Richardson 
172599a2dd95SBruce Richardson static int
172699a2dd95SBruce Richardson eth_dev_config_restore(struct rte_eth_dev *dev,
172799a2dd95SBruce Richardson 		struct rte_eth_dev_info *dev_info, uint16_t port_id)
172899a2dd95SBruce Richardson {
172999a2dd95SBruce Richardson 	int ret;
173099a2dd95SBruce Richardson 
173199a2dd95SBruce Richardson 	if (!(*dev_info->dev_flags & RTE_ETH_DEV_NOLIVE_MAC_ADDR))
173299a2dd95SBruce Richardson 		eth_dev_mac_restore(dev, dev_info);
173399a2dd95SBruce Richardson 
173499a2dd95SBruce Richardson 	/* replay promiscuous configuration */
173599a2dd95SBruce Richardson 	/*
173699a2dd95SBruce Richardson 	 * use callbacks directly since we don't need port_id check and
173799a2dd95SBruce Richardson 	 * would like to bypass the same value set
173899a2dd95SBruce Richardson 	 */
173999a2dd95SBruce Richardson 	if (rte_eth_promiscuous_get(port_id) == 1 &&
174099a2dd95SBruce Richardson 	    *dev->dev_ops->promiscuous_enable != NULL) {
174199a2dd95SBruce Richardson 		ret = eth_err(port_id,
174299a2dd95SBruce Richardson 			      (*dev->dev_ops->promiscuous_enable)(dev));
174399a2dd95SBruce Richardson 		if (ret != 0 && ret != -ENOTSUP) {
174499a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
174599a2dd95SBruce Richardson 				"Failed to enable promiscuous mode for device (port %u): %s\n",
174699a2dd95SBruce Richardson 				port_id, rte_strerror(-ret));
174799a2dd95SBruce Richardson 			return ret;
174899a2dd95SBruce Richardson 		}
174999a2dd95SBruce Richardson 	} else if (rte_eth_promiscuous_get(port_id) == 0 &&
175099a2dd95SBruce Richardson 		   *dev->dev_ops->promiscuous_disable != NULL) {
175199a2dd95SBruce Richardson 		ret = eth_err(port_id,
175299a2dd95SBruce Richardson 			      (*dev->dev_ops->promiscuous_disable)(dev));
175399a2dd95SBruce Richardson 		if (ret != 0 && ret != -ENOTSUP) {
175499a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
175599a2dd95SBruce Richardson 				"Failed to disable promiscuous mode for device (port %u): %s\n",
175699a2dd95SBruce Richardson 				port_id, rte_strerror(-ret));
175799a2dd95SBruce Richardson 			return ret;
175899a2dd95SBruce Richardson 		}
175999a2dd95SBruce Richardson 	}
176099a2dd95SBruce Richardson 
176199a2dd95SBruce Richardson 	/* replay all multicast configuration */
176299a2dd95SBruce Richardson 	/*
176399a2dd95SBruce Richardson 	 * use callbacks directly since we don't need port_id check and
176499a2dd95SBruce Richardson 	 * would like to bypass the same value set
176599a2dd95SBruce Richardson 	 */
176699a2dd95SBruce Richardson 	if (rte_eth_allmulticast_get(port_id) == 1 &&
176799a2dd95SBruce Richardson 	    *dev->dev_ops->allmulticast_enable != NULL) {
176899a2dd95SBruce Richardson 		ret = eth_err(port_id,
176999a2dd95SBruce Richardson 			      (*dev->dev_ops->allmulticast_enable)(dev));
177099a2dd95SBruce Richardson 		if (ret != 0 && ret != -ENOTSUP) {
177199a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
177299a2dd95SBruce Richardson 				"Failed to enable allmulticast mode for device (port %u): %s\n",
177399a2dd95SBruce Richardson 				port_id, rte_strerror(-ret));
177499a2dd95SBruce Richardson 			return ret;
177599a2dd95SBruce Richardson 		}
177699a2dd95SBruce Richardson 	} else if (rte_eth_allmulticast_get(port_id) == 0 &&
177799a2dd95SBruce Richardson 		   *dev->dev_ops->allmulticast_disable != NULL) {
177899a2dd95SBruce Richardson 		ret = eth_err(port_id,
177999a2dd95SBruce Richardson 			      (*dev->dev_ops->allmulticast_disable)(dev));
178099a2dd95SBruce Richardson 		if (ret != 0 && ret != -ENOTSUP) {
178199a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
178299a2dd95SBruce Richardson 				"Failed to disable allmulticast mode for device (port %u): %s\n",
178399a2dd95SBruce Richardson 				port_id, rte_strerror(-ret));
178499a2dd95SBruce Richardson 			return ret;
178599a2dd95SBruce Richardson 		}
178699a2dd95SBruce Richardson 	}
178799a2dd95SBruce Richardson 
178899a2dd95SBruce Richardson 	return 0;
178999a2dd95SBruce Richardson }
179099a2dd95SBruce Richardson 
179199a2dd95SBruce Richardson int
179299a2dd95SBruce Richardson rte_eth_dev_start(uint16_t port_id)
179399a2dd95SBruce Richardson {
179499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
179599a2dd95SBruce Richardson 	struct rte_eth_dev_info dev_info;
179699a2dd95SBruce Richardson 	int diag;
179799a2dd95SBruce Richardson 	int ret, ret_stop;
179899a2dd95SBruce Richardson 
179999a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
180099a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
180199a2dd95SBruce Richardson 
180299a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
180399a2dd95SBruce Richardson 
180402edbfabSHuisong Li 	if (dev->data->dev_configured == 0) {
180502edbfabSHuisong Li 		RTE_ETHDEV_LOG(INFO,
180602edbfabSHuisong Li 			"Device with port_id=%"PRIu16" is not configured.\n",
180702edbfabSHuisong Li 			port_id);
180802edbfabSHuisong Li 		return -EINVAL;
180902edbfabSHuisong Li 	}
181002edbfabSHuisong Li 
181199a2dd95SBruce Richardson 	if (dev->data->dev_started != 0) {
181299a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(INFO,
181399a2dd95SBruce Richardson 			"Device with port_id=%"PRIu16" already started\n",
181499a2dd95SBruce Richardson 			port_id);
181599a2dd95SBruce Richardson 		return 0;
181699a2dd95SBruce Richardson 	}
181799a2dd95SBruce Richardson 
181899a2dd95SBruce Richardson 	ret = rte_eth_dev_info_get(port_id, &dev_info);
181999a2dd95SBruce Richardson 	if (ret != 0)
182099a2dd95SBruce Richardson 		return ret;
182199a2dd95SBruce Richardson 
182299a2dd95SBruce Richardson 	/* Lets restore MAC now if device does not support live change */
182399a2dd95SBruce Richardson 	if (*dev_info.dev_flags & RTE_ETH_DEV_NOLIVE_MAC_ADDR)
182499a2dd95SBruce Richardson 		eth_dev_mac_restore(dev, &dev_info);
182599a2dd95SBruce Richardson 
182699a2dd95SBruce Richardson 	diag = (*dev->dev_ops->dev_start)(dev);
182799a2dd95SBruce Richardson 	if (diag == 0)
182899a2dd95SBruce Richardson 		dev->data->dev_started = 1;
182999a2dd95SBruce Richardson 	else
183099a2dd95SBruce Richardson 		return eth_err(port_id, diag);
183199a2dd95SBruce Richardson 
183299a2dd95SBruce Richardson 	ret = eth_dev_config_restore(dev, &dev_info, port_id);
183399a2dd95SBruce Richardson 	if (ret != 0) {
183499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
183599a2dd95SBruce Richardson 			"Error during restoring configuration for device (port %u): %s\n",
183699a2dd95SBruce Richardson 			port_id, rte_strerror(-ret));
183799a2dd95SBruce Richardson 		ret_stop = rte_eth_dev_stop(port_id);
183899a2dd95SBruce Richardson 		if (ret_stop != 0) {
183999a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
184099a2dd95SBruce Richardson 				"Failed to stop device (port %u): %s\n",
184199a2dd95SBruce Richardson 				port_id, rte_strerror(-ret_stop));
184299a2dd95SBruce Richardson 		}
184399a2dd95SBruce Richardson 
184499a2dd95SBruce Richardson 		return ret;
184599a2dd95SBruce Richardson 	}
184699a2dd95SBruce Richardson 
184799a2dd95SBruce Richardson 	if (dev->data->dev_conf.intr_conf.lsc == 0) {
184899a2dd95SBruce Richardson 		RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->link_update, -ENOTSUP);
184999a2dd95SBruce Richardson 		(*dev->dev_ops->link_update)(dev, 0);
185099a2dd95SBruce Richardson 	}
185199a2dd95SBruce Richardson 
1852c87d435aSKonstantin Ananyev 	/* expose selection of PMD fast-path functions */
1853c87d435aSKonstantin Ananyev 	eth_dev_fp_ops_setup(rte_eth_fp_ops + port_id, dev);
1854c87d435aSKonstantin Ananyev 
185599a2dd95SBruce Richardson 	rte_ethdev_trace_start(port_id);
185699a2dd95SBruce Richardson 	return 0;
185799a2dd95SBruce Richardson }
185899a2dd95SBruce Richardson 
185999a2dd95SBruce Richardson int
186099a2dd95SBruce Richardson rte_eth_dev_stop(uint16_t port_id)
186199a2dd95SBruce Richardson {
186299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
186399a2dd95SBruce Richardson 	int ret;
186499a2dd95SBruce Richardson 
186599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
186699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
186799a2dd95SBruce Richardson 
186899a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_stop, -ENOTSUP);
186999a2dd95SBruce Richardson 
187099a2dd95SBruce Richardson 	if (dev->data->dev_started == 0) {
187199a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(INFO,
187299a2dd95SBruce Richardson 			"Device with port_id=%"PRIu16" already stopped\n",
187399a2dd95SBruce Richardson 			port_id);
187499a2dd95SBruce Richardson 		return 0;
187599a2dd95SBruce Richardson 	}
187699a2dd95SBruce Richardson 
1877c87d435aSKonstantin Ananyev 	/* point fast-path functions to dummy ones */
1878c87d435aSKonstantin Ananyev 	eth_dev_fp_ops_reset(rte_eth_fp_ops + port_id);
1879c87d435aSKonstantin Ananyev 
188099a2dd95SBruce Richardson 	dev->data->dev_started = 0;
188199a2dd95SBruce Richardson 	ret = (*dev->dev_ops->dev_stop)(dev);
188299a2dd95SBruce Richardson 	rte_ethdev_trace_stop(port_id, ret);
188399a2dd95SBruce Richardson 
188499a2dd95SBruce Richardson 	return ret;
188599a2dd95SBruce Richardson }
188699a2dd95SBruce Richardson 
188799a2dd95SBruce Richardson int
188899a2dd95SBruce Richardson rte_eth_dev_set_link_up(uint16_t port_id)
188999a2dd95SBruce Richardson {
189099a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
189199a2dd95SBruce Richardson 
189299a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
189399a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
189499a2dd95SBruce Richardson 
189599a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_set_link_up, -ENOTSUP);
189699a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->dev_set_link_up)(dev));
189799a2dd95SBruce Richardson }
189899a2dd95SBruce Richardson 
189999a2dd95SBruce Richardson int
190099a2dd95SBruce Richardson rte_eth_dev_set_link_down(uint16_t port_id)
190199a2dd95SBruce Richardson {
190299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
190399a2dd95SBruce Richardson 
190499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
190599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
190699a2dd95SBruce Richardson 
190799a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_set_link_down, -ENOTSUP);
190899a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->dev_set_link_down)(dev));
190999a2dd95SBruce Richardson }
191099a2dd95SBruce Richardson 
191199a2dd95SBruce Richardson int
191299a2dd95SBruce Richardson rte_eth_dev_close(uint16_t port_id)
191399a2dd95SBruce Richardson {
191499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
191599a2dd95SBruce Richardson 	int firsterr, binerr;
191699a2dd95SBruce Richardson 	int *lasterr = &firsterr;
191799a2dd95SBruce Richardson 
191899a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
191999a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
192099a2dd95SBruce Richardson 
1921febc855bSAndrew Rybchenko 	if (dev->data->dev_started) {
1922febc855bSAndrew Rybchenko 		RTE_ETHDEV_LOG(ERR, "Cannot close started device (port %u)\n",
1923febc855bSAndrew Rybchenko 			       port_id);
1924febc855bSAndrew Rybchenko 		return -EINVAL;
1925febc855bSAndrew Rybchenko 	}
1926febc855bSAndrew Rybchenko 
192799a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
192899a2dd95SBruce Richardson 	*lasterr = (*dev->dev_ops->dev_close)(dev);
192999a2dd95SBruce Richardson 	if (*lasterr != 0)
193099a2dd95SBruce Richardson 		lasterr = &binerr;
193199a2dd95SBruce Richardson 
193299a2dd95SBruce Richardson 	rte_ethdev_trace_close(port_id);
193399a2dd95SBruce Richardson 	*lasterr = rte_eth_dev_release_port(dev);
193499a2dd95SBruce Richardson 
193599a2dd95SBruce Richardson 	return firsterr;
193699a2dd95SBruce Richardson }
193799a2dd95SBruce Richardson 
193899a2dd95SBruce Richardson int
193999a2dd95SBruce Richardson rte_eth_dev_reset(uint16_t port_id)
194099a2dd95SBruce Richardson {
194199a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
194299a2dd95SBruce Richardson 	int ret;
194399a2dd95SBruce Richardson 
194499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
194599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
194699a2dd95SBruce Richardson 
194799a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_reset, -ENOTSUP);
194899a2dd95SBruce Richardson 
194999a2dd95SBruce Richardson 	ret = rte_eth_dev_stop(port_id);
195099a2dd95SBruce Richardson 	if (ret != 0) {
195199a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
195299a2dd95SBruce Richardson 			"Failed to stop device (port %u) before reset: %s - ignore\n",
195399a2dd95SBruce Richardson 			port_id, rte_strerror(-ret));
195499a2dd95SBruce Richardson 	}
195599a2dd95SBruce Richardson 	ret = dev->dev_ops->dev_reset(dev);
195699a2dd95SBruce Richardson 
195799a2dd95SBruce Richardson 	return eth_err(port_id, ret);
195899a2dd95SBruce Richardson }
195999a2dd95SBruce Richardson 
196099a2dd95SBruce Richardson int
196199a2dd95SBruce Richardson rte_eth_dev_is_removed(uint16_t port_id)
196299a2dd95SBruce Richardson {
196399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
196499a2dd95SBruce Richardson 	int ret;
196599a2dd95SBruce Richardson 
196699a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, 0);
196799a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
196899a2dd95SBruce Richardson 
196999a2dd95SBruce Richardson 	if (dev->state == RTE_ETH_DEV_REMOVED)
197099a2dd95SBruce Richardson 		return 1;
197199a2dd95SBruce Richardson 
197299a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->is_removed, 0);
197399a2dd95SBruce Richardson 
197499a2dd95SBruce Richardson 	ret = dev->dev_ops->is_removed(dev);
197599a2dd95SBruce Richardson 	if (ret != 0)
197699a2dd95SBruce Richardson 		/* Device is physically removed. */
197799a2dd95SBruce Richardson 		dev->state = RTE_ETH_DEV_REMOVED;
197899a2dd95SBruce Richardson 
197999a2dd95SBruce Richardson 	return ret;
198099a2dd95SBruce Richardson }
198199a2dd95SBruce Richardson 
198299a2dd95SBruce Richardson static int
198399a2dd95SBruce Richardson rte_eth_rx_queue_check_split(const struct rte_eth_rxseg_split *rx_seg,
198499a2dd95SBruce Richardson 			     uint16_t n_seg, uint32_t *mbp_buf_size,
198599a2dd95SBruce Richardson 			     const struct rte_eth_dev_info *dev_info)
198699a2dd95SBruce Richardson {
198799a2dd95SBruce Richardson 	const struct rte_eth_rxseg_capa *seg_capa = &dev_info->rx_seg_capa;
198899a2dd95SBruce Richardson 	struct rte_mempool *mp_first;
198999a2dd95SBruce Richardson 	uint32_t offset_mask;
199099a2dd95SBruce Richardson 	uint16_t seg_idx;
199199a2dd95SBruce Richardson 
199299a2dd95SBruce Richardson 	if (n_seg > seg_capa->max_nseg) {
199399a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
199499a2dd95SBruce Richardson 			       "Requested Rx segments %u exceed supported %u\n",
199599a2dd95SBruce Richardson 			       n_seg, seg_capa->max_nseg);
199699a2dd95SBruce Richardson 		return -EINVAL;
199799a2dd95SBruce Richardson 	}
199899a2dd95SBruce Richardson 	/*
199999a2dd95SBruce Richardson 	 * Check the sizes and offsets against buffer sizes
200099a2dd95SBruce Richardson 	 * for each segment specified in extended configuration.
200199a2dd95SBruce Richardson 	 */
200299a2dd95SBruce Richardson 	mp_first = rx_seg[0].mp;
2003e1823e08SThomas Monjalon 	offset_mask = RTE_BIT32(seg_capa->offset_align_log2) - 1;
200499a2dd95SBruce Richardson 	for (seg_idx = 0; seg_idx < n_seg; seg_idx++) {
200599a2dd95SBruce Richardson 		struct rte_mempool *mpl = rx_seg[seg_idx].mp;
200699a2dd95SBruce Richardson 		uint32_t length = rx_seg[seg_idx].length;
200799a2dd95SBruce Richardson 		uint32_t offset = rx_seg[seg_idx].offset;
200899a2dd95SBruce Richardson 
200999a2dd95SBruce Richardson 		if (mpl == NULL) {
201099a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR, "null mempool pointer\n");
201199a2dd95SBruce Richardson 			return -EINVAL;
201299a2dd95SBruce Richardson 		}
201399a2dd95SBruce Richardson 		if (seg_idx != 0 && mp_first != mpl &&
201499a2dd95SBruce Richardson 		    seg_capa->multi_pools == 0) {
201599a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR, "Receiving to multiple pools is not supported\n");
201699a2dd95SBruce Richardson 			return -ENOTSUP;
201799a2dd95SBruce Richardson 		}
201899a2dd95SBruce Richardson 		if (offset != 0) {
201999a2dd95SBruce Richardson 			if (seg_capa->offset_allowed == 0) {
202099a2dd95SBruce Richardson 				RTE_ETHDEV_LOG(ERR, "Rx segmentation with offset is not supported\n");
202199a2dd95SBruce Richardson 				return -ENOTSUP;
202299a2dd95SBruce Richardson 			}
202399a2dd95SBruce Richardson 			if (offset & offset_mask) {
202499a2dd95SBruce Richardson 				RTE_ETHDEV_LOG(ERR, "Rx segmentation invalid offset alignment %u, %u\n",
202599a2dd95SBruce Richardson 					       offset,
202699a2dd95SBruce Richardson 					       seg_capa->offset_align_log2);
202799a2dd95SBruce Richardson 				return -EINVAL;
202899a2dd95SBruce Richardson 			}
202999a2dd95SBruce Richardson 		}
203099a2dd95SBruce Richardson 		if (mpl->private_data_size <
203199a2dd95SBruce Richardson 			sizeof(struct rte_pktmbuf_pool_private)) {
203299a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
203399a2dd95SBruce Richardson 				       "%s private_data_size %u < %u\n",
203499a2dd95SBruce Richardson 				       mpl->name, mpl->private_data_size,
203599a2dd95SBruce Richardson 				       (unsigned int)sizeof
203699a2dd95SBruce Richardson 					(struct rte_pktmbuf_pool_private));
203799a2dd95SBruce Richardson 			return -ENOSPC;
203899a2dd95SBruce Richardson 		}
203999a2dd95SBruce Richardson 		offset += seg_idx != 0 ? 0 : RTE_PKTMBUF_HEADROOM;
204099a2dd95SBruce Richardson 		*mbp_buf_size = rte_pktmbuf_data_room_size(mpl);
204199a2dd95SBruce Richardson 		length = length != 0 ? length : *mbp_buf_size;
204299a2dd95SBruce Richardson 		if (*mbp_buf_size < length + offset) {
204399a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
204499a2dd95SBruce Richardson 				       "%s mbuf_data_room_size %u < %u (segment length=%u + segment offset=%u)\n",
204599a2dd95SBruce Richardson 				       mpl->name, *mbp_buf_size,
204699a2dd95SBruce Richardson 				       length + offset, length, offset);
204799a2dd95SBruce Richardson 			return -EINVAL;
204899a2dd95SBruce Richardson 		}
204999a2dd95SBruce Richardson 	}
205099a2dd95SBruce Richardson 	return 0;
205199a2dd95SBruce Richardson }
205299a2dd95SBruce Richardson 
205399a2dd95SBruce Richardson int
205499a2dd95SBruce Richardson rte_eth_rx_queue_setup(uint16_t port_id, uint16_t rx_queue_id,
205599a2dd95SBruce Richardson 		       uint16_t nb_rx_desc, unsigned int socket_id,
205699a2dd95SBruce Richardson 		       const struct rte_eth_rxconf *rx_conf,
205799a2dd95SBruce Richardson 		       struct rte_mempool *mp)
205899a2dd95SBruce Richardson {
205999a2dd95SBruce Richardson 	int ret;
206099a2dd95SBruce Richardson 	uint32_t mbp_buf_size;
206199a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
206299a2dd95SBruce Richardson 	struct rte_eth_dev_info dev_info;
206399a2dd95SBruce Richardson 	struct rte_eth_rxconf local_conf;
206499a2dd95SBruce Richardson 
206599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
206699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
206753ef1b34SMin Hu (Connor) 
206899a2dd95SBruce Richardson 	if (rx_queue_id >= dev->data->nb_rx_queues) {
206909fd4227SAndrew Rybchenko 		RTE_ETHDEV_LOG(ERR, "Invalid Rx queue_id=%u\n", rx_queue_id);
207099a2dd95SBruce Richardson 		return -EINVAL;
207199a2dd95SBruce Richardson 	}
207299a2dd95SBruce Richardson 
207399a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_setup, -ENOTSUP);
207499a2dd95SBruce Richardson 
207599a2dd95SBruce Richardson 	ret = rte_eth_dev_info_get(port_id, &dev_info);
207699a2dd95SBruce Richardson 	if (ret != 0)
207799a2dd95SBruce Richardson 		return ret;
207899a2dd95SBruce Richardson 
207999a2dd95SBruce Richardson 	if (mp != NULL) {
208099a2dd95SBruce Richardson 		/* Single pool configuration check. */
208199a2dd95SBruce Richardson 		if (rx_conf != NULL && rx_conf->rx_nseg != 0) {
208299a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
208399a2dd95SBruce Richardson 				       "Ambiguous segment configuration\n");
208499a2dd95SBruce Richardson 			return -EINVAL;
208599a2dd95SBruce Richardson 		}
208699a2dd95SBruce Richardson 		/*
208799a2dd95SBruce Richardson 		 * Check the size of the mbuf data buffer, this value
208899a2dd95SBruce Richardson 		 * must be provided in the private data of the memory pool.
208999a2dd95SBruce Richardson 		 * First check that the memory pool(s) has a valid private data.
209099a2dd95SBruce Richardson 		 */
209199a2dd95SBruce Richardson 		if (mp->private_data_size <
209299a2dd95SBruce Richardson 				sizeof(struct rte_pktmbuf_pool_private)) {
209399a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR, "%s private_data_size %u < %u\n",
209499a2dd95SBruce Richardson 				mp->name, mp->private_data_size,
209599a2dd95SBruce Richardson 				(unsigned int)
209699a2dd95SBruce Richardson 				sizeof(struct rte_pktmbuf_pool_private));
209799a2dd95SBruce Richardson 			return -ENOSPC;
209899a2dd95SBruce Richardson 		}
209999a2dd95SBruce Richardson 		mbp_buf_size = rte_pktmbuf_data_room_size(mp);
210099a2dd95SBruce Richardson 		if (mbp_buf_size < dev_info.min_rx_bufsize +
210199a2dd95SBruce Richardson 				   RTE_PKTMBUF_HEADROOM) {
210299a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
210399a2dd95SBruce Richardson 				       "%s mbuf_data_room_size %u < %u (RTE_PKTMBUF_HEADROOM=%u + min_rx_bufsize(dev)=%u)\n",
210499a2dd95SBruce Richardson 				       mp->name, mbp_buf_size,
210599a2dd95SBruce Richardson 				       RTE_PKTMBUF_HEADROOM +
210699a2dd95SBruce Richardson 				       dev_info.min_rx_bufsize,
210799a2dd95SBruce Richardson 				       RTE_PKTMBUF_HEADROOM,
210899a2dd95SBruce Richardson 				       dev_info.min_rx_bufsize);
210999a2dd95SBruce Richardson 			return -EINVAL;
211099a2dd95SBruce Richardson 		}
211199a2dd95SBruce Richardson 	} else {
211299a2dd95SBruce Richardson 		const struct rte_eth_rxseg_split *rx_seg;
211399a2dd95SBruce Richardson 		uint16_t n_seg;
211499a2dd95SBruce Richardson 
211599a2dd95SBruce Richardson 		/* Extended multi-segment configuration check. */
211699a2dd95SBruce Richardson 		if (rx_conf == NULL || rx_conf->rx_seg == NULL || rx_conf->rx_nseg == 0) {
211799a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
211899a2dd95SBruce Richardson 				       "Memory pool is null and no extended configuration provided\n");
211999a2dd95SBruce Richardson 			return -EINVAL;
212099a2dd95SBruce Richardson 		}
212199a2dd95SBruce Richardson 
212299a2dd95SBruce Richardson 		rx_seg = (const struct rte_eth_rxseg_split *)rx_conf->rx_seg;
212399a2dd95SBruce Richardson 		n_seg = rx_conf->rx_nseg;
212499a2dd95SBruce Richardson 
212599a2dd95SBruce Richardson 		if (rx_conf->offloads & RTE_ETH_RX_OFFLOAD_BUFFER_SPLIT) {
212699a2dd95SBruce Richardson 			ret = rte_eth_rx_queue_check_split(rx_seg, n_seg,
212799a2dd95SBruce Richardson 							   &mbp_buf_size,
212899a2dd95SBruce Richardson 							   &dev_info);
212999a2dd95SBruce Richardson 			if (ret != 0)
213099a2dd95SBruce Richardson 				return ret;
213199a2dd95SBruce Richardson 		} else {
213299a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR, "No Rx segmentation offload configured\n");
213399a2dd95SBruce Richardson 			return -EINVAL;
213499a2dd95SBruce Richardson 		}
213599a2dd95SBruce Richardson 	}
213699a2dd95SBruce Richardson 
213799a2dd95SBruce Richardson 	/* Use default specified by driver, if nb_rx_desc is zero */
213899a2dd95SBruce Richardson 	if (nb_rx_desc == 0) {
213999a2dd95SBruce Richardson 		nb_rx_desc = dev_info.default_rxportconf.ring_size;
214099a2dd95SBruce Richardson 		/* If driver default is also zero, fall back on EAL default */
214199a2dd95SBruce Richardson 		if (nb_rx_desc == 0)
214299a2dd95SBruce Richardson 			nb_rx_desc = RTE_ETH_DEV_FALLBACK_RX_RINGSIZE;
214399a2dd95SBruce Richardson 	}
214499a2dd95SBruce Richardson 
214599a2dd95SBruce Richardson 	if (nb_rx_desc > dev_info.rx_desc_lim.nb_max ||
214699a2dd95SBruce Richardson 			nb_rx_desc < dev_info.rx_desc_lim.nb_min ||
214799a2dd95SBruce Richardson 			nb_rx_desc % dev_info.rx_desc_lim.nb_align != 0) {
214899a2dd95SBruce Richardson 
214999a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
215099a2dd95SBruce Richardson 			"Invalid value for nb_rx_desc(=%hu), should be: <= %hu, >= %hu, and a product of %hu\n",
215199a2dd95SBruce Richardson 			nb_rx_desc, dev_info.rx_desc_lim.nb_max,
215299a2dd95SBruce Richardson 			dev_info.rx_desc_lim.nb_min,
215399a2dd95SBruce Richardson 			dev_info.rx_desc_lim.nb_align);
215499a2dd95SBruce Richardson 		return -EINVAL;
215599a2dd95SBruce Richardson 	}
215699a2dd95SBruce Richardson 
215799a2dd95SBruce Richardson 	if (dev->data->dev_started &&
215899a2dd95SBruce Richardson 		!(dev_info.dev_capa &
215999a2dd95SBruce Richardson 			RTE_ETH_DEV_CAPA_RUNTIME_RX_QUEUE_SETUP))
216099a2dd95SBruce Richardson 		return -EBUSY;
216199a2dd95SBruce Richardson 
216299a2dd95SBruce Richardson 	if (dev->data->dev_started &&
216399a2dd95SBruce Richardson 		(dev->data->rx_queue_state[rx_queue_id] !=
216499a2dd95SBruce Richardson 			RTE_ETH_QUEUE_STATE_STOPPED))
216599a2dd95SBruce Richardson 		return -EBUSY;
216699a2dd95SBruce Richardson 
216749ed3224SXueming Li 	eth_dev_rxq_release(dev, rx_queue_id);
216899a2dd95SBruce Richardson 
216999a2dd95SBruce Richardson 	if (rx_conf == NULL)
217099a2dd95SBruce Richardson 		rx_conf = &dev_info.default_rxconf;
217199a2dd95SBruce Richardson 
217299a2dd95SBruce Richardson 	local_conf = *rx_conf;
217399a2dd95SBruce Richardson 
217499a2dd95SBruce Richardson 	/*
217599a2dd95SBruce Richardson 	 * If an offloading has already been enabled in
217699a2dd95SBruce Richardson 	 * rte_eth_dev_configure(), it has been enabled on all queues,
217799a2dd95SBruce Richardson 	 * so there is no need to enable it in this queue again.
217899a2dd95SBruce Richardson 	 * The local_conf.offloads input to underlying PMD only carries
217999a2dd95SBruce Richardson 	 * those offloadings which are only enabled on this queue and
218099a2dd95SBruce Richardson 	 * not enabled on all queues.
218199a2dd95SBruce Richardson 	 */
218299a2dd95SBruce Richardson 	local_conf.offloads &= ~dev->data->dev_conf.rxmode.offloads;
218399a2dd95SBruce Richardson 
218499a2dd95SBruce Richardson 	/*
218599a2dd95SBruce Richardson 	 * New added offloadings for this queue are those not enabled in
218699a2dd95SBruce Richardson 	 * rte_eth_dev_configure() and they must be per-queue type.
218799a2dd95SBruce Richardson 	 * A pure per-port offloading can't be enabled on a queue while
218899a2dd95SBruce Richardson 	 * disabled on another queue. A pure per-port offloading can't
218999a2dd95SBruce Richardson 	 * be enabled for any queue as new added one if it hasn't been
219099a2dd95SBruce Richardson 	 * enabled in rte_eth_dev_configure().
219199a2dd95SBruce Richardson 	 */
219299a2dd95SBruce Richardson 	if ((local_conf.offloads & dev_info.rx_queue_offload_capa) !=
219399a2dd95SBruce Richardson 	     local_conf.offloads) {
219499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
219599a2dd95SBruce Richardson 			"Ethdev port_id=%d rx_queue_id=%d, new added offloads 0x%"PRIx64" must be "
219699a2dd95SBruce Richardson 			"within per-queue offload capabilities 0x%"PRIx64" in %s()\n",
219799a2dd95SBruce Richardson 			port_id, rx_queue_id, local_conf.offloads,
219899a2dd95SBruce Richardson 			dev_info.rx_queue_offload_capa,
219999a2dd95SBruce Richardson 			__func__);
220099a2dd95SBruce Richardson 		return -EINVAL;
220199a2dd95SBruce Richardson 	}
220299a2dd95SBruce Richardson 
2203dd22740cSXueming Li 	if (local_conf.share_group > 0 &&
2204dd22740cSXueming Li 	    (dev_info.dev_capa & RTE_ETH_DEV_CAPA_RXQ_SHARE) == 0) {
2205dd22740cSXueming Li 		RTE_ETHDEV_LOG(ERR,
2206dd22740cSXueming Li 			"Ethdev port_id=%d rx_queue_id=%d, enabled share_group=%hu while device doesn't support Rx queue share\n",
2207dd22740cSXueming Li 			port_id, rx_queue_id, local_conf.share_group);
2208dd22740cSXueming Li 		return -EINVAL;
2209dd22740cSXueming Li 	}
2210dd22740cSXueming Li 
221199a2dd95SBruce Richardson 	/*
221299a2dd95SBruce Richardson 	 * If LRO is enabled, check that the maximum aggregated packet
221399a2dd95SBruce Richardson 	 * size is supported by the configured device.
221499a2dd95SBruce Richardson 	 */
22151bb4a528SFerruh Yigit 	/* Get the real Ethernet overhead length */
2216295968d1SFerruh Yigit 	if (local_conf.offloads & RTE_ETH_RX_OFFLOAD_TCP_LRO) {
22171bb4a528SFerruh Yigit 		uint32_t overhead_len;
22181bb4a528SFerruh Yigit 		uint32_t max_rx_pktlen;
22191bb4a528SFerruh Yigit 		int ret;
22201bb4a528SFerruh Yigit 
22211bb4a528SFerruh Yigit 		overhead_len = eth_dev_get_overhead_len(dev_info.max_rx_pktlen,
22221bb4a528SFerruh Yigit 				dev_info.max_mtu);
22231bb4a528SFerruh Yigit 		max_rx_pktlen = dev->data->mtu + overhead_len;
222499a2dd95SBruce Richardson 		if (dev->data->dev_conf.rxmode.max_lro_pkt_size == 0)
22251bb4a528SFerruh Yigit 			dev->data->dev_conf.rxmode.max_lro_pkt_size = max_rx_pktlen;
22261bb4a528SFerruh Yigit 		ret = eth_dev_check_lro_pkt_size(port_id,
222799a2dd95SBruce Richardson 				dev->data->dev_conf.rxmode.max_lro_pkt_size,
22281bb4a528SFerruh Yigit 				max_rx_pktlen,
222999a2dd95SBruce Richardson 				dev_info.max_lro_pkt_size);
223099a2dd95SBruce Richardson 		if (ret != 0)
223199a2dd95SBruce Richardson 			return ret;
223299a2dd95SBruce Richardson 	}
223399a2dd95SBruce Richardson 
223499a2dd95SBruce Richardson 	ret = (*dev->dev_ops->rx_queue_setup)(dev, rx_queue_id, nb_rx_desc,
223599a2dd95SBruce Richardson 					      socket_id, &local_conf, mp);
223699a2dd95SBruce Richardson 	if (!ret) {
223799a2dd95SBruce Richardson 		if (!dev->data->min_rx_buf_size ||
223899a2dd95SBruce Richardson 		    dev->data->min_rx_buf_size > mbp_buf_size)
223999a2dd95SBruce Richardson 			dev->data->min_rx_buf_size = mbp_buf_size;
224099a2dd95SBruce Richardson 	}
224199a2dd95SBruce Richardson 
224299a2dd95SBruce Richardson 	rte_ethdev_trace_rxq_setup(port_id, rx_queue_id, nb_rx_desc, mp,
224399a2dd95SBruce Richardson 		rx_conf, ret);
224499a2dd95SBruce Richardson 	return eth_err(port_id, ret);
224599a2dd95SBruce Richardson }
224699a2dd95SBruce Richardson 
224799a2dd95SBruce Richardson int
224899a2dd95SBruce Richardson rte_eth_rx_hairpin_queue_setup(uint16_t port_id, uint16_t rx_queue_id,
224999a2dd95SBruce Richardson 			       uint16_t nb_rx_desc,
225099a2dd95SBruce Richardson 			       const struct rte_eth_hairpin_conf *conf)
225199a2dd95SBruce Richardson {
225299a2dd95SBruce Richardson 	int ret;
225399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
225499a2dd95SBruce Richardson 	struct rte_eth_hairpin_cap cap;
225599a2dd95SBruce Richardson 	int i;
225699a2dd95SBruce Richardson 	int count;
225799a2dd95SBruce Richardson 
225899a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
225999a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
226053ef1b34SMin Hu (Connor) 
226199a2dd95SBruce Richardson 	if (rx_queue_id >= dev->data->nb_rx_queues) {
226209fd4227SAndrew Rybchenko 		RTE_ETHDEV_LOG(ERR, "Invalid Rx queue_id=%u\n", rx_queue_id);
226399a2dd95SBruce Richardson 		return -EINVAL;
226499a2dd95SBruce Richardson 	}
226553ef1b34SMin Hu (Connor) 
226653ef1b34SMin Hu (Connor) 	if (conf == NULL) {
226753ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
226853ef1b34SMin Hu (Connor) 			"Cannot setup ethdev port %u Rx hairpin queue from NULL config\n",
226953ef1b34SMin Hu (Connor) 			port_id);
227053ef1b34SMin Hu (Connor) 		return -EINVAL;
227153ef1b34SMin Hu (Connor) 	}
227253ef1b34SMin Hu (Connor) 
227399a2dd95SBruce Richardson 	ret = rte_eth_dev_hairpin_capability_get(port_id, &cap);
227499a2dd95SBruce Richardson 	if (ret != 0)
227599a2dd95SBruce Richardson 		return ret;
227699a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_hairpin_queue_setup,
227799a2dd95SBruce Richardson 				-ENOTSUP);
227899a2dd95SBruce Richardson 	/* if nb_rx_desc is zero use max number of desc from the driver. */
227999a2dd95SBruce Richardson 	if (nb_rx_desc == 0)
228099a2dd95SBruce Richardson 		nb_rx_desc = cap.max_nb_desc;
228199a2dd95SBruce Richardson 	if (nb_rx_desc > cap.max_nb_desc) {
228299a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
228399a2dd95SBruce Richardson 			"Invalid value for nb_rx_desc(=%hu), should be: <= %hu",
228499a2dd95SBruce Richardson 			nb_rx_desc, cap.max_nb_desc);
228599a2dd95SBruce Richardson 		return -EINVAL;
228699a2dd95SBruce Richardson 	}
228799a2dd95SBruce Richardson 	if (conf->peer_count > cap.max_rx_2_tx) {
228899a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
228999a2dd95SBruce Richardson 			"Invalid value for number of peers for Rx queue(=%u), should be: <= %hu",
229099a2dd95SBruce Richardson 			conf->peer_count, cap.max_rx_2_tx);
229199a2dd95SBruce Richardson 		return -EINVAL;
229299a2dd95SBruce Richardson 	}
229399a2dd95SBruce Richardson 	if (conf->peer_count == 0) {
229499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
229599a2dd95SBruce Richardson 			"Invalid value for number of peers for Rx queue(=%u), should be: > 0",
229699a2dd95SBruce Richardson 			conf->peer_count);
229799a2dd95SBruce Richardson 		return -EINVAL;
229899a2dd95SBruce Richardson 	}
229999a2dd95SBruce Richardson 	for (i = 0, count = 0; i < dev->data->nb_rx_queues &&
230099a2dd95SBruce Richardson 	     cap.max_nb_queues != UINT16_MAX; i++) {
230199a2dd95SBruce Richardson 		if (i == rx_queue_id || rte_eth_dev_is_rx_hairpin_queue(dev, i))
230299a2dd95SBruce Richardson 			count++;
230399a2dd95SBruce Richardson 	}
230499a2dd95SBruce Richardson 	if (count > cap.max_nb_queues) {
230599a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "To many Rx hairpin queues max is %d",
230699a2dd95SBruce Richardson 		cap.max_nb_queues);
230799a2dd95SBruce Richardson 		return -EINVAL;
230899a2dd95SBruce Richardson 	}
230999a2dd95SBruce Richardson 	if (dev->data->dev_started)
231099a2dd95SBruce Richardson 		return -EBUSY;
231149ed3224SXueming Li 	eth_dev_rxq_release(dev, rx_queue_id);
231299a2dd95SBruce Richardson 	ret = (*dev->dev_ops->rx_hairpin_queue_setup)(dev, rx_queue_id,
231399a2dd95SBruce Richardson 						      nb_rx_desc, conf);
231499a2dd95SBruce Richardson 	if (ret == 0)
231599a2dd95SBruce Richardson 		dev->data->rx_queue_state[rx_queue_id] =
231699a2dd95SBruce Richardson 			RTE_ETH_QUEUE_STATE_HAIRPIN;
231799a2dd95SBruce Richardson 	return eth_err(port_id, ret);
231899a2dd95SBruce Richardson }
231999a2dd95SBruce Richardson 
232099a2dd95SBruce Richardson int
232199a2dd95SBruce Richardson rte_eth_tx_queue_setup(uint16_t port_id, uint16_t tx_queue_id,
232299a2dd95SBruce Richardson 		       uint16_t nb_tx_desc, unsigned int socket_id,
232399a2dd95SBruce Richardson 		       const struct rte_eth_txconf *tx_conf)
232499a2dd95SBruce Richardson {
232599a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
232699a2dd95SBruce Richardson 	struct rte_eth_dev_info dev_info;
232799a2dd95SBruce Richardson 	struct rte_eth_txconf local_conf;
232899a2dd95SBruce Richardson 	int ret;
232999a2dd95SBruce Richardson 
233099a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
233199a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
233253ef1b34SMin Hu (Connor) 
233399a2dd95SBruce Richardson 	if (tx_queue_id >= dev->data->nb_tx_queues) {
233409fd4227SAndrew Rybchenko 		RTE_ETHDEV_LOG(ERR, "Invalid Tx queue_id=%u\n", tx_queue_id);
233599a2dd95SBruce Richardson 		return -EINVAL;
233699a2dd95SBruce Richardson 	}
233799a2dd95SBruce Richardson 
233899a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_setup, -ENOTSUP);
233999a2dd95SBruce Richardson 
234099a2dd95SBruce Richardson 	ret = rte_eth_dev_info_get(port_id, &dev_info);
234199a2dd95SBruce Richardson 	if (ret != 0)
234299a2dd95SBruce Richardson 		return ret;
234399a2dd95SBruce Richardson 
234499a2dd95SBruce Richardson 	/* Use default specified by driver, if nb_tx_desc is zero */
234599a2dd95SBruce Richardson 	if (nb_tx_desc == 0) {
234699a2dd95SBruce Richardson 		nb_tx_desc = dev_info.default_txportconf.ring_size;
234799a2dd95SBruce Richardson 		/* If driver default is zero, fall back on EAL default */
234899a2dd95SBruce Richardson 		if (nb_tx_desc == 0)
234999a2dd95SBruce Richardson 			nb_tx_desc = RTE_ETH_DEV_FALLBACK_TX_RINGSIZE;
235099a2dd95SBruce Richardson 	}
235199a2dd95SBruce Richardson 	if (nb_tx_desc > dev_info.tx_desc_lim.nb_max ||
235299a2dd95SBruce Richardson 	    nb_tx_desc < dev_info.tx_desc_lim.nb_min ||
235399a2dd95SBruce Richardson 	    nb_tx_desc % dev_info.tx_desc_lim.nb_align != 0) {
235499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
235599a2dd95SBruce Richardson 			"Invalid value for nb_tx_desc(=%hu), should be: <= %hu, >= %hu, and a product of %hu\n",
235699a2dd95SBruce Richardson 			nb_tx_desc, dev_info.tx_desc_lim.nb_max,
235799a2dd95SBruce Richardson 			dev_info.tx_desc_lim.nb_min,
235899a2dd95SBruce Richardson 			dev_info.tx_desc_lim.nb_align);
235999a2dd95SBruce Richardson 		return -EINVAL;
236099a2dd95SBruce Richardson 	}
236199a2dd95SBruce Richardson 
236299a2dd95SBruce Richardson 	if (dev->data->dev_started &&
236399a2dd95SBruce Richardson 		!(dev_info.dev_capa &
236499a2dd95SBruce Richardson 			RTE_ETH_DEV_CAPA_RUNTIME_TX_QUEUE_SETUP))
236599a2dd95SBruce Richardson 		return -EBUSY;
236699a2dd95SBruce Richardson 
236799a2dd95SBruce Richardson 	if (dev->data->dev_started &&
236899a2dd95SBruce Richardson 		(dev->data->tx_queue_state[tx_queue_id] !=
236999a2dd95SBruce Richardson 			RTE_ETH_QUEUE_STATE_STOPPED))
237099a2dd95SBruce Richardson 		return -EBUSY;
237199a2dd95SBruce Richardson 
237249ed3224SXueming Li 	eth_dev_txq_release(dev, tx_queue_id);
237399a2dd95SBruce Richardson 
237499a2dd95SBruce Richardson 	if (tx_conf == NULL)
237599a2dd95SBruce Richardson 		tx_conf = &dev_info.default_txconf;
237699a2dd95SBruce Richardson 
237799a2dd95SBruce Richardson 	local_conf = *tx_conf;
237899a2dd95SBruce Richardson 
237999a2dd95SBruce Richardson 	/*
238099a2dd95SBruce Richardson 	 * If an offloading has already been enabled in
238199a2dd95SBruce Richardson 	 * rte_eth_dev_configure(), it has been enabled on all queues,
238299a2dd95SBruce Richardson 	 * so there is no need to enable it in this queue again.
238399a2dd95SBruce Richardson 	 * The local_conf.offloads input to underlying PMD only carries
238499a2dd95SBruce Richardson 	 * those offloadings which are only enabled on this queue and
238599a2dd95SBruce Richardson 	 * not enabled on all queues.
238699a2dd95SBruce Richardson 	 */
238799a2dd95SBruce Richardson 	local_conf.offloads &= ~dev->data->dev_conf.txmode.offloads;
238899a2dd95SBruce Richardson 
238999a2dd95SBruce Richardson 	/*
239099a2dd95SBruce Richardson 	 * New added offloadings for this queue are those not enabled in
239199a2dd95SBruce Richardson 	 * rte_eth_dev_configure() and they must be per-queue type.
239299a2dd95SBruce Richardson 	 * A pure per-port offloading can't be enabled on a queue while
239399a2dd95SBruce Richardson 	 * disabled on another queue. A pure per-port offloading can't
239499a2dd95SBruce Richardson 	 * be enabled for any queue as new added one if it hasn't been
239599a2dd95SBruce Richardson 	 * enabled in rte_eth_dev_configure().
239699a2dd95SBruce Richardson 	 */
239799a2dd95SBruce Richardson 	if ((local_conf.offloads & dev_info.tx_queue_offload_capa) !=
239899a2dd95SBruce Richardson 	     local_conf.offloads) {
239999a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
240099a2dd95SBruce Richardson 			"Ethdev port_id=%d tx_queue_id=%d, new added offloads 0x%"PRIx64" must be "
240199a2dd95SBruce Richardson 			"within per-queue offload capabilities 0x%"PRIx64" in %s()\n",
240299a2dd95SBruce Richardson 			port_id, tx_queue_id, local_conf.offloads,
240399a2dd95SBruce Richardson 			dev_info.tx_queue_offload_capa,
240499a2dd95SBruce Richardson 			__func__);
240599a2dd95SBruce Richardson 		return -EINVAL;
240699a2dd95SBruce Richardson 	}
240799a2dd95SBruce Richardson 
240899a2dd95SBruce Richardson 	rte_ethdev_trace_txq_setup(port_id, tx_queue_id, nb_tx_desc, tx_conf);
240999a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->tx_queue_setup)(dev,
241099a2dd95SBruce Richardson 		       tx_queue_id, nb_tx_desc, socket_id, &local_conf));
241199a2dd95SBruce Richardson }
241299a2dd95SBruce Richardson 
241399a2dd95SBruce Richardson int
241499a2dd95SBruce Richardson rte_eth_tx_hairpin_queue_setup(uint16_t port_id, uint16_t tx_queue_id,
241599a2dd95SBruce Richardson 			       uint16_t nb_tx_desc,
241699a2dd95SBruce Richardson 			       const struct rte_eth_hairpin_conf *conf)
241799a2dd95SBruce Richardson {
241899a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
241999a2dd95SBruce Richardson 	struct rte_eth_hairpin_cap cap;
242099a2dd95SBruce Richardson 	int i;
242199a2dd95SBruce Richardson 	int count;
242299a2dd95SBruce Richardson 	int ret;
242399a2dd95SBruce Richardson 
242499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
242599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
242653ef1b34SMin Hu (Connor) 
242799a2dd95SBruce Richardson 	if (tx_queue_id >= dev->data->nb_tx_queues) {
242809fd4227SAndrew Rybchenko 		RTE_ETHDEV_LOG(ERR, "Invalid Tx queue_id=%u\n", tx_queue_id);
242999a2dd95SBruce Richardson 		return -EINVAL;
243099a2dd95SBruce Richardson 	}
243153ef1b34SMin Hu (Connor) 
243253ef1b34SMin Hu (Connor) 	if (conf == NULL) {
243353ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
243453ef1b34SMin Hu (Connor) 			"Cannot setup ethdev port %u Tx hairpin queue from NULL config\n",
243553ef1b34SMin Hu (Connor) 			port_id);
243653ef1b34SMin Hu (Connor) 		return -EINVAL;
243753ef1b34SMin Hu (Connor) 	}
243853ef1b34SMin Hu (Connor) 
243999a2dd95SBruce Richardson 	ret = rte_eth_dev_hairpin_capability_get(port_id, &cap);
244099a2dd95SBruce Richardson 	if (ret != 0)
244199a2dd95SBruce Richardson 		return ret;
244299a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_hairpin_queue_setup,
244399a2dd95SBruce Richardson 				-ENOTSUP);
244499a2dd95SBruce Richardson 	/* if nb_rx_desc is zero use max number of desc from the driver. */
244599a2dd95SBruce Richardson 	if (nb_tx_desc == 0)
244699a2dd95SBruce Richardson 		nb_tx_desc = cap.max_nb_desc;
244799a2dd95SBruce Richardson 	if (nb_tx_desc > cap.max_nb_desc) {
244899a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
244999a2dd95SBruce Richardson 			"Invalid value for nb_tx_desc(=%hu), should be: <= %hu",
245099a2dd95SBruce Richardson 			nb_tx_desc, cap.max_nb_desc);
245199a2dd95SBruce Richardson 		return -EINVAL;
245299a2dd95SBruce Richardson 	}
245399a2dd95SBruce Richardson 	if (conf->peer_count > cap.max_tx_2_rx) {
245499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
245599a2dd95SBruce Richardson 			"Invalid value for number of peers for Tx queue(=%u), should be: <= %hu",
245699a2dd95SBruce Richardson 			conf->peer_count, cap.max_tx_2_rx);
245799a2dd95SBruce Richardson 		return -EINVAL;
245899a2dd95SBruce Richardson 	}
245999a2dd95SBruce Richardson 	if (conf->peer_count == 0) {
246099a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
246199a2dd95SBruce Richardson 			"Invalid value for number of peers for Tx queue(=%u), should be: > 0",
246299a2dd95SBruce Richardson 			conf->peer_count);
246399a2dd95SBruce Richardson 		return -EINVAL;
246499a2dd95SBruce Richardson 	}
246599a2dd95SBruce Richardson 	for (i = 0, count = 0; i < dev->data->nb_tx_queues &&
246699a2dd95SBruce Richardson 	     cap.max_nb_queues != UINT16_MAX; i++) {
246799a2dd95SBruce Richardson 		if (i == tx_queue_id || rte_eth_dev_is_tx_hairpin_queue(dev, i))
246899a2dd95SBruce Richardson 			count++;
246999a2dd95SBruce Richardson 	}
247099a2dd95SBruce Richardson 	if (count > cap.max_nb_queues) {
247199a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "To many Tx hairpin queues max is %d",
247299a2dd95SBruce Richardson 		cap.max_nb_queues);
247399a2dd95SBruce Richardson 		return -EINVAL;
247499a2dd95SBruce Richardson 	}
247599a2dd95SBruce Richardson 	if (dev->data->dev_started)
247699a2dd95SBruce Richardson 		return -EBUSY;
247749ed3224SXueming Li 	eth_dev_txq_release(dev, tx_queue_id);
247899a2dd95SBruce Richardson 	ret = (*dev->dev_ops->tx_hairpin_queue_setup)
247999a2dd95SBruce Richardson 		(dev, tx_queue_id, nb_tx_desc, conf);
248099a2dd95SBruce Richardson 	if (ret == 0)
248199a2dd95SBruce Richardson 		dev->data->tx_queue_state[tx_queue_id] =
248299a2dd95SBruce Richardson 			RTE_ETH_QUEUE_STATE_HAIRPIN;
248399a2dd95SBruce Richardson 	return eth_err(port_id, ret);
248499a2dd95SBruce Richardson }
248599a2dd95SBruce Richardson 
248699a2dd95SBruce Richardson int
248799a2dd95SBruce Richardson rte_eth_hairpin_bind(uint16_t tx_port, uint16_t rx_port)
248899a2dd95SBruce Richardson {
248999a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
249099a2dd95SBruce Richardson 	int ret;
249199a2dd95SBruce Richardson 
249299a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(tx_port, -ENODEV);
249399a2dd95SBruce Richardson 	dev = &rte_eth_devices[tx_port];
249453ef1b34SMin Hu (Connor) 
249599a2dd95SBruce Richardson 	if (dev->data->dev_started == 0) {
249699a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Tx port %d is not started\n", tx_port);
249799a2dd95SBruce Richardson 		return -EBUSY;
249899a2dd95SBruce Richardson 	}
249999a2dd95SBruce Richardson 
250099a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->hairpin_bind, -ENOTSUP);
250199a2dd95SBruce Richardson 	ret = (*dev->dev_ops->hairpin_bind)(dev, rx_port);
250299a2dd95SBruce Richardson 	if (ret != 0)
250399a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Failed to bind hairpin Tx %d"
250499a2dd95SBruce Richardson 			       " to Rx %d (%d - all ports)\n",
250599a2dd95SBruce Richardson 			       tx_port, rx_port, RTE_MAX_ETHPORTS);
250699a2dd95SBruce Richardson 
250799a2dd95SBruce Richardson 	return ret;
250899a2dd95SBruce Richardson }
250999a2dd95SBruce Richardson 
251099a2dd95SBruce Richardson int
251199a2dd95SBruce Richardson rte_eth_hairpin_unbind(uint16_t tx_port, uint16_t rx_port)
251299a2dd95SBruce Richardson {
251399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
251499a2dd95SBruce Richardson 	int ret;
251599a2dd95SBruce Richardson 
251699a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(tx_port, -ENODEV);
251799a2dd95SBruce Richardson 	dev = &rte_eth_devices[tx_port];
251853ef1b34SMin Hu (Connor) 
251999a2dd95SBruce Richardson 	if (dev->data->dev_started == 0) {
252099a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Tx port %d is already stopped\n", tx_port);
252199a2dd95SBruce Richardson 		return -EBUSY;
252299a2dd95SBruce Richardson 	}
252399a2dd95SBruce Richardson 
252499a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->hairpin_unbind, -ENOTSUP);
252599a2dd95SBruce Richardson 	ret = (*dev->dev_ops->hairpin_unbind)(dev, rx_port);
252699a2dd95SBruce Richardson 	if (ret != 0)
252799a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Failed to unbind hairpin Tx %d"
252899a2dd95SBruce Richardson 			       " from Rx %d (%d - all ports)\n",
252999a2dd95SBruce Richardson 			       tx_port, rx_port, RTE_MAX_ETHPORTS);
253099a2dd95SBruce Richardson 
253199a2dd95SBruce Richardson 	return ret;
253299a2dd95SBruce Richardson }
253399a2dd95SBruce Richardson 
253499a2dd95SBruce Richardson int
253599a2dd95SBruce Richardson rte_eth_hairpin_get_peer_ports(uint16_t port_id, uint16_t *peer_ports,
253699a2dd95SBruce Richardson 			       size_t len, uint32_t direction)
253799a2dd95SBruce Richardson {
253899a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
253999a2dd95SBruce Richardson 	int ret;
254099a2dd95SBruce Richardson 
254199a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
254299a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
254353ef1b34SMin Hu (Connor) 
254453ef1b34SMin Hu (Connor) 	if (peer_ports == NULL) {
254553ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
254653ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u hairpin peer ports to NULL\n",
254753ef1b34SMin Hu (Connor) 			port_id);
254853ef1b34SMin Hu (Connor) 		return -EINVAL;
254953ef1b34SMin Hu (Connor) 	}
255053ef1b34SMin Hu (Connor) 
255153ef1b34SMin Hu (Connor) 	if (len == 0) {
255253ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
255353ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u hairpin peer ports to array with zero size\n",
255453ef1b34SMin Hu (Connor) 			port_id);
255553ef1b34SMin Hu (Connor) 		return -EINVAL;
255653ef1b34SMin Hu (Connor) 	}
255753ef1b34SMin Hu (Connor) 
255899a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->hairpin_get_peer_ports,
255999a2dd95SBruce Richardson 				-ENOTSUP);
256099a2dd95SBruce Richardson 
256199a2dd95SBruce Richardson 	ret = (*dev->dev_ops->hairpin_get_peer_ports)(dev, peer_ports,
256299a2dd95SBruce Richardson 						      len, direction);
256399a2dd95SBruce Richardson 	if (ret < 0)
256499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Failed to get %d hairpin peer %s ports\n",
256599a2dd95SBruce Richardson 			       port_id, direction ? "Rx" : "Tx");
256699a2dd95SBruce Richardson 
256799a2dd95SBruce Richardson 	return ret;
256899a2dd95SBruce Richardson }
256999a2dd95SBruce Richardson 
257099a2dd95SBruce Richardson void
257199a2dd95SBruce Richardson rte_eth_tx_buffer_drop_callback(struct rte_mbuf **pkts, uint16_t unsent,
257299a2dd95SBruce Richardson 		void *userdata __rte_unused)
257399a2dd95SBruce Richardson {
257499a2dd95SBruce Richardson 	rte_pktmbuf_free_bulk(pkts, unsent);
257599a2dd95SBruce Richardson }
257699a2dd95SBruce Richardson 
257799a2dd95SBruce Richardson void
257899a2dd95SBruce Richardson rte_eth_tx_buffer_count_callback(struct rte_mbuf **pkts, uint16_t unsent,
257999a2dd95SBruce Richardson 		void *userdata)
258099a2dd95SBruce Richardson {
258199a2dd95SBruce Richardson 	uint64_t *count = userdata;
258299a2dd95SBruce Richardson 
258399a2dd95SBruce Richardson 	rte_pktmbuf_free_bulk(pkts, unsent);
258499a2dd95SBruce Richardson 	*count += unsent;
258599a2dd95SBruce Richardson }
258699a2dd95SBruce Richardson 
258799a2dd95SBruce Richardson int
258899a2dd95SBruce Richardson rte_eth_tx_buffer_set_err_callback(struct rte_eth_dev_tx_buffer *buffer,
258999a2dd95SBruce Richardson 		buffer_tx_error_fn cbfn, void *userdata)
259099a2dd95SBruce Richardson {
259153ef1b34SMin Hu (Connor) 	if (buffer == NULL) {
259253ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
259353ef1b34SMin Hu (Connor) 			"Cannot set Tx buffer error callback to NULL buffer\n");
259453ef1b34SMin Hu (Connor) 		return -EINVAL;
259553ef1b34SMin Hu (Connor) 	}
259653ef1b34SMin Hu (Connor) 
259799a2dd95SBruce Richardson 	buffer->error_callback = cbfn;
259899a2dd95SBruce Richardson 	buffer->error_userdata = userdata;
259999a2dd95SBruce Richardson 	return 0;
260099a2dd95SBruce Richardson }
260199a2dd95SBruce Richardson 
260299a2dd95SBruce Richardson int
260399a2dd95SBruce Richardson rte_eth_tx_buffer_init(struct rte_eth_dev_tx_buffer *buffer, uint16_t size)
260499a2dd95SBruce Richardson {
260599a2dd95SBruce Richardson 	int ret = 0;
260699a2dd95SBruce Richardson 
260753ef1b34SMin Hu (Connor) 	if (buffer == NULL) {
260853ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot initialize NULL buffer\n");
260999a2dd95SBruce Richardson 		return -EINVAL;
261053ef1b34SMin Hu (Connor) 	}
261199a2dd95SBruce Richardson 
261299a2dd95SBruce Richardson 	buffer->size = size;
261399a2dd95SBruce Richardson 	if (buffer->error_callback == NULL) {
261499a2dd95SBruce Richardson 		ret = rte_eth_tx_buffer_set_err_callback(
261599a2dd95SBruce Richardson 			buffer, rte_eth_tx_buffer_drop_callback, NULL);
261699a2dd95SBruce Richardson 	}
261799a2dd95SBruce Richardson 
261899a2dd95SBruce Richardson 	return ret;
261999a2dd95SBruce Richardson }
262099a2dd95SBruce Richardson 
262199a2dd95SBruce Richardson int
262299a2dd95SBruce Richardson rte_eth_tx_done_cleanup(uint16_t port_id, uint16_t queue_id, uint32_t free_cnt)
262399a2dd95SBruce Richardson {
262453ef1b34SMin Hu (Connor) 	struct rte_eth_dev *dev;
262599a2dd95SBruce Richardson 	int ret;
262699a2dd95SBruce Richardson 
262799a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
262853ef1b34SMin Hu (Connor) 	dev = &rte_eth_devices[port_id];
262953ef1b34SMin Hu (Connor) 
263099a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_done_cleanup, -ENOTSUP);
263199a2dd95SBruce Richardson 
263299a2dd95SBruce Richardson 	/* Call driver to free pending mbufs. */
263399a2dd95SBruce Richardson 	ret = (*dev->dev_ops->tx_done_cleanup)(dev->data->tx_queues[queue_id],
263499a2dd95SBruce Richardson 					       free_cnt);
263599a2dd95SBruce Richardson 	return eth_err(port_id, ret);
263699a2dd95SBruce Richardson }
263799a2dd95SBruce Richardson 
263899a2dd95SBruce Richardson int
263999a2dd95SBruce Richardson rte_eth_promiscuous_enable(uint16_t port_id)
264099a2dd95SBruce Richardson {
264199a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
264299a2dd95SBruce Richardson 	int diag = 0;
264399a2dd95SBruce Richardson 
264499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
264599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
264699a2dd95SBruce Richardson 
264799a2dd95SBruce Richardson 	if (dev->data->promiscuous == 1)
264899a2dd95SBruce Richardson 		return 0;
264999a2dd95SBruce Richardson 
265099a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->promiscuous_enable, -ENOTSUP);
265199a2dd95SBruce Richardson 
265299a2dd95SBruce Richardson 	diag = (*dev->dev_ops->promiscuous_enable)(dev);
265399a2dd95SBruce Richardson 	dev->data->promiscuous = (diag == 0) ? 1 : 0;
265499a2dd95SBruce Richardson 
265599a2dd95SBruce Richardson 	return eth_err(port_id, diag);
265699a2dd95SBruce Richardson }
265799a2dd95SBruce Richardson 
265899a2dd95SBruce Richardson int
265999a2dd95SBruce Richardson rte_eth_promiscuous_disable(uint16_t port_id)
266099a2dd95SBruce Richardson {
266199a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
266299a2dd95SBruce Richardson 	int diag = 0;
266399a2dd95SBruce Richardson 
266499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
266599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
266699a2dd95SBruce Richardson 
266799a2dd95SBruce Richardson 	if (dev->data->promiscuous == 0)
266899a2dd95SBruce Richardson 		return 0;
266999a2dd95SBruce Richardson 
267099a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->promiscuous_disable, -ENOTSUP);
267199a2dd95SBruce Richardson 
267299a2dd95SBruce Richardson 	dev->data->promiscuous = 0;
267399a2dd95SBruce Richardson 	diag = (*dev->dev_ops->promiscuous_disable)(dev);
267499a2dd95SBruce Richardson 	if (diag != 0)
267599a2dd95SBruce Richardson 		dev->data->promiscuous = 1;
267699a2dd95SBruce Richardson 
267799a2dd95SBruce Richardson 	return eth_err(port_id, diag);
267899a2dd95SBruce Richardson }
267999a2dd95SBruce Richardson 
268099a2dd95SBruce Richardson int
268199a2dd95SBruce Richardson rte_eth_promiscuous_get(uint16_t port_id)
268299a2dd95SBruce Richardson {
268399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
268499a2dd95SBruce Richardson 
268599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
268699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
268753ef1b34SMin Hu (Connor) 
268899a2dd95SBruce Richardson 	return dev->data->promiscuous;
268999a2dd95SBruce Richardson }
269099a2dd95SBruce Richardson 
269199a2dd95SBruce Richardson int
269299a2dd95SBruce Richardson rte_eth_allmulticast_enable(uint16_t port_id)
269399a2dd95SBruce Richardson {
269499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
269599a2dd95SBruce Richardson 	int diag;
269699a2dd95SBruce Richardson 
269799a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
269899a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
269999a2dd95SBruce Richardson 
270099a2dd95SBruce Richardson 	if (dev->data->all_multicast == 1)
270199a2dd95SBruce Richardson 		return 0;
270299a2dd95SBruce Richardson 
270399a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->allmulticast_enable, -ENOTSUP);
270499a2dd95SBruce Richardson 	diag = (*dev->dev_ops->allmulticast_enable)(dev);
270599a2dd95SBruce Richardson 	dev->data->all_multicast = (diag == 0) ? 1 : 0;
270699a2dd95SBruce Richardson 
270799a2dd95SBruce Richardson 	return eth_err(port_id, diag);
270899a2dd95SBruce Richardson }
270999a2dd95SBruce Richardson 
271099a2dd95SBruce Richardson int
271199a2dd95SBruce Richardson rte_eth_allmulticast_disable(uint16_t port_id)
271299a2dd95SBruce Richardson {
271399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
271499a2dd95SBruce Richardson 	int diag;
271599a2dd95SBruce Richardson 
271699a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
271799a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
271899a2dd95SBruce Richardson 
271999a2dd95SBruce Richardson 	if (dev->data->all_multicast == 0)
272099a2dd95SBruce Richardson 		return 0;
272199a2dd95SBruce Richardson 
272299a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->allmulticast_disable, -ENOTSUP);
272399a2dd95SBruce Richardson 	dev->data->all_multicast = 0;
272499a2dd95SBruce Richardson 	diag = (*dev->dev_ops->allmulticast_disable)(dev);
272599a2dd95SBruce Richardson 	if (diag != 0)
272699a2dd95SBruce Richardson 		dev->data->all_multicast = 1;
272799a2dd95SBruce Richardson 
272899a2dd95SBruce Richardson 	return eth_err(port_id, diag);
272999a2dd95SBruce Richardson }
273099a2dd95SBruce Richardson 
273199a2dd95SBruce Richardson int
273299a2dd95SBruce Richardson rte_eth_allmulticast_get(uint16_t port_id)
273399a2dd95SBruce Richardson {
273499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
273599a2dd95SBruce Richardson 
273699a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
273799a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
273853ef1b34SMin Hu (Connor) 
273999a2dd95SBruce Richardson 	return dev->data->all_multicast;
274099a2dd95SBruce Richardson }
274199a2dd95SBruce Richardson 
274299a2dd95SBruce Richardson int
274399a2dd95SBruce Richardson rte_eth_link_get(uint16_t port_id, struct rte_eth_link *eth_link)
274499a2dd95SBruce Richardson {
274599a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
274699a2dd95SBruce Richardson 
274799a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
274899a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
274999a2dd95SBruce Richardson 
275053ef1b34SMin Hu (Connor) 	if (eth_link == NULL) {
275153ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot get ethdev port %u link to NULL\n",
275253ef1b34SMin Hu (Connor) 			port_id);
275353ef1b34SMin Hu (Connor) 		return -EINVAL;
275453ef1b34SMin Hu (Connor) 	}
275553ef1b34SMin Hu (Connor) 
275653ef1b34SMin Hu (Connor) 	if (dev->data->dev_conf.intr_conf.lsc && dev->data->dev_started)
275799a2dd95SBruce Richardson 		rte_eth_linkstatus_get(dev, eth_link);
275899a2dd95SBruce Richardson 	else {
275999a2dd95SBruce Richardson 		RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->link_update, -ENOTSUP);
276099a2dd95SBruce Richardson 		(*dev->dev_ops->link_update)(dev, 1);
276199a2dd95SBruce Richardson 		*eth_link = dev->data->dev_link;
276299a2dd95SBruce Richardson 	}
276399a2dd95SBruce Richardson 
276499a2dd95SBruce Richardson 	return 0;
276599a2dd95SBruce Richardson }
276699a2dd95SBruce Richardson 
276799a2dd95SBruce Richardson int
276899a2dd95SBruce Richardson rte_eth_link_get_nowait(uint16_t port_id, struct rte_eth_link *eth_link)
276999a2dd95SBruce Richardson {
277099a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
277199a2dd95SBruce Richardson 
277299a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
277399a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
277499a2dd95SBruce Richardson 
277553ef1b34SMin Hu (Connor) 	if (eth_link == NULL) {
277653ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot get ethdev port %u link to NULL\n",
277753ef1b34SMin Hu (Connor) 			port_id);
277853ef1b34SMin Hu (Connor) 		return -EINVAL;
277953ef1b34SMin Hu (Connor) 	}
278053ef1b34SMin Hu (Connor) 
278153ef1b34SMin Hu (Connor) 	if (dev->data->dev_conf.intr_conf.lsc && dev->data->dev_started)
278299a2dd95SBruce Richardson 		rte_eth_linkstatus_get(dev, eth_link);
278399a2dd95SBruce Richardson 	else {
278499a2dd95SBruce Richardson 		RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->link_update, -ENOTSUP);
278599a2dd95SBruce Richardson 		(*dev->dev_ops->link_update)(dev, 0);
278699a2dd95SBruce Richardson 		*eth_link = dev->data->dev_link;
278799a2dd95SBruce Richardson 	}
278899a2dd95SBruce Richardson 
278999a2dd95SBruce Richardson 	return 0;
279099a2dd95SBruce Richardson }
279199a2dd95SBruce Richardson 
279299a2dd95SBruce Richardson const char *
279399a2dd95SBruce Richardson rte_eth_link_speed_to_str(uint32_t link_speed)
279499a2dd95SBruce Richardson {
279599a2dd95SBruce Richardson 	switch (link_speed) {
2796295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_NONE: return "None";
2797295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_10M:  return "10 Mbps";
2798295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_100M: return "100 Mbps";
2799295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_1G:   return "1 Gbps";
2800295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_2_5G: return "2.5 Gbps";
2801295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_5G:   return "5 Gbps";
2802295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_10G:  return "10 Gbps";
2803295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_20G:  return "20 Gbps";
2804295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_25G:  return "25 Gbps";
2805295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_40G:  return "40 Gbps";
2806295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_50G:  return "50 Gbps";
2807295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_56G:  return "56 Gbps";
2808295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_100G: return "100 Gbps";
2809295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_200G: return "200 Gbps";
2810295968d1SFerruh Yigit 	case RTE_ETH_SPEED_NUM_UNKNOWN: return "Unknown";
281199a2dd95SBruce Richardson 	default: return "Invalid";
281299a2dd95SBruce Richardson 	}
281399a2dd95SBruce Richardson }
281499a2dd95SBruce Richardson 
281599a2dd95SBruce Richardson int
281699a2dd95SBruce Richardson rte_eth_link_to_str(char *str, size_t len, const struct rte_eth_link *eth_link)
281799a2dd95SBruce Richardson {
281853ef1b34SMin Hu (Connor) 	if (str == NULL) {
281953ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot convert link to NULL string\n");
282053ef1b34SMin Hu (Connor) 		return -EINVAL;
282153ef1b34SMin Hu (Connor) 	}
282253ef1b34SMin Hu (Connor) 
282353ef1b34SMin Hu (Connor) 	if (len == 0) {
282453ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
282553ef1b34SMin Hu (Connor) 			"Cannot convert link to string with zero size\n");
282653ef1b34SMin Hu (Connor) 		return -EINVAL;
282753ef1b34SMin Hu (Connor) 	}
282853ef1b34SMin Hu (Connor) 
282953ef1b34SMin Hu (Connor) 	if (eth_link == NULL) {
283053ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot convert to string from NULL link\n");
283153ef1b34SMin Hu (Connor) 		return -EINVAL;
283253ef1b34SMin Hu (Connor) 	}
283353ef1b34SMin Hu (Connor) 
2834295968d1SFerruh Yigit 	if (eth_link->link_status == RTE_ETH_LINK_DOWN)
283599a2dd95SBruce Richardson 		return snprintf(str, len, "Link down");
283699a2dd95SBruce Richardson 	else
283799a2dd95SBruce Richardson 		return snprintf(str, len, "Link up at %s %s %s",
283899a2dd95SBruce Richardson 			rte_eth_link_speed_to_str(eth_link->link_speed),
2839295968d1SFerruh Yigit 			(eth_link->link_duplex == RTE_ETH_LINK_FULL_DUPLEX) ?
284099a2dd95SBruce Richardson 			"FDX" : "HDX",
2841295968d1SFerruh Yigit 			(eth_link->link_autoneg == RTE_ETH_LINK_AUTONEG) ?
284299a2dd95SBruce Richardson 			"Autoneg" : "Fixed");
284399a2dd95SBruce Richardson }
284499a2dd95SBruce Richardson 
284599a2dd95SBruce Richardson int
284699a2dd95SBruce Richardson rte_eth_stats_get(uint16_t port_id, struct rte_eth_stats *stats)
284799a2dd95SBruce Richardson {
284899a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
284999a2dd95SBruce Richardson 
285099a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
285199a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
285253ef1b34SMin Hu (Connor) 
285353ef1b34SMin Hu (Connor) 	if (stats == NULL) {
285453ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot get ethdev port %u stats to NULL\n",
285553ef1b34SMin Hu (Connor) 			port_id);
285653ef1b34SMin Hu (Connor) 		return -EINVAL;
285753ef1b34SMin Hu (Connor) 	}
285853ef1b34SMin Hu (Connor) 
285999a2dd95SBruce Richardson 	memset(stats, 0, sizeof(*stats));
286099a2dd95SBruce Richardson 
286199a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
286299a2dd95SBruce Richardson 	stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
286399a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->stats_get)(dev, stats));
286499a2dd95SBruce Richardson }
286599a2dd95SBruce Richardson 
286699a2dd95SBruce Richardson int
286799a2dd95SBruce Richardson rte_eth_stats_reset(uint16_t port_id)
286899a2dd95SBruce Richardson {
286999a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
287099a2dd95SBruce Richardson 	int ret;
287199a2dd95SBruce Richardson 
287299a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
287399a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
287499a2dd95SBruce Richardson 
287599a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_reset, -ENOTSUP);
287699a2dd95SBruce Richardson 	ret = (*dev->dev_ops->stats_reset)(dev);
287799a2dd95SBruce Richardson 	if (ret != 0)
287899a2dd95SBruce Richardson 		return eth_err(port_id, ret);
287999a2dd95SBruce Richardson 
288099a2dd95SBruce Richardson 	dev->data->rx_mbuf_alloc_failed = 0;
288199a2dd95SBruce Richardson 
288299a2dd95SBruce Richardson 	return 0;
288399a2dd95SBruce Richardson }
288499a2dd95SBruce Richardson 
288599a2dd95SBruce Richardson static inline int
288699a2dd95SBruce Richardson eth_dev_get_xstats_basic_count(struct rte_eth_dev *dev)
288799a2dd95SBruce Richardson {
288899a2dd95SBruce Richardson 	uint16_t nb_rxqs, nb_txqs;
288999a2dd95SBruce Richardson 	int count;
289099a2dd95SBruce Richardson 
289199a2dd95SBruce Richardson 	nb_rxqs = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
289299a2dd95SBruce Richardson 	nb_txqs = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
289399a2dd95SBruce Richardson 
289499a2dd95SBruce Richardson 	count = RTE_NB_STATS;
289599a2dd95SBruce Richardson 	if (dev->data->dev_flags & RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS) {
289699a2dd95SBruce Richardson 		count += nb_rxqs * RTE_NB_RXQ_STATS;
289799a2dd95SBruce Richardson 		count += nb_txqs * RTE_NB_TXQ_STATS;
289899a2dd95SBruce Richardson 	}
289999a2dd95SBruce Richardson 
290099a2dd95SBruce Richardson 	return count;
290199a2dd95SBruce Richardson }
290299a2dd95SBruce Richardson 
290399a2dd95SBruce Richardson static int
290499a2dd95SBruce Richardson eth_dev_get_xstats_count(uint16_t port_id)
290599a2dd95SBruce Richardson {
290699a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
290799a2dd95SBruce Richardson 	int count;
290899a2dd95SBruce Richardson 
290999a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
291099a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
291199a2dd95SBruce Richardson 	if (dev->dev_ops->xstats_get_names != NULL) {
291299a2dd95SBruce Richardson 		count = (*dev->dev_ops->xstats_get_names)(dev, NULL, 0);
291399a2dd95SBruce Richardson 		if (count < 0)
291499a2dd95SBruce Richardson 			return eth_err(port_id, count);
291599a2dd95SBruce Richardson 	} else
291699a2dd95SBruce Richardson 		count = 0;
291799a2dd95SBruce Richardson 
291899a2dd95SBruce Richardson 
291999a2dd95SBruce Richardson 	count += eth_dev_get_xstats_basic_count(dev);
292099a2dd95SBruce Richardson 
292199a2dd95SBruce Richardson 	return count;
292299a2dd95SBruce Richardson }
292399a2dd95SBruce Richardson 
292499a2dd95SBruce Richardson int
292599a2dd95SBruce Richardson rte_eth_xstats_get_id_by_name(uint16_t port_id, const char *xstat_name,
292699a2dd95SBruce Richardson 		uint64_t *id)
292799a2dd95SBruce Richardson {
292899a2dd95SBruce Richardson 	int cnt_xstats, idx_xstat;
292999a2dd95SBruce Richardson 
293099a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
293199a2dd95SBruce Richardson 
293253ef1b34SMin Hu (Connor) 	if (xstat_name == NULL) {
293353ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
293453ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u xstats ID from NULL xstat name\n",
293553ef1b34SMin Hu (Connor) 			port_id);
293699a2dd95SBruce Richardson 		return -ENOMEM;
293799a2dd95SBruce Richardson 	}
293899a2dd95SBruce Richardson 
293953ef1b34SMin Hu (Connor) 	if (id == NULL) {
294053ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
294153ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u xstats ID to NULL\n",
294253ef1b34SMin Hu (Connor) 			port_id);
294399a2dd95SBruce Richardson 		return -ENOMEM;
294499a2dd95SBruce Richardson 	}
294599a2dd95SBruce Richardson 
294699a2dd95SBruce Richardson 	/* Get count */
294799a2dd95SBruce Richardson 	cnt_xstats = rte_eth_xstats_get_names_by_id(port_id, NULL, 0, NULL);
294899a2dd95SBruce Richardson 	if (cnt_xstats  < 0) {
294999a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Cannot get count of xstats\n");
295099a2dd95SBruce Richardson 		return -ENODEV;
295199a2dd95SBruce Richardson 	}
295299a2dd95SBruce Richardson 
295399a2dd95SBruce Richardson 	/* Get id-name lookup table */
295499a2dd95SBruce Richardson 	struct rte_eth_xstat_name xstats_names[cnt_xstats];
295599a2dd95SBruce Richardson 
295699a2dd95SBruce Richardson 	if (cnt_xstats != rte_eth_xstats_get_names_by_id(
295799a2dd95SBruce Richardson 			port_id, xstats_names, cnt_xstats, NULL)) {
295899a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Cannot get xstats lookup\n");
295999a2dd95SBruce Richardson 		return -1;
296099a2dd95SBruce Richardson 	}
296199a2dd95SBruce Richardson 
296299a2dd95SBruce Richardson 	for (idx_xstat = 0; idx_xstat < cnt_xstats; idx_xstat++) {
296399a2dd95SBruce Richardson 		if (!strcmp(xstats_names[idx_xstat].name, xstat_name)) {
296499a2dd95SBruce Richardson 			*id = idx_xstat;
296599a2dd95SBruce Richardson 			return 0;
296699a2dd95SBruce Richardson 		};
296799a2dd95SBruce Richardson 	}
296899a2dd95SBruce Richardson 
296999a2dd95SBruce Richardson 	return -EINVAL;
297099a2dd95SBruce Richardson }
297199a2dd95SBruce Richardson 
297299a2dd95SBruce Richardson /* retrieve basic stats names */
297399a2dd95SBruce Richardson static int
297499a2dd95SBruce Richardson eth_basic_stats_get_names(struct rte_eth_dev *dev,
297599a2dd95SBruce Richardson 	struct rte_eth_xstat_name *xstats_names)
297699a2dd95SBruce Richardson {
297799a2dd95SBruce Richardson 	int cnt_used_entries = 0;
297899a2dd95SBruce Richardson 	uint32_t idx, id_queue;
297999a2dd95SBruce Richardson 	uint16_t num_q;
298099a2dd95SBruce Richardson 
298199a2dd95SBruce Richardson 	for (idx = 0; idx < RTE_NB_STATS; idx++) {
298299a2dd95SBruce Richardson 		strlcpy(xstats_names[cnt_used_entries].name,
298399a2dd95SBruce Richardson 			eth_dev_stats_strings[idx].name,
298499a2dd95SBruce Richardson 			sizeof(xstats_names[0].name));
298599a2dd95SBruce Richardson 		cnt_used_entries++;
298699a2dd95SBruce Richardson 	}
298799a2dd95SBruce Richardson 
298899a2dd95SBruce Richardson 	if ((dev->data->dev_flags & RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS) == 0)
298999a2dd95SBruce Richardson 		return cnt_used_entries;
299099a2dd95SBruce Richardson 
299199a2dd95SBruce Richardson 	num_q = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
299299a2dd95SBruce Richardson 	for (id_queue = 0; id_queue < num_q; id_queue++) {
299399a2dd95SBruce Richardson 		for (idx = 0; idx < RTE_NB_RXQ_STATS; idx++) {
299499a2dd95SBruce Richardson 			snprintf(xstats_names[cnt_used_entries].name,
299599a2dd95SBruce Richardson 				sizeof(xstats_names[0].name),
299699a2dd95SBruce Richardson 				"rx_q%u_%s",
299799a2dd95SBruce Richardson 				id_queue, eth_dev_rxq_stats_strings[idx].name);
299899a2dd95SBruce Richardson 			cnt_used_entries++;
299999a2dd95SBruce Richardson 		}
300099a2dd95SBruce Richardson 
300199a2dd95SBruce Richardson 	}
300299a2dd95SBruce Richardson 	num_q = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
300399a2dd95SBruce Richardson 	for (id_queue = 0; id_queue < num_q; id_queue++) {
300499a2dd95SBruce Richardson 		for (idx = 0; idx < RTE_NB_TXQ_STATS; idx++) {
300599a2dd95SBruce Richardson 			snprintf(xstats_names[cnt_used_entries].name,
300699a2dd95SBruce Richardson 				sizeof(xstats_names[0].name),
300799a2dd95SBruce Richardson 				"tx_q%u_%s",
300899a2dd95SBruce Richardson 				id_queue, eth_dev_txq_stats_strings[idx].name);
300999a2dd95SBruce Richardson 			cnt_used_entries++;
301099a2dd95SBruce Richardson 		}
301199a2dd95SBruce Richardson 	}
301299a2dd95SBruce Richardson 	return cnt_used_entries;
301399a2dd95SBruce Richardson }
301499a2dd95SBruce Richardson 
301599a2dd95SBruce Richardson /* retrieve ethdev extended statistics names */
301699a2dd95SBruce Richardson int
301799a2dd95SBruce Richardson rte_eth_xstats_get_names_by_id(uint16_t port_id,
301899a2dd95SBruce Richardson 	struct rte_eth_xstat_name *xstats_names, unsigned int size,
301999a2dd95SBruce Richardson 	uint64_t *ids)
302099a2dd95SBruce Richardson {
302199a2dd95SBruce Richardson 	struct rte_eth_xstat_name *xstats_names_copy;
302299a2dd95SBruce Richardson 	unsigned int no_basic_stat_requested = 1;
302399a2dd95SBruce Richardson 	unsigned int no_ext_stat_requested = 1;
302499a2dd95SBruce Richardson 	unsigned int expected_entries;
302599a2dd95SBruce Richardson 	unsigned int basic_count;
302699a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
302799a2dd95SBruce Richardson 	unsigned int i;
302899a2dd95SBruce Richardson 	int ret;
302999a2dd95SBruce Richardson 
303099a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
303199a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
303299a2dd95SBruce Richardson 
303399a2dd95SBruce Richardson 	basic_count = eth_dev_get_xstats_basic_count(dev);
303499a2dd95SBruce Richardson 	ret = eth_dev_get_xstats_count(port_id);
303599a2dd95SBruce Richardson 	if (ret < 0)
303699a2dd95SBruce Richardson 		return ret;
303799a2dd95SBruce Richardson 	expected_entries = (unsigned int)ret;
303899a2dd95SBruce Richardson 
303999a2dd95SBruce Richardson 	/* Return max number of stats if no ids given */
304099a2dd95SBruce Richardson 	if (!ids) {
304199a2dd95SBruce Richardson 		if (!xstats_names)
304299a2dd95SBruce Richardson 			return expected_entries;
304399a2dd95SBruce Richardson 		else if (xstats_names && size < expected_entries)
304499a2dd95SBruce Richardson 			return expected_entries;
304599a2dd95SBruce Richardson 	}
304699a2dd95SBruce Richardson 
304799a2dd95SBruce Richardson 	if (ids && !xstats_names)
304899a2dd95SBruce Richardson 		return -EINVAL;
304999a2dd95SBruce Richardson 
305099a2dd95SBruce Richardson 	if (ids && dev->dev_ops->xstats_get_names_by_id != NULL && size > 0) {
305199a2dd95SBruce Richardson 		uint64_t ids_copy[size];
305299a2dd95SBruce Richardson 
305399a2dd95SBruce Richardson 		for (i = 0; i < size; i++) {
305499a2dd95SBruce Richardson 			if (ids[i] < basic_count) {
305599a2dd95SBruce Richardson 				no_basic_stat_requested = 0;
305699a2dd95SBruce Richardson 				break;
305799a2dd95SBruce Richardson 			}
305899a2dd95SBruce Richardson 
305999a2dd95SBruce Richardson 			/*
306099a2dd95SBruce Richardson 			 * Convert ids to xstats ids that PMD knows.
306199a2dd95SBruce Richardson 			 * ids known by user are basic + extended stats.
306299a2dd95SBruce Richardson 			 */
306399a2dd95SBruce Richardson 			ids_copy[i] = ids[i] - basic_count;
306499a2dd95SBruce Richardson 		}
306599a2dd95SBruce Richardson 
306699a2dd95SBruce Richardson 		if (no_basic_stat_requested)
306799a2dd95SBruce Richardson 			return (*dev->dev_ops->xstats_get_names_by_id)(dev,
30688c9f976fSAndrew Rybchenko 					ids_copy, xstats_names, size);
306999a2dd95SBruce Richardson 	}
307099a2dd95SBruce Richardson 
307199a2dd95SBruce Richardson 	/* Retrieve all stats */
307299a2dd95SBruce Richardson 	if (!ids) {
307399a2dd95SBruce Richardson 		int num_stats = rte_eth_xstats_get_names(port_id, xstats_names,
307499a2dd95SBruce Richardson 				expected_entries);
307599a2dd95SBruce Richardson 		if (num_stats < 0 || num_stats > (int)expected_entries)
307699a2dd95SBruce Richardson 			return num_stats;
307799a2dd95SBruce Richardson 		else
307899a2dd95SBruce Richardson 			return expected_entries;
307999a2dd95SBruce Richardson 	}
308099a2dd95SBruce Richardson 
308199a2dd95SBruce Richardson 	xstats_names_copy = calloc(expected_entries,
308299a2dd95SBruce Richardson 		sizeof(struct rte_eth_xstat_name));
308399a2dd95SBruce Richardson 
308499a2dd95SBruce Richardson 	if (!xstats_names_copy) {
308599a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Can't allocate memory\n");
308699a2dd95SBruce Richardson 		return -ENOMEM;
308799a2dd95SBruce Richardson 	}
308899a2dd95SBruce Richardson 
308999a2dd95SBruce Richardson 	if (ids) {
309099a2dd95SBruce Richardson 		for (i = 0; i < size; i++) {
309199a2dd95SBruce Richardson 			if (ids[i] >= basic_count) {
309299a2dd95SBruce Richardson 				no_ext_stat_requested = 0;
309399a2dd95SBruce Richardson 				break;
309499a2dd95SBruce Richardson 			}
309599a2dd95SBruce Richardson 		}
309699a2dd95SBruce Richardson 	}
309799a2dd95SBruce Richardson 
309899a2dd95SBruce Richardson 	/* Fill xstats_names_copy structure */
309999a2dd95SBruce Richardson 	if (ids && no_ext_stat_requested) {
310099a2dd95SBruce Richardson 		eth_basic_stats_get_names(dev, xstats_names_copy);
310199a2dd95SBruce Richardson 	} else {
310299a2dd95SBruce Richardson 		ret = rte_eth_xstats_get_names(port_id, xstats_names_copy,
310399a2dd95SBruce Richardson 			expected_entries);
310499a2dd95SBruce Richardson 		if (ret < 0) {
310599a2dd95SBruce Richardson 			free(xstats_names_copy);
310699a2dd95SBruce Richardson 			return ret;
310799a2dd95SBruce Richardson 		}
310899a2dd95SBruce Richardson 	}
310999a2dd95SBruce Richardson 
311099a2dd95SBruce Richardson 	/* Filter stats */
311199a2dd95SBruce Richardson 	for (i = 0; i < size; i++) {
311299a2dd95SBruce Richardson 		if (ids[i] >= expected_entries) {
311399a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR, "Id value isn't valid\n");
311499a2dd95SBruce Richardson 			free(xstats_names_copy);
311599a2dd95SBruce Richardson 			return -1;
311699a2dd95SBruce Richardson 		}
311799a2dd95SBruce Richardson 		xstats_names[i] = xstats_names_copy[ids[i]];
311899a2dd95SBruce Richardson 	}
311999a2dd95SBruce Richardson 
312099a2dd95SBruce Richardson 	free(xstats_names_copy);
312199a2dd95SBruce Richardson 	return size;
312299a2dd95SBruce Richardson }
312399a2dd95SBruce Richardson 
312499a2dd95SBruce Richardson int
312599a2dd95SBruce Richardson rte_eth_xstats_get_names(uint16_t port_id,
312699a2dd95SBruce Richardson 	struct rte_eth_xstat_name *xstats_names,
312799a2dd95SBruce Richardson 	unsigned int size)
312899a2dd95SBruce Richardson {
312999a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
313099a2dd95SBruce Richardson 	int cnt_used_entries;
313199a2dd95SBruce Richardson 	int cnt_expected_entries;
313299a2dd95SBruce Richardson 	int cnt_driver_entries;
313399a2dd95SBruce Richardson 
313499a2dd95SBruce Richardson 	cnt_expected_entries = eth_dev_get_xstats_count(port_id);
313599a2dd95SBruce Richardson 	if (xstats_names == NULL || cnt_expected_entries < 0 ||
313699a2dd95SBruce Richardson 			(int)size < cnt_expected_entries)
313799a2dd95SBruce Richardson 		return cnt_expected_entries;
313899a2dd95SBruce Richardson 
313999a2dd95SBruce Richardson 	/* port_id checked in eth_dev_get_xstats_count() */
314099a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
314199a2dd95SBruce Richardson 
314299a2dd95SBruce Richardson 	cnt_used_entries = eth_basic_stats_get_names(dev, xstats_names);
314399a2dd95SBruce Richardson 
314499a2dd95SBruce Richardson 	if (dev->dev_ops->xstats_get_names != NULL) {
314599a2dd95SBruce Richardson 		/* If there are any driver-specific xstats, append them
314699a2dd95SBruce Richardson 		 * to end of list.
314799a2dd95SBruce Richardson 		 */
314899a2dd95SBruce Richardson 		cnt_driver_entries = (*dev->dev_ops->xstats_get_names)(
314999a2dd95SBruce Richardson 			dev,
315099a2dd95SBruce Richardson 			xstats_names + cnt_used_entries,
315199a2dd95SBruce Richardson 			size - cnt_used_entries);
315299a2dd95SBruce Richardson 		if (cnt_driver_entries < 0)
315399a2dd95SBruce Richardson 			return eth_err(port_id, cnt_driver_entries);
315499a2dd95SBruce Richardson 		cnt_used_entries += cnt_driver_entries;
315599a2dd95SBruce Richardson 	}
315699a2dd95SBruce Richardson 
315799a2dd95SBruce Richardson 	return cnt_used_entries;
315899a2dd95SBruce Richardson }
315999a2dd95SBruce Richardson 
316099a2dd95SBruce Richardson 
316199a2dd95SBruce Richardson static int
316299a2dd95SBruce Richardson eth_basic_stats_get(uint16_t port_id, struct rte_eth_xstat *xstats)
316399a2dd95SBruce Richardson {
316499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
316599a2dd95SBruce Richardson 	struct rte_eth_stats eth_stats;
316699a2dd95SBruce Richardson 	unsigned int count = 0, i, q;
316799a2dd95SBruce Richardson 	uint64_t val, *stats_ptr;
316899a2dd95SBruce Richardson 	uint16_t nb_rxqs, nb_txqs;
316999a2dd95SBruce Richardson 	int ret;
317099a2dd95SBruce Richardson 
317199a2dd95SBruce Richardson 	ret = rte_eth_stats_get(port_id, &eth_stats);
317299a2dd95SBruce Richardson 	if (ret < 0)
317399a2dd95SBruce Richardson 		return ret;
317499a2dd95SBruce Richardson 
317599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
317699a2dd95SBruce Richardson 
317799a2dd95SBruce Richardson 	nb_rxqs = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
317899a2dd95SBruce Richardson 	nb_txqs = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
317999a2dd95SBruce Richardson 
318099a2dd95SBruce Richardson 	/* global stats */
318199a2dd95SBruce Richardson 	for (i = 0; i < RTE_NB_STATS; i++) {
318299a2dd95SBruce Richardson 		stats_ptr = RTE_PTR_ADD(&eth_stats,
318399a2dd95SBruce Richardson 					eth_dev_stats_strings[i].offset);
318499a2dd95SBruce Richardson 		val = *stats_ptr;
318599a2dd95SBruce Richardson 		xstats[count++].value = val;
318699a2dd95SBruce Richardson 	}
318799a2dd95SBruce Richardson 
318899a2dd95SBruce Richardson 	if ((dev->data->dev_flags & RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS) == 0)
318999a2dd95SBruce Richardson 		return count;
319099a2dd95SBruce Richardson 
319199a2dd95SBruce Richardson 	/* per-rxq stats */
319299a2dd95SBruce Richardson 	for (q = 0; q < nb_rxqs; q++) {
319399a2dd95SBruce Richardson 		for (i = 0; i < RTE_NB_RXQ_STATS; i++) {
319499a2dd95SBruce Richardson 			stats_ptr = RTE_PTR_ADD(&eth_stats,
319599a2dd95SBruce Richardson 					eth_dev_rxq_stats_strings[i].offset +
319699a2dd95SBruce Richardson 					q * sizeof(uint64_t));
319799a2dd95SBruce Richardson 			val = *stats_ptr;
319899a2dd95SBruce Richardson 			xstats[count++].value = val;
319999a2dd95SBruce Richardson 		}
320099a2dd95SBruce Richardson 	}
320199a2dd95SBruce Richardson 
320299a2dd95SBruce Richardson 	/* per-txq stats */
320399a2dd95SBruce Richardson 	for (q = 0; q < nb_txqs; q++) {
320499a2dd95SBruce Richardson 		for (i = 0; i < RTE_NB_TXQ_STATS; i++) {
320599a2dd95SBruce Richardson 			stats_ptr = RTE_PTR_ADD(&eth_stats,
320699a2dd95SBruce Richardson 					eth_dev_txq_stats_strings[i].offset +
320799a2dd95SBruce Richardson 					q * sizeof(uint64_t));
320899a2dd95SBruce Richardson 			val = *stats_ptr;
320999a2dd95SBruce Richardson 			xstats[count++].value = val;
321099a2dd95SBruce Richardson 		}
321199a2dd95SBruce Richardson 	}
321299a2dd95SBruce Richardson 	return count;
321399a2dd95SBruce Richardson }
321499a2dd95SBruce Richardson 
321599a2dd95SBruce Richardson /* retrieve ethdev extended statistics */
321699a2dd95SBruce Richardson int
321799a2dd95SBruce Richardson rte_eth_xstats_get_by_id(uint16_t port_id, const uint64_t *ids,
321899a2dd95SBruce Richardson 			 uint64_t *values, unsigned int size)
321999a2dd95SBruce Richardson {
322099a2dd95SBruce Richardson 	unsigned int no_basic_stat_requested = 1;
322199a2dd95SBruce Richardson 	unsigned int no_ext_stat_requested = 1;
322299a2dd95SBruce Richardson 	unsigned int num_xstats_filled;
322399a2dd95SBruce Richardson 	unsigned int basic_count;
322499a2dd95SBruce Richardson 	uint16_t expected_entries;
322599a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
322699a2dd95SBruce Richardson 	unsigned int i;
322799a2dd95SBruce Richardson 	int ret;
322899a2dd95SBruce Richardson 
322999a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
323053ef1b34SMin Hu (Connor) 	dev = &rte_eth_devices[port_id];
323153ef1b34SMin Hu (Connor) 
323299a2dd95SBruce Richardson 	ret = eth_dev_get_xstats_count(port_id);
323399a2dd95SBruce Richardson 	if (ret < 0)
323499a2dd95SBruce Richardson 		return ret;
323599a2dd95SBruce Richardson 	expected_entries = (uint16_t)ret;
323699a2dd95SBruce Richardson 	struct rte_eth_xstat xstats[expected_entries];
323799a2dd95SBruce Richardson 	basic_count = eth_dev_get_xstats_basic_count(dev);
323899a2dd95SBruce Richardson 
323999a2dd95SBruce Richardson 	/* Return max number of stats if no ids given */
324099a2dd95SBruce Richardson 	if (!ids) {
324199a2dd95SBruce Richardson 		if (!values)
324299a2dd95SBruce Richardson 			return expected_entries;
324399a2dd95SBruce Richardson 		else if (values && size < expected_entries)
324499a2dd95SBruce Richardson 			return expected_entries;
324599a2dd95SBruce Richardson 	}
324699a2dd95SBruce Richardson 
324799a2dd95SBruce Richardson 	if (ids && !values)
324899a2dd95SBruce Richardson 		return -EINVAL;
324999a2dd95SBruce Richardson 
325099a2dd95SBruce Richardson 	if (ids && dev->dev_ops->xstats_get_by_id != NULL && size) {
325199a2dd95SBruce Richardson 		unsigned int basic_count = eth_dev_get_xstats_basic_count(dev);
325299a2dd95SBruce Richardson 		uint64_t ids_copy[size];
325399a2dd95SBruce Richardson 
325499a2dd95SBruce Richardson 		for (i = 0; i < size; i++) {
325599a2dd95SBruce Richardson 			if (ids[i] < basic_count) {
325699a2dd95SBruce Richardson 				no_basic_stat_requested = 0;
325799a2dd95SBruce Richardson 				break;
325899a2dd95SBruce Richardson 			}
325999a2dd95SBruce Richardson 
326099a2dd95SBruce Richardson 			/*
326199a2dd95SBruce Richardson 			 * Convert ids to xstats ids that PMD knows.
326299a2dd95SBruce Richardson 			 * ids known by user are basic + extended stats.
326399a2dd95SBruce Richardson 			 */
326499a2dd95SBruce Richardson 			ids_copy[i] = ids[i] - basic_count;
326599a2dd95SBruce Richardson 		}
326699a2dd95SBruce Richardson 
326799a2dd95SBruce Richardson 		if (no_basic_stat_requested)
326899a2dd95SBruce Richardson 			return (*dev->dev_ops->xstats_get_by_id)(dev, ids_copy,
326999a2dd95SBruce Richardson 					values, size);
327099a2dd95SBruce Richardson 	}
327199a2dd95SBruce Richardson 
327299a2dd95SBruce Richardson 	if (ids) {
327399a2dd95SBruce Richardson 		for (i = 0; i < size; i++) {
327499a2dd95SBruce Richardson 			if (ids[i] >= basic_count) {
327599a2dd95SBruce Richardson 				no_ext_stat_requested = 0;
327699a2dd95SBruce Richardson 				break;
327799a2dd95SBruce Richardson 			}
327899a2dd95SBruce Richardson 		}
327999a2dd95SBruce Richardson 	}
328099a2dd95SBruce Richardson 
328199a2dd95SBruce Richardson 	/* Fill the xstats structure */
328299a2dd95SBruce Richardson 	if (ids && no_ext_stat_requested)
328399a2dd95SBruce Richardson 		ret = eth_basic_stats_get(port_id, xstats);
328499a2dd95SBruce Richardson 	else
328599a2dd95SBruce Richardson 		ret = rte_eth_xstats_get(port_id, xstats, expected_entries);
328699a2dd95SBruce Richardson 
328799a2dd95SBruce Richardson 	if (ret < 0)
328899a2dd95SBruce Richardson 		return ret;
328999a2dd95SBruce Richardson 	num_xstats_filled = (unsigned int)ret;
329099a2dd95SBruce Richardson 
329199a2dd95SBruce Richardson 	/* Return all stats */
329299a2dd95SBruce Richardson 	if (!ids) {
329399a2dd95SBruce Richardson 		for (i = 0; i < num_xstats_filled; i++)
329499a2dd95SBruce Richardson 			values[i] = xstats[i].value;
329599a2dd95SBruce Richardson 		return expected_entries;
329699a2dd95SBruce Richardson 	}
329799a2dd95SBruce Richardson 
329899a2dd95SBruce Richardson 	/* Filter stats */
329999a2dd95SBruce Richardson 	for (i = 0; i < size; i++) {
330099a2dd95SBruce Richardson 		if (ids[i] >= expected_entries) {
330199a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR, "Id value isn't valid\n");
330299a2dd95SBruce Richardson 			return -1;
330399a2dd95SBruce Richardson 		}
330499a2dd95SBruce Richardson 		values[i] = xstats[ids[i]].value;
330599a2dd95SBruce Richardson 	}
330699a2dd95SBruce Richardson 	return size;
330799a2dd95SBruce Richardson }
330899a2dd95SBruce Richardson 
330999a2dd95SBruce Richardson int
331099a2dd95SBruce Richardson rte_eth_xstats_get(uint16_t port_id, struct rte_eth_xstat *xstats,
331199a2dd95SBruce Richardson 	unsigned int n)
331299a2dd95SBruce Richardson {
331399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
331499a2dd95SBruce Richardson 	unsigned int count = 0, i;
331599a2dd95SBruce Richardson 	signed int xcount = 0;
331699a2dd95SBruce Richardson 	uint16_t nb_rxqs, nb_txqs;
331799a2dd95SBruce Richardson 	int ret;
331899a2dd95SBruce Richardson 
331999a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
332099a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
332199a2dd95SBruce Richardson 
332299a2dd95SBruce Richardson 	nb_rxqs = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
332399a2dd95SBruce Richardson 	nb_txqs = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
332499a2dd95SBruce Richardson 
332599a2dd95SBruce Richardson 	/* Return generic statistics */
332699a2dd95SBruce Richardson 	count = RTE_NB_STATS;
332799a2dd95SBruce Richardson 	if (dev->data->dev_flags & RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS)
332899a2dd95SBruce Richardson 		count += (nb_rxqs * RTE_NB_RXQ_STATS) + (nb_txqs * RTE_NB_TXQ_STATS);
332999a2dd95SBruce Richardson 
333099a2dd95SBruce Richardson 	/* implemented by the driver */
333199a2dd95SBruce Richardson 	if (dev->dev_ops->xstats_get != NULL) {
333299a2dd95SBruce Richardson 		/* Retrieve the xstats from the driver at the end of the
333399a2dd95SBruce Richardson 		 * xstats struct.
333499a2dd95SBruce Richardson 		 */
333599a2dd95SBruce Richardson 		xcount = (*dev->dev_ops->xstats_get)(dev,
333699a2dd95SBruce Richardson 				     xstats ? xstats + count : NULL,
333799a2dd95SBruce Richardson 				     (n > count) ? n - count : 0);
333899a2dd95SBruce Richardson 
333999a2dd95SBruce Richardson 		if (xcount < 0)
334099a2dd95SBruce Richardson 			return eth_err(port_id, xcount);
334199a2dd95SBruce Richardson 	}
334299a2dd95SBruce Richardson 
334399a2dd95SBruce Richardson 	if (n < count + xcount || xstats == NULL)
334499a2dd95SBruce Richardson 		return count + xcount;
334599a2dd95SBruce Richardson 
334699a2dd95SBruce Richardson 	/* now fill the xstats structure */
334799a2dd95SBruce Richardson 	ret = eth_basic_stats_get(port_id, xstats);
334899a2dd95SBruce Richardson 	if (ret < 0)
334999a2dd95SBruce Richardson 		return ret;
335099a2dd95SBruce Richardson 	count = ret;
335199a2dd95SBruce Richardson 
335299a2dd95SBruce Richardson 	for (i = 0; i < count; i++)
335399a2dd95SBruce Richardson 		xstats[i].id = i;
335499a2dd95SBruce Richardson 	/* add an offset to driver-specific stats */
335599a2dd95SBruce Richardson 	for ( ; i < count + xcount; i++)
335699a2dd95SBruce Richardson 		xstats[i].id += count;
335799a2dd95SBruce Richardson 
335899a2dd95SBruce Richardson 	return count + xcount;
335999a2dd95SBruce Richardson }
336099a2dd95SBruce Richardson 
336199a2dd95SBruce Richardson /* reset ethdev extended statistics */
336299a2dd95SBruce Richardson int
336399a2dd95SBruce Richardson rte_eth_xstats_reset(uint16_t port_id)
336499a2dd95SBruce Richardson {
336599a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
336699a2dd95SBruce Richardson 
336799a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
336899a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
336999a2dd95SBruce Richardson 
337099a2dd95SBruce Richardson 	/* implemented by the driver */
337199a2dd95SBruce Richardson 	if (dev->dev_ops->xstats_reset != NULL)
337299a2dd95SBruce Richardson 		return eth_err(port_id, (*dev->dev_ops->xstats_reset)(dev));
337399a2dd95SBruce Richardson 
337499a2dd95SBruce Richardson 	/* fallback to default */
337599a2dd95SBruce Richardson 	return rte_eth_stats_reset(port_id);
337699a2dd95SBruce Richardson }
337799a2dd95SBruce Richardson 
337899a2dd95SBruce Richardson static int
337999a2dd95SBruce Richardson eth_dev_set_queue_stats_mapping(uint16_t port_id, uint16_t queue_id,
338099a2dd95SBruce Richardson 		uint8_t stat_idx, uint8_t is_rx)
338199a2dd95SBruce Richardson {
338299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
338399a2dd95SBruce Richardson 
338499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
338599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
338699a2dd95SBruce Richardson 
338799a2dd95SBruce Richardson 	if (is_rx && (queue_id >= dev->data->nb_rx_queues))
338899a2dd95SBruce Richardson 		return -EINVAL;
338999a2dd95SBruce Richardson 
339099a2dd95SBruce Richardson 	if (!is_rx && (queue_id >= dev->data->nb_tx_queues))
339199a2dd95SBruce Richardson 		return -EINVAL;
339299a2dd95SBruce Richardson 
339399a2dd95SBruce Richardson 	if (stat_idx >= RTE_ETHDEV_QUEUE_STAT_CNTRS)
339499a2dd95SBruce Richardson 		return -EINVAL;
339599a2dd95SBruce Richardson 
339653ef1b34SMin Hu (Connor) 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_stats_mapping_set, -ENOTSUP);
339753ef1b34SMin Hu (Connor) 	return (*dev->dev_ops->queue_stats_mapping_set) (dev, queue_id, stat_idx, is_rx);
339899a2dd95SBruce Richardson }
339999a2dd95SBruce Richardson 
340099a2dd95SBruce Richardson int
340199a2dd95SBruce Richardson rte_eth_dev_set_tx_queue_stats_mapping(uint16_t port_id, uint16_t tx_queue_id,
340299a2dd95SBruce Richardson 		uint8_t stat_idx)
340399a2dd95SBruce Richardson {
340499a2dd95SBruce Richardson 	return eth_err(port_id, eth_dev_set_queue_stats_mapping(port_id,
340599a2dd95SBruce Richardson 						tx_queue_id,
340699a2dd95SBruce Richardson 						stat_idx, STAT_QMAP_TX));
340799a2dd95SBruce Richardson }
340899a2dd95SBruce Richardson 
340999a2dd95SBruce Richardson int
341099a2dd95SBruce Richardson rte_eth_dev_set_rx_queue_stats_mapping(uint16_t port_id, uint16_t rx_queue_id,
341199a2dd95SBruce Richardson 		uint8_t stat_idx)
341299a2dd95SBruce Richardson {
341399a2dd95SBruce Richardson 	return eth_err(port_id, eth_dev_set_queue_stats_mapping(port_id,
341499a2dd95SBruce Richardson 						rx_queue_id,
341599a2dd95SBruce Richardson 						stat_idx, STAT_QMAP_RX));
341699a2dd95SBruce Richardson }
341799a2dd95SBruce Richardson 
341899a2dd95SBruce Richardson int
341999a2dd95SBruce Richardson rte_eth_dev_fw_version_get(uint16_t port_id, char *fw_version, size_t fw_size)
342099a2dd95SBruce Richardson {
342199a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
342299a2dd95SBruce Richardson 
342399a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
342499a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
342599a2dd95SBruce Richardson 
342653ef1b34SMin Hu (Connor) 	if (fw_version == NULL && fw_size > 0) {
342753ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
342853ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u FW version to NULL when string size is non zero\n",
342953ef1b34SMin Hu (Connor) 			port_id);
343053ef1b34SMin Hu (Connor) 		return -EINVAL;
343153ef1b34SMin Hu (Connor) 	}
343253ef1b34SMin Hu (Connor) 
343399a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fw_version_get, -ENOTSUP);
343499a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->fw_version_get)(dev,
343599a2dd95SBruce Richardson 							fw_version, fw_size));
343699a2dd95SBruce Richardson }
343799a2dd95SBruce Richardson 
343899a2dd95SBruce Richardson int
343999a2dd95SBruce Richardson rte_eth_dev_info_get(uint16_t port_id, struct rte_eth_dev_info *dev_info)
344099a2dd95SBruce Richardson {
344199a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
344299a2dd95SBruce Richardson 	const struct rte_eth_desc_lim lim = {
344399a2dd95SBruce Richardson 		.nb_max = UINT16_MAX,
344499a2dd95SBruce Richardson 		.nb_min = 0,
344599a2dd95SBruce Richardson 		.nb_align = 1,
344699a2dd95SBruce Richardson 		.nb_seg_max = UINT16_MAX,
344799a2dd95SBruce Richardson 		.nb_mtu_seg_max = UINT16_MAX,
344899a2dd95SBruce Richardson 	};
344999a2dd95SBruce Richardson 	int diag;
345099a2dd95SBruce Richardson 
345153ef1b34SMin Hu (Connor) 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
345253ef1b34SMin Hu (Connor) 	dev = &rte_eth_devices[port_id];
345353ef1b34SMin Hu (Connor) 
345453ef1b34SMin Hu (Connor) 	if (dev_info == NULL) {
345553ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot get ethdev port %u info to NULL\n",
345653ef1b34SMin Hu (Connor) 			port_id);
345753ef1b34SMin Hu (Connor) 		return -EINVAL;
345853ef1b34SMin Hu (Connor) 	}
345953ef1b34SMin Hu (Connor) 
346099a2dd95SBruce Richardson 	/*
346199a2dd95SBruce Richardson 	 * Init dev_info before port_id check since caller does not have
346299a2dd95SBruce Richardson 	 * return status and does not know if get is successful or not.
346399a2dd95SBruce Richardson 	 */
346499a2dd95SBruce Richardson 	memset(dev_info, 0, sizeof(struct rte_eth_dev_info));
346599a2dd95SBruce Richardson 	dev_info->switch_info.domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
346699a2dd95SBruce Richardson 
346799a2dd95SBruce Richardson 	dev_info->rx_desc_lim = lim;
346899a2dd95SBruce Richardson 	dev_info->tx_desc_lim = lim;
346999a2dd95SBruce Richardson 	dev_info->device = dev->device;
3470990912e6SFerruh Yigit 	dev_info->min_mtu = RTE_ETHER_MIN_LEN - RTE_ETHER_HDR_LEN -
3471990912e6SFerruh Yigit 		RTE_ETHER_CRC_LEN;
347299a2dd95SBruce Richardson 	dev_info->max_mtu = UINT16_MAX;
347399a2dd95SBruce Richardson 
347499a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
347599a2dd95SBruce Richardson 	diag = (*dev->dev_ops->dev_infos_get)(dev, dev_info);
347699a2dd95SBruce Richardson 	if (diag != 0) {
347799a2dd95SBruce Richardson 		/* Cleanup already filled in device information */
347899a2dd95SBruce Richardson 		memset(dev_info, 0, sizeof(struct rte_eth_dev_info));
347999a2dd95SBruce Richardson 		return eth_err(port_id, diag);
348099a2dd95SBruce Richardson 	}
348199a2dd95SBruce Richardson 
348299a2dd95SBruce Richardson 	/* Maximum number of queues should be <= RTE_MAX_QUEUES_PER_PORT */
348399a2dd95SBruce Richardson 	dev_info->max_rx_queues = RTE_MIN(dev_info->max_rx_queues,
348499a2dd95SBruce Richardson 			RTE_MAX_QUEUES_PER_PORT);
348599a2dd95SBruce Richardson 	dev_info->max_tx_queues = RTE_MIN(dev_info->max_tx_queues,
348699a2dd95SBruce Richardson 			RTE_MAX_QUEUES_PER_PORT);
348799a2dd95SBruce Richardson 
348899a2dd95SBruce Richardson 	dev_info->driver_name = dev->device->driver->name;
348999a2dd95SBruce Richardson 	dev_info->nb_rx_queues = dev->data->nb_rx_queues;
349099a2dd95SBruce Richardson 	dev_info->nb_tx_queues = dev->data->nb_tx_queues;
349199a2dd95SBruce Richardson 
349299a2dd95SBruce Richardson 	dev_info->dev_flags = &dev->data->dev_flags;
349399a2dd95SBruce Richardson 
349499a2dd95SBruce Richardson 	return 0;
349599a2dd95SBruce Richardson }
349699a2dd95SBruce Richardson 
349799a2dd95SBruce Richardson int
3498632be327SJie Wang rte_eth_dev_conf_get(uint16_t port_id, struct rte_eth_conf *dev_conf)
3499632be327SJie Wang {
3500632be327SJie Wang 	struct rte_eth_dev *dev;
3501632be327SJie Wang 
3502632be327SJie Wang 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3503632be327SJie Wang 	dev = &rte_eth_devices[port_id];
3504632be327SJie Wang 
3505632be327SJie Wang 	if (dev_conf == NULL) {
3506632be327SJie Wang 		RTE_ETHDEV_LOG(ERR,
3507632be327SJie Wang 			"Cannot get ethdev port %u configuration to NULL\n",
3508632be327SJie Wang 			port_id);
3509632be327SJie Wang 		return -EINVAL;
3510632be327SJie Wang 	}
3511632be327SJie Wang 
3512632be327SJie Wang 	memcpy(dev_conf, &dev->data->dev_conf, sizeof(struct rte_eth_conf));
3513632be327SJie Wang 
3514632be327SJie Wang 	return 0;
3515632be327SJie Wang }
3516632be327SJie Wang 
3517632be327SJie Wang int
351899a2dd95SBruce Richardson rte_eth_dev_get_supported_ptypes(uint16_t port_id, uint32_t ptype_mask,
351999a2dd95SBruce Richardson 				 uint32_t *ptypes, int num)
352099a2dd95SBruce Richardson {
352199a2dd95SBruce Richardson 	int i, j;
352299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
352399a2dd95SBruce Richardson 	const uint32_t *all_ptypes;
352499a2dd95SBruce Richardson 
352599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
352699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
352753ef1b34SMin Hu (Connor) 
352853ef1b34SMin Hu (Connor) 	if (ptypes == NULL && num > 0) {
352953ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
353053ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u supported packet types to NULL when array size is non zero\n",
353153ef1b34SMin Hu (Connor) 			port_id);
353253ef1b34SMin Hu (Connor) 		return -EINVAL;
353353ef1b34SMin Hu (Connor) 	}
353453ef1b34SMin Hu (Connor) 
353599a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_supported_ptypes_get, 0);
353699a2dd95SBruce Richardson 	all_ptypes = (*dev->dev_ops->dev_supported_ptypes_get)(dev);
353799a2dd95SBruce Richardson 
353899a2dd95SBruce Richardson 	if (!all_ptypes)
353999a2dd95SBruce Richardson 		return 0;
354099a2dd95SBruce Richardson 
354199a2dd95SBruce Richardson 	for (i = 0, j = 0; all_ptypes[i] != RTE_PTYPE_UNKNOWN; ++i)
354299a2dd95SBruce Richardson 		if (all_ptypes[i] & ptype_mask) {
354399a2dd95SBruce Richardson 			if (j < num)
354499a2dd95SBruce Richardson 				ptypes[j] = all_ptypes[i];
354599a2dd95SBruce Richardson 			j++;
354699a2dd95SBruce Richardson 		}
354799a2dd95SBruce Richardson 
354899a2dd95SBruce Richardson 	return j;
354999a2dd95SBruce Richardson }
355099a2dd95SBruce Richardson 
355199a2dd95SBruce Richardson int
355299a2dd95SBruce Richardson rte_eth_dev_set_ptypes(uint16_t port_id, uint32_t ptype_mask,
355399a2dd95SBruce Richardson 				 uint32_t *set_ptypes, unsigned int num)
355499a2dd95SBruce Richardson {
355599a2dd95SBruce Richardson 	const uint32_t valid_ptype_masks[] = {
355699a2dd95SBruce Richardson 		RTE_PTYPE_L2_MASK,
355799a2dd95SBruce Richardson 		RTE_PTYPE_L3_MASK,
355899a2dd95SBruce Richardson 		RTE_PTYPE_L4_MASK,
355999a2dd95SBruce Richardson 		RTE_PTYPE_TUNNEL_MASK,
356099a2dd95SBruce Richardson 		RTE_PTYPE_INNER_L2_MASK,
356199a2dd95SBruce Richardson 		RTE_PTYPE_INNER_L3_MASK,
356299a2dd95SBruce Richardson 		RTE_PTYPE_INNER_L4_MASK,
356399a2dd95SBruce Richardson 	};
356499a2dd95SBruce Richardson 	const uint32_t *all_ptypes;
356599a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
356699a2dd95SBruce Richardson 	uint32_t unused_mask;
356799a2dd95SBruce Richardson 	unsigned int i, j;
356899a2dd95SBruce Richardson 	int ret;
356999a2dd95SBruce Richardson 
357099a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
357199a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
357299a2dd95SBruce Richardson 
357353ef1b34SMin Hu (Connor) 	if (num > 0 && set_ptypes == NULL) {
357453ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
357553ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u set packet types to NULL when array size is non zero\n",
357653ef1b34SMin Hu (Connor) 			port_id);
357799a2dd95SBruce Richardson 		return -EINVAL;
357853ef1b34SMin Hu (Connor) 	}
357999a2dd95SBruce Richardson 
358099a2dd95SBruce Richardson 	if (*dev->dev_ops->dev_supported_ptypes_get == NULL ||
358199a2dd95SBruce Richardson 			*dev->dev_ops->dev_ptypes_set == NULL) {
358299a2dd95SBruce Richardson 		ret = 0;
358399a2dd95SBruce Richardson 		goto ptype_unknown;
358499a2dd95SBruce Richardson 	}
358599a2dd95SBruce Richardson 
358699a2dd95SBruce Richardson 	if (ptype_mask == 0) {
358799a2dd95SBruce Richardson 		ret = (*dev->dev_ops->dev_ptypes_set)(dev,
358899a2dd95SBruce Richardson 				ptype_mask);
358999a2dd95SBruce Richardson 		goto ptype_unknown;
359099a2dd95SBruce Richardson 	}
359199a2dd95SBruce Richardson 
359299a2dd95SBruce Richardson 	unused_mask = ptype_mask;
359399a2dd95SBruce Richardson 	for (i = 0; i < RTE_DIM(valid_ptype_masks); i++) {
359499a2dd95SBruce Richardson 		uint32_t mask = ptype_mask & valid_ptype_masks[i];
359599a2dd95SBruce Richardson 		if (mask && mask != valid_ptype_masks[i]) {
359699a2dd95SBruce Richardson 			ret = -EINVAL;
359799a2dd95SBruce Richardson 			goto ptype_unknown;
359899a2dd95SBruce Richardson 		}
359999a2dd95SBruce Richardson 		unused_mask &= ~valid_ptype_masks[i];
360099a2dd95SBruce Richardson 	}
360199a2dd95SBruce Richardson 
360299a2dd95SBruce Richardson 	if (unused_mask) {
360399a2dd95SBruce Richardson 		ret = -EINVAL;
360499a2dd95SBruce Richardson 		goto ptype_unknown;
360599a2dd95SBruce Richardson 	}
360699a2dd95SBruce Richardson 
360799a2dd95SBruce Richardson 	all_ptypes = (*dev->dev_ops->dev_supported_ptypes_get)(dev);
360899a2dd95SBruce Richardson 	if (all_ptypes == NULL) {
360999a2dd95SBruce Richardson 		ret = 0;
361099a2dd95SBruce Richardson 		goto ptype_unknown;
361199a2dd95SBruce Richardson 	}
361299a2dd95SBruce Richardson 
361399a2dd95SBruce Richardson 	/*
361499a2dd95SBruce Richardson 	 * Accommodate as many set_ptypes as possible. If the supplied
361599a2dd95SBruce Richardson 	 * set_ptypes array is insufficient fill it partially.
361699a2dd95SBruce Richardson 	 */
361799a2dd95SBruce Richardson 	for (i = 0, j = 0; set_ptypes != NULL &&
361899a2dd95SBruce Richardson 				(all_ptypes[i] != RTE_PTYPE_UNKNOWN); ++i) {
361999a2dd95SBruce Richardson 		if (ptype_mask & all_ptypes[i]) {
362099a2dd95SBruce Richardson 			if (j < num - 1) {
362199a2dd95SBruce Richardson 				set_ptypes[j] = all_ptypes[i];
362299a2dd95SBruce Richardson 				j++;
362399a2dd95SBruce Richardson 				continue;
362499a2dd95SBruce Richardson 			}
362599a2dd95SBruce Richardson 			break;
362699a2dd95SBruce Richardson 		}
362799a2dd95SBruce Richardson 	}
362899a2dd95SBruce Richardson 
362999a2dd95SBruce Richardson 	if (set_ptypes != NULL && j < num)
363099a2dd95SBruce Richardson 		set_ptypes[j] = RTE_PTYPE_UNKNOWN;
363199a2dd95SBruce Richardson 
363299a2dd95SBruce Richardson 	return (*dev->dev_ops->dev_ptypes_set)(dev, ptype_mask);
363399a2dd95SBruce Richardson 
363499a2dd95SBruce Richardson ptype_unknown:
363599a2dd95SBruce Richardson 	if (num > 0)
363699a2dd95SBruce Richardson 		set_ptypes[0] = RTE_PTYPE_UNKNOWN;
363799a2dd95SBruce Richardson 
363899a2dd95SBruce Richardson 	return ret;
363999a2dd95SBruce Richardson }
364099a2dd95SBruce Richardson 
364199a2dd95SBruce Richardson int
364227a300e6SKonstantin Ananyev rte_eth_macaddrs_get(uint16_t port_id, struct rte_ether_addr *ma,
364327a300e6SKonstantin Ananyev 	unsigned int num)
364427a300e6SKonstantin Ananyev {
364527a300e6SKonstantin Ananyev 	int32_t ret;
364627a300e6SKonstantin Ananyev 	struct rte_eth_dev *dev;
364727a300e6SKonstantin Ananyev 	struct rte_eth_dev_info dev_info;
364827a300e6SKonstantin Ananyev 
364927a300e6SKonstantin Ananyev 	if (ma == NULL) {
365027a300e6SKonstantin Ananyev 		RTE_ETHDEV_LOG(ERR, "%s: invalid parameters\n", __func__);
365127a300e6SKonstantin Ananyev 		return -EINVAL;
365227a300e6SKonstantin Ananyev 	}
365327a300e6SKonstantin Ananyev 
365427a300e6SKonstantin Ananyev 	/* will check for us that port_id is a valid one */
365527a300e6SKonstantin Ananyev 	ret = rte_eth_dev_info_get(port_id, &dev_info);
365627a300e6SKonstantin Ananyev 	if (ret != 0)
365727a300e6SKonstantin Ananyev 		return ret;
365827a300e6SKonstantin Ananyev 
365927a300e6SKonstantin Ananyev 	dev = &rte_eth_devices[port_id];
366027a300e6SKonstantin Ananyev 	num = RTE_MIN(dev_info.max_mac_addrs, num);
366127a300e6SKonstantin Ananyev 	memcpy(ma, dev->data->mac_addrs, num * sizeof(ma[0]));
366227a300e6SKonstantin Ananyev 
366327a300e6SKonstantin Ananyev 	return num;
366427a300e6SKonstantin Ananyev }
366527a300e6SKonstantin Ananyev 
366627a300e6SKonstantin Ananyev int
366799a2dd95SBruce Richardson rte_eth_macaddr_get(uint16_t port_id, struct rte_ether_addr *mac_addr)
366899a2dd95SBruce Richardson {
366999a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
367099a2dd95SBruce Richardson 
367199a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
367299a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
367353ef1b34SMin Hu (Connor) 
367453ef1b34SMin Hu (Connor) 	if (mac_addr == NULL) {
367553ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
367653ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u MAC address to NULL\n",
367753ef1b34SMin Hu (Connor) 			port_id);
367853ef1b34SMin Hu (Connor) 		return -EINVAL;
367953ef1b34SMin Hu (Connor) 	}
368053ef1b34SMin Hu (Connor) 
368199a2dd95SBruce Richardson 	rte_ether_addr_copy(&dev->data->mac_addrs[0], mac_addr);
368299a2dd95SBruce Richardson 
368399a2dd95SBruce Richardson 	return 0;
368499a2dd95SBruce Richardson }
368599a2dd95SBruce Richardson 
368699a2dd95SBruce Richardson int
368799a2dd95SBruce Richardson rte_eth_dev_get_mtu(uint16_t port_id, uint16_t *mtu)
368899a2dd95SBruce Richardson {
368999a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
369099a2dd95SBruce Richardson 
369199a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
369299a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
369353ef1b34SMin Hu (Connor) 
369453ef1b34SMin Hu (Connor) 	if (mtu == NULL) {
369553ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot get ethdev port %u MTU to NULL\n",
369653ef1b34SMin Hu (Connor) 			port_id);
369753ef1b34SMin Hu (Connor) 		return -EINVAL;
369853ef1b34SMin Hu (Connor) 	}
369953ef1b34SMin Hu (Connor) 
370099a2dd95SBruce Richardson 	*mtu = dev->data->mtu;
370199a2dd95SBruce Richardson 	return 0;
370299a2dd95SBruce Richardson }
370399a2dd95SBruce Richardson 
370499a2dd95SBruce Richardson int
370599a2dd95SBruce Richardson rte_eth_dev_set_mtu(uint16_t port_id, uint16_t mtu)
370699a2dd95SBruce Richardson {
370799a2dd95SBruce Richardson 	int ret;
370899a2dd95SBruce Richardson 	struct rte_eth_dev_info dev_info;
370999a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
371099a2dd95SBruce Richardson 
371199a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
371299a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
371399a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mtu_set, -ENOTSUP);
371499a2dd95SBruce Richardson 
371599a2dd95SBruce Richardson 	/*
371699a2dd95SBruce Richardson 	 * Check if the device supports dev_infos_get, if it does not
371799a2dd95SBruce Richardson 	 * skip min_mtu/max_mtu validation here as this requires values
371899a2dd95SBruce Richardson 	 * that are populated within the call to rte_eth_dev_info_get()
371999a2dd95SBruce Richardson 	 * which relies on dev->dev_ops->dev_infos_get.
372099a2dd95SBruce Richardson 	 */
372199a2dd95SBruce Richardson 	if (*dev->dev_ops->dev_infos_get != NULL) {
372299a2dd95SBruce Richardson 		ret = rte_eth_dev_info_get(port_id, &dev_info);
372399a2dd95SBruce Richardson 		if (ret != 0)
372499a2dd95SBruce Richardson 			return ret;
372599a2dd95SBruce Richardson 
3726990912e6SFerruh Yigit 		ret = eth_dev_validate_mtu(port_id, &dev_info, mtu);
3727990912e6SFerruh Yigit 		if (ret != 0)
3728990912e6SFerruh Yigit 			return ret;
372999a2dd95SBruce Richardson 	}
373099a2dd95SBruce Richardson 
3731b26bee10SIvan Ilchenko 	if (dev->data->dev_configured == 0) {
3732b26bee10SIvan Ilchenko 		RTE_ETHDEV_LOG(ERR,
3733b26bee10SIvan Ilchenko 			"Port %u must be configured before MTU set\n",
3734b26bee10SIvan Ilchenko 			port_id);
3735b26bee10SIvan Ilchenko 		return -EINVAL;
3736b26bee10SIvan Ilchenko 	}
3737b26bee10SIvan Ilchenko 
373899a2dd95SBruce Richardson 	ret = (*dev->dev_ops->mtu_set)(dev, mtu);
3739b563c142SFerruh Yigit 	if (ret == 0)
374099a2dd95SBruce Richardson 		dev->data->mtu = mtu;
374199a2dd95SBruce Richardson 
374299a2dd95SBruce Richardson 	return eth_err(port_id, ret);
374399a2dd95SBruce Richardson }
374499a2dd95SBruce Richardson 
374599a2dd95SBruce Richardson int
374699a2dd95SBruce Richardson rte_eth_dev_vlan_filter(uint16_t port_id, uint16_t vlan_id, int on)
374799a2dd95SBruce Richardson {
374899a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
374999a2dd95SBruce Richardson 	int ret;
375099a2dd95SBruce Richardson 
375199a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
375299a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
375353ef1b34SMin Hu (Connor) 
375499a2dd95SBruce Richardson 	if (!(dev->data->dev_conf.rxmode.offloads &
3755295968d1SFerruh Yigit 	      RTE_ETH_RX_OFFLOAD_VLAN_FILTER)) {
37565b49ba65SAndrew Rybchenko 		RTE_ETHDEV_LOG(ERR, "Port %u: VLAN-filtering disabled\n",
375799a2dd95SBruce Richardson 			port_id);
375899a2dd95SBruce Richardson 		return -ENOSYS;
375999a2dd95SBruce Richardson 	}
376099a2dd95SBruce Richardson 
376199a2dd95SBruce Richardson 	if (vlan_id > 4095) {
376299a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Port_id=%u invalid vlan_id=%u > 4095\n",
376399a2dd95SBruce Richardson 			port_id, vlan_id);
376499a2dd95SBruce Richardson 		return -EINVAL;
376599a2dd95SBruce Richardson 	}
376699a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_filter_set, -ENOTSUP);
376799a2dd95SBruce Richardson 
376899a2dd95SBruce Richardson 	ret = (*dev->dev_ops->vlan_filter_set)(dev, vlan_id, on);
376999a2dd95SBruce Richardson 	if (ret == 0) {
377099a2dd95SBruce Richardson 		struct rte_vlan_filter_conf *vfc;
377199a2dd95SBruce Richardson 		int vidx;
377299a2dd95SBruce Richardson 		int vbit;
377399a2dd95SBruce Richardson 
377499a2dd95SBruce Richardson 		vfc = &dev->data->vlan_filter_conf;
377599a2dd95SBruce Richardson 		vidx = vlan_id / 64;
377699a2dd95SBruce Richardson 		vbit = vlan_id % 64;
377799a2dd95SBruce Richardson 
377899a2dd95SBruce Richardson 		if (on)
3779e1823e08SThomas Monjalon 			vfc->ids[vidx] |= RTE_BIT64(vbit);
378099a2dd95SBruce Richardson 		else
3781e1823e08SThomas Monjalon 			vfc->ids[vidx] &= ~RTE_BIT64(vbit);
378299a2dd95SBruce Richardson 	}
378399a2dd95SBruce Richardson 
378499a2dd95SBruce Richardson 	return eth_err(port_id, ret);
378599a2dd95SBruce Richardson }
378699a2dd95SBruce Richardson 
378799a2dd95SBruce Richardson int
378899a2dd95SBruce Richardson rte_eth_dev_set_vlan_strip_on_queue(uint16_t port_id, uint16_t rx_queue_id,
378999a2dd95SBruce Richardson 				    int on)
379099a2dd95SBruce Richardson {
379199a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
379299a2dd95SBruce Richardson 
379399a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
379499a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
379553ef1b34SMin Hu (Connor) 
379699a2dd95SBruce Richardson 	if (rx_queue_id >= dev->data->nb_rx_queues) {
379799a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid rx_queue_id=%u\n", rx_queue_id);
379899a2dd95SBruce Richardson 		return -EINVAL;
379999a2dd95SBruce Richardson 	}
380099a2dd95SBruce Richardson 
380199a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_strip_queue_set, -ENOTSUP);
380299a2dd95SBruce Richardson 	(*dev->dev_ops->vlan_strip_queue_set)(dev, rx_queue_id, on);
380399a2dd95SBruce Richardson 
380499a2dd95SBruce Richardson 	return 0;
380599a2dd95SBruce Richardson }
380699a2dd95SBruce Richardson 
380799a2dd95SBruce Richardson int
380899a2dd95SBruce Richardson rte_eth_dev_set_vlan_ether_type(uint16_t port_id,
380999a2dd95SBruce Richardson 				enum rte_vlan_type vlan_type,
381099a2dd95SBruce Richardson 				uint16_t tpid)
381199a2dd95SBruce Richardson {
381299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
381399a2dd95SBruce Richardson 
381499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
381599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
381699a2dd95SBruce Richardson 
381753ef1b34SMin Hu (Connor) 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_tpid_set, -ENOTSUP);
381899a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->vlan_tpid_set)(dev, vlan_type,
381999a2dd95SBruce Richardson 							       tpid));
382099a2dd95SBruce Richardson }
382199a2dd95SBruce Richardson 
382299a2dd95SBruce Richardson int
382399a2dd95SBruce Richardson rte_eth_dev_set_vlan_offload(uint16_t port_id, int offload_mask)
382499a2dd95SBruce Richardson {
382599a2dd95SBruce Richardson 	struct rte_eth_dev_info dev_info;
382699a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
382799a2dd95SBruce Richardson 	int ret = 0;
382899a2dd95SBruce Richardson 	int mask = 0;
382999a2dd95SBruce Richardson 	int cur, org = 0;
383099a2dd95SBruce Richardson 	uint64_t orig_offloads;
383199a2dd95SBruce Richardson 	uint64_t dev_offloads;
383299a2dd95SBruce Richardson 	uint64_t new_offloads;
383399a2dd95SBruce Richardson 
383499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
383599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
383699a2dd95SBruce Richardson 
383799a2dd95SBruce Richardson 	/* save original values in case of failure */
383899a2dd95SBruce Richardson 	orig_offloads = dev->data->dev_conf.rxmode.offloads;
383999a2dd95SBruce Richardson 	dev_offloads = orig_offloads;
384099a2dd95SBruce Richardson 
384199a2dd95SBruce Richardson 	/* check which option changed by application */
3842295968d1SFerruh Yigit 	cur = !!(offload_mask & RTE_ETH_VLAN_STRIP_OFFLOAD);
3843295968d1SFerruh Yigit 	org = !!(dev_offloads & RTE_ETH_RX_OFFLOAD_VLAN_STRIP);
384499a2dd95SBruce Richardson 	if (cur != org) {
384599a2dd95SBruce Richardson 		if (cur)
3846295968d1SFerruh Yigit 			dev_offloads |= RTE_ETH_RX_OFFLOAD_VLAN_STRIP;
384799a2dd95SBruce Richardson 		else
3848295968d1SFerruh Yigit 			dev_offloads &= ~RTE_ETH_RX_OFFLOAD_VLAN_STRIP;
3849295968d1SFerruh Yigit 		mask |= RTE_ETH_VLAN_STRIP_MASK;
385099a2dd95SBruce Richardson 	}
385199a2dd95SBruce Richardson 
3852295968d1SFerruh Yigit 	cur = !!(offload_mask & RTE_ETH_VLAN_FILTER_OFFLOAD);
3853295968d1SFerruh Yigit 	org = !!(dev_offloads & RTE_ETH_RX_OFFLOAD_VLAN_FILTER);
385499a2dd95SBruce Richardson 	if (cur != org) {
385599a2dd95SBruce Richardson 		if (cur)
3856295968d1SFerruh Yigit 			dev_offloads |= RTE_ETH_RX_OFFLOAD_VLAN_FILTER;
385799a2dd95SBruce Richardson 		else
3858295968d1SFerruh Yigit 			dev_offloads &= ~RTE_ETH_RX_OFFLOAD_VLAN_FILTER;
3859295968d1SFerruh Yigit 		mask |= RTE_ETH_VLAN_FILTER_MASK;
386099a2dd95SBruce Richardson 	}
386199a2dd95SBruce Richardson 
3862295968d1SFerruh Yigit 	cur = !!(offload_mask & RTE_ETH_VLAN_EXTEND_OFFLOAD);
3863295968d1SFerruh Yigit 	org = !!(dev_offloads & RTE_ETH_RX_OFFLOAD_VLAN_EXTEND);
386499a2dd95SBruce Richardson 	if (cur != org) {
386599a2dd95SBruce Richardson 		if (cur)
3866295968d1SFerruh Yigit 			dev_offloads |= RTE_ETH_RX_OFFLOAD_VLAN_EXTEND;
386799a2dd95SBruce Richardson 		else
3868295968d1SFerruh Yigit 			dev_offloads &= ~RTE_ETH_RX_OFFLOAD_VLAN_EXTEND;
3869295968d1SFerruh Yigit 		mask |= RTE_ETH_VLAN_EXTEND_MASK;
387099a2dd95SBruce Richardson 	}
387199a2dd95SBruce Richardson 
3872295968d1SFerruh Yigit 	cur = !!(offload_mask & RTE_ETH_QINQ_STRIP_OFFLOAD);
3873295968d1SFerruh Yigit 	org = !!(dev_offloads & RTE_ETH_RX_OFFLOAD_QINQ_STRIP);
387499a2dd95SBruce Richardson 	if (cur != org) {
387599a2dd95SBruce Richardson 		if (cur)
3876295968d1SFerruh Yigit 			dev_offloads |= RTE_ETH_RX_OFFLOAD_QINQ_STRIP;
387799a2dd95SBruce Richardson 		else
3878295968d1SFerruh Yigit 			dev_offloads &= ~RTE_ETH_RX_OFFLOAD_QINQ_STRIP;
3879295968d1SFerruh Yigit 		mask |= RTE_ETH_QINQ_STRIP_MASK;
388099a2dd95SBruce Richardson 	}
388199a2dd95SBruce Richardson 
388299a2dd95SBruce Richardson 	/*no change*/
388399a2dd95SBruce Richardson 	if (mask == 0)
388499a2dd95SBruce Richardson 		return ret;
388599a2dd95SBruce Richardson 
388699a2dd95SBruce Richardson 	ret = rte_eth_dev_info_get(port_id, &dev_info);
388799a2dd95SBruce Richardson 	if (ret != 0)
388899a2dd95SBruce Richardson 		return ret;
388999a2dd95SBruce Richardson 
389099a2dd95SBruce Richardson 	/* Rx VLAN offloading must be within its device capabilities */
389199a2dd95SBruce Richardson 	if ((dev_offloads & dev_info.rx_offload_capa) != dev_offloads) {
389299a2dd95SBruce Richardson 		new_offloads = dev_offloads & ~orig_offloads;
389399a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
389499a2dd95SBruce Richardson 			"Ethdev port_id=%u requested new added VLAN offloads "
389599a2dd95SBruce Richardson 			"0x%" PRIx64 " must be within Rx offloads capabilities "
389699a2dd95SBruce Richardson 			"0x%" PRIx64 " in %s()\n",
389799a2dd95SBruce Richardson 			port_id, new_offloads, dev_info.rx_offload_capa,
389899a2dd95SBruce Richardson 			__func__);
389999a2dd95SBruce Richardson 		return -EINVAL;
390099a2dd95SBruce Richardson 	}
390199a2dd95SBruce Richardson 
390299a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_offload_set, -ENOTSUP);
390399a2dd95SBruce Richardson 	dev->data->dev_conf.rxmode.offloads = dev_offloads;
390499a2dd95SBruce Richardson 	ret = (*dev->dev_ops->vlan_offload_set)(dev, mask);
390599a2dd95SBruce Richardson 	if (ret) {
390699a2dd95SBruce Richardson 		/* hit an error restore  original values */
390799a2dd95SBruce Richardson 		dev->data->dev_conf.rxmode.offloads = orig_offloads;
390899a2dd95SBruce Richardson 	}
390999a2dd95SBruce Richardson 
391099a2dd95SBruce Richardson 	return eth_err(port_id, ret);
391199a2dd95SBruce Richardson }
391299a2dd95SBruce Richardson 
391399a2dd95SBruce Richardson int
391499a2dd95SBruce Richardson rte_eth_dev_get_vlan_offload(uint16_t port_id)
391599a2dd95SBruce Richardson {
391699a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
391799a2dd95SBruce Richardson 	uint64_t *dev_offloads;
391899a2dd95SBruce Richardson 	int ret = 0;
391999a2dd95SBruce Richardson 
392099a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
392199a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
392299a2dd95SBruce Richardson 	dev_offloads = &dev->data->dev_conf.rxmode.offloads;
392399a2dd95SBruce Richardson 
3924295968d1SFerruh Yigit 	if (*dev_offloads & RTE_ETH_RX_OFFLOAD_VLAN_STRIP)
3925295968d1SFerruh Yigit 		ret |= RTE_ETH_VLAN_STRIP_OFFLOAD;
392699a2dd95SBruce Richardson 
3927295968d1SFerruh Yigit 	if (*dev_offloads & RTE_ETH_RX_OFFLOAD_VLAN_FILTER)
3928295968d1SFerruh Yigit 		ret |= RTE_ETH_VLAN_FILTER_OFFLOAD;
392999a2dd95SBruce Richardson 
3930295968d1SFerruh Yigit 	if (*dev_offloads & RTE_ETH_RX_OFFLOAD_VLAN_EXTEND)
3931295968d1SFerruh Yigit 		ret |= RTE_ETH_VLAN_EXTEND_OFFLOAD;
393299a2dd95SBruce Richardson 
3933295968d1SFerruh Yigit 	if (*dev_offloads & RTE_ETH_RX_OFFLOAD_QINQ_STRIP)
3934295968d1SFerruh Yigit 		ret |= RTE_ETH_QINQ_STRIP_OFFLOAD;
393599a2dd95SBruce Richardson 
393699a2dd95SBruce Richardson 	return ret;
393799a2dd95SBruce Richardson }
393899a2dd95SBruce Richardson 
393999a2dd95SBruce Richardson int
394099a2dd95SBruce Richardson rte_eth_dev_set_vlan_pvid(uint16_t port_id, uint16_t pvid, int on)
394199a2dd95SBruce Richardson {
394299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
394399a2dd95SBruce Richardson 
394499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
394599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
394699a2dd95SBruce Richardson 
394753ef1b34SMin Hu (Connor) 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_pvid_set, -ENOTSUP);
394899a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->vlan_pvid_set)(dev, pvid, on));
394999a2dd95SBruce Richardson }
395099a2dd95SBruce Richardson 
395199a2dd95SBruce Richardson int
395299a2dd95SBruce Richardson rte_eth_dev_flow_ctrl_get(uint16_t port_id, struct rte_eth_fc_conf *fc_conf)
395399a2dd95SBruce Richardson {
395499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
395599a2dd95SBruce Richardson 
395699a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
395799a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
395853ef1b34SMin Hu (Connor) 
395953ef1b34SMin Hu (Connor) 	if (fc_conf == NULL) {
396053ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
396153ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u flow control config to NULL\n",
396253ef1b34SMin Hu (Connor) 			port_id);
396353ef1b34SMin Hu (Connor) 		return -EINVAL;
396453ef1b34SMin Hu (Connor) 	}
396553ef1b34SMin Hu (Connor) 
396699a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_get, -ENOTSUP);
396799a2dd95SBruce Richardson 	memset(fc_conf, 0, sizeof(*fc_conf));
396899a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->flow_ctrl_get)(dev, fc_conf));
396999a2dd95SBruce Richardson }
397099a2dd95SBruce Richardson 
397199a2dd95SBruce Richardson int
397299a2dd95SBruce Richardson rte_eth_dev_flow_ctrl_set(uint16_t port_id, struct rte_eth_fc_conf *fc_conf)
397399a2dd95SBruce Richardson {
397499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
397599a2dd95SBruce Richardson 
397699a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
397753ef1b34SMin Hu (Connor) 	dev = &rte_eth_devices[port_id];
397853ef1b34SMin Hu (Connor) 
397953ef1b34SMin Hu (Connor) 	if (fc_conf == NULL) {
398053ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
398153ef1b34SMin Hu (Connor) 			"Cannot set ethdev port %u flow control from NULL config\n",
398253ef1b34SMin Hu (Connor) 			port_id);
398353ef1b34SMin Hu (Connor) 		return -EINVAL;
398453ef1b34SMin Hu (Connor) 	}
398553ef1b34SMin Hu (Connor) 
398699a2dd95SBruce Richardson 	if ((fc_conf->send_xon != 0) && (fc_conf->send_xon != 1)) {
398799a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid send_xon, only 0/1 allowed\n");
398899a2dd95SBruce Richardson 		return -EINVAL;
398999a2dd95SBruce Richardson 	}
399099a2dd95SBruce Richardson 
399199a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_set, -ENOTSUP);
399299a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->flow_ctrl_set)(dev, fc_conf));
399399a2dd95SBruce Richardson }
399499a2dd95SBruce Richardson 
399599a2dd95SBruce Richardson int
399699a2dd95SBruce Richardson rte_eth_dev_priority_flow_ctrl_set(uint16_t port_id,
399799a2dd95SBruce Richardson 				   struct rte_eth_pfc_conf *pfc_conf)
399899a2dd95SBruce Richardson {
399999a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
400099a2dd95SBruce Richardson 
400199a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
400253ef1b34SMin Hu (Connor) 	dev = &rte_eth_devices[port_id];
400353ef1b34SMin Hu (Connor) 
400453ef1b34SMin Hu (Connor) 	if (pfc_conf == NULL) {
400553ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
400653ef1b34SMin Hu (Connor) 			"Cannot set ethdev port %u priority flow control from NULL config\n",
400753ef1b34SMin Hu (Connor) 			port_id);
400853ef1b34SMin Hu (Connor) 		return -EINVAL;
400953ef1b34SMin Hu (Connor) 	}
401053ef1b34SMin Hu (Connor) 
4011295968d1SFerruh Yigit 	if (pfc_conf->priority > (RTE_ETH_DCB_NUM_USER_PRIORITIES - 1)) {
401299a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid priority, only 0-7 allowed\n");
401399a2dd95SBruce Richardson 		return -EINVAL;
401499a2dd95SBruce Richardson 	}
401599a2dd95SBruce Richardson 
401699a2dd95SBruce Richardson 	/* High water, low water validation are device specific */
401799a2dd95SBruce Richardson 	if  (*dev->dev_ops->priority_flow_ctrl_set)
401899a2dd95SBruce Richardson 		return eth_err(port_id, (*dev->dev_ops->priority_flow_ctrl_set)
401999a2dd95SBruce Richardson 					(dev, pfc_conf));
402099a2dd95SBruce Richardson 	return -ENOTSUP;
402199a2dd95SBruce Richardson }
402299a2dd95SBruce Richardson 
402399a2dd95SBruce Richardson static int
402499a2dd95SBruce Richardson eth_check_reta_mask(struct rte_eth_rss_reta_entry64 *reta_conf,
402599a2dd95SBruce Richardson 			uint16_t reta_size)
402699a2dd95SBruce Richardson {
402799a2dd95SBruce Richardson 	uint16_t i, num;
402899a2dd95SBruce Richardson 
4029295968d1SFerruh Yigit 	num = (reta_size + RTE_ETH_RETA_GROUP_SIZE - 1) / RTE_ETH_RETA_GROUP_SIZE;
403099a2dd95SBruce Richardson 	for (i = 0; i < num; i++) {
403199a2dd95SBruce Richardson 		if (reta_conf[i].mask)
403299a2dd95SBruce Richardson 			return 0;
403399a2dd95SBruce Richardson 	}
403499a2dd95SBruce Richardson 
403599a2dd95SBruce Richardson 	return -EINVAL;
403699a2dd95SBruce Richardson }
403799a2dd95SBruce Richardson 
403899a2dd95SBruce Richardson static int
403999a2dd95SBruce Richardson eth_check_reta_entry(struct rte_eth_rss_reta_entry64 *reta_conf,
404099a2dd95SBruce Richardson 			 uint16_t reta_size,
404199a2dd95SBruce Richardson 			 uint16_t max_rxq)
404299a2dd95SBruce Richardson {
404399a2dd95SBruce Richardson 	uint16_t i, idx, shift;
404499a2dd95SBruce Richardson 
404599a2dd95SBruce Richardson 	if (max_rxq == 0) {
404699a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "No receive queue is available\n");
404799a2dd95SBruce Richardson 		return -EINVAL;
404899a2dd95SBruce Richardson 	}
404999a2dd95SBruce Richardson 
405099a2dd95SBruce Richardson 	for (i = 0; i < reta_size; i++) {
4051295968d1SFerruh Yigit 		idx = i / RTE_ETH_RETA_GROUP_SIZE;
4052295968d1SFerruh Yigit 		shift = i % RTE_ETH_RETA_GROUP_SIZE;
4053e1823e08SThomas Monjalon 		if ((reta_conf[idx].mask & RTE_BIT64(shift)) &&
405499a2dd95SBruce Richardson 			(reta_conf[idx].reta[shift] >= max_rxq)) {
405599a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
405699a2dd95SBruce Richardson 				"reta_conf[%u]->reta[%u]: %u exceeds the maximum rxq index: %u\n",
405799a2dd95SBruce Richardson 				idx, shift,
405899a2dd95SBruce Richardson 				reta_conf[idx].reta[shift], max_rxq);
405999a2dd95SBruce Richardson 			return -EINVAL;
406099a2dd95SBruce Richardson 		}
406199a2dd95SBruce Richardson 	}
406299a2dd95SBruce Richardson 
406399a2dd95SBruce Richardson 	return 0;
406499a2dd95SBruce Richardson }
406599a2dd95SBruce Richardson 
406699a2dd95SBruce Richardson int
406799a2dd95SBruce Richardson rte_eth_dev_rss_reta_update(uint16_t port_id,
406899a2dd95SBruce Richardson 			    struct rte_eth_rss_reta_entry64 *reta_conf,
406999a2dd95SBruce Richardson 			    uint16_t reta_size)
407099a2dd95SBruce Richardson {
407199a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
407299a2dd95SBruce Richardson 	int ret;
407399a2dd95SBruce Richardson 
407499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
407553ef1b34SMin Hu (Connor) 	dev = &rte_eth_devices[port_id];
407653ef1b34SMin Hu (Connor) 
407753ef1b34SMin Hu (Connor) 	if (reta_conf == NULL) {
407853ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
407953ef1b34SMin Hu (Connor) 			"Cannot update ethdev port %u RSS RETA to NULL\n",
408053ef1b34SMin Hu (Connor) 			port_id);
408153ef1b34SMin Hu (Connor) 		return -EINVAL;
408253ef1b34SMin Hu (Connor) 	}
408353ef1b34SMin Hu (Connor) 
408453ef1b34SMin Hu (Connor) 	if (reta_size == 0) {
408553ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
408653ef1b34SMin Hu (Connor) 			"Cannot update ethdev port %u RSS RETA with zero size\n",
408753ef1b34SMin Hu (Connor) 			port_id);
408853ef1b34SMin Hu (Connor) 		return -EINVAL;
408953ef1b34SMin Hu (Connor) 	}
409053ef1b34SMin Hu (Connor) 
409199a2dd95SBruce Richardson 	/* Check mask bits */
409299a2dd95SBruce Richardson 	ret = eth_check_reta_mask(reta_conf, reta_size);
409399a2dd95SBruce Richardson 	if (ret < 0)
409499a2dd95SBruce Richardson 		return ret;
409599a2dd95SBruce Richardson 
409699a2dd95SBruce Richardson 	/* Check entry value */
409799a2dd95SBruce Richardson 	ret = eth_check_reta_entry(reta_conf, reta_size,
409899a2dd95SBruce Richardson 				dev->data->nb_rx_queues);
409999a2dd95SBruce Richardson 	if (ret < 0)
410099a2dd95SBruce Richardson 		return ret;
410199a2dd95SBruce Richardson 
410299a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->reta_update, -ENOTSUP);
410399a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->reta_update)(dev, reta_conf,
410499a2dd95SBruce Richardson 							     reta_size));
410599a2dd95SBruce Richardson }
410699a2dd95SBruce Richardson 
410799a2dd95SBruce Richardson int
410899a2dd95SBruce Richardson rte_eth_dev_rss_reta_query(uint16_t port_id,
410999a2dd95SBruce Richardson 			   struct rte_eth_rss_reta_entry64 *reta_conf,
411099a2dd95SBruce Richardson 			   uint16_t reta_size)
411199a2dd95SBruce Richardson {
411299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
411399a2dd95SBruce Richardson 	int ret;
411499a2dd95SBruce Richardson 
411599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
411653ef1b34SMin Hu (Connor) 	dev = &rte_eth_devices[port_id];
411753ef1b34SMin Hu (Connor) 
411853ef1b34SMin Hu (Connor) 	if (reta_conf == NULL) {
411953ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
412053ef1b34SMin Hu (Connor) 			"Cannot query ethdev port %u RSS RETA from NULL config\n",
412153ef1b34SMin Hu (Connor) 			port_id);
412253ef1b34SMin Hu (Connor) 		return -EINVAL;
412353ef1b34SMin Hu (Connor) 	}
412499a2dd95SBruce Richardson 
412599a2dd95SBruce Richardson 	/* Check mask bits */
412699a2dd95SBruce Richardson 	ret = eth_check_reta_mask(reta_conf, reta_size);
412799a2dd95SBruce Richardson 	if (ret < 0)
412899a2dd95SBruce Richardson 		return ret;
412999a2dd95SBruce Richardson 
413099a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->reta_query, -ENOTSUP);
413199a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->reta_query)(dev, reta_conf,
413299a2dd95SBruce Richardson 							    reta_size));
413399a2dd95SBruce Richardson }
413499a2dd95SBruce Richardson 
413599a2dd95SBruce Richardson int
413699a2dd95SBruce Richardson rte_eth_dev_rss_hash_update(uint16_t port_id,
413799a2dd95SBruce Richardson 			    struct rte_eth_rss_conf *rss_conf)
413899a2dd95SBruce Richardson {
413999a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
414099a2dd95SBruce Richardson 	struct rte_eth_dev_info dev_info = { .flow_type_rss_offloads = 0, };
414199a2dd95SBruce Richardson 	int ret;
414299a2dd95SBruce Richardson 
414399a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
414453ef1b34SMin Hu (Connor) 	dev = &rte_eth_devices[port_id];
414553ef1b34SMin Hu (Connor) 
414653ef1b34SMin Hu (Connor) 	if (rss_conf == NULL) {
414753ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
414853ef1b34SMin Hu (Connor) 			"Cannot update ethdev port %u RSS hash from NULL config\n",
414953ef1b34SMin Hu (Connor) 			port_id);
415053ef1b34SMin Hu (Connor) 		return -EINVAL;
415153ef1b34SMin Hu (Connor) 	}
415299a2dd95SBruce Richardson 
415399a2dd95SBruce Richardson 	ret = rte_eth_dev_info_get(port_id, &dev_info);
415499a2dd95SBruce Richardson 	if (ret != 0)
415599a2dd95SBruce Richardson 		return ret;
415699a2dd95SBruce Richardson 
415799a2dd95SBruce Richardson 	rss_conf->rss_hf = rte_eth_rss_hf_refine(rss_conf->rss_hf);
415899a2dd95SBruce Richardson 	if ((dev_info.flow_type_rss_offloads | rss_conf->rss_hf) !=
415999a2dd95SBruce Richardson 	    dev_info.flow_type_rss_offloads) {
416099a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
416199a2dd95SBruce Richardson 			"Ethdev port_id=%u invalid rss_hf: 0x%"PRIx64", valid value: 0x%"PRIx64"\n",
416299a2dd95SBruce Richardson 			port_id, rss_conf->rss_hf,
416399a2dd95SBruce Richardson 			dev_info.flow_type_rss_offloads);
416499a2dd95SBruce Richardson 		return -EINVAL;
416599a2dd95SBruce Richardson 	}
416699a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rss_hash_update, -ENOTSUP);
416799a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->rss_hash_update)(dev,
416899a2dd95SBruce Richardson 								 rss_conf));
416999a2dd95SBruce Richardson }
417099a2dd95SBruce Richardson 
417199a2dd95SBruce Richardson int
417299a2dd95SBruce Richardson rte_eth_dev_rss_hash_conf_get(uint16_t port_id,
417399a2dd95SBruce Richardson 			      struct rte_eth_rss_conf *rss_conf)
417499a2dd95SBruce Richardson {
417599a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
417699a2dd95SBruce Richardson 
417799a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
417899a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
417953ef1b34SMin Hu (Connor) 
418053ef1b34SMin Hu (Connor) 	if (rss_conf == NULL) {
418153ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
418253ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u RSS hash config to NULL\n",
418353ef1b34SMin Hu (Connor) 			port_id);
418453ef1b34SMin Hu (Connor) 		return -EINVAL;
418553ef1b34SMin Hu (Connor) 	}
418653ef1b34SMin Hu (Connor) 
418799a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rss_hash_conf_get, -ENOTSUP);
418899a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->rss_hash_conf_get)(dev,
418999a2dd95SBruce Richardson 								   rss_conf));
419099a2dd95SBruce Richardson }
419199a2dd95SBruce Richardson 
419299a2dd95SBruce Richardson int
419399a2dd95SBruce Richardson rte_eth_dev_udp_tunnel_port_add(uint16_t port_id,
419499a2dd95SBruce Richardson 				struct rte_eth_udp_tunnel *udp_tunnel)
419599a2dd95SBruce Richardson {
419699a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
419799a2dd95SBruce Richardson 
419899a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
419953ef1b34SMin Hu (Connor) 	dev = &rte_eth_devices[port_id];
420053ef1b34SMin Hu (Connor) 
420199a2dd95SBruce Richardson 	if (udp_tunnel == NULL) {
420253ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
420353ef1b34SMin Hu (Connor) 			"Cannot add ethdev port %u UDP tunnel port from NULL UDP tunnel\n",
420453ef1b34SMin Hu (Connor) 			port_id);
420599a2dd95SBruce Richardson 		return -EINVAL;
420699a2dd95SBruce Richardson 	}
420799a2dd95SBruce Richardson 
4208295968d1SFerruh Yigit 	if (udp_tunnel->prot_type >= RTE_ETH_TUNNEL_TYPE_MAX) {
420999a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid tunnel type\n");
421099a2dd95SBruce Richardson 		return -EINVAL;
421199a2dd95SBruce Richardson 	}
421299a2dd95SBruce Richardson 
421399a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->udp_tunnel_port_add, -ENOTSUP);
421499a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->udp_tunnel_port_add)(dev,
421599a2dd95SBruce Richardson 								udp_tunnel));
421699a2dd95SBruce Richardson }
421799a2dd95SBruce Richardson 
421899a2dd95SBruce Richardson int
421999a2dd95SBruce Richardson rte_eth_dev_udp_tunnel_port_delete(uint16_t port_id,
422099a2dd95SBruce Richardson 				   struct rte_eth_udp_tunnel *udp_tunnel)
422199a2dd95SBruce Richardson {
422299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
422399a2dd95SBruce Richardson 
422499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
422599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
422699a2dd95SBruce Richardson 
422799a2dd95SBruce Richardson 	if (udp_tunnel == NULL) {
422853ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
422953ef1b34SMin Hu (Connor) 			"Cannot delete ethdev port %u UDP tunnel port from NULL UDP tunnel\n",
423053ef1b34SMin Hu (Connor) 			port_id);
423199a2dd95SBruce Richardson 		return -EINVAL;
423299a2dd95SBruce Richardson 	}
423399a2dd95SBruce Richardson 
4234295968d1SFerruh Yigit 	if (udp_tunnel->prot_type >= RTE_ETH_TUNNEL_TYPE_MAX) {
423599a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid tunnel type\n");
423699a2dd95SBruce Richardson 		return -EINVAL;
423799a2dd95SBruce Richardson 	}
423899a2dd95SBruce Richardson 
423999a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->udp_tunnel_port_del, -ENOTSUP);
424099a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->udp_tunnel_port_del)(dev,
424199a2dd95SBruce Richardson 								udp_tunnel));
424299a2dd95SBruce Richardson }
424399a2dd95SBruce Richardson 
424499a2dd95SBruce Richardson int
424599a2dd95SBruce Richardson rte_eth_led_on(uint16_t port_id)
424699a2dd95SBruce Richardson {
424799a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
424899a2dd95SBruce Richardson 
424999a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
425099a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
425153ef1b34SMin Hu (Connor) 
425299a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_on, -ENOTSUP);
425399a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->dev_led_on)(dev));
425499a2dd95SBruce Richardson }
425599a2dd95SBruce Richardson 
425699a2dd95SBruce Richardson int
425799a2dd95SBruce Richardson rte_eth_led_off(uint16_t port_id)
425899a2dd95SBruce Richardson {
425999a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
426099a2dd95SBruce Richardson 
426199a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
426299a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
426353ef1b34SMin Hu (Connor) 
426499a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_off, -ENOTSUP);
426599a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->dev_led_off)(dev));
426699a2dd95SBruce Richardson }
426799a2dd95SBruce Richardson 
426899a2dd95SBruce Richardson int
426999a2dd95SBruce Richardson rte_eth_fec_get_capability(uint16_t port_id,
427099a2dd95SBruce Richardson 			   struct rte_eth_fec_capa *speed_fec_capa,
427199a2dd95SBruce Richardson 			   unsigned int num)
427299a2dd95SBruce Richardson {
427399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
427499a2dd95SBruce Richardson 	int ret;
427599a2dd95SBruce Richardson 
427699a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
427799a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
427853ef1b34SMin Hu (Connor) 
427953ef1b34SMin Hu (Connor) 	if (speed_fec_capa == NULL && num > 0) {
428053ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
428153ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u FEC capability to NULL when array size is non zero\n",
428253ef1b34SMin Hu (Connor) 			port_id);
428353ef1b34SMin Hu (Connor) 		return -EINVAL;
428453ef1b34SMin Hu (Connor) 	}
428553ef1b34SMin Hu (Connor) 
428699a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fec_get_capability, -ENOTSUP);
428799a2dd95SBruce Richardson 	ret = (*dev->dev_ops->fec_get_capability)(dev, speed_fec_capa, num);
428899a2dd95SBruce Richardson 
428999a2dd95SBruce Richardson 	return ret;
429099a2dd95SBruce Richardson }
429199a2dd95SBruce Richardson 
429299a2dd95SBruce Richardson int
429399a2dd95SBruce Richardson rte_eth_fec_get(uint16_t port_id, uint32_t *fec_capa)
429499a2dd95SBruce Richardson {
429599a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
429699a2dd95SBruce Richardson 
429799a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
429899a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
429953ef1b34SMin Hu (Connor) 
430053ef1b34SMin Hu (Connor) 	if (fec_capa == NULL) {
430153ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
430253ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u current FEC mode to NULL\n",
430353ef1b34SMin Hu (Connor) 			port_id);
430453ef1b34SMin Hu (Connor) 		return -EINVAL;
430553ef1b34SMin Hu (Connor) 	}
430653ef1b34SMin Hu (Connor) 
430799a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fec_get, -ENOTSUP);
430899a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->fec_get)(dev, fec_capa));
430999a2dd95SBruce Richardson }
431099a2dd95SBruce Richardson 
431199a2dd95SBruce Richardson int
431299a2dd95SBruce Richardson rte_eth_fec_set(uint16_t port_id, uint32_t fec_capa)
431399a2dd95SBruce Richardson {
431499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
431599a2dd95SBruce Richardson 
431699a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
431799a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
431853ef1b34SMin Hu (Connor) 
431999a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fec_set, -ENOTSUP);
432099a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->fec_set)(dev, fec_capa));
432199a2dd95SBruce Richardson }
432299a2dd95SBruce Richardson 
432399a2dd95SBruce Richardson /*
432499a2dd95SBruce Richardson  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
432599a2dd95SBruce Richardson  * an empty spot.
432699a2dd95SBruce Richardson  */
432799a2dd95SBruce Richardson static int
432899a2dd95SBruce Richardson eth_dev_get_mac_addr_index(uint16_t port_id, const struct rte_ether_addr *addr)
432999a2dd95SBruce Richardson {
433099a2dd95SBruce Richardson 	struct rte_eth_dev_info dev_info;
433199a2dd95SBruce Richardson 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
433299a2dd95SBruce Richardson 	unsigned i;
433399a2dd95SBruce Richardson 	int ret;
433499a2dd95SBruce Richardson 
433599a2dd95SBruce Richardson 	ret = rte_eth_dev_info_get(port_id, &dev_info);
433699a2dd95SBruce Richardson 	if (ret != 0)
433799a2dd95SBruce Richardson 		return -1;
433899a2dd95SBruce Richardson 
433999a2dd95SBruce Richardson 	for (i = 0; i < dev_info.max_mac_addrs; i++)
434099a2dd95SBruce Richardson 		if (memcmp(addr, &dev->data->mac_addrs[i],
434199a2dd95SBruce Richardson 				RTE_ETHER_ADDR_LEN) == 0)
434299a2dd95SBruce Richardson 			return i;
434399a2dd95SBruce Richardson 
434499a2dd95SBruce Richardson 	return -1;
434599a2dd95SBruce Richardson }
434699a2dd95SBruce Richardson 
434799a2dd95SBruce Richardson static const struct rte_ether_addr null_mac_addr;
434899a2dd95SBruce Richardson 
434999a2dd95SBruce Richardson int
435099a2dd95SBruce Richardson rte_eth_dev_mac_addr_add(uint16_t port_id, struct rte_ether_addr *addr,
435199a2dd95SBruce Richardson 			uint32_t pool)
435299a2dd95SBruce Richardson {
435399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
435499a2dd95SBruce Richardson 	int index;
435599a2dd95SBruce Richardson 	uint64_t pool_mask;
435699a2dd95SBruce Richardson 	int ret;
435799a2dd95SBruce Richardson 
435899a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
435999a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
436053ef1b34SMin Hu (Connor) 
436153ef1b34SMin Hu (Connor) 	if (addr == NULL) {
436253ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
436353ef1b34SMin Hu (Connor) 			"Cannot add ethdev port %u MAC address from NULL address\n",
436453ef1b34SMin Hu (Connor) 			port_id);
436553ef1b34SMin Hu (Connor) 		return -EINVAL;
436653ef1b34SMin Hu (Connor) 	}
436753ef1b34SMin Hu (Connor) 
436899a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_add, -ENOTSUP);
436999a2dd95SBruce Richardson 
437099a2dd95SBruce Richardson 	if (rte_is_zero_ether_addr(addr)) {
437199a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Port %u: Cannot add NULL MAC address\n",
437299a2dd95SBruce Richardson 			port_id);
437399a2dd95SBruce Richardson 		return -EINVAL;
437499a2dd95SBruce Richardson 	}
4375295968d1SFerruh Yigit 	if (pool >= RTE_ETH_64_POOLS) {
4376295968d1SFerruh Yigit 		RTE_ETHDEV_LOG(ERR, "Pool ID must be 0-%d\n", RTE_ETH_64_POOLS - 1);
437799a2dd95SBruce Richardson 		return -EINVAL;
437899a2dd95SBruce Richardson 	}
437999a2dd95SBruce Richardson 
438099a2dd95SBruce Richardson 	index = eth_dev_get_mac_addr_index(port_id, addr);
438199a2dd95SBruce Richardson 	if (index < 0) {
438299a2dd95SBruce Richardson 		index = eth_dev_get_mac_addr_index(port_id, &null_mac_addr);
438399a2dd95SBruce Richardson 		if (index < 0) {
438499a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR, "Port %u: MAC address array full\n",
438599a2dd95SBruce Richardson 				port_id);
438699a2dd95SBruce Richardson 			return -ENOSPC;
438799a2dd95SBruce Richardson 		}
438899a2dd95SBruce Richardson 	} else {
438999a2dd95SBruce Richardson 		pool_mask = dev->data->mac_pool_sel[index];
439099a2dd95SBruce Richardson 
439199a2dd95SBruce Richardson 		/* Check if both MAC address and pool is already there, and do nothing */
4392e1823e08SThomas Monjalon 		if (pool_mask & RTE_BIT64(pool))
439399a2dd95SBruce Richardson 			return 0;
439499a2dd95SBruce Richardson 	}
439599a2dd95SBruce Richardson 
439699a2dd95SBruce Richardson 	/* Update NIC */
439799a2dd95SBruce Richardson 	ret = (*dev->dev_ops->mac_addr_add)(dev, addr, index, pool);
439899a2dd95SBruce Richardson 
439999a2dd95SBruce Richardson 	if (ret == 0) {
440099a2dd95SBruce Richardson 		/* Update address in NIC data structure */
440199a2dd95SBruce Richardson 		rte_ether_addr_copy(addr, &dev->data->mac_addrs[index]);
440299a2dd95SBruce Richardson 
440399a2dd95SBruce Richardson 		/* Update pool bitmap in NIC data structure */
4404e1823e08SThomas Monjalon 		dev->data->mac_pool_sel[index] |= RTE_BIT64(pool);
440599a2dd95SBruce Richardson 	}
440699a2dd95SBruce Richardson 
440799a2dd95SBruce Richardson 	return eth_err(port_id, ret);
440899a2dd95SBruce Richardson }
440999a2dd95SBruce Richardson 
441099a2dd95SBruce Richardson int
441199a2dd95SBruce Richardson rte_eth_dev_mac_addr_remove(uint16_t port_id, struct rte_ether_addr *addr)
441299a2dd95SBruce Richardson {
441399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
441499a2dd95SBruce Richardson 	int index;
441599a2dd95SBruce Richardson 
441699a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
441799a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
441853ef1b34SMin Hu (Connor) 
441953ef1b34SMin Hu (Connor) 	if (addr == NULL) {
442053ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
442153ef1b34SMin Hu (Connor) 			"Cannot remove ethdev port %u MAC address from NULL address\n",
442253ef1b34SMin Hu (Connor) 			port_id);
442353ef1b34SMin Hu (Connor) 		return -EINVAL;
442453ef1b34SMin Hu (Connor) 	}
442553ef1b34SMin Hu (Connor) 
442699a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_remove, -ENOTSUP);
442799a2dd95SBruce Richardson 
442899a2dd95SBruce Richardson 	index = eth_dev_get_mac_addr_index(port_id, addr);
442999a2dd95SBruce Richardson 	if (index == 0) {
443099a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
443199a2dd95SBruce Richardson 			"Port %u: Cannot remove default MAC address\n",
443299a2dd95SBruce Richardson 			port_id);
443399a2dd95SBruce Richardson 		return -EADDRINUSE;
443499a2dd95SBruce Richardson 	} else if (index < 0)
443599a2dd95SBruce Richardson 		return 0;  /* Do nothing if address wasn't found */
443699a2dd95SBruce Richardson 
443799a2dd95SBruce Richardson 	/* Update NIC */
443899a2dd95SBruce Richardson 	(*dev->dev_ops->mac_addr_remove)(dev, index);
443999a2dd95SBruce Richardson 
444099a2dd95SBruce Richardson 	/* Update address in NIC data structure */
444199a2dd95SBruce Richardson 	rte_ether_addr_copy(&null_mac_addr, &dev->data->mac_addrs[index]);
444299a2dd95SBruce Richardson 
444399a2dd95SBruce Richardson 	/* reset pool bitmap */
444499a2dd95SBruce Richardson 	dev->data->mac_pool_sel[index] = 0;
444599a2dd95SBruce Richardson 
444699a2dd95SBruce Richardson 	return 0;
444799a2dd95SBruce Richardson }
444899a2dd95SBruce Richardson 
444999a2dd95SBruce Richardson int
445099a2dd95SBruce Richardson rte_eth_dev_default_mac_addr_set(uint16_t port_id, struct rte_ether_addr *addr)
445199a2dd95SBruce Richardson {
445299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
445399a2dd95SBruce Richardson 	int ret;
445499a2dd95SBruce Richardson 
445599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
445653ef1b34SMin Hu (Connor) 	dev = &rte_eth_devices[port_id];
445753ef1b34SMin Hu (Connor) 
445853ef1b34SMin Hu (Connor) 	if (addr == NULL) {
445953ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
446053ef1b34SMin Hu (Connor) 			"Cannot set ethdev port %u default MAC address from NULL address\n",
446153ef1b34SMin Hu (Connor) 			port_id);
446253ef1b34SMin Hu (Connor) 		return -EINVAL;
446353ef1b34SMin Hu (Connor) 	}
446499a2dd95SBruce Richardson 
446599a2dd95SBruce Richardson 	if (!rte_is_valid_assigned_ether_addr(addr))
446699a2dd95SBruce Richardson 		return -EINVAL;
446799a2dd95SBruce Richardson 
446899a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_set, -ENOTSUP);
446999a2dd95SBruce Richardson 
447099a2dd95SBruce Richardson 	ret = (*dev->dev_ops->mac_addr_set)(dev, addr);
447199a2dd95SBruce Richardson 	if (ret < 0)
447299a2dd95SBruce Richardson 		return ret;
447399a2dd95SBruce Richardson 
447499a2dd95SBruce Richardson 	/* Update default address in NIC data structure */
447599a2dd95SBruce Richardson 	rte_ether_addr_copy(addr, &dev->data->mac_addrs[0]);
447699a2dd95SBruce Richardson 
447799a2dd95SBruce Richardson 	return 0;
447899a2dd95SBruce Richardson }
447999a2dd95SBruce Richardson 
448099a2dd95SBruce Richardson 
448199a2dd95SBruce Richardson /*
448299a2dd95SBruce Richardson  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
448399a2dd95SBruce Richardson  * an empty spot.
448499a2dd95SBruce Richardson  */
448599a2dd95SBruce Richardson static int
448699a2dd95SBruce Richardson eth_dev_get_hash_mac_addr_index(uint16_t port_id,
448799a2dd95SBruce Richardson 		const struct rte_ether_addr *addr)
448899a2dd95SBruce Richardson {
448999a2dd95SBruce Richardson 	struct rte_eth_dev_info dev_info;
449099a2dd95SBruce Richardson 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
449199a2dd95SBruce Richardson 	unsigned i;
449299a2dd95SBruce Richardson 	int ret;
449399a2dd95SBruce Richardson 
449499a2dd95SBruce Richardson 	ret = rte_eth_dev_info_get(port_id, &dev_info);
449599a2dd95SBruce Richardson 	if (ret != 0)
449699a2dd95SBruce Richardson 		return -1;
449799a2dd95SBruce Richardson 
449899a2dd95SBruce Richardson 	if (!dev->data->hash_mac_addrs)
449999a2dd95SBruce Richardson 		return -1;
450099a2dd95SBruce Richardson 
450199a2dd95SBruce Richardson 	for (i = 0; i < dev_info.max_hash_mac_addrs; i++)
450299a2dd95SBruce Richardson 		if (memcmp(addr, &dev->data->hash_mac_addrs[i],
450399a2dd95SBruce Richardson 			RTE_ETHER_ADDR_LEN) == 0)
450499a2dd95SBruce Richardson 			return i;
450599a2dd95SBruce Richardson 
450699a2dd95SBruce Richardson 	return -1;
450799a2dd95SBruce Richardson }
450899a2dd95SBruce Richardson 
450999a2dd95SBruce Richardson int
451099a2dd95SBruce Richardson rte_eth_dev_uc_hash_table_set(uint16_t port_id, struct rte_ether_addr *addr,
451199a2dd95SBruce Richardson 				uint8_t on)
451299a2dd95SBruce Richardson {
451399a2dd95SBruce Richardson 	int index;
451499a2dd95SBruce Richardson 	int ret;
451599a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
451699a2dd95SBruce Richardson 
451799a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
451899a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
451953ef1b34SMin Hu (Connor) 
452053ef1b34SMin Hu (Connor) 	if (addr == NULL) {
452153ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
452253ef1b34SMin Hu (Connor) 			"Cannot set ethdev port %u unicast hash table from NULL address\n",
452353ef1b34SMin Hu (Connor) 			port_id);
452453ef1b34SMin Hu (Connor) 		return -EINVAL;
452553ef1b34SMin Hu (Connor) 	}
452653ef1b34SMin Hu (Connor) 
452799a2dd95SBruce Richardson 	if (rte_is_zero_ether_addr(addr)) {
452899a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Port %u: Cannot add NULL MAC address\n",
452999a2dd95SBruce Richardson 			port_id);
453099a2dd95SBruce Richardson 		return -EINVAL;
453199a2dd95SBruce Richardson 	}
453299a2dd95SBruce Richardson 
453399a2dd95SBruce Richardson 	index = eth_dev_get_hash_mac_addr_index(port_id, addr);
453499a2dd95SBruce Richardson 	/* Check if it's already there, and do nothing */
453599a2dd95SBruce Richardson 	if ((index >= 0) && on)
453699a2dd95SBruce Richardson 		return 0;
453799a2dd95SBruce Richardson 
453899a2dd95SBruce Richardson 	if (index < 0) {
453999a2dd95SBruce Richardson 		if (!on) {
454099a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
454199a2dd95SBruce Richardson 				"Port %u: the MAC address was not set in UTA\n",
454299a2dd95SBruce Richardson 				port_id);
454399a2dd95SBruce Richardson 			return -EINVAL;
454499a2dd95SBruce Richardson 		}
454599a2dd95SBruce Richardson 
454699a2dd95SBruce Richardson 		index = eth_dev_get_hash_mac_addr_index(port_id, &null_mac_addr);
454799a2dd95SBruce Richardson 		if (index < 0) {
454899a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR, "Port %u: MAC address array full\n",
454999a2dd95SBruce Richardson 				port_id);
455099a2dd95SBruce Richardson 			return -ENOSPC;
455199a2dd95SBruce Richardson 		}
455299a2dd95SBruce Richardson 	}
455399a2dd95SBruce Richardson 
455499a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->uc_hash_table_set, -ENOTSUP);
455599a2dd95SBruce Richardson 	ret = (*dev->dev_ops->uc_hash_table_set)(dev, addr, on);
455699a2dd95SBruce Richardson 	if (ret == 0) {
455799a2dd95SBruce Richardson 		/* Update address in NIC data structure */
455899a2dd95SBruce Richardson 		if (on)
455999a2dd95SBruce Richardson 			rte_ether_addr_copy(addr,
456099a2dd95SBruce Richardson 					&dev->data->hash_mac_addrs[index]);
456199a2dd95SBruce Richardson 		else
456299a2dd95SBruce Richardson 			rte_ether_addr_copy(&null_mac_addr,
456399a2dd95SBruce Richardson 					&dev->data->hash_mac_addrs[index]);
456499a2dd95SBruce Richardson 	}
456599a2dd95SBruce Richardson 
456699a2dd95SBruce Richardson 	return eth_err(port_id, ret);
456799a2dd95SBruce Richardson }
456899a2dd95SBruce Richardson 
456999a2dd95SBruce Richardson int
457099a2dd95SBruce Richardson rte_eth_dev_uc_all_hash_table_set(uint16_t port_id, uint8_t on)
457199a2dd95SBruce Richardson {
457299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
457399a2dd95SBruce Richardson 
457499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
457599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
457699a2dd95SBruce Richardson 
457799a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->uc_all_hash_table_set, -ENOTSUP);
457899a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->uc_all_hash_table_set)(dev,
457999a2dd95SBruce Richardson 								       on));
458099a2dd95SBruce Richardson }
458199a2dd95SBruce Richardson 
458299a2dd95SBruce Richardson int rte_eth_set_queue_rate_limit(uint16_t port_id, uint16_t queue_idx,
458399a2dd95SBruce Richardson 					uint16_t tx_rate)
458499a2dd95SBruce Richardson {
458599a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
458699a2dd95SBruce Richardson 	struct rte_eth_dev_info dev_info;
458799a2dd95SBruce Richardson 	struct rte_eth_link link;
458899a2dd95SBruce Richardson 	int ret;
458999a2dd95SBruce Richardson 
459099a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
459153ef1b34SMin Hu (Connor) 	dev = &rte_eth_devices[port_id];
459299a2dd95SBruce Richardson 
459399a2dd95SBruce Richardson 	ret = rte_eth_dev_info_get(port_id, &dev_info);
459499a2dd95SBruce Richardson 	if (ret != 0)
459599a2dd95SBruce Richardson 		return ret;
459699a2dd95SBruce Richardson 
459799a2dd95SBruce Richardson 	link = dev->data->dev_link;
459899a2dd95SBruce Richardson 
459999a2dd95SBruce Richardson 	if (queue_idx > dev_info.max_tx_queues) {
460099a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
46015906be5aSAndrew Rybchenko 			"Set queue rate limit:port %u: invalid queue ID=%u\n",
460299a2dd95SBruce Richardson 			port_id, queue_idx);
460399a2dd95SBruce Richardson 		return -EINVAL;
460499a2dd95SBruce Richardson 	}
460599a2dd95SBruce Richardson 
460699a2dd95SBruce Richardson 	if (tx_rate > link.link_speed) {
460799a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
460899a2dd95SBruce Richardson 			"Set queue rate limit:invalid tx_rate=%u, bigger than link speed= %d\n",
460999a2dd95SBruce Richardson 			tx_rate, link.link_speed);
461099a2dd95SBruce Richardson 		return -EINVAL;
461199a2dd95SBruce Richardson 	}
461299a2dd95SBruce Richardson 
461399a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_queue_rate_limit, -ENOTSUP);
461499a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->set_queue_rate_limit)(dev,
461599a2dd95SBruce Richardson 							queue_idx, tx_rate));
461699a2dd95SBruce Richardson }
461799a2dd95SBruce Richardson 
4618c87d435aSKonstantin Ananyev RTE_INIT(eth_dev_init_fp_ops)
4619c87d435aSKonstantin Ananyev {
4620c87d435aSKonstantin Ananyev 	uint32_t i;
4621c87d435aSKonstantin Ananyev 
4622c87d435aSKonstantin Ananyev 	for (i = 0; i != RTE_DIM(rte_eth_fp_ops); i++)
4623c87d435aSKonstantin Ananyev 		eth_dev_fp_ops_reset(rte_eth_fp_ops + i);
4624c87d435aSKonstantin Ananyev }
4625c87d435aSKonstantin Ananyev 
462699a2dd95SBruce Richardson RTE_INIT(eth_dev_init_cb_lists)
462799a2dd95SBruce Richardson {
462899a2dd95SBruce Richardson 	uint16_t i;
462999a2dd95SBruce Richardson 
463099a2dd95SBruce Richardson 	for (i = 0; i < RTE_MAX_ETHPORTS; i++)
463199a2dd95SBruce Richardson 		TAILQ_INIT(&rte_eth_devices[i].link_intr_cbs);
463299a2dd95SBruce Richardson }
463399a2dd95SBruce Richardson 
463499a2dd95SBruce Richardson int
463599a2dd95SBruce Richardson rte_eth_dev_callback_register(uint16_t port_id,
463699a2dd95SBruce Richardson 			enum rte_eth_event_type event,
463799a2dd95SBruce Richardson 			rte_eth_dev_cb_fn cb_fn, void *cb_arg)
463899a2dd95SBruce Richardson {
463999a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
464099a2dd95SBruce Richardson 	struct rte_eth_dev_callback *user_cb;
464199a2dd95SBruce Richardson 	uint16_t next_port;
464299a2dd95SBruce Richardson 	uint16_t last_port;
464399a2dd95SBruce Richardson 
464453ef1b34SMin Hu (Connor) 	if (cb_fn == NULL) {
464553ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
464653ef1b34SMin Hu (Connor) 			"Cannot register ethdev port %u callback from NULL\n",
464753ef1b34SMin Hu (Connor) 			port_id);
464899a2dd95SBruce Richardson 		return -EINVAL;
464953ef1b34SMin Hu (Connor) 	}
465099a2dd95SBruce Richardson 
465199a2dd95SBruce Richardson 	if (!rte_eth_dev_is_valid_port(port_id) && port_id != RTE_ETH_ALL) {
465299a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid port_id=%d\n", port_id);
465399a2dd95SBruce Richardson 		return -EINVAL;
465499a2dd95SBruce Richardson 	}
465599a2dd95SBruce Richardson 
465699a2dd95SBruce Richardson 	if (port_id == RTE_ETH_ALL) {
465799a2dd95SBruce Richardson 		next_port = 0;
465899a2dd95SBruce Richardson 		last_port = RTE_MAX_ETHPORTS - 1;
465999a2dd95SBruce Richardson 	} else {
466099a2dd95SBruce Richardson 		next_port = last_port = port_id;
466199a2dd95SBruce Richardson 	}
466299a2dd95SBruce Richardson 
466399a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_cb_lock);
466499a2dd95SBruce Richardson 
466599a2dd95SBruce Richardson 	do {
466699a2dd95SBruce Richardson 		dev = &rte_eth_devices[next_port];
466799a2dd95SBruce Richardson 
466899a2dd95SBruce Richardson 		TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
466999a2dd95SBruce Richardson 			if (user_cb->cb_fn == cb_fn &&
467099a2dd95SBruce Richardson 				user_cb->cb_arg == cb_arg &&
467199a2dd95SBruce Richardson 				user_cb->event == event) {
467299a2dd95SBruce Richardson 				break;
467399a2dd95SBruce Richardson 			}
467499a2dd95SBruce Richardson 		}
467599a2dd95SBruce Richardson 
467699a2dd95SBruce Richardson 		/* create a new callback. */
467799a2dd95SBruce Richardson 		if (user_cb == NULL) {
467899a2dd95SBruce Richardson 			user_cb = rte_zmalloc("INTR_USER_CALLBACK",
467999a2dd95SBruce Richardson 				sizeof(struct rte_eth_dev_callback), 0);
468099a2dd95SBruce Richardson 			if (user_cb != NULL) {
468199a2dd95SBruce Richardson 				user_cb->cb_fn = cb_fn;
468299a2dd95SBruce Richardson 				user_cb->cb_arg = cb_arg;
468399a2dd95SBruce Richardson 				user_cb->event = event;
468499a2dd95SBruce Richardson 				TAILQ_INSERT_TAIL(&(dev->link_intr_cbs),
468599a2dd95SBruce Richardson 						  user_cb, next);
468699a2dd95SBruce Richardson 			} else {
468799a2dd95SBruce Richardson 				rte_spinlock_unlock(&eth_dev_cb_lock);
468899a2dd95SBruce Richardson 				rte_eth_dev_callback_unregister(port_id, event,
468999a2dd95SBruce Richardson 								cb_fn, cb_arg);
469099a2dd95SBruce Richardson 				return -ENOMEM;
469199a2dd95SBruce Richardson 			}
469299a2dd95SBruce Richardson 
469399a2dd95SBruce Richardson 		}
469499a2dd95SBruce Richardson 	} while (++next_port <= last_port);
469599a2dd95SBruce Richardson 
469699a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_cb_lock);
469799a2dd95SBruce Richardson 	return 0;
469899a2dd95SBruce Richardson }
469999a2dd95SBruce Richardson 
470099a2dd95SBruce Richardson int
470199a2dd95SBruce Richardson rte_eth_dev_callback_unregister(uint16_t port_id,
470299a2dd95SBruce Richardson 			enum rte_eth_event_type event,
470399a2dd95SBruce Richardson 			rte_eth_dev_cb_fn cb_fn, void *cb_arg)
470499a2dd95SBruce Richardson {
470599a2dd95SBruce Richardson 	int ret;
470699a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
470799a2dd95SBruce Richardson 	struct rte_eth_dev_callback *cb, *next;
470899a2dd95SBruce Richardson 	uint16_t next_port;
470999a2dd95SBruce Richardson 	uint16_t last_port;
471099a2dd95SBruce Richardson 
471153ef1b34SMin Hu (Connor) 	if (cb_fn == NULL) {
471253ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
471353ef1b34SMin Hu (Connor) 			"Cannot unregister ethdev port %u callback from NULL\n",
471453ef1b34SMin Hu (Connor) 			port_id);
471599a2dd95SBruce Richardson 		return -EINVAL;
471653ef1b34SMin Hu (Connor) 	}
471799a2dd95SBruce Richardson 
471899a2dd95SBruce Richardson 	if (!rte_eth_dev_is_valid_port(port_id) && port_id != RTE_ETH_ALL) {
471999a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid port_id=%d\n", port_id);
472099a2dd95SBruce Richardson 		return -EINVAL;
472199a2dd95SBruce Richardson 	}
472299a2dd95SBruce Richardson 
472399a2dd95SBruce Richardson 	if (port_id == RTE_ETH_ALL) {
472499a2dd95SBruce Richardson 		next_port = 0;
472599a2dd95SBruce Richardson 		last_port = RTE_MAX_ETHPORTS - 1;
472699a2dd95SBruce Richardson 	} else {
472799a2dd95SBruce Richardson 		next_port = last_port = port_id;
472899a2dd95SBruce Richardson 	}
472999a2dd95SBruce Richardson 
473099a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_cb_lock);
473199a2dd95SBruce Richardson 
473299a2dd95SBruce Richardson 	do {
473399a2dd95SBruce Richardson 		dev = &rte_eth_devices[next_port];
473499a2dd95SBruce Richardson 		ret = 0;
473599a2dd95SBruce Richardson 		for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL;
473699a2dd95SBruce Richardson 		     cb = next) {
473799a2dd95SBruce Richardson 
473899a2dd95SBruce Richardson 			next = TAILQ_NEXT(cb, next);
473999a2dd95SBruce Richardson 
474099a2dd95SBruce Richardson 			if (cb->cb_fn != cb_fn || cb->event != event ||
474199a2dd95SBruce Richardson 			    (cb_arg != (void *)-1 && cb->cb_arg != cb_arg))
474299a2dd95SBruce Richardson 				continue;
474399a2dd95SBruce Richardson 
474499a2dd95SBruce Richardson 			/*
474599a2dd95SBruce Richardson 			 * if this callback is not executing right now,
474699a2dd95SBruce Richardson 			 * then remove it.
474799a2dd95SBruce Richardson 			 */
474899a2dd95SBruce Richardson 			if (cb->active == 0) {
474999a2dd95SBruce Richardson 				TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
475099a2dd95SBruce Richardson 				rte_free(cb);
475199a2dd95SBruce Richardson 			} else {
475299a2dd95SBruce Richardson 				ret = -EAGAIN;
475399a2dd95SBruce Richardson 			}
475499a2dd95SBruce Richardson 		}
475599a2dd95SBruce Richardson 	} while (++next_port <= last_port);
475699a2dd95SBruce Richardson 
475799a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_cb_lock);
475899a2dd95SBruce Richardson 	return ret;
475999a2dd95SBruce Richardson }
476099a2dd95SBruce Richardson 
476199a2dd95SBruce Richardson int
476299a2dd95SBruce Richardson rte_eth_dev_callback_process(struct rte_eth_dev *dev,
476399a2dd95SBruce Richardson 	enum rte_eth_event_type event, void *ret_param)
476499a2dd95SBruce Richardson {
476599a2dd95SBruce Richardson 	struct rte_eth_dev_callback *cb_lst;
476699a2dd95SBruce Richardson 	struct rte_eth_dev_callback dev_cb;
476799a2dd95SBruce Richardson 	int rc = 0;
476899a2dd95SBruce Richardson 
476999a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_cb_lock);
477099a2dd95SBruce Richardson 	TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
477199a2dd95SBruce Richardson 		if (cb_lst->cb_fn == NULL || cb_lst->event != event)
477299a2dd95SBruce Richardson 			continue;
477399a2dd95SBruce Richardson 		dev_cb = *cb_lst;
477499a2dd95SBruce Richardson 		cb_lst->active = 1;
477599a2dd95SBruce Richardson 		if (ret_param != NULL)
477699a2dd95SBruce Richardson 			dev_cb.ret_param = ret_param;
477799a2dd95SBruce Richardson 
477899a2dd95SBruce Richardson 		rte_spinlock_unlock(&eth_dev_cb_lock);
477999a2dd95SBruce Richardson 		rc = dev_cb.cb_fn(dev->data->port_id, dev_cb.event,
478099a2dd95SBruce Richardson 				dev_cb.cb_arg, dev_cb.ret_param);
478199a2dd95SBruce Richardson 		rte_spinlock_lock(&eth_dev_cb_lock);
478299a2dd95SBruce Richardson 		cb_lst->active = 0;
478399a2dd95SBruce Richardson 	}
478499a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_cb_lock);
478599a2dd95SBruce Richardson 	return rc;
478699a2dd95SBruce Richardson }
478799a2dd95SBruce Richardson 
478899a2dd95SBruce Richardson void
478999a2dd95SBruce Richardson rte_eth_dev_probing_finish(struct rte_eth_dev *dev)
479099a2dd95SBruce Richardson {
479199a2dd95SBruce Richardson 	if (dev == NULL)
479299a2dd95SBruce Richardson 		return;
479399a2dd95SBruce Richardson 
4794c87d435aSKonstantin Ananyev 	/*
4795c87d435aSKonstantin Ananyev 	 * for secondary process, at that point we expect device
4796c87d435aSKonstantin Ananyev 	 * to be already 'usable', so shared data and all function pointers
4797c87d435aSKonstantin Ananyev 	 * for fast-path devops have to be setup properly inside rte_eth_dev.
4798c87d435aSKonstantin Ananyev 	 */
4799c87d435aSKonstantin Ananyev 	if (rte_eal_process_type() == RTE_PROC_SECONDARY)
4800c87d435aSKonstantin Ananyev 		eth_dev_fp_ops_setup(rte_eth_fp_ops + dev->data->port_id, dev);
4801c87d435aSKonstantin Ananyev 
480299a2dd95SBruce Richardson 	rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_NEW, NULL);
480399a2dd95SBruce Richardson 
480499a2dd95SBruce Richardson 	dev->state = RTE_ETH_DEV_ATTACHED;
480599a2dd95SBruce Richardson }
480699a2dd95SBruce Richardson 
480799a2dd95SBruce Richardson int
480899a2dd95SBruce Richardson rte_eth_dev_rx_intr_ctl(uint16_t port_id, int epfd, int op, void *data)
480999a2dd95SBruce Richardson {
481099a2dd95SBruce Richardson 	uint32_t vec;
481199a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
481299a2dd95SBruce Richardson 	struct rte_intr_handle *intr_handle;
481399a2dd95SBruce Richardson 	uint16_t qid;
481499a2dd95SBruce Richardson 	int rc;
481599a2dd95SBruce Richardson 
481699a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
481799a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
481899a2dd95SBruce Richardson 
481999a2dd95SBruce Richardson 	if (!dev->intr_handle) {
482009fd4227SAndrew Rybchenko 		RTE_ETHDEV_LOG(ERR, "Rx Intr handle unset\n");
482199a2dd95SBruce Richardson 		return -ENOTSUP;
482299a2dd95SBruce Richardson 	}
482399a2dd95SBruce Richardson 
482499a2dd95SBruce Richardson 	intr_handle = dev->intr_handle;
4825c2bd9367SHarman Kalra 	if (rte_intr_vec_list_index_get(intr_handle, 0) < 0) {
482609fd4227SAndrew Rybchenko 		RTE_ETHDEV_LOG(ERR, "Rx Intr vector unset\n");
482799a2dd95SBruce Richardson 		return -EPERM;
482899a2dd95SBruce Richardson 	}
482999a2dd95SBruce Richardson 
483099a2dd95SBruce Richardson 	for (qid = 0; qid < dev->data->nb_rx_queues; qid++) {
4831c2bd9367SHarman Kalra 		vec = rte_intr_vec_list_index_get(intr_handle, qid);
483299a2dd95SBruce Richardson 		rc = rte_intr_rx_ctl(intr_handle, epfd, op, vec, data);
483399a2dd95SBruce Richardson 		if (rc && rc != -EEXIST) {
483499a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
483509fd4227SAndrew Rybchenko 				"p %u q %u Rx ctl error op %d epfd %d vec %u\n",
483699a2dd95SBruce Richardson 				port_id, qid, op, epfd, vec);
483799a2dd95SBruce Richardson 		}
483899a2dd95SBruce Richardson 	}
483999a2dd95SBruce Richardson 
484099a2dd95SBruce Richardson 	return 0;
484199a2dd95SBruce Richardson }
484299a2dd95SBruce Richardson 
484399a2dd95SBruce Richardson int
484499a2dd95SBruce Richardson rte_eth_dev_rx_intr_ctl_q_get_fd(uint16_t port_id, uint16_t queue_id)
484599a2dd95SBruce Richardson {
484699a2dd95SBruce Richardson 	struct rte_intr_handle *intr_handle;
484799a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
484899a2dd95SBruce Richardson 	unsigned int efd_idx;
484999a2dd95SBruce Richardson 	uint32_t vec;
485099a2dd95SBruce Richardson 	int fd;
485199a2dd95SBruce Richardson 
485299a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -1);
485399a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
485499a2dd95SBruce Richardson 
485599a2dd95SBruce Richardson 	if (queue_id >= dev->data->nb_rx_queues) {
485609fd4227SAndrew Rybchenko 		RTE_ETHDEV_LOG(ERR, "Invalid Rx queue_id=%u\n", queue_id);
485799a2dd95SBruce Richardson 		return -1;
485899a2dd95SBruce Richardson 	}
485999a2dd95SBruce Richardson 
486099a2dd95SBruce Richardson 	if (!dev->intr_handle) {
486109fd4227SAndrew Rybchenko 		RTE_ETHDEV_LOG(ERR, "Rx Intr handle unset\n");
486299a2dd95SBruce Richardson 		return -1;
486399a2dd95SBruce Richardson 	}
486499a2dd95SBruce Richardson 
486599a2dd95SBruce Richardson 	intr_handle = dev->intr_handle;
4866c2bd9367SHarman Kalra 	if (rte_intr_vec_list_index_get(intr_handle, 0) < 0) {
486709fd4227SAndrew Rybchenko 		RTE_ETHDEV_LOG(ERR, "Rx Intr vector unset\n");
486899a2dd95SBruce Richardson 		return -1;
486999a2dd95SBruce Richardson 	}
487099a2dd95SBruce Richardson 
4871c2bd9367SHarman Kalra 	vec = rte_intr_vec_list_index_get(intr_handle, queue_id);
487299a2dd95SBruce Richardson 	efd_idx = (vec >= RTE_INTR_VEC_RXTX_OFFSET) ?
487399a2dd95SBruce Richardson 		(vec - RTE_INTR_VEC_RXTX_OFFSET) : vec;
4874c2bd9367SHarman Kalra 	fd = rte_intr_efds_index_get(intr_handle, efd_idx);
487599a2dd95SBruce Richardson 
487699a2dd95SBruce Richardson 	return fd;
487799a2dd95SBruce Richardson }
487899a2dd95SBruce Richardson 
487999a2dd95SBruce Richardson static inline int
488099a2dd95SBruce Richardson eth_dev_dma_mzone_name(char *name, size_t len, uint16_t port_id, uint16_t queue_id,
488199a2dd95SBruce Richardson 		const char *ring_name)
488299a2dd95SBruce Richardson {
488399a2dd95SBruce Richardson 	return snprintf(name, len, "eth_p%d_q%d_%s",
488499a2dd95SBruce Richardson 			port_id, queue_id, ring_name);
488599a2dd95SBruce Richardson }
488699a2dd95SBruce Richardson 
488799a2dd95SBruce Richardson const struct rte_memzone *
488899a2dd95SBruce Richardson rte_eth_dma_zone_reserve(const struct rte_eth_dev *dev, const char *ring_name,
488999a2dd95SBruce Richardson 			 uint16_t queue_id, size_t size, unsigned align,
489099a2dd95SBruce Richardson 			 int socket_id)
489199a2dd95SBruce Richardson {
489299a2dd95SBruce Richardson 	char z_name[RTE_MEMZONE_NAMESIZE];
489399a2dd95SBruce Richardson 	const struct rte_memzone *mz;
489499a2dd95SBruce Richardson 	int rc;
489599a2dd95SBruce Richardson 
489699a2dd95SBruce Richardson 	rc = eth_dev_dma_mzone_name(z_name, sizeof(z_name), dev->data->port_id,
489799a2dd95SBruce Richardson 			queue_id, ring_name);
489899a2dd95SBruce Richardson 	if (rc >= RTE_MEMZONE_NAMESIZE) {
489999a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "ring name too long\n");
490099a2dd95SBruce Richardson 		rte_errno = ENAMETOOLONG;
490199a2dd95SBruce Richardson 		return NULL;
490299a2dd95SBruce Richardson 	}
490399a2dd95SBruce Richardson 
490499a2dd95SBruce Richardson 	mz = rte_memzone_lookup(z_name);
490599a2dd95SBruce Richardson 	if (mz) {
490699a2dd95SBruce Richardson 		if ((socket_id != SOCKET_ID_ANY && socket_id != mz->socket_id) ||
490799a2dd95SBruce Richardson 				size > mz->len ||
490899a2dd95SBruce Richardson 				((uintptr_t)mz->addr & (align - 1)) != 0) {
490999a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
491099a2dd95SBruce Richardson 				"memzone %s does not justify the requested attributes\n",
491199a2dd95SBruce Richardson 				mz->name);
491299a2dd95SBruce Richardson 			return NULL;
491399a2dd95SBruce Richardson 		}
491499a2dd95SBruce Richardson 
491599a2dd95SBruce Richardson 		return mz;
491699a2dd95SBruce Richardson 	}
491799a2dd95SBruce Richardson 
491899a2dd95SBruce Richardson 	return rte_memzone_reserve_aligned(z_name, size, socket_id,
491999a2dd95SBruce Richardson 			RTE_MEMZONE_IOVA_CONTIG, align);
492099a2dd95SBruce Richardson }
492199a2dd95SBruce Richardson 
492299a2dd95SBruce Richardson int
492399a2dd95SBruce Richardson rte_eth_dma_zone_free(const struct rte_eth_dev *dev, const char *ring_name,
492499a2dd95SBruce Richardson 		uint16_t queue_id)
492599a2dd95SBruce Richardson {
492699a2dd95SBruce Richardson 	char z_name[RTE_MEMZONE_NAMESIZE];
492799a2dd95SBruce Richardson 	const struct rte_memzone *mz;
492899a2dd95SBruce Richardson 	int rc = 0;
492999a2dd95SBruce Richardson 
493099a2dd95SBruce Richardson 	rc = eth_dev_dma_mzone_name(z_name, sizeof(z_name), dev->data->port_id,
493199a2dd95SBruce Richardson 			queue_id, ring_name);
493299a2dd95SBruce Richardson 	if (rc >= RTE_MEMZONE_NAMESIZE) {
493399a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "ring name too long\n");
493499a2dd95SBruce Richardson 		return -ENAMETOOLONG;
493599a2dd95SBruce Richardson 	}
493699a2dd95SBruce Richardson 
493799a2dd95SBruce Richardson 	mz = rte_memzone_lookup(z_name);
493899a2dd95SBruce Richardson 	if (mz)
493999a2dd95SBruce Richardson 		rc = rte_memzone_free(mz);
494099a2dd95SBruce Richardson 	else
494199a2dd95SBruce Richardson 		rc = -ENOENT;
494299a2dd95SBruce Richardson 
494399a2dd95SBruce Richardson 	return rc;
494499a2dd95SBruce Richardson }
494599a2dd95SBruce Richardson 
494699a2dd95SBruce Richardson int
494799a2dd95SBruce Richardson rte_eth_dev_create(struct rte_device *device, const char *name,
494899a2dd95SBruce Richardson 	size_t priv_data_size,
494999a2dd95SBruce Richardson 	ethdev_bus_specific_init ethdev_bus_specific_init,
495099a2dd95SBruce Richardson 	void *bus_init_params,
495199a2dd95SBruce Richardson 	ethdev_init_t ethdev_init, void *init_params)
495299a2dd95SBruce Richardson {
495399a2dd95SBruce Richardson 	struct rte_eth_dev *ethdev;
495499a2dd95SBruce Richardson 	int retval;
495599a2dd95SBruce Richardson 
495699a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*ethdev_init, -EINVAL);
495799a2dd95SBruce Richardson 
495899a2dd95SBruce Richardson 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
495999a2dd95SBruce Richardson 		ethdev = rte_eth_dev_allocate(name);
496099a2dd95SBruce Richardson 		if (!ethdev)
496199a2dd95SBruce Richardson 			return -ENODEV;
496299a2dd95SBruce Richardson 
496399a2dd95SBruce Richardson 		if (priv_data_size) {
496499a2dd95SBruce Richardson 			ethdev->data->dev_private = rte_zmalloc_socket(
496599a2dd95SBruce Richardson 				name, priv_data_size, RTE_CACHE_LINE_SIZE,
496699a2dd95SBruce Richardson 				device->numa_node);
496799a2dd95SBruce Richardson 
496899a2dd95SBruce Richardson 			if (!ethdev->data->dev_private) {
496999a2dd95SBruce Richardson 				RTE_ETHDEV_LOG(ERR,
497099a2dd95SBruce Richardson 					"failed to allocate private data\n");
497199a2dd95SBruce Richardson 				retval = -ENOMEM;
497299a2dd95SBruce Richardson 				goto probe_failed;
497399a2dd95SBruce Richardson 			}
497499a2dd95SBruce Richardson 		}
497599a2dd95SBruce Richardson 	} else {
497699a2dd95SBruce Richardson 		ethdev = rte_eth_dev_attach_secondary(name);
497799a2dd95SBruce Richardson 		if (!ethdev) {
497899a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
497999a2dd95SBruce Richardson 				"secondary process attach failed, ethdev doesn't exist\n");
498099a2dd95SBruce Richardson 			return  -ENODEV;
498199a2dd95SBruce Richardson 		}
498299a2dd95SBruce Richardson 	}
498399a2dd95SBruce Richardson 
498499a2dd95SBruce Richardson 	ethdev->device = device;
498599a2dd95SBruce Richardson 
498699a2dd95SBruce Richardson 	if (ethdev_bus_specific_init) {
498799a2dd95SBruce Richardson 		retval = ethdev_bus_specific_init(ethdev, bus_init_params);
498899a2dd95SBruce Richardson 		if (retval) {
498999a2dd95SBruce Richardson 			RTE_ETHDEV_LOG(ERR,
499099a2dd95SBruce Richardson 				"ethdev bus specific initialisation failed\n");
499199a2dd95SBruce Richardson 			goto probe_failed;
499299a2dd95SBruce Richardson 		}
499399a2dd95SBruce Richardson 	}
499499a2dd95SBruce Richardson 
499599a2dd95SBruce Richardson 	retval = ethdev_init(ethdev, init_params);
499699a2dd95SBruce Richardson 	if (retval) {
499799a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "ethdev initialisation failed\n");
499899a2dd95SBruce Richardson 		goto probe_failed;
499999a2dd95SBruce Richardson 	}
500099a2dd95SBruce Richardson 
500199a2dd95SBruce Richardson 	rte_eth_dev_probing_finish(ethdev);
500299a2dd95SBruce Richardson 
500399a2dd95SBruce Richardson 	return retval;
500499a2dd95SBruce Richardson 
500599a2dd95SBruce Richardson probe_failed:
500699a2dd95SBruce Richardson 	rte_eth_dev_release_port(ethdev);
500799a2dd95SBruce Richardson 	return retval;
500899a2dd95SBruce Richardson }
500999a2dd95SBruce Richardson 
501099a2dd95SBruce Richardson int
501199a2dd95SBruce Richardson rte_eth_dev_destroy(struct rte_eth_dev *ethdev,
501299a2dd95SBruce Richardson 	ethdev_uninit_t ethdev_uninit)
501399a2dd95SBruce Richardson {
501499a2dd95SBruce Richardson 	int ret;
501599a2dd95SBruce Richardson 
501699a2dd95SBruce Richardson 	ethdev = rte_eth_dev_allocated(ethdev->data->name);
501799a2dd95SBruce Richardson 	if (!ethdev)
501899a2dd95SBruce Richardson 		return -ENODEV;
501999a2dd95SBruce Richardson 
502099a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*ethdev_uninit, -EINVAL);
502199a2dd95SBruce Richardson 
502299a2dd95SBruce Richardson 	ret = ethdev_uninit(ethdev);
502399a2dd95SBruce Richardson 	if (ret)
502499a2dd95SBruce Richardson 		return ret;
502599a2dd95SBruce Richardson 
502699a2dd95SBruce Richardson 	return rte_eth_dev_release_port(ethdev);
502799a2dd95SBruce Richardson }
502899a2dd95SBruce Richardson 
502999a2dd95SBruce Richardson int
503099a2dd95SBruce Richardson rte_eth_dev_rx_intr_ctl_q(uint16_t port_id, uint16_t queue_id,
503199a2dd95SBruce Richardson 			  int epfd, int op, void *data)
503299a2dd95SBruce Richardson {
503399a2dd95SBruce Richardson 	uint32_t vec;
503499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
503599a2dd95SBruce Richardson 	struct rte_intr_handle *intr_handle;
503699a2dd95SBruce Richardson 	int rc;
503799a2dd95SBruce Richardson 
503899a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
503999a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
504053ef1b34SMin Hu (Connor) 
504199a2dd95SBruce Richardson 	if (queue_id >= dev->data->nb_rx_queues) {
504209fd4227SAndrew Rybchenko 		RTE_ETHDEV_LOG(ERR, "Invalid Rx queue_id=%u\n", queue_id);
504399a2dd95SBruce Richardson 		return -EINVAL;
504499a2dd95SBruce Richardson 	}
504599a2dd95SBruce Richardson 
504699a2dd95SBruce Richardson 	if (!dev->intr_handle) {
504709fd4227SAndrew Rybchenko 		RTE_ETHDEV_LOG(ERR, "Rx Intr handle unset\n");
504899a2dd95SBruce Richardson 		return -ENOTSUP;
504999a2dd95SBruce Richardson 	}
505099a2dd95SBruce Richardson 
505199a2dd95SBruce Richardson 	intr_handle = dev->intr_handle;
5052c2bd9367SHarman Kalra 	if (rte_intr_vec_list_index_get(intr_handle, 0) < 0) {
505309fd4227SAndrew Rybchenko 		RTE_ETHDEV_LOG(ERR, "Rx Intr vector unset\n");
505499a2dd95SBruce Richardson 		return -EPERM;
505599a2dd95SBruce Richardson 	}
505699a2dd95SBruce Richardson 
5057c2bd9367SHarman Kalra 	vec = rte_intr_vec_list_index_get(intr_handle, queue_id);
505899a2dd95SBruce Richardson 	rc = rte_intr_rx_ctl(intr_handle, epfd, op, vec, data);
505999a2dd95SBruce Richardson 	if (rc && rc != -EEXIST) {
506099a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
506109fd4227SAndrew Rybchenko 			"p %u q %u Rx ctl error op %d epfd %d vec %u\n",
506299a2dd95SBruce Richardson 			port_id, queue_id, op, epfd, vec);
506399a2dd95SBruce Richardson 		return rc;
506499a2dd95SBruce Richardson 	}
506599a2dd95SBruce Richardson 
506699a2dd95SBruce Richardson 	return 0;
506799a2dd95SBruce Richardson }
506899a2dd95SBruce Richardson 
506999a2dd95SBruce Richardson int
507099a2dd95SBruce Richardson rte_eth_dev_rx_intr_enable(uint16_t port_id,
507199a2dd95SBruce Richardson 			   uint16_t queue_id)
507299a2dd95SBruce Richardson {
507399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
507499a2dd95SBruce Richardson 	int ret;
507599a2dd95SBruce Richardson 
507699a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
507799a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
507899a2dd95SBruce Richardson 
507999a2dd95SBruce Richardson 	ret = eth_dev_validate_rx_queue(dev, queue_id);
508099a2dd95SBruce Richardson 	if (ret != 0)
508199a2dd95SBruce Richardson 		return ret;
508299a2dd95SBruce Richardson 
508399a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_intr_enable, -ENOTSUP);
508453ef1b34SMin Hu (Connor) 	return eth_err(port_id, (*dev->dev_ops->rx_queue_intr_enable)(dev, queue_id));
508599a2dd95SBruce Richardson }
508699a2dd95SBruce Richardson 
508799a2dd95SBruce Richardson int
508899a2dd95SBruce Richardson rte_eth_dev_rx_intr_disable(uint16_t port_id,
508999a2dd95SBruce Richardson 			    uint16_t queue_id)
509099a2dd95SBruce Richardson {
509199a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
509299a2dd95SBruce Richardson 	int ret;
509399a2dd95SBruce Richardson 
509499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
509599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
509699a2dd95SBruce Richardson 
509799a2dd95SBruce Richardson 	ret = eth_dev_validate_rx_queue(dev, queue_id);
509899a2dd95SBruce Richardson 	if (ret != 0)
509999a2dd95SBruce Richardson 		return ret;
510099a2dd95SBruce Richardson 
510199a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_intr_disable, -ENOTSUP);
510253ef1b34SMin Hu (Connor) 	return eth_err(port_id, (*dev->dev_ops->rx_queue_intr_disable)(dev, queue_id));
510399a2dd95SBruce Richardson }
510499a2dd95SBruce Richardson 
510599a2dd95SBruce Richardson 
510699a2dd95SBruce Richardson const struct rte_eth_rxtx_callback *
510799a2dd95SBruce Richardson rte_eth_add_rx_callback(uint16_t port_id, uint16_t queue_id,
510899a2dd95SBruce Richardson 		rte_rx_callback_fn fn, void *user_param)
510999a2dd95SBruce Richardson {
511099a2dd95SBruce Richardson #ifndef RTE_ETHDEV_RXTX_CALLBACKS
511199a2dd95SBruce Richardson 	rte_errno = ENOTSUP;
511299a2dd95SBruce Richardson 	return NULL;
511399a2dd95SBruce Richardson #endif
511499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
511599a2dd95SBruce Richardson 
511699a2dd95SBruce Richardson 	/* check input parameters */
511799a2dd95SBruce Richardson 	if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
511899a2dd95SBruce Richardson 		    queue_id >= rte_eth_devices[port_id].data->nb_rx_queues) {
511999a2dd95SBruce Richardson 		rte_errno = EINVAL;
512099a2dd95SBruce Richardson 		return NULL;
512199a2dd95SBruce Richardson 	}
512299a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
512399a2dd95SBruce Richardson 	if (rte_eth_dev_is_rx_hairpin_queue(dev, queue_id)) {
512499a2dd95SBruce Richardson 		rte_errno = EINVAL;
512599a2dd95SBruce Richardson 		return NULL;
512699a2dd95SBruce Richardson 	}
512799a2dd95SBruce Richardson 	struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
512899a2dd95SBruce Richardson 
512999a2dd95SBruce Richardson 	if (cb == NULL) {
513099a2dd95SBruce Richardson 		rte_errno = ENOMEM;
513199a2dd95SBruce Richardson 		return NULL;
513299a2dd95SBruce Richardson 	}
513399a2dd95SBruce Richardson 
513499a2dd95SBruce Richardson 	cb->fn.rx = fn;
513599a2dd95SBruce Richardson 	cb->param = user_param;
513699a2dd95SBruce Richardson 
513799a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_rx_cb_lock);
513899a2dd95SBruce Richardson 	/* Add the callbacks in fifo order. */
513999a2dd95SBruce Richardson 	struct rte_eth_rxtx_callback *tail =
514099a2dd95SBruce Richardson 		rte_eth_devices[port_id].post_rx_burst_cbs[queue_id];
514199a2dd95SBruce Richardson 
514299a2dd95SBruce Richardson 	if (!tail) {
514399a2dd95SBruce Richardson 		/* Stores to cb->fn and cb->param should complete before
514499a2dd95SBruce Richardson 		 * cb is visible to data plane.
514599a2dd95SBruce Richardson 		 */
514699a2dd95SBruce Richardson 		__atomic_store_n(
514799a2dd95SBruce Richardson 			&rte_eth_devices[port_id].post_rx_burst_cbs[queue_id],
514899a2dd95SBruce Richardson 			cb, __ATOMIC_RELEASE);
514999a2dd95SBruce Richardson 
515099a2dd95SBruce Richardson 	} else {
515199a2dd95SBruce Richardson 		while (tail->next)
515299a2dd95SBruce Richardson 			tail = tail->next;
515399a2dd95SBruce Richardson 		/* Stores to cb->fn and cb->param should complete before
515499a2dd95SBruce Richardson 		 * cb is visible to data plane.
515599a2dd95SBruce Richardson 		 */
515699a2dd95SBruce Richardson 		__atomic_store_n(&tail->next, cb, __ATOMIC_RELEASE);
515799a2dd95SBruce Richardson 	}
515899a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_rx_cb_lock);
515999a2dd95SBruce Richardson 
516099a2dd95SBruce Richardson 	return cb;
516199a2dd95SBruce Richardson }
516299a2dd95SBruce Richardson 
516399a2dd95SBruce Richardson const struct rte_eth_rxtx_callback *
516499a2dd95SBruce Richardson rte_eth_add_first_rx_callback(uint16_t port_id, uint16_t queue_id,
516599a2dd95SBruce Richardson 		rte_rx_callback_fn fn, void *user_param)
516699a2dd95SBruce Richardson {
516799a2dd95SBruce Richardson #ifndef RTE_ETHDEV_RXTX_CALLBACKS
516899a2dd95SBruce Richardson 	rte_errno = ENOTSUP;
516999a2dd95SBruce Richardson 	return NULL;
517099a2dd95SBruce Richardson #endif
517199a2dd95SBruce Richardson 	/* check input parameters */
517299a2dd95SBruce Richardson 	if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
517399a2dd95SBruce Richardson 		queue_id >= rte_eth_devices[port_id].data->nb_rx_queues) {
517499a2dd95SBruce Richardson 		rte_errno = EINVAL;
517599a2dd95SBruce Richardson 		return NULL;
517699a2dd95SBruce Richardson 	}
517799a2dd95SBruce Richardson 
517899a2dd95SBruce Richardson 	struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
517999a2dd95SBruce Richardson 
518099a2dd95SBruce Richardson 	if (cb == NULL) {
518199a2dd95SBruce Richardson 		rte_errno = ENOMEM;
518299a2dd95SBruce Richardson 		return NULL;
518399a2dd95SBruce Richardson 	}
518499a2dd95SBruce Richardson 
518599a2dd95SBruce Richardson 	cb->fn.rx = fn;
518699a2dd95SBruce Richardson 	cb->param = user_param;
518799a2dd95SBruce Richardson 
518899a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_rx_cb_lock);
518999a2dd95SBruce Richardson 	/* Add the callbacks at first position */
519099a2dd95SBruce Richardson 	cb->next = rte_eth_devices[port_id].post_rx_burst_cbs[queue_id];
519199a2dd95SBruce Richardson 	/* Stores to cb->fn, cb->param and cb->next should complete before
519299a2dd95SBruce Richardson 	 * cb is visible to data plane threads.
519399a2dd95SBruce Richardson 	 */
519499a2dd95SBruce Richardson 	__atomic_store_n(
519599a2dd95SBruce Richardson 		&rte_eth_devices[port_id].post_rx_burst_cbs[queue_id],
519699a2dd95SBruce Richardson 		cb, __ATOMIC_RELEASE);
519799a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_rx_cb_lock);
519899a2dd95SBruce Richardson 
519999a2dd95SBruce Richardson 	return cb;
520099a2dd95SBruce Richardson }
520199a2dd95SBruce Richardson 
520299a2dd95SBruce Richardson const struct rte_eth_rxtx_callback *
520399a2dd95SBruce Richardson rte_eth_add_tx_callback(uint16_t port_id, uint16_t queue_id,
520499a2dd95SBruce Richardson 		rte_tx_callback_fn fn, void *user_param)
520599a2dd95SBruce Richardson {
520699a2dd95SBruce Richardson #ifndef RTE_ETHDEV_RXTX_CALLBACKS
520799a2dd95SBruce Richardson 	rte_errno = ENOTSUP;
520899a2dd95SBruce Richardson 	return NULL;
520999a2dd95SBruce Richardson #endif
521099a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
521199a2dd95SBruce Richardson 
521299a2dd95SBruce Richardson 	/* check input parameters */
521399a2dd95SBruce Richardson 	if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
521499a2dd95SBruce Richardson 		    queue_id >= rte_eth_devices[port_id].data->nb_tx_queues) {
521599a2dd95SBruce Richardson 		rte_errno = EINVAL;
521699a2dd95SBruce Richardson 		return NULL;
521799a2dd95SBruce Richardson 	}
521899a2dd95SBruce Richardson 
521999a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
522099a2dd95SBruce Richardson 	if (rte_eth_dev_is_tx_hairpin_queue(dev, queue_id)) {
522199a2dd95SBruce Richardson 		rte_errno = EINVAL;
522299a2dd95SBruce Richardson 		return NULL;
522399a2dd95SBruce Richardson 	}
522499a2dd95SBruce Richardson 
522599a2dd95SBruce Richardson 	struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
522699a2dd95SBruce Richardson 
522799a2dd95SBruce Richardson 	if (cb == NULL) {
522899a2dd95SBruce Richardson 		rte_errno = ENOMEM;
522999a2dd95SBruce Richardson 		return NULL;
523099a2dd95SBruce Richardson 	}
523199a2dd95SBruce Richardson 
523299a2dd95SBruce Richardson 	cb->fn.tx = fn;
523399a2dd95SBruce Richardson 	cb->param = user_param;
523499a2dd95SBruce Richardson 
523599a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_tx_cb_lock);
523699a2dd95SBruce Richardson 	/* Add the callbacks in fifo order. */
523799a2dd95SBruce Richardson 	struct rte_eth_rxtx_callback *tail =
523899a2dd95SBruce Richardson 		rte_eth_devices[port_id].pre_tx_burst_cbs[queue_id];
523999a2dd95SBruce Richardson 
524099a2dd95SBruce Richardson 	if (!tail) {
524199a2dd95SBruce Richardson 		/* Stores to cb->fn and cb->param should complete before
524299a2dd95SBruce Richardson 		 * cb is visible to data plane.
524399a2dd95SBruce Richardson 		 */
524499a2dd95SBruce Richardson 		__atomic_store_n(
524599a2dd95SBruce Richardson 			&rte_eth_devices[port_id].pre_tx_burst_cbs[queue_id],
524699a2dd95SBruce Richardson 			cb, __ATOMIC_RELEASE);
524799a2dd95SBruce Richardson 
524899a2dd95SBruce Richardson 	} else {
524999a2dd95SBruce Richardson 		while (tail->next)
525099a2dd95SBruce Richardson 			tail = tail->next;
525199a2dd95SBruce Richardson 		/* Stores to cb->fn and cb->param should complete before
525299a2dd95SBruce Richardson 		 * cb is visible to data plane.
525399a2dd95SBruce Richardson 		 */
525499a2dd95SBruce Richardson 		__atomic_store_n(&tail->next, cb, __ATOMIC_RELEASE);
525599a2dd95SBruce Richardson 	}
525699a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_tx_cb_lock);
525799a2dd95SBruce Richardson 
525899a2dd95SBruce Richardson 	return cb;
525999a2dd95SBruce Richardson }
526099a2dd95SBruce Richardson 
526199a2dd95SBruce Richardson int
526299a2dd95SBruce Richardson rte_eth_remove_rx_callback(uint16_t port_id, uint16_t queue_id,
526399a2dd95SBruce Richardson 		const struct rte_eth_rxtx_callback *user_cb)
526499a2dd95SBruce Richardson {
526599a2dd95SBruce Richardson #ifndef RTE_ETHDEV_RXTX_CALLBACKS
526699a2dd95SBruce Richardson 	return -ENOTSUP;
526799a2dd95SBruce Richardson #endif
526899a2dd95SBruce Richardson 	/* Check input parameters. */
526999a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
527099a2dd95SBruce Richardson 	if (user_cb == NULL ||
527199a2dd95SBruce Richardson 			queue_id >= rte_eth_devices[port_id].data->nb_rx_queues)
527299a2dd95SBruce Richardson 		return -EINVAL;
527399a2dd95SBruce Richardson 
527499a2dd95SBruce Richardson 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
527599a2dd95SBruce Richardson 	struct rte_eth_rxtx_callback *cb;
527699a2dd95SBruce Richardson 	struct rte_eth_rxtx_callback **prev_cb;
527799a2dd95SBruce Richardson 	int ret = -EINVAL;
527899a2dd95SBruce Richardson 
527999a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_rx_cb_lock);
528099a2dd95SBruce Richardson 	prev_cb = &dev->post_rx_burst_cbs[queue_id];
528199a2dd95SBruce Richardson 	for (; *prev_cb != NULL; prev_cb = &cb->next) {
528299a2dd95SBruce Richardson 		cb = *prev_cb;
528399a2dd95SBruce Richardson 		if (cb == user_cb) {
528499a2dd95SBruce Richardson 			/* Remove the user cb from the callback list. */
528599a2dd95SBruce Richardson 			__atomic_store_n(prev_cb, cb->next, __ATOMIC_RELAXED);
528699a2dd95SBruce Richardson 			ret = 0;
528799a2dd95SBruce Richardson 			break;
528899a2dd95SBruce Richardson 		}
528999a2dd95SBruce Richardson 	}
529099a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_rx_cb_lock);
529199a2dd95SBruce Richardson 
529299a2dd95SBruce Richardson 	return ret;
529399a2dd95SBruce Richardson }
529499a2dd95SBruce Richardson 
529599a2dd95SBruce Richardson int
529699a2dd95SBruce Richardson rte_eth_remove_tx_callback(uint16_t port_id, uint16_t queue_id,
529799a2dd95SBruce Richardson 		const struct rte_eth_rxtx_callback *user_cb)
529899a2dd95SBruce Richardson {
529999a2dd95SBruce Richardson #ifndef RTE_ETHDEV_RXTX_CALLBACKS
530099a2dd95SBruce Richardson 	return -ENOTSUP;
530199a2dd95SBruce Richardson #endif
530299a2dd95SBruce Richardson 	/* Check input parameters. */
530399a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
530499a2dd95SBruce Richardson 	if (user_cb == NULL ||
530599a2dd95SBruce Richardson 			queue_id >= rte_eth_devices[port_id].data->nb_tx_queues)
530699a2dd95SBruce Richardson 		return -EINVAL;
530799a2dd95SBruce Richardson 
530899a2dd95SBruce Richardson 	struct rte_eth_dev *dev = &rte_eth_devices[port_id];
530999a2dd95SBruce Richardson 	int ret = -EINVAL;
531099a2dd95SBruce Richardson 	struct rte_eth_rxtx_callback *cb;
531199a2dd95SBruce Richardson 	struct rte_eth_rxtx_callback **prev_cb;
531299a2dd95SBruce Richardson 
531399a2dd95SBruce Richardson 	rte_spinlock_lock(&eth_dev_tx_cb_lock);
531499a2dd95SBruce Richardson 	prev_cb = &dev->pre_tx_burst_cbs[queue_id];
531599a2dd95SBruce Richardson 	for (; *prev_cb != NULL; prev_cb = &cb->next) {
531699a2dd95SBruce Richardson 		cb = *prev_cb;
531799a2dd95SBruce Richardson 		if (cb == user_cb) {
531899a2dd95SBruce Richardson 			/* Remove the user cb from the callback list. */
531999a2dd95SBruce Richardson 			__atomic_store_n(prev_cb, cb->next, __ATOMIC_RELAXED);
532099a2dd95SBruce Richardson 			ret = 0;
532199a2dd95SBruce Richardson 			break;
532299a2dd95SBruce Richardson 		}
532399a2dd95SBruce Richardson 	}
532499a2dd95SBruce Richardson 	rte_spinlock_unlock(&eth_dev_tx_cb_lock);
532599a2dd95SBruce Richardson 
532699a2dd95SBruce Richardson 	return ret;
532799a2dd95SBruce Richardson }
532899a2dd95SBruce Richardson 
532999a2dd95SBruce Richardson int
533099a2dd95SBruce Richardson rte_eth_rx_queue_info_get(uint16_t port_id, uint16_t queue_id,
533199a2dd95SBruce Richardson 	struct rte_eth_rxq_info *qinfo)
533299a2dd95SBruce Richardson {
533399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
533499a2dd95SBruce Richardson 
533599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
533699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
533753ef1b34SMin Hu (Connor) 
533899a2dd95SBruce Richardson 	if (queue_id >= dev->data->nb_rx_queues) {
533909fd4227SAndrew Rybchenko 		RTE_ETHDEV_LOG(ERR, "Invalid Rx queue_id=%u\n", queue_id);
534099a2dd95SBruce Richardson 		return -EINVAL;
534199a2dd95SBruce Richardson 	}
534299a2dd95SBruce Richardson 
534353ef1b34SMin Hu (Connor) 	if (qinfo == NULL) {
534453ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot get ethdev port %u Rx queue %u info to NULL\n",
534553ef1b34SMin Hu (Connor) 			port_id, queue_id);
534653ef1b34SMin Hu (Connor) 		return -EINVAL;
534753ef1b34SMin Hu (Connor) 	}
534853ef1b34SMin Hu (Connor) 
534999a2dd95SBruce Richardson 	if (dev->data->rx_queues == NULL ||
535099a2dd95SBruce Richardson 			dev->data->rx_queues[queue_id] == NULL) {
535199a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
535299a2dd95SBruce Richardson 			       "Rx queue %"PRIu16" of device with port_id=%"
535399a2dd95SBruce Richardson 			       PRIu16" has not been setup\n",
535499a2dd95SBruce Richardson 			       queue_id, port_id);
535599a2dd95SBruce Richardson 		return -EINVAL;
535699a2dd95SBruce Richardson 	}
535799a2dd95SBruce Richardson 
535899a2dd95SBruce Richardson 	if (rte_eth_dev_is_rx_hairpin_queue(dev, queue_id)) {
535999a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(INFO,
536099a2dd95SBruce Richardson 			"Can't get hairpin Rx queue %"PRIu16" info of device with port_id=%"PRIu16"\n",
536199a2dd95SBruce Richardson 			queue_id, port_id);
536299a2dd95SBruce Richardson 		return -EINVAL;
536399a2dd95SBruce Richardson 	}
536499a2dd95SBruce Richardson 
536599a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rxq_info_get, -ENOTSUP);
536699a2dd95SBruce Richardson 
536799a2dd95SBruce Richardson 	memset(qinfo, 0, sizeof(*qinfo));
536899a2dd95SBruce Richardson 	dev->dev_ops->rxq_info_get(dev, queue_id, qinfo);
53699ad9ff47SLijun Ou 	qinfo->queue_state = dev->data->rx_queue_state[queue_id];
53709ad9ff47SLijun Ou 
537199a2dd95SBruce Richardson 	return 0;
537299a2dd95SBruce Richardson }
537399a2dd95SBruce Richardson 
537499a2dd95SBruce Richardson int
537599a2dd95SBruce Richardson rte_eth_tx_queue_info_get(uint16_t port_id, uint16_t queue_id,
537699a2dd95SBruce Richardson 	struct rte_eth_txq_info *qinfo)
537799a2dd95SBruce Richardson {
537899a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
537999a2dd95SBruce Richardson 
538099a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
538199a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
538253ef1b34SMin Hu (Connor) 
538399a2dd95SBruce Richardson 	if (queue_id >= dev->data->nb_tx_queues) {
538409fd4227SAndrew Rybchenko 		RTE_ETHDEV_LOG(ERR, "Invalid Tx queue_id=%u\n", queue_id);
538599a2dd95SBruce Richardson 		return -EINVAL;
538699a2dd95SBruce Richardson 	}
538799a2dd95SBruce Richardson 
538853ef1b34SMin Hu (Connor) 	if (qinfo == NULL) {
538953ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot get ethdev port %u Tx queue %u info to NULL\n",
539053ef1b34SMin Hu (Connor) 			port_id, queue_id);
539153ef1b34SMin Hu (Connor) 		return -EINVAL;
539253ef1b34SMin Hu (Connor) 	}
539353ef1b34SMin Hu (Connor) 
539499a2dd95SBruce Richardson 	if (dev->data->tx_queues == NULL ||
539599a2dd95SBruce Richardson 			dev->data->tx_queues[queue_id] == NULL) {
539699a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR,
539799a2dd95SBruce Richardson 			       "Tx queue %"PRIu16" of device with port_id=%"
539899a2dd95SBruce Richardson 			       PRIu16" has not been setup\n",
539999a2dd95SBruce Richardson 			       queue_id, port_id);
540099a2dd95SBruce Richardson 		return -EINVAL;
540199a2dd95SBruce Richardson 	}
540299a2dd95SBruce Richardson 
540399a2dd95SBruce Richardson 	if (rte_eth_dev_is_tx_hairpin_queue(dev, queue_id)) {
540499a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(INFO,
540599a2dd95SBruce Richardson 			"Can't get hairpin Tx queue %"PRIu16" info of device with port_id=%"PRIu16"\n",
540699a2dd95SBruce Richardson 			queue_id, port_id);
540799a2dd95SBruce Richardson 		return -EINVAL;
540899a2dd95SBruce Richardson 	}
540999a2dd95SBruce Richardson 
541099a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->txq_info_get, -ENOTSUP);
541199a2dd95SBruce Richardson 
541299a2dd95SBruce Richardson 	memset(qinfo, 0, sizeof(*qinfo));
541399a2dd95SBruce Richardson 	dev->dev_ops->txq_info_get(dev, queue_id, qinfo);
54149ad9ff47SLijun Ou 	qinfo->queue_state = dev->data->tx_queue_state[queue_id];
541599a2dd95SBruce Richardson 
541699a2dd95SBruce Richardson 	return 0;
541799a2dd95SBruce Richardson }
541899a2dd95SBruce Richardson 
541999a2dd95SBruce Richardson int
542099a2dd95SBruce Richardson rte_eth_rx_burst_mode_get(uint16_t port_id, uint16_t queue_id,
542199a2dd95SBruce Richardson 			  struct rte_eth_burst_mode *mode)
542299a2dd95SBruce Richardson {
542399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
542499a2dd95SBruce Richardson 
542599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
542699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
542799a2dd95SBruce Richardson 
542899a2dd95SBruce Richardson 	if (queue_id >= dev->data->nb_rx_queues) {
542909fd4227SAndrew Rybchenko 		RTE_ETHDEV_LOG(ERR, "Invalid Rx queue_id=%u\n", queue_id);
543099a2dd95SBruce Richardson 		return -EINVAL;
543199a2dd95SBruce Richardson 	}
543299a2dd95SBruce Richardson 
543353ef1b34SMin Hu (Connor) 	if (mode == NULL) {
543453ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
543553ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u Rx queue %u burst mode to NULL\n",
543653ef1b34SMin Hu (Connor) 			port_id, queue_id);
543753ef1b34SMin Hu (Connor) 		return -EINVAL;
543853ef1b34SMin Hu (Connor) 	}
543953ef1b34SMin Hu (Connor) 
544099a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_burst_mode_get, -ENOTSUP);
544199a2dd95SBruce Richardson 	memset(mode, 0, sizeof(*mode));
544299a2dd95SBruce Richardson 	return eth_err(port_id,
544399a2dd95SBruce Richardson 		       dev->dev_ops->rx_burst_mode_get(dev, queue_id, mode));
544499a2dd95SBruce Richardson }
544599a2dd95SBruce Richardson 
544699a2dd95SBruce Richardson int
544799a2dd95SBruce Richardson rte_eth_tx_burst_mode_get(uint16_t port_id, uint16_t queue_id,
544899a2dd95SBruce Richardson 			  struct rte_eth_burst_mode *mode)
544999a2dd95SBruce Richardson {
545099a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
545199a2dd95SBruce Richardson 
545299a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
545399a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
545499a2dd95SBruce Richardson 
545599a2dd95SBruce Richardson 	if (queue_id >= dev->data->nb_tx_queues) {
545609fd4227SAndrew Rybchenko 		RTE_ETHDEV_LOG(ERR, "Invalid Tx queue_id=%u\n", queue_id);
545799a2dd95SBruce Richardson 		return -EINVAL;
545899a2dd95SBruce Richardson 	}
545999a2dd95SBruce Richardson 
546053ef1b34SMin Hu (Connor) 	if (mode == NULL) {
546153ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
546253ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u Tx queue %u burst mode to NULL\n",
546353ef1b34SMin Hu (Connor) 			port_id, queue_id);
546453ef1b34SMin Hu (Connor) 		return -EINVAL;
546553ef1b34SMin Hu (Connor) 	}
546653ef1b34SMin Hu (Connor) 
546799a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_burst_mode_get, -ENOTSUP);
546899a2dd95SBruce Richardson 	memset(mode, 0, sizeof(*mode));
546999a2dd95SBruce Richardson 	return eth_err(port_id,
547099a2dd95SBruce Richardson 		       dev->dev_ops->tx_burst_mode_get(dev, queue_id, mode));
547199a2dd95SBruce Richardson }
547299a2dd95SBruce Richardson 
547399a2dd95SBruce Richardson int
547499a2dd95SBruce Richardson rte_eth_get_monitor_addr(uint16_t port_id, uint16_t queue_id,
547599a2dd95SBruce Richardson 		struct rte_power_monitor_cond *pmc)
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 	if (queue_id >= dev->data->nb_rx_queues) {
548399a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(ERR, "Invalid Rx queue_id=%u\n", queue_id);
548499a2dd95SBruce Richardson 		return -EINVAL;
548599a2dd95SBruce Richardson 	}
548699a2dd95SBruce Richardson 
548799a2dd95SBruce Richardson 	if (pmc == NULL) {
548853ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
548953ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u Rx queue %u power monitor condition to NULL\n",
549053ef1b34SMin Hu (Connor) 			port_id, queue_id);
549199a2dd95SBruce Richardson 		return -EINVAL;
549299a2dd95SBruce Richardson 	}
549399a2dd95SBruce Richardson 
549453ef1b34SMin Hu (Connor) 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_monitor_addr, -ENOTSUP);
549599a2dd95SBruce Richardson 	return eth_err(port_id,
549653ef1b34SMin Hu (Connor) 		dev->dev_ops->get_monitor_addr(dev->data->rx_queues[queue_id], pmc));
549799a2dd95SBruce Richardson }
549899a2dd95SBruce Richardson 
549999a2dd95SBruce Richardson int
550099a2dd95SBruce Richardson rte_eth_dev_set_mc_addr_list(uint16_t port_id,
550199a2dd95SBruce Richardson 			     struct rte_ether_addr *mc_addr_set,
550299a2dd95SBruce Richardson 			     uint32_t nb_mc_addr)
550399a2dd95SBruce Richardson {
550499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
550599a2dd95SBruce Richardson 
550699a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
550799a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
550853ef1b34SMin Hu (Connor) 
550999a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_mc_addr_list, -ENOTSUP);
551099a2dd95SBruce Richardson 	return eth_err(port_id, dev->dev_ops->set_mc_addr_list(dev,
551199a2dd95SBruce Richardson 						mc_addr_set, nb_mc_addr));
551299a2dd95SBruce Richardson }
551399a2dd95SBruce Richardson 
551499a2dd95SBruce Richardson int
551599a2dd95SBruce Richardson rte_eth_timesync_enable(uint16_t port_id)
551699a2dd95SBruce Richardson {
551799a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
551899a2dd95SBruce Richardson 
551999a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
552099a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
552199a2dd95SBruce Richardson 
552299a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_enable, -ENOTSUP);
552399a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->timesync_enable)(dev));
552499a2dd95SBruce Richardson }
552599a2dd95SBruce Richardson 
552699a2dd95SBruce Richardson int
552799a2dd95SBruce Richardson rte_eth_timesync_disable(uint16_t port_id)
552899a2dd95SBruce Richardson {
552999a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
553099a2dd95SBruce Richardson 
553199a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
553299a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
553399a2dd95SBruce Richardson 
553499a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_disable, -ENOTSUP);
553599a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->timesync_disable)(dev));
553699a2dd95SBruce Richardson }
553799a2dd95SBruce Richardson 
553899a2dd95SBruce Richardson int
553999a2dd95SBruce Richardson rte_eth_timesync_read_rx_timestamp(uint16_t port_id, struct timespec *timestamp,
554099a2dd95SBruce Richardson 				   uint32_t flags)
554199a2dd95SBruce Richardson {
554299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
554399a2dd95SBruce Richardson 
554499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
554599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
554699a2dd95SBruce Richardson 
554753ef1b34SMin Hu (Connor) 	if (timestamp == NULL) {
554853ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
554953ef1b34SMin Hu (Connor) 			"Cannot read ethdev port %u Rx timestamp to NULL\n",
555053ef1b34SMin Hu (Connor) 			port_id);
555153ef1b34SMin Hu (Connor) 		return -EINVAL;
555253ef1b34SMin Hu (Connor) 	}
555353ef1b34SMin Hu (Connor) 
555499a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_rx_timestamp, -ENOTSUP);
555599a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->timesync_read_rx_timestamp)
555699a2dd95SBruce Richardson 				(dev, timestamp, flags));
555799a2dd95SBruce Richardson }
555899a2dd95SBruce Richardson 
555999a2dd95SBruce Richardson int
556099a2dd95SBruce Richardson rte_eth_timesync_read_tx_timestamp(uint16_t port_id,
556199a2dd95SBruce Richardson 				   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 read ethdev port %u Tx timestamp to NULL\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_read_tx_timestamp, -ENOTSUP);
557699a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->timesync_read_tx_timestamp)
557799a2dd95SBruce Richardson 				(dev, timestamp));
557899a2dd95SBruce Richardson }
557999a2dd95SBruce Richardson 
558099a2dd95SBruce Richardson int
558199a2dd95SBruce Richardson rte_eth_timesync_adjust_time(uint16_t port_id, int64_t delta)
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 
558899a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_adjust_time, -ENOTSUP);
558953ef1b34SMin Hu (Connor) 	return eth_err(port_id, (*dev->dev_ops->timesync_adjust_time)(dev, delta));
559099a2dd95SBruce Richardson }
559199a2dd95SBruce Richardson 
559299a2dd95SBruce Richardson int
559399a2dd95SBruce Richardson rte_eth_timesync_read_time(uint16_t port_id, struct timespec *timestamp)
559499a2dd95SBruce Richardson {
559599a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
559699a2dd95SBruce Richardson 
559799a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
559899a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
559999a2dd95SBruce Richardson 
560053ef1b34SMin Hu (Connor) 	if (timestamp == NULL) {
560153ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
560253ef1b34SMin Hu (Connor) 			"Cannot read ethdev port %u timesync time to NULL\n",
560353ef1b34SMin Hu (Connor) 			port_id);
560453ef1b34SMin Hu (Connor) 		return -EINVAL;
560553ef1b34SMin Hu (Connor) 	}
560653ef1b34SMin Hu (Connor) 
560799a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_time, -ENOTSUP);
560899a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->timesync_read_time)(dev,
560999a2dd95SBruce Richardson 								timestamp));
561099a2dd95SBruce Richardson }
561199a2dd95SBruce Richardson 
561299a2dd95SBruce Richardson int
561399a2dd95SBruce Richardson rte_eth_timesync_write_time(uint16_t port_id, const struct timespec *timestamp)
561499a2dd95SBruce Richardson {
561599a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
561699a2dd95SBruce Richardson 
561799a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
561899a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
561999a2dd95SBruce Richardson 
562053ef1b34SMin Hu (Connor) 	if (timestamp == NULL) {
562153ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
562253ef1b34SMin Hu (Connor) 			"Cannot write ethdev port %u timesync from NULL time\n",
562353ef1b34SMin Hu (Connor) 			port_id);
562453ef1b34SMin Hu (Connor) 		return -EINVAL;
562553ef1b34SMin Hu (Connor) 	}
562653ef1b34SMin Hu (Connor) 
562799a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_write_time, -ENOTSUP);
562899a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->timesync_write_time)(dev,
562999a2dd95SBruce Richardson 								timestamp));
563099a2dd95SBruce Richardson }
563199a2dd95SBruce Richardson 
563299a2dd95SBruce Richardson int
563399a2dd95SBruce Richardson rte_eth_read_clock(uint16_t port_id, uint64_t *clock)
563499a2dd95SBruce Richardson {
563599a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
563699a2dd95SBruce Richardson 
563799a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
563899a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
563999a2dd95SBruce Richardson 
564053ef1b34SMin Hu (Connor) 	if (clock == NULL) {
564153ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR, "Cannot read ethdev port %u clock to NULL\n",
564253ef1b34SMin Hu (Connor) 			port_id);
564353ef1b34SMin Hu (Connor) 		return -EINVAL;
564453ef1b34SMin Hu (Connor) 	}
564553ef1b34SMin Hu (Connor) 
564699a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->read_clock, -ENOTSUP);
564799a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->read_clock)(dev, clock));
564899a2dd95SBruce Richardson }
564999a2dd95SBruce Richardson 
565099a2dd95SBruce Richardson int
565199a2dd95SBruce Richardson rte_eth_dev_get_reg_info(uint16_t port_id, struct rte_dev_reg_info *info)
565299a2dd95SBruce Richardson {
565399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
565499a2dd95SBruce Richardson 
565599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
565699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
565753ef1b34SMin Hu (Connor) 
565853ef1b34SMin Hu (Connor) 	if (info == NULL) {
565953ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
566053ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u register info to NULL\n",
566153ef1b34SMin Hu (Connor) 			port_id);
566253ef1b34SMin Hu (Connor) 		return -EINVAL;
566353ef1b34SMin Hu (Connor) 	}
566453ef1b34SMin Hu (Connor) 
566599a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_reg, -ENOTSUP);
566699a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->get_reg)(dev, info));
566799a2dd95SBruce Richardson }
566899a2dd95SBruce Richardson 
566999a2dd95SBruce Richardson int
567099a2dd95SBruce Richardson rte_eth_dev_get_eeprom_length(uint16_t port_id)
567199a2dd95SBruce Richardson {
567299a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
567399a2dd95SBruce Richardson 
567499a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
567599a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
567653ef1b34SMin Hu (Connor) 
567799a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_eeprom_length, -ENOTSUP);
567899a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->get_eeprom_length)(dev));
567999a2dd95SBruce Richardson }
568099a2dd95SBruce Richardson 
568199a2dd95SBruce Richardson int
568299a2dd95SBruce Richardson rte_eth_dev_get_eeprom(uint16_t port_id, struct rte_dev_eeprom_info *info)
568399a2dd95SBruce Richardson {
568499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
568599a2dd95SBruce Richardson 
568699a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
568799a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
568853ef1b34SMin Hu (Connor) 
568953ef1b34SMin Hu (Connor) 	if (info == NULL) {
569053ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
569153ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u EEPROM info to NULL\n",
569253ef1b34SMin Hu (Connor) 			port_id);
569353ef1b34SMin Hu (Connor) 		return -EINVAL;
569453ef1b34SMin Hu (Connor) 	}
569553ef1b34SMin Hu (Connor) 
569699a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_eeprom, -ENOTSUP);
569799a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->get_eeprom)(dev, info));
569899a2dd95SBruce Richardson }
569999a2dd95SBruce Richardson 
570099a2dd95SBruce Richardson int
570199a2dd95SBruce Richardson rte_eth_dev_set_eeprom(uint16_t port_id, struct rte_dev_eeprom_info *info)
570299a2dd95SBruce Richardson {
570399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
570499a2dd95SBruce Richardson 
570599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
570699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
570753ef1b34SMin Hu (Connor) 
570853ef1b34SMin Hu (Connor) 	if (info == NULL) {
570953ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
571053ef1b34SMin Hu (Connor) 			"Cannot set ethdev port %u EEPROM from NULL info\n",
571153ef1b34SMin Hu (Connor) 			port_id);
571253ef1b34SMin Hu (Connor) 		return -EINVAL;
571353ef1b34SMin Hu (Connor) 	}
571453ef1b34SMin Hu (Connor) 
571599a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_eeprom, -ENOTSUP);
571699a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->set_eeprom)(dev, info));
571799a2dd95SBruce Richardson }
571899a2dd95SBruce Richardson 
571999a2dd95SBruce Richardson int
572099a2dd95SBruce Richardson rte_eth_dev_get_module_info(uint16_t port_id,
572199a2dd95SBruce Richardson 			    struct rte_eth_dev_module_info *modinfo)
572299a2dd95SBruce Richardson {
572399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
572499a2dd95SBruce Richardson 
572599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
572699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
572753ef1b34SMin Hu (Connor) 
572853ef1b34SMin Hu (Connor) 	if (modinfo == NULL) {
572953ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
573053ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u EEPROM module info to NULL\n",
573153ef1b34SMin Hu (Connor) 			port_id);
573253ef1b34SMin Hu (Connor) 		return -EINVAL;
573353ef1b34SMin Hu (Connor) 	}
573453ef1b34SMin Hu (Connor) 
573599a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_module_info, -ENOTSUP);
573699a2dd95SBruce Richardson 	return (*dev->dev_ops->get_module_info)(dev, modinfo);
573799a2dd95SBruce Richardson }
573899a2dd95SBruce Richardson 
573999a2dd95SBruce Richardson int
574099a2dd95SBruce Richardson rte_eth_dev_get_module_eeprom(uint16_t port_id,
574199a2dd95SBruce Richardson 			      struct rte_dev_eeprom_info *info)
574299a2dd95SBruce Richardson {
574399a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
574499a2dd95SBruce Richardson 
574599a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
574699a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
574753ef1b34SMin Hu (Connor) 
574853ef1b34SMin Hu (Connor) 	if (info == NULL) {
574953ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
575053ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u module EEPROM info to NULL\n",
575153ef1b34SMin Hu (Connor) 			port_id);
575253ef1b34SMin Hu (Connor) 		return -EINVAL;
575353ef1b34SMin Hu (Connor) 	}
575453ef1b34SMin Hu (Connor) 
575553ef1b34SMin Hu (Connor) 	if (info->data == NULL) {
575653ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
575753ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u module EEPROM data to NULL\n",
575853ef1b34SMin Hu (Connor) 			port_id);
575953ef1b34SMin Hu (Connor) 		return -EINVAL;
576053ef1b34SMin Hu (Connor) 	}
576153ef1b34SMin Hu (Connor) 
576253ef1b34SMin Hu (Connor) 	if (info->length == 0) {
576353ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
576453ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u module EEPROM to data with zero size\n",
576553ef1b34SMin Hu (Connor) 			port_id);
576653ef1b34SMin Hu (Connor) 		return -EINVAL;
576753ef1b34SMin Hu (Connor) 	}
576853ef1b34SMin Hu (Connor) 
576999a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_module_eeprom, -ENOTSUP);
577099a2dd95SBruce Richardson 	return (*dev->dev_ops->get_module_eeprom)(dev, info);
577199a2dd95SBruce Richardson }
577299a2dd95SBruce Richardson 
577399a2dd95SBruce Richardson int
577499a2dd95SBruce Richardson rte_eth_dev_get_dcb_info(uint16_t port_id,
577599a2dd95SBruce Richardson 			     struct rte_eth_dcb_info *dcb_info)
577699a2dd95SBruce Richardson {
577799a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
577899a2dd95SBruce Richardson 
577999a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
578099a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
578153ef1b34SMin Hu (Connor) 
578253ef1b34SMin Hu (Connor) 	if (dcb_info == NULL) {
578353ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
578453ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u DCB info to NULL\n",
578553ef1b34SMin Hu (Connor) 			port_id);
578653ef1b34SMin Hu (Connor) 		return -EINVAL;
578753ef1b34SMin Hu (Connor) 	}
578853ef1b34SMin Hu (Connor) 
578999a2dd95SBruce Richardson 	memset(dcb_info, 0, sizeof(struct rte_eth_dcb_info));
579099a2dd95SBruce Richardson 
579199a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_dcb_info, -ENOTSUP);
579299a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->get_dcb_info)(dev, dcb_info));
579399a2dd95SBruce Richardson }
579499a2dd95SBruce Richardson 
579599a2dd95SBruce Richardson static void
579699a2dd95SBruce Richardson eth_dev_adjust_nb_desc(uint16_t *nb_desc,
579799a2dd95SBruce Richardson 		const struct rte_eth_desc_lim *desc_lim)
579899a2dd95SBruce Richardson {
579999a2dd95SBruce Richardson 	if (desc_lim->nb_align != 0)
580099a2dd95SBruce Richardson 		*nb_desc = RTE_ALIGN_CEIL(*nb_desc, desc_lim->nb_align);
580199a2dd95SBruce Richardson 
580299a2dd95SBruce Richardson 	if (desc_lim->nb_max != 0)
580399a2dd95SBruce Richardson 		*nb_desc = RTE_MIN(*nb_desc, desc_lim->nb_max);
580499a2dd95SBruce Richardson 
580599a2dd95SBruce Richardson 	*nb_desc = RTE_MAX(*nb_desc, desc_lim->nb_min);
580699a2dd95SBruce Richardson }
580799a2dd95SBruce Richardson 
580899a2dd95SBruce Richardson int
580999a2dd95SBruce Richardson rte_eth_dev_adjust_nb_rx_tx_desc(uint16_t port_id,
581099a2dd95SBruce Richardson 				 uint16_t *nb_rx_desc,
581199a2dd95SBruce Richardson 				 uint16_t *nb_tx_desc)
581299a2dd95SBruce Richardson {
581399a2dd95SBruce Richardson 	struct rte_eth_dev_info dev_info;
581499a2dd95SBruce Richardson 	int ret;
581599a2dd95SBruce Richardson 
581699a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
581799a2dd95SBruce Richardson 
581899a2dd95SBruce Richardson 	ret = rte_eth_dev_info_get(port_id, &dev_info);
581999a2dd95SBruce Richardson 	if (ret != 0)
582099a2dd95SBruce Richardson 		return ret;
582199a2dd95SBruce Richardson 
582299a2dd95SBruce Richardson 	if (nb_rx_desc != NULL)
582399a2dd95SBruce Richardson 		eth_dev_adjust_nb_desc(nb_rx_desc, &dev_info.rx_desc_lim);
582499a2dd95SBruce Richardson 
582599a2dd95SBruce Richardson 	if (nb_tx_desc != NULL)
582699a2dd95SBruce Richardson 		eth_dev_adjust_nb_desc(nb_tx_desc, &dev_info.tx_desc_lim);
582799a2dd95SBruce Richardson 
582899a2dd95SBruce Richardson 	return 0;
582999a2dd95SBruce Richardson }
583099a2dd95SBruce Richardson 
583199a2dd95SBruce Richardson int
583299a2dd95SBruce Richardson rte_eth_dev_hairpin_capability_get(uint16_t port_id,
583399a2dd95SBruce Richardson 				   struct rte_eth_hairpin_cap *cap)
583499a2dd95SBruce Richardson {
583599a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
583699a2dd95SBruce Richardson 
583799a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
583899a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
583953ef1b34SMin Hu (Connor) 
584053ef1b34SMin Hu (Connor) 	if (cap == NULL) {
584153ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
584253ef1b34SMin Hu (Connor) 			"Cannot get ethdev port %u hairpin capability to NULL\n",
584353ef1b34SMin Hu (Connor) 			port_id);
584453ef1b34SMin Hu (Connor) 		return -EINVAL;
584553ef1b34SMin Hu (Connor) 	}
584653ef1b34SMin Hu (Connor) 
584799a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->hairpin_cap_get, -ENOTSUP);
584899a2dd95SBruce Richardson 	memset(cap, 0, sizeof(*cap));
584999a2dd95SBruce Richardson 	return eth_err(port_id, (*dev->dev_ops->hairpin_cap_get)(dev, cap));
585099a2dd95SBruce Richardson }
585199a2dd95SBruce Richardson 
585299a2dd95SBruce Richardson int
585399a2dd95SBruce Richardson rte_eth_dev_is_rx_hairpin_queue(struct rte_eth_dev *dev, uint16_t queue_id)
585499a2dd95SBruce Richardson {
585553ef1b34SMin Hu (Connor) 	if (dev->data->rx_queue_state[queue_id] == RTE_ETH_QUEUE_STATE_HAIRPIN)
585699a2dd95SBruce Richardson 		return 1;
585799a2dd95SBruce Richardson 	return 0;
585899a2dd95SBruce Richardson }
585999a2dd95SBruce Richardson 
586099a2dd95SBruce Richardson int
586199a2dd95SBruce Richardson rte_eth_dev_is_tx_hairpin_queue(struct rte_eth_dev *dev, uint16_t queue_id)
586299a2dd95SBruce Richardson {
586353ef1b34SMin Hu (Connor) 	if (dev->data->tx_queue_state[queue_id] == RTE_ETH_QUEUE_STATE_HAIRPIN)
586499a2dd95SBruce Richardson 		return 1;
586599a2dd95SBruce Richardson 	return 0;
586699a2dd95SBruce Richardson }
586799a2dd95SBruce Richardson 
586899a2dd95SBruce Richardson int
586999a2dd95SBruce Richardson rte_eth_dev_pool_ops_supported(uint16_t port_id, const char *pool)
587099a2dd95SBruce Richardson {
587199a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
587299a2dd95SBruce Richardson 
587399a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
587499a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
587599a2dd95SBruce Richardson 
587653ef1b34SMin Hu (Connor) 	if (pool == NULL) {
587753ef1b34SMin Hu (Connor) 		RTE_ETHDEV_LOG(ERR,
587853ef1b34SMin Hu (Connor) 			"Cannot test ethdev port %u mempool operation from NULL pool\n",
587953ef1b34SMin Hu (Connor) 			port_id);
588053ef1b34SMin Hu (Connor) 		return -EINVAL;
588153ef1b34SMin Hu (Connor) 	}
588253ef1b34SMin Hu (Connor) 
588399a2dd95SBruce Richardson 	if (*dev->dev_ops->pool_ops_supported == NULL)
588499a2dd95SBruce Richardson 		return 1; /* all pools are supported */
588599a2dd95SBruce Richardson 
588699a2dd95SBruce Richardson 	return (*dev->dev_ops->pool_ops_supported)(dev, pool);
588799a2dd95SBruce Richardson }
588899a2dd95SBruce Richardson 
588999a2dd95SBruce Richardson /**
589099a2dd95SBruce Richardson  * A set of values to describe the possible states of a switch domain.
589199a2dd95SBruce Richardson  */
589299a2dd95SBruce Richardson enum rte_eth_switch_domain_state {
589399a2dd95SBruce Richardson 	RTE_ETH_SWITCH_DOMAIN_UNUSED = 0,
589499a2dd95SBruce Richardson 	RTE_ETH_SWITCH_DOMAIN_ALLOCATED
589599a2dd95SBruce Richardson };
589699a2dd95SBruce Richardson 
589799a2dd95SBruce Richardson /**
589899a2dd95SBruce Richardson  * Array of switch domains available for allocation. Array is sized to
589999a2dd95SBruce Richardson  * RTE_MAX_ETHPORTS elements as there cannot be more active switch domains than
590099a2dd95SBruce Richardson  * ethdev ports in a single process.
590199a2dd95SBruce Richardson  */
590299a2dd95SBruce Richardson static struct rte_eth_dev_switch {
590399a2dd95SBruce Richardson 	enum rte_eth_switch_domain_state state;
590499a2dd95SBruce Richardson } eth_dev_switch_domains[RTE_MAX_ETHPORTS];
590599a2dd95SBruce Richardson 
590699a2dd95SBruce Richardson int
590799a2dd95SBruce Richardson rte_eth_switch_domain_alloc(uint16_t *domain_id)
590899a2dd95SBruce Richardson {
590999a2dd95SBruce Richardson 	uint16_t i;
591099a2dd95SBruce Richardson 
591199a2dd95SBruce Richardson 	*domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
591299a2dd95SBruce Richardson 
591399a2dd95SBruce Richardson 	for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
591499a2dd95SBruce Richardson 		if (eth_dev_switch_domains[i].state ==
591599a2dd95SBruce Richardson 			RTE_ETH_SWITCH_DOMAIN_UNUSED) {
591699a2dd95SBruce Richardson 			eth_dev_switch_domains[i].state =
591799a2dd95SBruce Richardson 				RTE_ETH_SWITCH_DOMAIN_ALLOCATED;
591899a2dd95SBruce Richardson 			*domain_id = i;
591999a2dd95SBruce Richardson 			return 0;
592099a2dd95SBruce Richardson 		}
592199a2dd95SBruce Richardson 	}
592299a2dd95SBruce Richardson 
592399a2dd95SBruce Richardson 	return -ENOSPC;
592499a2dd95SBruce Richardson }
592599a2dd95SBruce Richardson 
592699a2dd95SBruce Richardson int
592799a2dd95SBruce Richardson rte_eth_switch_domain_free(uint16_t domain_id)
592899a2dd95SBruce Richardson {
592999a2dd95SBruce Richardson 	if (domain_id == RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID ||
593099a2dd95SBruce Richardson 		domain_id >= RTE_MAX_ETHPORTS)
593199a2dd95SBruce Richardson 		return -EINVAL;
593299a2dd95SBruce Richardson 
593399a2dd95SBruce Richardson 	if (eth_dev_switch_domains[domain_id].state !=
593499a2dd95SBruce Richardson 		RTE_ETH_SWITCH_DOMAIN_ALLOCATED)
593599a2dd95SBruce Richardson 		return -EINVAL;
593699a2dd95SBruce Richardson 
593799a2dd95SBruce Richardson 	eth_dev_switch_domains[domain_id].state = RTE_ETH_SWITCH_DOMAIN_UNUSED;
593899a2dd95SBruce Richardson 
593999a2dd95SBruce Richardson 	return 0;
594099a2dd95SBruce Richardson }
594199a2dd95SBruce Richardson 
594299a2dd95SBruce Richardson static int
594399a2dd95SBruce Richardson eth_dev_devargs_tokenise(struct rte_kvargs *arglist, const char *str_in)
594499a2dd95SBruce Richardson {
594599a2dd95SBruce Richardson 	int state;
594699a2dd95SBruce Richardson 	struct rte_kvargs_pair *pair;
594799a2dd95SBruce Richardson 	char *letter;
594899a2dd95SBruce Richardson 
594999a2dd95SBruce Richardson 	arglist->str = strdup(str_in);
595099a2dd95SBruce Richardson 	if (arglist->str == NULL)
595199a2dd95SBruce Richardson 		return -ENOMEM;
595299a2dd95SBruce Richardson 
595399a2dd95SBruce Richardson 	letter = arglist->str;
595499a2dd95SBruce Richardson 	state = 0;
595599a2dd95SBruce Richardson 	arglist->count = 0;
595699a2dd95SBruce Richardson 	pair = &arglist->pairs[0];
595799a2dd95SBruce Richardson 	while (1) {
595899a2dd95SBruce Richardson 		switch (state) {
595999a2dd95SBruce Richardson 		case 0: /* Initial */
596099a2dd95SBruce Richardson 			if (*letter == '=')
596199a2dd95SBruce Richardson 				return -EINVAL;
596299a2dd95SBruce Richardson 			else if (*letter == '\0')
596399a2dd95SBruce Richardson 				return 0;
596499a2dd95SBruce Richardson 
596599a2dd95SBruce Richardson 			state = 1;
596699a2dd95SBruce Richardson 			pair->key = letter;
596799a2dd95SBruce Richardson 			/* fall-thru */
596899a2dd95SBruce Richardson 
596999a2dd95SBruce Richardson 		case 1: /* Parsing key */
597099a2dd95SBruce Richardson 			if (*letter == '=') {
597199a2dd95SBruce Richardson 				*letter = '\0';
597299a2dd95SBruce Richardson 				pair->value = letter + 1;
597399a2dd95SBruce Richardson 				state = 2;
597499a2dd95SBruce Richardson 			} else if (*letter == ',' || *letter == '\0')
597599a2dd95SBruce Richardson 				return -EINVAL;
597699a2dd95SBruce Richardson 			break;
597799a2dd95SBruce Richardson 
597899a2dd95SBruce Richardson 
597999a2dd95SBruce Richardson 		case 2: /* Parsing value */
598099a2dd95SBruce Richardson 			if (*letter == '[')
598199a2dd95SBruce Richardson 				state = 3;
598299a2dd95SBruce Richardson 			else if (*letter == ',') {
598399a2dd95SBruce Richardson 				*letter = '\0';
598499a2dd95SBruce Richardson 				arglist->count++;
598599a2dd95SBruce Richardson 				pair = &arglist->pairs[arglist->count];
598699a2dd95SBruce Richardson 				state = 0;
598799a2dd95SBruce Richardson 			} else if (*letter == '\0') {
598899a2dd95SBruce Richardson 				letter--;
598999a2dd95SBruce Richardson 				arglist->count++;
599099a2dd95SBruce Richardson 				pair = &arglist->pairs[arglist->count];
599199a2dd95SBruce Richardson 				state = 0;
599299a2dd95SBruce Richardson 			}
599399a2dd95SBruce Richardson 			break;
599499a2dd95SBruce Richardson 
599599a2dd95SBruce Richardson 		case 3: /* Parsing list */
599699a2dd95SBruce Richardson 			if (*letter == ']')
599799a2dd95SBruce Richardson 				state = 2;
599899a2dd95SBruce Richardson 			else if (*letter == '\0')
599999a2dd95SBruce Richardson 				return -EINVAL;
600099a2dd95SBruce Richardson 			break;
600199a2dd95SBruce Richardson 		}
600299a2dd95SBruce Richardson 		letter++;
600399a2dd95SBruce Richardson 	}
600499a2dd95SBruce Richardson }
600599a2dd95SBruce Richardson 
600699a2dd95SBruce Richardson int
600799a2dd95SBruce Richardson rte_eth_devargs_parse(const char *dargs, struct rte_eth_devargs *eth_da)
600899a2dd95SBruce Richardson {
600999a2dd95SBruce Richardson 	struct rte_kvargs args;
601099a2dd95SBruce Richardson 	struct rte_kvargs_pair *pair;
601199a2dd95SBruce Richardson 	unsigned int i;
601299a2dd95SBruce Richardson 	int result = 0;
601399a2dd95SBruce Richardson 
601499a2dd95SBruce Richardson 	memset(eth_da, 0, sizeof(*eth_da));
601599a2dd95SBruce Richardson 
601699a2dd95SBruce Richardson 	result = eth_dev_devargs_tokenise(&args, dargs);
601799a2dd95SBruce Richardson 	if (result < 0)
601899a2dd95SBruce Richardson 		goto parse_cleanup;
601999a2dd95SBruce Richardson 
602099a2dd95SBruce Richardson 	for (i = 0; i < args.count; i++) {
602199a2dd95SBruce Richardson 		pair = &args.pairs[i];
602299a2dd95SBruce Richardson 		if (strcmp("representor", pair->key) == 0) {
602399a2dd95SBruce Richardson 			if (eth_da->type != RTE_ETH_REPRESENTOR_NONE) {
602499a2dd95SBruce Richardson 				RTE_LOG(ERR, EAL, "duplicated representor key: %s\n",
602599a2dd95SBruce Richardson 					dargs);
602699a2dd95SBruce Richardson 				result = -1;
602799a2dd95SBruce Richardson 				goto parse_cleanup;
602899a2dd95SBruce Richardson 			}
602999a2dd95SBruce Richardson 			result = rte_eth_devargs_parse_representor_ports(
603099a2dd95SBruce Richardson 					pair->value, eth_da);
603199a2dd95SBruce Richardson 			if (result < 0)
603299a2dd95SBruce Richardson 				goto parse_cleanup;
603399a2dd95SBruce Richardson 		}
603499a2dd95SBruce Richardson 	}
603599a2dd95SBruce Richardson 
603699a2dd95SBruce Richardson parse_cleanup:
603799a2dd95SBruce Richardson 	if (args.str)
603899a2dd95SBruce Richardson 		free(args.str);
603999a2dd95SBruce Richardson 
604099a2dd95SBruce Richardson 	return result;
604199a2dd95SBruce Richardson }
604299a2dd95SBruce Richardson 
604399a2dd95SBruce Richardson int
6044ff4e52efSViacheslav Galaktionov rte_eth_representor_id_get(uint16_t port_id,
604599a2dd95SBruce Richardson 			   enum rte_eth_representor_type type,
604699a2dd95SBruce Richardson 			   int controller, int pf, int representor_port,
604799a2dd95SBruce Richardson 			   uint16_t *repr_id)
604899a2dd95SBruce Richardson {
604910eaf41dSViacheslav Galaktionov 	int ret, n, count;
605010eaf41dSViacheslav Galaktionov 	uint32_t i;
605199a2dd95SBruce Richardson 	struct rte_eth_representor_info *info = NULL;
605299a2dd95SBruce Richardson 	size_t size;
605399a2dd95SBruce Richardson 
605499a2dd95SBruce Richardson 	if (type == RTE_ETH_REPRESENTOR_NONE)
605599a2dd95SBruce Richardson 		return 0;
605699a2dd95SBruce Richardson 	if (repr_id == NULL)
605799a2dd95SBruce Richardson 		return -EINVAL;
605899a2dd95SBruce Richardson 
605999a2dd95SBruce Richardson 	/* Get PMD representor range info. */
6060ff4e52efSViacheslav Galaktionov 	ret = rte_eth_representor_info_get(port_id, NULL);
606199a2dd95SBruce Richardson 	if (ret == -ENOTSUP && type == RTE_ETH_REPRESENTOR_VF &&
606299a2dd95SBruce Richardson 	    controller == -1 && pf == -1) {
606399a2dd95SBruce Richardson 		/* Direct mapping for legacy VF representor. */
606499a2dd95SBruce Richardson 		*repr_id = representor_port;
606599a2dd95SBruce Richardson 		return 0;
606699a2dd95SBruce Richardson 	} else if (ret < 0) {
606799a2dd95SBruce Richardson 		return ret;
606899a2dd95SBruce Richardson 	}
606999a2dd95SBruce Richardson 	n = ret;
607099a2dd95SBruce Richardson 	size = sizeof(*info) + n * sizeof(info->ranges[0]);
607199a2dd95SBruce Richardson 	info = calloc(1, size);
607299a2dd95SBruce Richardson 	if (info == NULL)
607399a2dd95SBruce Richardson 		return -ENOMEM;
607410eaf41dSViacheslav Galaktionov 	info->nb_ranges_alloc = n;
6075ff4e52efSViacheslav Galaktionov 	ret = rte_eth_representor_info_get(port_id, info);
607699a2dd95SBruce Richardson 	if (ret < 0)
607799a2dd95SBruce Richardson 		goto out;
607899a2dd95SBruce Richardson 
607999a2dd95SBruce Richardson 	/* Default controller and pf to caller. */
608099a2dd95SBruce Richardson 	if (controller == -1)
608199a2dd95SBruce Richardson 		controller = info->controller;
608299a2dd95SBruce Richardson 	if (pf == -1)
608399a2dd95SBruce Richardson 		pf = info->pf;
608499a2dd95SBruce Richardson 
608599a2dd95SBruce Richardson 	/* Locate representor ID. */
608699a2dd95SBruce Richardson 	ret = -ENOENT;
608710eaf41dSViacheslav Galaktionov 	for (i = 0; i < info->nb_ranges; ++i) {
608899a2dd95SBruce Richardson 		if (info->ranges[i].type != type)
608999a2dd95SBruce Richardson 			continue;
609099a2dd95SBruce Richardson 		if (info->ranges[i].controller != controller)
609199a2dd95SBruce Richardson 			continue;
609299a2dd95SBruce Richardson 		if (info->ranges[i].id_end < info->ranges[i].id_base) {
609399a2dd95SBruce Richardson 			RTE_LOG(WARNING, EAL, "Port %hu invalid representor ID Range %u - %u, entry %d\n",
6094ff4e52efSViacheslav Galaktionov 				port_id, info->ranges[i].id_base,
609599a2dd95SBruce Richardson 				info->ranges[i].id_end, i);
609699a2dd95SBruce Richardson 			continue;
609799a2dd95SBruce Richardson 
609899a2dd95SBruce Richardson 		}
609999a2dd95SBruce Richardson 		count = info->ranges[i].id_end - info->ranges[i].id_base + 1;
610099a2dd95SBruce Richardson 		switch (info->ranges[i].type) {
610199a2dd95SBruce Richardson 		case RTE_ETH_REPRESENTOR_PF:
610299a2dd95SBruce Richardson 			if (pf < info->ranges[i].pf ||
610399a2dd95SBruce Richardson 			    pf >= info->ranges[i].pf + count)
610499a2dd95SBruce Richardson 				continue;
610599a2dd95SBruce Richardson 			*repr_id = info->ranges[i].id_base +
610699a2dd95SBruce Richardson 				   (pf - info->ranges[i].pf);
610799a2dd95SBruce Richardson 			ret = 0;
610899a2dd95SBruce Richardson 			goto out;
610999a2dd95SBruce Richardson 		case RTE_ETH_REPRESENTOR_VF:
611099a2dd95SBruce Richardson 			if (info->ranges[i].pf != pf)
611199a2dd95SBruce Richardson 				continue;
611299a2dd95SBruce Richardson 			if (representor_port < info->ranges[i].vf ||
611399a2dd95SBruce Richardson 			    representor_port >= info->ranges[i].vf + count)
611499a2dd95SBruce Richardson 				continue;
611599a2dd95SBruce Richardson 			*repr_id = info->ranges[i].id_base +
611699a2dd95SBruce Richardson 				   (representor_port - info->ranges[i].vf);
611799a2dd95SBruce Richardson 			ret = 0;
611899a2dd95SBruce Richardson 			goto out;
611999a2dd95SBruce Richardson 		case RTE_ETH_REPRESENTOR_SF:
612099a2dd95SBruce Richardson 			if (info->ranges[i].pf != pf)
612199a2dd95SBruce Richardson 				continue;
612299a2dd95SBruce Richardson 			if (representor_port < info->ranges[i].sf ||
612399a2dd95SBruce Richardson 			    representor_port >= info->ranges[i].sf + count)
612499a2dd95SBruce Richardson 				continue;
612599a2dd95SBruce Richardson 			*repr_id = info->ranges[i].id_base +
612699a2dd95SBruce Richardson 			      (representor_port - info->ranges[i].sf);
612799a2dd95SBruce Richardson 			ret = 0;
612899a2dd95SBruce Richardson 			goto out;
612999a2dd95SBruce Richardson 		default:
613099a2dd95SBruce Richardson 			break;
613199a2dd95SBruce Richardson 		}
613299a2dd95SBruce Richardson 	}
613399a2dd95SBruce Richardson out:
613499a2dd95SBruce Richardson 	free(info);
613599a2dd95SBruce Richardson 	return ret;
613699a2dd95SBruce Richardson }
613799a2dd95SBruce Richardson 
613899a2dd95SBruce Richardson static int
613999a2dd95SBruce Richardson eth_dev_handle_port_list(const char *cmd __rte_unused,
614099a2dd95SBruce Richardson 		const char *params __rte_unused,
614199a2dd95SBruce Richardson 		struct rte_tel_data *d)
614299a2dd95SBruce Richardson {
614399a2dd95SBruce Richardson 	int port_id;
614499a2dd95SBruce Richardson 
614599a2dd95SBruce Richardson 	rte_tel_data_start_array(d, RTE_TEL_INT_VAL);
614699a2dd95SBruce Richardson 	RTE_ETH_FOREACH_DEV(port_id)
614799a2dd95SBruce Richardson 		rte_tel_data_add_array_int(d, port_id);
614899a2dd95SBruce Richardson 	return 0;
614999a2dd95SBruce Richardson }
615099a2dd95SBruce Richardson 
615199a2dd95SBruce Richardson static void
615299a2dd95SBruce Richardson eth_dev_add_port_queue_stats(struct rte_tel_data *d, uint64_t *q_stats,
615399a2dd95SBruce Richardson 		const char *stat_name)
615499a2dd95SBruce Richardson {
615599a2dd95SBruce Richardson 	int q;
615699a2dd95SBruce Richardson 	struct rte_tel_data *q_data = rte_tel_data_alloc();
615799a2dd95SBruce Richardson 	rte_tel_data_start_array(q_data, RTE_TEL_U64_VAL);
615899a2dd95SBruce Richardson 	for (q = 0; q < RTE_ETHDEV_QUEUE_STAT_CNTRS; q++)
615999a2dd95SBruce Richardson 		rte_tel_data_add_array_u64(q_data, q_stats[q]);
616099a2dd95SBruce Richardson 	rte_tel_data_add_dict_container(d, stat_name, q_data, 0);
616199a2dd95SBruce Richardson }
616299a2dd95SBruce Richardson 
616399a2dd95SBruce Richardson #define ADD_DICT_STAT(stats, s) rte_tel_data_add_dict_u64(d, #s, stats.s)
616499a2dd95SBruce Richardson 
616599a2dd95SBruce Richardson static int
616699a2dd95SBruce Richardson eth_dev_handle_port_stats(const char *cmd __rte_unused,
616799a2dd95SBruce Richardson 		const char *params,
616899a2dd95SBruce Richardson 		struct rte_tel_data *d)
616999a2dd95SBruce Richardson {
617099a2dd95SBruce Richardson 	struct rte_eth_stats stats;
617199a2dd95SBruce Richardson 	int port_id, ret;
617299a2dd95SBruce Richardson 
617399a2dd95SBruce Richardson 	if (params == NULL || strlen(params) == 0 || !isdigit(*params))
617499a2dd95SBruce Richardson 		return -1;
617599a2dd95SBruce Richardson 
617699a2dd95SBruce Richardson 	port_id = atoi(params);
617799a2dd95SBruce Richardson 	if (!rte_eth_dev_is_valid_port(port_id))
617899a2dd95SBruce Richardson 		return -1;
617999a2dd95SBruce Richardson 
618099a2dd95SBruce Richardson 	ret = rte_eth_stats_get(port_id, &stats);
618199a2dd95SBruce Richardson 	if (ret < 0)
618299a2dd95SBruce Richardson 		return -1;
618399a2dd95SBruce Richardson 
618499a2dd95SBruce Richardson 	rte_tel_data_start_dict(d);
618599a2dd95SBruce Richardson 	ADD_DICT_STAT(stats, ipackets);
618699a2dd95SBruce Richardson 	ADD_DICT_STAT(stats, opackets);
618799a2dd95SBruce Richardson 	ADD_DICT_STAT(stats, ibytes);
618899a2dd95SBruce Richardson 	ADD_DICT_STAT(stats, obytes);
618999a2dd95SBruce Richardson 	ADD_DICT_STAT(stats, imissed);
619099a2dd95SBruce Richardson 	ADD_DICT_STAT(stats, ierrors);
619199a2dd95SBruce Richardson 	ADD_DICT_STAT(stats, oerrors);
619299a2dd95SBruce Richardson 	ADD_DICT_STAT(stats, rx_nombuf);
619399a2dd95SBruce Richardson 	eth_dev_add_port_queue_stats(d, stats.q_ipackets, "q_ipackets");
619499a2dd95SBruce Richardson 	eth_dev_add_port_queue_stats(d, stats.q_opackets, "q_opackets");
619599a2dd95SBruce Richardson 	eth_dev_add_port_queue_stats(d, stats.q_ibytes, "q_ibytes");
619699a2dd95SBruce Richardson 	eth_dev_add_port_queue_stats(d, stats.q_obytes, "q_obytes");
619799a2dd95SBruce Richardson 	eth_dev_add_port_queue_stats(d, stats.q_errors, "q_errors");
619899a2dd95SBruce Richardson 
619999a2dd95SBruce Richardson 	return 0;
620099a2dd95SBruce Richardson }
620199a2dd95SBruce Richardson 
620299a2dd95SBruce Richardson static int
620399a2dd95SBruce Richardson eth_dev_handle_port_xstats(const char *cmd __rte_unused,
620499a2dd95SBruce Richardson 		const char *params,
620599a2dd95SBruce Richardson 		struct rte_tel_data *d)
620699a2dd95SBruce Richardson {
620799a2dd95SBruce Richardson 	struct rte_eth_xstat *eth_xstats;
620899a2dd95SBruce Richardson 	struct rte_eth_xstat_name *xstat_names;
620999a2dd95SBruce Richardson 	int port_id, num_xstats;
621099a2dd95SBruce Richardson 	int i, ret;
621199a2dd95SBruce Richardson 	char *end_param;
621299a2dd95SBruce Richardson 
621399a2dd95SBruce Richardson 	if (params == NULL || strlen(params) == 0 || !isdigit(*params))
621499a2dd95SBruce Richardson 		return -1;
621599a2dd95SBruce Richardson 
621699a2dd95SBruce Richardson 	port_id = strtoul(params, &end_param, 0);
621799a2dd95SBruce Richardson 	if (*end_param != '\0')
621899a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(NOTICE,
621999a2dd95SBruce Richardson 			"Extra parameters passed to ethdev telemetry command, ignoring");
622099a2dd95SBruce Richardson 	if (!rte_eth_dev_is_valid_port(port_id))
622199a2dd95SBruce Richardson 		return -1;
622299a2dd95SBruce Richardson 
622399a2dd95SBruce Richardson 	num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
622499a2dd95SBruce Richardson 	if (num_xstats < 0)
622599a2dd95SBruce Richardson 		return -1;
622699a2dd95SBruce Richardson 
622799a2dd95SBruce Richardson 	/* use one malloc for both names and stats */
622899a2dd95SBruce Richardson 	eth_xstats = malloc((sizeof(struct rte_eth_xstat) +
622999a2dd95SBruce Richardson 			sizeof(struct rte_eth_xstat_name)) * num_xstats);
623099a2dd95SBruce Richardson 	if (eth_xstats == NULL)
623199a2dd95SBruce Richardson 		return -1;
623299a2dd95SBruce Richardson 	xstat_names = (void *)&eth_xstats[num_xstats];
623399a2dd95SBruce Richardson 
623499a2dd95SBruce Richardson 	ret = rte_eth_xstats_get_names(port_id, xstat_names, num_xstats);
623599a2dd95SBruce Richardson 	if (ret < 0 || ret > num_xstats) {
623699a2dd95SBruce Richardson 		free(eth_xstats);
623799a2dd95SBruce Richardson 		return -1;
623899a2dd95SBruce Richardson 	}
623999a2dd95SBruce Richardson 
624099a2dd95SBruce Richardson 	ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats);
624199a2dd95SBruce Richardson 	if (ret < 0 || ret > num_xstats) {
624299a2dd95SBruce Richardson 		free(eth_xstats);
624399a2dd95SBruce Richardson 		return -1;
624499a2dd95SBruce Richardson 	}
624599a2dd95SBruce Richardson 
624699a2dd95SBruce Richardson 	rte_tel_data_start_dict(d);
624799a2dd95SBruce Richardson 	for (i = 0; i < num_xstats; i++)
624899a2dd95SBruce Richardson 		rte_tel_data_add_dict_u64(d, xstat_names[i].name,
624999a2dd95SBruce Richardson 				eth_xstats[i].value);
625099a2dd95SBruce Richardson 	return 0;
625199a2dd95SBruce Richardson }
625299a2dd95SBruce Richardson 
625399a2dd95SBruce Richardson static int
625499a2dd95SBruce Richardson eth_dev_handle_port_link_status(const char *cmd __rte_unused,
625599a2dd95SBruce Richardson 		const char *params,
625699a2dd95SBruce Richardson 		struct rte_tel_data *d)
625799a2dd95SBruce Richardson {
625899a2dd95SBruce Richardson 	static const char *status_str = "status";
625999a2dd95SBruce Richardson 	int ret, port_id;
626099a2dd95SBruce Richardson 	struct rte_eth_link link;
626199a2dd95SBruce Richardson 	char *end_param;
626299a2dd95SBruce Richardson 
626399a2dd95SBruce Richardson 	if (params == NULL || strlen(params) == 0 || !isdigit(*params))
626499a2dd95SBruce Richardson 		return -1;
626599a2dd95SBruce Richardson 
626699a2dd95SBruce Richardson 	port_id = strtoul(params, &end_param, 0);
626799a2dd95SBruce Richardson 	if (*end_param != '\0')
626899a2dd95SBruce Richardson 		RTE_ETHDEV_LOG(NOTICE,
626999a2dd95SBruce Richardson 			"Extra parameters passed to ethdev telemetry command, ignoring");
627099a2dd95SBruce Richardson 	if (!rte_eth_dev_is_valid_port(port_id))
627199a2dd95SBruce Richardson 		return -1;
627299a2dd95SBruce Richardson 
627399a2dd95SBruce Richardson 	ret = rte_eth_link_get_nowait(port_id, &link);
627499a2dd95SBruce Richardson 	if (ret < 0)
627599a2dd95SBruce Richardson 		return -1;
627699a2dd95SBruce Richardson 
627799a2dd95SBruce Richardson 	rte_tel_data_start_dict(d);
627899a2dd95SBruce Richardson 	if (!link.link_status) {
627999a2dd95SBruce Richardson 		rte_tel_data_add_dict_string(d, status_str, "DOWN");
628099a2dd95SBruce Richardson 		return 0;
628199a2dd95SBruce Richardson 	}
628299a2dd95SBruce Richardson 	rte_tel_data_add_dict_string(d, status_str, "UP");
628399a2dd95SBruce Richardson 	rte_tel_data_add_dict_u64(d, "speed", link.link_speed);
628499a2dd95SBruce Richardson 	rte_tel_data_add_dict_string(d, "duplex",
6285295968d1SFerruh Yigit 			(link.link_duplex == RTE_ETH_LINK_FULL_DUPLEX) ?
628699a2dd95SBruce Richardson 				"full-duplex" : "half-duplex");
628799a2dd95SBruce Richardson 	return 0;
628899a2dd95SBruce Richardson }
628999a2dd95SBruce Richardson 
629058b43c1dSGowrishankar Muthukrishnan static int
629158b43c1dSGowrishankar Muthukrishnan eth_dev_handle_port_info(const char *cmd __rte_unused,
629258b43c1dSGowrishankar Muthukrishnan 		const char *params,
629358b43c1dSGowrishankar Muthukrishnan 		struct rte_tel_data *d)
629458b43c1dSGowrishankar Muthukrishnan {
629558b43c1dSGowrishankar Muthukrishnan 	struct rte_tel_data *rxq_state, *txq_state;
629658b43c1dSGowrishankar Muthukrishnan 	char mac_addr[RTE_ETHER_ADDR_LEN];
629758b43c1dSGowrishankar Muthukrishnan 	struct rte_eth_dev *eth_dev;
629858b43c1dSGowrishankar Muthukrishnan 	char *end_param;
629958b43c1dSGowrishankar Muthukrishnan 	int port_id, i;
630058b43c1dSGowrishankar Muthukrishnan 
630158b43c1dSGowrishankar Muthukrishnan 	if (params == NULL || strlen(params) == 0 || !isdigit(*params))
630258b43c1dSGowrishankar Muthukrishnan 		return -1;
630358b43c1dSGowrishankar Muthukrishnan 
630458b43c1dSGowrishankar Muthukrishnan 	port_id = strtoul(params, &end_param, 0);
630558b43c1dSGowrishankar Muthukrishnan 	if (*end_param != '\0')
630658b43c1dSGowrishankar Muthukrishnan 		RTE_ETHDEV_LOG(NOTICE,
630758b43c1dSGowrishankar Muthukrishnan 			"Extra parameters passed to ethdev telemetry command, ignoring");
630858b43c1dSGowrishankar Muthukrishnan 
630958b43c1dSGowrishankar Muthukrishnan 	if (!rte_eth_dev_is_valid_port(port_id))
631058b43c1dSGowrishankar Muthukrishnan 		return -EINVAL;
631158b43c1dSGowrishankar Muthukrishnan 
631258b43c1dSGowrishankar Muthukrishnan 	eth_dev = &rte_eth_devices[port_id];
631358b43c1dSGowrishankar Muthukrishnan 	if (!eth_dev)
631458b43c1dSGowrishankar Muthukrishnan 		return -EINVAL;
631558b43c1dSGowrishankar Muthukrishnan 
631658b43c1dSGowrishankar Muthukrishnan 	rxq_state = rte_tel_data_alloc();
631758b43c1dSGowrishankar Muthukrishnan 	if (!rxq_state)
631858b43c1dSGowrishankar Muthukrishnan 		return -ENOMEM;
631958b43c1dSGowrishankar Muthukrishnan 
632058b43c1dSGowrishankar Muthukrishnan 	txq_state = rte_tel_data_alloc();
632158b43c1dSGowrishankar Muthukrishnan 	if (!txq_state)
632258b43c1dSGowrishankar Muthukrishnan 		return -ENOMEM;
632358b43c1dSGowrishankar Muthukrishnan 
632458b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_start_dict(d);
632558b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_string(d, "name", eth_dev->data->name);
632658b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "state", eth_dev->state);
632758b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "nb_rx_queues",
632858b43c1dSGowrishankar Muthukrishnan 			eth_dev->data->nb_rx_queues);
632958b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "nb_tx_queues",
633058b43c1dSGowrishankar Muthukrishnan 			eth_dev->data->nb_tx_queues);
633158b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "port_id", eth_dev->data->port_id);
633258b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "mtu", eth_dev->data->mtu);
633358b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "rx_mbuf_size_min",
633458b43c1dSGowrishankar Muthukrishnan 			eth_dev->data->min_rx_buf_size);
633558b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "rx_mbuf_alloc_fail",
633658b43c1dSGowrishankar Muthukrishnan 			eth_dev->data->rx_mbuf_alloc_failed);
633758b43c1dSGowrishankar Muthukrishnan 	snprintf(mac_addr, RTE_ETHER_ADDR_LEN, "%02x:%02x:%02x:%02x:%02x:%02x",
633858b43c1dSGowrishankar Muthukrishnan 			 eth_dev->data->mac_addrs->addr_bytes[0],
633958b43c1dSGowrishankar Muthukrishnan 			 eth_dev->data->mac_addrs->addr_bytes[1],
634058b43c1dSGowrishankar Muthukrishnan 			 eth_dev->data->mac_addrs->addr_bytes[2],
634158b43c1dSGowrishankar Muthukrishnan 			 eth_dev->data->mac_addrs->addr_bytes[3],
634258b43c1dSGowrishankar Muthukrishnan 			 eth_dev->data->mac_addrs->addr_bytes[4],
634358b43c1dSGowrishankar Muthukrishnan 			 eth_dev->data->mac_addrs->addr_bytes[5]);
634458b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_string(d, "mac_addr", mac_addr);
634558b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "promiscuous",
634658b43c1dSGowrishankar Muthukrishnan 			eth_dev->data->promiscuous);
634758b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "scattered_rx",
634858b43c1dSGowrishankar Muthukrishnan 			eth_dev->data->scattered_rx);
634958b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "all_multicast",
635058b43c1dSGowrishankar Muthukrishnan 			eth_dev->data->all_multicast);
635158b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "dev_started", eth_dev->data->dev_started);
635258b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "lro", eth_dev->data->lro);
635358b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "dev_configured",
635458b43c1dSGowrishankar Muthukrishnan 			eth_dev->data->dev_configured);
635558b43c1dSGowrishankar Muthukrishnan 
635658b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_start_array(rxq_state, RTE_TEL_INT_VAL);
635758b43c1dSGowrishankar Muthukrishnan 	for (i = 0; i < eth_dev->data->nb_rx_queues; i++)
635858b43c1dSGowrishankar Muthukrishnan 		rte_tel_data_add_array_int(rxq_state,
635958b43c1dSGowrishankar Muthukrishnan 				eth_dev->data->rx_queue_state[i]);
636058b43c1dSGowrishankar Muthukrishnan 
636158b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_start_array(txq_state, RTE_TEL_INT_VAL);
636258b43c1dSGowrishankar Muthukrishnan 	for (i = 0; i < eth_dev->data->nb_tx_queues; i++)
636358b43c1dSGowrishankar Muthukrishnan 		rte_tel_data_add_array_int(txq_state,
636458b43c1dSGowrishankar Muthukrishnan 				eth_dev->data->tx_queue_state[i]);
636558b43c1dSGowrishankar Muthukrishnan 
636658b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_container(d, "rxq_state", rxq_state, 0);
636758b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_container(d, "txq_state", txq_state, 0);
636858b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "numa_node", eth_dev->data->numa_node);
636958b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "dev_flags", eth_dev->data->dev_flags);
637058b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "rx_offloads",
637158b43c1dSGowrishankar Muthukrishnan 			eth_dev->data->dev_conf.rxmode.offloads);
637258b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "tx_offloads",
637358b43c1dSGowrishankar Muthukrishnan 			eth_dev->data->dev_conf.txmode.offloads);
637458b43c1dSGowrishankar Muthukrishnan 	rte_tel_data_add_dict_int(d, "ethdev_rss_hf",
637558b43c1dSGowrishankar Muthukrishnan 			eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf);
637658b43c1dSGowrishankar Muthukrishnan 
637758b43c1dSGowrishankar Muthukrishnan 	return 0;
637858b43c1dSGowrishankar Muthukrishnan }
637958b43c1dSGowrishankar Muthukrishnan 
638099a2dd95SBruce Richardson int
638199a2dd95SBruce Richardson rte_eth_hairpin_queue_peer_update(uint16_t peer_port, uint16_t peer_queue,
638299a2dd95SBruce Richardson 				  struct rte_hairpin_peer_info *cur_info,
638399a2dd95SBruce Richardson 				  struct rte_hairpin_peer_info *peer_info,
638499a2dd95SBruce Richardson 				  uint32_t direction)
638599a2dd95SBruce Richardson {
638699a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
638799a2dd95SBruce Richardson 
638899a2dd95SBruce Richardson 	/* Current queue information is not mandatory. */
638999a2dd95SBruce Richardson 	if (peer_info == NULL)
639099a2dd95SBruce Richardson 		return -EINVAL;
639199a2dd95SBruce Richardson 
639299a2dd95SBruce Richardson 	/* No need to check the validity again. */
639399a2dd95SBruce Richardson 	dev = &rte_eth_devices[peer_port];
639499a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->hairpin_queue_peer_update,
639599a2dd95SBruce Richardson 				-ENOTSUP);
639699a2dd95SBruce Richardson 
639799a2dd95SBruce Richardson 	return (*dev->dev_ops->hairpin_queue_peer_update)(dev, peer_queue,
639899a2dd95SBruce Richardson 					cur_info, peer_info, direction);
639999a2dd95SBruce Richardson }
640099a2dd95SBruce Richardson 
640199a2dd95SBruce Richardson int
640299a2dd95SBruce Richardson rte_eth_hairpin_queue_peer_bind(uint16_t cur_port, uint16_t cur_queue,
640399a2dd95SBruce Richardson 				struct rte_hairpin_peer_info *peer_info,
640499a2dd95SBruce Richardson 				uint32_t direction)
640599a2dd95SBruce Richardson {
640699a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
640799a2dd95SBruce Richardson 
640899a2dd95SBruce Richardson 	if (peer_info == NULL)
640999a2dd95SBruce Richardson 		return -EINVAL;
641099a2dd95SBruce Richardson 
641199a2dd95SBruce Richardson 	/* No need to check the validity again. */
641299a2dd95SBruce Richardson 	dev = &rte_eth_devices[cur_port];
641399a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->hairpin_queue_peer_bind,
641499a2dd95SBruce Richardson 				-ENOTSUP);
641599a2dd95SBruce Richardson 
641699a2dd95SBruce Richardson 	return (*dev->dev_ops->hairpin_queue_peer_bind)(dev, cur_queue,
641799a2dd95SBruce Richardson 							peer_info, direction);
641899a2dd95SBruce Richardson }
641999a2dd95SBruce Richardson 
642099a2dd95SBruce Richardson int
642199a2dd95SBruce Richardson rte_eth_hairpin_queue_peer_unbind(uint16_t cur_port, uint16_t cur_queue,
642299a2dd95SBruce Richardson 				  uint32_t direction)
642399a2dd95SBruce Richardson {
642499a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
642599a2dd95SBruce Richardson 
642699a2dd95SBruce Richardson 	/* No need to check the validity again. */
642799a2dd95SBruce Richardson 	dev = &rte_eth_devices[cur_port];
642899a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->hairpin_queue_peer_unbind,
642999a2dd95SBruce Richardson 				-ENOTSUP);
643099a2dd95SBruce Richardson 
643199a2dd95SBruce Richardson 	return (*dev->dev_ops->hairpin_queue_peer_unbind)(dev, cur_queue,
643299a2dd95SBruce Richardson 							  direction);
643399a2dd95SBruce Richardson }
643499a2dd95SBruce Richardson 
643599a2dd95SBruce Richardson int
643699a2dd95SBruce Richardson rte_eth_representor_info_get(uint16_t port_id,
643799a2dd95SBruce Richardson 			     struct rte_eth_representor_info *info)
643899a2dd95SBruce Richardson {
643999a2dd95SBruce Richardson 	struct rte_eth_dev *dev;
644099a2dd95SBruce Richardson 
644199a2dd95SBruce Richardson 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
644299a2dd95SBruce Richardson 	dev = &rte_eth_devices[port_id];
644399a2dd95SBruce Richardson 
644499a2dd95SBruce Richardson 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->representor_info_get, -ENOTSUP);
644553ef1b34SMin Hu (Connor) 	return eth_err(port_id, (*dev->dev_ops->representor_info_get)(dev, info));
644699a2dd95SBruce Richardson }
644799a2dd95SBruce Richardson 
6448f6d8a6d3SIvan Malov int
6449f6d8a6d3SIvan Malov rte_eth_rx_metadata_negotiate(uint16_t port_id, uint64_t *features)
6450f6d8a6d3SIvan Malov {
6451f6d8a6d3SIvan Malov 	struct rte_eth_dev *dev;
6452f6d8a6d3SIvan Malov 
6453f6d8a6d3SIvan Malov 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
6454f6d8a6d3SIvan Malov 	dev = &rte_eth_devices[port_id];
6455f6d8a6d3SIvan Malov 
6456f6d8a6d3SIvan Malov 	if (dev->data->dev_configured != 0) {
6457f6d8a6d3SIvan Malov 		RTE_ETHDEV_LOG(ERR,
64585906be5aSAndrew Rybchenko 			"The port (ID=%"PRIu16") is already configured\n",
6459f6d8a6d3SIvan Malov 			port_id);
6460f6d8a6d3SIvan Malov 		return -EBUSY;
6461f6d8a6d3SIvan Malov 	}
6462f6d8a6d3SIvan Malov 
6463f6d8a6d3SIvan Malov 	if (features == NULL) {
6464f6d8a6d3SIvan Malov 		RTE_ETHDEV_LOG(ERR, "Invalid features (NULL)\n");
6465f6d8a6d3SIvan Malov 		return -EINVAL;
6466f6d8a6d3SIvan Malov 	}
6467f6d8a6d3SIvan Malov 
6468f6d8a6d3SIvan Malov 	RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_metadata_negotiate, -ENOTSUP);
6469f6d8a6d3SIvan Malov 	return eth_err(port_id,
6470f6d8a6d3SIvan Malov 		       (*dev->dev_ops->rx_metadata_negotiate)(dev, features));
6471f6d8a6d3SIvan Malov }
6472f6d8a6d3SIvan Malov 
6473eeded204SDavid Marchand RTE_LOG_REGISTER_DEFAULT(rte_eth_dev_logtype, INFO);
647499a2dd95SBruce Richardson 
647599a2dd95SBruce Richardson RTE_INIT(ethdev_init_telemetry)
647699a2dd95SBruce Richardson {
647799a2dd95SBruce Richardson 	rte_telemetry_register_cmd("/ethdev/list", eth_dev_handle_port_list,
647899a2dd95SBruce Richardson 			"Returns list of available ethdev ports. Takes no parameters");
647999a2dd95SBruce Richardson 	rte_telemetry_register_cmd("/ethdev/stats", eth_dev_handle_port_stats,
648099a2dd95SBruce Richardson 			"Returns the common stats for a port. Parameters: int port_id");
648199a2dd95SBruce Richardson 	rte_telemetry_register_cmd("/ethdev/xstats", eth_dev_handle_port_xstats,
648299a2dd95SBruce Richardson 			"Returns the extended stats for a port. Parameters: int port_id");
648399a2dd95SBruce Richardson 	rte_telemetry_register_cmd("/ethdev/link_status",
648499a2dd95SBruce Richardson 			eth_dev_handle_port_link_status,
648599a2dd95SBruce Richardson 			"Returns the link status for a port. Parameters: int port_id");
648658b43c1dSGowrishankar Muthukrishnan 	rte_telemetry_register_cmd("/ethdev/info", eth_dev_handle_port_info,
648758b43c1dSGowrishankar Muthukrishnan 			"Returns the device info for a port. Parameters: int port_id");
648899a2dd95SBruce Richardson }
6489