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