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