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