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