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