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