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