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