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