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