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