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