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