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