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