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