ethdev: add return code to stats reset function
[dpdk.git] / lib / librte_ether / rte_ethdev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <sys/types.h>
35 #include <sys/queue.h>
36 #include <ctype.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <stdarg.h>
41 #include <errno.h>
42 #include <stdint.h>
43 #include <inttypes.h>
44 #include <netinet/in.h>
45
46 #include <rte_byteorder.h>
47 #include <rte_log.h>
48 #include <rte_debug.h>
49 #include <rte_interrupts.h>
50 #include <rte_pci.h>
51 #include <rte_memory.h>
52 #include <rte_memcpy.h>
53 #include <rte_memzone.h>
54 #include <rte_launch.h>
55 #include <rte_eal.h>
56 #include <rte_per_lcore.h>
57 #include <rte_lcore.h>
58 #include <rte_atomic.h>
59 #include <rte_branch_prediction.h>
60 #include <rte_common.h>
61 #include <rte_mempool.h>
62 #include <rte_malloc.h>
63 #include <rte_mbuf.h>
64 #include <rte_errno.h>
65 #include <rte_spinlock.h>
66 #include <rte_string_fns.h>
67
68 #include "rte_ether.h"
69 #include "rte_ethdev.h"
70 #include "ethdev_profile.h"
71
72 static const char *MZ_RTE_ETH_DEV_DATA = "rte_eth_dev_data";
73 struct rte_eth_dev rte_eth_devices[RTE_MAX_ETHPORTS];
74 static struct rte_eth_dev_data *rte_eth_dev_data;
75 static uint8_t eth_dev_last_created_port;
76
77 /* spinlock for eth device callbacks */
78 static rte_spinlock_t rte_eth_dev_cb_lock = RTE_SPINLOCK_INITIALIZER;
79
80 /* spinlock for add/remove rx callbacks */
81 static rte_spinlock_t rte_eth_rx_cb_lock = RTE_SPINLOCK_INITIALIZER;
82
83 /* spinlock for add/remove tx callbacks */
84 static rte_spinlock_t rte_eth_tx_cb_lock = RTE_SPINLOCK_INITIALIZER;
85
86 /* store statistics names and its offset in stats structure  */
87 struct rte_eth_xstats_name_off {
88         char name[RTE_ETH_XSTATS_NAME_SIZE];
89         unsigned offset;
90 };
91
92 static const struct rte_eth_xstats_name_off rte_stats_strings[] = {
93         {"rx_good_packets", offsetof(struct rte_eth_stats, ipackets)},
94         {"tx_good_packets", offsetof(struct rte_eth_stats, opackets)},
95         {"rx_good_bytes", offsetof(struct rte_eth_stats, ibytes)},
96         {"tx_good_bytes", offsetof(struct rte_eth_stats, obytes)},
97         {"rx_errors", offsetof(struct rte_eth_stats, ierrors)},
98         {"tx_errors", offsetof(struct rte_eth_stats, oerrors)},
99         {"rx_mbuf_allocation_errors", offsetof(struct rte_eth_stats,
100                 rx_nombuf)},
101 };
102
103 #define RTE_NB_STATS (sizeof(rte_stats_strings) / sizeof(rte_stats_strings[0]))
104
105 static const struct rte_eth_xstats_name_off rte_rxq_stats_strings[] = {
106         {"packets", offsetof(struct rte_eth_stats, q_ipackets)},
107         {"bytes", offsetof(struct rte_eth_stats, q_ibytes)},
108         {"errors", offsetof(struct rte_eth_stats, q_errors)},
109 };
110
111 #define RTE_NB_RXQ_STATS (sizeof(rte_rxq_stats_strings) /       \
112                 sizeof(rte_rxq_stats_strings[0]))
113
114 static const struct rte_eth_xstats_name_off rte_txq_stats_strings[] = {
115         {"packets", offsetof(struct rte_eth_stats, q_opackets)},
116         {"bytes", offsetof(struct rte_eth_stats, q_obytes)},
117 };
118 #define RTE_NB_TXQ_STATS (sizeof(rte_txq_stats_strings) /       \
119                 sizeof(rte_txq_stats_strings[0]))
120
121
122 /**
123  * The user application callback description.
124  *
125  * It contains callback address to be registered by user application,
126  * the pointer to the parameters for callback, and the event type.
127  */
128 struct rte_eth_dev_callback {
129         TAILQ_ENTRY(rte_eth_dev_callback) next; /**< Callbacks list */
130         rte_eth_dev_cb_fn cb_fn;                /**< Callback address */
131         void *cb_arg;                           /**< Parameter for callback */
132         void *ret_param;                        /**< Return parameter */
133         enum rte_eth_event_type event;          /**< Interrupt event type */
134         uint32_t active;                        /**< Callback is executing */
135 };
136
137 enum {
138         STAT_QMAP_TX = 0,
139         STAT_QMAP_RX
140 };
141
142 uint16_t
143 rte_eth_find_next(uint16_t port_id)
144 {
145         while (port_id < RTE_MAX_ETHPORTS &&
146                rte_eth_devices[port_id].state != RTE_ETH_DEV_ATTACHED)
147                 port_id++;
148
149         if (port_id >= RTE_MAX_ETHPORTS)
150                 return RTE_MAX_ETHPORTS;
151
152         return port_id;
153 }
154
155 static void
156 rte_eth_dev_data_alloc(void)
157 {
158         const unsigned flags = 0;
159         const struct rte_memzone *mz;
160
161         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
162                 mz = rte_memzone_reserve(MZ_RTE_ETH_DEV_DATA,
163                                 RTE_MAX_ETHPORTS * sizeof(*rte_eth_dev_data),
164                                 rte_socket_id(), flags);
165         } else
166                 mz = rte_memzone_lookup(MZ_RTE_ETH_DEV_DATA);
167         if (mz == NULL)
168                 rte_panic("Cannot allocate memzone for ethernet port data\n");
169
170         rte_eth_dev_data = mz->addr;
171         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
172                 memset(rte_eth_dev_data, 0,
173                                 RTE_MAX_ETHPORTS * sizeof(*rte_eth_dev_data));
174 }
175
176 struct rte_eth_dev *
177 rte_eth_dev_allocated(const char *name)
178 {
179         unsigned i;
180
181         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
182                 if (rte_eth_devices[i].state == RTE_ETH_DEV_ATTACHED &&
183                                 rte_eth_devices[i].device) {
184                         if (!strcmp(rte_eth_devices[i].device->name, name))
185                                 return &rte_eth_devices[i];
186                 }
187         }
188         return NULL;
189 }
190
191 static uint16_t
192 rte_eth_dev_find_free_port(void)
193 {
194         unsigned i;
195
196         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
197                 if (rte_eth_devices[i].state == RTE_ETH_DEV_UNUSED)
198                         return i;
199         }
200         return RTE_MAX_ETHPORTS;
201 }
202
203 static struct rte_eth_dev *
204 eth_dev_get(uint16_t port_id)
205 {
206         struct rte_eth_dev *eth_dev = &rte_eth_devices[port_id];
207
208         eth_dev->data = &rte_eth_dev_data[port_id];
209         eth_dev->state = RTE_ETH_DEV_ATTACHED;
210         TAILQ_INIT(&(eth_dev->link_intr_cbs));
211
212         eth_dev_last_created_port = port_id;
213
214         return eth_dev;
215 }
216
217 struct rte_eth_dev *
218 rte_eth_dev_allocate(const char *name)
219 {
220         uint16_t port_id;
221         struct rte_eth_dev *eth_dev;
222
223         port_id = rte_eth_dev_find_free_port();
224         if (port_id == RTE_MAX_ETHPORTS) {
225                 RTE_PMD_DEBUG_TRACE("Reached maximum number of Ethernet ports\n");
226                 return NULL;
227         }
228
229         if (rte_eth_dev_data == NULL)
230                 rte_eth_dev_data_alloc();
231
232         if (rte_eth_dev_allocated(name) != NULL) {
233                 RTE_PMD_DEBUG_TRACE("Ethernet Device with name %s already allocated!\n",
234                                 name);
235                 return NULL;
236         }
237
238         memset(&rte_eth_dev_data[port_id], 0, sizeof(struct rte_eth_dev_data));
239         eth_dev = eth_dev_get(port_id);
240         snprintf(eth_dev->data->name, sizeof(eth_dev->data->name), "%s", name);
241         eth_dev->data->port_id = port_id;
242         eth_dev->data->mtu = ETHER_MTU;
243
244         return eth_dev;
245 }
246
247 /*
248  * Attach to a port already registered by the primary process, which
249  * makes sure that the same device would have the same port id both
250  * in the primary and secondary process.
251  */
252 struct rte_eth_dev *
253 rte_eth_dev_attach_secondary(const char *name)
254 {
255         uint16_t i;
256         struct rte_eth_dev *eth_dev;
257
258         if (rte_eth_dev_data == NULL)
259                 rte_eth_dev_data_alloc();
260
261         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
262                 if (strcmp(rte_eth_dev_data[i].name, name) == 0)
263                         break;
264         }
265         if (i == RTE_MAX_ETHPORTS) {
266                 RTE_PMD_DEBUG_TRACE(
267                         "device %s is not driven by the primary process\n",
268                         name);
269                 return NULL;
270         }
271
272         eth_dev = eth_dev_get(i);
273         RTE_ASSERT(eth_dev->data->port_id == i);
274
275         return eth_dev;
276 }
277
278 int
279 rte_eth_dev_release_port(struct rte_eth_dev *eth_dev)
280 {
281         if (eth_dev == NULL)
282                 return -EINVAL;
283
284         eth_dev->state = RTE_ETH_DEV_UNUSED;
285         return 0;
286 }
287
288 int
289 rte_eth_dev_is_valid_port(uint16_t port_id)
290 {
291         if (port_id >= RTE_MAX_ETHPORTS ||
292             (rte_eth_devices[port_id].state != RTE_ETH_DEV_ATTACHED &&
293              rte_eth_devices[port_id].state != RTE_ETH_DEV_DEFERRED))
294                 return 0;
295         else
296                 return 1;
297 }
298
299 int
300 rte_eth_dev_socket_id(uint16_t port_id)
301 {
302         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -1);
303         return rte_eth_devices[port_id].data->numa_node;
304 }
305
306 uint16_t
307 rte_eth_dev_count(void)
308 {
309         uint16_t p;
310         uint16_t count;
311
312         count = 0;
313
314         RTE_ETH_FOREACH_DEV(p)
315                 count++;
316
317         return count;
318 }
319
320 int
321 rte_eth_dev_get_name_by_port(uint16_t port_id, char *name)
322 {
323         const char *tmp;
324
325         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
326
327         if (name == NULL) {
328                 RTE_PMD_DEBUG_TRACE("Null pointer is specified\n");
329                 return -EINVAL;
330         }
331
332         /* shouldn't check 'rte_eth_devices[i].data',
333          * because it might be overwritten by VDEV PMD */
334         tmp = rte_eth_devices[port_id].device->name;
335         strcpy(name, tmp);
336         return 0;
337 }
338
339 int
340 rte_eth_dev_get_port_by_name(const char *name, uint16_t *port_id)
341 {
342         int ret;
343         int i;
344
345         if (name == NULL) {
346                 RTE_PMD_DEBUG_TRACE("Null pointer is specified\n");
347                 return -EINVAL;
348         }
349
350         RTE_ETH_FOREACH_DEV(i) {
351                 if (!rte_eth_devices[i].device)
352                         continue;
353
354                 ret = strncmp(name, rte_eth_devices[i].device->name,
355                                 strlen(name));
356                 if (ret == 0) {
357                         *port_id = i;
358                         return 0;
359                 }
360         }
361         return -ENODEV;
362 }
363
364 static int
365 rte_eth_dev_is_detachable(uint16_t port_id)
366 {
367         uint32_t dev_flags;
368
369         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
370
371         dev_flags = rte_eth_devices[port_id].data->dev_flags;
372         if ((dev_flags & RTE_ETH_DEV_DETACHABLE) &&
373                 (!(dev_flags & RTE_ETH_DEV_BONDED_SLAVE)))
374                 return 0;
375         else
376                 return 1;
377 }
378
379 /* attach the new device, then store port_id of the device */
380 int
381 rte_eth_dev_attach(const char *devargs, uint16_t *port_id)
382 {
383         int ret = -1;
384         int current = rte_eth_dev_count();
385         char *name = NULL;
386         char *args = NULL;
387
388         if ((devargs == NULL) || (port_id == NULL)) {
389                 ret = -EINVAL;
390                 goto err;
391         }
392
393         /* parse devargs, then retrieve device name and args */
394         if (rte_eal_parse_devargs_str(devargs, &name, &args))
395                 goto err;
396
397         ret = rte_eal_dev_attach(name, args);
398         if (ret < 0)
399                 goto err;
400
401         /* no point looking at the port count if no port exists */
402         if (!rte_eth_dev_count()) {
403                 RTE_LOG(ERR, EAL, "No port found for device (%s)\n", name);
404                 ret = -1;
405                 goto err;
406         }
407
408         /* if nothing happened, there is a bug here, since some driver told us
409          * it did attach a device, but did not create a port.
410          */
411         if (current == rte_eth_dev_count()) {
412                 ret = -1;
413                 goto err;
414         }
415
416         *port_id = eth_dev_last_created_port;
417         ret = 0;
418
419 err:
420         free(name);
421         free(args);
422         return ret;
423 }
424
425 /* detach the device, then store the name of the device */
426 int
427 rte_eth_dev_detach(uint16_t port_id, char *name)
428 {
429         int ret = -1;
430
431         if (name == NULL) {
432                 ret = -EINVAL;
433                 goto err;
434         }
435
436         /* FIXME: move this to eal, once device flags are relocated there */
437         if (rte_eth_dev_is_detachable(port_id))
438                 goto err;
439
440         snprintf(name, RTE_DEV_NAME_MAX_LEN, "%s",
441                  rte_eth_devices[port_id].device->name);
442
443         ret = rte_eal_dev_detach(rte_eth_devices[port_id].device);
444         if (ret < 0)
445                 goto err;
446
447         rte_eth_devices[port_id].state = RTE_ETH_DEV_UNUSED;
448         return 0;
449
450 err:
451         return ret;
452 }
453
454 static int
455 rte_eth_dev_rx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
456 {
457         uint16_t old_nb_queues = dev->data->nb_rx_queues;
458         void **rxq;
459         unsigned i;
460
461         if (dev->data->rx_queues == NULL && nb_queues != 0) { /* first time configuration */
462                 dev->data->rx_queues = rte_zmalloc("ethdev->rx_queues",
463                                 sizeof(dev->data->rx_queues[0]) * nb_queues,
464                                 RTE_CACHE_LINE_SIZE);
465                 if (dev->data->rx_queues == NULL) {
466                         dev->data->nb_rx_queues = 0;
467                         return -(ENOMEM);
468                 }
469         } else if (dev->data->rx_queues != NULL && nb_queues != 0) { /* re-configure */
470                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_release, -ENOTSUP);
471
472                 rxq = dev->data->rx_queues;
473
474                 for (i = nb_queues; i < old_nb_queues; i++)
475                         (*dev->dev_ops->rx_queue_release)(rxq[i]);
476                 rxq = rte_realloc(rxq, sizeof(rxq[0]) * nb_queues,
477                                 RTE_CACHE_LINE_SIZE);
478                 if (rxq == NULL)
479                         return -(ENOMEM);
480                 if (nb_queues > old_nb_queues) {
481                         uint16_t new_qs = nb_queues - old_nb_queues;
482
483                         memset(rxq + old_nb_queues, 0,
484                                 sizeof(rxq[0]) * new_qs);
485                 }
486
487                 dev->data->rx_queues = rxq;
488
489         } else if (dev->data->rx_queues != NULL && nb_queues == 0) {
490                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_release, -ENOTSUP);
491
492                 rxq = dev->data->rx_queues;
493
494                 for (i = nb_queues; i < old_nb_queues; i++)
495                         (*dev->dev_ops->rx_queue_release)(rxq[i]);
496
497                 rte_free(dev->data->rx_queues);
498                 dev->data->rx_queues = NULL;
499         }
500         dev->data->nb_rx_queues = nb_queues;
501         return 0;
502 }
503
504 int
505 rte_eth_dev_rx_queue_start(uint16_t port_id, uint16_t rx_queue_id)
506 {
507         struct rte_eth_dev *dev;
508
509         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
510
511         dev = &rte_eth_devices[port_id];
512         if (rx_queue_id >= dev->data->nb_rx_queues) {
513                 RTE_PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", rx_queue_id);
514                 return -EINVAL;
515         }
516
517         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_start, -ENOTSUP);
518
519         if (dev->data->rx_queue_state[rx_queue_id] != RTE_ETH_QUEUE_STATE_STOPPED) {
520                 RTE_PMD_DEBUG_TRACE("Queue %" PRIu16" of device with port_id=%" PRIu8
521                         " already started\n",
522                         rx_queue_id, port_id);
523                 return 0;
524         }
525
526         return dev->dev_ops->rx_queue_start(dev, rx_queue_id);
527
528 }
529
530 int
531 rte_eth_dev_rx_queue_stop(uint16_t port_id, uint16_t rx_queue_id)
532 {
533         struct rte_eth_dev *dev;
534
535         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
536
537         dev = &rte_eth_devices[port_id];
538         if (rx_queue_id >= dev->data->nb_rx_queues) {
539                 RTE_PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", rx_queue_id);
540                 return -EINVAL;
541         }
542
543         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_stop, -ENOTSUP);
544
545         if (dev->data->rx_queue_state[rx_queue_id] == RTE_ETH_QUEUE_STATE_STOPPED) {
546                 RTE_PMD_DEBUG_TRACE("Queue %" PRIu16" of device with port_id=%" PRIu8
547                         " already stopped\n",
548                         rx_queue_id, port_id);
549                 return 0;
550         }
551
552         return dev->dev_ops->rx_queue_stop(dev, rx_queue_id);
553
554 }
555
556 int
557 rte_eth_dev_tx_queue_start(uint16_t port_id, uint16_t tx_queue_id)
558 {
559         struct rte_eth_dev *dev;
560
561         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
562
563         dev = &rte_eth_devices[port_id];
564         if (tx_queue_id >= dev->data->nb_tx_queues) {
565                 RTE_PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", tx_queue_id);
566                 return -EINVAL;
567         }
568
569         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_start, -ENOTSUP);
570
571         if (dev->data->tx_queue_state[tx_queue_id] != RTE_ETH_QUEUE_STATE_STOPPED) {
572                 RTE_PMD_DEBUG_TRACE("Queue %" PRIu16" of device with port_id=%" PRIu8
573                         " already started\n",
574                         tx_queue_id, port_id);
575                 return 0;
576         }
577
578         return dev->dev_ops->tx_queue_start(dev, tx_queue_id);
579
580 }
581
582 int
583 rte_eth_dev_tx_queue_stop(uint16_t port_id, uint16_t tx_queue_id)
584 {
585         struct rte_eth_dev *dev;
586
587         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
588
589         dev = &rte_eth_devices[port_id];
590         if (tx_queue_id >= dev->data->nb_tx_queues) {
591                 RTE_PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", tx_queue_id);
592                 return -EINVAL;
593         }
594
595         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_stop, -ENOTSUP);
596
597         if (dev->data->tx_queue_state[tx_queue_id] == RTE_ETH_QUEUE_STATE_STOPPED) {
598                 RTE_PMD_DEBUG_TRACE("Queue %" PRIu16" of device with port_id=%" PRIu8
599                         " already stopped\n",
600                         tx_queue_id, port_id);
601                 return 0;
602         }
603
604         return dev->dev_ops->tx_queue_stop(dev, tx_queue_id);
605
606 }
607
608 static int
609 rte_eth_dev_tx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
610 {
611         uint16_t old_nb_queues = dev->data->nb_tx_queues;
612         void **txq;
613         unsigned i;
614
615         if (dev->data->tx_queues == NULL && nb_queues != 0) { /* first time configuration */
616                 dev->data->tx_queues = rte_zmalloc("ethdev->tx_queues",
617                                                    sizeof(dev->data->tx_queues[0]) * nb_queues,
618                                                    RTE_CACHE_LINE_SIZE);
619                 if (dev->data->tx_queues == NULL) {
620                         dev->data->nb_tx_queues = 0;
621                         return -(ENOMEM);
622                 }
623         } else if (dev->data->tx_queues != NULL && nb_queues != 0) { /* re-configure */
624                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_release, -ENOTSUP);
625
626                 txq = dev->data->tx_queues;
627
628                 for (i = nb_queues; i < old_nb_queues; i++)
629                         (*dev->dev_ops->tx_queue_release)(txq[i]);
630                 txq = rte_realloc(txq, sizeof(txq[0]) * nb_queues,
631                                   RTE_CACHE_LINE_SIZE);
632                 if (txq == NULL)
633                         return -ENOMEM;
634                 if (nb_queues > old_nb_queues) {
635                         uint16_t new_qs = nb_queues - old_nb_queues;
636
637                         memset(txq + old_nb_queues, 0,
638                                sizeof(txq[0]) * new_qs);
639                 }
640
641                 dev->data->tx_queues = txq;
642
643         } else if (dev->data->tx_queues != NULL && nb_queues == 0) {
644                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_release, -ENOTSUP);
645
646                 txq = dev->data->tx_queues;
647
648                 for (i = nb_queues; i < old_nb_queues; i++)
649                         (*dev->dev_ops->tx_queue_release)(txq[i]);
650
651                 rte_free(dev->data->tx_queues);
652                 dev->data->tx_queues = NULL;
653         }
654         dev->data->nb_tx_queues = nb_queues;
655         return 0;
656 }
657
658 uint32_t
659 rte_eth_speed_bitflag(uint32_t speed, int duplex)
660 {
661         switch (speed) {
662         case ETH_SPEED_NUM_10M:
663                 return duplex ? ETH_LINK_SPEED_10M : ETH_LINK_SPEED_10M_HD;
664         case ETH_SPEED_NUM_100M:
665                 return duplex ? ETH_LINK_SPEED_100M : ETH_LINK_SPEED_100M_HD;
666         case ETH_SPEED_NUM_1G:
667                 return ETH_LINK_SPEED_1G;
668         case ETH_SPEED_NUM_2_5G:
669                 return ETH_LINK_SPEED_2_5G;
670         case ETH_SPEED_NUM_5G:
671                 return ETH_LINK_SPEED_5G;
672         case ETH_SPEED_NUM_10G:
673                 return ETH_LINK_SPEED_10G;
674         case ETH_SPEED_NUM_20G:
675                 return ETH_LINK_SPEED_20G;
676         case ETH_SPEED_NUM_25G:
677                 return ETH_LINK_SPEED_25G;
678         case ETH_SPEED_NUM_40G:
679                 return ETH_LINK_SPEED_40G;
680         case ETH_SPEED_NUM_50G:
681                 return ETH_LINK_SPEED_50G;
682         case ETH_SPEED_NUM_56G:
683                 return ETH_LINK_SPEED_56G;
684         case ETH_SPEED_NUM_100G:
685                 return ETH_LINK_SPEED_100G;
686         default:
687                 return 0;
688         }
689 }
690
691 /**
692  * A conversion function from rxmode bitfield API.
693  */
694 static void
695 rte_eth_convert_rx_offload_bitfield(const struct rte_eth_rxmode *rxmode,
696                                     uint64_t *rx_offloads)
697 {
698         uint64_t offloads = 0;
699
700         if (rxmode->header_split == 1)
701                 offloads |= DEV_RX_OFFLOAD_HEADER_SPLIT;
702         if (rxmode->hw_ip_checksum == 1)
703                 offloads |= DEV_RX_OFFLOAD_CHECKSUM;
704         if (rxmode->hw_vlan_filter == 1)
705                 offloads |= DEV_RX_OFFLOAD_VLAN_FILTER;
706         if (rxmode->hw_vlan_strip == 1)
707                 offloads |= DEV_RX_OFFLOAD_VLAN_STRIP;
708         if (rxmode->hw_vlan_extend == 1)
709                 offloads |= DEV_RX_OFFLOAD_VLAN_EXTEND;
710         if (rxmode->jumbo_frame == 1)
711                 offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
712         if (rxmode->hw_strip_crc == 1)
713                 offloads |= DEV_RX_OFFLOAD_CRC_STRIP;
714         if (rxmode->enable_scatter == 1)
715                 offloads |= DEV_RX_OFFLOAD_SCATTER;
716         if (rxmode->enable_lro == 1)
717                 offloads |= DEV_RX_OFFLOAD_TCP_LRO;
718
719         *rx_offloads = offloads;
720 }
721
722 /**
723  * A conversion function from rxmode offloads API.
724  */
725 static void
726 rte_eth_convert_rx_offloads(const uint64_t rx_offloads,
727                             struct rte_eth_rxmode *rxmode)
728 {
729
730         if (rx_offloads & DEV_RX_OFFLOAD_HEADER_SPLIT)
731                 rxmode->header_split = 1;
732         else
733                 rxmode->header_split = 0;
734         if (rx_offloads & DEV_RX_OFFLOAD_CHECKSUM)
735                 rxmode->hw_ip_checksum = 1;
736         else
737                 rxmode->hw_ip_checksum = 0;
738         if (rx_offloads & DEV_RX_OFFLOAD_VLAN_FILTER)
739                 rxmode->hw_vlan_filter = 1;
740         else
741                 rxmode->hw_vlan_filter = 0;
742         if (rx_offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
743                 rxmode->hw_vlan_strip = 1;
744         else
745                 rxmode->hw_vlan_strip = 0;
746         if (rx_offloads & DEV_RX_OFFLOAD_VLAN_EXTEND)
747                 rxmode->hw_vlan_extend = 1;
748         else
749                 rxmode->hw_vlan_extend = 0;
750         if (rx_offloads & DEV_RX_OFFLOAD_JUMBO_FRAME)
751                 rxmode->jumbo_frame = 1;
752         else
753                 rxmode->jumbo_frame = 0;
754         if (rx_offloads & DEV_RX_OFFLOAD_CRC_STRIP)
755                 rxmode->hw_strip_crc = 1;
756         else
757                 rxmode->hw_strip_crc = 0;
758         if (rx_offloads & DEV_RX_OFFLOAD_SCATTER)
759                 rxmode->enable_scatter = 1;
760         else
761                 rxmode->enable_scatter = 0;
762         if (rx_offloads & DEV_RX_OFFLOAD_TCP_LRO)
763                 rxmode->enable_lro = 1;
764         else
765                 rxmode->enable_lro = 0;
766 }
767
768 int
769 rte_eth_dev_configure(uint16_t port_id, uint16_t nb_rx_q, uint16_t nb_tx_q,
770                       const struct rte_eth_conf *dev_conf)
771 {
772         struct rte_eth_dev *dev;
773         struct rte_eth_dev_info dev_info;
774         struct rte_eth_conf local_conf = *dev_conf;
775         int diag;
776
777         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
778
779         if (nb_rx_q > RTE_MAX_QUEUES_PER_PORT) {
780                 RTE_PMD_DEBUG_TRACE(
781                         "Number of RX queues requested (%u) is greater than max supported(%d)\n",
782                         nb_rx_q, RTE_MAX_QUEUES_PER_PORT);
783                 return -EINVAL;
784         }
785
786         if (nb_tx_q > RTE_MAX_QUEUES_PER_PORT) {
787                 RTE_PMD_DEBUG_TRACE(
788                         "Number of TX queues requested (%u) is greater than max supported(%d)\n",
789                         nb_tx_q, RTE_MAX_QUEUES_PER_PORT);
790                 return -EINVAL;
791         }
792
793         dev = &rte_eth_devices[port_id];
794
795         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
796         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_configure, -ENOTSUP);
797
798         if (dev->data->dev_started) {
799                 RTE_PMD_DEBUG_TRACE(
800                     "port %d must be stopped to allow configuration\n", port_id);
801                 return -EBUSY;
802         }
803
804         /*
805          * Convert between the offloads API to enable PMDs to support
806          * only one of them.
807          */
808         if ((dev_conf->rxmode.ignore_offload_bitfield == 0)) {
809                 rte_eth_convert_rx_offload_bitfield(
810                                 &dev_conf->rxmode, &local_conf.rxmode.offloads);
811         } else {
812                 rte_eth_convert_rx_offloads(dev_conf->rxmode.offloads,
813                                             &local_conf.rxmode);
814         }
815
816         /* Copy the dev_conf parameter into the dev structure */
817         memcpy(&dev->data->dev_conf, &local_conf, sizeof(dev->data->dev_conf));
818
819         /*
820          * Check that the numbers of RX and TX queues are not greater
821          * than the maximum number of RX and TX queues supported by the
822          * configured device.
823          */
824         (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
825
826         if (nb_rx_q == 0 && nb_tx_q == 0) {
827                 RTE_PMD_DEBUG_TRACE("ethdev port_id=%d both rx and tx queue cannot be 0\n", port_id);
828                 return -EINVAL;
829         }
830
831         if (nb_rx_q > dev_info.max_rx_queues) {
832                 RTE_PMD_DEBUG_TRACE("ethdev port_id=%d nb_rx_queues=%d > %d\n",
833                                 port_id, nb_rx_q, dev_info.max_rx_queues);
834                 return -EINVAL;
835         }
836
837         if (nb_tx_q > dev_info.max_tx_queues) {
838                 RTE_PMD_DEBUG_TRACE("ethdev port_id=%d nb_tx_queues=%d > %d\n",
839                                 port_id, nb_tx_q, dev_info.max_tx_queues);
840                 return -EINVAL;
841         }
842
843         /* Check that the device supports requested interrupts */
844         if ((dev_conf->intr_conf.lsc == 1) &&
845                 (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC))) {
846                         RTE_PMD_DEBUG_TRACE("driver %s does not support lsc\n",
847                                         dev->device->driver->name);
848                         return -EINVAL;
849         }
850         if ((dev_conf->intr_conf.rmv == 1) &&
851             (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_RMV))) {
852                 RTE_PMD_DEBUG_TRACE("driver %s does not support rmv\n",
853                                     dev->device->driver->name);
854                 return -EINVAL;
855         }
856
857         /*
858          * If jumbo frames are enabled, check that the maximum RX packet
859          * length is supported by the configured device.
860          */
861         if (local_conf.rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
862                 if (dev_conf->rxmode.max_rx_pkt_len >
863                     dev_info.max_rx_pktlen) {
864                         RTE_PMD_DEBUG_TRACE("ethdev port_id=%d max_rx_pkt_len %u"
865                                 " > max valid value %u\n",
866                                 port_id,
867                                 (unsigned)dev_conf->rxmode.max_rx_pkt_len,
868                                 (unsigned)dev_info.max_rx_pktlen);
869                         return -EINVAL;
870                 } else if (dev_conf->rxmode.max_rx_pkt_len < ETHER_MIN_LEN) {
871                         RTE_PMD_DEBUG_TRACE("ethdev port_id=%d max_rx_pkt_len %u"
872                                 " < min valid value %u\n",
873                                 port_id,
874                                 (unsigned)dev_conf->rxmode.max_rx_pkt_len,
875                                 (unsigned)ETHER_MIN_LEN);
876                         return -EINVAL;
877                 }
878         } else {
879                 if (dev_conf->rxmode.max_rx_pkt_len < ETHER_MIN_LEN ||
880                         dev_conf->rxmode.max_rx_pkt_len > ETHER_MAX_LEN)
881                         /* Use default value */
882                         dev->data->dev_conf.rxmode.max_rx_pkt_len =
883                                                         ETHER_MAX_LEN;
884         }
885
886         /*
887          * Setup new number of RX/TX queues and reconfigure device.
888          */
889         diag = rte_eth_dev_rx_queue_config(dev, nb_rx_q);
890         if (diag != 0) {
891                 RTE_PMD_DEBUG_TRACE("port%d rte_eth_dev_rx_queue_config = %d\n",
892                                 port_id, diag);
893                 return diag;
894         }
895
896         diag = rte_eth_dev_tx_queue_config(dev, nb_tx_q);
897         if (diag != 0) {
898                 RTE_PMD_DEBUG_TRACE("port%d rte_eth_dev_tx_queue_config = %d\n",
899                                 port_id, diag);
900                 rte_eth_dev_rx_queue_config(dev, 0);
901                 return diag;
902         }
903
904         diag = (*dev->dev_ops->dev_configure)(dev);
905         if (diag != 0) {
906                 RTE_PMD_DEBUG_TRACE("port%d dev_configure = %d\n",
907                                 port_id, diag);
908                 rte_eth_dev_rx_queue_config(dev, 0);
909                 rte_eth_dev_tx_queue_config(dev, 0);
910                 return diag;
911         }
912
913         /* Initialize Rx profiling if enabled at compilation time. */
914         diag = __rte_eth_profile_rx_init(port_id, dev);
915         if (diag != 0) {
916                 RTE_PMD_DEBUG_TRACE("port%d __rte_eth_profile_rx_init = %d\n",
917                                 port_id, diag);
918                 rte_eth_dev_rx_queue_config(dev, 0);
919                 rte_eth_dev_tx_queue_config(dev, 0);
920                 return diag;
921         }
922
923         return 0;
924 }
925
926 void
927 _rte_eth_dev_reset(struct rte_eth_dev *dev)
928 {
929         if (dev->data->dev_started) {
930                 RTE_PMD_DEBUG_TRACE(
931                         "port %d must be stopped to allow reset\n",
932                         dev->data->port_id);
933                 return;
934         }
935
936         rte_eth_dev_rx_queue_config(dev, 0);
937         rte_eth_dev_tx_queue_config(dev, 0);
938
939         memset(&dev->data->dev_conf, 0, sizeof(dev->data->dev_conf));
940 }
941
942 static void
943 rte_eth_dev_config_restore(uint16_t port_id)
944 {
945         struct rte_eth_dev *dev;
946         struct rte_eth_dev_info dev_info;
947         struct ether_addr *addr;
948         uint16_t i;
949         uint32_t pool = 0;
950         uint64_t pool_mask;
951
952         dev = &rte_eth_devices[port_id];
953
954         rte_eth_dev_info_get(port_id, &dev_info);
955
956         /* replay MAC address configuration including default MAC */
957         addr = &dev->data->mac_addrs[0];
958         if (*dev->dev_ops->mac_addr_set != NULL)
959                 (*dev->dev_ops->mac_addr_set)(dev, addr);
960         else if (*dev->dev_ops->mac_addr_add != NULL)
961                 (*dev->dev_ops->mac_addr_add)(dev, addr, 0, pool);
962
963         if (*dev->dev_ops->mac_addr_add != NULL) {
964                 for (i = 1; i < dev_info.max_mac_addrs; i++) {
965                         addr = &dev->data->mac_addrs[i];
966
967                         /* skip zero address */
968                         if (is_zero_ether_addr(addr))
969                                 continue;
970
971                         pool = 0;
972                         pool_mask = dev->data->mac_pool_sel[i];
973
974                         do {
975                                 if (pool_mask & 1ULL)
976                                         (*dev->dev_ops->mac_addr_add)(dev,
977                                                 addr, i, pool);
978                                 pool_mask >>= 1;
979                                 pool++;
980                         } while (pool_mask);
981                 }
982         }
983
984         /* replay promiscuous configuration */
985         if (rte_eth_promiscuous_get(port_id) == 1)
986                 rte_eth_promiscuous_enable(port_id);
987         else if (rte_eth_promiscuous_get(port_id) == 0)
988                 rte_eth_promiscuous_disable(port_id);
989
990         /* replay all multicast configuration */
991         if (rte_eth_allmulticast_get(port_id) == 1)
992                 rte_eth_allmulticast_enable(port_id);
993         else if (rte_eth_allmulticast_get(port_id) == 0)
994                 rte_eth_allmulticast_disable(port_id);
995 }
996
997 int
998 rte_eth_dev_start(uint16_t port_id)
999 {
1000         struct rte_eth_dev *dev;
1001         int diag;
1002
1003         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1004
1005         dev = &rte_eth_devices[port_id];
1006
1007         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
1008
1009         if (dev->data->dev_started != 0) {
1010                 RTE_PMD_DEBUG_TRACE("Device with port_id=%" PRIu16
1011                         " already started\n",
1012                         port_id);
1013                 return 0;
1014         }
1015
1016         diag = (*dev->dev_ops->dev_start)(dev);
1017         if (diag == 0)
1018                 dev->data->dev_started = 1;
1019         else
1020                 return diag;
1021
1022         rte_eth_dev_config_restore(port_id);
1023
1024         if (dev->data->dev_conf.intr_conf.lsc == 0) {
1025                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->link_update, -ENOTSUP);
1026                 (*dev->dev_ops->link_update)(dev, 0);
1027         }
1028         return 0;
1029 }
1030
1031 void
1032 rte_eth_dev_stop(uint16_t port_id)
1033 {
1034         struct rte_eth_dev *dev;
1035
1036         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1037         dev = &rte_eth_devices[port_id];
1038
1039         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
1040
1041         if (dev->data->dev_started == 0) {
1042                 RTE_PMD_DEBUG_TRACE("Device with port_id=%" PRIu16
1043                         " already stopped\n",
1044                         port_id);
1045                 return;
1046         }
1047
1048         dev->data->dev_started = 0;
1049         (*dev->dev_ops->dev_stop)(dev);
1050 }
1051
1052 int
1053 rte_eth_dev_set_link_up(uint16_t port_id)
1054 {
1055         struct rte_eth_dev *dev;
1056
1057         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1058
1059         dev = &rte_eth_devices[port_id];
1060
1061         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_set_link_up, -ENOTSUP);
1062         return (*dev->dev_ops->dev_set_link_up)(dev);
1063 }
1064
1065 int
1066 rte_eth_dev_set_link_down(uint16_t port_id)
1067 {
1068         struct rte_eth_dev *dev;
1069
1070         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1071
1072         dev = &rte_eth_devices[port_id];
1073
1074         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_set_link_down, -ENOTSUP);
1075         return (*dev->dev_ops->dev_set_link_down)(dev);
1076 }
1077
1078 void
1079 rte_eth_dev_close(uint16_t port_id)
1080 {
1081         struct rte_eth_dev *dev;
1082
1083         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1084         dev = &rte_eth_devices[port_id];
1085
1086         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_close);
1087         dev->data->dev_started = 0;
1088         (*dev->dev_ops->dev_close)(dev);
1089
1090         dev->data->nb_rx_queues = 0;
1091         rte_free(dev->data->rx_queues);
1092         dev->data->rx_queues = NULL;
1093         dev->data->nb_tx_queues = 0;
1094         rte_free(dev->data->tx_queues);
1095         dev->data->tx_queues = NULL;
1096 }
1097
1098 int
1099 rte_eth_dev_reset(uint16_t port_id)
1100 {
1101         struct rte_eth_dev *dev;
1102         int ret;
1103
1104         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1105         dev = &rte_eth_devices[port_id];
1106
1107         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_reset, -ENOTSUP);
1108
1109         rte_eth_dev_stop(port_id);
1110         ret = dev->dev_ops->dev_reset(dev);
1111
1112         return ret;
1113 }
1114
1115 int
1116 rte_eth_rx_queue_setup(uint16_t port_id, uint16_t rx_queue_id,
1117                        uint16_t nb_rx_desc, unsigned int socket_id,
1118                        const struct rte_eth_rxconf *rx_conf,
1119                        struct rte_mempool *mp)
1120 {
1121         int ret;
1122         uint32_t mbp_buf_size;
1123         struct rte_eth_dev *dev;
1124         struct rte_eth_dev_info dev_info;
1125         struct rte_eth_rxconf local_conf;
1126         void **rxq;
1127
1128         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1129
1130         dev = &rte_eth_devices[port_id];
1131         if (rx_queue_id >= dev->data->nb_rx_queues) {
1132                 RTE_PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", rx_queue_id);
1133                 return -EINVAL;
1134         }
1135
1136         if (dev->data->dev_started) {
1137                 RTE_PMD_DEBUG_TRACE(
1138                     "port %d must be stopped to allow configuration\n", port_id);
1139                 return -EBUSY;
1140         }
1141
1142         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
1143         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_setup, -ENOTSUP);
1144
1145         /*
1146          * Check the size of the mbuf data buffer.
1147          * This value must be provided in the private data of the memory pool.
1148          * First check that the memory pool has a valid private data.
1149          */
1150         rte_eth_dev_info_get(port_id, &dev_info);
1151         if (mp->private_data_size < sizeof(struct rte_pktmbuf_pool_private)) {
1152                 RTE_PMD_DEBUG_TRACE("%s private_data_size %d < %d\n",
1153                                 mp->name, (int) mp->private_data_size,
1154                                 (int) sizeof(struct rte_pktmbuf_pool_private));
1155                 return -ENOSPC;
1156         }
1157         mbp_buf_size = rte_pktmbuf_data_room_size(mp);
1158
1159         if ((mbp_buf_size - RTE_PKTMBUF_HEADROOM) < dev_info.min_rx_bufsize) {
1160                 RTE_PMD_DEBUG_TRACE("%s mbuf_data_room_size %d < %d "
1161                                 "(RTE_PKTMBUF_HEADROOM=%d + min_rx_bufsize(dev)"
1162                                 "=%d)\n",
1163                                 mp->name,
1164                                 (int)mbp_buf_size,
1165                                 (int)(RTE_PKTMBUF_HEADROOM +
1166                                       dev_info.min_rx_bufsize),
1167                                 (int)RTE_PKTMBUF_HEADROOM,
1168                                 (int)dev_info.min_rx_bufsize);
1169                 return -EINVAL;
1170         }
1171
1172         if (nb_rx_desc > dev_info.rx_desc_lim.nb_max ||
1173                         nb_rx_desc < dev_info.rx_desc_lim.nb_min ||
1174                         nb_rx_desc % dev_info.rx_desc_lim.nb_align != 0) {
1175
1176                 RTE_PMD_DEBUG_TRACE("Invalid value for nb_rx_desc(=%hu), "
1177                         "should be: <= %hu, = %hu, and a product of %hu\n",
1178                         nb_rx_desc,
1179                         dev_info.rx_desc_lim.nb_max,
1180                         dev_info.rx_desc_lim.nb_min,
1181                         dev_info.rx_desc_lim.nb_align);
1182                 return -EINVAL;
1183         }
1184
1185         rxq = dev->data->rx_queues;
1186         if (rxq[rx_queue_id]) {
1187                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_release,
1188                                         -ENOTSUP);
1189                 (*dev->dev_ops->rx_queue_release)(rxq[rx_queue_id]);
1190                 rxq[rx_queue_id] = NULL;
1191         }
1192
1193         if (rx_conf == NULL)
1194                 rx_conf = &dev_info.default_rxconf;
1195
1196         local_conf = *rx_conf;
1197         if (dev->data->dev_conf.rxmode.ignore_offload_bitfield == 0) {
1198                 /**
1199                  * Reflect port offloads to queue offloads in order for
1200                  * offloads to not be discarded.
1201                  */
1202                 rte_eth_convert_rx_offload_bitfield(&dev->data->dev_conf.rxmode,
1203                                                     &local_conf.offloads);
1204         }
1205
1206         ret = (*dev->dev_ops->rx_queue_setup)(dev, rx_queue_id, nb_rx_desc,
1207                                               socket_id, &local_conf, mp);
1208         if (!ret) {
1209                 if (!dev->data->min_rx_buf_size ||
1210                     dev->data->min_rx_buf_size > mbp_buf_size)
1211                         dev->data->min_rx_buf_size = mbp_buf_size;
1212         }
1213
1214         return ret;
1215 }
1216
1217 /**
1218  * A conversion function from txq_flags API.
1219  */
1220 static void
1221 rte_eth_convert_txq_flags(const uint32_t txq_flags, uint64_t *tx_offloads)
1222 {
1223         uint64_t offloads = 0;
1224
1225         if (!(txq_flags & ETH_TXQ_FLAGS_NOMULTSEGS))
1226                 offloads |= DEV_TX_OFFLOAD_MULTI_SEGS;
1227         if (!(txq_flags & ETH_TXQ_FLAGS_NOVLANOFFL))
1228                 offloads |= DEV_TX_OFFLOAD_VLAN_INSERT;
1229         if (!(txq_flags & ETH_TXQ_FLAGS_NOXSUMSCTP))
1230                 offloads |= DEV_TX_OFFLOAD_SCTP_CKSUM;
1231         if (!(txq_flags & ETH_TXQ_FLAGS_NOXSUMUDP))
1232                 offloads |= DEV_TX_OFFLOAD_UDP_CKSUM;
1233         if (!(txq_flags & ETH_TXQ_FLAGS_NOXSUMTCP))
1234                 offloads |= DEV_TX_OFFLOAD_TCP_CKSUM;
1235         if ((txq_flags & ETH_TXQ_FLAGS_NOREFCOUNT) &&
1236             (txq_flags & ETH_TXQ_FLAGS_NOMULTMEMP))
1237                 offloads |= DEV_TX_OFFLOAD_MBUF_FAST_FREE;
1238
1239         *tx_offloads = offloads;
1240 }
1241
1242 /**
1243  * A conversion function from offloads API.
1244  */
1245 static void
1246 rte_eth_convert_txq_offloads(const uint64_t tx_offloads, uint32_t *txq_flags)
1247 {
1248         uint32_t flags = 0;
1249
1250         if (!(tx_offloads & DEV_TX_OFFLOAD_MULTI_SEGS))
1251                 flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
1252         if (!(tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT))
1253                 flags |= ETH_TXQ_FLAGS_NOVLANOFFL;
1254         if (!(tx_offloads & DEV_TX_OFFLOAD_SCTP_CKSUM))
1255                 flags |= ETH_TXQ_FLAGS_NOXSUMSCTP;
1256         if (!(tx_offloads & DEV_TX_OFFLOAD_UDP_CKSUM))
1257                 flags |= ETH_TXQ_FLAGS_NOXSUMUDP;
1258         if (!(tx_offloads & DEV_TX_OFFLOAD_TCP_CKSUM))
1259                 flags |= ETH_TXQ_FLAGS_NOXSUMTCP;
1260         if (tx_offloads & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
1261                 flags |= (ETH_TXQ_FLAGS_NOREFCOUNT | ETH_TXQ_FLAGS_NOMULTMEMP);
1262
1263         *txq_flags = flags;
1264 }
1265
1266 int
1267 rte_eth_tx_queue_setup(uint16_t port_id, uint16_t tx_queue_id,
1268                        uint16_t nb_tx_desc, unsigned int socket_id,
1269                        const struct rte_eth_txconf *tx_conf)
1270 {
1271         struct rte_eth_dev *dev;
1272         struct rte_eth_dev_info dev_info;
1273         struct rte_eth_txconf local_conf;
1274         void **txq;
1275
1276         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1277
1278         dev = &rte_eth_devices[port_id];
1279         if (tx_queue_id >= dev->data->nb_tx_queues) {
1280                 RTE_PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", tx_queue_id);
1281                 return -EINVAL;
1282         }
1283
1284         if (dev->data->dev_started) {
1285                 RTE_PMD_DEBUG_TRACE(
1286                     "port %d must be stopped to allow configuration\n", port_id);
1287                 return -EBUSY;
1288         }
1289
1290         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
1291         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_setup, -ENOTSUP);
1292
1293         rte_eth_dev_info_get(port_id, &dev_info);
1294
1295         if (nb_tx_desc > dev_info.tx_desc_lim.nb_max ||
1296             nb_tx_desc < dev_info.tx_desc_lim.nb_min ||
1297             nb_tx_desc % dev_info.tx_desc_lim.nb_align != 0) {
1298                 RTE_PMD_DEBUG_TRACE("Invalid value for nb_tx_desc(=%hu), "
1299                                 "should be: <= %hu, = %hu, and a product of %hu\n",
1300                                 nb_tx_desc,
1301                                 dev_info.tx_desc_lim.nb_max,
1302                                 dev_info.tx_desc_lim.nb_min,
1303                                 dev_info.tx_desc_lim.nb_align);
1304                 return -EINVAL;
1305         }
1306
1307         txq = dev->data->tx_queues;
1308         if (txq[tx_queue_id]) {
1309                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_release,
1310                                         -ENOTSUP);
1311                 (*dev->dev_ops->tx_queue_release)(txq[tx_queue_id]);
1312                 txq[tx_queue_id] = NULL;
1313         }
1314
1315         if (tx_conf == NULL)
1316                 tx_conf = &dev_info.default_txconf;
1317
1318         /*
1319          * Convert between the offloads API to enable PMDs to support
1320          * only one of them.
1321          */
1322         local_conf = *tx_conf;
1323         if (tx_conf->txq_flags & ETH_TXQ_FLAGS_IGNORE) {
1324                 rte_eth_convert_txq_offloads(tx_conf->offloads,
1325                                              &local_conf.txq_flags);
1326                 /* Keep the ignore flag. */
1327                 local_conf.txq_flags |= ETH_TXQ_FLAGS_IGNORE;
1328         } else {
1329                 rte_eth_convert_txq_flags(tx_conf->txq_flags,
1330                                           &local_conf.offloads);
1331         }
1332
1333         return (*dev->dev_ops->tx_queue_setup)(dev, tx_queue_id, nb_tx_desc,
1334                                                socket_id, &local_conf);
1335 }
1336
1337 void
1338 rte_eth_tx_buffer_drop_callback(struct rte_mbuf **pkts, uint16_t unsent,
1339                 void *userdata __rte_unused)
1340 {
1341         unsigned i;
1342
1343         for (i = 0; i < unsent; i++)
1344                 rte_pktmbuf_free(pkts[i]);
1345 }
1346
1347 void
1348 rte_eth_tx_buffer_count_callback(struct rte_mbuf **pkts, uint16_t unsent,
1349                 void *userdata)
1350 {
1351         uint64_t *count = userdata;
1352         unsigned i;
1353
1354         for (i = 0; i < unsent; i++)
1355                 rte_pktmbuf_free(pkts[i]);
1356
1357         *count += unsent;
1358 }
1359
1360 int
1361 rte_eth_tx_buffer_set_err_callback(struct rte_eth_dev_tx_buffer *buffer,
1362                 buffer_tx_error_fn cbfn, void *userdata)
1363 {
1364         buffer->error_callback = cbfn;
1365         buffer->error_userdata = userdata;
1366         return 0;
1367 }
1368
1369 int
1370 rte_eth_tx_buffer_init(struct rte_eth_dev_tx_buffer *buffer, uint16_t size)
1371 {
1372         int ret = 0;
1373
1374         if (buffer == NULL)
1375                 return -EINVAL;
1376
1377         buffer->size = size;
1378         if (buffer->error_callback == NULL) {
1379                 ret = rte_eth_tx_buffer_set_err_callback(
1380                         buffer, rte_eth_tx_buffer_drop_callback, NULL);
1381         }
1382
1383         return ret;
1384 }
1385
1386 int
1387 rte_eth_tx_done_cleanup(uint16_t port_id, uint16_t queue_id, uint32_t free_cnt)
1388 {
1389         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1390
1391         /* Validate Input Data. Bail if not valid or not supported. */
1392         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1393         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_done_cleanup, -ENOTSUP);
1394
1395         /* Call driver to free pending mbufs. */
1396         return (*dev->dev_ops->tx_done_cleanup)(dev->data->tx_queues[queue_id],
1397                         free_cnt);
1398 }
1399
1400 void
1401 rte_eth_promiscuous_enable(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->promiscuous_enable);
1409         (*dev->dev_ops->promiscuous_enable)(dev);
1410         dev->data->promiscuous = 1;
1411 }
1412
1413 void
1414 rte_eth_promiscuous_disable(uint16_t port_id)
1415 {
1416         struct rte_eth_dev *dev;
1417
1418         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1419         dev = &rte_eth_devices[port_id];
1420
1421         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->promiscuous_disable);
1422         dev->data->promiscuous = 0;
1423         (*dev->dev_ops->promiscuous_disable)(dev);
1424 }
1425
1426 int
1427 rte_eth_promiscuous_get(uint16_t port_id)
1428 {
1429         struct rte_eth_dev *dev;
1430
1431         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1432
1433         dev = &rte_eth_devices[port_id];
1434         return dev->data->promiscuous;
1435 }
1436
1437 void
1438 rte_eth_allmulticast_enable(uint16_t port_id)
1439 {
1440         struct rte_eth_dev *dev;
1441
1442         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1443         dev = &rte_eth_devices[port_id];
1444
1445         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_enable);
1446         (*dev->dev_ops->allmulticast_enable)(dev);
1447         dev->data->all_multicast = 1;
1448 }
1449
1450 void
1451 rte_eth_allmulticast_disable(uint16_t port_id)
1452 {
1453         struct rte_eth_dev *dev;
1454
1455         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1456         dev = &rte_eth_devices[port_id];
1457
1458         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_disable);
1459         dev->data->all_multicast = 0;
1460         (*dev->dev_ops->allmulticast_disable)(dev);
1461 }
1462
1463 int
1464 rte_eth_allmulticast_get(uint16_t port_id)
1465 {
1466         struct rte_eth_dev *dev;
1467
1468         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1469
1470         dev = &rte_eth_devices[port_id];
1471         return dev->data->all_multicast;
1472 }
1473
1474 static inline int
1475 rte_eth_dev_atomic_read_link_status(struct rte_eth_dev *dev,
1476                                 struct rte_eth_link *link)
1477 {
1478         struct rte_eth_link *dst = link;
1479         struct rte_eth_link *src = &(dev->data->dev_link);
1480
1481         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
1482                                         *(uint64_t *)src) == 0)
1483                 return -1;
1484
1485         return 0;
1486 }
1487
1488 void
1489 rte_eth_link_get(uint16_t port_id, struct rte_eth_link *eth_link)
1490 {
1491         struct rte_eth_dev *dev;
1492
1493         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1494         dev = &rte_eth_devices[port_id];
1495
1496         if (dev->data->dev_conf.intr_conf.lsc != 0)
1497                 rte_eth_dev_atomic_read_link_status(dev, eth_link);
1498         else {
1499                 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
1500                 (*dev->dev_ops->link_update)(dev, 1);
1501                 *eth_link = dev->data->dev_link;
1502         }
1503 }
1504
1505 void
1506 rte_eth_link_get_nowait(uint16_t port_id, struct rte_eth_link *eth_link)
1507 {
1508         struct rte_eth_dev *dev;
1509
1510         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1511         dev = &rte_eth_devices[port_id];
1512
1513         if (dev->data->dev_conf.intr_conf.lsc != 0)
1514                 rte_eth_dev_atomic_read_link_status(dev, eth_link);
1515         else {
1516                 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
1517                 (*dev->dev_ops->link_update)(dev, 0);
1518                 *eth_link = dev->data->dev_link;
1519         }
1520 }
1521
1522 int
1523 rte_eth_stats_get(uint16_t port_id, struct rte_eth_stats *stats)
1524 {
1525         struct rte_eth_dev *dev;
1526
1527         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1528
1529         dev = &rte_eth_devices[port_id];
1530         memset(stats, 0, sizeof(*stats));
1531
1532         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
1533         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
1534         (*dev->dev_ops->stats_get)(dev, stats);
1535         return 0;
1536 }
1537
1538 int
1539 rte_eth_stats_reset(uint16_t port_id)
1540 {
1541         struct rte_eth_dev *dev;
1542
1543         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1544         dev = &rte_eth_devices[port_id];
1545
1546         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_reset, -ENOTSUP);
1547         (*dev->dev_ops->stats_reset)(dev);
1548         dev->data->rx_mbuf_alloc_failed = 0;
1549
1550         return 0;
1551 }
1552
1553 static int
1554 get_xstats_count(uint16_t port_id)
1555 {
1556         struct rte_eth_dev *dev;
1557         int count;
1558
1559         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1560         dev = &rte_eth_devices[port_id];
1561         if (dev->dev_ops->xstats_get_names_by_id != NULL) {
1562                 count = (*dev->dev_ops->xstats_get_names_by_id)(dev, NULL,
1563                                 NULL, 0);
1564                 if (count < 0)
1565                         return count;
1566         }
1567         if (dev->dev_ops->xstats_get_names != NULL) {
1568                 count = (*dev->dev_ops->xstats_get_names)(dev, NULL, 0);
1569                 if (count < 0)
1570                         return count;
1571         } else
1572                 count = 0;
1573
1574         count += RTE_NB_STATS;
1575         count += RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS) *
1576                  RTE_NB_RXQ_STATS;
1577         count += RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS) *
1578                  RTE_NB_TXQ_STATS;
1579         return count;
1580 }
1581
1582 int
1583 rte_eth_xstats_get_id_by_name(uint16_t port_id, const char *xstat_name,
1584                 uint64_t *id)
1585 {
1586         int cnt_xstats, idx_xstat;
1587
1588         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1589
1590         if (!id) {
1591                 RTE_PMD_DEBUG_TRACE("Error: id pointer is NULL\n");
1592                 return -ENOMEM;
1593         }
1594
1595         if (!xstat_name) {
1596                 RTE_PMD_DEBUG_TRACE("Error: xstat_name pointer is NULL\n");
1597                 return -ENOMEM;
1598         }
1599
1600         /* Get count */
1601         cnt_xstats = rte_eth_xstats_get_names_by_id(port_id, NULL, 0, NULL);
1602         if (cnt_xstats  < 0) {
1603                 RTE_PMD_DEBUG_TRACE("Error: Cannot get count of xstats\n");
1604                 return -ENODEV;
1605         }
1606
1607         /* Get id-name lookup table */
1608         struct rte_eth_xstat_name xstats_names[cnt_xstats];
1609
1610         if (cnt_xstats != rte_eth_xstats_get_names_by_id(
1611                         port_id, xstats_names, cnt_xstats, NULL)) {
1612                 RTE_PMD_DEBUG_TRACE("Error: Cannot get xstats lookup\n");
1613                 return -1;
1614         }
1615
1616         for (idx_xstat = 0; idx_xstat < cnt_xstats; idx_xstat++) {
1617                 if (!strcmp(xstats_names[idx_xstat].name, xstat_name)) {
1618                         *id = idx_xstat;
1619                         return 0;
1620                 };
1621         }
1622
1623         return -EINVAL;
1624 }
1625
1626 int
1627 rte_eth_xstats_get_names_by_id(uint16_t port_id,
1628         struct rte_eth_xstat_name *xstats_names, unsigned int size,
1629         uint64_t *ids)
1630 {
1631         /* Get all xstats */
1632         if (!ids) {
1633                 struct rte_eth_dev *dev;
1634                 int cnt_used_entries;
1635                 int cnt_expected_entries;
1636                 int cnt_driver_entries;
1637                 uint32_t idx, id_queue;
1638                 uint16_t num_q;
1639
1640                 cnt_expected_entries = get_xstats_count(port_id);
1641                 if (xstats_names == NULL || cnt_expected_entries < 0 ||
1642                                 (int)size < cnt_expected_entries)
1643                         return cnt_expected_entries;
1644
1645                 /* port_id checked in get_xstats_count() */
1646                 dev = &rte_eth_devices[port_id];
1647                 cnt_used_entries = 0;
1648
1649                 for (idx = 0; idx < RTE_NB_STATS; idx++) {
1650                         snprintf(xstats_names[cnt_used_entries].name,
1651                                 sizeof(xstats_names[0].name),
1652                                 "%s", rte_stats_strings[idx].name);
1653                         cnt_used_entries++;
1654                 }
1655                 num_q = RTE_MIN(dev->data->nb_rx_queues,
1656                                 RTE_ETHDEV_QUEUE_STAT_CNTRS);
1657                 for (id_queue = 0; id_queue < num_q; id_queue++) {
1658                         for (idx = 0; idx < RTE_NB_RXQ_STATS; idx++) {
1659                                 snprintf(xstats_names[cnt_used_entries].name,
1660                                         sizeof(xstats_names[0].name),
1661                                         "rx_q%u%s",
1662                                         id_queue,
1663                                         rte_rxq_stats_strings[idx].name);
1664                                 cnt_used_entries++;
1665                         }
1666
1667                 }
1668                 num_q = RTE_MIN(dev->data->nb_tx_queues,
1669                                 RTE_ETHDEV_QUEUE_STAT_CNTRS);
1670                 for (id_queue = 0; id_queue < num_q; id_queue++) {
1671                         for (idx = 0; idx < RTE_NB_TXQ_STATS; idx++) {
1672                                 snprintf(xstats_names[cnt_used_entries].name,
1673                                         sizeof(xstats_names[0].name),
1674                                         "tx_q%u%s",
1675                                         id_queue,
1676                                         rte_txq_stats_strings[idx].name);
1677                                 cnt_used_entries++;
1678                         }
1679                 }
1680
1681                 if (dev->dev_ops->xstats_get_names_by_id != NULL) {
1682                         /* If there are any driver-specific xstats, append them
1683                          * to end of list.
1684                          */
1685                         cnt_driver_entries =
1686                                 (*dev->dev_ops->xstats_get_names_by_id)(
1687                                 dev,
1688                                 xstats_names + cnt_used_entries,
1689                                 NULL,
1690                                 size - cnt_used_entries);
1691                         if (cnt_driver_entries < 0)
1692                                 return cnt_driver_entries;
1693                         cnt_used_entries += cnt_driver_entries;
1694
1695                 } else if (dev->dev_ops->xstats_get_names != NULL) {
1696                         /* If there are any driver-specific xstats, append them
1697                          * to end of list.
1698                          */
1699                         cnt_driver_entries = (*dev->dev_ops->xstats_get_names)(
1700                                 dev,
1701                                 xstats_names + cnt_used_entries,
1702                                 size - cnt_used_entries);
1703                         if (cnt_driver_entries < 0)
1704                                 return cnt_driver_entries;
1705                         cnt_used_entries += cnt_driver_entries;
1706                 }
1707
1708                 return cnt_used_entries;
1709         }
1710         /* Get only xstats given by IDS */
1711         else {
1712                 uint16_t len, i;
1713                 struct rte_eth_xstat_name *xstats_names_copy;
1714
1715                 len = rte_eth_xstats_get_names_by_id(port_id, NULL, 0, NULL);
1716
1717                 xstats_names_copy =
1718                                 malloc(sizeof(struct rte_eth_xstat_name) * len);
1719                 if (!xstats_names_copy) {
1720                         RTE_PMD_DEBUG_TRACE(
1721                              "ERROR: can't allocate memory for values_copy\n");
1722                         free(xstats_names_copy);
1723                         return -1;
1724                 }
1725
1726                 rte_eth_xstats_get_names_by_id(port_id, xstats_names_copy,
1727                                 len, NULL);
1728
1729                 for (i = 0; i < size; i++) {
1730                         if (ids[i] >= len) {
1731                                 RTE_PMD_DEBUG_TRACE(
1732                                         "ERROR: id value isn't valid\n");
1733                                 return -1;
1734                         }
1735                         strcpy(xstats_names[i].name,
1736                                         xstats_names_copy[ids[i]].name);
1737                 }
1738                 free(xstats_names_copy);
1739                 return size;
1740         }
1741 }
1742
1743 int
1744 rte_eth_xstats_get_names(uint16_t port_id,
1745         struct rte_eth_xstat_name *xstats_names,
1746         unsigned int size)
1747 {
1748         struct rte_eth_dev *dev;
1749         int cnt_used_entries;
1750         int cnt_expected_entries;
1751         int cnt_driver_entries;
1752         uint32_t idx, id_queue;
1753         uint16_t num_q;
1754
1755         cnt_expected_entries = get_xstats_count(port_id);
1756         if (xstats_names == NULL || cnt_expected_entries < 0 ||
1757                         (int)size < cnt_expected_entries)
1758                 return cnt_expected_entries;
1759
1760         /* port_id checked in get_xstats_count() */
1761         dev = &rte_eth_devices[port_id];
1762         cnt_used_entries = 0;
1763
1764         for (idx = 0; idx < RTE_NB_STATS; idx++) {
1765                 snprintf(xstats_names[cnt_used_entries].name,
1766                         sizeof(xstats_names[0].name),
1767                         "%s", rte_stats_strings[idx].name);
1768                 cnt_used_entries++;
1769         }
1770         num_q = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
1771         for (id_queue = 0; id_queue < num_q; id_queue++) {
1772                 for (idx = 0; idx < RTE_NB_RXQ_STATS; idx++) {
1773                         snprintf(xstats_names[cnt_used_entries].name,
1774                                 sizeof(xstats_names[0].name),
1775                                 "rx_q%u%s",
1776                                 id_queue, rte_rxq_stats_strings[idx].name);
1777                         cnt_used_entries++;
1778                 }
1779
1780         }
1781         num_q = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
1782         for (id_queue = 0; id_queue < num_q; id_queue++) {
1783                 for (idx = 0; idx < RTE_NB_TXQ_STATS; idx++) {
1784                         snprintf(xstats_names[cnt_used_entries].name,
1785                                 sizeof(xstats_names[0].name),
1786                                 "tx_q%u%s",
1787                                 id_queue, rte_txq_stats_strings[idx].name);
1788                         cnt_used_entries++;
1789                 }
1790         }
1791
1792         if (dev->dev_ops->xstats_get_names != NULL) {
1793                 /* If there are any driver-specific xstats, append them
1794                  * to end of list.
1795                  */
1796                 cnt_driver_entries = (*dev->dev_ops->xstats_get_names)(
1797                         dev,
1798                         xstats_names + cnt_used_entries,
1799                         size - cnt_used_entries);
1800                 if (cnt_driver_entries < 0)
1801                         return cnt_driver_entries;
1802                 cnt_used_entries += cnt_driver_entries;
1803         }
1804
1805         return cnt_used_entries;
1806 }
1807
1808 /* retrieve ethdev extended statistics */
1809 int
1810 rte_eth_xstats_get_by_id(uint16_t port_id, const uint64_t *ids,
1811                          uint64_t *values, unsigned int n)
1812 {
1813         /* If need all xstats */
1814         if (!ids) {
1815                 struct rte_eth_stats eth_stats;
1816                 struct rte_eth_dev *dev;
1817                 unsigned int count = 0, i, q;
1818                 signed int xcount = 0;
1819                 uint64_t val, *stats_ptr;
1820                 uint16_t nb_rxqs, nb_txqs;
1821
1822                 RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1823                 dev = &rte_eth_devices[port_id];
1824
1825                 nb_rxqs = RTE_MIN(dev->data->nb_rx_queues,
1826                                 RTE_ETHDEV_QUEUE_STAT_CNTRS);
1827                 nb_txqs = RTE_MIN(dev->data->nb_tx_queues,
1828                                 RTE_ETHDEV_QUEUE_STAT_CNTRS);
1829
1830                 /* Return generic statistics */
1831                 count = RTE_NB_STATS + (nb_rxqs * RTE_NB_RXQ_STATS) +
1832                         (nb_txqs * RTE_NB_TXQ_STATS);
1833
1834
1835                 /* implemented by the driver */
1836                 if (dev->dev_ops->xstats_get_by_id != NULL) {
1837                         /* Retrieve the xstats from the driver at the end of the
1838                          * xstats struct. Retrieve all xstats.
1839                          */
1840                         xcount = (*dev->dev_ops->xstats_get_by_id)(dev,
1841                                         NULL,
1842                                         values ? values + count : NULL,
1843                                         (n > count) ? n - count : 0);
1844
1845                         if (xcount < 0)
1846                                 return xcount;
1847                 /* implemented by the driver */
1848                 } else if (dev->dev_ops->xstats_get != NULL) {
1849                         /* Retrieve the xstats from the driver at the end of the
1850                          * xstats struct. Retrieve all xstats.
1851                          * Compatibility for PMD without xstats_get_by_ids
1852                          */
1853                         unsigned int size = (n > count) ? n - count : 1;
1854                         struct rte_eth_xstat xstats[size];
1855
1856                         xcount = (*dev->dev_ops->xstats_get)(dev,
1857                                         values ? xstats : NULL, size);
1858
1859                         if (xcount < 0)
1860                                 return xcount;
1861
1862                         if (values != NULL)
1863                                 for (i = 0 ; i < (unsigned int)xcount; i++)
1864                                         values[i + count] = xstats[i].value;
1865                 }
1866
1867                 if (n < count + xcount || values == NULL)
1868                         return count + xcount;
1869
1870                 /* now fill the xstats structure */
1871                 count = 0;
1872                 rte_eth_stats_get(port_id, &eth_stats);
1873
1874                 /* global stats */
1875                 for (i = 0; i < RTE_NB_STATS; i++) {
1876                         stats_ptr = RTE_PTR_ADD(&eth_stats,
1877                                                 rte_stats_strings[i].offset);
1878                         val = *stats_ptr;
1879                         values[count++] = val;
1880                 }
1881
1882                 /* per-rxq stats */
1883                 for (q = 0; q < nb_rxqs; q++) {
1884                         for (i = 0; i < RTE_NB_RXQ_STATS; i++) {
1885                                 stats_ptr = RTE_PTR_ADD(&eth_stats,
1886                                             rte_rxq_stats_strings[i].offset +
1887                                             q * sizeof(uint64_t));
1888                                 val = *stats_ptr;
1889                                 values[count++] = val;
1890                         }
1891                 }
1892
1893                 /* per-txq stats */
1894                 for (q = 0; q < nb_txqs; q++) {
1895                         for (i = 0; i < RTE_NB_TXQ_STATS; i++) {
1896                                 stats_ptr = RTE_PTR_ADD(&eth_stats,
1897                                             rte_txq_stats_strings[i].offset +
1898                                             q * sizeof(uint64_t));
1899                                 val = *stats_ptr;
1900                                 values[count++] = val;
1901                         }
1902                 }
1903
1904                 return count + xcount;
1905         }
1906         /* Need only xstats given by IDS array */
1907         else {
1908                 uint16_t i, size;
1909                 uint64_t *values_copy;
1910
1911                 size = rte_eth_xstats_get_by_id(port_id, NULL, NULL, 0);
1912
1913                 values_copy = malloc(sizeof(*values_copy) * size);
1914                 if (!values_copy) {
1915                         RTE_PMD_DEBUG_TRACE(
1916                             "ERROR: can't allocate memory for values_copy\n");
1917                         return -1;
1918                 }
1919
1920                 rte_eth_xstats_get_by_id(port_id, NULL, values_copy, size);
1921
1922                 for (i = 0; i < n; i++) {
1923                         if (ids[i] >= size) {
1924                                 RTE_PMD_DEBUG_TRACE(
1925                                         "ERROR: id value isn't valid\n");
1926                                 return -1;
1927                         }
1928                         values[i] = values_copy[ids[i]];
1929                 }
1930                 free(values_copy);
1931                 return n;
1932         }
1933 }
1934
1935 int
1936 rte_eth_xstats_get(uint16_t port_id, struct rte_eth_xstat *xstats,
1937         unsigned int n)
1938 {
1939         struct rte_eth_stats eth_stats;
1940         struct rte_eth_dev *dev;
1941         unsigned int count = 0, i, q;
1942         signed int xcount = 0;
1943         uint64_t val, *stats_ptr;
1944         uint16_t nb_rxqs, nb_txqs;
1945
1946         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1947
1948         dev = &rte_eth_devices[port_id];
1949
1950         nb_rxqs = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
1951         nb_txqs = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
1952
1953         /* Return generic statistics */
1954         count = RTE_NB_STATS + (nb_rxqs * RTE_NB_RXQ_STATS) +
1955                 (nb_txqs * RTE_NB_TXQ_STATS);
1956
1957         /* implemented by the driver */
1958         if (dev->dev_ops->xstats_get != NULL) {
1959                 /* Retrieve the xstats from the driver at the end of the
1960                  * xstats struct.
1961                  */
1962                 xcount = (*dev->dev_ops->xstats_get)(dev,
1963                                      xstats ? xstats + count : NULL,
1964                                      (n > count) ? n - count : 0);
1965
1966                 if (xcount < 0)
1967                         return xcount;
1968         }
1969
1970         if (n < count + xcount || xstats == NULL)
1971                 return count + xcount;
1972
1973         /* now fill the xstats structure */
1974         count = 0;
1975         rte_eth_stats_get(port_id, &eth_stats);
1976
1977         /* global stats */
1978         for (i = 0; i < RTE_NB_STATS; i++) {
1979                 stats_ptr = RTE_PTR_ADD(&eth_stats,
1980                                         rte_stats_strings[i].offset);
1981                 val = *stats_ptr;
1982                 xstats[count++].value = val;
1983         }
1984
1985         /* per-rxq stats */
1986         for (q = 0; q < nb_rxqs; q++) {
1987                 for (i = 0; i < RTE_NB_RXQ_STATS; i++) {
1988                         stats_ptr = RTE_PTR_ADD(&eth_stats,
1989                                         rte_rxq_stats_strings[i].offset +
1990                                         q * sizeof(uint64_t));
1991                         val = *stats_ptr;
1992                         xstats[count++].value = val;
1993                 }
1994         }
1995
1996         /* per-txq stats */
1997         for (q = 0; q < nb_txqs; q++) {
1998                 for (i = 0; i < RTE_NB_TXQ_STATS; i++) {
1999                         stats_ptr = RTE_PTR_ADD(&eth_stats,
2000                                         rte_txq_stats_strings[i].offset +
2001                                         q * sizeof(uint64_t));
2002                         val = *stats_ptr;
2003                         xstats[count++].value = val;
2004                 }
2005         }
2006
2007         for (i = 0; i < count; i++)
2008                 xstats[i].id = i;
2009         /* add an offset to driver-specific stats */
2010         for ( ; i < count + xcount; i++)
2011                 xstats[i].id += count;
2012
2013         return count + xcount;
2014 }
2015
2016 /* reset ethdev extended statistics */
2017 void
2018 rte_eth_xstats_reset(uint16_t port_id)
2019 {
2020         struct rte_eth_dev *dev;
2021
2022         RTE_ETH_VALID_PORTID_OR_RET(port_id);
2023         dev = &rte_eth_devices[port_id];
2024
2025         /* implemented by the driver */
2026         if (dev->dev_ops->xstats_reset != NULL) {
2027                 (*dev->dev_ops->xstats_reset)(dev);
2028                 return;
2029         }
2030
2031         /* fallback to default */
2032         rte_eth_stats_reset(port_id);
2033 }
2034
2035 static int
2036 set_queue_stats_mapping(uint16_t port_id, uint16_t queue_id, uint8_t stat_idx,
2037                 uint8_t is_rx)
2038 {
2039         struct rte_eth_dev *dev;
2040
2041         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2042
2043         dev = &rte_eth_devices[port_id];
2044
2045         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_stats_mapping_set, -ENOTSUP);
2046         return (*dev->dev_ops->queue_stats_mapping_set)
2047                         (dev, queue_id, stat_idx, is_rx);
2048 }
2049
2050
2051 int
2052 rte_eth_dev_set_tx_queue_stats_mapping(uint16_t port_id, uint16_t tx_queue_id,
2053                 uint8_t stat_idx)
2054 {
2055         return set_queue_stats_mapping(port_id, tx_queue_id, stat_idx,
2056                         STAT_QMAP_TX);
2057 }
2058
2059
2060 int
2061 rte_eth_dev_set_rx_queue_stats_mapping(uint16_t port_id, uint16_t rx_queue_id,
2062                 uint8_t stat_idx)
2063 {
2064         return set_queue_stats_mapping(port_id, rx_queue_id, stat_idx,
2065                         STAT_QMAP_RX);
2066 }
2067
2068 int
2069 rte_eth_dev_fw_version_get(uint16_t port_id, char *fw_version, size_t fw_size)
2070 {
2071         struct rte_eth_dev *dev;
2072
2073         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2074         dev = &rte_eth_devices[port_id];
2075
2076         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fw_version_get, -ENOTSUP);
2077         return (*dev->dev_ops->fw_version_get)(dev, fw_version, fw_size);
2078 }
2079
2080 void
2081 rte_eth_dev_info_get(uint16_t port_id, struct rte_eth_dev_info *dev_info)
2082 {
2083         struct rte_eth_dev *dev;
2084         const struct rte_eth_desc_lim lim = {
2085                 .nb_max = UINT16_MAX,
2086                 .nb_min = 0,
2087                 .nb_align = 1,
2088         };
2089
2090         RTE_ETH_VALID_PORTID_OR_RET(port_id);
2091         dev = &rte_eth_devices[port_id];
2092
2093         memset(dev_info, 0, sizeof(struct rte_eth_dev_info));
2094         dev_info->rx_desc_lim = lim;
2095         dev_info->tx_desc_lim = lim;
2096
2097         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
2098         (*dev->dev_ops->dev_infos_get)(dev, dev_info);
2099         dev_info->driver_name = dev->device->driver->name;
2100         dev_info->nb_rx_queues = dev->data->nb_rx_queues;
2101         dev_info->nb_tx_queues = dev->data->nb_tx_queues;
2102 }
2103
2104 int
2105 rte_eth_dev_get_supported_ptypes(uint16_t port_id, uint32_t ptype_mask,
2106                                  uint32_t *ptypes, int num)
2107 {
2108         int i, j;
2109         struct rte_eth_dev *dev;
2110         const uint32_t *all_ptypes;
2111
2112         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2113         dev = &rte_eth_devices[port_id];
2114         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_supported_ptypes_get, 0);
2115         all_ptypes = (*dev->dev_ops->dev_supported_ptypes_get)(dev);
2116
2117         if (!all_ptypes)
2118                 return 0;
2119
2120         for (i = 0, j = 0; all_ptypes[i] != RTE_PTYPE_UNKNOWN; ++i)
2121                 if (all_ptypes[i] & ptype_mask) {
2122                         if (j < num)
2123                                 ptypes[j] = all_ptypes[i];
2124                         j++;
2125                 }
2126
2127         return j;
2128 }
2129
2130 void
2131 rte_eth_macaddr_get(uint16_t port_id, struct ether_addr *mac_addr)
2132 {
2133         struct rte_eth_dev *dev;
2134
2135         RTE_ETH_VALID_PORTID_OR_RET(port_id);
2136         dev = &rte_eth_devices[port_id];
2137         ether_addr_copy(&dev->data->mac_addrs[0], mac_addr);
2138 }
2139
2140
2141 int
2142 rte_eth_dev_get_mtu(uint16_t port_id, uint16_t *mtu)
2143 {
2144         struct rte_eth_dev *dev;
2145
2146         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2147
2148         dev = &rte_eth_devices[port_id];
2149         *mtu = dev->data->mtu;
2150         return 0;
2151 }
2152
2153 int
2154 rte_eth_dev_set_mtu(uint16_t port_id, uint16_t mtu)
2155 {
2156         int ret;
2157         struct rte_eth_dev *dev;
2158
2159         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2160         dev = &rte_eth_devices[port_id];
2161         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mtu_set, -ENOTSUP);
2162
2163         ret = (*dev->dev_ops->mtu_set)(dev, mtu);
2164         if (!ret)
2165                 dev->data->mtu = mtu;
2166
2167         return ret;
2168 }
2169
2170 int
2171 rte_eth_dev_vlan_filter(uint16_t port_id, uint16_t vlan_id, int on)
2172 {
2173         struct rte_eth_dev *dev;
2174         int ret;
2175
2176         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2177         dev = &rte_eth_devices[port_id];
2178         if (!(dev->data->dev_conf.rxmode.offloads &
2179               DEV_RX_OFFLOAD_VLAN_FILTER)) {
2180                 RTE_PMD_DEBUG_TRACE("port %d: vlan-filtering disabled\n", port_id);
2181                 return -ENOSYS;
2182         }
2183
2184         if (vlan_id > 4095) {
2185                 RTE_PMD_DEBUG_TRACE("(port_id=%d) invalid vlan_id=%u > 4095\n",
2186                                 port_id, (unsigned) vlan_id);
2187                 return -EINVAL;
2188         }
2189         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_filter_set, -ENOTSUP);
2190
2191         ret = (*dev->dev_ops->vlan_filter_set)(dev, vlan_id, on);
2192         if (ret == 0) {
2193                 struct rte_vlan_filter_conf *vfc;
2194                 int vidx;
2195                 int vbit;
2196
2197                 vfc = &dev->data->vlan_filter_conf;
2198                 vidx = vlan_id / 64;
2199                 vbit = vlan_id % 64;
2200
2201                 if (on)
2202                         vfc->ids[vidx] |= UINT64_C(1) << vbit;
2203                 else
2204                         vfc->ids[vidx] &= ~(UINT64_C(1) << vbit);
2205         }
2206
2207         return ret;
2208 }
2209
2210 int
2211 rte_eth_dev_set_vlan_strip_on_queue(uint16_t port_id, uint16_t rx_queue_id,
2212                                     int on)
2213 {
2214         struct rte_eth_dev *dev;
2215
2216         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2217         dev = &rte_eth_devices[port_id];
2218         if (rx_queue_id >= dev->data->nb_rx_queues) {
2219                 RTE_PMD_DEBUG_TRACE("Invalid rx_queue_id=%d\n", port_id);
2220                 return -EINVAL;
2221         }
2222
2223         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_strip_queue_set, -ENOTSUP);
2224         (*dev->dev_ops->vlan_strip_queue_set)(dev, rx_queue_id, on);
2225
2226         return 0;
2227 }
2228
2229 int
2230 rte_eth_dev_set_vlan_ether_type(uint16_t port_id,
2231                                 enum rte_vlan_type vlan_type,
2232                                 uint16_t tpid)
2233 {
2234         struct rte_eth_dev *dev;
2235
2236         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2237         dev = &rte_eth_devices[port_id];
2238         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_tpid_set, -ENOTSUP);
2239
2240         return (*dev->dev_ops->vlan_tpid_set)(dev, vlan_type, tpid);
2241 }
2242
2243 int
2244 rte_eth_dev_set_vlan_offload(uint16_t port_id, int offload_mask)
2245 {
2246         struct rte_eth_dev *dev;
2247         int ret = 0;
2248         int mask = 0;
2249         int cur, org = 0;
2250
2251         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2252         dev = &rte_eth_devices[port_id];
2253
2254         /*check which option changed by application*/
2255         cur = !!(offload_mask & ETH_VLAN_STRIP_OFFLOAD);
2256         org = !!(dev->data->dev_conf.rxmode.offloads &
2257                  DEV_RX_OFFLOAD_VLAN_STRIP);
2258         if (cur != org) {
2259                 if (cur)
2260                         dev->data->dev_conf.rxmode.offloads |=
2261                                 DEV_RX_OFFLOAD_VLAN_STRIP;
2262                 else
2263                         dev->data->dev_conf.rxmode.offloads &=
2264                                 ~DEV_RX_OFFLOAD_VLAN_STRIP;
2265                 mask |= ETH_VLAN_STRIP_MASK;
2266         }
2267
2268         cur = !!(offload_mask & ETH_VLAN_FILTER_OFFLOAD);
2269         org = !!(dev->data->dev_conf.rxmode.offloads &
2270                  DEV_RX_OFFLOAD_VLAN_FILTER);
2271         if (cur != org) {
2272                 if (cur)
2273                         dev->data->dev_conf.rxmode.offloads |=
2274                                 DEV_RX_OFFLOAD_VLAN_FILTER;
2275                 else
2276                         dev->data->dev_conf.rxmode.offloads &=
2277                                 ~DEV_RX_OFFLOAD_VLAN_FILTER;
2278                 mask |= ETH_VLAN_FILTER_MASK;
2279         }
2280
2281         cur = !!(offload_mask & ETH_VLAN_EXTEND_OFFLOAD);
2282         org = !!(dev->data->dev_conf.rxmode.offloads &
2283                  DEV_RX_OFFLOAD_VLAN_EXTEND);
2284         if (cur != org) {
2285                 if (cur)
2286                         dev->data->dev_conf.rxmode.offloads |=
2287                                 DEV_RX_OFFLOAD_VLAN_EXTEND;
2288                 else
2289                         dev->data->dev_conf.rxmode.offloads &=
2290                                 ~DEV_RX_OFFLOAD_VLAN_EXTEND;
2291                 mask |= ETH_VLAN_EXTEND_MASK;
2292         }
2293
2294         /*no change*/
2295         if (mask == 0)
2296                 return ret;
2297
2298         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_offload_set, -ENOTSUP);
2299
2300         /*
2301          * Convert to the offload bitfield API just in case the underlying PMD
2302          * still supporting it.
2303          */
2304         rte_eth_convert_rx_offloads(dev->data->dev_conf.rxmode.offloads,
2305                                     &dev->data->dev_conf.rxmode);
2306         (*dev->dev_ops->vlan_offload_set)(dev, mask);
2307
2308         return ret;
2309 }
2310
2311 int
2312 rte_eth_dev_get_vlan_offload(uint16_t port_id)
2313 {
2314         struct rte_eth_dev *dev;
2315         int ret = 0;
2316
2317         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2318         dev = &rte_eth_devices[port_id];
2319
2320         if (dev->data->dev_conf.rxmode.offloads &
2321             DEV_RX_OFFLOAD_VLAN_STRIP)
2322                 ret |= ETH_VLAN_STRIP_OFFLOAD;
2323
2324         if (dev->data->dev_conf.rxmode.offloads &
2325             DEV_RX_OFFLOAD_VLAN_FILTER)
2326                 ret |= ETH_VLAN_FILTER_OFFLOAD;
2327
2328         if (dev->data->dev_conf.rxmode.offloads &
2329             DEV_RX_OFFLOAD_VLAN_EXTEND)
2330                 ret |= ETH_VLAN_EXTEND_OFFLOAD;
2331
2332         return ret;
2333 }
2334
2335 int
2336 rte_eth_dev_set_vlan_pvid(uint16_t port_id, uint16_t pvid, int on)
2337 {
2338         struct rte_eth_dev *dev;
2339
2340         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2341         dev = &rte_eth_devices[port_id];
2342         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_pvid_set, -ENOTSUP);
2343         (*dev->dev_ops->vlan_pvid_set)(dev, pvid, on);
2344
2345         return 0;
2346 }
2347
2348 int
2349 rte_eth_dev_flow_ctrl_get(uint16_t port_id, struct rte_eth_fc_conf *fc_conf)
2350 {
2351         struct rte_eth_dev *dev;
2352
2353         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2354         dev = &rte_eth_devices[port_id];
2355         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_get, -ENOTSUP);
2356         memset(fc_conf, 0, sizeof(*fc_conf));
2357         return (*dev->dev_ops->flow_ctrl_get)(dev, fc_conf);
2358 }
2359
2360 int
2361 rte_eth_dev_flow_ctrl_set(uint16_t port_id, struct rte_eth_fc_conf *fc_conf)
2362 {
2363         struct rte_eth_dev *dev;
2364
2365         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2366         if ((fc_conf->send_xon != 0) && (fc_conf->send_xon != 1)) {
2367                 RTE_PMD_DEBUG_TRACE("Invalid send_xon, only 0/1 allowed\n");
2368                 return -EINVAL;
2369         }
2370
2371         dev = &rte_eth_devices[port_id];
2372         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_set, -ENOTSUP);
2373         return (*dev->dev_ops->flow_ctrl_set)(dev, fc_conf);
2374 }
2375
2376 int
2377 rte_eth_dev_priority_flow_ctrl_set(uint16_t port_id,
2378                                    struct rte_eth_pfc_conf *pfc_conf)
2379 {
2380         struct rte_eth_dev *dev;
2381
2382         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2383         if (pfc_conf->priority > (ETH_DCB_NUM_USER_PRIORITIES - 1)) {
2384                 RTE_PMD_DEBUG_TRACE("Invalid priority, only 0-7 allowed\n");
2385                 return -EINVAL;
2386         }
2387
2388         dev = &rte_eth_devices[port_id];
2389         /* High water, low water validation are device specific */
2390         if  (*dev->dev_ops->priority_flow_ctrl_set)
2391                 return (*dev->dev_ops->priority_flow_ctrl_set)(dev, pfc_conf);
2392         return -ENOTSUP;
2393 }
2394
2395 static int
2396 rte_eth_check_reta_mask(struct rte_eth_rss_reta_entry64 *reta_conf,
2397                         uint16_t reta_size)
2398 {
2399         uint16_t i, num;
2400
2401         if (!reta_conf)
2402                 return -EINVAL;
2403
2404         num = (reta_size + RTE_RETA_GROUP_SIZE - 1) / RTE_RETA_GROUP_SIZE;
2405         for (i = 0; i < num; i++) {
2406                 if (reta_conf[i].mask)
2407                         return 0;
2408         }
2409
2410         return -EINVAL;
2411 }
2412
2413 static int
2414 rte_eth_check_reta_entry(struct rte_eth_rss_reta_entry64 *reta_conf,
2415                          uint16_t reta_size,
2416                          uint16_t max_rxq)
2417 {
2418         uint16_t i, idx, shift;
2419
2420         if (!reta_conf)
2421                 return -EINVAL;
2422
2423         if (max_rxq == 0) {
2424                 RTE_PMD_DEBUG_TRACE("No receive queue is available\n");
2425                 return -EINVAL;
2426         }
2427
2428         for (i = 0; i < reta_size; i++) {
2429                 idx = i / RTE_RETA_GROUP_SIZE;
2430                 shift = i % RTE_RETA_GROUP_SIZE;
2431                 if ((reta_conf[idx].mask & (1ULL << shift)) &&
2432                         (reta_conf[idx].reta[shift] >= max_rxq)) {
2433                         RTE_PMD_DEBUG_TRACE("reta_conf[%u]->reta[%u]: %u exceeds "
2434                                 "the maximum rxq index: %u\n", idx, shift,
2435                                 reta_conf[idx].reta[shift], max_rxq);
2436                         return -EINVAL;
2437                 }
2438         }
2439
2440         return 0;
2441 }
2442
2443 int
2444 rte_eth_dev_rss_reta_update(uint16_t port_id,
2445                             struct rte_eth_rss_reta_entry64 *reta_conf,
2446                             uint16_t reta_size)
2447 {
2448         struct rte_eth_dev *dev;
2449         int ret;
2450
2451         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2452         /* Check mask bits */
2453         ret = rte_eth_check_reta_mask(reta_conf, reta_size);
2454         if (ret < 0)
2455                 return ret;
2456
2457         dev = &rte_eth_devices[port_id];
2458
2459         /* Check entry value */
2460         ret = rte_eth_check_reta_entry(reta_conf, reta_size,
2461                                 dev->data->nb_rx_queues);
2462         if (ret < 0)
2463                 return ret;
2464
2465         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->reta_update, -ENOTSUP);
2466         return (*dev->dev_ops->reta_update)(dev, reta_conf, reta_size);
2467 }
2468
2469 int
2470 rte_eth_dev_rss_reta_query(uint16_t port_id,
2471                            struct rte_eth_rss_reta_entry64 *reta_conf,
2472                            uint16_t reta_size)
2473 {
2474         struct rte_eth_dev *dev;
2475         int ret;
2476
2477         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2478
2479         /* Check mask bits */
2480         ret = rte_eth_check_reta_mask(reta_conf, reta_size);
2481         if (ret < 0)
2482                 return ret;
2483
2484         dev = &rte_eth_devices[port_id];
2485         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->reta_query, -ENOTSUP);
2486         return (*dev->dev_ops->reta_query)(dev, reta_conf, reta_size);
2487 }
2488
2489 int
2490 rte_eth_dev_rss_hash_update(uint16_t port_id,
2491                             struct rte_eth_rss_conf *rss_conf)
2492 {
2493         struct rte_eth_dev *dev;
2494
2495         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2496         dev = &rte_eth_devices[port_id];
2497         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rss_hash_update, -ENOTSUP);
2498         return (*dev->dev_ops->rss_hash_update)(dev, rss_conf);
2499 }
2500
2501 int
2502 rte_eth_dev_rss_hash_conf_get(uint16_t port_id,
2503                               struct rte_eth_rss_conf *rss_conf)
2504 {
2505         struct rte_eth_dev *dev;
2506
2507         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2508         dev = &rte_eth_devices[port_id];
2509         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rss_hash_conf_get, -ENOTSUP);
2510         return (*dev->dev_ops->rss_hash_conf_get)(dev, rss_conf);
2511 }
2512
2513 int
2514 rte_eth_dev_udp_tunnel_port_add(uint16_t port_id,
2515                                 struct rte_eth_udp_tunnel *udp_tunnel)
2516 {
2517         struct rte_eth_dev *dev;
2518
2519         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2520         if (udp_tunnel == NULL) {
2521                 RTE_PMD_DEBUG_TRACE("Invalid udp_tunnel parameter\n");
2522                 return -EINVAL;
2523         }
2524
2525         if (udp_tunnel->prot_type >= RTE_TUNNEL_TYPE_MAX) {
2526                 RTE_PMD_DEBUG_TRACE("Invalid tunnel type\n");
2527                 return -EINVAL;
2528         }
2529
2530         dev = &rte_eth_devices[port_id];
2531         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->udp_tunnel_port_add, -ENOTSUP);
2532         return (*dev->dev_ops->udp_tunnel_port_add)(dev, udp_tunnel);
2533 }
2534
2535 int
2536 rte_eth_dev_udp_tunnel_port_delete(uint16_t port_id,
2537                                    struct rte_eth_udp_tunnel *udp_tunnel)
2538 {
2539         struct rte_eth_dev *dev;
2540
2541         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2542         dev = &rte_eth_devices[port_id];
2543
2544         if (udp_tunnel == NULL) {
2545                 RTE_PMD_DEBUG_TRACE("Invalid udp_tunnel parameter\n");
2546                 return -EINVAL;
2547         }
2548
2549         if (udp_tunnel->prot_type >= RTE_TUNNEL_TYPE_MAX) {
2550                 RTE_PMD_DEBUG_TRACE("Invalid tunnel type\n");
2551                 return -EINVAL;
2552         }
2553
2554         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->udp_tunnel_port_del, -ENOTSUP);
2555         return (*dev->dev_ops->udp_tunnel_port_del)(dev, udp_tunnel);
2556 }
2557
2558 int
2559 rte_eth_led_on(uint16_t port_id)
2560 {
2561         struct rte_eth_dev *dev;
2562
2563         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2564         dev = &rte_eth_devices[port_id];
2565         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_on, -ENOTSUP);
2566         return (*dev->dev_ops->dev_led_on)(dev);
2567 }
2568
2569 int
2570 rte_eth_led_off(uint16_t port_id)
2571 {
2572         struct rte_eth_dev *dev;
2573
2574         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2575         dev = &rte_eth_devices[port_id];
2576         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_off, -ENOTSUP);
2577         return (*dev->dev_ops->dev_led_off)(dev);
2578 }
2579
2580 /*
2581  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
2582  * an empty spot.
2583  */
2584 static int
2585 get_mac_addr_index(uint16_t port_id, const struct ether_addr *addr)
2586 {
2587         struct rte_eth_dev_info dev_info;
2588         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
2589         unsigned i;
2590
2591         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2592         rte_eth_dev_info_get(port_id, &dev_info);
2593
2594         for (i = 0; i < dev_info.max_mac_addrs; i++)
2595                 if (memcmp(addr, &dev->data->mac_addrs[i], ETHER_ADDR_LEN) == 0)
2596                         return i;
2597
2598         return -1;
2599 }
2600
2601 static const struct ether_addr null_mac_addr;
2602
2603 int
2604 rte_eth_dev_mac_addr_add(uint16_t port_id, struct ether_addr *addr,
2605                         uint32_t pool)
2606 {
2607         struct rte_eth_dev *dev;
2608         int index;
2609         uint64_t pool_mask;
2610         int ret;
2611
2612         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2613         dev = &rte_eth_devices[port_id];
2614         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_add, -ENOTSUP);
2615
2616         if (is_zero_ether_addr(addr)) {
2617                 RTE_PMD_DEBUG_TRACE("port %d: Cannot add NULL MAC address\n",
2618                         port_id);
2619                 return -EINVAL;
2620         }
2621         if (pool >= ETH_64_POOLS) {
2622                 RTE_PMD_DEBUG_TRACE("pool id must be 0-%d\n", ETH_64_POOLS - 1);
2623                 return -EINVAL;
2624         }
2625
2626         index = get_mac_addr_index(port_id, addr);
2627         if (index < 0) {
2628                 index = get_mac_addr_index(port_id, &null_mac_addr);
2629                 if (index < 0) {
2630                         RTE_PMD_DEBUG_TRACE("port %d: MAC address array full\n",
2631                                 port_id);
2632                         return -ENOSPC;
2633                 }
2634         } else {
2635                 pool_mask = dev->data->mac_pool_sel[index];
2636
2637                 /* Check if both MAC address and pool is already there, and do nothing */
2638                 if (pool_mask & (1ULL << pool))
2639                         return 0;
2640         }
2641
2642         /* Update NIC */
2643         ret = (*dev->dev_ops->mac_addr_add)(dev, addr, index, pool);
2644
2645         if (ret == 0) {
2646                 /* Update address in NIC data structure */
2647                 ether_addr_copy(addr, &dev->data->mac_addrs[index]);
2648
2649                 /* Update pool bitmap in NIC data structure */
2650                 dev->data->mac_pool_sel[index] |= (1ULL << pool);
2651         }
2652
2653         return ret;
2654 }
2655
2656 int
2657 rte_eth_dev_mac_addr_remove(uint16_t port_id, struct ether_addr *addr)
2658 {
2659         struct rte_eth_dev *dev;
2660         int index;
2661
2662         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2663         dev = &rte_eth_devices[port_id];
2664         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_remove, -ENOTSUP);
2665
2666         index = get_mac_addr_index(port_id, addr);
2667         if (index == 0) {
2668                 RTE_PMD_DEBUG_TRACE("port %d: Cannot remove default MAC address\n", port_id);
2669                 return -EADDRINUSE;
2670         } else if (index < 0)
2671                 return 0;  /* Do nothing if address wasn't found */
2672
2673         /* Update NIC */
2674         (*dev->dev_ops->mac_addr_remove)(dev, index);
2675
2676         /* Update address in NIC data structure */
2677         ether_addr_copy(&null_mac_addr, &dev->data->mac_addrs[index]);
2678
2679         /* reset pool bitmap */
2680         dev->data->mac_pool_sel[index] = 0;
2681
2682         return 0;
2683 }
2684
2685 int
2686 rte_eth_dev_default_mac_addr_set(uint16_t port_id, struct ether_addr *addr)
2687 {
2688         struct rte_eth_dev *dev;
2689
2690         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2691
2692         if (!is_valid_assigned_ether_addr(addr))
2693                 return -EINVAL;
2694
2695         dev = &rte_eth_devices[port_id];
2696         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_set, -ENOTSUP);
2697
2698         /* Update default address in NIC data structure */
2699         ether_addr_copy(addr, &dev->data->mac_addrs[0]);
2700
2701         (*dev->dev_ops->mac_addr_set)(dev, addr);
2702
2703         return 0;
2704 }
2705
2706
2707 /*
2708  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
2709  * an empty spot.
2710  */
2711 static int
2712 get_hash_mac_addr_index(uint16_t port_id, const struct ether_addr *addr)
2713 {
2714         struct rte_eth_dev_info dev_info;
2715         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
2716         unsigned i;
2717
2718         rte_eth_dev_info_get(port_id, &dev_info);
2719         if (!dev->data->hash_mac_addrs)
2720                 return -1;
2721
2722         for (i = 0; i < dev_info.max_hash_mac_addrs; i++)
2723                 if (memcmp(addr, &dev->data->hash_mac_addrs[i],
2724                         ETHER_ADDR_LEN) == 0)
2725                         return i;
2726
2727         return -1;
2728 }
2729
2730 int
2731 rte_eth_dev_uc_hash_table_set(uint16_t port_id, struct ether_addr *addr,
2732                                 uint8_t on)
2733 {
2734         int index;
2735         int ret;
2736         struct rte_eth_dev *dev;
2737
2738         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2739
2740         dev = &rte_eth_devices[port_id];
2741         if (is_zero_ether_addr(addr)) {
2742                 RTE_PMD_DEBUG_TRACE("port %d: Cannot add NULL MAC address\n",
2743                         port_id);
2744                 return -EINVAL;
2745         }
2746
2747         index = get_hash_mac_addr_index(port_id, addr);
2748         /* Check if it's already there, and do nothing */
2749         if ((index >= 0) && (on))
2750                 return 0;
2751
2752         if (index < 0) {
2753                 if (!on) {
2754                         RTE_PMD_DEBUG_TRACE("port %d: the MAC address was not "
2755                                 "set in UTA\n", port_id);
2756                         return -EINVAL;
2757                 }
2758
2759                 index = get_hash_mac_addr_index(port_id, &null_mac_addr);
2760                 if (index < 0) {
2761                         RTE_PMD_DEBUG_TRACE("port %d: MAC address array full\n",
2762                                         port_id);
2763                         return -ENOSPC;
2764                 }
2765         }
2766
2767         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->uc_hash_table_set, -ENOTSUP);
2768         ret = (*dev->dev_ops->uc_hash_table_set)(dev, addr, on);
2769         if (ret == 0) {
2770                 /* Update address in NIC data structure */
2771                 if (on)
2772                         ether_addr_copy(addr,
2773                                         &dev->data->hash_mac_addrs[index]);
2774                 else
2775                         ether_addr_copy(&null_mac_addr,
2776                                         &dev->data->hash_mac_addrs[index]);
2777         }
2778
2779         return ret;
2780 }
2781
2782 int
2783 rte_eth_dev_uc_all_hash_table_set(uint16_t port_id, uint8_t on)
2784 {
2785         struct rte_eth_dev *dev;
2786
2787         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2788
2789         dev = &rte_eth_devices[port_id];
2790
2791         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->uc_all_hash_table_set, -ENOTSUP);
2792         return (*dev->dev_ops->uc_all_hash_table_set)(dev, on);
2793 }
2794
2795 int rte_eth_set_queue_rate_limit(uint16_t port_id, uint16_t queue_idx,
2796                                         uint16_t tx_rate)
2797 {
2798         struct rte_eth_dev *dev;
2799         struct rte_eth_dev_info dev_info;
2800         struct rte_eth_link link;
2801
2802         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2803
2804         dev = &rte_eth_devices[port_id];
2805         rte_eth_dev_info_get(port_id, &dev_info);
2806         link = dev->data->dev_link;
2807
2808         if (queue_idx > dev_info.max_tx_queues) {
2809                 RTE_PMD_DEBUG_TRACE("set queue rate limit:port %d: "
2810                                 "invalid queue id=%d\n", port_id, queue_idx);
2811                 return -EINVAL;
2812         }
2813
2814         if (tx_rate > link.link_speed) {
2815                 RTE_PMD_DEBUG_TRACE("set queue rate limit:invalid tx_rate=%d, "
2816                                 "bigger than link speed= %d\n",
2817                         tx_rate, link.link_speed);
2818                 return -EINVAL;
2819         }
2820
2821         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_queue_rate_limit, -ENOTSUP);
2822         return (*dev->dev_ops->set_queue_rate_limit)(dev, queue_idx, tx_rate);
2823 }
2824
2825 int
2826 rte_eth_mirror_rule_set(uint16_t port_id,
2827                         struct rte_eth_mirror_conf *mirror_conf,
2828                         uint8_t rule_id, uint8_t on)
2829 {
2830         struct rte_eth_dev *dev;
2831
2832         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2833         if (mirror_conf->rule_type == 0) {
2834                 RTE_PMD_DEBUG_TRACE("mirror rule type can not be 0.\n");
2835                 return -EINVAL;
2836         }
2837
2838         if (mirror_conf->dst_pool >= ETH_64_POOLS) {
2839                 RTE_PMD_DEBUG_TRACE("Invalid dst pool, pool id must be 0-%d\n",
2840                                 ETH_64_POOLS - 1);
2841                 return -EINVAL;
2842         }
2843
2844         if ((mirror_conf->rule_type & (ETH_MIRROR_VIRTUAL_POOL_UP |
2845              ETH_MIRROR_VIRTUAL_POOL_DOWN)) &&
2846             (mirror_conf->pool_mask == 0)) {
2847                 RTE_PMD_DEBUG_TRACE("Invalid mirror pool, pool mask can not be 0.\n");
2848                 return -EINVAL;
2849         }
2850
2851         if ((mirror_conf->rule_type & ETH_MIRROR_VLAN) &&
2852             mirror_conf->vlan.vlan_mask == 0) {
2853                 RTE_PMD_DEBUG_TRACE("Invalid vlan mask, vlan mask can not be 0.\n");
2854                 return -EINVAL;
2855         }
2856
2857         dev = &rte_eth_devices[port_id];
2858         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mirror_rule_set, -ENOTSUP);
2859
2860         return (*dev->dev_ops->mirror_rule_set)(dev, mirror_conf, rule_id, on);
2861 }
2862
2863 int
2864 rte_eth_mirror_rule_reset(uint16_t port_id, uint8_t rule_id)
2865 {
2866         struct rte_eth_dev *dev;
2867
2868         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2869
2870         dev = &rte_eth_devices[port_id];
2871         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mirror_rule_reset, -ENOTSUP);
2872
2873         return (*dev->dev_ops->mirror_rule_reset)(dev, rule_id);
2874 }
2875
2876 int
2877 rte_eth_dev_callback_register(uint16_t port_id,
2878                         enum rte_eth_event_type event,
2879                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
2880 {
2881         struct rte_eth_dev *dev;
2882         struct rte_eth_dev_callback *user_cb;
2883
2884         if (!cb_fn)
2885                 return -EINVAL;
2886
2887         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
2888
2889         dev = &rte_eth_devices[port_id];
2890         rte_spinlock_lock(&rte_eth_dev_cb_lock);
2891
2892         TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
2893                 if (user_cb->cb_fn == cb_fn &&
2894                         user_cb->cb_arg == cb_arg &&
2895                         user_cb->event == event) {
2896                         break;
2897                 }
2898         }
2899
2900         /* create a new callback. */
2901         if (user_cb == NULL) {
2902                 user_cb = rte_zmalloc("INTR_USER_CALLBACK",
2903                                         sizeof(struct rte_eth_dev_callback), 0);
2904                 if (user_cb != NULL) {
2905                         user_cb->cb_fn = cb_fn;
2906                         user_cb->cb_arg = cb_arg;
2907                         user_cb->event = event;
2908                         TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
2909                 }
2910         }
2911
2912         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2913         return (user_cb == NULL) ? -ENOMEM : 0;
2914 }
2915
2916 int
2917 rte_eth_dev_callback_unregister(uint16_t port_id,
2918                         enum rte_eth_event_type event,
2919                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
2920 {
2921         int ret;
2922         struct rte_eth_dev *dev;
2923         struct rte_eth_dev_callback *cb, *next;
2924
2925         if (!cb_fn)
2926                 return -EINVAL;
2927
2928         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
2929
2930         dev = &rte_eth_devices[port_id];
2931         rte_spinlock_lock(&rte_eth_dev_cb_lock);
2932
2933         ret = 0;
2934         for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL; cb = next) {
2935
2936                 next = TAILQ_NEXT(cb, next);
2937
2938                 if (cb->cb_fn != cb_fn || cb->event != event ||
2939                                 (cb->cb_arg != (void *)-1 &&
2940                                 cb->cb_arg != cb_arg))
2941                         continue;
2942
2943                 /*
2944                  * if this callback is not executing right now,
2945                  * then remove it.
2946                  */
2947                 if (cb->active == 0) {
2948                         TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
2949                         rte_free(cb);
2950                 } else {
2951                         ret = -EAGAIN;
2952                 }
2953         }
2954
2955         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2956         return ret;
2957 }
2958
2959 int
2960 _rte_eth_dev_callback_process(struct rte_eth_dev *dev,
2961         enum rte_eth_event_type event, void *cb_arg, void *ret_param)
2962 {
2963         struct rte_eth_dev_callback *cb_lst;
2964         struct rte_eth_dev_callback dev_cb;
2965         int rc = 0;
2966
2967         rte_spinlock_lock(&rte_eth_dev_cb_lock);
2968         TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
2969                 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
2970                         continue;
2971                 dev_cb = *cb_lst;
2972                 cb_lst->active = 1;
2973                 if (cb_arg != NULL)
2974                         dev_cb.cb_arg = cb_arg;
2975                 if (ret_param != NULL)
2976                         dev_cb.ret_param = ret_param;
2977
2978                 rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2979                 rc = dev_cb.cb_fn(dev->data->port_id, dev_cb.event,
2980                                 dev_cb.cb_arg, dev_cb.ret_param);
2981                 rte_spinlock_lock(&rte_eth_dev_cb_lock);
2982                 cb_lst->active = 0;
2983         }
2984         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2985         return rc;
2986 }
2987
2988 int
2989 rte_eth_dev_rx_intr_ctl(uint16_t port_id, int epfd, int op, void *data)
2990 {
2991         uint32_t vec;
2992         struct rte_eth_dev *dev;
2993         struct rte_intr_handle *intr_handle;
2994         uint16_t qid;
2995         int rc;
2996
2997         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2998
2999         dev = &rte_eth_devices[port_id];
3000
3001         if (!dev->intr_handle) {
3002                 RTE_PMD_DEBUG_TRACE("RX Intr handle unset\n");
3003                 return -ENOTSUP;
3004         }
3005
3006         intr_handle = dev->intr_handle;
3007         if (!intr_handle->intr_vec) {
3008                 RTE_PMD_DEBUG_TRACE("RX Intr vector unset\n");
3009                 return -EPERM;
3010         }
3011
3012         for (qid = 0; qid < dev->data->nb_rx_queues; qid++) {
3013                 vec = intr_handle->intr_vec[qid];
3014                 rc = rte_intr_rx_ctl(intr_handle, epfd, op, vec, data);
3015                 if (rc && rc != -EEXIST) {
3016                         RTE_PMD_DEBUG_TRACE("p %u q %u rx ctl error"
3017                                         " op %d epfd %d vec %u\n",
3018                                         port_id, qid, op, epfd, vec);
3019                 }
3020         }
3021
3022         return 0;
3023 }
3024
3025 const struct rte_memzone *
3026 rte_eth_dma_zone_reserve(const struct rte_eth_dev *dev, const char *ring_name,
3027                          uint16_t queue_id, size_t size, unsigned align,
3028                          int socket_id)
3029 {
3030         char z_name[RTE_MEMZONE_NAMESIZE];
3031         const struct rte_memzone *mz;
3032
3033         snprintf(z_name, sizeof(z_name), "%s_%s_%d_%d",
3034                  dev->device->driver->name, ring_name,
3035                  dev->data->port_id, queue_id);
3036
3037         mz = rte_memzone_lookup(z_name);
3038         if (mz)
3039                 return mz;
3040
3041         return rte_memzone_reserve_aligned(z_name, size, socket_id, 0, align);
3042 }
3043
3044 int
3045 rte_eth_dev_rx_intr_ctl_q(uint16_t port_id, uint16_t queue_id,
3046                           int epfd, int op, void *data)
3047 {
3048         uint32_t vec;
3049         struct rte_eth_dev *dev;
3050         struct rte_intr_handle *intr_handle;
3051         int rc;
3052
3053         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3054
3055         dev = &rte_eth_devices[port_id];
3056         if (queue_id >= dev->data->nb_rx_queues) {
3057                 RTE_PMD_DEBUG_TRACE("Invalid RX queue_id=%u\n", queue_id);
3058                 return -EINVAL;
3059         }
3060
3061         if (!dev->intr_handle) {
3062                 RTE_PMD_DEBUG_TRACE("RX Intr handle unset\n");
3063                 return -ENOTSUP;
3064         }
3065
3066         intr_handle = dev->intr_handle;
3067         if (!intr_handle->intr_vec) {
3068                 RTE_PMD_DEBUG_TRACE("RX Intr vector unset\n");
3069                 return -EPERM;
3070         }
3071
3072         vec = intr_handle->intr_vec[queue_id];
3073         rc = rte_intr_rx_ctl(intr_handle, epfd, op, vec, data);
3074         if (rc && rc != -EEXIST) {
3075                 RTE_PMD_DEBUG_TRACE("p %u q %u rx ctl error"
3076                                 " op %d epfd %d vec %u\n",
3077                                 port_id, queue_id, op, epfd, vec);
3078                 return rc;
3079         }
3080
3081         return 0;
3082 }
3083
3084 int
3085 rte_eth_dev_rx_intr_enable(uint16_t port_id,
3086                            uint16_t queue_id)
3087 {
3088         struct rte_eth_dev *dev;
3089
3090         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3091
3092         dev = &rte_eth_devices[port_id];
3093
3094         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_intr_enable, -ENOTSUP);
3095         return (*dev->dev_ops->rx_queue_intr_enable)(dev, queue_id);
3096 }
3097
3098 int
3099 rte_eth_dev_rx_intr_disable(uint16_t port_id,
3100                             uint16_t queue_id)
3101 {
3102         struct rte_eth_dev *dev;
3103
3104         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3105
3106         dev = &rte_eth_devices[port_id];
3107
3108         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_intr_disable, -ENOTSUP);
3109         return (*dev->dev_ops->rx_queue_intr_disable)(dev, queue_id);
3110 }
3111
3112
3113 int
3114 rte_eth_dev_filter_supported(uint16_t port_id,
3115                              enum rte_filter_type filter_type)
3116 {
3117         struct rte_eth_dev *dev;
3118
3119         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3120
3121         dev = &rte_eth_devices[port_id];
3122         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->filter_ctrl, -ENOTSUP);
3123         return (*dev->dev_ops->filter_ctrl)(dev, filter_type,
3124                                 RTE_ETH_FILTER_NOP, NULL);
3125 }
3126
3127 int
3128 rte_eth_dev_filter_ctrl(uint16_t port_id, enum rte_filter_type filter_type,
3129                        enum rte_filter_op filter_op, void *arg)
3130 {
3131         struct rte_eth_dev *dev;
3132
3133         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3134
3135         dev = &rte_eth_devices[port_id];
3136         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->filter_ctrl, -ENOTSUP);
3137         return (*dev->dev_ops->filter_ctrl)(dev, filter_type, filter_op, arg);
3138 }
3139
3140 void *
3141 rte_eth_add_rx_callback(uint16_t port_id, uint16_t queue_id,
3142                 rte_rx_callback_fn fn, void *user_param)
3143 {
3144 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
3145         rte_errno = ENOTSUP;
3146         return NULL;
3147 #endif
3148         /* check input parameters */
3149         if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
3150                     queue_id >= rte_eth_devices[port_id].data->nb_rx_queues) {
3151                 rte_errno = EINVAL;
3152                 return NULL;
3153         }
3154         struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
3155
3156         if (cb == NULL) {
3157                 rte_errno = ENOMEM;
3158                 return NULL;
3159         }
3160
3161         cb->fn.rx = fn;
3162         cb->param = user_param;
3163
3164         rte_spinlock_lock(&rte_eth_rx_cb_lock);
3165         /* Add the callbacks in fifo order. */
3166         struct rte_eth_rxtx_callback *tail =
3167                 rte_eth_devices[port_id].post_rx_burst_cbs[queue_id];
3168
3169         if (!tail) {
3170                 rte_eth_devices[port_id].post_rx_burst_cbs[queue_id] = cb;
3171
3172         } else {
3173                 while (tail->next)
3174                         tail = tail->next;
3175                 tail->next = cb;
3176         }
3177         rte_spinlock_unlock(&rte_eth_rx_cb_lock);
3178
3179         return cb;
3180 }
3181
3182 void *
3183 rte_eth_add_first_rx_callback(uint16_t port_id, uint16_t queue_id,
3184                 rte_rx_callback_fn fn, void *user_param)
3185 {
3186 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
3187         rte_errno = ENOTSUP;
3188         return NULL;
3189 #endif
3190         /* check input parameters */
3191         if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
3192                 queue_id >= rte_eth_devices[port_id].data->nb_rx_queues) {
3193                 rte_errno = EINVAL;
3194                 return NULL;
3195         }
3196
3197         struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
3198
3199         if (cb == NULL) {
3200                 rte_errno = ENOMEM;
3201                 return NULL;
3202         }
3203
3204         cb->fn.rx = fn;
3205         cb->param = user_param;
3206
3207         rte_spinlock_lock(&rte_eth_rx_cb_lock);
3208         /* Add the callbacks at fisrt position*/
3209         cb->next = rte_eth_devices[port_id].post_rx_burst_cbs[queue_id];
3210         rte_smp_wmb();
3211         rte_eth_devices[port_id].post_rx_burst_cbs[queue_id] = cb;
3212         rte_spinlock_unlock(&rte_eth_rx_cb_lock);
3213
3214         return cb;
3215 }
3216
3217 void *
3218 rte_eth_add_tx_callback(uint16_t port_id, uint16_t queue_id,
3219                 rte_tx_callback_fn fn, void *user_param)
3220 {
3221 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
3222         rte_errno = ENOTSUP;
3223         return NULL;
3224 #endif
3225         /* check input parameters */
3226         if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
3227                     queue_id >= rte_eth_devices[port_id].data->nb_tx_queues) {
3228                 rte_errno = EINVAL;
3229                 return NULL;
3230         }
3231
3232         struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
3233
3234         if (cb == NULL) {
3235                 rte_errno = ENOMEM;
3236                 return NULL;
3237         }
3238
3239         cb->fn.tx = fn;
3240         cb->param = user_param;
3241
3242         rte_spinlock_lock(&rte_eth_tx_cb_lock);
3243         /* Add the callbacks in fifo order. */
3244         struct rte_eth_rxtx_callback *tail =
3245                 rte_eth_devices[port_id].pre_tx_burst_cbs[queue_id];
3246
3247         if (!tail) {
3248                 rte_eth_devices[port_id].pre_tx_burst_cbs[queue_id] = cb;
3249
3250         } else {
3251                 while (tail->next)
3252                         tail = tail->next;
3253                 tail->next = cb;
3254         }
3255         rte_spinlock_unlock(&rte_eth_tx_cb_lock);
3256
3257         return cb;
3258 }
3259
3260 int
3261 rte_eth_remove_rx_callback(uint16_t port_id, uint16_t queue_id,
3262                 struct rte_eth_rxtx_callback *user_cb)
3263 {
3264 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
3265         return -ENOTSUP;
3266 #endif
3267         /* Check input parameters. */
3268         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
3269         if (user_cb == NULL ||
3270                         queue_id >= rte_eth_devices[port_id].data->nb_rx_queues)
3271                 return -EINVAL;
3272
3273         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
3274         struct rte_eth_rxtx_callback *cb;
3275         struct rte_eth_rxtx_callback **prev_cb;
3276         int ret = -EINVAL;
3277
3278         rte_spinlock_lock(&rte_eth_rx_cb_lock);
3279         prev_cb = &dev->post_rx_burst_cbs[queue_id];
3280         for (; *prev_cb != NULL; prev_cb = &cb->next) {
3281                 cb = *prev_cb;
3282                 if (cb == user_cb) {
3283                         /* Remove the user cb from the callback list. */
3284                         *prev_cb = cb->next;
3285                         ret = 0;
3286                         break;
3287                 }
3288         }
3289         rte_spinlock_unlock(&rte_eth_rx_cb_lock);
3290
3291         return ret;
3292 }
3293
3294 int
3295 rte_eth_remove_tx_callback(uint16_t port_id, uint16_t queue_id,
3296                 struct rte_eth_rxtx_callback *user_cb)
3297 {
3298 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
3299         return -ENOTSUP;
3300 #endif
3301         /* Check input parameters. */
3302         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
3303         if (user_cb == NULL ||
3304                         queue_id >= rte_eth_devices[port_id].data->nb_tx_queues)
3305                 return -EINVAL;
3306
3307         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
3308         int ret = -EINVAL;
3309         struct rte_eth_rxtx_callback *cb;
3310         struct rte_eth_rxtx_callback **prev_cb;
3311
3312         rte_spinlock_lock(&rte_eth_tx_cb_lock);
3313         prev_cb = &dev->pre_tx_burst_cbs[queue_id];
3314         for (; *prev_cb != NULL; prev_cb = &cb->next) {
3315                 cb = *prev_cb;
3316                 if (cb == user_cb) {
3317                         /* Remove the user cb from the callback list. */
3318                         *prev_cb = cb->next;
3319                         ret = 0;
3320                         break;
3321                 }
3322         }
3323         rte_spinlock_unlock(&rte_eth_tx_cb_lock);
3324
3325         return ret;
3326 }
3327
3328 int
3329 rte_eth_rx_queue_info_get(uint16_t port_id, uint16_t queue_id,
3330         struct rte_eth_rxq_info *qinfo)
3331 {
3332         struct rte_eth_dev *dev;
3333
3334         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3335
3336         if (qinfo == NULL)
3337                 return -EINVAL;
3338
3339         dev = &rte_eth_devices[port_id];
3340         if (queue_id >= dev->data->nb_rx_queues) {
3341                 RTE_PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", queue_id);
3342                 return -EINVAL;
3343         }
3344
3345         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rxq_info_get, -ENOTSUP);
3346
3347         memset(qinfo, 0, sizeof(*qinfo));
3348         dev->dev_ops->rxq_info_get(dev, queue_id, qinfo);
3349         return 0;
3350 }
3351
3352 int
3353 rte_eth_tx_queue_info_get(uint16_t port_id, uint16_t queue_id,
3354         struct rte_eth_txq_info *qinfo)
3355 {
3356         struct rte_eth_dev *dev;
3357
3358         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3359
3360         if (qinfo == NULL)
3361                 return -EINVAL;
3362
3363         dev = &rte_eth_devices[port_id];
3364         if (queue_id >= dev->data->nb_tx_queues) {
3365                 RTE_PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", queue_id);
3366                 return -EINVAL;
3367         }
3368
3369         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->txq_info_get, -ENOTSUP);
3370
3371         memset(qinfo, 0, sizeof(*qinfo));
3372         dev->dev_ops->txq_info_get(dev, queue_id, qinfo);
3373         return 0;
3374 }
3375
3376 int
3377 rte_eth_dev_set_mc_addr_list(uint16_t port_id,
3378                              struct ether_addr *mc_addr_set,
3379                              uint32_t nb_mc_addr)
3380 {
3381         struct rte_eth_dev *dev;
3382
3383         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3384
3385         dev = &rte_eth_devices[port_id];
3386         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_mc_addr_list, -ENOTSUP);
3387         return dev->dev_ops->set_mc_addr_list(dev, mc_addr_set, nb_mc_addr);
3388 }
3389
3390 int
3391 rte_eth_timesync_enable(uint16_t port_id)
3392 {
3393         struct rte_eth_dev *dev;
3394
3395         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3396         dev = &rte_eth_devices[port_id];
3397
3398         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_enable, -ENOTSUP);
3399         return (*dev->dev_ops->timesync_enable)(dev);
3400 }
3401
3402 int
3403 rte_eth_timesync_disable(uint16_t port_id)
3404 {
3405         struct rte_eth_dev *dev;
3406
3407         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3408         dev = &rte_eth_devices[port_id];
3409
3410         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_disable, -ENOTSUP);
3411         return (*dev->dev_ops->timesync_disable)(dev);
3412 }
3413
3414 int
3415 rte_eth_timesync_read_rx_timestamp(uint16_t port_id, struct timespec *timestamp,
3416                                    uint32_t flags)
3417 {
3418         struct rte_eth_dev *dev;
3419
3420         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3421         dev = &rte_eth_devices[port_id];
3422
3423         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_rx_timestamp, -ENOTSUP);
3424         return (*dev->dev_ops->timesync_read_rx_timestamp)(dev, timestamp, flags);
3425 }
3426
3427 int
3428 rte_eth_timesync_read_tx_timestamp(uint16_t port_id,
3429                                    struct timespec *timestamp)
3430 {
3431         struct rte_eth_dev *dev;
3432
3433         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3434         dev = &rte_eth_devices[port_id];
3435
3436         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_tx_timestamp, -ENOTSUP);
3437         return (*dev->dev_ops->timesync_read_tx_timestamp)(dev, timestamp);
3438 }
3439
3440 int
3441 rte_eth_timesync_adjust_time(uint16_t port_id, int64_t delta)
3442 {
3443         struct rte_eth_dev *dev;
3444
3445         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3446         dev = &rte_eth_devices[port_id];
3447
3448         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_adjust_time, -ENOTSUP);
3449         return (*dev->dev_ops->timesync_adjust_time)(dev, delta);
3450 }
3451
3452 int
3453 rte_eth_timesync_read_time(uint16_t port_id, struct timespec *timestamp)
3454 {
3455         struct rte_eth_dev *dev;
3456
3457         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3458         dev = &rte_eth_devices[port_id];
3459
3460         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_time, -ENOTSUP);
3461         return (*dev->dev_ops->timesync_read_time)(dev, timestamp);
3462 }
3463
3464 int
3465 rte_eth_timesync_write_time(uint16_t port_id, const struct timespec *timestamp)
3466 {
3467         struct rte_eth_dev *dev;
3468
3469         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3470         dev = &rte_eth_devices[port_id];
3471
3472         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_write_time, -ENOTSUP);
3473         return (*dev->dev_ops->timesync_write_time)(dev, timestamp);
3474 }
3475
3476 int
3477 rte_eth_dev_get_reg_info(uint16_t port_id, struct rte_dev_reg_info *info)
3478 {
3479         struct rte_eth_dev *dev;
3480
3481         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3482
3483         dev = &rte_eth_devices[port_id];
3484         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_reg, -ENOTSUP);
3485         return (*dev->dev_ops->get_reg)(dev, info);
3486 }
3487
3488 int
3489 rte_eth_dev_get_eeprom_length(uint16_t port_id)
3490 {
3491         struct rte_eth_dev *dev;
3492
3493         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3494
3495         dev = &rte_eth_devices[port_id];
3496         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_eeprom_length, -ENOTSUP);
3497         return (*dev->dev_ops->get_eeprom_length)(dev);
3498 }
3499
3500 int
3501 rte_eth_dev_get_eeprom(uint16_t port_id, struct rte_dev_eeprom_info *info)
3502 {
3503         struct rte_eth_dev *dev;
3504
3505         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3506
3507         dev = &rte_eth_devices[port_id];
3508         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_eeprom, -ENOTSUP);
3509         return (*dev->dev_ops->get_eeprom)(dev, info);
3510 }
3511
3512 int
3513 rte_eth_dev_set_eeprom(uint16_t port_id, struct rte_dev_eeprom_info *info)
3514 {
3515         struct rte_eth_dev *dev;
3516
3517         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3518
3519         dev = &rte_eth_devices[port_id];
3520         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_eeprom, -ENOTSUP);
3521         return (*dev->dev_ops->set_eeprom)(dev, info);
3522 }
3523
3524 int
3525 rte_eth_dev_get_dcb_info(uint16_t port_id,
3526                              struct rte_eth_dcb_info *dcb_info)
3527 {
3528         struct rte_eth_dev *dev;
3529
3530         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3531
3532         dev = &rte_eth_devices[port_id];
3533         memset(dcb_info, 0, sizeof(struct rte_eth_dcb_info));
3534
3535         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_dcb_info, -ENOTSUP);
3536         return (*dev->dev_ops->get_dcb_info)(dev, dcb_info);
3537 }
3538
3539 int
3540 rte_eth_dev_l2_tunnel_eth_type_conf(uint16_t port_id,
3541                                     struct rte_eth_l2_tunnel_conf *l2_tunnel)
3542 {
3543         struct rte_eth_dev *dev;
3544
3545         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3546         if (l2_tunnel == NULL) {
3547                 RTE_PMD_DEBUG_TRACE("Invalid l2_tunnel parameter\n");
3548                 return -EINVAL;
3549         }
3550
3551         if (l2_tunnel->l2_tunnel_type >= RTE_TUNNEL_TYPE_MAX) {
3552                 RTE_PMD_DEBUG_TRACE("Invalid tunnel type\n");
3553                 return -EINVAL;
3554         }
3555
3556         dev = &rte_eth_devices[port_id];
3557         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->l2_tunnel_eth_type_conf,
3558                                 -ENOTSUP);
3559         return (*dev->dev_ops->l2_tunnel_eth_type_conf)(dev, l2_tunnel);
3560 }
3561
3562 int
3563 rte_eth_dev_l2_tunnel_offload_set(uint16_t port_id,
3564                                   struct rte_eth_l2_tunnel_conf *l2_tunnel,
3565                                   uint32_t mask,
3566                                   uint8_t en)
3567 {
3568         struct rte_eth_dev *dev;
3569
3570         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3571
3572         if (l2_tunnel == NULL) {
3573                 RTE_PMD_DEBUG_TRACE("Invalid l2_tunnel parameter\n");
3574                 return -EINVAL;
3575         }
3576
3577         if (l2_tunnel->l2_tunnel_type >= RTE_TUNNEL_TYPE_MAX) {
3578                 RTE_PMD_DEBUG_TRACE("Invalid tunnel type.\n");
3579                 return -EINVAL;
3580         }
3581
3582         if (mask == 0) {
3583                 RTE_PMD_DEBUG_TRACE("Mask should have a value.\n");
3584                 return -EINVAL;
3585         }
3586
3587         dev = &rte_eth_devices[port_id];
3588         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->l2_tunnel_offload_set,
3589                                 -ENOTSUP);
3590         return (*dev->dev_ops->l2_tunnel_offload_set)(dev, l2_tunnel, mask, en);
3591 }
3592
3593 static void
3594 rte_eth_dev_adjust_nb_desc(uint16_t *nb_desc,
3595                            const struct rte_eth_desc_lim *desc_lim)
3596 {
3597         if (desc_lim->nb_align != 0)
3598                 *nb_desc = RTE_ALIGN_CEIL(*nb_desc, desc_lim->nb_align);
3599
3600         if (desc_lim->nb_max != 0)
3601                 *nb_desc = RTE_MIN(*nb_desc, desc_lim->nb_max);
3602
3603         *nb_desc = RTE_MAX(*nb_desc, desc_lim->nb_min);
3604 }
3605
3606 int
3607 rte_eth_dev_adjust_nb_rx_tx_desc(uint16_t port_id,
3608                                  uint16_t *nb_rx_desc,
3609                                  uint16_t *nb_tx_desc)
3610 {
3611         struct rte_eth_dev *dev;
3612         struct rte_eth_dev_info dev_info;
3613
3614         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3615
3616         dev = &rte_eth_devices[port_id];
3617         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
3618
3619         rte_eth_dev_info_get(port_id, &dev_info);
3620
3621         if (nb_rx_desc != NULL)
3622                 rte_eth_dev_adjust_nb_desc(nb_rx_desc, &dev_info.rx_desc_lim);
3623
3624         if (nb_tx_desc != NULL)
3625                 rte_eth_dev_adjust_nb_desc(nb_tx_desc, &dev_info.tx_desc_lim);
3626
3627         return 0;
3628 }
3629
3630 int
3631 rte_eth_dev_pool_ops_supported(uint8_t port_id, const char *pool)
3632 {
3633         struct rte_eth_dev *dev;
3634
3635         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3636
3637         if (pool == NULL)
3638                 return -EINVAL;
3639
3640         dev = &rte_eth_devices[port_id];
3641
3642         if (*dev->dev_ops->pool_ops_supported == NULL)
3643                 return 1; /* all pools are supported */
3644
3645         return (*dev->dev_ops->pool_ops_supported)(dev, pool);
3646 }