ethdev: add function to get hairpin peer ports list
[dpdk.git] / lib / librte_ethdev / rte_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2017 Intel Corporation
3  */
4
5 #include <sys/types.h>
6 #include <sys/queue.h>
7 #include <ctype.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdarg.h>
12 #include <errno.h>
13 #include <stdbool.h>
14 #include <stdint.h>
15 #include <inttypes.h>
16 #include <netinet/in.h>
17
18 #include <rte_byteorder.h>
19 #include <rte_log.h>
20 #include <rte_debug.h>
21 #include <rte_interrupts.h>
22 #include <rte_memory.h>
23 #include <rte_memcpy.h>
24 #include <rte_memzone.h>
25 #include <rte_launch.h>
26 #include <rte_eal.h>
27 #include <rte_per_lcore.h>
28 #include <rte_lcore.h>
29 #include <rte_branch_prediction.h>
30 #include <rte_common.h>
31 #include <rte_mempool.h>
32 #include <rte_malloc.h>
33 #include <rte_mbuf.h>
34 #include <rte_errno.h>
35 #include <rte_spinlock.h>
36 #include <rte_string_fns.h>
37 #include <rte_kvargs.h>
38 #include <rte_class.h>
39 #include <rte_ether.h>
40 #include <rte_telemetry.h>
41
42 #include "rte_ethdev_trace.h"
43 #include "rte_ethdev.h"
44 #include "rte_ethdev_driver.h"
45 #include "ethdev_profile.h"
46 #include "ethdev_private.h"
47
48 static const char *MZ_RTE_ETH_DEV_DATA = "rte_eth_dev_data";
49 struct rte_eth_dev rte_eth_devices[RTE_MAX_ETHPORTS];
50
51 /* spinlock for eth device callbacks */
52 static rte_spinlock_t rte_eth_dev_cb_lock = RTE_SPINLOCK_INITIALIZER;
53
54 /* spinlock for add/remove rx callbacks */
55 static rte_spinlock_t rte_eth_rx_cb_lock = RTE_SPINLOCK_INITIALIZER;
56
57 /* spinlock for add/remove tx callbacks */
58 static rte_spinlock_t rte_eth_tx_cb_lock = RTE_SPINLOCK_INITIALIZER;
59
60 /* spinlock for shared data allocation */
61 static rte_spinlock_t rte_eth_shared_data_lock = RTE_SPINLOCK_INITIALIZER;
62
63 /* store statistics names and its offset in stats structure  */
64 struct rte_eth_xstats_name_off {
65         char name[RTE_ETH_XSTATS_NAME_SIZE];
66         unsigned offset;
67 };
68
69 /* Shared memory between primary and secondary processes. */
70 static struct {
71         uint64_t next_owner_id;
72         rte_spinlock_t ownership_lock;
73         struct rte_eth_dev_data data[RTE_MAX_ETHPORTS];
74 } *rte_eth_dev_shared_data;
75
76 static const struct rte_eth_xstats_name_off rte_stats_strings[] = {
77         {"rx_good_packets", offsetof(struct rte_eth_stats, ipackets)},
78         {"tx_good_packets", offsetof(struct rte_eth_stats, opackets)},
79         {"rx_good_bytes", offsetof(struct rte_eth_stats, ibytes)},
80         {"tx_good_bytes", offsetof(struct rte_eth_stats, obytes)},
81         {"rx_missed_errors", offsetof(struct rte_eth_stats, imissed)},
82         {"rx_errors", offsetof(struct rte_eth_stats, ierrors)},
83         {"tx_errors", offsetof(struct rte_eth_stats, oerrors)},
84         {"rx_mbuf_allocation_errors", offsetof(struct rte_eth_stats,
85                 rx_nombuf)},
86 };
87
88 #define RTE_NB_STATS RTE_DIM(rte_stats_strings)
89
90 static const struct rte_eth_xstats_name_off rte_rxq_stats_strings[] = {
91         {"packets", offsetof(struct rte_eth_stats, q_ipackets)},
92         {"bytes", offsetof(struct rte_eth_stats, q_ibytes)},
93         {"errors", offsetof(struct rte_eth_stats, q_errors)},
94 };
95
96 #define RTE_NB_RXQ_STATS RTE_DIM(rte_rxq_stats_strings)
97
98 static const struct rte_eth_xstats_name_off rte_txq_stats_strings[] = {
99         {"packets", offsetof(struct rte_eth_stats, q_opackets)},
100         {"bytes", offsetof(struct rte_eth_stats, q_obytes)},
101 };
102 #define RTE_NB_TXQ_STATS RTE_DIM(rte_txq_stats_strings)
103
104 #define RTE_RX_OFFLOAD_BIT2STR(_name)   \
105         { DEV_RX_OFFLOAD_##_name, #_name }
106
107 static const struct {
108         uint64_t offload;
109         const char *name;
110 } rte_rx_offload_names[] = {
111         RTE_RX_OFFLOAD_BIT2STR(VLAN_STRIP),
112         RTE_RX_OFFLOAD_BIT2STR(IPV4_CKSUM),
113         RTE_RX_OFFLOAD_BIT2STR(UDP_CKSUM),
114         RTE_RX_OFFLOAD_BIT2STR(TCP_CKSUM),
115         RTE_RX_OFFLOAD_BIT2STR(TCP_LRO),
116         RTE_RX_OFFLOAD_BIT2STR(QINQ_STRIP),
117         RTE_RX_OFFLOAD_BIT2STR(OUTER_IPV4_CKSUM),
118         RTE_RX_OFFLOAD_BIT2STR(MACSEC_STRIP),
119         RTE_RX_OFFLOAD_BIT2STR(HEADER_SPLIT),
120         RTE_RX_OFFLOAD_BIT2STR(VLAN_FILTER),
121         RTE_RX_OFFLOAD_BIT2STR(VLAN_EXTEND),
122         RTE_RX_OFFLOAD_BIT2STR(JUMBO_FRAME),
123         RTE_RX_OFFLOAD_BIT2STR(SCATTER),
124         RTE_RX_OFFLOAD_BIT2STR(TIMESTAMP),
125         RTE_RX_OFFLOAD_BIT2STR(SECURITY),
126         RTE_RX_OFFLOAD_BIT2STR(KEEP_CRC),
127         RTE_RX_OFFLOAD_BIT2STR(SCTP_CKSUM),
128         RTE_RX_OFFLOAD_BIT2STR(OUTER_UDP_CKSUM),
129         RTE_RX_OFFLOAD_BIT2STR(RSS_HASH),
130 };
131
132 #undef RTE_RX_OFFLOAD_BIT2STR
133
134 #define RTE_TX_OFFLOAD_BIT2STR(_name)   \
135         { DEV_TX_OFFLOAD_##_name, #_name }
136
137 static const struct {
138         uint64_t offload;
139         const char *name;
140 } rte_tx_offload_names[] = {
141         RTE_TX_OFFLOAD_BIT2STR(VLAN_INSERT),
142         RTE_TX_OFFLOAD_BIT2STR(IPV4_CKSUM),
143         RTE_TX_OFFLOAD_BIT2STR(UDP_CKSUM),
144         RTE_TX_OFFLOAD_BIT2STR(TCP_CKSUM),
145         RTE_TX_OFFLOAD_BIT2STR(SCTP_CKSUM),
146         RTE_TX_OFFLOAD_BIT2STR(TCP_TSO),
147         RTE_TX_OFFLOAD_BIT2STR(UDP_TSO),
148         RTE_TX_OFFLOAD_BIT2STR(OUTER_IPV4_CKSUM),
149         RTE_TX_OFFLOAD_BIT2STR(QINQ_INSERT),
150         RTE_TX_OFFLOAD_BIT2STR(VXLAN_TNL_TSO),
151         RTE_TX_OFFLOAD_BIT2STR(GRE_TNL_TSO),
152         RTE_TX_OFFLOAD_BIT2STR(IPIP_TNL_TSO),
153         RTE_TX_OFFLOAD_BIT2STR(GENEVE_TNL_TSO),
154         RTE_TX_OFFLOAD_BIT2STR(MACSEC_INSERT),
155         RTE_TX_OFFLOAD_BIT2STR(MT_LOCKFREE),
156         RTE_TX_OFFLOAD_BIT2STR(MULTI_SEGS),
157         RTE_TX_OFFLOAD_BIT2STR(MBUF_FAST_FREE),
158         RTE_TX_OFFLOAD_BIT2STR(SECURITY),
159         RTE_TX_OFFLOAD_BIT2STR(UDP_TNL_TSO),
160         RTE_TX_OFFLOAD_BIT2STR(IP_TNL_TSO),
161         RTE_TX_OFFLOAD_BIT2STR(OUTER_UDP_CKSUM),
162         RTE_TX_OFFLOAD_BIT2STR(SEND_ON_TIMESTAMP),
163 };
164
165 #undef RTE_TX_OFFLOAD_BIT2STR
166
167 /**
168  * The user application callback description.
169  *
170  * It contains callback address to be registered by user application,
171  * the pointer to the parameters for callback, and the event type.
172  */
173 struct rte_eth_dev_callback {
174         TAILQ_ENTRY(rte_eth_dev_callback) next; /**< Callbacks list */
175         rte_eth_dev_cb_fn cb_fn;                /**< Callback address */
176         void *cb_arg;                           /**< Parameter for callback */
177         void *ret_param;                        /**< Return parameter */
178         enum rte_eth_event_type event;          /**< Interrupt event type */
179         uint32_t active;                        /**< Callback is executing */
180 };
181
182 enum {
183         STAT_QMAP_TX = 0,
184         STAT_QMAP_RX
185 };
186
187 int
188 rte_eth_iterator_init(struct rte_dev_iterator *iter, const char *devargs_str)
189 {
190         int ret;
191         struct rte_devargs devargs = {.args = NULL};
192         const char *bus_param_key;
193         char *bus_str = NULL;
194         char *cls_str = NULL;
195         int str_size;
196
197         memset(iter, 0, sizeof(*iter));
198
199         /*
200          * The devargs string may use various syntaxes:
201          *   - 0000:08:00.0,representor=[1-3]
202          *   - pci:0000:06:00.0,representor=[0,5]
203          *   - class=eth,mac=00:11:22:33:44:55
204          * A new syntax is in development (not yet supported):
205          *   - bus=X,paramX=x/class=Y,paramY=y/driver=Z,paramZ=z
206          */
207
208         /*
209          * Handle pure class filter (i.e. without any bus-level argument),
210          * from future new syntax.
211          * rte_devargs_parse() is not yet supporting the new syntax,
212          * that's why this simple case is temporarily parsed here.
213          */
214 #define iter_anybus_str "class=eth,"
215         if (strncmp(devargs_str, iter_anybus_str,
216                         strlen(iter_anybus_str)) == 0) {
217                 iter->cls_str = devargs_str + strlen(iter_anybus_str);
218                 goto end;
219         }
220
221         /* Split bus, device and parameters. */
222         ret = rte_devargs_parse(&devargs, devargs_str);
223         if (ret != 0)
224                 goto error;
225
226         /*
227          * Assume parameters of old syntax can match only at ethdev level.
228          * Extra parameters will be ignored, thanks to "+" prefix.
229          */
230         str_size = strlen(devargs.args) + 2;
231         cls_str = malloc(str_size);
232         if (cls_str == NULL) {
233                 ret = -ENOMEM;
234                 goto error;
235         }
236         ret = snprintf(cls_str, str_size, "+%s", devargs.args);
237         if (ret != str_size - 1) {
238                 ret = -EINVAL;
239                 goto error;
240         }
241         iter->cls_str = cls_str;
242         free(devargs.args); /* allocated by rte_devargs_parse() */
243         devargs.args = NULL;
244
245         iter->bus = devargs.bus;
246         if (iter->bus->dev_iterate == NULL) {
247                 ret = -ENOTSUP;
248                 goto error;
249         }
250
251         /* Convert bus args to new syntax for use with new API dev_iterate. */
252         if (strcmp(iter->bus->name, "vdev") == 0) {
253                 bus_param_key = "name";
254         } else if (strcmp(iter->bus->name, "pci") == 0) {
255                 bus_param_key = "addr";
256         } else {
257                 ret = -ENOTSUP;
258                 goto error;
259         }
260         str_size = strlen(bus_param_key) + strlen(devargs.name) + 2;
261         bus_str = malloc(str_size);
262         if (bus_str == NULL) {
263                 ret = -ENOMEM;
264                 goto error;
265         }
266         ret = snprintf(bus_str, str_size, "%s=%s",
267                         bus_param_key, devargs.name);
268         if (ret != str_size - 1) {
269                 ret = -EINVAL;
270                 goto error;
271         }
272         iter->bus_str = bus_str;
273
274 end:
275         iter->cls = rte_class_find_by_name("eth");
276         return 0;
277
278 error:
279         if (ret == -ENOTSUP)
280                 RTE_ETHDEV_LOG(ERR, "Bus %s does not support iterating.\n",
281                                 iter->bus->name);
282         free(devargs.args);
283         free(bus_str);
284         free(cls_str);
285         return ret;
286 }
287
288 uint16_t
289 rte_eth_iterator_next(struct rte_dev_iterator *iter)
290 {
291         if (iter->cls == NULL) /* invalid ethdev iterator */
292                 return RTE_MAX_ETHPORTS;
293
294         do { /* loop to try all matching rte_device */
295                 /* If not pure ethdev filter and */
296                 if (iter->bus != NULL &&
297                                 /* not in middle of rte_eth_dev iteration, */
298                                 iter->class_device == NULL) {
299                         /* get next rte_device to try. */
300                         iter->device = iter->bus->dev_iterate(
301                                         iter->device, iter->bus_str, iter);
302                         if (iter->device == NULL)
303                                 break; /* no more rte_device candidate */
304                 }
305                 /* A device is matching bus part, need to check ethdev part. */
306                 iter->class_device = iter->cls->dev_iterate(
307                                 iter->class_device, iter->cls_str, iter);
308                 if (iter->class_device != NULL)
309                         return eth_dev_to_id(iter->class_device); /* match */
310         } while (iter->bus != NULL); /* need to try next rte_device */
311
312         /* No more ethdev port to iterate. */
313         rte_eth_iterator_cleanup(iter);
314         return RTE_MAX_ETHPORTS;
315 }
316
317 void
318 rte_eth_iterator_cleanup(struct rte_dev_iterator *iter)
319 {
320         if (iter->bus_str == NULL)
321                 return; /* nothing to free in pure class filter */
322         free(RTE_CAST_FIELD(iter, bus_str, char *)); /* workaround const */
323         free(RTE_CAST_FIELD(iter, cls_str, char *)); /* workaround const */
324         memset(iter, 0, sizeof(*iter));
325 }
326
327 uint16_t
328 rte_eth_find_next(uint16_t port_id)
329 {
330         while (port_id < RTE_MAX_ETHPORTS &&
331                         rte_eth_devices[port_id].state == RTE_ETH_DEV_UNUSED)
332                 port_id++;
333
334         if (port_id >= RTE_MAX_ETHPORTS)
335                 return RTE_MAX_ETHPORTS;
336
337         return port_id;
338 }
339
340 /*
341  * Macro to iterate over all valid ports for internal usage.
342  * Note: RTE_ETH_FOREACH_DEV is different because filtering owned ports.
343  */
344 #define RTE_ETH_FOREACH_VALID_DEV(port_id) \
345         for (port_id = rte_eth_find_next(0); \
346              port_id < RTE_MAX_ETHPORTS; \
347              port_id = rte_eth_find_next(port_id + 1))
348
349 uint16_t
350 rte_eth_find_next_of(uint16_t port_id, const struct rte_device *parent)
351 {
352         port_id = rte_eth_find_next(port_id);
353         while (port_id < RTE_MAX_ETHPORTS &&
354                         rte_eth_devices[port_id].device != parent)
355                 port_id = rte_eth_find_next(port_id + 1);
356
357         return port_id;
358 }
359
360 uint16_t
361 rte_eth_find_next_sibling(uint16_t port_id, uint16_t ref_port_id)
362 {
363         RTE_ETH_VALID_PORTID_OR_ERR_RET(ref_port_id, RTE_MAX_ETHPORTS);
364         return rte_eth_find_next_of(port_id,
365                         rte_eth_devices[ref_port_id].device);
366 }
367
368 static void
369 rte_eth_dev_shared_data_prepare(void)
370 {
371         const unsigned flags = 0;
372         const struct rte_memzone *mz;
373
374         rte_spinlock_lock(&rte_eth_shared_data_lock);
375
376         if (rte_eth_dev_shared_data == NULL) {
377                 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
378                         /* Allocate port data and ownership shared memory. */
379                         mz = rte_memzone_reserve(MZ_RTE_ETH_DEV_DATA,
380                                         sizeof(*rte_eth_dev_shared_data),
381                                         rte_socket_id(), flags);
382                 } else
383                         mz = rte_memzone_lookup(MZ_RTE_ETH_DEV_DATA);
384                 if (mz == NULL)
385                         rte_panic("Cannot allocate ethdev shared data\n");
386
387                 rte_eth_dev_shared_data = mz->addr;
388                 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
389                         rte_eth_dev_shared_data->next_owner_id =
390                                         RTE_ETH_DEV_NO_OWNER + 1;
391                         rte_spinlock_init(&rte_eth_dev_shared_data->ownership_lock);
392                         memset(rte_eth_dev_shared_data->data, 0,
393                                sizeof(rte_eth_dev_shared_data->data));
394                 }
395         }
396
397         rte_spinlock_unlock(&rte_eth_shared_data_lock);
398 }
399
400 static bool
401 is_allocated(const struct rte_eth_dev *ethdev)
402 {
403         return ethdev->data->name[0] != '\0';
404 }
405
406 static struct rte_eth_dev *
407 _rte_eth_dev_allocated(const char *name)
408 {
409         unsigned i;
410
411         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
412                 if (rte_eth_devices[i].data != NULL &&
413                     strcmp(rte_eth_devices[i].data->name, name) == 0)
414                         return &rte_eth_devices[i];
415         }
416         return NULL;
417 }
418
419 struct rte_eth_dev *
420 rte_eth_dev_allocated(const char *name)
421 {
422         struct rte_eth_dev *ethdev;
423
424         rte_eth_dev_shared_data_prepare();
425
426         rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock);
427
428         ethdev = _rte_eth_dev_allocated(name);
429
430         rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock);
431
432         return ethdev;
433 }
434
435 static uint16_t
436 rte_eth_dev_find_free_port(void)
437 {
438         unsigned i;
439
440         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
441                 /* Using shared name field to find a free port. */
442                 if (rte_eth_dev_shared_data->data[i].name[0] == '\0') {
443                         RTE_ASSERT(rte_eth_devices[i].state ==
444                                    RTE_ETH_DEV_UNUSED);
445                         return i;
446                 }
447         }
448         return RTE_MAX_ETHPORTS;
449 }
450
451 static struct rte_eth_dev *
452 eth_dev_get(uint16_t port_id)
453 {
454         struct rte_eth_dev *eth_dev = &rte_eth_devices[port_id];
455
456         eth_dev->data = &rte_eth_dev_shared_data->data[port_id];
457
458         return eth_dev;
459 }
460
461 struct rte_eth_dev *
462 rte_eth_dev_allocate(const char *name)
463 {
464         uint16_t port_id;
465         struct rte_eth_dev *eth_dev = NULL;
466         size_t name_len;
467
468         name_len = strnlen(name, RTE_ETH_NAME_MAX_LEN);
469         if (name_len == 0) {
470                 RTE_ETHDEV_LOG(ERR, "Zero length Ethernet device name\n");
471                 return NULL;
472         }
473
474         if (name_len >= RTE_ETH_NAME_MAX_LEN) {
475                 RTE_ETHDEV_LOG(ERR, "Ethernet device name is too long\n");
476                 return NULL;
477         }
478
479         rte_eth_dev_shared_data_prepare();
480
481         /* Synchronize port creation between primary and secondary threads. */
482         rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock);
483
484         if (_rte_eth_dev_allocated(name) != NULL) {
485                 RTE_ETHDEV_LOG(ERR,
486                         "Ethernet device with name %s already allocated\n",
487                         name);
488                 goto unlock;
489         }
490
491         port_id = rte_eth_dev_find_free_port();
492         if (port_id == RTE_MAX_ETHPORTS) {
493                 RTE_ETHDEV_LOG(ERR,
494                         "Reached maximum number of Ethernet ports\n");
495                 goto unlock;
496         }
497
498         eth_dev = eth_dev_get(port_id);
499         strlcpy(eth_dev->data->name, name, sizeof(eth_dev->data->name));
500         eth_dev->data->port_id = port_id;
501         eth_dev->data->mtu = RTE_ETHER_MTU;
502         pthread_mutex_init(&eth_dev->data->flow_ops_mutex, NULL);
503
504 unlock:
505         rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock);
506
507         return eth_dev;
508 }
509
510 /*
511  * Attach to a port already registered by the primary process, which
512  * makes sure that the same device would have the same port id both
513  * in the primary and secondary process.
514  */
515 struct rte_eth_dev *
516 rte_eth_dev_attach_secondary(const char *name)
517 {
518         uint16_t i;
519         struct rte_eth_dev *eth_dev = NULL;
520
521         rte_eth_dev_shared_data_prepare();
522
523         /* Synchronize port attachment to primary port creation and release. */
524         rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock);
525
526         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
527                 if (strcmp(rte_eth_dev_shared_data->data[i].name, name) == 0)
528                         break;
529         }
530         if (i == RTE_MAX_ETHPORTS) {
531                 RTE_ETHDEV_LOG(ERR,
532                         "Device %s is not driven by the primary process\n",
533                         name);
534         } else {
535                 eth_dev = eth_dev_get(i);
536                 RTE_ASSERT(eth_dev->data->port_id == i);
537         }
538
539         rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock);
540         return eth_dev;
541 }
542
543 int
544 rte_eth_dev_release_port(struct rte_eth_dev *eth_dev)
545 {
546         if (eth_dev == NULL)
547                 return -EINVAL;
548
549         rte_eth_dev_shared_data_prepare();
550
551         if (eth_dev->state != RTE_ETH_DEV_UNUSED)
552                 rte_eth_dev_callback_process(eth_dev,
553                                 RTE_ETH_EVENT_DESTROY, NULL);
554
555         rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock);
556
557         eth_dev->state = RTE_ETH_DEV_UNUSED;
558         eth_dev->device = NULL;
559         eth_dev->intr_handle = NULL;
560
561         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
562                 rte_free(eth_dev->data->rx_queues);
563                 rte_free(eth_dev->data->tx_queues);
564                 rte_free(eth_dev->data->mac_addrs);
565                 rte_free(eth_dev->data->hash_mac_addrs);
566                 rte_free(eth_dev->data->dev_private);
567                 pthread_mutex_destroy(&eth_dev->data->flow_ops_mutex);
568                 memset(eth_dev->data, 0, sizeof(struct rte_eth_dev_data));
569         }
570
571         rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock);
572
573         return 0;
574 }
575
576 int
577 rte_eth_dev_is_valid_port(uint16_t port_id)
578 {
579         if (port_id >= RTE_MAX_ETHPORTS ||
580             (rte_eth_devices[port_id].state == RTE_ETH_DEV_UNUSED))
581                 return 0;
582         else
583                 return 1;
584 }
585
586 static int
587 rte_eth_is_valid_owner_id(uint64_t owner_id)
588 {
589         if (owner_id == RTE_ETH_DEV_NO_OWNER ||
590             rte_eth_dev_shared_data->next_owner_id <= owner_id)
591                 return 0;
592         return 1;
593 }
594
595 uint64_t
596 rte_eth_find_next_owned_by(uint16_t port_id, const uint64_t owner_id)
597 {
598         port_id = rte_eth_find_next(port_id);
599         while (port_id < RTE_MAX_ETHPORTS &&
600                         rte_eth_devices[port_id].data->owner.id != owner_id)
601                 port_id = rte_eth_find_next(port_id + 1);
602
603         return port_id;
604 }
605
606 int
607 rte_eth_dev_owner_new(uint64_t *owner_id)
608 {
609         rte_eth_dev_shared_data_prepare();
610
611         rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock);
612
613         *owner_id = rte_eth_dev_shared_data->next_owner_id++;
614
615         rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock);
616         return 0;
617 }
618
619 static int
620 _rte_eth_dev_owner_set(const uint16_t port_id, const uint64_t old_owner_id,
621                        const struct rte_eth_dev_owner *new_owner)
622 {
623         struct rte_eth_dev *ethdev = &rte_eth_devices[port_id];
624         struct rte_eth_dev_owner *port_owner;
625
626         if (port_id >= RTE_MAX_ETHPORTS || !is_allocated(ethdev)) {
627                 RTE_ETHDEV_LOG(ERR, "Port id %"PRIu16" is not allocated\n",
628                         port_id);
629                 return -ENODEV;
630         }
631
632         if (!rte_eth_is_valid_owner_id(new_owner->id) &&
633             !rte_eth_is_valid_owner_id(old_owner_id)) {
634                 RTE_ETHDEV_LOG(ERR,
635                         "Invalid owner old_id=%016"PRIx64" new_id=%016"PRIx64"\n",
636                        old_owner_id, new_owner->id);
637                 return -EINVAL;
638         }
639
640         port_owner = &rte_eth_devices[port_id].data->owner;
641         if (port_owner->id != old_owner_id) {
642                 RTE_ETHDEV_LOG(ERR,
643                         "Cannot set owner to port %u already owned by %s_%016"PRIX64"\n",
644                         port_id, port_owner->name, port_owner->id);
645                 return -EPERM;
646         }
647
648         /* can not truncate (same structure) */
649         strlcpy(port_owner->name, new_owner->name, RTE_ETH_MAX_OWNER_NAME_LEN);
650
651         port_owner->id = new_owner->id;
652
653         RTE_ETHDEV_LOG(DEBUG, "Port %u owner is %s_%016"PRIx64"\n",
654                 port_id, new_owner->name, new_owner->id);
655
656         return 0;
657 }
658
659 int
660 rte_eth_dev_owner_set(const uint16_t port_id,
661                       const struct rte_eth_dev_owner *owner)
662 {
663         int ret;
664
665         rte_eth_dev_shared_data_prepare();
666
667         rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock);
668
669         ret = _rte_eth_dev_owner_set(port_id, RTE_ETH_DEV_NO_OWNER, owner);
670
671         rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock);
672         return ret;
673 }
674
675 int
676 rte_eth_dev_owner_unset(const uint16_t port_id, const uint64_t owner_id)
677 {
678         const struct rte_eth_dev_owner new_owner = (struct rte_eth_dev_owner)
679                         {.id = RTE_ETH_DEV_NO_OWNER, .name = ""};
680         int ret;
681
682         rte_eth_dev_shared_data_prepare();
683
684         rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock);
685
686         ret = _rte_eth_dev_owner_set(port_id, owner_id, &new_owner);
687
688         rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock);
689         return ret;
690 }
691
692 int
693 rte_eth_dev_owner_delete(const uint64_t owner_id)
694 {
695         uint16_t port_id;
696         int ret = 0;
697
698         rte_eth_dev_shared_data_prepare();
699
700         rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock);
701
702         if (rte_eth_is_valid_owner_id(owner_id)) {
703                 for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++)
704                         if (rte_eth_devices[port_id].data->owner.id == owner_id)
705                                 memset(&rte_eth_devices[port_id].data->owner, 0,
706                                        sizeof(struct rte_eth_dev_owner));
707                 RTE_ETHDEV_LOG(NOTICE,
708                         "All port owners owned by %016"PRIx64" identifier have removed\n",
709                         owner_id);
710         } else {
711                 RTE_ETHDEV_LOG(ERR,
712                                "Invalid owner id=%016"PRIx64"\n",
713                                owner_id);
714                 ret = -EINVAL;
715         }
716
717         rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock);
718
719         return ret;
720 }
721
722 int
723 rte_eth_dev_owner_get(const uint16_t port_id, struct rte_eth_dev_owner *owner)
724 {
725         int ret = 0;
726         struct rte_eth_dev *ethdev = &rte_eth_devices[port_id];
727
728         rte_eth_dev_shared_data_prepare();
729
730         rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock);
731
732         if (port_id >= RTE_MAX_ETHPORTS || !is_allocated(ethdev)) {
733                 RTE_ETHDEV_LOG(ERR, "Port id %"PRIu16" is not allocated\n",
734                         port_id);
735                 ret = -ENODEV;
736         } else {
737                 rte_memcpy(owner, &ethdev->data->owner, sizeof(*owner));
738         }
739
740         rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock);
741         return ret;
742 }
743
744 int
745 rte_eth_dev_socket_id(uint16_t port_id)
746 {
747         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -1);
748         return rte_eth_devices[port_id].data->numa_node;
749 }
750
751 void *
752 rte_eth_dev_get_sec_ctx(uint16_t port_id)
753 {
754         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, NULL);
755         return rte_eth_devices[port_id].security_ctx;
756 }
757
758 uint16_t
759 rte_eth_dev_count_avail(void)
760 {
761         uint16_t p;
762         uint16_t count;
763
764         count = 0;
765
766         RTE_ETH_FOREACH_DEV(p)
767                 count++;
768
769         return count;
770 }
771
772 uint16_t
773 rte_eth_dev_count_total(void)
774 {
775         uint16_t port, count = 0;
776
777         RTE_ETH_FOREACH_VALID_DEV(port)
778                 count++;
779
780         return count;
781 }
782
783 int
784 rte_eth_dev_get_name_by_port(uint16_t port_id, char *name)
785 {
786         char *tmp;
787
788         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
789
790         if (name == NULL) {
791                 RTE_ETHDEV_LOG(ERR, "Null pointer is specified\n");
792                 return -EINVAL;
793         }
794
795         /* shouldn't check 'rte_eth_devices[i].data',
796          * because it might be overwritten by VDEV PMD */
797         tmp = rte_eth_dev_shared_data->data[port_id].name;
798         strcpy(name, tmp);
799         return 0;
800 }
801
802 int
803 rte_eth_dev_get_port_by_name(const char *name, uint16_t *port_id)
804 {
805         uint32_t pid;
806
807         if (name == NULL) {
808                 RTE_ETHDEV_LOG(ERR, "Null pointer is specified\n");
809                 return -EINVAL;
810         }
811
812         RTE_ETH_FOREACH_VALID_DEV(pid)
813                 if (!strcmp(name, rte_eth_dev_shared_data->data[pid].name)) {
814                         *port_id = pid;
815                         return 0;
816                 }
817
818         return -ENODEV;
819 }
820
821 static int
822 eth_err(uint16_t port_id, int ret)
823 {
824         if (ret == 0)
825                 return 0;
826         if (rte_eth_dev_is_removed(port_id))
827                 return -EIO;
828         return ret;
829 }
830
831 static int
832 rte_eth_dev_rx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
833 {
834         uint16_t old_nb_queues = dev->data->nb_rx_queues;
835         void **rxq;
836         unsigned i;
837
838         if (dev->data->rx_queues == NULL && nb_queues != 0) { /* first time configuration */
839                 dev->data->rx_queues = rte_zmalloc("ethdev->rx_queues",
840                                 sizeof(dev->data->rx_queues[0]) * nb_queues,
841                                 RTE_CACHE_LINE_SIZE);
842                 if (dev->data->rx_queues == NULL) {
843                         dev->data->nb_rx_queues = 0;
844                         return -(ENOMEM);
845                 }
846         } else if (dev->data->rx_queues != NULL && nb_queues != 0) { /* re-configure */
847                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_release, -ENOTSUP);
848
849                 rxq = dev->data->rx_queues;
850
851                 for (i = nb_queues; i < old_nb_queues; i++)
852                         (*dev->dev_ops->rx_queue_release)(rxq[i]);
853                 rxq = rte_realloc(rxq, sizeof(rxq[0]) * nb_queues,
854                                 RTE_CACHE_LINE_SIZE);
855                 if (rxq == NULL)
856                         return -(ENOMEM);
857                 if (nb_queues > old_nb_queues) {
858                         uint16_t new_qs = nb_queues - old_nb_queues;
859
860                         memset(rxq + old_nb_queues, 0,
861                                 sizeof(rxq[0]) * new_qs);
862                 }
863
864                 dev->data->rx_queues = rxq;
865
866         } else if (dev->data->rx_queues != NULL && nb_queues == 0) {
867                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_release, -ENOTSUP);
868
869                 rxq = dev->data->rx_queues;
870
871                 for (i = nb_queues; i < old_nb_queues; i++)
872                         (*dev->dev_ops->rx_queue_release)(rxq[i]);
873
874                 rte_free(dev->data->rx_queues);
875                 dev->data->rx_queues = NULL;
876         }
877         dev->data->nb_rx_queues = nb_queues;
878         return 0;
879 }
880
881 static int
882 eth_dev_validate_rx_queue(const struct rte_eth_dev *dev, uint16_t rx_queue_id)
883 {
884         uint16_t port_id;
885
886         if (rx_queue_id >= dev->data->nb_rx_queues) {
887                 port_id = dev->data->port_id;
888                 RTE_ETHDEV_LOG(ERR,
889                                "Invalid Rx queue_id=%u of device with port_id=%u\n",
890                                rx_queue_id, port_id);
891                 return -EINVAL;
892         }
893
894         if (dev->data->rx_queues[rx_queue_id] == NULL) {
895                 port_id = dev->data->port_id;
896                 RTE_ETHDEV_LOG(ERR,
897                                "Queue %u of device with port_id=%u has not been setup\n",
898                                rx_queue_id, port_id);
899                 return -EINVAL;
900         }
901
902         return 0;
903 }
904
905 static int
906 eth_dev_validate_tx_queue(const struct rte_eth_dev *dev, uint16_t tx_queue_id)
907 {
908         uint16_t port_id;
909
910         if (tx_queue_id >= dev->data->nb_tx_queues) {
911                 port_id = dev->data->port_id;
912                 RTE_ETHDEV_LOG(ERR,
913                                "Invalid Tx queue_id=%u of device with port_id=%u\n",
914                                tx_queue_id, port_id);
915                 return -EINVAL;
916         }
917
918         if (dev->data->tx_queues[tx_queue_id] == NULL) {
919                 port_id = dev->data->port_id;
920                 RTE_ETHDEV_LOG(ERR,
921                                "Queue %u of device with port_id=%u has not been setup\n",
922                                tx_queue_id, port_id);
923                 return -EINVAL;
924         }
925
926         return 0;
927 }
928
929 int
930 rte_eth_dev_rx_queue_start(uint16_t port_id, uint16_t rx_queue_id)
931 {
932         struct rte_eth_dev *dev;
933         int ret;
934
935         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
936
937         dev = &rte_eth_devices[port_id];
938         if (!dev->data->dev_started) {
939                 RTE_ETHDEV_LOG(ERR,
940                         "Port %u must be started before start any queue\n",
941                         port_id);
942                 return -EINVAL;
943         }
944
945         ret = eth_dev_validate_rx_queue(dev, rx_queue_id);
946         if (ret != 0)
947                 return ret;
948
949         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_start, -ENOTSUP);
950
951         if (rte_eth_dev_is_rx_hairpin_queue(dev, rx_queue_id)) {
952                 RTE_ETHDEV_LOG(INFO,
953                         "Can't start Rx hairpin queue %"PRIu16" of device with port_id=%"PRIu16"\n",
954                         rx_queue_id, port_id);
955                 return -EINVAL;
956         }
957
958         if (dev->data->rx_queue_state[rx_queue_id] != RTE_ETH_QUEUE_STATE_STOPPED) {
959                 RTE_ETHDEV_LOG(INFO,
960                         "Queue %"PRIu16" of device with port_id=%"PRIu16" already started\n",
961                         rx_queue_id, port_id);
962                 return 0;
963         }
964
965         return eth_err(port_id, dev->dev_ops->rx_queue_start(dev,
966                                                              rx_queue_id));
967
968 }
969
970 int
971 rte_eth_dev_rx_queue_stop(uint16_t port_id, uint16_t rx_queue_id)
972 {
973         struct rte_eth_dev *dev;
974         int ret;
975
976         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
977
978         dev = &rte_eth_devices[port_id];
979
980         ret = eth_dev_validate_rx_queue(dev, rx_queue_id);
981         if (ret != 0)
982                 return ret;
983
984         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_stop, -ENOTSUP);
985
986         if (rte_eth_dev_is_rx_hairpin_queue(dev, rx_queue_id)) {
987                 RTE_ETHDEV_LOG(INFO,
988                         "Can't stop Rx hairpin queue %"PRIu16" of device with port_id=%"PRIu16"\n",
989                         rx_queue_id, port_id);
990                 return -EINVAL;
991         }
992
993         if (dev->data->rx_queue_state[rx_queue_id] == RTE_ETH_QUEUE_STATE_STOPPED) {
994                 RTE_ETHDEV_LOG(INFO,
995                         "Queue %"PRIu16" of device with port_id=%"PRIu16" already stopped\n",
996                         rx_queue_id, port_id);
997                 return 0;
998         }
999
1000         return eth_err(port_id, dev->dev_ops->rx_queue_stop(dev, rx_queue_id));
1001
1002 }
1003
1004 int
1005 rte_eth_dev_tx_queue_start(uint16_t port_id, uint16_t tx_queue_id)
1006 {
1007         struct rte_eth_dev *dev;
1008         int ret;
1009
1010         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1011
1012         dev = &rte_eth_devices[port_id];
1013         if (!dev->data->dev_started) {
1014                 RTE_ETHDEV_LOG(ERR,
1015                         "Port %u must be started before start any queue\n",
1016                         port_id);
1017                 return -EINVAL;
1018         }
1019
1020         ret = eth_dev_validate_tx_queue(dev, tx_queue_id);
1021         if (ret != 0)
1022                 return ret;
1023
1024         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_start, -ENOTSUP);
1025
1026         if (rte_eth_dev_is_tx_hairpin_queue(dev, tx_queue_id)) {
1027                 RTE_ETHDEV_LOG(INFO,
1028                         "Can't start Tx hairpin queue %"PRIu16" of device with port_id=%"PRIu16"\n",
1029                         tx_queue_id, port_id);
1030                 return -EINVAL;
1031         }
1032
1033         if (dev->data->tx_queue_state[tx_queue_id] != RTE_ETH_QUEUE_STATE_STOPPED) {
1034                 RTE_ETHDEV_LOG(INFO,
1035                         "Queue %"PRIu16" of device with port_id=%"PRIu16" already started\n",
1036                         tx_queue_id, port_id);
1037                 return 0;
1038         }
1039
1040         return eth_err(port_id, dev->dev_ops->tx_queue_start(dev, tx_queue_id));
1041 }
1042
1043 int
1044 rte_eth_dev_tx_queue_stop(uint16_t port_id, uint16_t tx_queue_id)
1045 {
1046         struct rte_eth_dev *dev;
1047         int ret;
1048
1049         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1050
1051         dev = &rte_eth_devices[port_id];
1052
1053         ret = eth_dev_validate_tx_queue(dev, tx_queue_id);
1054         if (ret != 0)
1055                 return ret;
1056
1057         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_stop, -ENOTSUP);
1058
1059         if (rte_eth_dev_is_tx_hairpin_queue(dev, tx_queue_id)) {
1060                 RTE_ETHDEV_LOG(INFO,
1061                         "Can't stop Tx hairpin queue %"PRIu16" of device with port_id=%"PRIu16"\n",
1062                         tx_queue_id, port_id);
1063                 return -EINVAL;
1064         }
1065
1066         if (dev->data->tx_queue_state[tx_queue_id] == RTE_ETH_QUEUE_STATE_STOPPED) {
1067                 RTE_ETHDEV_LOG(INFO,
1068                         "Queue %"PRIu16" of device with port_id=%"PRIu16" already stopped\n",
1069                         tx_queue_id, port_id);
1070                 return 0;
1071         }
1072
1073         return eth_err(port_id, dev->dev_ops->tx_queue_stop(dev, tx_queue_id));
1074
1075 }
1076
1077 static int
1078 rte_eth_dev_tx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
1079 {
1080         uint16_t old_nb_queues = dev->data->nb_tx_queues;
1081         void **txq;
1082         unsigned i;
1083
1084         if (dev->data->tx_queues == NULL && nb_queues != 0) { /* first time configuration */
1085                 dev->data->tx_queues = rte_zmalloc("ethdev->tx_queues",
1086                                                    sizeof(dev->data->tx_queues[0]) * nb_queues,
1087                                                    RTE_CACHE_LINE_SIZE);
1088                 if (dev->data->tx_queues == NULL) {
1089                         dev->data->nb_tx_queues = 0;
1090                         return -(ENOMEM);
1091                 }
1092         } else if (dev->data->tx_queues != NULL && nb_queues != 0) { /* re-configure */
1093                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_release, -ENOTSUP);
1094
1095                 txq = dev->data->tx_queues;
1096
1097                 for (i = nb_queues; i < old_nb_queues; i++)
1098                         (*dev->dev_ops->tx_queue_release)(txq[i]);
1099                 txq = rte_realloc(txq, sizeof(txq[0]) * nb_queues,
1100                                   RTE_CACHE_LINE_SIZE);
1101                 if (txq == NULL)
1102                         return -ENOMEM;
1103                 if (nb_queues > old_nb_queues) {
1104                         uint16_t new_qs = nb_queues - old_nb_queues;
1105
1106                         memset(txq + old_nb_queues, 0,
1107                                sizeof(txq[0]) * new_qs);
1108                 }
1109
1110                 dev->data->tx_queues = txq;
1111
1112         } else if (dev->data->tx_queues != NULL && nb_queues == 0) {
1113                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_release, -ENOTSUP);
1114
1115                 txq = dev->data->tx_queues;
1116
1117                 for (i = nb_queues; i < old_nb_queues; i++)
1118                         (*dev->dev_ops->tx_queue_release)(txq[i]);
1119
1120                 rte_free(dev->data->tx_queues);
1121                 dev->data->tx_queues = NULL;
1122         }
1123         dev->data->nb_tx_queues = nb_queues;
1124         return 0;
1125 }
1126
1127 uint32_t
1128 rte_eth_speed_bitflag(uint32_t speed, int duplex)
1129 {
1130         switch (speed) {
1131         case ETH_SPEED_NUM_10M:
1132                 return duplex ? ETH_LINK_SPEED_10M : ETH_LINK_SPEED_10M_HD;
1133         case ETH_SPEED_NUM_100M:
1134                 return duplex ? ETH_LINK_SPEED_100M : ETH_LINK_SPEED_100M_HD;
1135         case ETH_SPEED_NUM_1G:
1136                 return ETH_LINK_SPEED_1G;
1137         case ETH_SPEED_NUM_2_5G:
1138                 return ETH_LINK_SPEED_2_5G;
1139         case ETH_SPEED_NUM_5G:
1140                 return ETH_LINK_SPEED_5G;
1141         case ETH_SPEED_NUM_10G:
1142                 return ETH_LINK_SPEED_10G;
1143         case ETH_SPEED_NUM_20G:
1144                 return ETH_LINK_SPEED_20G;
1145         case ETH_SPEED_NUM_25G:
1146                 return ETH_LINK_SPEED_25G;
1147         case ETH_SPEED_NUM_40G:
1148                 return ETH_LINK_SPEED_40G;
1149         case ETH_SPEED_NUM_50G:
1150                 return ETH_LINK_SPEED_50G;
1151         case ETH_SPEED_NUM_56G:
1152                 return ETH_LINK_SPEED_56G;
1153         case ETH_SPEED_NUM_100G:
1154                 return ETH_LINK_SPEED_100G;
1155         case ETH_SPEED_NUM_200G:
1156                 return ETH_LINK_SPEED_200G;
1157         default:
1158                 return 0;
1159         }
1160 }
1161
1162 const char *
1163 rte_eth_dev_rx_offload_name(uint64_t offload)
1164 {
1165         const char *name = "UNKNOWN";
1166         unsigned int i;
1167
1168         for (i = 0; i < RTE_DIM(rte_rx_offload_names); ++i) {
1169                 if (offload == rte_rx_offload_names[i].offload) {
1170                         name = rte_rx_offload_names[i].name;
1171                         break;
1172                 }
1173         }
1174
1175         return name;
1176 }
1177
1178 const char *
1179 rte_eth_dev_tx_offload_name(uint64_t offload)
1180 {
1181         const char *name = "UNKNOWN";
1182         unsigned int i;
1183
1184         for (i = 0; i < RTE_DIM(rte_tx_offload_names); ++i) {
1185                 if (offload == rte_tx_offload_names[i].offload) {
1186                         name = rte_tx_offload_names[i].name;
1187                         break;
1188                 }
1189         }
1190
1191         return name;
1192 }
1193
1194 static inline int
1195 check_lro_pkt_size(uint16_t port_id, uint32_t config_size,
1196                    uint32_t max_rx_pkt_len, uint32_t dev_info_size)
1197 {
1198         int ret = 0;
1199
1200         if (dev_info_size == 0) {
1201                 if (config_size != max_rx_pkt_len) {
1202                         RTE_ETHDEV_LOG(ERR, "Ethdev port_id=%d max_lro_pkt_size"
1203                                        " %u != %u is not allowed\n",
1204                                        port_id, config_size, max_rx_pkt_len);
1205                         ret = -EINVAL;
1206                 }
1207         } else if (config_size > dev_info_size) {
1208                 RTE_ETHDEV_LOG(ERR, "Ethdev port_id=%d max_lro_pkt_size %u "
1209                                "> max allowed value %u\n", port_id, config_size,
1210                                dev_info_size);
1211                 ret = -EINVAL;
1212         } else if (config_size < RTE_ETHER_MIN_LEN) {
1213                 RTE_ETHDEV_LOG(ERR, "Ethdev port_id=%d max_lro_pkt_size %u "
1214                                "< min allowed value %u\n", port_id, config_size,
1215                                (unsigned int)RTE_ETHER_MIN_LEN);
1216                 ret = -EINVAL;
1217         }
1218         return ret;
1219 }
1220
1221 /*
1222  * Validate offloads that are requested through rte_eth_dev_configure against
1223  * the offloads successfully set by the ethernet device.
1224  *
1225  * @param port_id
1226  *   The port identifier of the Ethernet device.
1227  * @param req_offloads
1228  *   The offloads that have been requested through `rte_eth_dev_configure`.
1229  * @param set_offloads
1230  *   The offloads successfully set by the ethernet device.
1231  * @param offload_type
1232  *   The offload type i.e. Rx/Tx string.
1233  * @param offload_name
1234  *   The function that prints the offload name.
1235  * @return
1236  *   - (0) if validation successful.
1237  *   - (-EINVAL) if requested offload has been silently disabled.
1238  *
1239  */
1240 static int
1241 validate_offloads(uint16_t port_id, uint64_t req_offloads,
1242                   uint64_t set_offloads, const char *offload_type,
1243                   const char *(*offload_name)(uint64_t))
1244 {
1245         uint64_t offloads_diff = req_offloads ^ set_offloads;
1246         uint64_t offload;
1247         int ret = 0;
1248
1249         while (offloads_diff != 0) {
1250                 /* Check if any offload is requested but not enabled. */
1251                 offload = 1ULL << __builtin_ctzll(offloads_diff);
1252                 if (offload & req_offloads) {
1253                         RTE_ETHDEV_LOG(ERR,
1254                                 "Port %u failed to enable %s offload %s\n",
1255                                 port_id, offload_type, offload_name(offload));
1256                         ret = -EINVAL;
1257                 }
1258
1259                 /* Check if offload couldn't be disabled. */
1260                 if (offload & set_offloads) {
1261                         RTE_ETHDEV_LOG(DEBUG,
1262                                 "Port %u %s offload %s is not requested but enabled\n",
1263                                 port_id, offload_type, offload_name(offload));
1264                 }
1265
1266                 offloads_diff &= ~offload;
1267         }
1268
1269         return ret;
1270 }
1271
1272 int
1273 rte_eth_dev_configure(uint16_t port_id, uint16_t nb_rx_q, uint16_t nb_tx_q,
1274                       const struct rte_eth_conf *dev_conf)
1275 {
1276         struct rte_eth_dev *dev;
1277         struct rte_eth_dev_info dev_info;
1278         struct rte_eth_conf orig_conf;
1279         int diag;
1280         int ret;
1281
1282         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1283
1284         dev = &rte_eth_devices[port_id];
1285
1286         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_configure, -ENOTSUP);
1287
1288         if (dev->data->dev_started) {
1289                 RTE_ETHDEV_LOG(ERR,
1290                         "Port %u must be stopped to allow configuration\n",
1291                         port_id);
1292                 return -EBUSY;
1293         }
1294
1295          /* Store original config, as rollback required on failure */
1296         memcpy(&orig_conf, &dev->data->dev_conf, sizeof(dev->data->dev_conf));
1297
1298         /*
1299          * Copy the dev_conf parameter into the dev structure.
1300          * rte_eth_dev_info_get() requires dev_conf, copy it before dev_info get
1301          */
1302         if (dev_conf != &dev->data->dev_conf)
1303                 memcpy(&dev->data->dev_conf, dev_conf,
1304                        sizeof(dev->data->dev_conf));
1305
1306         ret = rte_eth_dev_info_get(port_id, &dev_info);
1307         if (ret != 0)
1308                 goto rollback;
1309
1310         /* If number of queues specified by application for both Rx and Tx is
1311          * zero, use driver preferred values. This cannot be done individually
1312          * as it is valid for either Tx or Rx (but not both) to be zero.
1313          * If driver does not provide any preferred valued, fall back on
1314          * EAL defaults.
1315          */
1316         if (nb_rx_q == 0 && nb_tx_q == 0) {
1317                 nb_rx_q = dev_info.default_rxportconf.nb_queues;
1318                 if (nb_rx_q == 0)
1319                         nb_rx_q = RTE_ETH_DEV_FALLBACK_RX_NBQUEUES;
1320                 nb_tx_q = dev_info.default_txportconf.nb_queues;
1321                 if (nb_tx_q == 0)
1322                         nb_tx_q = RTE_ETH_DEV_FALLBACK_TX_NBQUEUES;
1323         }
1324
1325         if (nb_rx_q > RTE_MAX_QUEUES_PER_PORT) {
1326                 RTE_ETHDEV_LOG(ERR,
1327                         "Number of RX queues requested (%u) is greater than max supported(%d)\n",
1328                         nb_rx_q, RTE_MAX_QUEUES_PER_PORT);
1329                 ret = -EINVAL;
1330                 goto rollback;
1331         }
1332
1333         if (nb_tx_q > RTE_MAX_QUEUES_PER_PORT) {
1334                 RTE_ETHDEV_LOG(ERR,
1335                         "Number of TX queues requested (%u) is greater than max supported(%d)\n",
1336                         nb_tx_q, RTE_MAX_QUEUES_PER_PORT);
1337                 ret = -EINVAL;
1338                 goto rollback;
1339         }
1340
1341         /*
1342          * Check that the numbers of RX and TX queues are not greater
1343          * than the maximum number of RX and TX queues supported by the
1344          * configured device.
1345          */
1346         if (nb_rx_q > dev_info.max_rx_queues) {
1347                 RTE_ETHDEV_LOG(ERR, "Ethdev port_id=%u nb_rx_queues=%u > %u\n",
1348                         port_id, nb_rx_q, dev_info.max_rx_queues);
1349                 ret = -EINVAL;
1350                 goto rollback;
1351         }
1352
1353         if (nb_tx_q > dev_info.max_tx_queues) {
1354                 RTE_ETHDEV_LOG(ERR, "Ethdev port_id=%u nb_tx_queues=%u > %u\n",
1355                         port_id, nb_tx_q, dev_info.max_tx_queues);
1356                 ret = -EINVAL;
1357                 goto rollback;
1358         }
1359
1360         /* Check that the device supports requested interrupts */
1361         if ((dev_conf->intr_conf.lsc == 1) &&
1362                         (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC))) {
1363                 RTE_ETHDEV_LOG(ERR, "Driver %s does not support lsc\n",
1364                         dev->device->driver->name);
1365                 ret = -EINVAL;
1366                 goto rollback;
1367         }
1368         if ((dev_conf->intr_conf.rmv == 1) &&
1369                         (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_RMV))) {
1370                 RTE_ETHDEV_LOG(ERR, "Driver %s does not support rmv\n",
1371                         dev->device->driver->name);
1372                 ret = -EINVAL;
1373                 goto rollback;
1374         }
1375
1376         /*
1377          * If jumbo frames are enabled, check that the maximum RX packet
1378          * length is supported by the configured device.
1379          */
1380         if (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
1381                 if (dev_conf->rxmode.max_rx_pkt_len > dev_info.max_rx_pktlen) {
1382                         RTE_ETHDEV_LOG(ERR,
1383                                 "Ethdev port_id=%u max_rx_pkt_len %u > max valid value %u\n",
1384                                 port_id, dev_conf->rxmode.max_rx_pkt_len,
1385                                 dev_info.max_rx_pktlen);
1386                         ret = -EINVAL;
1387                         goto rollback;
1388                 } else if (dev_conf->rxmode.max_rx_pkt_len < RTE_ETHER_MIN_LEN) {
1389                         RTE_ETHDEV_LOG(ERR,
1390                                 "Ethdev port_id=%u max_rx_pkt_len %u < min valid value %u\n",
1391                                 port_id, dev_conf->rxmode.max_rx_pkt_len,
1392                                 (unsigned int)RTE_ETHER_MIN_LEN);
1393                         ret = -EINVAL;
1394                         goto rollback;
1395                 }
1396         } else {
1397                 if (dev_conf->rxmode.max_rx_pkt_len < RTE_ETHER_MIN_LEN ||
1398                         dev_conf->rxmode.max_rx_pkt_len > RTE_ETHER_MAX_LEN)
1399                         /* Use default value */
1400                         dev->data->dev_conf.rxmode.max_rx_pkt_len =
1401                                                         RTE_ETHER_MAX_LEN;
1402         }
1403
1404         /*
1405          * If LRO is enabled, check that the maximum aggregated packet
1406          * size is supported by the configured device.
1407          */
1408         if (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_TCP_LRO) {
1409                 if (dev_conf->rxmode.max_lro_pkt_size == 0)
1410                         dev->data->dev_conf.rxmode.max_lro_pkt_size =
1411                                 dev->data->dev_conf.rxmode.max_rx_pkt_len;
1412                 ret = check_lro_pkt_size(port_id,
1413                                 dev->data->dev_conf.rxmode.max_lro_pkt_size,
1414                                 dev->data->dev_conf.rxmode.max_rx_pkt_len,
1415                                 dev_info.max_lro_pkt_size);
1416                 if (ret != 0)
1417                         goto rollback;
1418         }
1419
1420         /* Any requested offloading must be within its device capabilities */
1421         if ((dev_conf->rxmode.offloads & dev_info.rx_offload_capa) !=
1422              dev_conf->rxmode.offloads) {
1423                 RTE_ETHDEV_LOG(ERR,
1424                         "Ethdev port_id=%u requested Rx offloads 0x%"PRIx64" doesn't match Rx offloads "
1425                         "capabilities 0x%"PRIx64" in %s()\n",
1426                         port_id, dev_conf->rxmode.offloads,
1427                         dev_info.rx_offload_capa,
1428                         __func__);
1429                 ret = -EINVAL;
1430                 goto rollback;
1431         }
1432         if ((dev_conf->txmode.offloads & dev_info.tx_offload_capa) !=
1433              dev_conf->txmode.offloads) {
1434                 RTE_ETHDEV_LOG(ERR,
1435                         "Ethdev port_id=%u requested Tx offloads 0x%"PRIx64" doesn't match Tx offloads "
1436                         "capabilities 0x%"PRIx64" in %s()\n",
1437                         port_id, dev_conf->txmode.offloads,
1438                         dev_info.tx_offload_capa,
1439                         __func__);
1440                 ret = -EINVAL;
1441                 goto rollback;
1442         }
1443
1444         dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf =
1445                 rte_eth_rss_hf_refine(dev_conf->rx_adv_conf.rss_conf.rss_hf);
1446
1447         /* Check that device supports requested rss hash functions. */
1448         if ((dev_info.flow_type_rss_offloads |
1449              dev_conf->rx_adv_conf.rss_conf.rss_hf) !=
1450             dev_info.flow_type_rss_offloads) {
1451                 RTE_ETHDEV_LOG(ERR,
1452                         "Ethdev port_id=%u invalid rss_hf: 0x%"PRIx64", valid value: 0x%"PRIx64"\n",
1453                         port_id, dev_conf->rx_adv_conf.rss_conf.rss_hf,
1454                         dev_info.flow_type_rss_offloads);
1455                 ret = -EINVAL;
1456                 goto rollback;
1457         }
1458
1459         /* Check if Rx RSS distribution is disabled but RSS hash is enabled. */
1460         if (((dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) == 0) &&
1461             (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_RSS_HASH)) {
1462                 RTE_ETHDEV_LOG(ERR,
1463                         "Ethdev port_id=%u config invalid Rx mq_mode without RSS but %s offload is requested\n",
1464                         port_id,
1465                         rte_eth_dev_rx_offload_name(DEV_RX_OFFLOAD_RSS_HASH));
1466                 ret = -EINVAL;
1467                 goto rollback;
1468         }
1469
1470         /*
1471          * Setup new number of RX/TX queues and reconfigure device.
1472          */
1473         diag = rte_eth_dev_rx_queue_config(dev, nb_rx_q);
1474         if (diag != 0) {
1475                 RTE_ETHDEV_LOG(ERR,
1476                         "Port%u rte_eth_dev_rx_queue_config = %d\n",
1477                         port_id, diag);
1478                 ret = diag;
1479                 goto rollback;
1480         }
1481
1482         diag = rte_eth_dev_tx_queue_config(dev, nb_tx_q);
1483         if (diag != 0) {
1484                 RTE_ETHDEV_LOG(ERR,
1485                         "Port%u rte_eth_dev_tx_queue_config = %d\n",
1486                         port_id, diag);
1487                 rte_eth_dev_rx_queue_config(dev, 0);
1488                 ret = diag;
1489                 goto rollback;
1490         }
1491
1492         diag = (*dev->dev_ops->dev_configure)(dev);
1493         if (diag != 0) {
1494                 RTE_ETHDEV_LOG(ERR, "Port%u dev_configure = %d\n",
1495                         port_id, diag);
1496                 ret = eth_err(port_id, diag);
1497                 goto reset_queues;
1498         }
1499
1500         /* Initialize Rx profiling if enabled at compilation time. */
1501         diag = __rte_eth_dev_profile_init(port_id, dev);
1502         if (diag != 0) {
1503                 RTE_ETHDEV_LOG(ERR, "Port%u __rte_eth_dev_profile_init = %d\n",
1504                         port_id, diag);
1505                 ret = eth_err(port_id, diag);
1506                 goto reset_queues;
1507         }
1508
1509         /* Validate Rx offloads. */
1510         diag = validate_offloads(port_id,
1511                         dev_conf->rxmode.offloads,
1512                         dev->data->dev_conf.rxmode.offloads, "Rx",
1513                         rte_eth_dev_rx_offload_name);
1514         if (diag != 0) {
1515                 ret = diag;
1516                 goto reset_queues;
1517         }
1518
1519         /* Validate Tx offloads. */
1520         diag = validate_offloads(port_id,
1521                         dev_conf->txmode.offloads,
1522                         dev->data->dev_conf.txmode.offloads, "Tx",
1523                         rte_eth_dev_tx_offload_name);
1524         if (diag != 0) {
1525                 ret = diag;
1526                 goto reset_queues;
1527         }
1528
1529         rte_ethdev_trace_configure(port_id, nb_rx_q, nb_tx_q, dev_conf, 0);
1530         return 0;
1531 reset_queues:
1532         rte_eth_dev_rx_queue_config(dev, 0);
1533         rte_eth_dev_tx_queue_config(dev, 0);
1534 rollback:
1535         memcpy(&dev->data->dev_conf, &orig_conf, sizeof(dev->data->dev_conf));
1536
1537         rte_ethdev_trace_configure(port_id, nb_rx_q, nb_tx_q, dev_conf, ret);
1538         return ret;
1539 }
1540
1541 void
1542 rte_eth_dev_internal_reset(struct rte_eth_dev *dev)
1543 {
1544         if (dev->data->dev_started) {
1545                 RTE_ETHDEV_LOG(ERR, "Port %u must be stopped to allow reset\n",
1546                         dev->data->port_id);
1547                 return;
1548         }
1549
1550         rte_eth_dev_rx_queue_config(dev, 0);
1551         rte_eth_dev_tx_queue_config(dev, 0);
1552
1553         memset(&dev->data->dev_conf, 0, sizeof(dev->data->dev_conf));
1554 }
1555
1556 static void
1557 rte_eth_dev_mac_restore(struct rte_eth_dev *dev,
1558                         struct rte_eth_dev_info *dev_info)
1559 {
1560         struct rte_ether_addr *addr;
1561         uint16_t i;
1562         uint32_t pool = 0;
1563         uint64_t pool_mask;
1564
1565         /* replay MAC address configuration including default MAC */
1566         addr = &dev->data->mac_addrs[0];
1567         if (*dev->dev_ops->mac_addr_set != NULL)
1568                 (*dev->dev_ops->mac_addr_set)(dev, addr);
1569         else if (*dev->dev_ops->mac_addr_add != NULL)
1570                 (*dev->dev_ops->mac_addr_add)(dev, addr, 0, pool);
1571
1572         if (*dev->dev_ops->mac_addr_add != NULL) {
1573                 for (i = 1; i < dev_info->max_mac_addrs; i++) {
1574                         addr = &dev->data->mac_addrs[i];
1575
1576                         /* skip zero address */
1577                         if (rte_is_zero_ether_addr(addr))
1578                                 continue;
1579
1580                         pool = 0;
1581                         pool_mask = dev->data->mac_pool_sel[i];
1582
1583                         do {
1584                                 if (pool_mask & 1ULL)
1585                                         (*dev->dev_ops->mac_addr_add)(dev,
1586                                                 addr, i, pool);
1587                                 pool_mask >>= 1;
1588                                 pool++;
1589                         } while (pool_mask);
1590                 }
1591         }
1592 }
1593
1594 static int
1595 rte_eth_dev_config_restore(struct rte_eth_dev *dev,
1596                            struct rte_eth_dev_info *dev_info, uint16_t port_id)
1597 {
1598         int ret;
1599
1600         if (!(*dev_info->dev_flags & RTE_ETH_DEV_NOLIVE_MAC_ADDR))
1601                 rte_eth_dev_mac_restore(dev, dev_info);
1602
1603         /* replay promiscuous configuration */
1604         /*
1605          * use callbacks directly since we don't need port_id check and
1606          * would like to bypass the same value set
1607          */
1608         if (rte_eth_promiscuous_get(port_id) == 1 &&
1609             *dev->dev_ops->promiscuous_enable != NULL) {
1610                 ret = eth_err(port_id,
1611                               (*dev->dev_ops->promiscuous_enable)(dev));
1612                 if (ret != 0 && ret != -ENOTSUP) {
1613                         RTE_ETHDEV_LOG(ERR,
1614                                 "Failed to enable promiscuous mode for device (port %u): %s\n",
1615                                 port_id, rte_strerror(-ret));
1616                         return ret;
1617                 }
1618         } else if (rte_eth_promiscuous_get(port_id) == 0 &&
1619                    *dev->dev_ops->promiscuous_disable != NULL) {
1620                 ret = eth_err(port_id,
1621                               (*dev->dev_ops->promiscuous_disable)(dev));
1622                 if (ret != 0 && ret != -ENOTSUP) {
1623                         RTE_ETHDEV_LOG(ERR,
1624                                 "Failed to disable promiscuous mode for device (port %u): %s\n",
1625                                 port_id, rte_strerror(-ret));
1626                         return ret;
1627                 }
1628         }
1629
1630         /* replay all multicast configuration */
1631         /*
1632          * use callbacks directly since we don't need port_id check and
1633          * would like to bypass the same value set
1634          */
1635         if (rte_eth_allmulticast_get(port_id) == 1 &&
1636             *dev->dev_ops->allmulticast_enable != NULL) {
1637                 ret = eth_err(port_id,
1638                               (*dev->dev_ops->allmulticast_enable)(dev));
1639                 if (ret != 0 && ret != -ENOTSUP) {
1640                         RTE_ETHDEV_LOG(ERR,
1641                                 "Failed to enable allmulticast mode for device (port %u): %s\n",
1642                                 port_id, rte_strerror(-ret));
1643                         return ret;
1644                 }
1645         } else if (rte_eth_allmulticast_get(port_id) == 0 &&
1646                    *dev->dev_ops->allmulticast_disable != NULL) {
1647                 ret = eth_err(port_id,
1648                               (*dev->dev_ops->allmulticast_disable)(dev));
1649                 if (ret != 0 && ret != -ENOTSUP) {
1650                         RTE_ETHDEV_LOG(ERR,
1651                                 "Failed to disable allmulticast mode for device (port %u): %s\n",
1652                                 port_id, rte_strerror(-ret));
1653                         return ret;
1654                 }
1655         }
1656
1657         return 0;
1658 }
1659
1660 int
1661 rte_eth_dev_start(uint16_t port_id)
1662 {
1663         struct rte_eth_dev *dev;
1664         struct rte_eth_dev_info dev_info;
1665         int diag;
1666         int ret;
1667
1668         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1669
1670         dev = &rte_eth_devices[port_id];
1671
1672         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
1673
1674         if (dev->data->dev_started != 0) {
1675                 RTE_ETHDEV_LOG(INFO,
1676                         "Device with port_id=%"PRIu16" already started\n",
1677                         port_id);
1678                 return 0;
1679         }
1680
1681         ret = rte_eth_dev_info_get(port_id, &dev_info);
1682         if (ret != 0)
1683                 return ret;
1684
1685         /* Lets restore MAC now if device does not support live change */
1686         if (*dev_info.dev_flags & RTE_ETH_DEV_NOLIVE_MAC_ADDR)
1687                 rte_eth_dev_mac_restore(dev, &dev_info);
1688
1689         diag = (*dev->dev_ops->dev_start)(dev);
1690         if (diag == 0)
1691                 dev->data->dev_started = 1;
1692         else
1693                 return eth_err(port_id, diag);
1694
1695         ret = rte_eth_dev_config_restore(dev, &dev_info, port_id);
1696         if (ret != 0) {
1697                 RTE_ETHDEV_LOG(ERR,
1698                         "Error during restoring configuration for device (port %u): %s\n",
1699                         port_id, rte_strerror(-ret));
1700                 rte_eth_dev_stop(port_id);
1701                 return ret;
1702         }
1703
1704         if (dev->data->dev_conf.intr_conf.lsc == 0) {
1705                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->link_update, -ENOTSUP);
1706                 (*dev->dev_ops->link_update)(dev, 0);
1707         }
1708
1709         rte_ethdev_trace_start(port_id);
1710         return 0;
1711 }
1712
1713 void
1714 rte_eth_dev_stop(uint16_t port_id)
1715 {
1716         struct rte_eth_dev *dev;
1717
1718         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1719         dev = &rte_eth_devices[port_id];
1720
1721         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
1722
1723         if (dev->data->dev_started == 0) {
1724                 RTE_ETHDEV_LOG(INFO,
1725                         "Device with port_id=%"PRIu16" already stopped\n",
1726                         port_id);
1727                 return;
1728         }
1729
1730         dev->data->dev_started = 0;
1731         (*dev->dev_ops->dev_stop)(dev);
1732         rte_ethdev_trace_stop(port_id);
1733 }
1734
1735 int
1736 rte_eth_dev_set_link_up(uint16_t port_id)
1737 {
1738         struct rte_eth_dev *dev;
1739
1740         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1741
1742         dev = &rte_eth_devices[port_id];
1743
1744         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_set_link_up, -ENOTSUP);
1745         return eth_err(port_id, (*dev->dev_ops->dev_set_link_up)(dev));
1746 }
1747
1748 int
1749 rte_eth_dev_set_link_down(uint16_t port_id)
1750 {
1751         struct rte_eth_dev *dev;
1752
1753         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1754
1755         dev = &rte_eth_devices[port_id];
1756
1757         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_set_link_down, -ENOTSUP);
1758         return eth_err(port_id, (*dev->dev_ops->dev_set_link_down)(dev));
1759 }
1760
1761 void
1762 rte_eth_dev_close(uint16_t port_id)
1763 {
1764         struct rte_eth_dev *dev;
1765
1766         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1767         dev = &rte_eth_devices[port_id];
1768
1769         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_close);
1770         dev->data->dev_started = 0;
1771         (*dev->dev_ops->dev_close)(dev);
1772
1773         rte_ethdev_trace_close(port_id);
1774         rte_eth_dev_release_port(dev);
1775 }
1776
1777 int
1778 rte_eth_dev_reset(uint16_t port_id)
1779 {
1780         struct rte_eth_dev *dev;
1781         int ret;
1782
1783         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1784         dev = &rte_eth_devices[port_id];
1785
1786         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_reset, -ENOTSUP);
1787
1788         rte_eth_dev_stop(port_id);
1789         ret = dev->dev_ops->dev_reset(dev);
1790
1791         return eth_err(port_id, ret);
1792 }
1793
1794 int
1795 rte_eth_dev_is_removed(uint16_t port_id)
1796 {
1797         struct rte_eth_dev *dev;
1798         int ret;
1799
1800         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, 0);
1801
1802         dev = &rte_eth_devices[port_id];
1803
1804         if (dev->state == RTE_ETH_DEV_REMOVED)
1805                 return 1;
1806
1807         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->is_removed, 0);
1808
1809         ret = dev->dev_ops->is_removed(dev);
1810         if (ret != 0)
1811                 /* Device is physically removed. */
1812                 dev->state = RTE_ETH_DEV_REMOVED;
1813
1814         return ret;
1815 }
1816
1817 int
1818 rte_eth_rx_queue_setup(uint16_t port_id, uint16_t rx_queue_id,
1819                        uint16_t nb_rx_desc, unsigned int socket_id,
1820                        const struct rte_eth_rxconf *rx_conf,
1821                        struct rte_mempool *mp)
1822 {
1823         int ret;
1824         uint32_t mbp_buf_size;
1825         struct rte_eth_dev *dev;
1826         struct rte_eth_dev_info dev_info;
1827         struct rte_eth_rxconf local_conf;
1828         void **rxq;
1829
1830         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1831
1832         dev = &rte_eth_devices[port_id];
1833         if (rx_queue_id >= dev->data->nb_rx_queues) {
1834                 RTE_ETHDEV_LOG(ERR, "Invalid RX queue_id=%u\n", rx_queue_id);
1835                 return -EINVAL;
1836         }
1837
1838         if (mp == NULL) {
1839                 RTE_ETHDEV_LOG(ERR, "Invalid null mempool pointer\n");
1840                 return -EINVAL;
1841         }
1842
1843         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_setup, -ENOTSUP);
1844
1845         /*
1846          * Check the size of the mbuf data buffer.
1847          * This value must be provided in the private data of the memory pool.
1848          * First check that the memory pool has a valid private data.
1849          */
1850         ret = rte_eth_dev_info_get(port_id, &dev_info);
1851         if (ret != 0)
1852                 return ret;
1853
1854         if (mp->private_data_size < sizeof(struct rte_pktmbuf_pool_private)) {
1855                 RTE_ETHDEV_LOG(ERR, "%s private_data_size %d < %d\n",
1856                         mp->name, (int)mp->private_data_size,
1857                         (int)sizeof(struct rte_pktmbuf_pool_private));
1858                 return -ENOSPC;
1859         }
1860         mbp_buf_size = rte_pktmbuf_data_room_size(mp);
1861
1862         if (mbp_buf_size < dev_info.min_rx_bufsize + RTE_PKTMBUF_HEADROOM) {
1863                 RTE_ETHDEV_LOG(ERR,
1864                         "%s mbuf_data_room_size %d < %d (RTE_PKTMBUF_HEADROOM=%d + min_rx_bufsize(dev)=%d)\n",
1865                         mp->name, (int)mbp_buf_size,
1866                         (int)(RTE_PKTMBUF_HEADROOM + dev_info.min_rx_bufsize),
1867                         (int)RTE_PKTMBUF_HEADROOM,
1868                         (int)dev_info.min_rx_bufsize);
1869                 return -EINVAL;
1870         }
1871
1872         /* Use default specified by driver, if nb_rx_desc is zero */
1873         if (nb_rx_desc == 0) {
1874                 nb_rx_desc = dev_info.default_rxportconf.ring_size;
1875                 /* If driver default is also zero, fall back on EAL default */
1876                 if (nb_rx_desc == 0)
1877                         nb_rx_desc = RTE_ETH_DEV_FALLBACK_RX_RINGSIZE;
1878         }
1879
1880         if (nb_rx_desc > dev_info.rx_desc_lim.nb_max ||
1881                         nb_rx_desc < dev_info.rx_desc_lim.nb_min ||
1882                         nb_rx_desc % dev_info.rx_desc_lim.nb_align != 0) {
1883
1884                 RTE_ETHDEV_LOG(ERR,
1885                         "Invalid value for nb_rx_desc(=%hu), should be: <= %hu, >= %hu, and a product of %hu\n",
1886                         nb_rx_desc, dev_info.rx_desc_lim.nb_max,
1887                         dev_info.rx_desc_lim.nb_min,
1888                         dev_info.rx_desc_lim.nb_align);
1889                 return -EINVAL;
1890         }
1891
1892         if (dev->data->dev_started &&
1893                 !(dev_info.dev_capa &
1894                         RTE_ETH_DEV_CAPA_RUNTIME_RX_QUEUE_SETUP))
1895                 return -EBUSY;
1896
1897         if (dev->data->dev_started &&
1898                 (dev->data->rx_queue_state[rx_queue_id] !=
1899                         RTE_ETH_QUEUE_STATE_STOPPED))
1900                 return -EBUSY;
1901
1902         rxq = dev->data->rx_queues;
1903         if (rxq[rx_queue_id]) {
1904                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_release,
1905                                         -ENOTSUP);
1906                 (*dev->dev_ops->rx_queue_release)(rxq[rx_queue_id]);
1907                 rxq[rx_queue_id] = NULL;
1908         }
1909
1910         if (rx_conf == NULL)
1911                 rx_conf = &dev_info.default_rxconf;
1912
1913         local_conf = *rx_conf;
1914
1915         /*
1916          * If an offloading has already been enabled in
1917          * rte_eth_dev_configure(), it has been enabled on all queues,
1918          * so there is no need to enable it in this queue again.
1919          * The local_conf.offloads input to underlying PMD only carries
1920          * those offloadings which are only enabled on this queue and
1921          * not enabled on all queues.
1922          */
1923         local_conf.offloads &= ~dev->data->dev_conf.rxmode.offloads;
1924
1925         /*
1926          * New added offloadings for this queue are those not enabled in
1927          * rte_eth_dev_configure() and they must be per-queue type.
1928          * A pure per-port offloading can't be enabled on a queue while
1929          * disabled on another queue. A pure per-port offloading can't
1930          * be enabled for any queue as new added one if it hasn't been
1931          * enabled in rte_eth_dev_configure().
1932          */
1933         if ((local_conf.offloads & dev_info.rx_queue_offload_capa) !=
1934              local_conf.offloads) {
1935                 RTE_ETHDEV_LOG(ERR,
1936                         "Ethdev port_id=%d rx_queue_id=%d, new added offloads 0x%"PRIx64" must be "
1937                         "within per-queue offload capabilities 0x%"PRIx64" in %s()\n",
1938                         port_id, rx_queue_id, local_conf.offloads,
1939                         dev_info.rx_queue_offload_capa,
1940                         __func__);
1941                 return -EINVAL;
1942         }
1943
1944         /*
1945          * If LRO is enabled, check that the maximum aggregated packet
1946          * size is supported by the configured device.
1947          */
1948         if (local_conf.offloads & DEV_RX_OFFLOAD_TCP_LRO) {
1949                 if (dev->data->dev_conf.rxmode.max_lro_pkt_size == 0)
1950                         dev->data->dev_conf.rxmode.max_lro_pkt_size =
1951                                 dev->data->dev_conf.rxmode.max_rx_pkt_len;
1952                 int ret = check_lro_pkt_size(port_id,
1953                                 dev->data->dev_conf.rxmode.max_lro_pkt_size,
1954                                 dev->data->dev_conf.rxmode.max_rx_pkt_len,
1955                                 dev_info.max_lro_pkt_size);
1956                 if (ret != 0)
1957                         return ret;
1958         }
1959
1960         ret = (*dev->dev_ops->rx_queue_setup)(dev, rx_queue_id, nb_rx_desc,
1961                                               socket_id, &local_conf, mp);
1962         if (!ret) {
1963                 if (!dev->data->min_rx_buf_size ||
1964                     dev->data->min_rx_buf_size > mbp_buf_size)
1965                         dev->data->min_rx_buf_size = mbp_buf_size;
1966         }
1967
1968         rte_ethdev_trace_rxq_setup(port_id, rx_queue_id, nb_rx_desc, mp,
1969                 rx_conf, ret);
1970         return eth_err(port_id, ret);
1971 }
1972
1973 int
1974 rte_eth_rx_hairpin_queue_setup(uint16_t port_id, uint16_t rx_queue_id,
1975                                uint16_t nb_rx_desc,
1976                                const struct rte_eth_hairpin_conf *conf)
1977 {
1978         int ret;
1979         struct rte_eth_dev *dev;
1980         struct rte_eth_hairpin_cap cap;
1981         void **rxq;
1982         int i;
1983         int count;
1984
1985         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1986
1987         dev = &rte_eth_devices[port_id];
1988         if (rx_queue_id >= dev->data->nb_rx_queues) {
1989                 RTE_ETHDEV_LOG(ERR, "Invalid RX queue_id=%u\n", rx_queue_id);
1990                 return -EINVAL;
1991         }
1992         ret = rte_eth_dev_hairpin_capability_get(port_id, &cap);
1993         if (ret != 0)
1994                 return ret;
1995         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_hairpin_queue_setup,
1996                                 -ENOTSUP);
1997         /* if nb_rx_desc is zero use max number of desc from the driver. */
1998         if (nb_rx_desc == 0)
1999                 nb_rx_desc = cap.max_nb_desc;
2000         if (nb_rx_desc > cap.max_nb_desc) {
2001                 RTE_ETHDEV_LOG(ERR,
2002                         "Invalid value for nb_rx_desc(=%hu), should be: <= %hu",
2003                         nb_rx_desc, cap.max_nb_desc);
2004                 return -EINVAL;
2005         }
2006         if (conf->peer_count > cap.max_rx_2_tx) {
2007                 RTE_ETHDEV_LOG(ERR,
2008                         "Invalid value for number of peers for Rx queue(=%u), should be: <= %hu",
2009                         conf->peer_count, cap.max_rx_2_tx);
2010                 return -EINVAL;
2011         }
2012         if (conf->peer_count == 0) {
2013                 RTE_ETHDEV_LOG(ERR,
2014                         "Invalid value for number of peers for Rx queue(=%u), should be: > 0",
2015                         conf->peer_count);
2016                 return -EINVAL;
2017         }
2018         for (i = 0, count = 0; i < dev->data->nb_rx_queues &&
2019              cap.max_nb_queues != UINT16_MAX; i++) {
2020                 if (i == rx_queue_id || rte_eth_dev_is_rx_hairpin_queue(dev, i))
2021                         count++;
2022         }
2023         if (count > cap.max_nb_queues) {
2024                 RTE_ETHDEV_LOG(ERR, "To many Rx hairpin queues max is %d",
2025                 cap.max_nb_queues);
2026                 return -EINVAL;
2027         }
2028         if (dev->data->dev_started)
2029                 return -EBUSY;
2030         rxq = dev->data->rx_queues;
2031         if (rxq[rx_queue_id] != NULL) {
2032                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_release,
2033                                         -ENOTSUP);
2034                 (*dev->dev_ops->rx_queue_release)(rxq[rx_queue_id]);
2035                 rxq[rx_queue_id] = NULL;
2036         }
2037         ret = (*dev->dev_ops->rx_hairpin_queue_setup)(dev, rx_queue_id,
2038                                                       nb_rx_desc, conf);
2039         if (ret == 0)
2040                 dev->data->rx_queue_state[rx_queue_id] =
2041                         RTE_ETH_QUEUE_STATE_HAIRPIN;
2042         return eth_err(port_id, ret);
2043 }
2044
2045 int
2046 rte_eth_tx_queue_setup(uint16_t port_id, uint16_t tx_queue_id,
2047                        uint16_t nb_tx_desc, unsigned int socket_id,
2048                        const struct rte_eth_txconf *tx_conf)
2049 {
2050         struct rte_eth_dev *dev;
2051         struct rte_eth_dev_info dev_info;
2052         struct rte_eth_txconf local_conf;
2053         void **txq;
2054         int ret;
2055
2056         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
2057
2058         dev = &rte_eth_devices[port_id];
2059         if (tx_queue_id >= dev->data->nb_tx_queues) {
2060                 RTE_ETHDEV_LOG(ERR, "Invalid TX queue_id=%u\n", tx_queue_id);
2061                 return -EINVAL;
2062         }
2063
2064         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_setup, -ENOTSUP);
2065
2066         ret = rte_eth_dev_info_get(port_id, &dev_info);
2067         if (ret != 0)
2068                 return ret;
2069
2070         /* Use default specified by driver, if nb_tx_desc is zero */
2071         if (nb_tx_desc == 0) {
2072                 nb_tx_desc = dev_info.default_txportconf.ring_size;
2073                 /* If driver default is zero, fall back on EAL default */
2074                 if (nb_tx_desc == 0)
2075                         nb_tx_desc = RTE_ETH_DEV_FALLBACK_TX_RINGSIZE;
2076         }
2077         if (nb_tx_desc > dev_info.tx_desc_lim.nb_max ||
2078             nb_tx_desc < dev_info.tx_desc_lim.nb_min ||
2079             nb_tx_desc % dev_info.tx_desc_lim.nb_align != 0) {
2080                 RTE_ETHDEV_LOG(ERR,
2081                         "Invalid value for nb_tx_desc(=%hu), should be: <= %hu, >= %hu, and a product of %hu\n",
2082                         nb_tx_desc, dev_info.tx_desc_lim.nb_max,
2083                         dev_info.tx_desc_lim.nb_min,
2084                         dev_info.tx_desc_lim.nb_align);
2085                 return -EINVAL;
2086         }
2087
2088         if (dev->data->dev_started &&
2089                 !(dev_info.dev_capa &
2090                         RTE_ETH_DEV_CAPA_RUNTIME_TX_QUEUE_SETUP))
2091                 return -EBUSY;
2092
2093         if (dev->data->dev_started &&
2094                 (dev->data->tx_queue_state[tx_queue_id] !=
2095                         RTE_ETH_QUEUE_STATE_STOPPED))
2096                 return -EBUSY;
2097
2098         txq = dev->data->tx_queues;
2099         if (txq[tx_queue_id]) {
2100                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_release,
2101                                         -ENOTSUP);
2102                 (*dev->dev_ops->tx_queue_release)(txq[tx_queue_id]);
2103                 txq[tx_queue_id] = NULL;
2104         }
2105
2106         if (tx_conf == NULL)
2107                 tx_conf = &dev_info.default_txconf;
2108
2109         local_conf = *tx_conf;
2110
2111         /*
2112          * If an offloading has already been enabled in
2113          * rte_eth_dev_configure(), it has been enabled on all queues,
2114          * so there is no need to enable it in this queue again.
2115          * The local_conf.offloads input to underlying PMD only carries
2116          * those offloadings which are only enabled on this queue and
2117          * not enabled on all queues.
2118          */
2119         local_conf.offloads &= ~dev->data->dev_conf.txmode.offloads;
2120
2121         /*
2122          * New added offloadings for this queue are those not enabled in
2123          * rte_eth_dev_configure() and they must be per-queue type.
2124          * A pure per-port offloading can't be enabled on a queue while
2125          * disabled on another queue. A pure per-port offloading can't
2126          * be enabled for any queue as new added one if it hasn't been
2127          * enabled in rte_eth_dev_configure().
2128          */
2129         if ((local_conf.offloads & dev_info.tx_queue_offload_capa) !=
2130              local_conf.offloads) {
2131                 RTE_ETHDEV_LOG(ERR,
2132                         "Ethdev port_id=%d tx_queue_id=%d, new added offloads 0x%"PRIx64" must be "
2133                         "within per-queue offload capabilities 0x%"PRIx64" in %s()\n",
2134                         port_id, tx_queue_id, local_conf.offloads,
2135                         dev_info.tx_queue_offload_capa,
2136                         __func__);
2137                 return -EINVAL;
2138         }
2139
2140         rte_ethdev_trace_txq_setup(port_id, tx_queue_id, nb_tx_desc, tx_conf);
2141         return eth_err(port_id, (*dev->dev_ops->tx_queue_setup)(dev,
2142                        tx_queue_id, nb_tx_desc, socket_id, &local_conf));
2143 }
2144
2145 int
2146 rte_eth_tx_hairpin_queue_setup(uint16_t port_id, uint16_t tx_queue_id,
2147                                uint16_t nb_tx_desc,
2148                                const struct rte_eth_hairpin_conf *conf)
2149 {
2150         struct rte_eth_dev *dev;
2151         struct rte_eth_hairpin_cap cap;
2152         void **txq;
2153         int i;
2154         int count;
2155         int ret;
2156
2157         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
2158         dev = &rte_eth_devices[port_id];
2159         if (tx_queue_id >= dev->data->nb_tx_queues) {
2160                 RTE_ETHDEV_LOG(ERR, "Invalid TX queue_id=%u\n", tx_queue_id);
2161                 return -EINVAL;
2162         }
2163         ret = rte_eth_dev_hairpin_capability_get(port_id, &cap);
2164         if (ret != 0)
2165                 return ret;
2166         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_hairpin_queue_setup,
2167                                 -ENOTSUP);
2168         /* if nb_rx_desc is zero use max number of desc from the driver. */
2169         if (nb_tx_desc == 0)
2170                 nb_tx_desc = cap.max_nb_desc;
2171         if (nb_tx_desc > cap.max_nb_desc) {
2172                 RTE_ETHDEV_LOG(ERR,
2173                         "Invalid value for nb_tx_desc(=%hu), should be: <= %hu",
2174                         nb_tx_desc, cap.max_nb_desc);
2175                 return -EINVAL;
2176         }
2177         if (conf->peer_count > cap.max_tx_2_rx) {
2178                 RTE_ETHDEV_LOG(ERR,
2179                         "Invalid value for number of peers for Tx queue(=%u), should be: <= %hu",
2180                         conf->peer_count, cap.max_tx_2_rx);
2181                 return -EINVAL;
2182         }
2183         if (conf->peer_count == 0) {
2184                 RTE_ETHDEV_LOG(ERR,
2185                         "Invalid value for number of peers for Tx queue(=%u), should be: > 0",
2186                         conf->peer_count);
2187                 return -EINVAL;
2188         }
2189         for (i = 0, count = 0; i < dev->data->nb_tx_queues &&
2190              cap.max_nb_queues != UINT16_MAX; i++) {
2191                 if (i == tx_queue_id || rte_eth_dev_is_tx_hairpin_queue(dev, i))
2192                         count++;
2193         }
2194         if (count > cap.max_nb_queues) {
2195                 RTE_ETHDEV_LOG(ERR, "To many Tx hairpin queues max is %d",
2196                 cap.max_nb_queues);
2197                 return -EINVAL;
2198         }
2199         if (dev->data->dev_started)
2200                 return -EBUSY;
2201         txq = dev->data->tx_queues;
2202         if (txq[tx_queue_id] != NULL) {
2203                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_release,
2204                                         -ENOTSUP);
2205                 (*dev->dev_ops->tx_queue_release)(txq[tx_queue_id]);
2206                 txq[tx_queue_id] = NULL;
2207         }
2208         ret = (*dev->dev_ops->tx_hairpin_queue_setup)
2209                 (dev, tx_queue_id, nb_tx_desc, conf);
2210         if (ret == 0)
2211                 dev->data->tx_queue_state[tx_queue_id] =
2212                         RTE_ETH_QUEUE_STATE_HAIRPIN;
2213         return eth_err(port_id, ret);
2214 }
2215
2216 int
2217 rte_eth_hairpin_bind(uint16_t tx_port, uint16_t rx_port)
2218 {
2219         struct rte_eth_dev *dev;
2220         int ret;
2221
2222         RTE_ETH_VALID_PORTID_OR_ERR_RET(tx_port, -ENODEV);
2223         dev = &rte_eth_devices[tx_port];
2224         if (dev->data->dev_started == 0) {
2225                 RTE_ETHDEV_LOG(ERR, "Tx port %d is not started\n", tx_port);
2226                 return -EBUSY;
2227         }
2228
2229         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->hairpin_bind, -ENOTSUP);
2230         ret = (*dev->dev_ops->hairpin_bind)(dev, rx_port);
2231         if (ret != 0)
2232                 RTE_ETHDEV_LOG(ERR, "Failed to bind hairpin Tx %d"
2233                                " to Rx %d (%d - all ports)\n",
2234                                tx_port, rx_port, RTE_MAX_ETHPORTS);
2235
2236         return ret;
2237 }
2238
2239 int
2240 rte_eth_hairpin_unbind(uint16_t tx_port, uint16_t rx_port)
2241 {
2242         struct rte_eth_dev *dev;
2243         int ret;
2244
2245         RTE_ETH_VALID_PORTID_OR_ERR_RET(tx_port, -ENODEV);
2246         dev = &rte_eth_devices[tx_port];
2247         if (dev->data->dev_started == 0) {
2248                 RTE_ETHDEV_LOG(ERR, "Tx port %d is already stopped\n", tx_port);
2249                 return -EBUSY;
2250         }
2251
2252         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->hairpin_unbind, -ENOTSUP);
2253         ret = (*dev->dev_ops->hairpin_unbind)(dev, rx_port);
2254         if (ret != 0)
2255                 RTE_ETHDEV_LOG(ERR, "Failed to unbind hairpin Tx %d"
2256                                " from Rx %d (%d - all ports)\n",
2257                                tx_port, rx_port, RTE_MAX_ETHPORTS);
2258
2259         return ret;
2260 }
2261
2262 int
2263 rte_eth_hairpin_get_peer_ports(uint16_t port_id, uint16_t *peer_ports,
2264                                size_t len, uint32_t direction)
2265 {
2266         struct rte_eth_dev *dev;
2267         int ret;
2268
2269         if (peer_ports == NULL || len == 0)
2270                 return -EINVAL;
2271
2272         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2273         dev = &rte_eth_devices[port_id];
2274         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->hairpin_get_peer_ports,
2275                                 -ENOTSUP);
2276
2277         ret = (*dev->dev_ops->hairpin_get_peer_ports)(dev, peer_ports,
2278                                                       len, direction);
2279         if (ret < 0)
2280                 RTE_ETHDEV_LOG(ERR, "Failed to get %d hairpin peer %s ports\n",
2281                                port_id, direction ? "Rx" : "Tx");
2282
2283         return ret;
2284 }
2285
2286 void
2287 rte_eth_tx_buffer_drop_callback(struct rte_mbuf **pkts, uint16_t unsent,
2288                 void *userdata __rte_unused)
2289 {
2290         rte_pktmbuf_free_bulk(pkts, unsent);
2291 }
2292
2293 void
2294 rte_eth_tx_buffer_count_callback(struct rte_mbuf **pkts, uint16_t unsent,
2295                 void *userdata)
2296 {
2297         uint64_t *count = userdata;
2298
2299         rte_pktmbuf_free_bulk(pkts, unsent);
2300         *count += unsent;
2301 }
2302
2303 int
2304 rte_eth_tx_buffer_set_err_callback(struct rte_eth_dev_tx_buffer *buffer,
2305                 buffer_tx_error_fn cbfn, void *userdata)
2306 {
2307         buffer->error_callback = cbfn;
2308         buffer->error_userdata = userdata;
2309         return 0;
2310 }
2311
2312 int
2313 rte_eth_tx_buffer_init(struct rte_eth_dev_tx_buffer *buffer, uint16_t size)
2314 {
2315         int ret = 0;
2316
2317         if (buffer == NULL)
2318                 return -EINVAL;
2319
2320         buffer->size = size;
2321         if (buffer->error_callback == NULL) {
2322                 ret = rte_eth_tx_buffer_set_err_callback(
2323                         buffer, rte_eth_tx_buffer_drop_callback, NULL);
2324         }
2325
2326         return ret;
2327 }
2328
2329 int
2330 rte_eth_tx_done_cleanup(uint16_t port_id, uint16_t queue_id, uint32_t free_cnt)
2331 {
2332         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
2333         int ret;
2334
2335         /* Validate Input Data. Bail if not valid or not supported. */
2336         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2337         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_done_cleanup, -ENOTSUP);
2338
2339         /* Call driver to free pending mbufs. */
2340         ret = (*dev->dev_ops->tx_done_cleanup)(dev->data->tx_queues[queue_id],
2341                                                free_cnt);
2342         return eth_err(port_id, ret);
2343 }
2344
2345 int
2346 rte_eth_promiscuous_enable(uint16_t port_id)
2347 {
2348         struct rte_eth_dev *dev;
2349         int diag = 0;
2350
2351         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2352         dev = &rte_eth_devices[port_id];
2353
2354         if (dev->data->promiscuous == 1)
2355                 return 0;
2356
2357         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->promiscuous_enable, -ENOTSUP);
2358
2359         diag = (*dev->dev_ops->promiscuous_enable)(dev);
2360         dev->data->promiscuous = (diag == 0) ? 1 : 0;
2361
2362         return eth_err(port_id, diag);
2363 }
2364
2365 int
2366 rte_eth_promiscuous_disable(uint16_t port_id)
2367 {
2368         struct rte_eth_dev *dev;
2369         int diag = 0;
2370
2371         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2372         dev = &rte_eth_devices[port_id];
2373
2374         if (dev->data->promiscuous == 0)
2375                 return 0;
2376
2377         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->promiscuous_disable, -ENOTSUP);
2378
2379         dev->data->promiscuous = 0;
2380         diag = (*dev->dev_ops->promiscuous_disable)(dev);
2381         if (diag != 0)
2382                 dev->data->promiscuous = 1;
2383
2384         return eth_err(port_id, diag);
2385 }
2386
2387 int
2388 rte_eth_promiscuous_get(uint16_t port_id)
2389 {
2390         struct rte_eth_dev *dev;
2391
2392         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
2393
2394         dev = &rte_eth_devices[port_id];
2395         return dev->data->promiscuous;
2396 }
2397
2398 int
2399 rte_eth_allmulticast_enable(uint16_t port_id)
2400 {
2401         struct rte_eth_dev *dev;
2402         int diag;
2403
2404         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2405         dev = &rte_eth_devices[port_id];
2406
2407         if (dev->data->all_multicast == 1)
2408                 return 0;
2409
2410         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->allmulticast_enable, -ENOTSUP);
2411         diag = (*dev->dev_ops->allmulticast_enable)(dev);
2412         dev->data->all_multicast = (diag == 0) ? 1 : 0;
2413
2414         return eth_err(port_id, diag);
2415 }
2416
2417 int
2418 rte_eth_allmulticast_disable(uint16_t port_id)
2419 {
2420         struct rte_eth_dev *dev;
2421         int diag;
2422
2423         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2424         dev = &rte_eth_devices[port_id];
2425
2426         if (dev->data->all_multicast == 0)
2427                 return 0;
2428
2429         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->allmulticast_disable, -ENOTSUP);
2430         dev->data->all_multicast = 0;
2431         diag = (*dev->dev_ops->allmulticast_disable)(dev);
2432         if (diag != 0)
2433                 dev->data->all_multicast = 1;
2434
2435         return eth_err(port_id, diag);
2436 }
2437
2438 int
2439 rte_eth_allmulticast_get(uint16_t port_id)
2440 {
2441         struct rte_eth_dev *dev;
2442
2443         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
2444
2445         dev = &rte_eth_devices[port_id];
2446         return dev->data->all_multicast;
2447 }
2448
2449 int
2450 rte_eth_link_get(uint16_t port_id, struct rte_eth_link *eth_link)
2451 {
2452         struct rte_eth_dev *dev;
2453
2454         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2455         dev = &rte_eth_devices[port_id];
2456
2457         if (dev->data->dev_conf.intr_conf.lsc &&
2458             dev->data->dev_started)
2459                 rte_eth_linkstatus_get(dev, eth_link);
2460         else {
2461                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->link_update, -ENOTSUP);
2462                 (*dev->dev_ops->link_update)(dev, 1);
2463                 *eth_link = dev->data->dev_link;
2464         }
2465
2466         return 0;
2467 }
2468
2469 int
2470 rte_eth_link_get_nowait(uint16_t port_id, struct rte_eth_link *eth_link)
2471 {
2472         struct rte_eth_dev *dev;
2473
2474         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2475         dev = &rte_eth_devices[port_id];
2476
2477         if (dev->data->dev_conf.intr_conf.lsc &&
2478             dev->data->dev_started)
2479                 rte_eth_linkstatus_get(dev, eth_link);
2480         else {
2481                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->link_update, -ENOTSUP);
2482                 (*dev->dev_ops->link_update)(dev, 0);
2483                 *eth_link = dev->data->dev_link;
2484         }
2485
2486         return 0;
2487 }
2488
2489 const char *
2490 rte_eth_link_speed_to_str(uint32_t link_speed)
2491 {
2492         switch (link_speed) {
2493         case ETH_SPEED_NUM_NONE: return "None";
2494         case ETH_SPEED_NUM_10M:  return "10 Mbps";
2495         case ETH_SPEED_NUM_100M: return "100 Mbps";
2496         case ETH_SPEED_NUM_1G:   return "1 Gbps";
2497         case ETH_SPEED_NUM_2_5G: return "2.5 Gbps";
2498         case ETH_SPEED_NUM_5G:   return "5 Gbps";
2499         case ETH_SPEED_NUM_10G:  return "10 Gbps";
2500         case ETH_SPEED_NUM_20G:  return "20 Gbps";
2501         case ETH_SPEED_NUM_25G:  return "25 Gbps";
2502         case ETH_SPEED_NUM_40G:  return "40 Gbps";
2503         case ETH_SPEED_NUM_50G:  return "50 Gbps";
2504         case ETH_SPEED_NUM_56G:  return "56 Gbps";
2505         case ETH_SPEED_NUM_100G: return "100 Gbps";
2506         case ETH_SPEED_NUM_200G: return "200 Gbps";
2507         case ETH_SPEED_NUM_UNKNOWN: return "Unknown";
2508         default: return "Invalid";
2509         }
2510 }
2511
2512 int
2513 rte_eth_link_to_str(char *str, size_t len, const struct rte_eth_link *eth_link)
2514 {
2515         if (eth_link->link_status == ETH_LINK_DOWN)
2516                 return snprintf(str, len, "Link down");
2517         else
2518                 return snprintf(str, len, "Link up at %s %s %s",
2519                         rte_eth_link_speed_to_str(eth_link->link_speed),
2520                         (eth_link->link_duplex == ETH_LINK_FULL_DUPLEX) ?
2521                         "FDX" : "HDX",
2522                         (eth_link->link_autoneg == ETH_LINK_AUTONEG) ?
2523                         "Autoneg" : "Fixed");
2524 }
2525
2526 int
2527 rte_eth_stats_get(uint16_t port_id, struct rte_eth_stats *stats)
2528 {
2529         struct rte_eth_dev *dev;
2530
2531         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
2532
2533         dev = &rte_eth_devices[port_id];
2534         memset(stats, 0, sizeof(*stats));
2535
2536         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
2537         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
2538         return eth_err(port_id, (*dev->dev_ops->stats_get)(dev, stats));
2539 }
2540
2541 int
2542 rte_eth_stats_reset(uint16_t port_id)
2543 {
2544         struct rte_eth_dev *dev;
2545         int ret;
2546
2547         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2548         dev = &rte_eth_devices[port_id];
2549
2550         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_reset, -ENOTSUP);
2551         ret = (*dev->dev_ops->stats_reset)(dev);
2552         if (ret != 0)
2553                 return eth_err(port_id, ret);
2554
2555         dev->data->rx_mbuf_alloc_failed = 0;
2556
2557         return 0;
2558 }
2559
2560 static inline int
2561 get_xstats_basic_count(struct rte_eth_dev *dev)
2562 {
2563         uint16_t nb_rxqs, nb_txqs;
2564         int count;
2565
2566         nb_rxqs = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
2567         nb_txqs = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
2568
2569         count = RTE_NB_STATS;
2570         count += nb_rxqs * RTE_NB_RXQ_STATS;
2571         count += nb_txqs * RTE_NB_TXQ_STATS;
2572
2573         return count;
2574 }
2575
2576 static int
2577 get_xstats_count(uint16_t port_id)
2578 {
2579         struct rte_eth_dev *dev;
2580         int count;
2581
2582         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
2583         dev = &rte_eth_devices[port_id];
2584         if (dev->dev_ops->xstats_get_names_by_id != NULL) {
2585                 count = (*dev->dev_ops->xstats_get_names_by_id)(dev, NULL,
2586                                 NULL, 0);
2587                 if (count < 0)
2588                         return eth_err(port_id, count);
2589         }
2590         if (dev->dev_ops->xstats_get_names != NULL) {
2591                 count = (*dev->dev_ops->xstats_get_names)(dev, NULL, 0);
2592                 if (count < 0)
2593                         return eth_err(port_id, count);
2594         } else
2595                 count = 0;
2596
2597
2598         count += get_xstats_basic_count(dev);
2599
2600         return count;
2601 }
2602
2603 int
2604 rte_eth_xstats_get_id_by_name(uint16_t port_id, const char *xstat_name,
2605                 uint64_t *id)
2606 {
2607         int cnt_xstats, idx_xstat;
2608
2609         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2610
2611         if (!id) {
2612                 RTE_ETHDEV_LOG(ERR, "Id pointer is NULL\n");
2613                 return -ENOMEM;
2614         }
2615
2616         if (!xstat_name) {
2617                 RTE_ETHDEV_LOG(ERR, "xstat_name pointer is NULL\n");
2618                 return -ENOMEM;
2619         }
2620
2621         /* Get count */
2622         cnt_xstats = rte_eth_xstats_get_names_by_id(port_id, NULL, 0, NULL);
2623         if (cnt_xstats  < 0) {
2624                 RTE_ETHDEV_LOG(ERR, "Cannot get count of xstats\n");
2625                 return -ENODEV;
2626         }
2627
2628         /* Get id-name lookup table */
2629         struct rte_eth_xstat_name xstats_names[cnt_xstats];
2630
2631         if (cnt_xstats != rte_eth_xstats_get_names_by_id(
2632                         port_id, xstats_names, cnt_xstats, NULL)) {
2633                 RTE_ETHDEV_LOG(ERR, "Cannot get xstats lookup\n");
2634                 return -1;
2635         }
2636
2637         for (idx_xstat = 0; idx_xstat < cnt_xstats; idx_xstat++) {
2638                 if (!strcmp(xstats_names[idx_xstat].name, xstat_name)) {
2639                         *id = idx_xstat;
2640                         return 0;
2641                 };
2642         }
2643
2644         return -EINVAL;
2645 }
2646
2647 /* retrieve basic stats names */
2648 static int
2649 rte_eth_basic_stats_get_names(struct rte_eth_dev *dev,
2650         struct rte_eth_xstat_name *xstats_names)
2651 {
2652         int cnt_used_entries = 0;
2653         uint32_t idx, id_queue;
2654         uint16_t num_q;
2655
2656         for (idx = 0; idx < RTE_NB_STATS; idx++) {
2657                 strlcpy(xstats_names[cnt_used_entries].name,
2658                         rte_stats_strings[idx].name,
2659                         sizeof(xstats_names[0].name));
2660                 cnt_used_entries++;
2661         }
2662         num_q = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
2663         for (id_queue = 0; id_queue < num_q; id_queue++) {
2664                 for (idx = 0; idx < RTE_NB_RXQ_STATS; idx++) {
2665                         snprintf(xstats_names[cnt_used_entries].name,
2666                                 sizeof(xstats_names[0].name),
2667                                 "rx_q%u_%s",
2668                                 id_queue, rte_rxq_stats_strings[idx].name);
2669                         cnt_used_entries++;
2670                 }
2671
2672         }
2673         num_q = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
2674         for (id_queue = 0; id_queue < num_q; id_queue++) {
2675                 for (idx = 0; idx < RTE_NB_TXQ_STATS; idx++) {
2676                         snprintf(xstats_names[cnt_used_entries].name,
2677                                 sizeof(xstats_names[0].name),
2678                                 "tx_q%u_%s",
2679                                 id_queue, rte_txq_stats_strings[idx].name);
2680                         cnt_used_entries++;
2681                 }
2682         }
2683         return cnt_used_entries;
2684 }
2685
2686 /* retrieve ethdev extended statistics names */
2687 int
2688 rte_eth_xstats_get_names_by_id(uint16_t port_id,
2689         struct rte_eth_xstat_name *xstats_names, unsigned int size,
2690         uint64_t *ids)
2691 {
2692         struct rte_eth_xstat_name *xstats_names_copy;
2693         unsigned int no_basic_stat_requested = 1;
2694         unsigned int no_ext_stat_requested = 1;
2695         unsigned int expected_entries;
2696         unsigned int basic_count;
2697         struct rte_eth_dev *dev;
2698         unsigned int i;
2699         int ret;
2700
2701         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2702         dev = &rte_eth_devices[port_id];
2703
2704         basic_count = get_xstats_basic_count(dev);
2705         ret = get_xstats_count(port_id);
2706         if (ret < 0)
2707                 return ret;
2708         expected_entries = (unsigned int)ret;
2709
2710         /* Return max number of stats if no ids given */
2711         if (!ids) {
2712                 if (!xstats_names)
2713                         return expected_entries;
2714                 else if (xstats_names && size < expected_entries)
2715                         return expected_entries;
2716         }
2717
2718         if (ids && !xstats_names)
2719                 return -EINVAL;
2720
2721         if (ids && dev->dev_ops->xstats_get_names_by_id != NULL && size > 0) {
2722                 uint64_t ids_copy[size];
2723
2724                 for (i = 0; i < size; i++) {
2725                         if (ids[i] < basic_count) {
2726                                 no_basic_stat_requested = 0;
2727                                 break;
2728                         }
2729
2730                         /*
2731                          * Convert ids to xstats ids that PMD knows.
2732                          * ids known by user are basic + extended stats.
2733                          */
2734                         ids_copy[i] = ids[i] - basic_count;
2735                 }
2736
2737                 if (no_basic_stat_requested)
2738                         return (*dev->dev_ops->xstats_get_names_by_id)(dev,
2739                                         xstats_names, ids_copy, size);
2740         }
2741
2742         /* Retrieve all stats */
2743         if (!ids) {
2744                 int num_stats = rte_eth_xstats_get_names(port_id, xstats_names,
2745                                 expected_entries);
2746                 if (num_stats < 0 || num_stats > (int)expected_entries)
2747                         return num_stats;
2748                 else
2749                         return expected_entries;
2750         }
2751
2752         xstats_names_copy = calloc(expected_entries,
2753                 sizeof(struct rte_eth_xstat_name));
2754
2755         if (!xstats_names_copy) {
2756                 RTE_ETHDEV_LOG(ERR, "Can't allocate memory\n");
2757                 return -ENOMEM;
2758         }
2759
2760         if (ids) {
2761                 for (i = 0; i < size; i++) {
2762                         if (ids[i] >= basic_count) {
2763                                 no_ext_stat_requested = 0;
2764                                 break;
2765                         }
2766                 }
2767         }
2768
2769         /* Fill xstats_names_copy structure */
2770         if (ids && no_ext_stat_requested) {
2771                 rte_eth_basic_stats_get_names(dev, xstats_names_copy);
2772         } else {
2773                 ret = rte_eth_xstats_get_names(port_id, xstats_names_copy,
2774                         expected_entries);
2775                 if (ret < 0) {
2776                         free(xstats_names_copy);
2777                         return ret;
2778                 }
2779         }
2780
2781         /* Filter stats */
2782         for (i = 0; i < size; i++) {
2783                 if (ids[i] >= expected_entries) {
2784                         RTE_ETHDEV_LOG(ERR, "Id value isn't valid\n");
2785                         free(xstats_names_copy);
2786                         return -1;
2787                 }
2788                 xstats_names[i] = xstats_names_copy[ids[i]];
2789         }
2790
2791         free(xstats_names_copy);
2792         return size;
2793 }
2794
2795 int
2796 rte_eth_xstats_get_names(uint16_t port_id,
2797         struct rte_eth_xstat_name *xstats_names,
2798         unsigned int size)
2799 {
2800         struct rte_eth_dev *dev;
2801         int cnt_used_entries;
2802         int cnt_expected_entries;
2803         int cnt_driver_entries;
2804
2805         cnt_expected_entries = get_xstats_count(port_id);
2806         if (xstats_names == NULL || cnt_expected_entries < 0 ||
2807                         (int)size < cnt_expected_entries)
2808                 return cnt_expected_entries;
2809
2810         /* port_id checked in get_xstats_count() */
2811         dev = &rte_eth_devices[port_id];
2812
2813         cnt_used_entries = rte_eth_basic_stats_get_names(
2814                 dev, xstats_names);
2815
2816         if (dev->dev_ops->xstats_get_names != NULL) {
2817                 /* If there are any driver-specific xstats, append them
2818                  * to end of list.
2819                  */
2820                 cnt_driver_entries = (*dev->dev_ops->xstats_get_names)(
2821                         dev,
2822                         xstats_names + cnt_used_entries,
2823                         size - cnt_used_entries);
2824                 if (cnt_driver_entries < 0)
2825                         return eth_err(port_id, cnt_driver_entries);
2826                 cnt_used_entries += cnt_driver_entries;
2827         }
2828
2829         return cnt_used_entries;
2830 }
2831
2832
2833 static int
2834 rte_eth_basic_stats_get(uint16_t port_id, struct rte_eth_xstat *xstats)
2835 {
2836         struct rte_eth_dev *dev;
2837         struct rte_eth_stats eth_stats;
2838         unsigned int count = 0, i, q;
2839         uint64_t val, *stats_ptr;
2840         uint16_t nb_rxqs, nb_txqs;
2841         int ret;
2842
2843         ret = rte_eth_stats_get(port_id, &eth_stats);
2844         if (ret < 0)
2845                 return ret;
2846
2847         dev = &rte_eth_devices[port_id];
2848
2849         nb_rxqs = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
2850         nb_txqs = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
2851
2852         /* global stats */
2853         for (i = 0; i < RTE_NB_STATS; i++) {
2854                 stats_ptr = RTE_PTR_ADD(&eth_stats,
2855                                         rte_stats_strings[i].offset);
2856                 val = *stats_ptr;
2857                 xstats[count++].value = val;
2858         }
2859
2860         /* per-rxq stats */
2861         for (q = 0; q < nb_rxqs; q++) {
2862                 for (i = 0; i < RTE_NB_RXQ_STATS; i++) {
2863                         stats_ptr = RTE_PTR_ADD(&eth_stats,
2864                                         rte_rxq_stats_strings[i].offset +
2865                                         q * sizeof(uint64_t));
2866                         val = *stats_ptr;
2867                         xstats[count++].value = val;
2868                 }
2869         }
2870
2871         /* per-txq stats */
2872         for (q = 0; q < nb_txqs; q++) {
2873                 for (i = 0; i < RTE_NB_TXQ_STATS; i++) {
2874                         stats_ptr = RTE_PTR_ADD(&eth_stats,
2875                                         rte_txq_stats_strings[i].offset +
2876                                         q * sizeof(uint64_t));
2877                         val = *stats_ptr;
2878                         xstats[count++].value = val;
2879                 }
2880         }
2881         return count;
2882 }
2883
2884 /* retrieve ethdev extended statistics */
2885 int
2886 rte_eth_xstats_get_by_id(uint16_t port_id, const uint64_t *ids,
2887                          uint64_t *values, unsigned int size)
2888 {
2889         unsigned int no_basic_stat_requested = 1;
2890         unsigned int no_ext_stat_requested = 1;
2891         unsigned int num_xstats_filled;
2892         unsigned int basic_count;
2893         uint16_t expected_entries;
2894         struct rte_eth_dev *dev;
2895         unsigned int i;
2896         int ret;
2897
2898         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2899         ret = get_xstats_count(port_id);
2900         if (ret < 0)
2901                 return ret;
2902         expected_entries = (uint16_t)ret;
2903         struct rte_eth_xstat xstats[expected_entries];
2904         dev = &rte_eth_devices[port_id];
2905         basic_count = get_xstats_basic_count(dev);
2906
2907         /* Return max number of stats if no ids given */
2908         if (!ids) {
2909                 if (!values)
2910                         return expected_entries;
2911                 else if (values && size < expected_entries)
2912                         return expected_entries;
2913         }
2914
2915         if (ids && !values)
2916                 return -EINVAL;
2917
2918         if (ids && dev->dev_ops->xstats_get_by_id != NULL && size) {
2919                 unsigned int basic_count = get_xstats_basic_count(dev);
2920                 uint64_t ids_copy[size];
2921
2922                 for (i = 0; i < size; i++) {
2923                         if (ids[i] < basic_count) {
2924                                 no_basic_stat_requested = 0;
2925                                 break;
2926                         }
2927
2928                         /*
2929                          * Convert ids to xstats ids that PMD knows.
2930                          * ids known by user are basic + extended stats.
2931                          */
2932                         ids_copy[i] = ids[i] - basic_count;
2933                 }
2934
2935                 if (no_basic_stat_requested)
2936                         return (*dev->dev_ops->xstats_get_by_id)(dev, ids_copy,
2937                                         values, size);
2938         }
2939
2940         if (ids) {
2941                 for (i = 0; i < size; i++) {
2942                         if (ids[i] >= basic_count) {
2943                                 no_ext_stat_requested = 0;
2944                                 break;
2945                         }
2946                 }
2947         }
2948
2949         /* Fill the xstats structure */
2950         if (ids && no_ext_stat_requested)
2951                 ret = rte_eth_basic_stats_get(port_id, xstats);
2952         else
2953                 ret = rte_eth_xstats_get(port_id, xstats, expected_entries);
2954
2955         if (ret < 0)
2956                 return ret;
2957         num_xstats_filled = (unsigned int)ret;
2958
2959         /* Return all stats */
2960         if (!ids) {
2961                 for (i = 0; i < num_xstats_filled; i++)
2962                         values[i] = xstats[i].value;
2963                 return expected_entries;
2964         }
2965
2966         /* Filter stats */
2967         for (i = 0; i < size; i++) {
2968                 if (ids[i] >= expected_entries) {
2969                         RTE_ETHDEV_LOG(ERR, "Id value isn't valid\n");
2970                         return -1;
2971                 }
2972                 values[i] = xstats[ids[i]].value;
2973         }
2974         return size;
2975 }
2976
2977 int
2978 rte_eth_xstats_get(uint16_t port_id, struct rte_eth_xstat *xstats,
2979         unsigned int n)
2980 {
2981         struct rte_eth_dev *dev;
2982         unsigned int count = 0, i;
2983         signed int xcount = 0;
2984         uint16_t nb_rxqs, nb_txqs;
2985         int ret;
2986
2987         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
2988
2989         dev = &rte_eth_devices[port_id];
2990
2991         nb_rxqs = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
2992         nb_txqs = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
2993
2994         /* Return generic statistics */
2995         count = RTE_NB_STATS + (nb_rxqs * RTE_NB_RXQ_STATS) +
2996                 (nb_txqs * RTE_NB_TXQ_STATS);
2997
2998         /* implemented by the driver */
2999         if (dev->dev_ops->xstats_get != NULL) {
3000                 /* Retrieve the xstats from the driver at the end of the
3001                  * xstats struct.
3002                  */
3003                 xcount = (*dev->dev_ops->xstats_get)(dev,
3004                                      xstats ? xstats + count : NULL,
3005                                      (n > count) ? n - count : 0);
3006
3007                 if (xcount < 0)
3008                         return eth_err(port_id, xcount);
3009         }
3010
3011         if (n < count + xcount || xstats == NULL)
3012                 return count + xcount;
3013
3014         /* now fill the xstats structure */
3015         ret = rte_eth_basic_stats_get(port_id, xstats);
3016         if (ret < 0)
3017                 return ret;
3018         count = ret;
3019
3020         for (i = 0; i < count; i++)
3021                 xstats[i].id = i;
3022         /* add an offset to driver-specific stats */
3023         for ( ; i < count + xcount; i++)
3024                 xstats[i].id += count;
3025
3026         return count + xcount;
3027 }
3028
3029 /* reset ethdev extended statistics */
3030 int
3031 rte_eth_xstats_reset(uint16_t port_id)
3032 {
3033         struct rte_eth_dev *dev;
3034
3035         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3036         dev = &rte_eth_devices[port_id];
3037
3038         /* implemented by the driver */
3039         if (dev->dev_ops->xstats_reset != NULL)
3040                 return eth_err(port_id, (*dev->dev_ops->xstats_reset)(dev));
3041
3042         /* fallback to default */
3043         return rte_eth_stats_reset(port_id);
3044 }
3045
3046 static int
3047 set_queue_stats_mapping(uint16_t port_id, uint16_t queue_id, uint8_t stat_idx,
3048                 uint8_t is_rx)
3049 {
3050         struct rte_eth_dev *dev;
3051
3052         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3053
3054         dev = &rte_eth_devices[port_id];
3055
3056         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_stats_mapping_set, -ENOTSUP);
3057
3058         if (is_rx && (queue_id >= dev->data->nb_rx_queues))
3059                 return -EINVAL;
3060
3061         if (!is_rx && (queue_id >= dev->data->nb_tx_queues))
3062                 return -EINVAL;
3063
3064         if (stat_idx >= RTE_ETHDEV_QUEUE_STAT_CNTRS)
3065                 return -EINVAL;
3066
3067         return (*dev->dev_ops->queue_stats_mapping_set)
3068                         (dev, queue_id, stat_idx, is_rx);
3069 }
3070
3071
3072 int
3073 rte_eth_dev_set_tx_queue_stats_mapping(uint16_t port_id, uint16_t tx_queue_id,
3074                 uint8_t stat_idx)
3075 {
3076         return eth_err(port_id, set_queue_stats_mapping(port_id, tx_queue_id,
3077                                                 stat_idx, STAT_QMAP_TX));
3078 }
3079
3080
3081 int
3082 rte_eth_dev_set_rx_queue_stats_mapping(uint16_t port_id, uint16_t rx_queue_id,
3083                 uint8_t stat_idx)
3084 {
3085         return eth_err(port_id, set_queue_stats_mapping(port_id, rx_queue_id,
3086                                                 stat_idx, STAT_QMAP_RX));
3087 }
3088
3089 int
3090 rte_eth_dev_fw_version_get(uint16_t port_id, char *fw_version, size_t fw_size)
3091 {
3092         struct rte_eth_dev *dev;
3093
3094         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3095         dev = &rte_eth_devices[port_id];
3096
3097         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fw_version_get, -ENOTSUP);
3098         return eth_err(port_id, (*dev->dev_ops->fw_version_get)(dev,
3099                                                         fw_version, fw_size));
3100 }
3101
3102 int
3103 rte_eth_dev_info_get(uint16_t port_id, struct rte_eth_dev_info *dev_info)
3104 {
3105         struct rte_eth_dev *dev;
3106         const struct rte_eth_desc_lim lim = {
3107                 .nb_max = UINT16_MAX,
3108                 .nb_min = 0,
3109                 .nb_align = 1,
3110                 .nb_seg_max = UINT16_MAX,
3111                 .nb_mtu_seg_max = UINT16_MAX,
3112         };
3113         int diag;
3114
3115         /*
3116          * Init dev_info before port_id check since caller does not have
3117          * return status and does not know if get is successful or not.
3118          */
3119         memset(dev_info, 0, sizeof(struct rte_eth_dev_info));
3120         dev_info->switch_info.domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
3121
3122         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3123         dev = &rte_eth_devices[port_id];
3124
3125         dev_info->rx_desc_lim = lim;
3126         dev_info->tx_desc_lim = lim;
3127         dev_info->device = dev->device;
3128         dev_info->min_mtu = RTE_ETHER_MIN_MTU;
3129         dev_info->max_mtu = UINT16_MAX;
3130
3131         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
3132         diag = (*dev->dev_ops->dev_infos_get)(dev, dev_info);
3133         if (diag != 0) {
3134                 /* Cleanup already filled in device information */
3135                 memset(dev_info, 0, sizeof(struct rte_eth_dev_info));
3136                 return eth_err(port_id, diag);
3137         }
3138
3139         /* Maximum number of queues should be <= RTE_MAX_QUEUES_PER_PORT */
3140         dev_info->max_rx_queues = RTE_MIN(dev_info->max_rx_queues,
3141                         RTE_MAX_QUEUES_PER_PORT);
3142         dev_info->max_tx_queues = RTE_MIN(dev_info->max_tx_queues,
3143                         RTE_MAX_QUEUES_PER_PORT);
3144
3145         dev_info->driver_name = dev->device->driver->name;
3146         dev_info->nb_rx_queues = dev->data->nb_rx_queues;
3147         dev_info->nb_tx_queues = dev->data->nb_tx_queues;
3148
3149         dev_info->dev_flags = &dev->data->dev_flags;
3150
3151         return 0;
3152 }
3153
3154 int
3155 rte_eth_dev_get_supported_ptypes(uint16_t port_id, uint32_t ptype_mask,
3156                                  uint32_t *ptypes, int num)
3157 {
3158         int i, j;
3159         struct rte_eth_dev *dev;
3160         const uint32_t *all_ptypes;
3161
3162         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3163         dev = &rte_eth_devices[port_id];
3164         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_supported_ptypes_get, 0);
3165         all_ptypes = (*dev->dev_ops->dev_supported_ptypes_get)(dev);
3166
3167         if (!all_ptypes)
3168                 return 0;
3169
3170         for (i = 0, j = 0; all_ptypes[i] != RTE_PTYPE_UNKNOWN; ++i)
3171                 if (all_ptypes[i] & ptype_mask) {
3172                         if (j < num)
3173                                 ptypes[j] = all_ptypes[i];
3174                         j++;
3175                 }
3176
3177         return j;
3178 }
3179
3180 int
3181 rte_eth_dev_set_ptypes(uint16_t port_id, uint32_t ptype_mask,
3182                                  uint32_t *set_ptypes, unsigned int num)
3183 {
3184         const uint32_t valid_ptype_masks[] = {
3185                 RTE_PTYPE_L2_MASK,
3186                 RTE_PTYPE_L3_MASK,
3187                 RTE_PTYPE_L4_MASK,
3188                 RTE_PTYPE_TUNNEL_MASK,
3189                 RTE_PTYPE_INNER_L2_MASK,
3190                 RTE_PTYPE_INNER_L3_MASK,
3191                 RTE_PTYPE_INNER_L4_MASK,
3192         };
3193         const uint32_t *all_ptypes;
3194         struct rte_eth_dev *dev;
3195         uint32_t unused_mask;
3196         unsigned int i, j;
3197         int ret;
3198
3199         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3200         dev = &rte_eth_devices[port_id];
3201
3202         if (num > 0 && set_ptypes == NULL)
3203                 return -EINVAL;
3204
3205         if (*dev->dev_ops->dev_supported_ptypes_get == NULL ||
3206                         *dev->dev_ops->dev_ptypes_set == NULL) {
3207                 ret = 0;
3208                 goto ptype_unknown;
3209         }
3210
3211         if (ptype_mask == 0) {
3212                 ret = (*dev->dev_ops->dev_ptypes_set)(dev,
3213                                 ptype_mask);
3214                 goto ptype_unknown;
3215         }
3216
3217         unused_mask = ptype_mask;
3218         for (i = 0; i < RTE_DIM(valid_ptype_masks); i++) {
3219                 uint32_t mask = ptype_mask & valid_ptype_masks[i];
3220                 if (mask && mask != valid_ptype_masks[i]) {
3221                         ret = -EINVAL;
3222                         goto ptype_unknown;
3223                 }
3224                 unused_mask &= ~valid_ptype_masks[i];
3225         }
3226
3227         if (unused_mask) {
3228                 ret = -EINVAL;
3229                 goto ptype_unknown;
3230         }
3231
3232         all_ptypes = (*dev->dev_ops->dev_supported_ptypes_get)(dev);
3233         if (all_ptypes == NULL) {
3234                 ret = 0;
3235                 goto ptype_unknown;
3236         }
3237
3238         /*
3239          * Accommodate as many set_ptypes as possible. If the supplied
3240          * set_ptypes array is insufficient fill it partially.
3241          */
3242         for (i = 0, j = 0; set_ptypes != NULL &&
3243                                 (all_ptypes[i] != RTE_PTYPE_UNKNOWN); ++i) {
3244                 if (ptype_mask & all_ptypes[i]) {
3245                         if (j < num - 1) {
3246                                 set_ptypes[j] = all_ptypes[i];
3247                                 j++;
3248                                 continue;
3249                         }
3250                         break;
3251                 }
3252         }
3253
3254         if (set_ptypes != NULL && j < num)
3255                 set_ptypes[j] = RTE_PTYPE_UNKNOWN;
3256
3257         return (*dev->dev_ops->dev_ptypes_set)(dev, ptype_mask);
3258
3259 ptype_unknown:
3260         if (num > 0)
3261                 set_ptypes[0] = RTE_PTYPE_UNKNOWN;
3262
3263         return ret;
3264 }
3265
3266 int
3267 rte_eth_macaddr_get(uint16_t port_id, struct rte_ether_addr *mac_addr)
3268 {
3269         struct rte_eth_dev *dev;
3270
3271         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3272         dev = &rte_eth_devices[port_id];
3273         rte_ether_addr_copy(&dev->data->mac_addrs[0], mac_addr);
3274
3275         return 0;
3276 }
3277
3278 int
3279 rte_eth_dev_get_mtu(uint16_t port_id, uint16_t *mtu)
3280 {
3281         struct rte_eth_dev *dev;
3282
3283         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3284
3285         dev = &rte_eth_devices[port_id];
3286         *mtu = dev->data->mtu;
3287         return 0;
3288 }
3289
3290 int
3291 rte_eth_dev_set_mtu(uint16_t port_id, uint16_t mtu)
3292 {
3293         int ret;
3294         struct rte_eth_dev_info dev_info;
3295         struct rte_eth_dev *dev;
3296
3297         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3298         dev = &rte_eth_devices[port_id];
3299         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mtu_set, -ENOTSUP);
3300
3301         /*
3302          * Check if the device supports dev_infos_get, if it does not
3303          * skip min_mtu/max_mtu validation here as this requires values
3304          * that are populated within the call to rte_eth_dev_info_get()
3305          * which relies on dev->dev_ops->dev_infos_get.
3306          */
3307         if (*dev->dev_ops->dev_infos_get != NULL) {
3308                 ret = rte_eth_dev_info_get(port_id, &dev_info);
3309                 if (ret != 0)
3310                         return ret;
3311
3312                 if (mtu < dev_info.min_mtu || mtu > dev_info.max_mtu)
3313                         return -EINVAL;
3314         }
3315
3316         ret = (*dev->dev_ops->mtu_set)(dev, mtu);
3317         if (!ret)
3318                 dev->data->mtu = mtu;
3319
3320         return eth_err(port_id, ret);
3321 }
3322
3323 int
3324 rte_eth_dev_vlan_filter(uint16_t port_id, uint16_t vlan_id, int on)
3325 {
3326         struct rte_eth_dev *dev;
3327         int ret;
3328
3329         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3330         dev = &rte_eth_devices[port_id];
3331         if (!(dev->data->dev_conf.rxmode.offloads &
3332               DEV_RX_OFFLOAD_VLAN_FILTER)) {
3333                 RTE_ETHDEV_LOG(ERR, "Port %u: vlan-filtering disabled\n",
3334                         port_id);
3335                 return -ENOSYS;
3336         }
3337
3338         if (vlan_id > 4095) {
3339                 RTE_ETHDEV_LOG(ERR, "Port_id=%u invalid vlan_id=%u > 4095\n",
3340                         port_id, vlan_id);
3341                 return -EINVAL;
3342         }
3343         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_filter_set, -ENOTSUP);
3344
3345         ret = (*dev->dev_ops->vlan_filter_set)(dev, vlan_id, on);
3346         if (ret == 0) {
3347                 struct rte_vlan_filter_conf *vfc;
3348                 int vidx;
3349                 int vbit;
3350
3351                 vfc = &dev->data->vlan_filter_conf;
3352                 vidx = vlan_id / 64;
3353                 vbit = vlan_id % 64;
3354
3355                 if (on)
3356                         vfc->ids[vidx] |= UINT64_C(1) << vbit;
3357                 else
3358                         vfc->ids[vidx] &= ~(UINT64_C(1) << vbit);
3359         }
3360
3361         return eth_err(port_id, ret);
3362 }
3363
3364 int
3365 rte_eth_dev_set_vlan_strip_on_queue(uint16_t port_id, uint16_t rx_queue_id,
3366                                     int on)
3367 {
3368         struct rte_eth_dev *dev;
3369
3370         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3371         dev = &rte_eth_devices[port_id];
3372         if (rx_queue_id >= dev->data->nb_rx_queues) {
3373                 RTE_ETHDEV_LOG(ERR, "Invalid rx_queue_id=%u\n", rx_queue_id);
3374                 return -EINVAL;
3375         }
3376
3377         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_strip_queue_set, -ENOTSUP);
3378         (*dev->dev_ops->vlan_strip_queue_set)(dev, rx_queue_id, on);
3379
3380         return 0;
3381 }
3382
3383 int
3384 rte_eth_dev_set_vlan_ether_type(uint16_t port_id,
3385                                 enum rte_vlan_type vlan_type,
3386                                 uint16_t tpid)
3387 {
3388         struct rte_eth_dev *dev;
3389
3390         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3391         dev = &rte_eth_devices[port_id];
3392         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_tpid_set, -ENOTSUP);
3393
3394         return eth_err(port_id, (*dev->dev_ops->vlan_tpid_set)(dev, vlan_type,
3395                                                                tpid));
3396 }
3397
3398 int
3399 rte_eth_dev_set_vlan_offload(uint16_t port_id, int offload_mask)
3400 {
3401         struct rte_eth_dev_info dev_info;
3402         struct rte_eth_dev *dev;
3403         int ret = 0;
3404         int mask = 0;
3405         int cur, org = 0;
3406         uint64_t orig_offloads;
3407         uint64_t dev_offloads;
3408         uint64_t new_offloads;
3409
3410         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3411         dev = &rte_eth_devices[port_id];
3412
3413         /* save original values in case of failure */
3414         orig_offloads = dev->data->dev_conf.rxmode.offloads;
3415         dev_offloads = orig_offloads;
3416
3417         /* check which option changed by application */
3418         cur = !!(offload_mask & ETH_VLAN_STRIP_OFFLOAD);
3419         org = !!(dev_offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
3420         if (cur != org) {
3421                 if (cur)
3422                         dev_offloads |= DEV_RX_OFFLOAD_VLAN_STRIP;
3423                 else
3424                         dev_offloads &= ~DEV_RX_OFFLOAD_VLAN_STRIP;
3425                 mask |= ETH_VLAN_STRIP_MASK;
3426         }
3427
3428         cur = !!(offload_mask & ETH_VLAN_FILTER_OFFLOAD);
3429         org = !!(dev_offloads & DEV_RX_OFFLOAD_VLAN_FILTER);
3430         if (cur != org) {
3431                 if (cur)
3432                         dev_offloads |= DEV_RX_OFFLOAD_VLAN_FILTER;
3433                 else
3434                         dev_offloads &= ~DEV_RX_OFFLOAD_VLAN_FILTER;
3435                 mask |= ETH_VLAN_FILTER_MASK;
3436         }
3437
3438         cur = !!(offload_mask & ETH_VLAN_EXTEND_OFFLOAD);
3439         org = !!(dev_offloads & DEV_RX_OFFLOAD_VLAN_EXTEND);
3440         if (cur != org) {
3441                 if (cur)
3442                         dev_offloads |= DEV_RX_OFFLOAD_VLAN_EXTEND;
3443                 else
3444                         dev_offloads &= ~DEV_RX_OFFLOAD_VLAN_EXTEND;
3445                 mask |= ETH_VLAN_EXTEND_MASK;
3446         }
3447
3448         cur = !!(offload_mask & ETH_QINQ_STRIP_OFFLOAD);
3449         org = !!(dev_offloads & DEV_RX_OFFLOAD_QINQ_STRIP);
3450         if (cur != org) {
3451                 if (cur)
3452                         dev_offloads |= DEV_RX_OFFLOAD_QINQ_STRIP;
3453                 else
3454                         dev_offloads &= ~DEV_RX_OFFLOAD_QINQ_STRIP;
3455                 mask |= ETH_QINQ_STRIP_MASK;
3456         }
3457
3458         /*no change*/
3459         if (mask == 0)
3460                 return ret;
3461
3462         ret = rte_eth_dev_info_get(port_id, &dev_info);
3463         if (ret != 0)
3464                 return ret;
3465
3466         /* Rx VLAN offloading must be within its device capabilities */
3467         if ((dev_offloads & dev_info.rx_offload_capa) != dev_offloads) {
3468                 new_offloads = dev_offloads & ~orig_offloads;
3469                 RTE_ETHDEV_LOG(ERR,
3470                         "Ethdev port_id=%u requested new added VLAN offloads "
3471                         "0x%" PRIx64 " must be within Rx offloads capabilities "
3472                         "0x%" PRIx64 " in %s()\n",
3473                         port_id, new_offloads, dev_info.rx_offload_capa,
3474                         __func__);
3475                 return -EINVAL;
3476         }
3477
3478         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_offload_set, -ENOTSUP);
3479         dev->data->dev_conf.rxmode.offloads = dev_offloads;
3480         ret = (*dev->dev_ops->vlan_offload_set)(dev, mask);
3481         if (ret) {
3482                 /* hit an error restore  original values */
3483                 dev->data->dev_conf.rxmode.offloads = orig_offloads;
3484         }
3485
3486         return eth_err(port_id, ret);
3487 }
3488
3489 int
3490 rte_eth_dev_get_vlan_offload(uint16_t port_id)
3491 {
3492         struct rte_eth_dev *dev;
3493         uint64_t *dev_offloads;
3494         int ret = 0;
3495
3496         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3497         dev = &rte_eth_devices[port_id];
3498         dev_offloads = &dev->data->dev_conf.rxmode.offloads;
3499
3500         if (*dev_offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
3501                 ret |= ETH_VLAN_STRIP_OFFLOAD;
3502
3503         if (*dev_offloads & DEV_RX_OFFLOAD_VLAN_FILTER)
3504                 ret |= ETH_VLAN_FILTER_OFFLOAD;
3505
3506         if (*dev_offloads & DEV_RX_OFFLOAD_VLAN_EXTEND)
3507                 ret |= ETH_VLAN_EXTEND_OFFLOAD;
3508
3509         if (*dev_offloads & DEV_RX_OFFLOAD_QINQ_STRIP)
3510                 ret |= ETH_QINQ_STRIP_OFFLOAD;
3511
3512         return ret;
3513 }
3514
3515 int
3516 rte_eth_dev_set_vlan_pvid(uint16_t port_id, uint16_t pvid, int on)
3517 {
3518         struct rte_eth_dev *dev;
3519
3520         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3521         dev = &rte_eth_devices[port_id];
3522         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_pvid_set, -ENOTSUP);
3523
3524         return eth_err(port_id, (*dev->dev_ops->vlan_pvid_set)(dev, pvid, on));
3525 }
3526
3527 int
3528 rte_eth_dev_flow_ctrl_get(uint16_t port_id, struct rte_eth_fc_conf *fc_conf)
3529 {
3530         struct rte_eth_dev *dev;
3531
3532         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3533         dev = &rte_eth_devices[port_id];
3534         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_get, -ENOTSUP);
3535         memset(fc_conf, 0, sizeof(*fc_conf));
3536         return eth_err(port_id, (*dev->dev_ops->flow_ctrl_get)(dev, fc_conf));
3537 }
3538
3539 int
3540 rte_eth_dev_flow_ctrl_set(uint16_t port_id, struct rte_eth_fc_conf *fc_conf)
3541 {
3542         struct rte_eth_dev *dev;
3543
3544         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3545         if ((fc_conf->send_xon != 0) && (fc_conf->send_xon != 1)) {
3546                 RTE_ETHDEV_LOG(ERR, "Invalid send_xon, only 0/1 allowed\n");
3547                 return -EINVAL;
3548         }
3549
3550         dev = &rte_eth_devices[port_id];
3551         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_set, -ENOTSUP);
3552         return eth_err(port_id, (*dev->dev_ops->flow_ctrl_set)(dev, fc_conf));
3553 }
3554
3555 int
3556 rte_eth_dev_priority_flow_ctrl_set(uint16_t port_id,
3557                                    struct rte_eth_pfc_conf *pfc_conf)
3558 {
3559         struct rte_eth_dev *dev;
3560
3561         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3562         if (pfc_conf->priority > (ETH_DCB_NUM_USER_PRIORITIES - 1)) {
3563                 RTE_ETHDEV_LOG(ERR, "Invalid priority, only 0-7 allowed\n");
3564                 return -EINVAL;
3565         }
3566
3567         dev = &rte_eth_devices[port_id];
3568         /* High water, low water validation are device specific */
3569         if  (*dev->dev_ops->priority_flow_ctrl_set)
3570                 return eth_err(port_id, (*dev->dev_ops->priority_flow_ctrl_set)
3571                                         (dev, pfc_conf));
3572         return -ENOTSUP;
3573 }
3574
3575 static int
3576 rte_eth_check_reta_mask(struct rte_eth_rss_reta_entry64 *reta_conf,
3577                         uint16_t reta_size)
3578 {
3579         uint16_t i, num;
3580
3581         if (!reta_conf)
3582                 return -EINVAL;
3583
3584         num = (reta_size + RTE_RETA_GROUP_SIZE - 1) / RTE_RETA_GROUP_SIZE;
3585         for (i = 0; i < num; i++) {
3586                 if (reta_conf[i].mask)
3587                         return 0;
3588         }
3589
3590         return -EINVAL;
3591 }
3592
3593 static int
3594 rte_eth_check_reta_entry(struct rte_eth_rss_reta_entry64 *reta_conf,
3595                          uint16_t reta_size,
3596                          uint16_t max_rxq)
3597 {
3598         uint16_t i, idx, shift;
3599
3600         if (!reta_conf)
3601                 return -EINVAL;
3602
3603         if (max_rxq == 0) {
3604                 RTE_ETHDEV_LOG(ERR, "No receive queue is available\n");
3605                 return -EINVAL;
3606         }
3607
3608         for (i = 0; i < reta_size; i++) {
3609                 idx = i / RTE_RETA_GROUP_SIZE;
3610                 shift = i % RTE_RETA_GROUP_SIZE;
3611                 if ((reta_conf[idx].mask & (1ULL << shift)) &&
3612                         (reta_conf[idx].reta[shift] >= max_rxq)) {
3613                         RTE_ETHDEV_LOG(ERR,
3614                                 "reta_conf[%u]->reta[%u]: %u exceeds the maximum rxq index: %u\n",
3615                                 idx, shift,
3616                                 reta_conf[idx].reta[shift], max_rxq);
3617                         return -EINVAL;
3618                 }
3619         }
3620
3621         return 0;
3622 }
3623
3624 int
3625 rte_eth_dev_rss_reta_update(uint16_t port_id,
3626                             struct rte_eth_rss_reta_entry64 *reta_conf,
3627                             uint16_t reta_size)
3628 {
3629         struct rte_eth_dev *dev;
3630         int ret;
3631
3632         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3633         /* Check mask bits */
3634         ret = rte_eth_check_reta_mask(reta_conf, reta_size);
3635         if (ret < 0)
3636                 return ret;
3637
3638         dev = &rte_eth_devices[port_id];
3639
3640         /* Check entry value */
3641         ret = rte_eth_check_reta_entry(reta_conf, reta_size,
3642                                 dev->data->nb_rx_queues);
3643         if (ret < 0)
3644                 return ret;
3645
3646         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->reta_update, -ENOTSUP);
3647         return eth_err(port_id, (*dev->dev_ops->reta_update)(dev, reta_conf,
3648                                                              reta_size));
3649 }
3650
3651 int
3652 rte_eth_dev_rss_reta_query(uint16_t port_id,
3653                            struct rte_eth_rss_reta_entry64 *reta_conf,
3654                            uint16_t reta_size)
3655 {
3656         struct rte_eth_dev *dev;
3657         int ret;
3658
3659         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3660
3661         /* Check mask bits */
3662         ret = rte_eth_check_reta_mask(reta_conf, reta_size);
3663         if (ret < 0)
3664                 return ret;
3665
3666         dev = &rte_eth_devices[port_id];
3667         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->reta_query, -ENOTSUP);
3668         return eth_err(port_id, (*dev->dev_ops->reta_query)(dev, reta_conf,
3669                                                             reta_size));
3670 }
3671
3672 int
3673 rte_eth_dev_rss_hash_update(uint16_t port_id,
3674                             struct rte_eth_rss_conf *rss_conf)
3675 {
3676         struct rte_eth_dev *dev;
3677         struct rte_eth_dev_info dev_info = { .flow_type_rss_offloads = 0, };
3678         int ret;
3679
3680         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3681
3682         ret = rte_eth_dev_info_get(port_id, &dev_info);
3683         if (ret != 0)
3684                 return ret;
3685
3686         rss_conf->rss_hf = rte_eth_rss_hf_refine(rss_conf->rss_hf);
3687
3688         dev = &rte_eth_devices[port_id];
3689         if ((dev_info.flow_type_rss_offloads | rss_conf->rss_hf) !=
3690             dev_info.flow_type_rss_offloads) {
3691                 RTE_ETHDEV_LOG(ERR,
3692                         "Ethdev port_id=%u invalid rss_hf: 0x%"PRIx64", valid value: 0x%"PRIx64"\n",
3693                         port_id, rss_conf->rss_hf,
3694                         dev_info.flow_type_rss_offloads);
3695                 return -EINVAL;
3696         }
3697         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rss_hash_update, -ENOTSUP);
3698         return eth_err(port_id, (*dev->dev_ops->rss_hash_update)(dev,
3699                                                                  rss_conf));
3700 }
3701
3702 int
3703 rte_eth_dev_rss_hash_conf_get(uint16_t port_id,
3704                               struct rte_eth_rss_conf *rss_conf)
3705 {
3706         struct rte_eth_dev *dev;
3707
3708         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3709         dev = &rte_eth_devices[port_id];
3710         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rss_hash_conf_get, -ENOTSUP);
3711         return eth_err(port_id, (*dev->dev_ops->rss_hash_conf_get)(dev,
3712                                                                    rss_conf));
3713 }
3714
3715 int
3716 rte_eth_dev_udp_tunnel_port_add(uint16_t port_id,
3717                                 struct rte_eth_udp_tunnel *udp_tunnel)
3718 {
3719         struct rte_eth_dev *dev;
3720
3721         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3722         if (udp_tunnel == NULL) {
3723                 RTE_ETHDEV_LOG(ERR, "Invalid udp_tunnel parameter\n");
3724                 return -EINVAL;
3725         }
3726
3727         if (udp_tunnel->prot_type >= RTE_TUNNEL_TYPE_MAX) {
3728                 RTE_ETHDEV_LOG(ERR, "Invalid tunnel type\n");
3729                 return -EINVAL;
3730         }
3731
3732         dev = &rte_eth_devices[port_id];
3733         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->udp_tunnel_port_add, -ENOTSUP);
3734         return eth_err(port_id, (*dev->dev_ops->udp_tunnel_port_add)(dev,
3735                                                                 udp_tunnel));
3736 }
3737
3738 int
3739 rte_eth_dev_udp_tunnel_port_delete(uint16_t port_id,
3740                                    struct rte_eth_udp_tunnel *udp_tunnel)
3741 {
3742         struct rte_eth_dev *dev;
3743
3744         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3745         dev = &rte_eth_devices[port_id];
3746
3747         if (udp_tunnel == NULL) {
3748                 RTE_ETHDEV_LOG(ERR, "Invalid udp_tunnel parameter\n");
3749                 return -EINVAL;
3750         }
3751
3752         if (udp_tunnel->prot_type >= RTE_TUNNEL_TYPE_MAX) {
3753                 RTE_ETHDEV_LOG(ERR, "Invalid tunnel type\n");
3754                 return -EINVAL;
3755         }
3756
3757         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->udp_tunnel_port_del, -ENOTSUP);
3758         return eth_err(port_id, (*dev->dev_ops->udp_tunnel_port_del)(dev,
3759                                                                 udp_tunnel));
3760 }
3761
3762 int
3763 rte_eth_led_on(uint16_t port_id)
3764 {
3765         struct rte_eth_dev *dev;
3766
3767         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3768         dev = &rte_eth_devices[port_id];
3769         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_on, -ENOTSUP);
3770         return eth_err(port_id, (*dev->dev_ops->dev_led_on)(dev));
3771 }
3772
3773 int
3774 rte_eth_led_off(uint16_t port_id)
3775 {
3776         struct rte_eth_dev *dev;
3777
3778         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3779         dev = &rte_eth_devices[port_id];
3780         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_off, -ENOTSUP);
3781         return eth_err(port_id, (*dev->dev_ops->dev_led_off)(dev));
3782 }
3783
3784 int
3785 rte_eth_fec_get_capability(uint16_t port_id,
3786                            struct rte_eth_fec_capa *speed_fec_capa,
3787                            unsigned int num)
3788 {
3789         struct rte_eth_dev *dev;
3790         int ret;
3791
3792         if (speed_fec_capa == NULL && num > 0)
3793                 return -EINVAL;
3794
3795         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3796         dev = &rte_eth_devices[port_id];
3797         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fec_get_capability, -ENOTSUP);
3798         ret = (*dev->dev_ops->fec_get_capability)(dev, speed_fec_capa, num);
3799
3800         return ret;
3801 }
3802
3803 int
3804 rte_eth_fec_get(uint16_t port_id, uint32_t *fec_capa)
3805 {
3806         struct rte_eth_dev *dev;
3807
3808         if (fec_capa == NULL)
3809                 return -EINVAL;
3810
3811         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3812         dev = &rte_eth_devices[port_id];
3813         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fec_get, -ENOTSUP);
3814         return eth_err(port_id, (*dev->dev_ops->fec_get)(dev, fec_capa));
3815 }
3816
3817 int
3818 rte_eth_fec_set(uint16_t port_id, uint32_t fec_capa)
3819 {
3820         struct rte_eth_dev *dev;
3821
3822         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3823         dev = &rte_eth_devices[port_id];
3824         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fec_set, -ENOTSUP);
3825         return eth_err(port_id, (*dev->dev_ops->fec_set)(dev, fec_capa));
3826 }
3827
3828 /*
3829  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
3830  * an empty spot.
3831  */
3832 static int
3833 get_mac_addr_index(uint16_t port_id, const struct rte_ether_addr *addr)
3834 {
3835         struct rte_eth_dev_info dev_info;
3836         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
3837         unsigned i;
3838         int ret;
3839
3840         ret = rte_eth_dev_info_get(port_id, &dev_info);
3841         if (ret != 0)
3842                 return -1;
3843
3844         for (i = 0; i < dev_info.max_mac_addrs; i++)
3845                 if (memcmp(addr, &dev->data->mac_addrs[i],
3846                                 RTE_ETHER_ADDR_LEN) == 0)
3847                         return i;
3848
3849         return -1;
3850 }
3851
3852 static const struct rte_ether_addr null_mac_addr;
3853
3854 int
3855 rte_eth_dev_mac_addr_add(uint16_t port_id, struct rte_ether_addr *addr,
3856                         uint32_t pool)
3857 {
3858         struct rte_eth_dev *dev;
3859         int index;
3860         uint64_t pool_mask;
3861         int ret;
3862
3863         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3864         dev = &rte_eth_devices[port_id];
3865         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_add, -ENOTSUP);
3866
3867         if (rte_is_zero_ether_addr(addr)) {
3868                 RTE_ETHDEV_LOG(ERR, "Port %u: Cannot add NULL MAC address\n",
3869                         port_id);
3870                 return -EINVAL;
3871         }
3872         if (pool >= ETH_64_POOLS) {
3873                 RTE_ETHDEV_LOG(ERR, "Pool id must be 0-%d\n", ETH_64_POOLS - 1);
3874                 return -EINVAL;
3875         }
3876
3877         index = get_mac_addr_index(port_id, addr);
3878         if (index < 0) {
3879                 index = get_mac_addr_index(port_id, &null_mac_addr);
3880                 if (index < 0) {
3881                         RTE_ETHDEV_LOG(ERR, "Port %u: MAC address array full\n",
3882                                 port_id);
3883                         return -ENOSPC;
3884                 }
3885         } else {
3886                 pool_mask = dev->data->mac_pool_sel[index];
3887
3888                 /* Check if both MAC address and pool is already there, and do nothing */
3889                 if (pool_mask & (1ULL << pool))
3890                         return 0;
3891         }
3892
3893         /* Update NIC */
3894         ret = (*dev->dev_ops->mac_addr_add)(dev, addr, index, pool);
3895
3896         if (ret == 0) {
3897                 /* Update address in NIC data structure */
3898                 rte_ether_addr_copy(addr, &dev->data->mac_addrs[index]);
3899
3900                 /* Update pool bitmap in NIC data structure */
3901                 dev->data->mac_pool_sel[index] |= (1ULL << pool);
3902         }
3903
3904         return eth_err(port_id, ret);
3905 }
3906
3907 int
3908 rte_eth_dev_mac_addr_remove(uint16_t port_id, struct rte_ether_addr *addr)
3909 {
3910         struct rte_eth_dev *dev;
3911         int index;
3912
3913         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3914         dev = &rte_eth_devices[port_id];
3915         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_remove, -ENOTSUP);
3916
3917         index = get_mac_addr_index(port_id, addr);
3918         if (index == 0) {
3919                 RTE_ETHDEV_LOG(ERR,
3920                         "Port %u: Cannot remove default MAC address\n",
3921                         port_id);
3922                 return -EADDRINUSE;
3923         } else if (index < 0)
3924                 return 0;  /* Do nothing if address wasn't found */
3925
3926         /* Update NIC */
3927         (*dev->dev_ops->mac_addr_remove)(dev, index);
3928
3929         /* Update address in NIC data structure */
3930         rte_ether_addr_copy(&null_mac_addr, &dev->data->mac_addrs[index]);
3931
3932         /* reset pool bitmap */
3933         dev->data->mac_pool_sel[index] = 0;
3934
3935         return 0;
3936 }
3937
3938 int
3939 rte_eth_dev_default_mac_addr_set(uint16_t port_id, struct rte_ether_addr *addr)
3940 {
3941         struct rte_eth_dev *dev;
3942         int ret;
3943
3944         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3945
3946         if (!rte_is_valid_assigned_ether_addr(addr))
3947                 return -EINVAL;
3948
3949         dev = &rte_eth_devices[port_id];
3950         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_set, -ENOTSUP);
3951
3952         ret = (*dev->dev_ops->mac_addr_set)(dev, addr);
3953         if (ret < 0)
3954                 return ret;
3955
3956         /* Update default address in NIC data structure */
3957         rte_ether_addr_copy(addr, &dev->data->mac_addrs[0]);
3958
3959         return 0;
3960 }
3961
3962
3963 /*
3964  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
3965  * an empty spot.
3966  */
3967 static int
3968 get_hash_mac_addr_index(uint16_t port_id, const struct rte_ether_addr *addr)
3969 {
3970         struct rte_eth_dev_info dev_info;
3971         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
3972         unsigned i;
3973         int ret;
3974
3975         ret = rte_eth_dev_info_get(port_id, &dev_info);
3976         if (ret != 0)
3977                 return -1;
3978
3979         if (!dev->data->hash_mac_addrs)
3980                 return -1;
3981
3982         for (i = 0; i < dev_info.max_hash_mac_addrs; i++)
3983                 if (memcmp(addr, &dev->data->hash_mac_addrs[i],
3984                         RTE_ETHER_ADDR_LEN) == 0)
3985                         return i;
3986
3987         return -1;
3988 }
3989
3990 int
3991 rte_eth_dev_uc_hash_table_set(uint16_t port_id, struct rte_ether_addr *addr,
3992                                 uint8_t on)
3993 {
3994         int index;
3995         int ret;
3996         struct rte_eth_dev *dev;
3997
3998         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3999
4000         dev = &rte_eth_devices[port_id];
4001         if (rte_is_zero_ether_addr(addr)) {
4002                 RTE_ETHDEV_LOG(ERR, "Port %u: Cannot add NULL MAC address\n",
4003                         port_id);
4004                 return -EINVAL;
4005         }
4006
4007         index = get_hash_mac_addr_index(port_id, addr);
4008         /* Check if it's already there, and do nothing */
4009         if ((index >= 0) && on)
4010                 return 0;
4011
4012         if (index < 0) {
4013                 if (!on) {
4014                         RTE_ETHDEV_LOG(ERR,
4015                                 "Port %u: the MAC address was not set in UTA\n",
4016                                 port_id);
4017                         return -EINVAL;
4018                 }
4019
4020                 index = get_hash_mac_addr_index(port_id, &null_mac_addr);
4021                 if (index < 0) {
4022                         RTE_ETHDEV_LOG(ERR, "Port %u: MAC address array full\n",
4023                                 port_id);
4024                         return -ENOSPC;
4025                 }
4026         }
4027
4028         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->uc_hash_table_set, -ENOTSUP);
4029         ret = (*dev->dev_ops->uc_hash_table_set)(dev, addr, on);
4030         if (ret == 0) {
4031                 /* Update address in NIC data structure */
4032                 if (on)
4033                         rte_ether_addr_copy(addr,
4034                                         &dev->data->hash_mac_addrs[index]);
4035                 else
4036                         rte_ether_addr_copy(&null_mac_addr,
4037                                         &dev->data->hash_mac_addrs[index]);
4038         }
4039
4040         return eth_err(port_id, ret);
4041 }
4042
4043 int
4044 rte_eth_dev_uc_all_hash_table_set(uint16_t port_id, uint8_t on)
4045 {
4046         struct rte_eth_dev *dev;
4047
4048         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4049
4050         dev = &rte_eth_devices[port_id];
4051
4052         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->uc_all_hash_table_set, -ENOTSUP);
4053         return eth_err(port_id, (*dev->dev_ops->uc_all_hash_table_set)(dev,
4054                                                                        on));
4055 }
4056
4057 int rte_eth_set_queue_rate_limit(uint16_t port_id, uint16_t queue_idx,
4058                                         uint16_t tx_rate)
4059 {
4060         struct rte_eth_dev *dev;
4061         struct rte_eth_dev_info dev_info;
4062         struct rte_eth_link link;
4063         int ret;
4064
4065         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4066
4067         ret = rte_eth_dev_info_get(port_id, &dev_info);
4068         if (ret != 0)
4069                 return ret;
4070
4071         dev = &rte_eth_devices[port_id];
4072         link = dev->data->dev_link;
4073
4074         if (queue_idx > dev_info.max_tx_queues) {
4075                 RTE_ETHDEV_LOG(ERR,
4076                         "Set queue rate limit:port %u: invalid queue id=%u\n",
4077                         port_id, queue_idx);
4078                 return -EINVAL;
4079         }
4080
4081         if (tx_rate > link.link_speed) {
4082                 RTE_ETHDEV_LOG(ERR,
4083                         "Set queue rate limit:invalid tx_rate=%u, bigger than link speed= %d\n",
4084                         tx_rate, link.link_speed);
4085                 return -EINVAL;
4086         }
4087
4088         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_queue_rate_limit, -ENOTSUP);
4089         return eth_err(port_id, (*dev->dev_ops->set_queue_rate_limit)(dev,
4090                                                         queue_idx, tx_rate));
4091 }
4092
4093 int
4094 rte_eth_mirror_rule_set(uint16_t port_id,
4095                         struct rte_eth_mirror_conf *mirror_conf,
4096                         uint8_t rule_id, uint8_t on)
4097 {
4098         struct rte_eth_dev *dev;
4099
4100         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4101         if (mirror_conf->rule_type == 0) {
4102                 RTE_ETHDEV_LOG(ERR, "Mirror rule type can not be 0\n");
4103                 return -EINVAL;
4104         }
4105
4106         if (mirror_conf->dst_pool >= ETH_64_POOLS) {
4107                 RTE_ETHDEV_LOG(ERR, "Invalid dst pool, pool id must be 0-%d\n",
4108                         ETH_64_POOLS - 1);
4109                 return -EINVAL;
4110         }
4111
4112         if ((mirror_conf->rule_type & (ETH_MIRROR_VIRTUAL_POOL_UP |
4113              ETH_MIRROR_VIRTUAL_POOL_DOWN)) &&
4114             (mirror_conf->pool_mask == 0)) {
4115                 RTE_ETHDEV_LOG(ERR,
4116                         "Invalid mirror pool, pool mask can not be 0\n");
4117                 return -EINVAL;
4118         }
4119
4120         if ((mirror_conf->rule_type & ETH_MIRROR_VLAN) &&
4121             mirror_conf->vlan.vlan_mask == 0) {
4122                 RTE_ETHDEV_LOG(ERR,
4123                         "Invalid vlan mask, vlan mask can not be 0\n");
4124                 return -EINVAL;
4125         }
4126
4127         dev = &rte_eth_devices[port_id];
4128         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mirror_rule_set, -ENOTSUP);
4129
4130         return eth_err(port_id, (*dev->dev_ops->mirror_rule_set)(dev,
4131                                                 mirror_conf, rule_id, on));
4132 }
4133
4134 int
4135 rte_eth_mirror_rule_reset(uint16_t port_id, uint8_t rule_id)
4136 {
4137         struct rte_eth_dev *dev;
4138
4139         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4140
4141         dev = &rte_eth_devices[port_id];
4142         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mirror_rule_reset, -ENOTSUP);
4143
4144         return eth_err(port_id, (*dev->dev_ops->mirror_rule_reset)(dev,
4145                                                                    rule_id));
4146 }
4147
4148 RTE_INIT(eth_dev_init_cb_lists)
4149 {
4150         int i;
4151
4152         for (i = 0; i < RTE_MAX_ETHPORTS; i++)
4153                 TAILQ_INIT(&rte_eth_devices[i].link_intr_cbs);
4154 }
4155
4156 int
4157 rte_eth_dev_callback_register(uint16_t port_id,
4158                         enum rte_eth_event_type event,
4159                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
4160 {
4161         struct rte_eth_dev *dev;
4162         struct rte_eth_dev_callback *user_cb;
4163         uint32_t next_port; /* size is 32-bit to prevent loop wrap-around */
4164         uint16_t last_port;
4165
4166         if (!cb_fn)
4167                 return -EINVAL;
4168
4169         if (!rte_eth_dev_is_valid_port(port_id) && port_id != RTE_ETH_ALL) {
4170                 RTE_ETHDEV_LOG(ERR, "Invalid port_id=%d\n", port_id);
4171                 return -EINVAL;
4172         }
4173
4174         if (port_id == RTE_ETH_ALL) {
4175                 next_port = 0;
4176                 last_port = RTE_MAX_ETHPORTS - 1;
4177         } else {
4178                 next_port = last_port = port_id;
4179         }
4180
4181         rte_spinlock_lock(&rte_eth_dev_cb_lock);
4182
4183         do {
4184                 dev = &rte_eth_devices[next_port];
4185
4186                 TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
4187                         if (user_cb->cb_fn == cb_fn &&
4188                                 user_cb->cb_arg == cb_arg &&
4189                                 user_cb->event == event) {
4190                                 break;
4191                         }
4192                 }
4193
4194                 /* create a new callback. */
4195                 if (user_cb == NULL) {
4196                         user_cb = rte_zmalloc("INTR_USER_CALLBACK",
4197                                 sizeof(struct rte_eth_dev_callback), 0);
4198                         if (user_cb != NULL) {
4199                                 user_cb->cb_fn = cb_fn;
4200                                 user_cb->cb_arg = cb_arg;
4201                                 user_cb->event = event;
4202                                 TAILQ_INSERT_TAIL(&(dev->link_intr_cbs),
4203                                                   user_cb, next);
4204                         } else {
4205                                 rte_spinlock_unlock(&rte_eth_dev_cb_lock);
4206                                 rte_eth_dev_callback_unregister(port_id, event,
4207                                                                 cb_fn, cb_arg);
4208                                 return -ENOMEM;
4209                         }
4210
4211                 }
4212         } while (++next_port <= last_port);
4213
4214         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
4215         return 0;
4216 }
4217
4218 int
4219 rte_eth_dev_callback_unregister(uint16_t port_id,
4220                         enum rte_eth_event_type event,
4221                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
4222 {
4223         int ret;
4224         struct rte_eth_dev *dev;
4225         struct rte_eth_dev_callback *cb, *next;
4226         uint32_t next_port; /* size is 32-bit to prevent loop wrap-around */
4227         uint16_t last_port;
4228
4229         if (!cb_fn)
4230                 return -EINVAL;
4231
4232         if (!rte_eth_dev_is_valid_port(port_id) && port_id != RTE_ETH_ALL) {
4233                 RTE_ETHDEV_LOG(ERR, "Invalid port_id=%d\n", port_id);
4234                 return -EINVAL;
4235         }
4236
4237         if (port_id == RTE_ETH_ALL) {
4238                 next_port = 0;
4239                 last_port = RTE_MAX_ETHPORTS - 1;
4240         } else {
4241                 next_port = last_port = port_id;
4242         }
4243
4244         rte_spinlock_lock(&rte_eth_dev_cb_lock);
4245
4246         do {
4247                 dev = &rte_eth_devices[next_port];
4248                 ret = 0;
4249                 for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL;
4250                      cb = next) {
4251
4252                         next = TAILQ_NEXT(cb, next);
4253
4254                         if (cb->cb_fn != cb_fn || cb->event != event ||
4255                             (cb_arg != (void *)-1 && cb->cb_arg != cb_arg))
4256                                 continue;
4257
4258                         /*
4259                          * if this callback is not executing right now,
4260                          * then remove it.
4261                          */
4262                         if (cb->active == 0) {
4263                                 TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
4264                                 rte_free(cb);
4265                         } else {
4266                                 ret = -EAGAIN;
4267                         }
4268                 }
4269         } while (++next_port <= last_port);
4270
4271         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
4272         return ret;
4273 }
4274
4275 int
4276 rte_eth_dev_callback_process(struct rte_eth_dev *dev,
4277         enum rte_eth_event_type event, void *ret_param)
4278 {
4279         struct rte_eth_dev_callback *cb_lst;
4280         struct rte_eth_dev_callback dev_cb;
4281         int rc = 0;
4282
4283         rte_spinlock_lock(&rte_eth_dev_cb_lock);
4284         TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
4285                 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
4286                         continue;
4287                 dev_cb = *cb_lst;
4288                 cb_lst->active = 1;
4289                 if (ret_param != NULL)
4290                         dev_cb.ret_param = ret_param;
4291
4292                 rte_spinlock_unlock(&rte_eth_dev_cb_lock);
4293                 rc = dev_cb.cb_fn(dev->data->port_id, dev_cb.event,
4294                                 dev_cb.cb_arg, dev_cb.ret_param);
4295                 rte_spinlock_lock(&rte_eth_dev_cb_lock);
4296                 cb_lst->active = 0;
4297         }
4298         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
4299         return rc;
4300 }
4301
4302 void
4303 rte_eth_dev_probing_finish(struct rte_eth_dev *dev)
4304 {
4305         if (dev == NULL)
4306                 return;
4307
4308         rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_NEW, NULL);
4309
4310         dev->state = RTE_ETH_DEV_ATTACHED;
4311 }
4312
4313 int
4314 rte_eth_dev_rx_intr_ctl(uint16_t port_id, int epfd, int op, void *data)
4315 {
4316         uint32_t vec;
4317         struct rte_eth_dev *dev;
4318         struct rte_intr_handle *intr_handle;
4319         uint16_t qid;
4320         int rc;
4321
4322         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4323
4324         dev = &rte_eth_devices[port_id];
4325
4326         if (!dev->intr_handle) {
4327                 RTE_ETHDEV_LOG(ERR, "RX Intr handle unset\n");
4328                 return -ENOTSUP;
4329         }
4330
4331         intr_handle = dev->intr_handle;
4332         if (!intr_handle->intr_vec) {
4333                 RTE_ETHDEV_LOG(ERR, "RX Intr vector unset\n");
4334                 return -EPERM;
4335         }
4336
4337         for (qid = 0; qid < dev->data->nb_rx_queues; qid++) {
4338                 vec = intr_handle->intr_vec[qid];
4339                 rc = rte_intr_rx_ctl(intr_handle, epfd, op, vec, data);
4340                 if (rc && rc != -EEXIST) {
4341                         RTE_ETHDEV_LOG(ERR,
4342                                 "p %u q %u rx ctl error op %d epfd %d vec %u\n",
4343                                 port_id, qid, op, epfd, vec);
4344                 }
4345         }
4346
4347         return 0;
4348 }
4349
4350 int
4351 rte_eth_dev_rx_intr_ctl_q_get_fd(uint16_t port_id, uint16_t queue_id)
4352 {
4353         struct rte_intr_handle *intr_handle;
4354         struct rte_eth_dev *dev;
4355         unsigned int efd_idx;
4356         uint32_t vec;
4357         int fd;
4358
4359         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -1);
4360
4361         dev = &rte_eth_devices[port_id];
4362
4363         if (queue_id >= dev->data->nb_rx_queues) {
4364                 RTE_ETHDEV_LOG(ERR, "Invalid RX queue_id=%u\n", queue_id);
4365                 return -1;
4366         }
4367
4368         if (!dev->intr_handle) {
4369                 RTE_ETHDEV_LOG(ERR, "RX Intr handle unset\n");
4370                 return -1;
4371         }
4372
4373         intr_handle = dev->intr_handle;
4374         if (!intr_handle->intr_vec) {
4375                 RTE_ETHDEV_LOG(ERR, "RX Intr vector unset\n");
4376                 return -1;
4377         }
4378
4379         vec = intr_handle->intr_vec[queue_id];
4380         efd_idx = (vec >= RTE_INTR_VEC_RXTX_OFFSET) ?
4381                 (vec - RTE_INTR_VEC_RXTX_OFFSET) : vec;
4382         fd = intr_handle->efds[efd_idx];
4383
4384         return fd;
4385 }
4386
4387 static inline int
4388 eth_dma_mzone_name(char *name, size_t len, uint16_t port_id, uint16_t queue_id,
4389                 const char *ring_name)
4390 {
4391         return snprintf(name, len, "eth_p%d_q%d_%s",
4392                         port_id, queue_id, ring_name);
4393 }
4394
4395 const struct rte_memzone *
4396 rte_eth_dma_zone_reserve(const struct rte_eth_dev *dev, const char *ring_name,
4397                          uint16_t queue_id, size_t size, unsigned align,
4398                          int socket_id)
4399 {
4400         char z_name[RTE_MEMZONE_NAMESIZE];
4401         const struct rte_memzone *mz;
4402         int rc;
4403
4404         rc = eth_dma_mzone_name(z_name, sizeof(z_name), dev->data->port_id,
4405                         queue_id, ring_name);
4406         if (rc >= RTE_MEMZONE_NAMESIZE) {
4407                 RTE_ETHDEV_LOG(ERR, "ring name too long\n");
4408                 rte_errno = ENAMETOOLONG;
4409                 return NULL;
4410         }
4411
4412         mz = rte_memzone_lookup(z_name);
4413         if (mz) {
4414                 if ((socket_id != SOCKET_ID_ANY && socket_id != mz->socket_id) ||
4415                                 size > mz->len ||
4416                                 ((uintptr_t)mz->addr & (align - 1)) != 0) {
4417                         RTE_ETHDEV_LOG(ERR,
4418                                 "memzone %s does not justify the requested attributes\n",
4419                                 mz->name);
4420                         return NULL;
4421                 }
4422
4423                 return mz;
4424         }
4425
4426         return rte_memzone_reserve_aligned(z_name, size, socket_id,
4427                         RTE_MEMZONE_IOVA_CONTIG, align);
4428 }
4429
4430 int
4431 rte_eth_dma_zone_free(const struct rte_eth_dev *dev, const char *ring_name,
4432                 uint16_t queue_id)
4433 {
4434         char z_name[RTE_MEMZONE_NAMESIZE];
4435         const struct rte_memzone *mz;
4436         int rc = 0;
4437
4438         rc = eth_dma_mzone_name(z_name, sizeof(z_name), dev->data->port_id,
4439                         queue_id, ring_name);
4440         if (rc >= RTE_MEMZONE_NAMESIZE) {
4441                 RTE_ETHDEV_LOG(ERR, "ring name too long\n");
4442                 return -ENAMETOOLONG;
4443         }
4444
4445         mz = rte_memzone_lookup(z_name);
4446         if (mz)
4447                 rc = rte_memzone_free(mz);
4448         else
4449                 rc = -ENOENT;
4450
4451         return rc;
4452 }
4453
4454 int
4455 rte_eth_dev_create(struct rte_device *device, const char *name,
4456         size_t priv_data_size,
4457         ethdev_bus_specific_init ethdev_bus_specific_init,
4458         void *bus_init_params,
4459         ethdev_init_t ethdev_init, void *init_params)
4460 {
4461         struct rte_eth_dev *ethdev;
4462         int retval;
4463
4464         RTE_FUNC_PTR_OR_ERR_RET(*ethdev_init, -EINVAL);
4465
4466         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
4467                 ethdev = rte_eth_dev_allocate(name);
4468                 if (!ethdev)
4469                         return -ENODEV;
4470
4471                 if (priv_data_size) {
4472                         ethdev->data->dev_private = rte_zmalloc_socket(
4473                                 name, priv_data_size, RTE_CACHE_LINE_SIZE,
4474                                 device->numa_node);
4475
4476                         if (!ethdev->data->dev_private) {
4477                                 RTE_ETHDEV_LOG(ERR,
4478                                         "failed to allocate private data\n");
4479                                 retval = -ENOMEM;
4480                                 goto probe_failed;
4481                         }
4482                 }
4483         } else {
4484                 ethdev = rte_eth_dev_attach_secondary(name);
4485                 if (!ethdev) {
4486                         RTE_ETHDEV_LOG(ERR,
4487                                 "secondary process attach failed, ethdev doesn't exist\n");
4488                         return  -ENODEV;
4489                 }
4490         }
4491
4492         ethdev->device = device;
4493
4494         if (ethdev_bus_specific_init) {
4495                 retval = ethdev_bus_specific_init(ethdev, bus_init_params);
4496                 if (retval) {
4497                         RTE_ETHDEV_LOG(ERR,
4498                                 "ethdev bus specific initialisation failed\n");
4499                         goto probe_failed;
4500                 }
4501         }
4502
4503         retval = ethdev_init(ethdev, init_params);
4504         if (retval) {
4505                 RTE_ETHDEV_LOG(ERR, "ethdev initialisation failed\n");
4506                 goto probe_failed;
4507         }
4508
4509         rte_eth_dev_probing_finish(ethdev);
4510
4511         return retval;
4512
4513 probe_failed:
4514         rte_eth_dev_release_port(ethdev);
4515         return retval;
4516 }
4517
4518 int
4519 rte_eth_dev_destroy(struct rte_eth_dev *ethdev,
4520         ethdev_uninit_t ethdev_uninit)
4521 {
4522         int ret;
4523
4524         ethdev = rte_eth_dev_allocated(ethdev->data->name);
4525         if (!ethdev)
4526                 return -ENODEV;
4527
4528         RTE_FUNC_PTR_OR_ERR_RET(*ethdev_uninit, -EINVAL);
4529
4530         ret = ethdev_uninit(ethdev);
4531         if (ret)
4532                 return ret;
4533
4534         return rte_eth_dev_release_port(ethdev);
4535 }
4536
4537 int
4538 rte_eth_dev_rx_intr_ctl_q(uint16_t port_id, uint16_t queue_id,
4539                           int epfd, int op, void *data)
4540 {
4541         uint32_t vec;
4542         struct rte_eth_dev *dev;
4543         struct rte_intr_handle *intr_handle;
4544         int rc;
4545
4546         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4547
4548         dev = &rte_eth_devices[port_id];
4549         if (queue_id >= dev->data->nb_rx_queues) {
4550                 RTE_ETHDEV_LOG(ERR, "Invalid RX queue_id=%u\n", queue_id);
4551                 return -EINVAL;
4552         }
4553
4554         if (!dev->intr_handle) {
4555                 RTE_ETHDEV_LOG(ERR, "RX Intr handle unset\n");
4556                 return -ENOTSUP;
4557         }
4558
4559         intr_handle = dev->intr_handle;
4560         if (!intr_handle->intr_vec) {
4561                 RTE_ETHDEV_LOG(ERR, "RX Intr vector unset\n");
4562                 return -EPERM;
4563         }
4564
4565         vec = intr_handle->intr_vec[queue_id];
4566         rc = rte_intr_rx_ctl(intr_handle, epfd, op, vec, data);
4567         if (rc && rc != -EEXIST) {
4568                 RTE_ETHDEV_LOG(ERR,
4569                         "p %u q %u rx ctl error op %d epfd %d vec %u\n",
4570                         port_id, queue_id, op, epfd, vec);
4571                 return rc;
4572         }
4573
4574         return 0;
4575 }
4576
4577 int
4578 rte_eth_dev_rx_intr_enable(uint16_t port_id,
4579                            uint16_t queue_id)
4580 {
4581         struct rte_eth_dev *dev;
4582         int ret;
4583
4584         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4585
4586         dev = &rte_eth_devices[port_id];
4587
4588         ret = eth_dev_validate_rx_queue(dev, queue_id);
4589         if (ret != 0)
4590                 return ret;
4591
4592         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_intr_enable, -ENOTSUP);
4593         return eth_err(port_id, (*dev->dev_ops->rx_queue_intr_enable)(dev,
4594                                                                 queue_id));
4595 }
4596
4597 int
4598 rte_eth_dev_rx_intr_disable(uint16_t port_id,
4599                             uint16_t queue_id)
4600 {
4601         struct rte_eth_dev *dev;
4602         int ret;
4603
4604         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4605
4606         dev = &rte_eth_devices[port_id];
4607
4608         ret = eth_dev_validate_rx_queue(dev, queue_id);
4609         if (ret != 0)
4610                 return ret;
4611
4612         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_intr_disable, -ENOTSUP);
4613         return eth_err(port_id, (*dev->dev_ops->rx_queue_intr_disable)(dev,
4614                                                                 queue_id));
4615 }
4616
4617
4618 int
4619 rte_eth_dev_filter_supported(uint16_t port_id,
4620                              enum rte_filter_type filter_type)
4621 {
4622         struct rte_eth_dev *dev;
4623
4624         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4625
4626         dev = &rte_eth_devices[port_id];
4627         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->filter_ctrl, -ENOTSUP);
4628         return (*dev->dev_ops->filter_ctrl)(dev, filter_type,
4629                                 RTE_ETH_FILTER_NOP, NULL);
4630 }
4631
4632 int
4633 rte_eth_dev_filter_ctrl(uint16_t port_id, enum rte_filter_type filter_type,
4634                         enum rte_filter_op filter_op, void *arg)
4635 {
4636         struct rte_eth_dev *dev;
4637
4638         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4639
4640         dev = &rte_eth_devices[port_id];
4641         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->filter_ctrl, -ENOTSUP);
4642         return eth_err(port_id, (*dev->dev_ops->filter_ctrl)(dev, filter_type,
4643                                                              filter_op, arg));
4644 }
4645
4646 const struct rte_eth_rxtx_callback *
4647 rte_eth_add_rx_callback(uint16_t port_id, uint16_t queue_id,
4648                 rte_rx_callback_fn fn, void *user_param)
4649 {
4650 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
4651         rte_errno = ENOTSUP;
4652         return NULL;
4653 #endif
4654         struct rte_eth_dev *dev;
4655
4656         /* check input parameters */
4657         if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
4658                     queue_id >= rte_eth_devices[port_id].data->nb_rx_queues) {
4659                 rte_errno = EINVAL;
4660                 return NULL;
4661         }
4662         dev = &rte_eth_devices[port_id];
4663         if (rte_eth_dev_is_rx_hairpin_queue(dev, queue_id)) {
4664                 rte_errno = EINVAL;
4665                 return NULL;
4666         }
4667         struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
4668
4669         if (cb == NULL) {
4670                 rte_errno = ENOMEM;
4671                 return NULL;
4672         }
4673
4674         cb->fn.rx = fn;
4675         cb->param = user_param;
4676
4677         rte_spinlock_lock(&rte_eth_rx_cb_lock);
4678         /* Add the callbacks in fifo order. */
4679         struct rte_eth_rxtx_callback *tail =
4680                 rte_eth_devices[port_id].post_rx_burst_cbs[queue_id];
4681
4682         if (!tail) {
4683                 /* Stores to cb->fn and cb->param should complete before
4684                  * cb is visible to data plane.
4685                  */
4686                 __atomic_store_n(
4687                         &rte_eth_devices[port_id].post_rx_burst_cbs[queue_id],
4688                         cb, __ATOMIC_RELEASE);
4689
4690         } else {
4691                 while (tail->next)
4692                         tail = tail->next;
4693                 /* Stores to cb->fn and cb->param should complete before
4694                  * cb is visible to data plane.
4695                  */
4696                 __atomic_store_n(&tail->next, cb, __ATOMIC_RELEASE);
4697         }
4698         rte_spinlock_unlock(&rte_eth_rx_cb_lock);
4699
4700         return cb;
4701 }
4702
4703 const struct rte_eth_rxtx_callback *
4704 rte_eth_add_first_rx_callback(uint16_t port_id, uint16_t queue_id,
4705                 rte_rx_callback_fn fn, void *user_param)
4706 {
4707 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
4708         rte_errno = ENOTSUP;
4709         return NULL;
4710 #endif
4711         /* check input parameters */
4712         if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
4713                 queue_id >= rte_eth_devices[port_id].data->nb_rx_queues) {
4714                 rte_errno = EINVAL;
4715                 return NULL;
4716         }
4717
4718         struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
4719
4720         if (cb == NULL) {
4721                 rte_errno = ENOMEM;
4722                 return NULL;
4723         }
4724
4725         cb->fn.rx = fn;
4726         cb->param = user_param;
4727
4728         rte_spinlock_lock(&rte_eth_rx_cb_lock);
4729         /* Add the callbacks at first position */
4730         cb->next = rte_eth_devices[port_id].post_rx_burst_cbs[queue_id];
4731         /* Stores to cb->fn, cb->param and cb->next should complete before
4732          * cb is visible to data plane threads.
4733          */
4734         __atomic_store_n(
4735                 &rte_eth_devices[port_id].post_rx_burst_cbs[queue_id],
4736                 cb, __ATOMIC_RELEASE);
4737         rte_spinlock_unlock(&rte_eth_rx_cb_lock);
4738
4739         return cb;
4740 }
4741
4742 const struct rte_eth_rxtx_callback *
4743 rte_eth_add_tx_callback(uint16_t port_id, uint16_t queue_id,
4744                 rte_tx_callback_fn fn, void *user_param)
4745 {
4746 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
4747         rte_errno = ENOTSUP;
4748         return NULL;
4749 #endif
4750         struct rte_eth_dev *dev;
4751
4752         /* check input parameters */
4753         if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
4754                     queue_id >= rte_eth_devices[port_id].data->nb_tx_queues) {
4755                 rte_errno = EINVAL;
4756                 return NULL;
4757         }
4758
4759         dev = &rte_eth_devices[port_id];
4760         if (rte_eth_dev_is_tx_hairpin_queue(dev, queue_id)) {
4761                 rte_errno = EINVAL;
4762                 return NULL;
4763         }
4764
4765         struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
4766
4767         if (cb == NULL) {
4768                 rte_errno = ENOMEM;
4769                 return NULL;
4770         }
4771
4772         cb->fn.tx = fn;
4773         cb->param = user_param;
4774
4775         rte_spinlock_lock(&rte_eth_tx_cb_lock);
4776         /* Add the callbacks in fifo order. */
4777         struct rte_eth_rxtx_callback *tail =
4778                 rte_eth_devices[port_id].pre_tx_burst_cbs[queue_id];
4779
4780         if (!tail) {
4781                 /* Stores to cb->fn and cb->param should complete before
4782                  * cb is visible to data plane.
4783                  */
4784                 __atomic_store_n(
4785                         &rte_eth_devices[port_id].pre_tx_burst_cbs[queue_id],
4786                         cb, __ATOMIC_RELEASE);
4787
4788         } else {
4789                 while (tail->next)
4790                         tail = tail->next;
4791                 /* Stores to cb->fn and cb->param should complete before
4792                  * cb is visible to data plane.
4793                  */
4794                 __atomic_store_n(&tail->next, cb, __ATOMIC_RELEASE);
4795         }
4796         rte_spinlock_unlock(&rte_eth_tx_cb_lock);
4797
4798         return cb;
4799 }
4800
4801 int
4802 rte_eth_remove_rx_callback(uint16_t port_id, uint16_t queue_id,
4803                 const struct rte_eth_rxtx_callback *user_cb)
4804 {
4805 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
4806         return -ENOTSUP;
4807 #endif
4808         /* Check input parameters. */
4809         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
4810         if (user_cb == NULL ||
4811                         queue_id >= rte_eth_devices[port_id].data->nb_rx_queues)
4812                 return -EINVAL;
4813
4814         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
4815         struct rte_eth_rxtx_callback *cb;
4816         struct rte_eth_rxtx_callback **prev_cb;
4817         int ret = -EINVAL;
4818
4819         rte_spinlock_lock(&rte_eth_rx_cb_lock);
4820         prev_cb = &dev->post_rx_burst_cbs[queue_id];
4821         for (; *prev_cb != NULL; prev_cb = &cb->next) {
4822                 cb = *prev_cb;
4823                 if (cb == user_cb) {
4824                         /* Remove the user cb from the callback list. */
4825                         __atomic_store_n(prev_cb, cb->next, __ATOMIC_RELAXED);
4826                         ret = 0;
4827                         break;
4828                 }
4829         }
4830         rte_spinlock_unlock(&rte_eth_rx_cb_lock);
4831
4832         return ret;
4833 }
4834
4835 int
4836 rte_eth_remove_tx_callback(uint16_t port_id, uint16_t queue_id,
4837                 const struct rte_eth_rxtx_callback *user_cb)
4838 {
4839 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
4840         return -ENOTSUP;
4841 #endif
4842         /* Check input parameters. */
4843         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
4844         if (user_cb == NULL ||
4845                         queue_id >= rte_eth_devices[port_id].data->nb_tx_queues)
4846                 return -EINVAL;
4847
4848         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
4849         int ret = -EINVAL;
4850         struct rte_eth_rxtx_callback *cb;
4851         struct rte_eth_rxtx_callback **prev_cb;
4852
4853         rte_spinlock_lock(&rte_eth_tx_cb_lock);
4854         prev_cb = &dev->pre_tx_burst_cbs[queue_id];
4855         for (; *prev_cb != NULL; prev_cb = &cb->next) {
4856                 cb = *prev_cb;
4857                 if (cb == user_cb) {
4858                         /* Remove the user cb from the callback list. */
4859                         __atomic_store_n(prev_cb, cb->next, __ATOMIC_RELAXED);
4860                         ret = 0;
4861                         break;
4862                 }
4863         }
4864         rte_spinlock_unlock(&rte_eth_tx_cb_lock);
4865
4866         return ret;
4867 }
4868
4869 int
4870 rte_eth_rx_queue_info_get(uint16_t port_id, uint16_t queue_id,
4871         struct rte_eth_rxq_info *qinfo)
4872 {
4873         struct rte_eth_dev *dev;
4874
4875         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4876
4877         if (qinfo == NULL)
4878                 return -EINVAL;
4879
4880         dev = &rte_eth_devices[port_id];
4881         if (queue_id >= dev->data->nb_rx_queues) {
4882                 RTE_ETHDEV_LOG(ERR, "Invalid RX queue_id=%u\n", queue_id);
4883                 return -EINVAL;
4884         }
4885
4886         if (dev->data->rx_queues == NULL ||
4887                         dev->data->rx_queues[queue_id] == NULL) {
4888                 RTE_ETHDEV_LOG(ERR,
4889                                "Rx queue %"PRIu16" of device with port_id=%"
4890                                PRIu16" has not been setup\n",
4891                                queue_id, port_id);
4892                 return -EINVAL;
4893         }
4894
4895         if (rte_eth_dev_is_rx_hairpin_queue(dev, queue_id)) {
4896                 RTE_ETHDEV_LOG(INFO,
4897                         "Can't get hairpin Rx queue %"PRIu16" info of device with port_id=%"PRIu16"\n",
4898                         queue_id, port_id);
4899                 return -EINVAL;
4900         }
4901
4902         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rxq_info_get, -ENOTSUP);
4903
4904         memset(qinfo, 0, sizeof(*qinfo));
4905         dev->dev_ops->rxq_info_get(dev, queue_id, qinfo);
4906         return 0;
4907 }
4908
4909 int
4910 rte_eth_tx_queue_info_get(uint16_t port_id, uint16_t queue_id,
4911         struct rte_eth_txq_info *qinfo)
4912 {
4913         struct rte_eth_dev *dev;
4914
4915         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4916
4917         if (qinfo == NULL)
4918                 return -EINVAL;
4919
4920         dev = &rte_eth_devices[port_id];
4921         if (queue_id >= dev->data->nb_tx_queues) {
4922                 RTE_ETHDEV_LOG(ERR, "Invalid TX queue_id=%u\n", queue_id);
4923                 return -EINVAL;
4924         }
4925
4926         if (dev->data->tx_queues == NULL ||
4927                         dev->data->tx_queues[queue_id] == NULL) {
4928                 RTE_ETHDEV_LOG(ERR,
4929                                "Tx queue %"PRIu16" of device with port_id=%"
4930                                PRIu16" has not been setup\n",
4931                                queue_id, port_id);
4932                 return -EINVAL;
4933         }
4934
4935         if (rte_eth_dev_is_tx_hairpin_queue(dev, queue_id)) {
4936                 RTE_ETHDEV_LOG(INFO,
4937                         "Can't get hairpin Tx queue %"PRIu16" info of device with port_id=%"PRIu16"\n",
4938                         queue_id, port_id);
4939                 return -EINVAL;
4940         }
4941
4942         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->txq_info_get, -ENOTSUP);
4943
4944         memset(qinfo, 0, sizeof(*qinfo));
4945         dev->dev_ops->txq_info_get(dev, queue_id, qinfo);
4946
4947         return 0;
4948 }
4949
4950 int
4951 rte_eth_rx_burst_mode_get(uint16_t port_id, uint16_t queue_id,
4952                           struct rte_eth_burst_mode *mode)
4953 {
4954         struct rte_eth_dev *dev;
4955
4956         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4957
4958         if (mode == NULL)
4959                 return -EINVAL;
4960
4961         dev = &rte_eth_devices[port_id];
4962
4963         if (queue_id >= dev->data->nb_rx_queues) {
4964                 RTE_ETHDEV_LOG(ERR, "Invalid RX queue_id=%u\n", queue_id);
4965                 return -EINVAL;
4966         }
4967
4968         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_burst_mode_get, -ENOTSUP);
4969         memset(mode, 0, sizeof(*mode));
4970         return eth_err(port_id,
4971                        dev->dev_ops->rx_burst_mode_get(dev, queue_id, mode));
4972 }
4973
4974 int
4975 rte_eth_tx_burst_mode_get(uint16_t port_id, uint16_t queue_id,
4976                           struct rte_eth_burst_mode *mode)
4977 {
4978         struct rte_eth_dev *dev;
4979
4980         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4981
4982         if (mode == NULL)
4983                 return -EINVAL;
4984
4985         dev = &rte_eth_devices[port_id];
4986
4987         if (queue_id >= dev->data->nb_tx_queues) {
4988                 RTE_ETHDEV_LOG(ERR, "Invalid TX queue_id=%u\n", queue_id);
4989                 return -EINVAL;
4990         }
4991
4992         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_burst_mode_get, -ENOTSUP);
4993         memset(mode, 0, sizeof(*mode));
4994         return eth_err(port_id,
4995                        dev->dev_ops->tx_burst_mode_get(dev, queue_id, mode));
4996 }
4997
4998 int
4999 rte_eth_dev_set_mc_addr_list(uint16_t port_id,
5000                              struct rte_ether_addr *mc_addr_set,
5001                              uint32_t nb_mc_addr)
5002 {
5003         struct rte_eth_dev *dev;
5004
5005         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
5006
5007         dev = &rte_eth_devices[port_id];
5008         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_mc_addr_list, -ENOTSUP);
5009         return eth_err(port_id, dev->dev_ops->set_mc_addr_list(dev,
5010                                                 mc_addr_set, nb_mc_addr));
5011 }
5012
5013 int
5014 rte_eth_timesync_enable(uint16_t port_id)
5015 {
5016         struct rte_eth_dev *dev;
5017
5018         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
5019         dev = &rte_eth_devices[port_id];
5020
5021         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_enable, -ENOTSUP);
5022         return eth_err(port_id, (*dev->dev_ops->timesync_enable)(dev));
5023 }
5024
5025 int
5026 rte_eth_timesync_disable(uint16_t port_id)
5027 {
5028         struct rte_eth_dev *dev;
5029
5030         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
5031         dev = &rte_eth_devices[port_id];
5032
5033         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_disable, -ENOTSUP);
5034         return eth_err(port_id, (*dev->dev_ops->timesync_disable)(dev));
5035 }
5036
5037 int
5038 rte_eth_timesync_read_rx_timestamp(uint16_t port_id, struct timespec *timestamp,
5039                                    uint32_t flags)
5040 {
5041         struct rte_eth_dev *dev;
5042
5043         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
5044         dev = &rte_eth_devices[port_id];
5045
5046         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_rx_timestamp, -ENOTSUP);
5047         return eth_err(port_id, (*dev->dev_ops->timesync_read_rx_timestamp)
5048                                 (dev, timestamp, flags));
5049 }
5050
5051 int
5052 rte_eth_timesync_read_tx_timestamp(uint16_t port_id,
5053                                    struct timespec *timestamp)
5054 {
5055         struct rte_eth_dev *dev;
5056
5057         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
5058         dev = &rte_eth_devices[port_id];
5059
5060         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_tx_timestamp, -ENOTSUP);
5061         return eth_err(port_id, (*dev->dev_ops->timesync_read_tx_timestamp)
5062                                 (dev, timestamp));
5063 }
5064
5065 int
5066 rte_eth_timesync_adjust_time(uint16_t port_id, int64_t delta)
5067 {
5068         struct rte_eth_dev *dev;
5069
5070         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
5071         dev = &rte_eth_devices[port_id];
5072
5073         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_adjust_time, -ENOTSUP);
5074         return eth_err(port_id, (*dev->dev_ops->timesync_adjust_time)(dev,
5075                                                                       delta));
5076 }
5077
5078 int
5079 rte_eth_timesync_read_time(uint16_t port_id, struct timespec *timestamp)
5080 {
5081         struct rte_eth_dev *dev;
5082
5083         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
5084         dev = &rte_eth_devices[port_id];
5085
5086         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_time, -ENOTSUP);
5087         return eth_err(port_id, (*dev->dev_ops->timesync_read_time)(dev,
5088                                                                 timestamp));
5089 }
5090
5091 int
5092 rte_eth_timesync_write_time(uint16_t port_id, const struct timespec *timestamp)
5093 {
5094         struct rte_eth_dev *dev;
5095
5096         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
5097         dev = &rte_eth_devices[port_id];
5098
5099         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_write_time, -ENOTSUP);
5100         return eth_err(port_id, (*dev->dev_ops->timesync_write_time)(dev,
5101                                                                 timestamp));
5102 }
5103
5104 int
5105 rte_eth_read_clock(uint16_t port_id, uint64_t *clock)
5106 {
5107         struct rte_eth_dev *dev;
5108
5109         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
5110         dev = &rte_eth_devices[port_id];
5111
5112         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->read_clock, -ENOTSUP);
5113         return eth_err(port_id, (*dev->dev_ops->read_clock)(dev, clock));
5114 }
5115
5116 int
5117 rte_eth_dev_get_reg_info(uint16_t port_id, struct rte_dev_reg_info *info)
5118 {
5119         struct rte_eth_dev *dev;
5120
5121         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
5122
5123         dev = &rte_eth_devices[port_id];
5124         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_reg, -ENOTSUP);
5125         return eth_err(port_id, (*dev->dev_ops->get_reg)(dev, info));
5126 }
5127
5128 int
5129 rte_eth_dev_get_eeprom_length(uint16_t port_id)
5130 {
5131         struct rte_eth_dev *dev;
5132
5133         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
5134
5135         dev = &rte_eth_devices[port_id];
5136         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_eeprom_length, -ENOTSUP);
5137         return eth_err(port_id, (*dev->dev_ops->get_eeprom_length)(dev));
5138 }
5139
5140 int
5141 rte_eth_dev_get_eeprom(uint16_t port_id, struct rte_dev_eeprom_info *info)
5142 {
5143         struct rte_eth_dev *dev;
5144
5145         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
5146
5147         dev = &rte_eth_devices[port_id];
5148         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_eeprom, -ENOTSUP);
5149         return eth_err(port_id, (*dev->dev_ops->get_eeprom)(dev, info));
5150 }
5151
5152 int
5153 rte_eth_dev_set_eeprom(uint16_t port_id, struct rte_dev_eeprom_info *info)
5154 {
5155         struct rte_eth_dev *dev;
5156
5157         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
5158
5159         dev = &rte_eth_devices[port_id];
5160         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_eeprom, -ENOTSUP);
5161         return eth_err(port_id, (*dev->dev_ops->set_eeprom)(dev, info));
5162 }
5163
5164 int
5165 rte_eth_dev_get_module_info(uint16_t port_id,
5166                             struct rte_eth_dev_module_info *modinfo)
5167 {
5168         struct rte_eth_dev *dev;
5169
5170         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
5171
5172         dev = &rte_eth_devices[port_id];
5173         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_module_info, -ENOTSUP);
5174         return (*dev->dev_ops->get_module_info)(dev, modinfo);
5175 }
5176
5177 int
5178 rte_eth_dev_get_module_eeprom(uint16_t port_id,
5179                               struct rte_dev_eeprom_info *info)
5180 {
5181         struct rte_eth_dev *dev;
5182
5183         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
5184
5185         dev = &rte_eth_devices[port_id];
5186         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_module_eeprom, -ENOTSUP);
5187         return (*dev->dev_ops->get_module_eeprom)(dev, info);
5188 }
5189
5190 int
5191 rte_eth_dev_get_dcb_info(uint16_t port_id,
5192                              struct rte_eth_dcb_info *dcb_info)
5193 {
5194         struct rte_eth_dev *dev;
5195
5196         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
5197
5198         dev = &rte_eth_devices[port_id];
5199         memset(dcb_info, 0, sizeof(struct rte_eth_dcb_info));
5200
5201         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_dcb_info, -ENOTSUP);
5202         return eth_err(port_id, (*dev->dev_ops->get_dcb_info)(dev, dcb_info));
5203 }
5204
5205 int
5206 rte_eth_dev_l2_tunnel_eth_type_conf(uint16_t port_id,
5207                                     struct rte_eth_l2_tunnel_conf *l2_tunnel)
5208 {
5209         struct rte_eth_dev *dev;
5210
5211         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
5212         if (l2_tunnel == NULL) {
5213                 RTE_ETHDEV_LOG(ERR, "Invalid l2_tunnel parameter\n");
5214                 return -EINVAL;
5215         }
5216
5217         if (l2_tunnel->l2_tunnel_type >= RTE_TUNNEL_TYPE_MAX) {
5218                 RTE_ETHDEV_LOG(ERR, "Invalid tunnel type\n");
5219                 return -EINVAL;
5220         }
5221
5222         dev = &rte_eth_devices[port_id];
5223         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->l2_tunnel_eth_type_conf,
5224                                 -ENOTSUP);
5225         return eth_err(port_id, (*dev->dev_ops->l2_tunnel_eth_type_conf)(dev,
5226                                                                 l2_tunnel));
5227 }
5228
5229 int
5230 rte_eth_dev_l2_tunnel_offload_set(uint16_t port_id,
5231                                   struct rte_eth_l2_tunnel_conf *l2_tunnel,
5232                                   uint32_t mask,
5233                                   uint8_t en)
5234 {
5235         struct rte_eth_dev *dev;
5236
5237         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
5238
5239         if (l2_tunnel == NULL) {
5240                 RTE_ETHDEV_LOG(ERR, "Invalid l2_tunnel parameter\n");
5241                 return -EINVAL;
5242         }
5243
5244         if (l2_tunnel->l2_tunnel_type >= RTE_TUNNEL_TYPE_MAX) {
5245                 RTE_ETHDEV_LOG(ERR, "Invalid tunnel type\n");
5246                 return -EINVAL;
5247         }
5248
5249         if (mask == 0) {
5250                 RTE_ETHDEV_LOG(ERR, "Mask should have a value\n");
5251                 return -EINVAL;
5252         }
5253
5254         dev = &rte_eth_devices[port_id];
5255         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->l2_tunnel_offload_set,
5256                                 -ENOTSUP);
5257         return eth_err(port_id, (*dev->dev_ops->l2_tunnel_offload_set)(dev,
5258                                                         l2_tunnel, mask, en));
5259 }
5260
5261 static void
5262 rte_eth_dev_adjust_nb_desc(uint16_t *nb_desc,
5263                            const struct rte_eth_desc_lim *desc_lim)
5264 {
5265         if (desc_lim->nb_align != 0)
5266                 *nb_desc = RTE_ALIGN_CEIL(*nb_desc, desc_lim->nb_align);
5267
5268         if (desc_lim->nb_max != 0)
5269                 *nb_desc = RTE_MIN(*nb_desc, desc_lim->nb_max);
5270
5271         *nb_desc = RTE_MAX(*nb_desc, desc_lim->nb_min);
5272 }
5273
5274 int
5275 rte_eth_dev_adjust_nb_rx_tx_desc(uint16_t port_id,
5276                                  uint16_t *nb_rx_desc,
5277                                  uint16_t *nb_tx_desc)
5278 {
5279         struct rte_eth_dev_info dev_info;
5280         int ret;
5281
5282         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
5283
5284         ret = rte_eth_dev_info_get(port_id, &dev_info);
5285         if (ret != 0)
5286                 return ret;
5287
5288         if (nb_rx_desc != NULL)
5289                 rte_eth_dev_adjust_nb_desc(nb_rx_desc, &dev_info.rx_desc_lim);
5290
5291         if (nb_tx_desc != NULL)
5292                 rte_eth_dev_adjust_nb_desc(nb_tx_desc, &dev_info.tx_desc_lim);
5293
5294         return 0;
5295 }
5296
5297 int
5298 rte_eth_dev_hairpin_capability_get(uint16_t port_id,
5299                                    struct rte_eth_hairpin_cap *cap)
5300 {
5301         struct rte_eth_dev *dev;
5302
5303         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
5304
5305         dev = &rte_eth_devices[port_id];
5306         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->hairpin_cap_get, -ENOTSUP);
5307         memset(cap, 0, sizeof(*cap));
5308         return eth_err(port_id, (*dev->dev_ops->hairpin_cap_get)(dev, cap));
5309 }
5310
5311 int
5312 rte_eth_dev_is_rx_hairpin_queue(struct rte_eth_dev *dev, uint16_t queue_id)
5313 {
5314         if (dev->data->rx_queue_state[queue_id] ==
5315             RTE_ETH_QUEUE_STATE_HAIRPIN)
5316                 return 1;
5317         return 0;
5318 }
5319
5320 int
5321 rte_eth_dev_is_tx_hairpin_queue(struct rte_eth_dev *dev, uint16_t queue_id)
5322 {
5323         if (dev->data->tx_queue_state[queue_id] ==
5324             RTE_ETH_QUEUE_STATE_HAIRPIN)
5325                 return 1;
5326         return 0;
5327 }
5328
5329 int
5330 rte_eth_dev_pool_ops_supported(uint16_t port_id, const char *pool)
5331 {
5332         struct rte_eth_dev *dev;
5333
5334         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
5335
5336         if (pool == NULL)
5337                 return -EINVAL;
5338
5339         dev = &rte_eth_devices[port_id];
5340
5341         if (*dev->dev_ops->pool_ops_supported == NULL)
5342                 return 1; /* all pools are supported */
5343
5344         return (*dev->dev_ops->pool_ops_supported)(dev, pool);
5345 }
5346
5347 /**
5348  * A set of values to describe the possible states of a switch domain.
5349  */
5350 enum rte_eth_switch_domain_state {
5351         RTE_ETH_SWITCH_DOMAIN_UNUSED = 0,
5352         RTE_ETH_SWITCH_DOMAIN_ALLOCATED
5353 };
5354
5355 /**
5356  * Array of switch domains available for allocation. Array is sized to
5357  * RTE_MAX_ETHPORTS elements as there cannot be more active switch domains than
5358  * ethdev ports in a single process.
5359  */
5360 static struct rte_eth_dev_switch {
5361         enum rte_eth_switch_domain_state state;
5362 } rte_eth_switch_domains[RTE_MAX_ETHPORTS];
5363
5364 int
5365 rte_eth_switch_domain_alloc(uint16_t *domain_id)
5366 {
5367         unsigned int i;
5368
5369         *domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
5370
5371         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
5372                 if (rte_eth_switch_domains[i].state ==
5373                         RTE_ETH_SWITCH_DOMAIN_UNUSED) {
5374                         rte_eth_switch_domains[i].state =
5375                                 RTE_ETH_SWITCH_DOMAIN_ALLOCATED;
5376                         *domain_id = i;
5377                         return 0;
5378                 }
5379         }
5380
5381         return -ENOSPC;
5382 }
5383
5384 int
5385 rte_eth_switch_domain_free(uint16_t domain_id)
5386 {
5387         if (domain_id == RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID ||
5388                 domain_id >= RTE_MAX_ETHPORTS)
5389                 return -EINVAL;
5390
5391         if (rte_eth_switch_domains[domain_id].state !=
5392                 RTE_ETH_SWITCH_DOMAIN_ALLOCATED)
5393                 return -EINVAL;
5394
5395         rte_eth_switch_domains[domain_id].state = RTE_ETH_SWITCH_DOMAIN_UNUSED;
5396
5397         return 0;
5398 }
5399
5400 static int
5401 rte_eth_devargs_tokenise(struct rte_kvargs *arglist, const char *str_in)
5402 {
5403         int state;
5404         struct rte_kvargs_pair *pair;
5405         char *letter;
5406
5407         arglist->str = strdup(str_in);
5408         if (arglist->str == NULL)
5409                 return -ENOMEM;
5410
5411         letter = arglist->str;
5412         state = 0;
5413         arglist->count = 0;
5414         pair = &arglist->pairs[0];
5415         while (1) {
5416                 switch (state) {
5417                 case 0: /* Initial */
5418                         if (*letter == '=')
5419                                 return -EINVAL;
5420                         else if (*letter == '\0')
5421                                 return 0;
5422
5423                         state = 1;
5424                         pair->key = letter;
5425                         /* fall-thru */
5426
5427                 case 1: /* Parsing key */
5428                         if (*letter == '=') {
5429                                 *letter = '\0';
5430                                 pair->value = letter + 1;
5431                                 state = 2;
5432                         } else if (*letter == ',' || *letter == '\0')
5433                                 return -EINVAL;
5434                         break;
5435
5436
5437                 case 2: /* Parsing value */
5438                         if (*letter == '[')
5439                                 state = 3;
5440                         else if (*letter == ',') {
5441                                 *letter = '\0';
5442                                 arglist->count++;
5443                                 pair = &arglist->pairs[arglist->count];
5444                                 state = 0;
5445                         } else if (*letter == '\0') {
5446                                 letter--;
5447                                 arglist->count++;
5448                                 pair = &arglist->pairs[arglist->count];
5449                                 state = 0;
5450                         }
5451                         break;
5452
5453                 case 3: /* Parsing list */
5454                         if (*letter == ']')
5455                                 state = 2;
5456                         else if (*letter == '\0')
5457                                 return -EINVAL;
5458                         break;
5459                 }
5460                 letter++;
5461         }
5462 }
5463
5464 int
5465 rte_eth_devargs_parse(const char *dargs, struct rte_eth_devargs *eth_da)
5466 {
5467         struct rte_kvargs args;
5468         struct rte_kvargs_pair *pair;
5469         unsigned int i;
5470         int result = 0;
5471
5472         memset(eth_da, 0, sizeof(*eth_da));
5473
5474         result = rte_eth_devargs_tokenise(&args, dargs);
5475         if (result < 0)
5476                 goto parse_cleanup;
5477
5478         for (i = 0; i < args.count; i++) {
5479                 pair = &args.pairs[i];
5480                 if (strcmp("representor", pair->key) == 0) {
5481                         result = rte_eth_devargs_parse_list(pair->value,
5482                                 rte_eth_devargs_parse_representor_ports,
5483                                 eth_da);
5484                         if (result < 0)
5485                                 goto parse_cleanup;
5486                 }
5487         }
5488
5489 parse_cleanup:
5490         if (args.str)
5491                 free(args.str);
5492
5493         return result;
5494 }
5495
5496 static int
5497 handle_port_list(const char *cmd __rte_unused,
5498                 const char *params __rte_unused,
5499                 struct rte_tel_data *d)
5500 {
5501         int port_id;
5502
5503         rte_tel_data_start_array(d, RTE_TEL_INT_VAL);
5504         RTE_ETH_FOREACH_DEV(port_id)
5505                 rte_tel_data_add_array_int(d, port_id);
5506         return 0;
5507 }
5508
5509 static void
5510 add_port_queue_stats(struct rte_tel_data *d, uint64_t *q_stats,
5511                 const char *stat_name)
5512 {
5513         int q;
5514         struct rte_tel_data *q_data = rte_tel_data_alloc();
5515         rte_tel_data_start_array(q_data, RTE_TEL_U64_VAL);
5516         for (q = 0; q < RTE_ETHDEV_QUEUE_STAT_CNTRS; q++)
5517                 rte_tel_data_add_array_u64(q_data, q_stats[q]);
5518         rte_tel_data_add_dict_container(d, stat_name, q_data, 0);
5519 }
5520
5521 #define ADD_DICT_STAT(stats, s) rte_tel_data_add_dict_u64(d, #s, stats.s)
5522
5523 static int
5524 handle_port_stats(const char *cmd __rte_unused,
5525                 const char *params,
5526                 struct rte_tel_data *d)
5527 {
5528         struct rte_eth_stats stats;
5529         int port_id, ret;
5530
5531         if (params == NULL || strlen(params) == 0 || !isdigit(*params))
5532                 return -1;
5533
5534         port_id = atoi(params);
5535         if (!rte_eth_dev_is_valid_port(port_id))
5536                 return -1;
5537
5538         ret = rte_eth_stats_get(port_id, &stats);
5539         if (ret < 0)
5540                 return -1;
5541
5542         rte_tel_data_start_dict(d);
5543         ADD_DICT_STAT(stats, ipackets);
5544         ADD_DICT_STAT(stats, opackets);
5545         ADD_DICT_STAT(stats, ibytes);
5546         ADD_DICT_STAT(stats, obytes);
5547         ADD_DICT_STAT(stats, imissed);
5548         ADD_DICT_STAT(stats, ierrors);
5549         ADD_DICT_STAT(stats, oerrors);
5550         ADD_DICT_STAT(stats, rx_nombuf);
5551         add_port_queue_stats(d, stats.q_ipackets, "q_ipackets");
5552         add_port_queue_stats(d, stats.q_opackets, "q_opackets");
5553         add_port_queue_stats(d, stats.q_ibytes, "q_ibytes");
5554         add_port_queue_stats(d, stats.q_obytes, "q_obytes");
5555         add_port_queue_stats(d, stats.q_errors, "q_errors");
5556
5557         return 0;
5558 }
5559
5560 static int
5561 handle_port_xstats(const char *cmd __rte_unused,
5562                 const char *params,
5563                 struct rte_tel_data *d)
5564 {
5565         struct rte_eth_xstat *eth_xstats;
5566         struct rte_eth_xstat_name *xstat_names;
5567         int port_id, num_xstats;
5568         int i, ret;
5569         char *end_param;
5570
5571         if (params == NULL || strlen(params) == 0 || !isdigit(*params))
5572                 return -1;
5573
5574         port_id = strtoul(params, &end_param, 0);
5575         if (*end_param != '\0')
5576                 RTE_ETHDEV_LOG(NOTICE,
5577                         "Extra parameters passed to ethdev telemetry command, ignoring");
5578         if (!rte_eth_dev_is_valid_port(port_id))
5579                 return -1;
5580
5581         num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
5582         if (num_xstats < 0)
5583                 return -1;
5584
5585         /* use one malloc for both names and stats */
5586         eth_xstats = malloc((sizeof(struct rte_eth_xstat) +
5587                         sizeof(struct rte_eth_xstat_name)) * num_xstats);
5588         if (eth_xstats == NULL)
5589                 return -1;
5590         xstat_names = (void *)&eth_xstats[num_xstats];
5591
5592         ret = rte_eth_xstats_get_names(port_id, xstat_names, num_xstats);
5593         if (ret < 0 || ret > num_xstats) {
5594                 free(eth_xstats);
5595                 return -1;
5596         }
5597
5598         ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats);
5599         if (ret < 0 || ret > num_xstats) {
5600                 free(eth_xstats);
5601                 return -1;
5602         }
5603
5604         rte_tel_data_start_dict(d);
5605         for (i = 0; i < num_xstats; i++)
5606                 rte_tel_data_add_dict_u64(d, xstat_names[i].name,
5607                                 eth_xstats[i].value);
5608         return 0;
5609 }
5610
5611 static int
5612 handle_port_link_status(const char *cmd __rte_unused,
5613                 const char *params,
5614                 struct rte_tel_data *d)
5615 {
5616         static const char *status_str = "status";
5617         int ret, port_id;
5618         struct rte_eth_link link;
5619         char *end_param;
5620
5621         if (params == NULL || strlen(params) == 0 || !isdigit(*params))
5622                 return -1;
5623
5624         port_id = strtoul(params, &end_param, 0);
5625         if (*end_param != '\0')
5626                 RTE_ETHDEV_LOG(NOTICE,
5627                         "Extra parameters passed to ethdev telemetry command, ignoring");
5628         if (!rte_eth_dev_is_valid_port(port_id))
5629                 return -1;
5630
5631         ret = rte_eth_link_get(port_id, &link);
5632         if (ret < 0)
5633                 return -1;
5634
5635         rte_tel_data_start_dict(d);
5636         if (!link.link_status) {
5637                 rte_tel_data_add_dict_string(d, status_str, "DOWN");
5638                 return 0;
5639         }
5640         rte_tel_data_add_dict_string(d, status_str, "UP");
5641         rte_tel_data_add_dict_u64(d, "speed", link.link_speed);
5642         rte_tel_data_add_dict_string(d, "duplex",
5643                         (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
5644                                 "full-duplex" : "half-duplex");
5645         return 0;
5646 }
5647
5648 RTE_LOG_REGISTER(rte_eth_dev_logtype, lib.ethdev, INFO);
5649
5650 RTE_INIT(ethdev_init_telemetry)
5651 {
5652         rte_telemetry_register_cmd("/ethdev/list", handle_port_list,
5653                         "Returns list of available ethdev ports. Takes no parameters");
5654         rte_telemetry_register_cmd("/ethdev/stats", handle_port_stats,
5655                         "Returns the common stats for a port. Parameters: int port_id");
5656         rte_telemetry_register_cmd("/ethdev/xstats", handle_port_xstats,
5657                         "Returns the extended stats for a port. Parameters: int port_id");
5658         rte_telemetry_register_cmd("/ethdev/link_status",
5659                         handle_port_link_status,
5660                         "Returns the link status for a port. Parameters: int port_id");
5661 }