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