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