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