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