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