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