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