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