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