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