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