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