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