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