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