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