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