ethdev: remove useless pointer initialization
[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_dev_data[port_id], 0, sizeof(struct rte_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         uint64_t pool_mask;
938
939         dev = &rte_eth_devices[port_id];
940
941         rte_eth_dev_info_get(port_id, &dev_info);
942
943         /* replay MAC address configuration including default MAC */
944         addr = &dev->data->mac_addrs[0];
945         if (*dev->dev_ops->mac_addr_set != NULL)
946                 (*dev->dev_ops->mac_addr_set)(dev, addr);
947         else if (*dev->dev_ops->mac_addr_add != NULL)
948                 (*dev->dev_ops->mac_addr_add)(dev, addr, 0, pool);
949
950         if (*dev->dev_ops->mac_addr_add != NULL) {
951                 for (i = 1; i < dev_info.max_mac_addrs; i++) {
952                         addr = &dev->data->mac_addrs[i];
953
954                         /* skip zero address */
955                         if (is_zero_ether_addr(addr))
956                                 continue;
957
958                         pool = 0;
959                         pool_mask = dev->data->mac_pool_sel[i];
960
961                         do {
962                                 if (pool_mask & 1ULL)
963                                         (*dev->dev_ops->mac_addr_add)(dev,
964                                                 addr, i, pool);
965                                 pool_mask >>= 1;
966                                 pool++;
967                         } while (pool_mask);
968                 }
969         }
970
971         /* replay promiscuous configuration */
972         if (rte_eth_promiscuous_get(port_id) == 1)
973                 rte_eth_promiscuous_enable(port_id);
974         else if (rte_eth_promiscuous_get(port_id) == 0)
975                 rte_eth_promiscuous_disable(port_id);
976
977         /* replay all multicast configuration */
978         if (rte_eth_allmulticast_get(port_id) == 1)
979                 rte_eth_allmulticast_enable(port_id);
980         else if (rte_eth_allmulticast_get(port_id) == 0)
981                 rte_eth_allmulticast_disable(port_id);
982 }
983
984 int
985 rte_eth_dev_start(uint8_t port_id)
986 {
987         struct rte_eth_dev *dev;
988         int diag;
989
990         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
991
992         dev = &rte_eth_devices[port_id];
993
994         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
995
996         if (dev->data->dev_started != 0) {
997                 RTE_PMD_DEBUG_TRACE("Device with port_id=%" PRIu8
998                         " already started\n",
999                         port_id);
1000                 return 0;
1001         }
1002
1003         diag = (*dev->dev_ops->dev_start)(dev);
1004         if (diag == 0)
1005                 dev->data->dev_started = 1;
1006         else
1007                 return diag;
1008
1009         rte_eth_dev_config_restore(port_id);
1010
1011         if (dev->data->dev_conf.intr_conf.lsc == 0) {
1012                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->link_update, -ENOTSUP);
1013                 (*dev->dev_ops->link_update)(dev, 0);
1014         }
1015         return 0;
1016 }
1017
1018 void
1019 rte_eth_dev_stop(uint8_t port_id)
1020 {
1021         struct rte_eth_dev *dev;
1022
1023         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1024         dev = &rte_eth_devices[port_id];
1025
1026         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
1027
1028         if (dev->data->dev_started == 0) {
1029                 RTE_PMD_DEBUG_TRACE("Device with port_id=%" PRIu8
1030                         " already stopped\n",
1031                         port_id);
1032                 return;
1033         }
1034
1035         dev->data->dev_started = 0;
1036         (*dev->dev_ops->dev_stop)(dev);
1037 }
1038
1039 int
1040 rte_eth_dev_set_link_up(uint8_t port_id)
1041 {
1042         struct rte_eth_dev *dev;
1043
1044         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1045
1046         dev = &rte_eth_devices[port_id];
1047
1048         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_set_link_up, -ENOTSUP);
1049         return (*dev->dev_ops->dev_set_link_up)(dev);
1050 }
1051
1052 int
1053 rte_eth_dev_set_link_down(uint8_t port_id)
1054 {
1055         struct rte_eth_dev *dev;
1056
1057         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1058
1059         dev = &rte_eth_devices[port_id];
1060
1061         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_set_link_down, -ENOTSUP);
1062         return (*dev->dev_ops->dev_set_link_down)(dev);
1063 }
1064
1065 void
1066 rte_eth_dev_close(uint8_t port_id)
1067 {
1068         struct rte_eth_dev *dev;
1069
1070         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1071         dev = &rte_eth_devices[port_id];
1072
1073         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_close);
1074         dev->data->dev_started = 0;
1075         (*dev->dev_ops->dev_close)(dev);
1076
1077         rte_free(dev->data->rx_queues);
1078         dev->data->rx_queues = NULL;
1079         rte_free(dev->data->tx_queues);
1080         dev->data->tx_queues = NULL;
1081 }
1082
1083 int
1084 rte_eth_rx_queue_setup(uint8_t port_id, uint16_t rx_queue_id,
1085                        uint16_t nb_rx_desc, unsigned int socket_id,
1086                        const struct rte_eth_rxconf *rx_conf,
1087                        struct rte_mempool *mp)
1088 {
1089         int ret;
1090         uint32_t mbp_buf_size;
1091         struct rte_eth_dev *dev;
1092         struct rte_eth_dev_info dev_info;
1093         void **rxq;
1094
1095         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1096
1097         dev = &rte_eth_devices[port_id];
1098         if (rx_queue_id >= dev->data->nb_rx_queues) {
1099                 RTE_PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", rx_queue_id);
1100                 return -EINVAL;
1101         }
1102
1103         if (dev->data->dev_started) {
1104                 RTE_PMD_DEBUG_TRACE(
1105                     "port %d must be stopped to allow configuration\n", port_id);
1106                 return -EBUSY;
1107         }
1108
1109         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
1110         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_setup, -ENOTSUP);
1111
1112         /*
1113          * Check the size of the mbuf data buffer.
1114          * This value must be provided in the private data of the memory pool.
1115          * First check that the memory pool has a valid private data.
1116          */
1117         rte_eth_dev_info_get(port_id, &dev_info);
1118         if (mp->private_data_size < sizeof(struct rte_pktmbuf_pool_private)) {
1119                 RTE_PMD_DEBUG_TRACE("%s private_data_size %d < %d\n",
1120                                 mp->name, (int) mp->private_data_size,
1121                                 (int) sizeof(struct rte_pktmbuf_pool_private));
1122                 return -ENOSPC;
1123         }
1124         mbp_buf_size = rte_pktmbuf_data_room_size(mp);
1125
1126         if ((mbp_buf_size - RTE_PKTMBUF_HEADROOM) < dev_info.min_rx_bufsize) {
1127                 RTE_PMD_DEBUG_TRACE("%s mbuf_data_room_size %d < %d "
1128                                 "(RTE_PKTMBUF_HEADROOM=%d + min_rx_bufsize(dev)"
1129                                 "=%d)\n",
1130                                 mp->name,
1131                                 (int)mbp_buf_size,
1132                                 (int)(RTE_PKTMBUF_HEADROOM +
1133                                       dev_info.min_rx_bufsize),
1134                                 (int)RTE_PKTMBUF_HEADROOM,
1135                                 (int)dev_info.min_rx_bufsize);
1136                 return -EINVAL;
1137         }
1138
1139         if (nb_rx_desc > dev_info.rx_desc_lim.nb_max ||
1140                         nb_rx_desc < dev_info.rx_desc_lim.nb_min ||
1141                         nb_rx_desc % dev_info.rx_desc_lim.nb_align != 0) {
1142
1143                 RTE_PMD_DEBUG_TRACE("Invalid value for nb_rx_desc(=%hu), "
1144                         "should be: <= %hu, = %hu, and a product of %hu\n",
1145                         nb_rx_desc,
1146                         dev_info.rx_desc_lim.nb_max,
1147                         dev_info.rx_desc_lim.nb_min,
1148                         dev_info.rx_desc_lim.nb_align);
1149                 return -EINVAL;
1150         }
1151
1152         rxq = dev->data->rx_queues;
1153         if (rxq[rx_queue_id]) {
1154                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_release,
1155                                         -ENOTSUP);
1156                 (*dev->dev_ops->rx_queue_release)(rxq[rx_queue_id]);
1157                 rxq[rx_queue_id] = NULL;
1158         }
1159
1160         if (rx_conf == NULL)
1161                 rx_conf = &dev_info.default_rxconf;
1162
1163         ret = (*dev->dev_ops->rx_queue_setup)(dev, rx_queue_id, nb_rx_desc,
1164                                               socket_id, rx_conf, mp);
1165         if (!ret) {
1166                 if (!dev->data->min_rx_buf_size ||
1167                     dev->data->min_rx_buf_size > mbp_buf_size)
1168                         dev->data->min_rx_buf_size = mbp_buf_size;
1169         }
1170
1171         return ret;
1172 }
1173
1174 int
1175 rte_eth_tx_queue_setup(uint8_t port_id, uint16_t tx_queue_id,
1176                        uint16_t nb_tx_desc, unsigned int socket_id,
1177                        const struct rte_eth_txconf *tx_conf)
1178 {
1179         struct rte_eth_dev *dev;
1180         struct rte_eth_dev_info dev_info;
1181         void **txq;
1182
1183         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1184
1185         dev = &rte_eth_devices[port_id];
1186         if (tx_queue_id >= dev->data->nb_tx_queues) {
1187                 RTE_PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", tx_queue_id);
1188                 return -EINVAL;
1189         }
1190
1191         if (dev->data->dev_started) {
1192                 RTE_PMD_DEBUG_TRACE(
1193                     "port %d must be stopped to allow configuration\n", port_id);
1194                 return -EBUSY;
1195         }
1196
1197         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
1198         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_setup, -ENOTSUP);
1199
1200         rte_eth_dev_info_get(port_id, &dev_info);
1201
1202         if (nb_tx_desc > dev_info.tx_desc_lim.nb_max ||
1203             nb_tx_desc < dev_info.tx_desc_lim.nb_min ||
1204             nb_tx_desc % dev_info.tx_desc_lim.nb_align != 0) {
1205                 RTE_PMD_DEBUG_TRACE("Invalid value for nb_tx_desc(=%hu), "
1206                                 "should be: <= %hu, = %hu, and a product of %hu\n",
1207                                 nb_tx_desc,
1208                                 dev_info.tx_desc_lim.nb_max,
1209                                 dev_info.tx_desc_lim.nb_min,
1210                                 dev_info.tx_desc_lim.nb_align);
1211                 return -EINVAL;
1212         }
1213
1214         txq = dev->data->tx_queues;
1215         if (txq[tx_queue_id]) {
1216                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_release,
1217                                         -ENOTSUP);
1218                 (*dev->dev_ops->tx_queue_release)(txq[tx_queue_id]);
1219                 txq[tx_queue_id] = NULL;
1220         }
1221
1222         if (tx_conf == NULL)
1223                 tx_conf = &dev_info.default_txconf;
1224
1225         return (*dev->dev_ops->tx_queue_setup)(dev, tx_queue_id, nb_tx_desc,
1226                                                socket_id, tx_conf);
1227 }
1228
1229 void
1230 rte_eth_tx_buffer_drop_callback(struct rte_mbuf **pkts, uint16_t unsent,
1231                 void *userdata __rte_unused)
1232 {
1233         unsigned i;
1234
1235         for (i = 0; i < unsent; i++)
1236                 rte_pktmbuf_free(pkts[i]);
1237 }
1238
1239 void
1240 rte_eth_tx_buffer_count_callback(struct rte_mbuf **pkts, uint16_t unsent,
1241                 void *userdata)
1242 {
1243         uint64_t *count = userdata;
1244         unsigned i;
1245
1246         for (i = 0; i < unsent; i++)
1247                 rte_pktmbuf_free(pkts[i]);
1248
1249         *count += unsent;
1250 }
1251
1252 int
1253 rte_eth_tx_buffer_set_err_callback(struct rte_eth_dev_tx_buffer *buffer,
1254                 buffer_tx_error_fn cbfn, void *userdata)
1255 {
1256         buffer->error_callback = cbfn;
1257         buffer->error_userdata = userdata;
1258         return 0;
1259 }
1260
1261 int
1262 rte_eth_tx_buffer_init(struct rte_eth_dev_tx_buffer *buffer, uint16_t size)
1263 {
1264         int ret = 0;
1265
1266         if (buffer == NULL)
1267                 return -EINVAL;
1268
1269         buffer->size = size;
1270         if (buffer->error_callback == NULL) {
1271                 ret = rte_eth_tx_buffer_set_err_callback(
1272                         buffer, rte_eth_tx_buffer_drop_callback, NULL);
1273         }
1274
1275         return ret;
1276 }
1277
1278 void
1279 rte_eth_promiscuous_enable(uint8_t port_id)
1280 {
1281         struct rte_eth_dev *dev;
1282
1283         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1284         dev = &rte_eth_devices[port_id];
1285
1286         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->promiscuous_enable);
1287         (*dev->dev_ops->promiscuous_enable)(dev);
1288         dev->data->promiscuous = 1;
1289 }
1290
1291 void
1292 rte_eth_promiscuous_disable(uint8_t port_id)
1293 {
1294         struct rte_eth_dev *dev;
1295
1296         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1297         dev = &rte_eth_devices[port_id];
1298
1299         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->promiscuous_disable);
1300         dev->data->promiscuous = 0;
1301         (*dev->dev_ops->promiscuous_disable)(dev);
1302 }
1303
1304 int
1305 rte_eth_promiscuous_get(uint8_t port_id)
1306 {
1307         struct rte_eth_dev *dev;
1308
1309         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1310
1311         dev = &rte_eth_devices[port_id];
1312         return dev->data->promiscuous;
1313 }
1314
1315 void
1316 rte_eth_allmulticast_enable(uint8_t port_id)
1317 {
1318         struct rte_eth_dev *dev;
1319
1320         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1321         dev = &rte_eth_devices[port_id];
1322
1323         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_enable);
1324         (*dev->dev_ops->allmulticast_enable)(dev);
1325         dev->data->all_multicast = 1;
1326 }
1327
1328 void
1329 rte_eth_allmulticast_disable(uint8_t port_id)
1330 {
1331         struct rte_eth_dev *dev;
1332
1333         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1334         dev = &rte_eth_devices[port_id];
1335
1336         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_disable);
1337         dev->data->all_multicast = 0;
1338         (*dev->dev_ops->allmulticast_disable)(dev);
1339 }
1340
1341 int
1342 rte_eth_allmulticast_get(uint8_t port_id)
1343 {
1344         struct rte_eth_dev *dev;
1345
1346         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1347
1348         dev = &rte_eth_devices[port_id];
1349         return dev->data->all_multicast;
1350 }
1351
1352 static inline int
1353 rte_eth_dev_atomic_read_link_status(struct rte_eth_dev *dev,
1354                                 struct rte_eth_link *link)
1355 {
1356         struct rte_eth_link *dst = link;
1357         struct rte_eth_link *src = &(dev->data->dev_link);
1358
1359         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
1360                                         *(uint64_t *)src) == 0)
1361                 return -1;
1362
1363         return 0;
1364 }
1365
1366 void
1367 rte_eth_link_get(uint8_t port_id, struct rte_eth_link *eth_link)
1368 {
1369         struct rte_eth_dev *dev;
1370
1371         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1372         dev = &rte_eth_devices[port_id];
1373
1374         if (dev->data->dev_conf.intr_conf.lsc != 0)
1375                 rte_eth_dev_atomic_read_link_status(dev, eth_link);
1376         else {
1377                 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
1378                 (*dev->dev_ops->link_update)(dev, 1);
1379                 *eth_link = dev->data->dev_link;
1380         }
1381 }
1382
1383 void
1384 rte_eth_link_get_nowait(uint8_t port_id, struct rte_eth_link *eth_link)
1385 {
1386         struct rte_eth_dev *dev;
1387
1388         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1389         dev = &rte_eth_devices[port_id];
1390
1391         if (dev->data->dev_conf.intr_conf.lsc != 0)
1392                 rte_eth_dev_atomic_read_link_status(dev, eth_link);
1393         else {
1394                 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
1395                 (*dev->dev_ops->link_update)(dev, 0);
1396                 *eth_link = dev->data->dev_link;
1397         }
1398 }
1399
1400 int
1401 rte_eth_stats_get(uint8_t port_id, struct rte_eth_stats *stats)
1402 {
1403         struct rte_eth_dev *dev;
1404
1405         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1406
1407         dev = &rte_eth_devices[port_id];
1408         memset(stats, 0, sizeof(*stats));
1409
1410         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
1411         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
1412         (*dev->dev_ops->stats_get)(dev, stats);
1413         return 0;
1414 }
1415
1416 void
1417 rte_eth_stats_reset(uint8_t port_id)
1418 {
1419         struct rte_eth_dev *dev;
1420
1421         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1422         dev = &rte_eth_devices[port_id];
1423
1424         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
1425         (*dev->dev_ops->stats_reset)(dev);
1426         dev->data->rx_mbuf_alloc_failed = 0;
1427 }
1428
1429 static int
1430 get_xstats_count(uint8_t port_id)
1431 {
1432         struct rte_eth_dev *dev;
1433         int count;
1434
1435         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1436         dev = &rte_eth_devices[port_id];
1437         if (dev->dev_ops->xstats_get_names != NULL) {
1438                 count = (*dev->dev_ops->xstats_get_names)(dev, NULL, 0);
1439                 if (count < 0)
1440                         return count;
1441         } else
1442                 count = 0;
1443         count += RTE_NB_STATS;
1444         count += RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS) *
1445                  RTE_NB_RXQ_STATS;
1446         count += RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS) *
1447                  RTE_NB_TXQ_STATS;
1448         return count;
1449 }
1450
1451 int
1452 rte_eth_xstats_get_names(uint8_t port_id,
1453         struct rte_eth_xstat_name *xstats_names,
1454         unsigned size)
1455 {
1456         struct rte_eth_dev *dev;
1457         int cnt_used_entries;
1458         int cnt_expected_entries;
1459         int cnt_driver_entries;
1460         uint32_t idx, id_queue;
1461         uint16_t num_q;
1462
1463         cnt_expected_entries = get_xstats_count(port_id);
1464         if (xstats_names == NULL || cnt_expected_entries < 0 ||
1465                         (int)size < cnt_expected_entries)
1466                 return cnt_expected_entries;
1467
1468         /* port_id checked in get_xstats_count() */
1469         dev = &rte_eth_devices[port_id];
1470         cnt_used_entries = 0;
1471
1472         for (idx = 0; idx < RTE_NB_STATS; idx++) {
1473                 snprintf(xstats_names[cnt_used_entries].name,
1474                         sizeof(xstats_names[0].name),
1475                         "%s", rte_stats_strings[idx].name);
1476                 cnt_used_entries++;
1477         }
1478         num_q = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
1479         for (id_queue = 0; id_queue < num_q; id_queue++) {
1480                 for (idx = 0; idx < RTE_NB_RXQ_STATS; idx++) {
1481                         snprintf(xstats_names[cnt_used_entries].name,
1482                                 sizeof(xstats_names[0].name),
1483                                 "rx_q%u%s",
1484                                 id_queue, rte_rxq_stats_strings[idx].name);
1485                         cnt_used_entries++;
1486                 }
1487
1488         }
1489         num_q = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
1490         for (id_queue = 0; id_queue < num_q; id_queue++) {
1491                 for (idx = 0; idx < RTE_NB_TXQ_STATS; idx++) {
1492                         snprintf(xstats_names[cnt_used_entries].name,
1493                                 sizeof(xstats_names[0].name),
1494                                 "tx_q%u%s",
1495                                 id_queue, rte_txq_stats_strings[idx].name);
1496                         cnt_used_entries++;
1497                 }
1498         }
1499
1500         if (dev->dev_ops->xstats_get_names != NULL) {
1501                 /* If there are any driver-specific xstats, append them
1502                  * to end of list.
1503                  */
1504                 cnt_driver_entries = (*dev->dev_ops->xstats_get_names)(
1505                         dev,
1506                         xstats_names + cnt_used_entries,
1507                         size - cnt_used_entries);
1508                 if (cnt_driver_entries < 0)
1509                         return cnt_driver_entries;
1510                 cnt_used_entries += cnt_driver_entries;
1511         }
1512
1513         return cnt_used_entries;
1514 }
1515
1516 /* retrieve ethdev extended statistics */
1517 int
1518 rte_eth_xstats_get(uint8_t port_id, struct rte_eth_xstat *xstats,
1519         unsigned n)
1520 {
1521         struct rte_eth_stats eth_stats;
1522         struct rte_eth_dev *dev;
1523         unsigned count = 0, i, q;
1524         signed xcount = 0;
1525         uint64_t val, *stats_ptr;
1526         uint16_t nb_rxqs, nb_txqs;
1527
1528         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1529
1530         dev = &rte_eth_devices[port_id];
1531
1532         nb_rxqs = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
1533         nb_txqs = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
1534
1535         /* Return generic statistics */
1536         count = RTE_NB_STATS + (nb_rxqs * RTE_NB_RXQ_STATS) +
1537                 (nb_txqs * RTE_NB_TXQ_STATS);
1538
1539         /* implemented by the driver */
1540         if (dev->dev_ops->xstats_get != NULL) {
1541                 /* Retrieve the xstats from the driver at the end of the
1542                  * xstats struct.
1543                  */
1544                 xcount = (*dev->dev_ops->xstats_get)(dev,
1545                                      xstats ? xstats + count : NULL,
1546                                      (n > count) ? n - count : 0);
1547
1548                 if (xcount < 0)
1549                         return xcount;
1550         }
1551
1552         if (n < count + xcount || xstats == NULL)
1553                 return count + xcount;
1554
1555         /* now fill the xstats structure */
1556         count = 0;
1557         rte_eth_stats_get(port_id, &eth_stats);
1558
1559         /* global stats */
1560         for (i = 0; i < RTE_NB_STATS; i++) {
1561                 stats_ptr = RTE_PTR_ADD(&eth_stats,
1562                                         rte_stats_strings[i].offset);
1563                 val = *stats_ptr;
1564                 xstats[count++].value = val;
1565         }
1566
1567         /* per-rxq stats */
1568         for (q = 0; q < nb_rxqs; q++) {
1569                 for (i = 0; i < RTE_NB_RXQ_STATS; i++) {
1570                         stats_ptr = RTE_PTR_ADD(&eth_stats,
1571                                         rte_rxq_stats_strings[i].offset +
1572                                         q * sizeof(uint64_t));
1573                         val = *stats_ptr;
1574                         xstats[count++].value = val;
1575                 }
1576         }
1577
1578         /* per-txq stats */
1579         for (q = 0; q < nb_txqs; q++) {
1580                 for (i = 0; i < RTE_NB_TXQ_STATS; i++) {
1581                         stats_ptr = RTE_PTR_ADD(&eth_stats,
1582                                         rte_txq_stats_strings[i].offset +
1583                                         q * sizeof(uint64_t));
1584                         val = *stats_ptr;
1585                         xstats[count++].value = val;
1586                 }
1587         }
1588
1589         for (i = 0; i < count; i++)
1590                 xstats[i].id = i;
1591         /* add an offset to driver-specific stats */
1592         for ( ; i < count + xcount; i++)
1593                 xstats[i].id += count;
1594
1595         return count + xcount;
1596 }
1597
1598 /* reset ethdev extended statistics */
1599 void
1600 rte_eth_xstats_reset(uint8_t port_id)
1601 {
1602         struct rte_eth_dev *dev;
1603
1604         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1605         dev = &rte_eth_devices[port_id];
1606
1607         /* implemented by the driver */
1608         if (dev->dev_ops->xstats_reset != NULL) {
1609                 (*dev->dev_ops->xstats_reset)(dev);
1610                 return;
1611         }
1612
1613         /* fallback to default */
1614         rte_eth_stats_reset(port_id);
1615 }
1616
1617 static int
1618 set_queue_stats_mapping(uint8_t port_id, uint16_t queue_id, uint8_t stat_idx,
1619                 uint8_t is_rx)
1620 {
1621         struct rte_eth_dev *dev;
1622
1623         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1624
1625         dev = &rte_eth_devices[port_id];
1626
1627         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_stats_mapping_set, -ENOTSUP);
1628         return (*dev->dev_ops->queue_stats_mapping_set)
1629                         (dev, queue_id, stat_idx, is_rx);
1630 }
1631
1632
1633 int
1634 rte_eth_dev_set_tx_queue_stats_mapping(uint8_t port_id, uint16_t tx_queue_id,
1635                 uint8_t stat_idx)
1636 {
1637         return set_queue_stats_mapping(port_id, tx_queue_id, stat_idx,
1638                         STAT_QMAP_TX);
1639 }
1640
1641
1642 int
1643 rte_eth_dev_set_rx_queue_stats_mapping(uint8_t port_id, uint16_t rx_queue_id,
1644                 uint8_t stat_idx)
1645 {
1646         return set_queue_stats_mapping(port_id, rx_queue_id, stat_idx,
1647                         STAT_QMAP_RX);
1648 }
1649
1650 int
1651 rte_eth_dev_fw_version_get(uint8_t port_id, char *fw_version, size_t fw_size)
1652 {
1653         struct rte_eth_dev *dev;
1654
1655         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1656         dev = &rte_eth_devices[port_id];
1657
1658         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fw_version_get, -ENOTSUP);
1659         return (*dev->dev_ops->fw_version_get)(dev, fw_version, fw_size);
1660 }
1661
1662 void
1663 rte_eth_dev_info_get(uint8_t port_id, struct rte_eth_dev_info *dev_info)
1664 {
1665         struct rte_eth_dev *dev;
1666         const struct rte_eth_desc_lim lim = {
1667                 .nb_max = UINT16_MAX,
1668                 .nb_min = 0,
1669                 .nb_align = 1,
1670         };
1671
1672         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1673         dev = &rte_eth_devices[port_id];
1674
1675         memset(dev_info, 0, sizeof(struct rte_eth_dev_info));
1676         dev_info->rx_desc_lim = lim;
1677         dev_info->tx_desc_lim = lim;
1678
1679         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
1680         (*dev->dev_ops->dev_infos_get)(dev, dev_info);
1681         dev_info->driver_name = dev->data->drv_name;
1682         dev_info->nb_rx_queues = dev->data->nb_rx_queues;
1683         dev_info->nb_tx_queues = dev->data->nb_tx_queues;
1684 }
1685
1686 int
1687 rte_eth_dev_get_supported_ptypes(uint8_t port_id, uint32_t ptype_mask,
1688                                  uint32_t *ptypes, int num)
1689 {
1690         int i, j;
1691         struct rte_eth_dev *dev;
1692         const uint32_t *all_ptypes;
1693
1694         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1695         dev = &rte_eth_devices[port_id];
1696         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_supported_ptypes_get, 0);
1697         all_ptypes = (*dev->dev_ops->dev_supported_ptypes_get)(dev);
1698
1699         if (!all_ptypes)
1700                 return 0;
1701
1702         for (i = 0, j = 0; all_ptypes[i] != RTE_PTYPE_UNKNOWN; ++i)
1703                 if (all_ptypes[i] & ptype_mask) {
1704                         if (j < num)
1705                                 ptypes[j] = all_ptypes[i];
1706                         j++;
1707                 }
1708
1709         return j;
1710 }
1711
1712 void
1713 rte_eth_macaddr_get(uint8_t port_id, struct ether_addr *mac_addr)
1714 {
1715         struct rte_eth_dev *dev;
1716
1717         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1718         dev = &rte_eth_devices[port_id];
1719         ether_addr_copy(&dev->data->mac_addrs[0], mac_addr);
1720 }
1721
1722
1723 int
1724 rte_eth_dev_get_mtu(uint8_t port_id, uint16_t *mtu)
1725 {
1726         struct rte_eth_dev *dev;
1727
1728         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1729
1730         dev = &rte_eth_devices[port_id];
1731         *mtu = dev->data->mtu;
1732         return 0;
1733 }
1734
1735 int
1736 rte_eth_dev_set_mtu(uint8_t port_id, uint16_t mtu)
1737 {
1738         int ret;
1739         struct rte_eth_dev *dev;
1740
1741         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1742         dev = &rte_eth_devices[port_id];
1743         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mtu_set, -ENOTSUP);
1744
1745         ret = (*dev->dev_ops->mtu_set)(dev, mtu);
1746         if (!ret)
1747                 dev->data->mtu = mtu;
1748
1749         return ret;
1750 }
1751
1752 int
1753 rte_eth_dev_vlan_filter(uint8_t port_id, uint16_t vlan_id, int on)
1754 {
1755         struct rte_eth_dev *dev;
1756
1757         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1758         dev = &rte_eth_devices[port_id];
1759         if (!(dev->data->dev_conf.rxmode.hw_vlan_filter)) {
1760                 RTE_PMD_DEBUG_TRACE("port %d: vlan-filtering disabled\n", port_id);
1761                 return -ENOSYS;
1762         }
1763
1764         if (vlan_id > 4095) {
1765                 RTE_PMD_DEBUG_TRACE("(port_id=%d) invalid vlan_id=%u > 4095\n",
1766                                 port_id, (unsigned) vlan_id);
1767                 return -EINVAL;
1768         }
1769         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_filter_set, -ENOTSUP);
1770
1771         return (*dev->dev_ops->vlan_filter_set)(dev, vlan_id, on);
1772 }
1773
1774 int
1775 rte_eth_dev_set_vlan_strip_on_queue(uint8_t port_id, uint16_t rx_queue_id, int on)
1776 {
1777         struct rte_eth_dev *dev;
1778
1779         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1780         dev = &rte_eth_devices[port_id];
1781         if (rx_queue_id >= dev->data->nb_rx_queues) {
1782                 RTE_PMD_DEBUG_TRACE("Invalid rx_queue_id=%d\n", port_id);
1783                 return -EINVAL;
1784         }
1785
1786         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_strip_queue_set, -ENOTSUP);
1787         (*dev->dev_ops->vlan_strip_queue_set)(dev, rx_queue_id, on);
1788
1789         return 0;
1790 }
1791
1792 int
1793 rte_eth_dev_set_vlan_ether_type(uint8_t port_id,
1794                                 enum rte_vlan_type vlan_type,
1795                                 uint16_t tpid)
1796 {
1797         struct rte_eth_dev *dev;
1798
1799         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1800         dev = &rte_eth_devices[port_id];
1801         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_tpid_set, -ENOTSUP);
1802
1803         return (*dev->dev_ops->vlan_tpid_set)(dev, vlan_type, tpid);
1804 }
1805
1806 int
1807 rte_eth_dev_set_vlan_offload(uint8_t port_id, int offload_mask)
1808 {
1809         struct rte_eth_dev *dev;
1810         int ret = 0;
1811         int mask = 0;
1812         int cur, org = 0;
1813
1814         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1815         dev = &rte_eth_devices[port_id];
1816
1817         /*check which option changed by application*/
1818         cur = !!(offload_mask & ETH_VLAN_STRIP_OFFLOAD);
1819         org = !!(dev->data->dev_conf.rxmode.hw_vlan_strip);
1820         if (cur != org) {
1821                 dev->data->dev_conf.rxmode.hw_vlan_strip = (uint8_t)cur;
1822                 mask |= ETH_VLAN_STRIP_MASK;
1823         }
1824
1825         cur = !!(offload_mask & ETH_VLAN_FILTER_OFFLOAD);
1826         org = !!(dev->data->dev_conf.rxmode.hw_vlan_filter);
1827         if (cur != org) {
1828                 dev->data->dev_conf.rxmode.hw_vlan_filter = (uint8_t)cur;
1829                 mask |= ETH_VLAN_FILTER_MASK;
1830         }
1831
1832         cur = !!(offload_mask & ETH_VLAN_EXTEND_OFFLOAD);
1833         org = !!(dev->data->dev_conf.rxmode.hw_vlan_extend);
1834         if (cur != org) {
1835                 dev->data->dev_conf.rxmode.hw_vlan_extend = (uint8_t)cur;
1836                 mask |= ETH_VLAN_EXTEND_MASK;
1837         }
1838
1839         /*no change*/
1840         if (mask == 0)
1841                 return ret;
1842
1843         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_offload_set, -ENOTSUP);
1844         (*dev->dev_ops->vlan_offload_set)(dev, mask);
1845
1846         return ret;
1847 }
1848
1849 int
1850 rte_eth_dev_get_vlan_offload(uint8_t port_id)
1851 {
1852         struct rte_eth_dev *dev;
1853         int ret = 0;
1854
1855         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1856         dev = &rte_eth_devices[port_id];
1857
1858         if (dev->data->dev_conf.rxmode.hw_vlan_strip)
1859                 ret |= ETH_VLAN_STRIP_OFFLOAD;
1860
1861         if (dev->data->dev_conf.rxmode.hw_vlan_filter)
1862                 ret |= ETH_VLAN_FILTER_OFFLOAD;
1863
1864         if (dev->data->dev_conf.rxmode.hw_vlan_extend)
1865                 ret |= ETH_VLAN_EXTEND_OFFLOAD;
1866
1867         return ret;
1868 }
1869
1870 int
1871 rte_eth_dev_set_vlan_pvid(uint8_t port_id, uint16_t pvid, int on)
1872 {
1873         struct rte_eth_dev *dev;
1874
1875         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1876         dev = &rte_eth_devices[port_id];
1877         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_pvid_set, -ENOTSUP);
1878         (*dev->dev_ops->vlan_pvid_set)(dev, pvid, on);
1879
1880         return 0;
1881 }
1882
1883 int
1884 rte_eth_dev_flow_ctrl_get(uint8_t port_id, struct rte_eth_fc_conf *fc_conf)
1885 {
1886         struct rte_eth_dev *dev;
1887
1888         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1889         dev = &rte_eth_devices[port_id];
1890         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_get, -ENOTSUP);
1891         memset(fc_conf, 0, sizeof(*fc_conf));
1892         return (*dev->dev_ops->flow_ctrl_get)(dev, fc_conf);
1893 }
1894
1895 int
1896 rte_eth_dev_flow_ctrl_set(uint8_t port_id, struct rte_eth_fc_conf *fc_conf)
1897 {
1898         struct rte_eth_dev *dev;
1899
1900         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1901         if ((fc_conf->send_xon != 0) && (fc_conf->send_xon != 1)) {
1902                 RTE_PMD_DEBUG_TRACE("Invalid send_xon, only 0/1 allowed\n");
1903                 return -EINVAL;
1904         }
1905
1906         dev = &rte_eth_devices[port_id];
1907         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_set, -ENOTSUP);
1908         return (*dev->dev_ops->flow_ctrl_set)(dev, fc_conf);
1909 }
1910
1911 int
1912 rte_eth_dev_priority_flow_ctrl_set(uint8_t port_id, struct rte_eth_pfc_conf *pfc_conf)
1913 {
1914         struct rte_eth_dev *dev;
1915
1916         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1917         if (pfc_conf->priority > (ETH_DCB_NUM_USER_PRIORITIES - 1)) {
1918                 RTE_PMD_DEBUG_TRACE("Invalid priority, only 0-7 allowed\n");
1919                 return -EINVAL;
1920         }
1921
1922         dev = &rte_eth_devices[port_id];
1923         /* High water, low water validation are device specific */
1924         if  (*dev->dev_ops->priority_flow_ctrl_set)
1925                 return (*dev->dev_ops->priority_flow_ctrl_set)(dev, pfc_conf);
1926         return -ENOTSUP;
1927 }
1928
1929 static int
1930 rte_eth_check_reta_mask(struct rte_eth_rss_reta_entry64 *reta_conf,
1931                         uint16_t reta_size)
1932 {
1933         uint16_t i, num;
1934
1935         if (!reta_conf)
1936                 return -EINVAL;
1937
1938         if (reta_size != RTE_ALIGN(reta_size, RTE_RETA_GROUP_SIZE)) {
1939                 RTE_PMD_DEBUG_TRACE("Invalid reta size, should be %u aligned\n",
1940                                                         RTE_RETA_GROUP_SIZE);
1941                 return -EINVAL;
1942         }
1943
1944         num = reta_size / RTE_RETA_GROUP_SIZE;
1945         for (i = 0; i < num; i++) {
1946                 if (reta_conf[i].mask)
1947                         return 0;
1948         }
1949
1950         return -EINVAL;
1951 }
1952
1953 static int
1954 rte_eth_check_reta_entry(struct rte_eth_rss_reta_entry64 *reta_conf,
1955                          uint16_t reta_size,
1956                          uint16_t max_rxq)
1957 {
1958         uint16_t i, idx, shift;
1959
1960         if (!reta_conf)
1961                 return -EINVAL;
1962
1963         if (max_rxq == 0) {
1964                 RTE_PMD_DEBUG_TRACE("No receive queue is available\n");
1965                 return -EINVAL;
1966         }
1967
1968         for (i = 0; i < reta_size; i++) {
1969                 idx = i / RTE_RETA_GROUP_SIZE;
1970                 shift = i % RTE_RETA_GROUP_SIZE;
1971                 if ((reta_conf[idx].mask & (1ULL << shift)) &&
1972                         (reta_conf[idx].reta[shift] >= max_rxq)) {
1973                         RTE_PMD_DEBUG_TRACE("reta_conf[%u]->reta[%u]: %u exceeds "
1974                                 "the maximum rxq index: %u\n", idx, shift,
1975                                 reta_conf[idx].reta[shift], max_rxq);
1976                         return -EINVAL;
1977                 }
1978         }
1979
1980         return 0;
1981 }
1982
1983 int
1984 rte_eth_dev_rss_reta_update(uint8_t port_id,
1985                             struct rte_eth_rss_reta_entry64 *reta_conf,
1986                             uint16_t reta_size)
1987 {
1988         struct rte_eth_dev *dev;
1989         int ret;
1990
1991         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1992         /* Check mask bits */
1993         ret = rte_eth_check_reta_mask(reta_conf, reta_size);
1994         if (ret < 0)
1995                 return ret;
1996
1997         dev = &rte_eth_devices[port_id];
1998
1999         /* Check entry value */
2000         ret = rte_eth_check_reta_entry(reta_conf, reta_size,
2001                                 dev->data->nb_rx_queues);
2002         if (ret < 0)
2003                 return ret;
2004
2005         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->reta_update, -ENOTSUP);
2006         return (*dev->dev_ops->reta_update)(dev, reta_conf, reta_size);
2007 }
2008
2009 int
2010 rte_eth_dev_rss_reta_query(uint8_t port_id,
2011                            struct rte_eth_rss_reta_entry64 *reta_conf,
2012                            uint16_t reta_size)
2013 {
2014         struct rte_eth_dev *dev;
2015         int ret;
2016
2017         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2018
2019         /* Check mask bits */
2020         ret = rte_eth_check_reta_mask(reta_conf, reta_size);
2021         if (ret < 0)
2022                 return ret;
2023
2024         dev = &rte_eth_devices[port_id];
2025         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->reta_query, -ENOTSUP);
2026         return (*dev->dev_ops->reta_query)(dev, reta_conf, reta_size);
2027 }
2028
2029 int
2030 rte_eth_dev_rss_hash_update(uint8_t port_id, struct rte_eth_rss_conf *rss_conf)
2031 {
2032         struct rte_eth_dev *dev;
2033         uint16_t rss_hash_protos;
2034
2035         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2036         rss_hash_protos = rss_conf->rss_hf;
2037         if ((rss_hash_protos != 0) &&
2038             ((rss_hash_protos & ETH_RSS_PROTO_MASK) == 0)) {
2039                 RTE_PMD_DEBUG_TRACE("Invalid rss_hash_protos=0x%x\n",
2040                                 rss_hash_protos);
2041                 return -EINVAL;
2042         }
2043         dev = &rte_eth_devices[port_id];
2044         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rss_hash_update, -ENOTSUP);
2045         return (*dev->dev_ops->rss_hash_update)(dev, rss_conf);
2046 }
2047
2048 int
2049 rte_eth_dev_rss_hash_conf_get(uint8_t port_id,
2050                               struct rte_eth_rss_conf *rss_conf)
2051 {
2052         struct rte_eth_dev *dev;
2053
2054         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2055         dev = &rte_eth_devices[port_id];
2056         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rss_hash_conf_get, -ENOTSUP);
2057         return (*dev->dev_ops->rss_hash_conf_get)(dev, rss_conf);
2058 }
2059
2060 int
2061 rte_eth_dev_udp_tunnel_port_add(uint8_t port_id,
2062                                 struct rte_eth_udp_tunnel *udp_tunnel)
2063 {
2064         struct rte_eth_dev *dev;
2065
2066         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2067         if (udp_tunnel == NULL) {
2068                 RTE_PMD_DEBUG_TRACE("Invalid udp_tunnel parameter\n");
2069                 return -EINVAL;
2070         }
2071
2072         if (udp_tunnel->prot_type >= RTE_TUNNEL_TYPE_MAX) {
2073                 RTE_PMD_DEBUG_TRACE("Invalid tunnel type\n");
2074                 return -EINVAL;
2075         }
2076
2077         dev = &rte_eth_devices[port_id];
2078         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->udp_tunnel_port_add, -ENOTSUP);
2079         return (*dev->dev_ops->udp_tunnel_port_add)(dev, udp_tunnel);
2080 }
2081
2082 int
2083 rte_eth_dev_udp_tunnel_port_delete(uint8_t port_id,
2084                                    struct rte_eth_udp_tunnel *udp_tunnel)
2085 {
2086         struct rte_eth_dev *dev;
2087
2088         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2089         dev = &rte_eth_devices[port_id];
2090
2091         if (udp_tunnel == NULL) {
2092                 RTE_PMD_DEBUG_TRACE("Invalid udp_tunnel parameter\n");
2093                 return -EINVAL;
2094         }
2095
2096         if (udp_tunnel->prot_type >= RTE_TUNNEL_TYPE_MAX) {
2097                 RTE_PMD_DEBUG_TRACE("Invalid tunnel type\n");
2098                 return -EINVAL;
2099         }
2100
2101         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->udp_tunnel_port_del, -ENOTSUP);
2102         return (*dev->dev_ops->udp_tunnel_port_del)(dev, udp_tunnel);
2103 }
2104
2105 int
2106 rte_eth_led_on(uint8_t port_id)
2107 {
2108         struct rte_eth_dev *dev;
2109
2110         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2111         dev = &rte_eth_devices[port_id];
2112         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_on, -ENOTSUP);
2113         return (*dev->dev_ops->dev_led_on)(dev);
2114 }
2115
2116 int
2117 rte_eth_led_off(uint8_t port_id)
2118 {
2119         struct rte_eth_dev *dev;
2120
2121         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2122         dev = &rte_eth_devices[port_id];
2123         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_off, -ENOTSUP);
2124         return (*dev->dev_ops->dev_led_off)(dev);
2125 }
2126
2127 /*
2128  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
2129  * an empty spot.
2130  */
2131 static int
2132 get_mac_addr_index(uint8_t port_id, const struct ether_addr *addr)
2133 {
2134         struct rte_eth_dev_info dev_info;
2135         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
2136         unsigned i;
2137
2138         rte_eth_dev_info_get(port_id, &dev_info);
2139
2140         for (i = 0; i < dev_info.max_mac_addrs; i++)
2141                 if (memcmp(addr, &dev->data->mac_addrs[i], ETHER_ADDR_LEN) == 0)
2142                         return i;
2143
2144         return -1;
2145 }
2146
2147 static const struct ether_addr null_mac_addr;
2148
2149 int
2150 rte_eth_dev_mac_addr_add(uint8_t port_id, struct ether_addr *addr,
2151                         uint32_t pool)
2152 {
2153         struct rte_eth_dev *dev;
2154         int index;
2155         uint64_t pool_mask;
2156
2157         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2158         dev = &rte_eth_devices[port_id];
2159         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_add, -ENOTSUP);
2160
2161         if (is_zero_ether_addr(addr)) {
2162                 RTE_PMD_DEBUG_TRACE("port %d: Cannot add NULL MAC address\n",
2163                         port_id);
2164                 return -EINVAL;
2165         }
2166         if (pool >= ETH_64_POOLS) {
2167                 RTE_PMD_DEBUG_TRACE("pool id must be 0-%d\n", ETH_64_POOLS - 1);
2168                 return -EINVAL;
2169         }
2170
2171         index = get_mac_addr_index(port_id, addr);
2172         if (index < 0) {
2173                 index = get_mac_addr_index(port_id, &null_mac_addr);
2174                 if (index < 0) {
2175                         RTE_PMD_DEBUG_TRACE("port %d: MAC address array full\n",
2176                                 port_id);
2177                         return -ENOSPC;
2178                 }
2179         } else {
2180                 pool_mask = dev->data->mac_pool_sel[index];
2181
2182                 /* Check if both MAC address and pool is already there, and do nothing */
2183                 if (pool_mask & (1ULL << pool))
2184                         return 0;
2185         }
2186
2187         /* Update NIC */
2188         (*dev->dev_ops->mac_addr_add)(dev, addr, index, pool);
2189
2190         /* Update address in NIC data structure */
2191         ether_addr_copy(addr, &dev->data->mac_addrs[index]);
2192
2193         /* Update pool bitmap in NIC data structure */
2194         dev->data->mac_pool_sel[index] |= (1ULL << pool);
2195
2196         return 0;
2197 }
2198
2199 int
2200 rte_eth_dev_mac_addr_remove(uint8_t port_id, struct ether_addr *addr)
2201 {
2202         struct rte_eth_dev *dev;
2203         int index;
2204
2205         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2206         dev = &rte_eth_devices[port_id];
2207         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_remove, -ENOTSUP);
2208
2209         index = get_mac_addr_index(port_id, addr);
2210         if (index == 0) {
2211                 RTE_PMD_DEBUG_TRACE("port %d: Cannot remove default MAC address\n", port_id);
2212                 return -EADDRINUSE;
2213         } else if (index < 0)
2214                 return 0;  /* Do nothing if address wasn't found */
2215
2216         /* Update NIC */
2217         (*dev->dev_ops->mac_addr_remove)(dev, index);
2218
2219         /* Update address in NIC data structure */
2220         ether_addr_copy(&null_mac_addr, &dev->data->mac_addrs[index]);
2221
2222         /* reset pool bitmap */
2223         dev->data->mac_pool_sel[index] = 0;
2224
2225         return 0;
2226 }
2227
2228 int
2229 rte_eth_dev_default_mac_addr_set(uint8_t port_id, struct ether_addr *addr)
2230 {
2231         struct rte_eth_dev *dev;
2232
2233         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2234
2235         if (!is_valid_assigned_ether_addr(addr))
2236                 return -EINVAL;
2237
2238         dev = &rte_eth_devices[port_id];
2239         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_set, -ENOTSUP);
2240
2241         /* Update default address in NIC data structure */
2242         ether_addr_copy(addr, &dev->data->mac_addrs[0]);
2243
2244         (*dev->dev_ops->mac_addr_set)(dev, addr);
2245
2246         return 0;
2247 }
2248
2249
2250 /*
2251  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
2252  * an empty spot.
2253  */
2254 static int
2255 get_hash_mac_addr_index(uint8_t port_id, const struct ether_addr *addr)
2256 {
2257         struct rte_eth_dev_info dev_info;
2258         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
2259         unsigned i;
2260
2261         rte_eth_dev_info_get(port_id, &dev_info);
2262         if (!dev->data->hash_mac_addrs)
2263                 return -1;
2264
2265         for (i = 0; i < dev_info.max_hash_mac_addrs; i++)
2266                 if (memcmp(addr, &dev->data->hash_mac_addrs[i],
2267                         ETHER_ADDR_LEN) == 0)
2268                         return i;
2269
2270         return -1;
2271 }
2272
2273 int
2274 rte_eth_dev_uc_hash_table_set(uint8_t port_id, struct ether_addr *addr,
2275                                 uint8_t on)
2276 {
2277         int index;
2278         int ret;
2279         struct rte_eth_dev *dev;
2280
2281         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2282
2283         dev = &rte_eth_devices[port_id];
2284         if (is_zero_ether_addr(addr)) {
2285                 RTE_PMD_DEBUG_TRACE("port %d: Cannot add NULL MAC address\n",
2286                         port_id);
2287                 return -EINVAL;
2288         }
2289
2290         index = get_hash_mac_addr_index(port_id, addr);
2291         /* Check if it's already there, and do nothing */
2292         if ((index >= 0) && (on))
2293                 return 0;
2294
2295         if (index < 0) {
2296                 if (!on) {
2297                         RTE_PMD_DEBUG_TRACE("port %d: the MAC address was not "
2298                                 "set in UTA\n", port_id);
2299                         return -EINVAL;
2300                 }
2301
2302                 index = get_hash_mac_addr_index(port_id, &null_mac_addr);
2303                 if (index < 0) {
2304                         RTE_PMD_DEBUG_TRACE("port %d: MAC address array full\n",
2305                                         port_id);
2306                         return -ENOSPC;
2307                 }
2308         }
2309
2310         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->uc_hash_table_set, -ENOTSUP);
2311         ret = (*dev->dev_ops->uc_hash_table_set)(dev, addr, on);
2312         if (ret == 0) {
2313                 /* Update address in NIC data structure */
2314                 if (on)
2315                         ether_addr_copy(addr,
2316                                         &dev->data->hash_mac_addrs[index]);
2317                 else
2318                         ether_addr_copy(&null_mac_addr,
2319                                         &dev->data->hash_mac_addrs[index]);
2320         }
2321
2322         return ret;
2323 }
2324
2325 int
2326 rte_eth_dev_uc_all_hash_table_set(uint8_t port_id, uint8_t on)
2327 {
2328         struct rte_eth_dev *dev;
2329
2330         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2331
2332         dev = &rte_eth_devices[port_id];
2333
2334         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->uc_all_hash_table_set, -ENOTSUP);
2335         return (*dev->dev_ops->uc_all_hash_table_set)(dev, on);
2336 }
2337
2338 int rte_eth_set_queue_rate_limit(uint8_t port_id, uint16_t queue_idx,
2339                                         uint16_t tx_rate)
2340 {
2341         struct rte_eth_dev *dev;
2342         struct rte_eth_dev_info dev_info;
2343         struct rte_eth_link link;
2344
2345         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2346
2347         dev = &rte_eth_devices[port_id];
2348         rte_eth_dev_info_get(port_id, &dev_info);
2349         link = dev->data->dev_link;
2350
2351         if (queue_idx > dev_info.max_tx_queues) {
2352                 RTE_PMD_DEBUG_TRACE("set queue rate limit:port %d: "
2353                                 "invalid queue id=%d\n", port_id, queue_idx);
2354                 return -EINVAL;
2355         }
2356
2357         if (tx_rate > link.link_speed) {
2358                 RTE_PMD_DEBUG_TRACE("set queue rate limit:invalid tx_rate=%d, "
2359                                 "bigger than link speed= %d\n",
2360                         tx_rate, link.link_speed);
2361                 return -EINVAL;
2362         }
2363
2364         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_queue_rate_limit, -ENOTSUP);
2365         return (*dev->dev_ops->set_queue_rate_limit)(dev, queue_idx, tx_rate);
2366 }
2367
2368 int
2369 rte_eth_mirror_rule_set(uint8_t port_id,
2370                         struct rte_eth_mirror_conf *mirror_conf,
2371                         uint8_t rule_id, uint8_t on)
2372 {
2373         struct rte_eth_dev *dev;
2374
2375         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2376         if (mirror_conf->rule_type == 0) {
2377                 RTE_PMD_DEBUG_TRACE("mirror rule type can not be 0.\n");
2378                 return -EINVAL;
2379         }
2380
2381         if (mirror_conf->dst_pool >= ETH_64_POOLS) {
2382                 RTE_PMD_DEBUG_TRACE("Invalid dst pool, pool id must be 0-%d\n",
2383                                 ETH_64_POOLS - 1);
2384                 return -EINVAL;
2385         }
2386
2387         if ((mirror_conf->rule_type & (ETH_MIRROR_VIRTUAL_POOL_UP |
2388              ETH_MIRROR_VIRTUAL_POOL_DOWN)) &&
2389             (mirror_conf->pool_mask == 0)) {
2390                 RTE_PMD_DEBUG_TRACE("Invalid mirror pool, pool mask can not be 0.\n");
2391                 return -EINVAL;
2392         }
2393
2394         if ((mirror_conf->rule_type & ETH_MIRROR_VLAN) &&
2395             mirror_conf->vlan.vlan_mask == 0) {
2396                 RTE_PMD_DEBUG_TRACE("Invalid vlan mask, vlan mask can not be 0.\n");
2397                 return -EINVAL;
2398         }
2399
2400         dev = &rte_eth_devices[port_id];
2401         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mirror_rule_set, -ENOTSUP);
2402
2403         return (*dev->dev_ops->mirror_rule_set)(dev, mirror_conf, rule_id, on);
2404 }
2405
2406 int
2407 rte_eth_mirror_rule_reset(uint8_t port_id, uint8_t rule_id)
2408 {
2409         struct rte_eth_dev *dev;
2410
2411         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2412
2413         dev = &rte_eth_devices[port_id];
2414         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mirror_rule_reset, -ENOTSUP);
2415
2416         return (*dev->dev_ops->mirror_rule_reset)(dev, rule_id);
2417 }
2418
2419 int
2420 rte_eth_dev_callback_register(uint8_t port_id,
2421                         enum rte_eth_event_type event,
2422                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
2423 {
2424         struct rte_eth_dev *dev;
2425         struct rte_eth_dev_callback *user_cb;
2426
2427         if (!cb_fn)
2428                 return -EINVAL;
2429
2430         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
2431
2432         dev = &rte_eth_devices[port_id];
2433         rte_spinlock_lock(&rte_eth_dev_cb_lock);
2434
2435         TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
2436                 if (user_cb->cb_fn == cb_fn &&
2437                         user_cb->cb_arg == cb_arg &&
2438                         user_cb->event == event) {
2439                         break;
2440                 }
2441         }
2442
2443         /* create a new callback. */
2444         if (user_cb == NULL) {
2445                 user_cb = rte_zmalloc("INTR_USER_CALLBACK",
2446                                         sizeof(struct rte_eth_dev_callback), 0);
2447                 if (user_cb != NULL) {
2448                         user_cb->cb_fn = cb_fn;
2449                         user_cb->cb_arg = cb_arg;
2450                         user_cb->event = event;
2451                         TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
2452                 }
2453         }
2454
2455         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2456         return (user_cb == NULL) ? -ENOMEM : 0;
2457 }
2458
2459 int
2460 rte_eth_dev_callback_unregister(uint8_t port_id,
2461                         enum rte_eth_event_type event,
2462                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
2463 {
2464         int ret;
2465         struct rte_eth_dev *dev;
2466         struct rte_eth_dev_callback *cb, *next;
2467
2468         if (!cb_fn)
2469                 return -EINVAL;
2470
2471         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
2472
2473         dev = &rte_eth_devices[port_id];
2474         rte_spinlock_lock(&rte_eth_dev_cb_lock);
2475
2476         ret = 0;
2477         for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL; cb = next) {
2478
2479                 next = TAILQ_NEXT(cb, next);
2480
2481                 if (cb->cb_fn != cb_fn || cb->event != event ||
2482                                 (cb->cb_arg != (void *)-1 &&
2483                                 cb->cb_arg != cb_arg))
2484                         continue;
2485
2486                 /*
2487                  * if this callback is not executing right now,
2488                  * then remove it.
2489                  */
2490                 if (cb->active == 0) {
2491                         TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
2492                         rte_free(cb);
2493                 } else {
2494                         ret = -EAGAIN;
2495                 }
2496         }
2497
2498         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2499         return ret;
2500 }
2501
2502 void
2503 _rte_eth_dev_callback_process(struct rte_eth_dev *dev,
2504         enum rte_eth_event_type event, void *cb_arg)
2505 {
2506         struct rte_eth_dev_callback *cb_lst;
2507         struct rte_eth_dev_callback dev_cb;
2508
2509         rte_spinlock_lock(&rte_eth_dev_cb_lock);
2510         TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
2511                 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
2512                         continue;
2513                 dev_cb = *cb_lst;
2514                 cb_lst->active = 1;
2515                 if (cb_arg != NULL)
2516                         dev_cb.cb_arg = (void *) cb_arg;
2517
2518                 rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2519                 dev_cb.cb_fn(dev->data->port_id, dev_cb.event,
2520                                                 dev_cb.cb_arg);
2521                 rte_spinlock_lock(&rte_eth_dev_cb_lock);
2522                 cb_lst->active = 0;
2523         }
2524         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2525 }
2526
2527 int
2528 rte_eth_dev_rx_intr_ctl(uint8_t port_id, int epfd, int op, void *data)
2529 {
2530         uint32_t vec;
2531         struct rte_eth_dev *dev;
2532         struct rte_intr_handle *intr_handle;
2533         uint16_t qid;
2534         int rc;
2535
2536         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2537
2538         dev = &rte_eth_devices[port_id];
2539
2540         if (!dev->intr_handle) {
2541                 RTE_PMD_DEBUG_TRACE("RX Intr handle unset\n");
2542                 return -ENOTSUP;
2543         }
2544
2545         intr_handle = dev->intr_handle;
2546         if (!intr_handle->intr_vec) {
2547                 RTE_PMD_DEBUG_TRACE("RX Intr vector unset\n");
2548                 return -EPERM;
2549         }
2550
2551         for (qid = 0; qid < dev->data->nb_rx_queues; qid++) {
2552                 vec = intr_handle->intr_vec[qid];
2553                 rc = rte_intr_rx_ctl(intr_handle, epfd, op, vec, data);
2554                 if (rc && rc != -EEXIST) {
2555                         RTE_PMD_DEBUG_TRACE("p %u q %u rx ctl error"
2556                                         " op %d epfd %d vec %u\n",
2557                                         port_id, qid, op, epfd, vec);
2558                 }
2559         }
2560
2561         return 0;
2562 }
2563
2564 const struct rte_memzone *
2565 rte_eth_dma_zone_reserve(const struct rte_eth_dev *dev, const char *ring_name,
2566                          uint16_t queue_id, size_t size, unsigned align,
2567                          int socket_id)
2568 {
2569         char z_name[RTE_MEMZONE_NAMESIZE];
2570         const struct rte_memzone *mz;
2571
2572         snprintf(z_name, sizeof(z_name), "%s_%s_%d_%d",
2573                  dev->data->drv_name, ring_name,
2574                  dev->data->port_id, queue_id);
2575
2576         mz = rte_memzone_lookup(z_name);
2577         if (mz)
2578                 return mz;
2579
2580         if (rte_xen_dom0_supported())
2581                 return rte_memzone_reserve_bounded(z_name, size, socket_id,
2582                                                    0, align, RTE_PGSIZE_2M);
2583         else
2584                 return rte_memzone_reserve_aligned(z_name, size, socket_id,
2585                                                    0, align);
2586 }
2587
2588 int
2589 rte_eth_dev_rx_intr_ctl_q(uint8_t port_id, uint16_t queue_id,
2590                           int epfd, int op, void *data)
2591 {
2592         uint32_t vec;
2593         struct rte_eth_dev *dev;
2594         struct rte_intr_handle *intr_handle;
2595         int rc;
2596
2597         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2598
2599         dev = &rte_eth_devices[port_id];
2600         if (queue_id >= dev->data->nb_rx_queues) {
2601                 RTE_PMD_DEBUG_TRACE("Invalid RX queue_id=%u\n", queue_id);
2602                 return -EINVAL;
2603         }
2604
2605         if (!dev->intr_handle) {
2606                 RTE_PMD_DEBUG_TRACE("RX Intr handle unset\n");
2607                 return -ENOTSUP;
2608         }
2609
2610         intr_handle = dev->intr_handle;
2611         if (!intr_handle->intr_vec) {
2612                 RTE_PMD_DEBUG_TRACE("RX Intr vector unset\n");
2613                 return -EPERM;
2614         }
2615
2616         vec = intr_handle->intr_vec[queue_id];
2617         rc = rte_intr_rx_ctl(intr_handle, epfd, op, vec, data);
2618         if (rc && rc != -EEXIST) {
2619                 RTE_PMD_DEBUG_TRACE("p %u q %u rx ctl error"
2620                                 " op %d epfd %d vec %u\n",
2621                                 port_id, queue_id, op, epfd, vec);
2622                 return rc;
2623         }
2624
2625         return 0;
2626 }
2627
2628 int
2629 rte_eth_dev_rx_intr_enable(uint8_t port_id,
2630                            uint16_t queue_id)
2631 {
2632         struct rte_eth_dev *dev;
2633
2634         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2635
2636         dev = &rte_eth_devices[port_id];
2637
2638         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_intr_enable, -ENOTSUP);
2639         return (*dev->dev_ops->rx_queue_intr_enable)(dev, queue_id);
2640 }
2641
2642 int
2643 rte_eth_dev_rx_intr_disable(uint8_t port_id,
2644                             uint16_t queue_id)
2645 {
2646         struct rte_eth_dev *dev;
2647
2648         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2649
2650         dev = &rte_eth_devices[port_id];
2651
2652         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_intr_disable, -ENOTSUP);
2653         return (*dev->dev_ops->rx_queue_intr_disable)(dev, queue_id);
2654 }
2655
2656 #ifdef RTE_NIC_BYPASS
2657 int rte_eth_dev_bypass_init(uint8_t port_id)
2658 {
2659         struct rte_eth_dev *dev;
2660
2661         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2662
2663         dev = &rte_eth_devices[port_id];
2664         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_init, -ENOTSUP);
2665         (*dev->dev_ops->bypass_init)(dev);
2666         return 0;
2667 }
2668
2669 int
2670 rte_eth_dev_bypass_state_show(uint8_t port_id, uint32_t *state)
2671 {
2672         struct rte_eth_dev *dev;
2673
2674         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2675
2676         dev = &rte_eth_devices[port_id];
2677         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_state_show, -ENOTSUP);
2678         (*dev->dev_ops->bypass_state_show)(dev, state);
2679         return 0;
2680 }
2681
2682 int
2683 rte_eth_dev_bypass_state_set(uint8_t port_id, uint32_t *new_state)
2684 {
2685         struct rte_eth_dev *dev;
2686
2687         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2688
2689         dev = &rte_eth_devices[port_id];
2690         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_state_set, -ENOTSUP);
2691         (*dev->dev_ops->bypass_state_set)(dev, new_state);
2692         return 0;
2693 }
2694
2695 int
2696 rte_eth_dev_bypass_event_show(uint8_t port_id, uint32_t event, uint32_t *state)
2697 {
2698         struct rte_eth_dev *dev;
2699
2700         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2701
2702         dev = &rte_eth_devices[port_id];
2703         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_state_show, -ENOTSUP);
2704         (*dev->dev_ops->bypass_event_show)(dev, event, state);
2705         return 0;
2706 }
2707
2708 int
2709 rte_eth_dev_bypass_event_store(uint8_t port_id, uint32_t event, uint32_t state)
2710 {
2711         struct rte_eth_dev *dev;
2712
2713         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2714
2715         dev = &rte_eth_devices[port_id];
2716
2717         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_event_set, -ENOTSUP);
2718         (*dev->dev_ops->bypass_event_set)(dev, event, state);
2719         return 0;
2720 }
2721
2722 int
2723 rte_eth_dev_wd_timeout_store(uint8_t port_id, uint32_t timeout)
2724 {
2725         struct rte_eth_dev *dev;
2726
2727         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2728
2729         dev = &rte_eth_devices[port_id];
2730
2731         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_wd_timeout_set, -ENOTSUP);
2732         (*dev->dev_ops->bypass_wd_timeout_set)(dev, timeout);
2733         return 0;
2734 }
2735
2736 int
2737 rte_eth_dev_bypass_ver_show(uint8_t port_id, uint32_t *ver)
2738 {
2739         struct rte_eth_dev *dev;
2740
2741         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2742
2743         dev = &rte_eth_devices[port_id];
2744
2745         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_ver_show, -ENOTSUP);
2746         (*dev->dev_ops->bypass_ver_show)(dev, ver);
2747         return 0;
2748 }
2749
2750 int
2751 rte_eth_dev_bypass_wd_timeout_show(uint8_t port_id, uint32_t *wd_timeout)
2752 {
2753         struct rte_eth_dev *dev;
2754
2755         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2756
2757         dev = &rte_eth_devices[port_id];
2758
2759         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_wd_timeout_show, -ENOTSUP);
2760         (*dev->dev_ops->bypass_wd_timeout_show)(dev, wd_timeout);
2761         return 0;
2762 }
2763
2764 int
2765 rte_eth_dev_bypass_wd_reset(uint8_t port_id)
2766 {
2767         struct rte_eth_dev *dev;
2768
2769         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2770
2771         dev = &rte_eth_devices[port_id];
2772
2773         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_wd_reset, -ENOTSUP);
2774         (*dev->dev_ops->bypass_wd_reset)(dev);
2775         return 0;
2776 }
2777 #endif
2778
2779 int
2780 rte_eth_dev_filter_supported(uint8_t port_id, enum rte_filter_type filter_type)
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->filter_ctrl, -ENOTSUP);
2788         return (*dev->dev_ops->filter_ctrl)(dev, filter_type,
2789                                 RTE_ETH_FILTER_NOP, NULL);
2790 }
2791
2792 int
2793 rte_eth_dev_filter_ctrl(uint8_t port_id, enum rte_filter_type filter_type,
2794                        enum rte_filter_op filter_op, void *arg)
2795 {
2796         struct rte_eth_dev *dev;
2797
2798         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2799
2800         dev = &rte_eth_devices[port_id];
2801         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->filter_ctrl, -ENOTSUP);
2802         return (*dev->dev_ops->filter_ctrl)(dev, filter_type, filter_op, arg);
2803 }
2804
2805 void *
2806 rte_eth_add_rx_callback(uint8_t port_id, uint16_t queue_id,
2807                 rte_rx_callback_fn fn, void *user_param)
2808 {
2809 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
2810         rte_errno = ENOTSUP;
2811         return NULL;
2812 #endif
2813         /* check input parameters */
2814         if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
2815                     queue_id >= rte_eth_devices[port_id].data->nb_rx_queues) {
2816                 rte_errno = EINVAL;
2817                 return NULL;
2818         }
2819         struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
2820
2821         if (cb == NULL) {
2822                 rte_errno = ENOMEM;
2823                 return NULL;
2824         }
2825
2826         cb->fn.rx = fn;
2827         cb->param = user_param;
2828
2829         rte_spinlock_lock(&rte_eth_rx_cb_lock);
2830         /* Add the callbacks in fifo order. */
2831         struct rte_eth_rxtx_callback *tail =
2832                 rte_eth_devices[port_id].post_rx_burst_cbs[queue_id];
2833
2834         if (!tail) {
2835                 rte_eth_devices[port_id].post_rx_burst_cbs[queue_id] = cb;
2836
2837         } else {
2838                 while (tail->next)
2839                         tail = tail->next;
2840                 tail->next = cb;
2841         }
2842         rte_spinlock_unlock(&rte_eth_rx_cb_lock);
2843
2844         return cb;
2845 }
2846
2847 void *
2848 rte_eth_add_first_rx_callback(uint8_t port_id, uint16_t queue_id,
2849                 rte_rx_callback_fn fn, void *user_param)
2850 {
2851 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
2852         rte_errno = ENOTSUP;
2853         return NULL;
2854 #endif
2855         /* check input parameters */
2856         if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
2857                 queue_id >= rte_eth_devices[port_id].data->nb_rx_queues) {
2858                 rte_errno = EINVAL;
2859                 return NULL;
2860         }
2861
2862         struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
2863
2864         if (cb == NULL) {
2865                 rte_errno = ENOMEM;
2866                 return NULL;
2867         }
2868
2869         cb->fn.rx = fn;
2870         cb->param = user_param;
2871
2872         rte_spinlock_lock(&rte_eth_rx_cb_lock);
2873         /* Add the callbacks at fisrt position*/
2874         cb->next = rte_eth_devices[port_id].post_rx_burst_cbs[queue_id];
2875         rte_smp_wmb();
2876         rte_eth_devices[port_id].post_rx_burst_cbs[queue_id] = cb;
2877         rte_spinlock_unlock(&rte_eth_rx_cb_lock);
2878
2879         return cb;
2880 }
2881
2882 void *
2883 rte_eth_add_tx_callback(uint8_t port_id, uint16_t queue_id,
2884                 rte_tx_callback_fn fn, void *user_param)
2885 {
2886 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
2887         rte_errno = ENOTSUP;
2888         return NULL;
2889 #endif
2890         /* check input parameters */
2891         if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
2892                     queue_id >= rte_eth_devices[port_id].data->nb_tx_queues) {
2893                 rte_errno = EINVAL;
2894                 return NULL;
2895         }
2896
2897         struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
2898
2899         if (cb == NULL) {
2900                 rte_errno = ENOMEM;
2901                 return NULL;
2902         }
2903
2904         cb->fn.tx = fn;
2905         cb->param = user_param;
2906
2907         rte_spinlock_lock(&rte_eth_tx_cb_lock);
2908         /* Add the callbacks in fifo order. */
2909         struct rte_eth_rxtx_callback *tail =
2910                 rte_eth_devices[port_id].pre_tx_burst_cbs[queue_id];
2911
2912         if (!tail) {
2913                 rte_eth_devices[port_id].pre_tx_burst_cbs[queue_id] = cb;
2914
2915         } else {
2916                 while (tail->next)
2917                         tail = tail->next;
2918                 tail->next = cb;
2919         }
2920         rte_spinlock_unlock(&rte_eth_tx_cb_lock);
2921
2922         return cb;
2923 }
2924
2925 int
2926 rte_eth_remove_rx_callback(uint8_t port_id, uint16_t queue_id,
2927                 struct rte_eth_rxtx_callback *user_cb)
2928 {
2929 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
2930         return -ENOTSUP;
2931 #endif
2932         /* Check input parameters. */
2933         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
2934         if (user_cb == NULL ||
2935                         queue_id >= rte_eth_devices[port_id].data->nb_rx_queues)
2936                 return -EINVAL;
2937
2938         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
2939         struct rte_eth_rxtx_callback *cb;
2940         struct rte_eth_rxtx_callback **prev_cb;
2941         int ret = -EINVAL;
2942
2943         rte_spinlock_lock(&rte_eth_rx_cb_lock);
2944         prev_cb = &dev->post_rx_burst_cbs[queue_id];
2945         for (; *prev_cb != NULL; prev_cb = &cb->next) {
2946                 cb = *prev_cb;
2947                 if (cb == user_cb) {
2948                         /* Remove the user cb from the callback list. */
2949                         *prev_cb = cb->next;
2950                         ret = 0;
2951                         break;
2952                 }
2953         }
2954         rte_spinlock_unlock(&rte_eth_rx_cb_lock);
2955
2956         return ret;
2957 }
2958
2959 int
2960 rte_eth_remove_tx_callback(uint8_t port_id, uint16_t queue_id,
2961                 struct rte_eth_rxtx_callback *user_cb)
2962 {
2963 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
2964         return -ENOTSUP;
2965 #endif
2966         /* Check input parameters. */
2967         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
2968         if (user_cb == NULL ||
2969                         queue_id >= rte_eth_devices[port_id].data->nb_tx_queues)
2970                 return -EINVAL;
2971
2972         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
2973         int ret = -EINVAL;
2974         struct rte_eth_rxtx_callback *cb;
2975         struct rte_eth_rxtx_callback **prev_cb;
2976
2977         rte_spinlock_lock(&rte_eth_tx_cb_lock);
2978         prev_cb = &dev->pre_tx_burst_cbs[queue_id];
2979         for (; *prev_cb != NULL; prev_cb = &cb->next) {
2980                 cb = *prev_cb;
2981                 if (cb == user_cb) {
2982                         /* Remove the user cb from the callback list. */
2983                         *prev_cb = cb->next;
2984                         ret = 0;
2985                         break;
2986                 }
2987         }
2988         rte_spinlock_unlock(&rte_eth_tx_cb_lock);
2989
2990         return ret;
2991 }
2992
2993 int
2994 rte_eth_rx_queue_info_get(uint8_t port_id, uint16_t queue_id,
2995         struct rte_eth_rxq_info *qinfo)
2996 {
2997         struct rte_eth_dev *dev;
2998
2999         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3000
3001         if (qinfo == NULL)
3002                 return -EINVAL;
3003
3004         dev = &rte_eth_devices[port_id];
3005         if (queue_id >= dev->data->nb_rx_queues) {
3006                 RTE_PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", queue_id);
3007                 return -EINVAL;
3008         }
3009
3010         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rxq_info_get, -ENOTSUP);
3011
3012         memset(qinfo, 0, sizeof(*qinfo));
3013         dev->dev_ops->rxq_info_get(dev, queue_id, qinfo);
3014         return 0;
3015 }
3016
3017 int
3018 rte_eth_tx_queue_info_get(uint8_t port_id, uint16_t queue_id,
3019         struct rte_eth_txq_info *qinfo)
3020 {
3021         struct rte_eth_dev *dev;
3022
3023         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3024
3025         if (qinfo == NULL)
3026                 return -EINVAL;
3027
3028         dev = &rte_eth_devices[port_id];
3029         if (queue_id >= dev->data->nb_tx_queues) {
3030                 RTE_PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", queue_id);
3031                 return -EINVAL;
3032         }
3033
3034         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->txq_info_get, -ENOTSUP);
3035
3036         memset(qinfo, 0, sizeof(*qinfo));
3037         dev->dev_ops->txq_info_get(dev, queue_id, qinfo);
3038         return 0;
3039 }
3040
3041 int
3042 rte_eth_dev_set_mc_addr_list(uint8_t port_id,
3043                              struct ether_addr *mc_addr_set,
3044                              uint32_t nb_mc_addr)
3045 {
3046         struct rte_eth_dev *dev;
3047
3048         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3049
3050         dev = &rte_eth_devices[port_id];
3051         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_mc_addr_list, -ENOTSUP);
3052         return dev->dev_ops->set_mc_addr_list(dev, mc_addr_set, nb_mc_addr);
3053 }
3054
3055 int
3056 rte_eth_timesync_enable(uint8_t port_id)
3057 {
3058         struct rte_eth_dev *dev;
3059
3060         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3061         dev = &rte_eth_devices[port_id];
3062
3063         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_enable, -ENOTSUP);
3064         return (*dev->dev_ops->timesync_enable)(dev);
3065 }
3066
3067 int
3068 rte_eth_timesync_disable(uint8_t port_id)
3069 {
3070         struct rte_eth_dev *dev;
3071
3072         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3073         dev = &rte_eth_devices[port_id];
3074
3075         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_disable, -ENOTSUP);
3076         return (*dev->dev_ops->timesync_disable)(dev);
3077 }
3078
3079 int
3080 rte_eth_timesync_read_rx_timestamp(uint8_t port_id, struct timespec *timestamp,
3081                                    uint32_t flags)
3082 {
3083         struct rte_eth_dev *dev;
3084
3085         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3086         dev = &rte_eth_devices[port_id];
3087
3088         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_rx_timestamp, -ENOTSUP);
3089         return (*dev->dev_ops->timesync_read_rx_timestamp)(dev, timestamp, flags);
3090 }
3091
3092 int
3093 rte_eth_timesync_read_tx_timestamp(uint8_t port_id, struct timespec *timestamp)
3094 {
3095         struct rte_eth_dev *dev;
3096
3097         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3098         dev = &rte_eth_devices[port_id];
3099
3100         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_tx_timestamp, -ENOTSUP);
3101         return (*dev->dev_ops->timesync_read_tx_timestamp)(dev, timestamp);
3102 }
3103
3104 int
3105 rte_eth_timesync_adjust_time(uint8_t port_id, int64_t delta)
3106 {
3107         struct rte_eth_dev *dev;
3108
3109         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3110         dev = &rte_eth_devices[port_id];
3111
3112         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_adjust_time, -ENOTSUP);
3113         return (*dev->dev_ops->timesync_adjust_time)(dev, delta);
3114 }
3115
3116 int
3117 rte_eth_timesync_read_time(uint8_t port_id, struct timespec *timestamp)
3118 {
3119         struct rte_eth_dev *dev;
3120
3121         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3122         dev = &rte_eth_devices[port_id];
3123
3124         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_time, -ENOTSUP);
3125         return (*dev->dev_ops->timesync_read_time)(dev, timestamp);
3126 }
3127
3128 int
3129 rte_eth_timesync_write_time(uint8_t port_id, const struct timespec *timestamp)
3130 {
3131         struct rte_eth_dev *dev;
3132
3133         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3134         dev = &rte_eth_devices[port_id];
3135
3136         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_write_time, -ENOTSUP);
3137         return (*dev->dev_ops->timesync_write_time)(dev, timestamp);
3138 }
3139
3140 int
3141 rte_eth_dev_get_reg_info(uint8_t port_id, struct rte_dev_reg_info *info)
3142 {
3143         struct rte_eth_dev *dev;
3144
3145         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3146
3147         dev = &rte_eth_devices[port_id];
3148         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_reg, -ENOTSUP);
3149         return (*dev->dev_ops->get_reg)(dev, info);
3150 }
3151
3152 int
3153 rte_eth_dev_get_eeprom_length(uint8_t port_id)
3154 {
3155         struct rte_eth_dev *dev;
3156
3157         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3158
3159         dev = &rte_eth_devices[port_id];
3160         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_eeprom_length, -ENOTSUP);
3161         return (*dev->dev_ops->get_eeprom_length)(dev);
3162 }
3163
3164 int
3165 rte_eth_dev_get_eeprom(uint8_t port_id, struct rte_dev_eeprom_info *info)
3166 {
3167         struct rte_eth_dev *dev;
3168
3169         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3170
3171         dev = &rte_eth_devices[port_id];
3172         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_eeprom, -ENOTSUP);
3173         return (*dev->dev_ops->get_eeprom)(dev, info);
3174 }
3175
3176 int
3177 rte_eth_dev_set_eeprom(uint8_t port_id, struct rte_dev_eeprom_info *info)
3178 {
3179         struct rte_eth_dev *dev;
3180
3181         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3182
3183         dev = &rte_eth_devices[port_id];
3184         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_eeprom, -ENOTSUP);
3185         return (*dev->dev_ops->set_eeprom)(dev, info);
3186 }
3187
3188 int
3189 rte_eth_dev_get_dcb_info(uint8_t port_id,
3190                              struct rte_eth_dcb_info *dcb_info)
3191 {
3192         struct rte_eth_dev *dev;
3193
3194         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3195
3196         dev = &rte_eth_devices[port_id];
3197         memset(dcb_info, 0, sizeof(struct rte_eth_dcb_info));
3198
3199         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_dcb_info, -ENOTSUP);
3200         return (*dev->dev_ops->get_dcb_info)(dev, dcb_info);
3201 }
3202
3203 void
3204 rte_eth_copy_pci_info(struct rte_eth_dev *eth_dev, struct rte_pci_device *pci_dev)
3205 {
3206         if ((eth_dev == NULL) || (pci_dev == NULL)) {
3207                 RTE_PMD_DEBUG_TRACE("NULL pointer eth_dev=%p pci_dev=%p\n",
3208                                 eth_dev, pci_dev);
3209                 return;
3210         }
3211
3212         eth_dev->intr_handle = &pci_dev->intr_handle;
3213
3214         eth_dev->data->dev_flags = 0;
3215         if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
3216                 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
3217
3218         eth_dev->data->kdrv = pci_dev->kdrv;
3219         eth_dev->data->numa_node = pci_dev->device.numa_node;
3220         eth_dev->data->drv_name = pci_dev->driver->driver.name;
3221 }
3222
3223 int
3224 rte_eth_dev_l2_tunnel_eth_type_conf(uint8_t port_id,
3225                                     struct rte_eth_l2_tunnel_conf *l2_tunnel)
3226 {
3227         struct rte_eth_dev *dev;
3228
3229         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3230         if (l2_tunnel == NULL) {
3231                 RTE_PMD_DEBUG_TRACE("Invalid l2_tunnel parameter\n");
3232                 return -EINVAL;
3233         }
3234
3235         if (l2_tunnel->l2_tunnel_type >= RTE_TUNNEL_TYPE_MAX) {
3236                 RTE_PMD_DEBUG_TRACE("Invalid tunnel type\n");
3237                 return -EINVAL;
3238         }
3239
3240         dev = &rte_eth_devices[port_id];
3241         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->l2_tunnel_eth_type_conf,
3242                                 -ENOTSUP);
3243         return (*dev->dev_ops->l2_tunnel_eth_type_conf)(dev, l2_tunnel);
3244 }
3245
3246 int
3247 rte_eth_dev_l2_tunnel_offload_set(uint8_t port_id,
3248                                   struct rte_eth_l2_tunnel_conf *l2_tunnel,
3249                                   uint32_t mask,
3250                                   uint8_t en)
3251 {
3252         struct rte_eth_dev *dev;
3253
3254         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3255
3256         if (l2_tunnel == NULL) {
3257                 RTE_PMD_DEBUG_TRACE("Invalid l2_tunnel parameter\n");
3258                 return -EINVAL;
3259         }
3260
3261         if (l2_tunnel->l2_tunnel_type >= RTE_TUNNEL_TYPE_MAX) {
3262                 RTE_PMD_DEBUG_TRACE("Invalid tunnel type.\n");
3263                 return -EINVAL;
3264         }
3265
3266         if (mask == 0) {
3267                 RTE_PMD_DEBUG_TRACE("Mask should have a value.\n");
3268                 return -EINVAL;
3269         }
3270
3271         dev = &rte_eth_devices[port_id];
3272         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->l2_tunnel_offload_set,
3273                                 -ENOTSUP);
3274         return (*dev->dev_ops->l2_tunnel_offload_set)(dev, l2_tunnel, mask, en);
3275 }