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