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