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