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