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