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