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