a88916f2adbba8e8f14b6f435ac38096c95772e5
[dpdk.git] / lib / librte_ether / rte_ethdev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2017 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
76 /* spinlock for eth device callbacks */
77 static rte_spinlock_t rte_eth_dev_cb_lock = RTE_SPINLOCK_INITIALIZER;
78
79 /* spinlock for add/remove rx callbacks */
80 static rte_spinlock_t rte_eth_rx_cb_lock = RTE_SPINLOCK_INITIALIZER;
81
82 /* spinlock for add/remove tx callbacks */
83 static rte_spinlock_t rte_eth_tx_cb_lock = RTE_SPINLOCK_INITIALIZER;
84
85 /* store statistics names and its offset in stats structure  */
86 struct rte_eth_xstats_name_off {
87         char name[RTE_ETH_XSTATS_NAME_SIZE];
88         unsigned offset;
89 };
90
91 static const struct rte_eth_xstats_name_off rte_stats_strings[] = {
92         {"rx_good_packets", offsetof(struct rte_eth_stats, ipackets)},
93         {"tx_good_packets", offsetof(struct rte_eth_stats, opackets)},
94         {"rx_good_bytes", offsetof(struct rte_eth_stats, ibytes)},
95         {"tx_good_bytes", offsetof(struct rte_eth_stats, obytes)},
96         {"rx_errors", offsetof(struct rte_eth_stats, ierrors)},
97         {"tx_errors", offsetof(struct rte_eth_stats, oerrors)},
98         {"rx_mbuf_allocation_errors", offsetof(struct rte_eth_stats,
99                 rx_nombuf)},
100 };
101
102 #define RTE_NB_STATS (sizeof(rte_stats_strings) / sizeof(rte_stats_strings[0]))
103
104 static const struct rte_eth_xstats_name_off rte_rxq_stats_strings[] = {
105         {"packets", offsetof(struct rte_eth_stats, q_ipackets)},
106         {"bytes", offsetof(struct rte_eth_stats, q_ibytes)},
107         {"errors", offsetof(struct rte_eth_stats, q_errors)},
108 };
109
110 #define RTE_NB_RXQ_STATS (sizeof(rte_rxq_stats_strings) /       \
111                 sizeof(rte_rxq_stats_strings[0]))
112
113 static const struct rte_eth_xstats_name_off rte_txq_stats_strings[] = {
114         {"packets", offsetof(struct rte_eth_stats, q_opackets)},
115         {"bytes", offsetof(struct rte_eth_stats, q_obytes)},
116 };
117 #define RTE_NB_TXQ_STATS (sizeof(rte_txq_stats_strings) /       \
118                 sizeof(rte_txq_stats_strings[0]))
119
120
121 /**
122  * The user application callback description.
123  *
124  * It contains callback address to be registered by user application,
125  * the pointer to the parameters for callback, and the event type.
126  */
127 struct rte_eth_dev_callback {
128         TAILQ_ENTRY(rte_eth_dev_callback) next; /**< Callbacks list */
129         rte_eth_dev_cb_fn cb_fn;                /**< Callback address */
130         void *cb_arg;                           /**< Parameter for callback */
131         void *ret_param;                        /**< Return parameter */
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                                 rte_eth_devices[i].device) {
183                         if (!strcmp(rte_eth_devices[i].device->name, name))
184                                 return &rte_eth_devices[i];
185                 }
186         }
187         return NULL;
188 }
189
190 static uint8_t
191 rte_eth_dev_find_free_port(void)
192 {
193         unsigned i;
194
195         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
196                 if (rte_eth_devices[i].state == RTE_ETH_DEV_UNUSED)
197                         return i;
198         }
199         return RTE_MAX_ETHPORTS;
200 }
201
202 static struct rte_eth_dev *
203 eth_dev_get(uint8_t port_id)
204 {
205         struct rte_eth_dev *eth_dev = &rte_eth_devices[port_id];
206
207         eth_dev->data = &rte_eth_dev_data[port_id];
208         eth_dev->state = RTE_ETH_DEV_ATTACHED;
209         TAILQ_INIT(&(eth_dev->link_intr_cbs));
210
211         eth_dev_last_created_port = port_id;
212
213         return eth_dev;
214 }
215
216 struct rte_eth_dev *
217 rte_eth_dev_allocate(const char *name)
218 {
219         uint8_t port_id;
220         struct rte_eth_dev *eth_dev;
221
222         port_id = rte_eth_dev_find_free_port();
223         if (port_id == RTE_MAX_ETHPORTS) {
224                 RTE_PMD_DEBUG_TRACE("Reached maximum number of Ethernet ports\n");
225                 return NULL;
226         }
227
228         if (rte_eth_dev_data == NULL)
229                 rte_eth_dev_data_alloc();
230
231         if (rte_eth_dev_allocated(name) != NULL) {
232                 RTE_PMD_DEBUG_TRACE("Ethernet Device with name %s already allocated!\n",
233                                 name);
234                 return NULL;
235         }
236
237         memset(&rte_eth_dev_data[port_id], 0, sizeof(struct rte_eth_dev_data));
238         eth_dev = eth_dev_get(port_id);
239         snprintf(eth_dev->data->name, sizeof(eth_dev->data->name), "%s", name);
240         eth_dev->data->port_id = port_id;
241         eth_dev->data->mtu = ETHER_MTU;
242
243         return eth_dev;
244 }
245
246 /*
247  * Attach to a port already registered by the primary process, which
248  * makes sure that the same device would have the same port id both
249  * in the primary and secondary process.
250  */
251 struct rte_eth_dev *
252 rte_eth_dev_attach_secondary(const char *name)
253 {
254         uint8_t i;
255         struct rte_eth_dev *eth_dev;
256
257         if (rte_eth_dev_data == NULL)
258                 rte_eth_dev_data_alloc();
259
260         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
261                 if (strcmp(rte_eth_dev_data[i].name, name) == 0)
262                         break;
263         }
264         if (i == RTE_MAX_ETHPORTS) {
265                 RTE_PMD_DEBUG_TRACE(
266                         "device %s is not driven by the primary process\n",
267                         name);
268                 return NULL;
269         }
270
271         eth_dev = eth_dev_get(i);
272         RTE_ASSERT(eth_dev->data->port_id == i);
273
274         return eth_dev;
275 }
276
277 int
278 rte_eth_dev_release_port(struct rte_eth_dev *eth_dev)
279 {
280         if (eth_dev == NULL)
281                 return -EINVAL;
282
283         eth_dev->state = RTE_ETH_DEV_UNUSED;
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              rte_eth_devices[port_id].state != RTE_ETH_DEV_DEFERRED))
293                 return 0;
294         else
295                 return 1;
296 }
297
298 int
299 rte_eth_dev_socket_id(uint8_t port_id)
300 {
301         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -1);
302         return rte_eth_devices[port_id].data->numa_node;
303 }
304
305 uint8_t
306 rte_eth_dev_count(void)
307 {
308         uint8_t p;
309         uint8_t count;
310
311         count = 0;
312
313         RTE_ETH_FOREACH_DEV(p)
314                 count++;
315
316         return count;
317 }
318
319 int
320 rte_eth_dev_get_name_by_port(uint8_t port_id, char *name)
321 {
322         const char *tmp;
323
324         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
325
326         if (name == NULL) {
327                 RTE_PMD_DEBUG_TRACE("Null pointer is specified\n");
328                 return -EINVAL;
329         }
330
331         /* shouldn't check 'rte_eth_devices[i].data',
332          * because it might be overwritten by VDEV PMD */
333         tmp = rte_eth_devices[port_id].device->name;
334         strcpy(name, tmp);
335         return 0;
336 }
337
338 int
339 rte_eth_dev_get_port_by_name(const char *name, uint8_t *port_id)
340 {
341         int ret;
342         int i;
343
344         if (name == NULL) {
345                 RTE_PMD_DEBUG_TRACE("Null pointer is specified\n");
346                 return -EINVAL;
347         }
348
349         RTE_ETH_FOREACH_DEV(i) {
350                 if (!rte_eth_devices[i].device)
351                         continue;
352
353                 ret = strncmp(name, rte_eth_devices[i].device->name,
354                                 strlen(name));
355                 if (ret == 0) {
356                         *port_id = i;
357                         return 0;
358                 }
359         }
360         return -ENODEV;
361 }
362
363 static int
364 rte_eth_dev_is_detachable(uint8_t port_id)
365 {
366         uint32_t dev_flags;
367
368         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
369
370         dev_flags = rte_eth_devices[port_id].data->dev_flags;
371         if ((dev_flags & RTE_ETH_DEV_DETACHABLE) &&
372                 (!(dev_flags & RTE_ETH_DEV_BONDED_SLAVE)))
373                 return 0;
374         else
375                 return 1;
376 }
377
378 /* attach the new device, then store port_id of the device */
379 int
380 rte_eth_dev_attach(const char *devargs, uint8_t *port_id)
381 {
382         int ret = -1;
383         int current = rte_eth_dev_count();
384         char *name = NULL;
385         char *args = NULL;
386
387         if ((devargs == NULL) || (port_id == NULL)) {
388                 ret = -EINVAL;
389                 goto err;
390         }
391
392         /* parse devargs, then retrieve device name and args */
393         if (rte_eal_parse_devargs_str(devargs, &name, &args))
394                 goto err;
395
396         ret = rte_eal_dev_attach(name, args);
397         if (ret < 0)
398                 goto err;
399
400         /* no point looking at the port count if no port exists */
401         if (!rte_eth_dev_count()) {
402                 RTE_LOG(ERR, EAL, "No port found for device (%s)\n", name);
403                 ret = -1;
404                 goto err;
405         }
406
407         /* if nothing happened, there is a bug here, since some driver told us
408          * it did attach a device, but did not create a port.
409          */
410         if (current == rte_eth_dev_count()) {
411                 ret = -1;
412                 goto err;
413         }
414
415         *port_id = eth_dev_last_created_port;
416         ret = 0;
417
418 err:
419         free(name);
420         free(args);
421         return ret;
422 }
423
424 /* detach the device, then store the name of the device */
425 int
426 rte_eth_dev_detach(uint8_t port_id, char *name)
427 {
428         int ret = -1;
429
430         if (name == NULL) {
431                 ret = -EINVAL;
432                 goto err;
433         }
434
435         /* FIXME: move this to eal, once device flags are relocated there */
436         if (rte_eth_dev_is_detachable(port_id))
437                 goto err;
438
439         snprintf(name, RTE_DEV_NAME_MAX_LEN, "%s",
440                  rte_eth_devices[port_id].device->name);
441
442         ret = rte_eal_dev_detach(rte_eth_devices[port_id].device);
443         if (ret < 0)
444                 goto err;
445
446         rte_eth_devices[port_id].state = RTE_ETH_DEV_UNUSED;
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->device->driver->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->device->driver->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         dev->data->nb_rx_queues = 0;
990         rte_free(dev->data->rx_queues);
991         dev->data->rx_queues = NULL;
992         dev->data->nb_tx_queues = 0;
993         rte_free(dev->data->tx_queues);
994         dev->data->tx_queues = NULL;
995 }
996
997 int
998 rte_eth_dev_reset(uint8_t port_id)
999 {
1000         struct rte_eth_dev *dev;
1001         int ret;
1002
1003         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1004         dev = &rte_eth_devices[port_id];
1005
1006         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_reset, -ENOTSUP);
1007
1008         rte_eth_dev_stop(port_id);
1009         ret = dev->dev_ops->dev_reset(dev);
1010
1011         return ret;
1012 }
1013
1014 int
1015 rte_eth_rx_queue_setup(uint8_t port_id, uint16_t rx_queue_id,
1016                        uint16_t nb_rx_desc, unsigned int socket_id,
1017                        const struct rte_eth_rxconf *rx_conf,
1018                        struct rte_mempool *mp)
1019 {
1020         int ret;
1021         uint32_t mbp_buf_size;
1022         struct rte_eth_dev *dev;
1023         struct rte_eth_dev_info dev_info;
1024         void **rxq;
1025
1026         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1027
1028         dev = &rte_eth_devices[port_id];
1029         if (rx_queue_id >= dev->data->nb_rx_queues) {
1030                 RTE_PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", rx_queue_id);
1031                 return -EINVAL;
1032         }
1033
1034         if (dev->data->dev_started) {
1035                 RTE_PMD_DEBUG_TRACE(
1036                     "port %d must be stopped to allow configuration\n", port_id);
1037                 return -EBUSY;
1038         }
1039
1040         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
1041         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_setup, -ENOTSUP);
1042
1043         /*
1044          * Check the size of the mbuf data buffer.
1045          * This value must be provided in the private data of the memory pool.
1046          * First check that the memory pool has a valid private data.
1047          */
1048         rte_eth_dev_info_get(port_id, &dev_info);
1049         if (mp->private_data_size < sizeof(struct rte_pktmbuf_pool_private)) {
1050                 RTE_PMD_DEBUG_TRACE("%s private_data_size %d < %d\n",
1051                                 mp->name, (int) mp->private_data_size,
1052                                 (int) sizeof(struct rte_pktmbuf_pool_private));
1053                 return -ENOSPC;
1054         }
1055         mbp_buf_size = rte_pktmbuf_data_room_size(mp);
1056
1057         if ((mbp_buf_size - RTE_PKTMBUF_HEADROOM) < dev_info.min_rx_bufsize) {
1058                 RTE_PMD_DEBUG_TRACE("%s mbuf_data_room_size %d < %d "
1059                                 "(RTE_PKTMBUF_HEADROOM=%d + min_rx_bufsize(dev)"
1060                                 "=%d)\n",
1061                                 mp->name,
1062                                 (int)mbp_buf_size,
1063                                 (int)(RTE_PKTMBUF_HEADROOM +
1064                                       dev_info.min_rx_bufsize),
1065                                 (int)RTE_PKTMBUF_HEADROOM,
1066                                 (int)dev_info.min_rx_bufsize);
1067                 return -EINVAL;
1068         }
1069
1070         if (nb_rx_desc > dev_info.rx_desc_lim.nb_max ||
1071                         nb_rx_desc < dev_info.rx_desc_lim.nb_min ||
1072                         nb_rx_desc % dev_info.rx_desc_lim.nb_align != 0) {
1073
1074                 RTE_PMD_DEBUG_TRACE("Invalid value for nb_rx_desc(=%hu), "
1075                         "should be: <= %hu, = %hu, and a product of %hu\n",
1076                         nb_rx_desc,
1077                         dev_info.rx_desc_lim.nb_max,
1078                         dev_info.rx_desc_lim.nb_min,
1079                         dev_info.rx_desc_lim.nb_align);
1080                 return -EINVAL;
1081         }
1082
1083         rxq = dev->data->rx_queues;
1084         if (rxq[rx_queue_id]) {
1085                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_release,
1086                                         -ENOTSUP);
1087                 (*dev->dev_ops->rx_queue_release)(rxq[rx_queue_id]);
1088                 rxq[rx_queue_id] = NULL;
1089         }
1090
1091         if (rx_conf == NULL)
1092                 rx_conf = &dev_info.default_rxconf;
1093
1094         ret = (*dev->dev_ops->rx_queue_setup)(dev, rx_queue_id, nb_rx_desc,
1095                                               socket_id, rx_conf, mp);
1096         if (!ret) {
1097                 if (!dev->data->min_rx_buf_size ||
1098                     dev->data->min_rx_buf_size > mbp_buf_size)
1099                         dev->data->min_rx_buf_size = mbp_buf_size;
1100         }
1101
1102         return ret;
1103 }
1104
1105 int
1106 rte_eth_tx_queue_setup(uint8_t port_id, uint16_t tx_queue_id,
1107                        uint16_t nb_tx_desc, unsigned int socket_id,
1108                        const struct rte_eth_txconf *tx_conf)
1109 {
1110         struct rte_eth_dev *dev;
1111         struct rte_eth_dev_info dev_info;
1112         void **txq;
1113
1114         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1115
1116         dev = &rte_eth_devices[port_id];
1117         if (tx_queue_id >= dev->data->nb_tx_queues) {
1118                 RTE_PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", tx_queue_id);
1119                 return -EINVAL;
1120         }
1121
1122         if (dev->data->dev_started) {
1123                 RTE_PMD_DEBUG_TRACE(
1124                     "port %d must be stopped to allow configuration\n", port_id);
1125                 return -EBUSY;
1126         }
1127
1128         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
1129         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_setup, -ENOTSUP);
1130
1131         rte_eth_dev_info_get(port_id, &dev_info);
1132
1133         if (nb_tx_desc > dev_info.tx_desc_lim.nb_max ||
1134             nb_tx_desc < dev_info.tx_desc_lim.nb_min ||
1135             nb_tx_desc % dev_info.tx_desc_lim.nb_align != 0) {
1136                 RTE_PMD_DEBUG_TRACE("Invalid value for nb_tx_desc(=%hu), "
1137                                 "should be: <= %hu, = %hu, and a product of %hu\n",
1138                                 nb_tx_desc,
1139                                 dev_info.tx_desc_lim.nb_max,
1140                                 dev_info.tx_desc_lim.nb_min,
1141                                 dev_info.tx_desc_lim.nb_align);
1142                 return -EINVAL;
1143         }
1144
1145         txq = dev->data->tx_queues;
1146         if (txq[tx_queue_id]) {
1147                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_release,
1148                                         -ENOTSUP);
1149                 (*dev->dev_ops->tx_queue_release)(txq[tx_queue_id]);
1150                 txq[tx_queue_id] = NULL;
1151         }
1152
1153         if (tx_conf == NULL)
1154                 tx_conf = &dev_info.default_txconf;
1155
1156         return (*dev->dev_ops->tx_queue_setup)(dev, tx_queue_id, nb_tx_desc,
1157                                                socket_id, tx_conf);
1158 }
1159
1160 void
1161 rte_eth_tx_buffer_drop_callback(struct rte_mbuf **pkts, uint16_t unsent,
1162                 void *userdata __rte_unused)
1163 {
1164         unsigned i;
1165
1166         for (i = 0; i < unsent; i++)
1167                 rte_pktmbuf_free(pkts[i]);
1168 }
1169
1170 void
1171 rte_eth_tx_buffer_count_callback(struct rte_mbuf **pkts, uint16_t unsent,
1172                 void *userdata)
1173 {
1174         uint64_t *count = userdata;
1175         unsigned i;
1176
1177         for (i = 0; i < unsent; i++)
1178                 rte_pktmbuf_free(pkts[i]);
1179
1180         *count += unsent;
1181 }
1182
1183 int
1184 rte_eth_tx_buffer_set_err_callback(struct rte_eth_dev_tx_buffer *buffer,
1185                 buffer_tx_error_fn cbfn, void *userdata)
1186 {
1187         buffer->error_callback = cbfn;
1188         buffer->error_userdata = userdata;
1189         return 0;
1190 }
1191
1192 int
1193 rte_eth_tx_buffer_init(struct rte_eth_dev_tx_buffer *buffer, uint16_t size)
1194 {
1195         int ret = 0;
1196
1197         if (buffer == NULL)
1198                 return -EINVAL;
1199
1200         buffer->size = size;
1201         if (buffer->error_callback == NULL) {
1202                 ret = rte_eth_tx_buffer_set_err_callback(
1203                         buffer, rte_eth_tx_buffer_drop_callback, NULL);
1204         }
1205
1206         return ret;
1207 }
1208
1209 int
1210 rte_eth_tx_done_cleanup(uint8_t port_id, uint16_t queue_id, uint32_t free_cnt)
1211 {
1212         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1213
1214         /* Validate Input Data. Bail if not valid or not supported. */
1215         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1216         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_done_cleanup, -ENOTSUP);
1217
1218         /* Call driver to free pending mbufs. */
1219         return (*dev->dev_ops->tx_done_cleanup)(dev->data->tx_queues[queue_id],
1220                         free_cnt);
1221 }
1222
1223 void
1224 rte_eth_promiscuous_enable(uint8_t port_id)
1225 {
1226         struct rte_eth_dev *dev;
1227
1228         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1229         dev = &rte_eth_devices[port_id];
1230
1231         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->promiscuous_enable);
1232         (*dev->dev_ops->promiscuous_enable)(dev);
1233         dev->data->promiscuous = 1;
1234 }
1235
1236 void
1237 rte_eth_promiscuous_disable(uint8_t port_id)
1238 {
1239         struct rte_eth_dev *dev;
1240
1241         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1242         dev = &rte_eth_devices[port_id];
1243
1244         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->promiscuous_disable);
1245         dev->data->promiscuous = 0;
1246         (*dev->dev_ops->promiscuous_disable)(dev);
1247 }
1248
1249 int
1250 rte_eth_promiscuous_get(uint8_t port_id)
1251 {
1252         struct rte_eth_dev *dev;
1253
1254         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1255
1256         dev = &rte_eth_devices[port_id];
1257         return dev->data->promiscuous;
1258 }
1259
1260 void
1261 rte_eth_allmulticast_enable(uint8_t port_id)
1262 {
1263         struct rte_eth_dev *dev;
1264
1265         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1266         dev = &rte_eth_devices[port_id];
1267
1268         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_enable);
1269         (*dev->dev_ops->allmulticast_enable)(dev);
1270         dev->data->all_multicast = 1;
1271 }
1272
1273 void
1274 rte_eth_allmulticast_disable(uint8_t port_id)
1275 {
1276         struct rte_eth_dev *dev;
1277
1278         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1279         dev = &rte_eth_devices[port_id];
1280
1281         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_disable);
1282         dev->data->all_multicast = 0;
1283         (*dev->dev_ops->allmulticast_disable)(dev);
1284 }
1285
1286 int
1287 rte_eth_allmulticast_get(uint8_t port_id)
1288 {
1289         struct rte_eth_dev *dev;
1290
1291         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1292
1293         dev = &rte_eth_devices[port_id];
1294         return dev->data->all_multicast;
1295 }
1296
1297 static inline int
1298 rte_eth_dev_atomic_read_link_status(struct rte_eth_dev *dev,
1299                                 struct rte_eth_link *link)
1300 {
1301         struct rte_eth_link *dst = link;
1302         struct rte_eth_link *src = &(dev->data->dev_link);
1303
1304         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
1305                                         *(uint64_t *)src) == 0)
1306                 return -1;
1307
1308         return 0;
1309 }
1310
1311 void
1312 rte_eth_link_get(uint8_t port_id, struct rte_eth_link *eth_link)
1313 {
1314         struct rte_eth_dev *dev;
1315
1316         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1317         dev = &rte_eth_devices[port_id];
1318
1319         if (dev->data->dev_conf.intr_conf.lsc != 0)
1320                 rte_eth_dev_atomic_read_link_status(dev, eth_link);
1321         else {
1322                 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
1323                 (*dev->dev_ops->link_update)(dev, 1);
1324                 *eth_link = dev->data->dev_link;
1325         }
1326 }
1327
1328 void
1329 rte_eth_link_get_nowait(uint8_t port_id, struct rte_eth_link *eth_link)
1330 {
1331         struct rte_eth_dev *dev;
1332
1333         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1334         dev = &rte_eth_devices[port_id];
1335
1336         if (dev->data->dev_conf.intr_conf.lsc != 0)
1337                 rte_eth_dev_atomic_read_link_status(dev, eth_link);
1338         else {
1339                 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
1340                 (*dev->dev_ops->link_update)(dev, 0);
1341                 *eth_link = dev->data->dev_link;
1342         }
1343 }
1344
1345 int
1346 rte_eth_stats_get(uint8_t port_id, struct rte_eth_stats *stats)
1347 {
1348         struct rte_eth_dev *dev;
1349
1350         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1351
1352         dev = &rte_eth_devices[port_id];
1353         memset(stats, 0, sizeof(*stats));
1354
1355         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
1356         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
1357         (*dev->dev_ops->stats_get)(dev, stats);
1358         return 0;
1359 }
1360
1361 void
1362 rte_eth_stats_reset(uint8_t port_id)
1363 {
1364         struct rte_eth_dev *dev;
1365
1366         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1367         dev = &rte_eth_devices[port_id];
1368
1369         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
1370         (*dev->dev_ops->stats_reset)(dev);
1371         dev->data->rx_mbuf_alloc_failed = 0;
1372 }
1373
1374 static int
1375 get_xstats_count(uint8_t port_id)
1376 {
1377         struct rte_eth_dev *dev;
1378         int count;
1379
1380         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1381         dev = &rte_eth_devices[port_id];
1382         if (dev->dev_ops->xstats_get_names_by_id != NULL) {
1383                 count = (*dev->dev_ops->xstats_get_names_by_id)(dev, NULL,
1384                                 NULL, 0);
1385                 if (count < 0)
1386                         return count;
1387         }
1388         if (dev->dev_ops->xstats_get_names != NULL) {
1389                 count = (*dev->dev_ops->xstats_get_names)(dev, NULL, 0);
1390                 if (count < 0)
1391                         return count;
1392         } else
1393                 count = 0;
1394
1395         count += RTE_NB_STATS;
1396         count += RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS) *
1397                  RTE_NB_RXQ_STATS;
1398         count += RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS) *
1399                  RTE_NB_TXQ_STATS;
1400         return count;
1401 }
1402
1403 int
1404 rte_eth_xstats_get_id_by_name(uint8_t port_id, const char *xstat_name,
1405                 uint64_t *id)
1406 {
1407         int cnt_xstats, idx_xstat;
1408
1409         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1410
1411         if (!id) {
1412                 RTE_PMD_DEBUG_TRACE("Error: id pointer is NULL\n");
1413                 return -ENOMEM;
1414         }
1415
1416         if (!xstat_name) {
1417                 RTE_PMD_DEBUG_TRACE("Error: xstat_name pointer is NULL\n");
1418                 return -ENOMEM;
1419         }
1420
1421         /* Get count */
1422         cnt_xstats = rte_eth_xstats_get_names_by_id(port_id, NULL, 0, NULL);
1423         if (cnt_xstats  < 0) {
1424                 RTE_PMD_DEBUG_TRACE("Error: Cannot get count of xstats\n");
1425                 return -ENODEV;
1426         }
1427
1428         /* Get id-name lookup table */
1429         struct rte_eth_xstat_name xstats_names[cnt_xstats];
1430
1431         if (cnt_xstats != rte_eth_xstats_get_names_by_id(
1432                         port_id, xstats_names, cnt_xstats, NULL)) {
1433                 RTE_PMD_DEBUG_TRACE("Error: Cannot get xstats lookup\n");
1434                 return -1;
1435         }
1436
1437         for (idx_xstat = 0; idx_xstat < cnt_xstats; idx_xstat++) {
1438                 if (!strcmp(xstats_names[idx_xstat].name, xstat_name)) {
1439                         *id = idx_xstat;
1440                         return 0;
1441                 };
1442         }
1443
1444         return -EINVAL;
1445 }
1446
1447 int
1448 rte_eth_xstats_get_names_by_id(uint8_t port_id,
1449         struct rte_eth_xstat_name *xstats_names, unsigned int size,
1450         uint64_t *ids)
1451 {
1452         /* Get all xstats */
1453         if (!ids) {
1454                 struct rte_eth_dev *dev;
1455                 int cnt_used_entries;
1456                 int cnt_expected_entries;
1457                 int cnt_driver_entries;
1458                 uint32_t idx, id_queue;
1459                 uint16_t num_q;
1460
1461                 cnt_expected_entries = get_xstats_count(port_id);
1462                 if (xstats_names == NULL || cnt_expected_entries < 0 ||
1463                                 (int)size < cnt_expected_entries)
1464                         return cnt_expected_entries;
1465
1466                 /* port_id checked in get_xstats_count() */
1467                 dev = &rte_eth_devices[port_id];
1468                 cnt_used_entries = 0;
1469
1470                 for (idx = 0; idx < RTE_NB_STATS; idx++) {
1471                         snprintf(xstats_names[cnt_used_entries].name,
1472                                 sizeof(xstats_names[0].name),
1473                                 "%s", rte_stats_strings[idx].name);
1474                         cnt_used_entries++;
1475                 }
1476                 num_q = RTE_MIN(dev->data->nb_rx_queues,
1477                                 RTE_ETHDEV_QUEUE_STAT_CNTRS);
1478                 for (id_queue = 0; id_queue < num_q; id_queue++) {
1479                         for (idx = 0; idx < RTE_NB_RXQ_STATS; idx++) {
1480                                 snprintf(xstats_names[cnt_used_entries].name,
1481                                         sizeof(xstats_names[0].name),
1482                                         "rx_q%u%s",
1483                                         id_queue,
1484                                         rte_rxq_stats_strings[idx].name);
1485                                 cnt_used_entries++;
1486                         }
1487
1488                 }
1489                 num_q = RTE_MIN(dev->data->nb_tx_queues,
1490                                 RTE_ETHDEV_QUEUE_STAT_CNTRS);
1491                 for (id_queue = 0; id_queue < num_q; id_queue++) {
1492                         for (idx = 0; idx < RTE_NB_TXQ_STATS; idx++) {
1493                                 snprintf(xstats_names[cnt_used_entries].name,
1494                                         sizeof(xstats_names[0].name),
1495                                         "tx_q%u%s",
1496                                         id_queue,
1497                                         rte_txq_stats_strings[idx].name);
1498                                 cnt_used_entries++;
1499                         }
1500                 }
1501
1502                 if (dev->dev_ops->xstats_get_names_by_id != NULL) {
1503                         /* If there are any driver-specific xstats, append them
1504                          * to end of list.
1505                          */
1506                         cnt_driver_entries =
1507                                 (*dev->dev_ops->xstats_get_names_by_id)(
1508                                 dev,
1509                                 xstats_names + cnt_used_entries,
1510                                 NULL,
1511                                 size - cnt_used_entries);
1512                         if (cnt_driver_entries < 0)
1513                                 return cnt_driver_entries;
1514                         cnt_used_entries += cnt_driver_entries;
1515
1516                 } else if (dev->dev_ops->xstats_get_names != NULL) {
1517                         /* If there are any driver-specific xstats, append them
1518                          * to end of list.
1519                          */
1520                         cnt_driver_entries = (*dev->dev_ops->xstats_get_names)(
1521                                 dev,
1522                                 xstats_names + cnt_used_entries,
1523                                 size - cnt_used_entries);
1524                         if (cnt_driver_entries < 0)
1525                                 return cnt_driver_entries;
1526                         cnt_used_entries += cnt_driver_entries;
1527                 }
1528
1529                 return cnt_used_entries;
1530         }
1531         /* Get only xstats given by IDS */
1532         else {
1533                 uint16_t len, i;
1534                 struct rte_eth_xstat_name *xstats_names_copy;
1535
1536                 len = rte_eth_xstats_get_names_by_id(port_id, NULL, 0, NULL);
1537
1538                 xstats_names_copy =
1539                                 malloc(sizeof(struct rte_eth_xstat_name) * len);
1540                 if (!xstats_names_copy) {
1541                         RTE_PMD_DEBUG_TRACE(
1542                              "ERROR: can't allocate memory for values_copy\n");
1543                         free(xstats_names_copy);
1544                         return -1;
1545                 }
1546
1547                 rte_eth_xstats_get_names_by_id(port_id, xstats_names_copy,
1548                                 len, NULL);
1549
1550                 for (i = 0; i < size; i++) {
1551                         if (ids[i] >= len) {
1552                                 RTE_PMD_DEBUG_TRACE(
1553                                         "ERROR: id value isn't valid\n");
1554                                 return -1;
1555                         }
1556                         strcpy(xstats_names[i].name,
1557                                         xstats_names_copy[ids[i]].name);
1558                 }
1559                 free(xstats_names_copy);
1560                 return size;
1561         }
1562 }
1563
1564 int
1565 rte_eth_xstats_get_names(uint8_t port_id,
1566         struct rte_eth_xstat_name *xstats_names,
1567         unsigned int size)
1568 {
1569         struct rte_eth_dev *dev;
1570         int cnt_used_entries;
1571         int cnt_expected_entries;
1572         int cnt_driver_entries;
1573         uint32_t idx, id_queue;
1574         uint16_t num_q;
1575
1576         cnt_expected_entries = get_xstats_count(port_id);
1577         if (xstats_names == NULL || cnt_expected_entries < 0 ||
1578                         (int)size < cnt_expected_entries)
1579                 return cnt_expected_entries;
1580
1581         /* port_id checked in get_xstats_count() */
1582         dev = &rte_eth_devices[port_id];
1583         cnt_used_entries = 0;
1584
1585         for (idx = 0; idx < RTE_NB_STATS; idx++) {
1586                 snprintf(xstats_names[cnt_used_entries].name,
1587                         sizeof(xstats_names[0].name),
1588                         "%s", rte_stats_strings[idx].name);
1589                 cnt_used_entries++;
1590         }
1591         num_q = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
1592         for (id_queue = 0; id_queue < num_q; id_queue++) {
1593                 for (idx = 0; idx < RTE_NB_RXQ_STATS; idx++) {
1594                         snprintf(xstats_names[cnt_used_entries].name,
1595                                 sizeof(xstats_names[0].name),
1596                                 "rx_q%u%s",
1597                                 id_queue, rte_rxq_stats_strings[idx].name);
1598                         cnt_used_entries++;
1599                 }
1600
1601         }
1602         num_q = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
1603         for (id_queue = 0; id_queue < num_q; id_queue++) {
1604                 for (idx = 0; idx < RTE_NB_TXQ_STATS; idx++) {
1605                         snprintf(xstats_names[cnt_used_entries].name,
1606                                 sizeof(xstats_names[0].name),
1607                                 "tx_q%u%s",
1608                                 id_queue, rte_txq_stats_strings[idx].name);
1609                         cnt_used_entries++;
1610                 }
1611         }
1612
1613         if (dev->dev_ops->xstats_get_names != NULL) {
1614                 /* If there are any driver-specific xstats, append them
1615                  * to end of list.
1616                  */
1617                 cnt_driver_entries = (*dev->dev_ops->xstats_get_names)(
1618                         dev,
1619                         xstats_names + cnt_used_entries,
1620                         size - cnt_used_entries);
1621                 if (cnt_driver_entries < 0)
1622                         return cnt_driver_entries;
1623                 cnt_used_entries += cnt_driver_entries;
1624         }
1625
1626         return cnt_used_entries;
1627 }
1628
1629 /* retrieve ethdev extended statistics */
1630 int
1631 rte_eth_xstats_get_by_id(uint8_t port_id, const uint64_t *ids, uint64_t *values,
1632         unsigned int n)
1633 {
1634         /* If need all xstats */
1635         if (!ids) {
1636                 struct rte_eth_stats eth_stats;
1637                 struct rte_eth_dev *dev;
1638                 unsigned int count = 0, i, q;
1639                 signed int xcount = 0;
1640                 uint64_t val, *stats_ptr;
1641                 uint16_t nb_rxqs, nb_txqs;
1642
1643                 RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1644                 dev = &rte_eth_devices[port_id];
1645
1646                 nb_rxqs = RTE_MIN(dev->data->nb_rx_queues,
1647                                 RTE_ETHDEV_QUEUE_STAT_CNTRS);
1648                 nb_txqs = RTE_MIN(dev->data->nb_tx_queues,
1649                                 RTE_ETHDEV_QUEUE_STAT_CNTRS);
1650
1651                 /* Return generic statistics */
1652                 count = RTE_NB_STATS + (nb_rxqs * RTE_NB_RXQ_STATS) +
1653                         (nb_txqs * RTE_NB_TXQ_STATS);
1654
1655
1656                 /* implemented by the driver */
1657                 if (dev->dev_ops->xstats_get_by_id != NULL) {
1658                         /* Retrieve the xstats from the driver at the end of the
1659                          * xstats struct. Retrieve all xstats.
1660                          */
1661                         xcount = (*dev->dev_ops->xstats_get_by_id)(dev,
1662                                         NULL,
1663                                         values ? values + count : NULL,
1664                                         (n > count) ? n - count : 0);
1665
1666                         if (xcount < 0)
1667                                 return xcount;
1668                 /* implemented by the driver */
1669                 } else if (dev->dev_ops->xstats_get != NULL) {
1670                         /* Retrieve the xstats from the driver at the end of the
1671                          * xstats struct. Retrieve all xstats.
1672                          * Compatibility for PMD without xstats_get_by_ids
1673                          */
1674                         unsigned int size = (n > count) ? n - count : 1;
1675                         struct rte_eth_xstat xstats[size];
1676
1677                         xcount = (*dev->dev_ops->xstats_get)(dev,
1678                                         values ? xstats : NULL, size);
1679
1680                         if (xcount < 0)
1681                                 return xcount;
1682
1683                         if (values != NULL)
1684                                 for (i = 0 ; i < (unsigned int)xcount; i++)
1685                                         values[i + count] = xstats[i].value;
1686                 }
1687
1688                 if (n < count + xcount || values == NULL)
1689                         return count + xcount;
1690
1691                 /* now fill the xstats structure */
1692                 count = 0;
1693                 rte_eth_stats_get(port_id, &eth_stats);
1694
1695                 /* global stats */
1696                 for (i = 0; i < RTE_NB_STATS; i++) {
1697                         stats_ptr = RTE_PTR_ADD(&eth_stats,
1698                                                 rte_stats_strings[i].offset);
1699                         val = *stats_ptr;
1700                         values[count++] = val;
1701                 }
1702
1703                 /* per-rxq stats */
1704                 for (q = 0; q < nb_rxqs; q++) {
1705                         for (i = 0; i < RTE_NB_RXQ_STATS; i++) {
1706                                 stats_ptr = RTE_PTR_ADD(&eth_stats,
1707                                             rte_rxq_stats_strings[i].offset +
1708                                             q * sizeof(uint64_t));
1709                                 val = *stats_ptr;
1710                                 values[count++] = val;
1711                         }
1712                 }
1713
1714                 /* per-txq stats */
1715                 for (q = 0; q < nb_txqs; q++) {
1716                         for (i = 0; i < RTE_NB_TXQ_STATS; i++) {
1717                                 stats_ptr = RTE_PTR_ADD(&eth_stats,
1718                                             rte_txq_stats_strings[i].offset +
1719                                             q * sizeof(uint64_t));
1720                                 val = *stats_ptr;
1721                                 values[count++] = val;
1722                         }
1723                 }
1724
1725                 return count + xcount;
1726         }
1727         /* Need only xstats given by IDS array */
1728         else {
1729                 uint16_t i, size;
1730                 uint64_t *values_copy;
1731
1732                 size = rte_eth_xstats_get_by_id(port_id, NULL, NULL, 0);
1733
1734                 values_copy = malloc(sizeof(*values_copy) * size);
1735                 if (!values_copy) {
1736                         RTE_PMD_DEBUG_TRACE(
1737                             "ERROR: can't allocate memory for values_copy\n");
1738                         return -1;
1739                 }
1740
1741                 rte_eth_xstats_get_by_id(port_id, NULL, values_copy, size);
1742
1743                 for (i = 0; i < n; i++) {
1744                         if (ids[i] >= size) {
1745                                 RTE_PMD_DEBUG_TRACE(
1746                                         "ERROR: id value isn't valid\n");
1747                                 return -1;
1748                         }
1749                         values[i] = values_copy[ids[i]];
1750                 }
1751                 free(values_copy);
1752                 return n;
1753         }
1754 }
1755
1756 int
1757 rte_eth_xstats_get(uint8_t port_id, struct rte_eth_xstat *xstats,
1758         unsigned int n)
1759 {
1760         struct rte_eth_stats eth_stats;
1761         struct rte_eth_dev *dev;
1762         unsigned int count = 0, i, q;
1763         signed int xcount = 0;
1764         uint64_t val, *stats_ptr;
1765         uint16_t nb_rxqs, nb_txqs;
1766
1767         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1768
1769         dev = &rte_eth_devices[port_id];
1770
1771         nb_rxqs = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
1772         nb_txqs = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
1773
1774         /* Return generic statistics */
1775         count = RTE_NB_STATS + (nb_rxqs * RTE_NB_RXQ_STATS) +
1776                 (nb_txqs * RTE_NB_TXQ_STATS);
1777
1778         /* implemented by the driver */
1779         if (dev->dev_ops->xstats_get != NULL) {
1780                 /* Retrieve the xstats from the driver at the end of the
1781                  * xstats struct.
1782                  */
1783                 xcount = (*dev->dev_ops->xstats_get)(dev,
1784                                      xstats ? xstats + count : NULL,
1785                                      (n > count) ? n - count : 0);
1786
1787                 if (xcount < 0)
1788                         return xcount;
1789         }
1790
1791         if (n < count + xcount || xstats == NULL)
1792                 return count + xcount;
1793
1794         /* now fill the xstats structure */
1795         count = 0;
1796         rte_eth_stats_get(port_id, &eth_stats);
1797
1798         /* global stats */
1799         for (i = 0; i < RTE_NB_STATS; i++) {
1800                 stats_ptr = RTE_PTR_ADD(&eth_stats,
1801                                         rte_stats_strings[i].offset);
1802                 val = *stats_ptr;
1803                 xstats[count++].value = val;
1804         }
1805
1806         /* per-rxq stats */
1807         for (q = 0; q < nb_rxqs; q++) {
1808                 for (i = 0; i < RTE_NB_RXQ_STATS; i++) {
1809                         stats_ptr = RTE_PTR_ADD(&eth_stats,
1810                                         rte_rxq_stats_strings[i].offset +
1811                                         q * sizeof(uint64_t));
1812                         val = *stats_ptr;
1813                         xstats[count++].value = val;
1814                 }
1815         }
1816
1817         /* per-txq stats */
1818         for (q = 0; q < nb_txqs; q++) {
1819                 for (i = 0; i < RTE_NB_TXQ_STATS; i++) {
1820                         stats_ptr = RTE_PTR_ADD(&eth_stats,
1821                                         rte_txq_stats_strings[i].offset +
1822                                         q * sizeof(uint64_t));
1823                         val = *stats_ptr;
1824                         xstats[count++].value = val;
1825                 }
1826         }
1827
1828         for (i = 0; i < count; i++)
1829                 xstats[i].id = i;
1830         /* add an offset to driver-specific stats */
1831         for ( ; i < count + xcount; i++)
1832                 xstats[i].id += count;
1833
1834         return count + xcount;
1835 }
1836
1837 /* reset ethdev extended statistics */
1838 void
1839 rte_eth_xstats_reset(uint8_t port_id)
1840 {
1841         struct rte_eth_dev *dev;
1842
1843         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1844         dev = &rte_eth_devices[port_id];
1845
1846         /* implemented by the driver */
1847         if (dev->dev_ops->xstats_reset != NULL) {
1848                 (*dev->dev_ops->xstats_reset)(dev);
1849                 return;
1850         }
1851
1852         /* fallback to default */
1853         rte_eth_stats_reset(port_id);
1854 }
1855
1856 static int
1857 set_queue_stats_mapping(uint8_t port_id, uint16_t queue_id, uint8_t stat_idx,
1858                 uint8_t is_rx)
1859 {
1860         struct rte_eth_dev *dev;
1861
1862         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1863
1864         dev = &rte_eth_devices[port_id];
1865
1866         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_stats_mapping_set, -ENOTSUP);
1867         return (*dev->dev_ops->queue_stats_mapping_set)
1868                         (dev, queue_id, stat_idx, is_rx);
1869 }
1870
1871
1872 int
1873 rte_eth_dev_set_tx_queue_stats_mapping(uint8_t port_id, uint16_t tx_queue_id,
1874                 uint8_t stat_idx)
1875 {
1876         return set_queue_stats_mapping(port_id, tx_queue_id, stat_idx,
1877                         STAT_QMAP_TX);
1878 }
1879
1880
1881 int
1882 rte_eth_dev_set_rx_queue_stats_mapping(uint8_t port_id, uint16_t rx_queue_id,
1883                 uint8_t stat_idx)
1884 {
1885         return set_queue_stats_mapping(port_id, rx_queue_id, stat_idx,
1886                         STAT_QMAP_RX);
1887 }
1888
1889 int
1890 rte_eth_dev_fw_version_get(uint8_t port_id, char *fw_version, size_t fw_size)
1891 {
1892         struct rte_eth_dev *dev;
1893
1894         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1895         dev = &rte_eth_devices[port_id];
1896
1897         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fw_version_get, -ENOTSUP);
1898         return (*dev->dev_ops->fw_version_get)(dev, fw_version, fw_size);
1899 }
1900
1901 void
1902 rte_eth_dev_info_get(uint8_t port_id, struct rte_eth_dev_info *dev_info)
1903 {
1904         struct rte_eth_dev *dev;
1905         const struct rte_eth_desc_lim lim = {
1906                 .nb_max = UINT16_MAX,
1907                 .nb_min = 0,
1908                 .nb_align = 1,
1909         };
1910
1911         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1912         dev = &rte_eth_devices[port_id];
1913
1914         memset(dev_info, 0, sizeof(struct rte_eth_dev_info));
1915         dev_info->rx_desc_lim = lim;
1916         dev_info->tx_desc_lim = lim;
1917
1918         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
1919         (*dev->dev_ops->dev_infos_get)(dev, dev_info);
1920         dev_info->driver_name = dev->device->driver->name;
1921         dev_info->nb_rx_queues = dev->data->nb_rx_queues;
1922         dev_info->nb_tx_queues = dev->data->nb_tx_queues;
1923 }
1924
1925 int
1926 rte_eth_dev_get_supported_ptypes(uint8_t port_id, uint32_t ptype_mask,
1927                                  uint32_t *ptypes, int num)
1928 {
1929         int i, j;
1930         struct rte_eth_dev *dev;
1931         const uint32_t *all_ptypes;
1932
1933         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1934         dev = &rte_eth_devices[port_id];
1935         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_supported_ptypes_get, 0);
1936         all_ptypes = (*dev->dev_ops->dev_supported_ptypes_get)(dev);
1937
1938         if (!all_ptypes)
1939                 return 0;
1940
1941         for (i = 0, j = 0; all_ptypes[i] != RTE_PTYPE_UNKNOWN; ++i)
1942                 if (all_ptypes[i] & ptype_mask) {
1943                         if (j < num)
1944                                 ptypes[j] = all_ptypes[i];
1945                         j++;
1946                 }
1947
1948         return j;
1949 }
1950
1951 void
1952 rte_eth_macaddr_get(uint8_t port_id, struct ether_addr *mac_addr)
1953 {
1954         struct rte_eth_dev *dev;
1955
1956         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1957         dev = &rte_eth_devices[port_id];
1958         ether_addr_copy(&dev->data->mac_addrs[0], mac_addr);
1959 }
1960
1961
1962 int
1963 rte_eth_dev_get_mtu(uint8_t port_id, uint16_t *mtu)
1964 {
1965         struct rte_eth_dev *dev;
1966
1967         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1968
1969         dev = &rte_eth_devices[port_id];
1970         *mtu = dev->data->mtu;
1971         return 0;
1972 }
1973
1974 int
1975 rte_eth_dev_set_mtu(uint8_t port_id, uint16_t mtu)
1976 {
1977         int ret;
1978         struct rte_eth_dev *dev;
1979
1980         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1981         dev = &rte_eth_devices[port_id];
1982         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mtu_set, -ENOTSUP);
1983
1984         ret = (*dev->dev_ops->mtu_set)(dev, mtu);
1985         if (!ret)
1986                 dev->data->mtu = mtu;
1987
1988         return ret;
1989 }
1990
1991 int
1992 rte_eth_dev_vlan_filter(uint8_t port_id, uint16_t vlan_id, int on)
1993 {
1994         struct rte_eth_dev *dev;
1995         int ret;
1996
1997         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1998         dev = &rte_eth_devices[port_id];
1999         if (!(dev->data->dev_conf.rxmode.hw_vlan_filter)) {
2000                 RTE_PMD_DEBUG_TRACE("port %d: vlan-filtering disabled\n", port_id);
2001                 return -ENOSYS;
2002         }
2003
2004         if (vlan_id > 4095) {
2005                 RTE_PMD_DEBUG_TRACE("(port_id=%d) invalid vlan_id=%u > 4095\n",
2006                                 port_id, (unsigned) vlan_id);
2007                 return -EINVAL;
2008         }
2009         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_filter_set, -ENOTSUP);
2010
2011         ret = (*dev->dev_ops->vlan_filter_set)(dev, vlan_id, on);
2012         if (ret == 0) {
2013                 struct rte_vlan_filter_conf *vfc;
2014                 int vidx;
2015                 int vbit;
2016
2017                 vfc = &dev->data->vlan_filter_conf;
2018                 vidx = vlan_id / 64;
2019                 vbit = vlan_id % 64;
2020
2021                 if (on)
2022                         vfc->ids[vidx] |= UINT64_C(1) << vbit;
2023                 else
2024                         vfc->ids[vidx] &= ~(UINT64_C(1) << vbit);
2025         }
2026
2027         return ret;
2028 }
2029
2030 int
2031 rte_eth_dev_set_vlan_strip_on_queue(uint8_t port_id, uint16_t rx_queue_id, int on)
2032 {
2033         struct rte_eth_dev *dev;
2034
2035         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2036         dev = &rte_eth_devices[port_id];
2037         if (rx_queue_id >= dev->data->nb_rx_queues) {
2038                 RTE_PMD_DEBUG_TRACE("Invalid rx_queue_id=%d\n", port_id);
2039                 return -EINVAL;
2040         }
2041
2042         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_strip_queue_set, -ENOTSUP);
2043         (*dev->dev_ops->vlan_strip_queue_set)(dev, rx_queue_id, on);
2044
2045         return 0;
2046 }
2047
2048 int
2049 rte_eth_dev_set_vlan_ether_type(uint8_t port_id,
2050                                 enum rte_vlan_type vlan_type,
2051                                 uint16_t tpid)
2052 {
2053         struct rte_eth_dev *dev;
2054
2055         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2056         dev = &rte_eth_devices[port_id];
2057         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_tpid_set, -ENOTSUP);
2058
2059         return (*dev->dev_ops->vlan_tpid_set)(dev, vlan_type, tpid);
2060 }
2061
2062 int
2063 rte_eth_dev_set_vlan_offload(uint8_t port_id, int offload_mask)
2064 {
2065         struct rte_eth_dev *dev;
2066         int ret = 0;
2067         int mask = 0;
2068         int cur, org = 0;
2069
2070         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2071         dev = &rte_eth_devices[port_id];
2072
2073         /*check which option changed by application*/
2074         cur = !!(offload_mask & ETH_VLAN_STRIP_OFFLOAD);
2075         org = !!(dev->data->dev_conf.rxmode.hw_vlan_strip);
2076         if (cur != org) {
2077                 dev->data->dev_conf.rxmode.hw_vlan_strip = (uint8_t)cur;
2078                 mask |= ETH_VLAN_STRIP_MASK;
2079         }
2080
2081         cur = !!(offload_mask & ETH_VLAN_FILTER_OFFLOAD);
2082         org = !!(dev->data->dev_conf.rxmode.hw_vlan_filter);
2083         if (cur != org) {
2084                 dev->data->dev_conf.rxmode.hw_vlan_filter = (uint8_t)cur;
2085                 mask |= ETH_VLAN_FILTER_MASK;
2086         }
2087
2088         cur = !!(offload_mask & ETH_VLAN_EXTEND_OFFLOAD);
2089         org = !!(dev->data->dev_conf.rxmode.hw_vlan_extend);
2090         if (cur != org) {
2091                 dev->data->dev_conf.rxmode.hw_vlan_extend = (uint8_t)cur;
2092                 mask |= ETH_VLAN_EXTEND_MASK;
2093         }
2094
2095         /*no change*/
2096         if (mask == 0)
2097                 return ret;
2098
2099         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_offload_set, -ENOTSUP);
2100         (*dev->dev_ops->vlan_offload_set)(dev, mask);
2101
2102         return ret;
2103 }
2104
2105 int
2106 rte_eth_dev_get_vlan_offload(uint8_t port_id)
2107 {
2108         struct rte_eth_dev *dev;
2109         int ret = 0;
2110
2111         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2112         dev = &rte_eth_devices[port_id];
2113
2114         if (dev->data->dev_conf.rxmode.hw_vlan_strip)
2115                 ret |= ETH_VLAN_STRIP_OFFLOAD;
2116
2117         if (dev->data->dev_conf.rxmode.hw_vlan_filter)
2118                 ret |= ETH_VLAN_FILTER_OFFLOAD;
2119
2120         if (dev->data->dev_conf.rxmode.hw_vlan_extend)
2121                 ret |= ETH_VLAN_EXTEND_OFFLOAD;
2122
2123         return ret;
2124 }
2125
2126 int
2127 rte_eth_dev_set_vlan_pvid(uint8_t port_id, uint16_t pvid, int on)
2128 {
2129         struct rte_eth_dev *dev;
2130
2131         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2132         dev = &rte_eth_devices[port_id];
2133         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_pvid_set, -ENOTSUP);
2134         (*dev->dev_ops->vlan_pvid_set)(dev, pvid, on);
2135
2136         return 0;
2137 }
2138
2139 int
2140 rte_eth_dev_flow_ctrl_get(uint8_t port_id, struct rte_eth_fc_conf *fc_conf)
2141 {
2142         struct rte_eth_dev *dev;
2143
2144         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2145         dev = &rte_eth_devices[port_id];
2146         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_get, -ENOTSUP);
2147         memset(fc_conf, 0, sizeof(*fc_conf));
2148         return (*dev->dev_ops->flow_ctrl_get)(dev, fc_conf);
2149 }
2150
2151 int
2152 rte_eth_dev_flow_ctrl_set(uint8_t port_id, struct rte_eth_fc_conf *fc_conf)
2153 {
2154         struct rte_eth_dev *dev;
2155
2156         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2157         if ((fc_conf->send_xon != 0) && (fc_conf->send_xon != 1)) {
2158                 RTE_PMD_DEBUG_TRACE("Invalid send_xon, only 0/1 allowed\n");
2159                 return -EINVAL;
2160         }
2161
2162         dev = &rte_eth_devices[port_id];
2163         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_set, -ENOTSUP);
2164         return (*dev->dev_ops->flow_ctrl_set)(dev, fc_conf);
2165 }
2166
2167 int
2168 rte_eth_dev_priority_flow_ctrl_set(uint8_t port_id, struct rte_eth_pfc_conf *pfc_conf)
2169 {
2170         struct rte_eth_dev *dev;
2171
2172         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2173         if (pfc_conf->priority > (ETH_DCB_NUM_USER_PRIORITIES - 1)) {
2174                 RTE_PMD_DEBUG_TRACE("Invalid priority, only 0-7 allowed\n");
2175                 return -EINVAL;
2176         }
2177
2178         dev = &rte_eth_devices[port_id];
2179         /* High water, low water validation are device specific */
2180         if  (*dev->dev_ops->priority_flow_ctrl_set)
2181                 return (*dev->dev_ops->priority_flow_ctrl_set)(dev, pfc_conf);
2182         return -ENOTSUP;
2183 }
2184
2185 static int
2186 rte_eth_check_reta_mask(struct rte_eth_rss_reta_entry64 *reta_conf,
2187                         uint16_t reta_size)
2188 {
2189         uint16_t i, num;
2190
2191         if (!reta_conf)
2192                 return -EINVAL;
2193
2194         num = (reta_size + RTE_RETA_GROUP_SIZE - 1) / RTE_RETA_GROUP_SIZE;
2195         for (i = 0; i < num; i++) {
2196                 if (reta_conf[i].mask)
2197                         return 0;
2198         }
2199
2200         return -EINVAL;
2201 }
2202
2203 static int
2204 rte_eth_check_reta_entry(struct rte_eth_rss_reta_entry64 *reta_conf,
2205                          uint16_t reta_size,
2206                          uint16_t max_rxq)
2207 {
2208         uint16_t i, idx, shift;
2209
2210         if (!reta_conf)
2211                 return -EINVAL;
2212
2213         if (max_rxq == 0) {
2214                 RTE_PMD_DEBUG_TRACE("No receive queue is available\n");
2215                 return -EINVAL;
2216         }
2217
2218         for (i = 0; i < reta_size; i++) {
2219                 idx = i / RTE_RETA_GROUP_SIZE;
2220                 shift = i % RTE_RETA_GROUP_SIZE;
2221                 if ((reta_conf[idx].mask & (1ULL << shift)) &&
2222                         (reta_conf[idx].reta[shift] >= max_rxq)) {
2223                         RTE_PMD_DEBUG_TRACE("reta_conf[%u]->reta[%u]: %u exceeds "
2224                                 "the maximum rxq index: %u\n", idx, shift,
2225                                 reta_conf[idx].reta[shift], max_rxq);
2226                         return -EINVAL;
2227                 }
2228         }
2229
2230         return 0;
2231 }
2232
2233 int
2234 rte_eth_dev_rss_reta_update(uint8_t port_id,
2235                             struct rte_eth_rss_reta_entry64 *reta_conf,
2236                             uint16_t reta_size)
2237 {
2238         struct rte_eth_dev *dev;
2239         int ret;
2240
2241         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2242         /* Check mask bits */
2243         ret = rte_eth_check_reta_mask(reta_conf, reta_size);
2244         if (ret < 0)
2245                 return ret;
2246
2247         dev = &rte_eth_devices[port_id];
2248
2249         /* Check entry value */
2250         ret = rte_eth_check_reta_entry(reta_conf, reta_size,
2251                                 dev->data->nb_rx_queues);
2252         if (ret < 0)
2253                 return ret;
2254
2255         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->reta_update, -ENOTSUP);
2256         return (*dev->dev_ops->reta_update)(dev, reta_conf, reta_size);
2257 }
2258
2259 int
2260 rte_eth_dev_rss_reta_query(uint8_t port_id,
2261                            struct rte_eth_rss_reta_entry64 *reta_conf,
2262                            uint16_t reta_size)
2263 {
2264         struct rte_eth_dev *dev;
2265         int ret;
2266
2267         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2268
2269         /* Check mask bits */
2270         ret = rte_eth_check_reta_mask(reta_conf, reta_size);
2271         if (ret < 0)
2272                 return ret;
2273
2274         dev = &rte_eth_devices[port_id];
2275         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->reta_query, -ENOTSUP);
2276         return (*dev->dev_ops->reta_query)(dev, reta_conf, reta_size);
2277 }
2278
2279 int
2280 rte_eth_dev_rss_hash_update(uint8_t port_id, struct rte_eth_rss_conf *rss_conf)
2281 {
2282         struct rte_eth_dev *dev;
2283         uint16_t rss_hash_protos;
2284
2285         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2286         rss_hash_protos = rss_conf->rss_hf;
2287         if ((rss_hash_protos != 0) &&
2288             ((rss_hash_protos & ETH_RSS_PROTO_MASK) == 0)) {
2289                 RTE_PMD_DEBUG_TRACE("Invalid rss_hash_protos=0x%x\n",
2290                                 rss_hash_protos);
2291                 return -EINVAL;
2292         }
2293         dev = &rte_eth_devices[port_id];
2294         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rss_hash_update, -ENOTSUP);
2295         return (*dev->dev_ops->rss_hash_update)(dev, rss_conf);
2296 }
2297
2298 int
2299 rte_eth_dev_rss_hash_conf_get(uint8_t port_id,
2300                               struct rte_eth_rss_conf *rss_conf)
2301 {
2302         struct rte_eth_dev *dev;
2303
2304         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2305         dev = &rte_eth_devices[port_id];
2306         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rss_hash_conf_get, -ENOTSUP);
2307         return (*dev->dev_ops->rss_hash_conf_get)(dev, rss_conf);
2308 }
2309
2310 int
2311 rte_eth_dev_udp_tunnel_port_add(uint8_t port_id,
2312                                 struct rte_eth_udp_tunnel *udp_tunnel)
2313 {
2314         struct rte_eth_dev *dev;
2315
2316         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2317         if (udp_tunnel == NULL) {
2318                 RTE_PMD_DEBUG_TRACE("Invalid udp_tunnel parameter\n");
2319                 return -EINVAL;
2320         }
2321
2322         if (udp_tunnel->prot_type >= RTE_TUNNEL_TYPE_MAX) {
2323                 RTE_PMD_DEBUG_TRACE("Invalid tunnel type\n");
2324                 return -EINVAL;
2325         }
2326
2327         dev = &rte_eth_devices[port_id];
2328         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->udp_tunnel_port_add, -ENOTSUP);
2329         return (*dev->dev_ops->udp_tunnel_port_add)(dev, udp_tunnel);
2330 }
2331
2332 int
2333 rte_eth_dev_udp_tunnel_port_delete(uint8_t port_id,
2334                                    struct rte_eth_udp_tunnel *udp_tunnel)
2335 {
2336         struct rte_eth_dev *dev;
2337
2338         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2339         dev = &rte_eth_devices[port_id];
2340
2341         if (udp_tunnel == NULL) {
2342                 RTE_PMD_DEBUG_TRACE("Invalid udp_tunnel parameter\n");
2343                 return -EINVAL;
2344         }
2345
2346         if (udp_tunnel->prot_type >= RTE_TUNNEL_TYPE_MAX) {
2347                 RTE_PMD_DEBUG_TRACE("Invalid tunnel type\n");
2348                 return -EINVAL;
2349         }
2350
2351         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->udp_tunnel_port_del, -ENOTSUP);
2352         return (*dev->dev_ops->udp_tunnel_port_del)(dev, udp_tunnel);
2353 }
2354
2355 int
2356 rte_eth_led_on(uint8_t port_id)
2357 {
2358         struct rte_eth_dev *dev;
2359
2360         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2361         dev = &rte_eth_devices[port_id];
2362         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_on, -ENOTSUP);
2363         return (*dev->dev_ops->dev_led_on)(dev);
2364 }
2365
2366 int
2367 rte_eth_led_off(uint8_t port_id)
2368 {
2369         struct rte_eth_dev *dev;
2370
2371         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2372         dev = &rte_eth_devices[port_id];
2373         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_off, -ENOTSUP);
2374         return (*dev->dev_ops->dev_led_off)(dev);
2375 }
2376
2377 /*
2378  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
2379  * an empty spot.
2380  */
2381 static int
2382 get_mac_addr_index(uint8_t port_id, const struct ether_addr *addr)
2383 {
2384         struct rte_eth_dev_info dev_info;
2385         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
2386         unsigned i;
2387
2388         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2389         rte_eth_dev_info_get(port_id, &dev_info);
2390
2391         for (i = 0; i < dev_info.max_mac_addrs; i++)
2392                 if (memcmp(addr, &dev->data->mac_addrs[i], ETHER_ADDR_LEN) == 0)
2393                         return i;
2394
2395         return -1;
2396 }
2397
2398 static const struct ether_addr null_mac_addr;
2399
2400 int
2401 rte_eth_dev_mac_addr_add(uint8_t port_id, struct ether_addr *addr,
2402                         uint32_t pool)
2403 {
2404         struct rte_eth_dev *dev;
2405         int index;
2406         uint64_t pool_mask;
2407         int ret;
2408
2409         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2410         dev = &rte_eth_devices[port_id];
2411         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_add, -ENOTSUP);
2412
2413         if (is_zero_ether_addr(addr)) {
2414                 RTE_PMD_DEBUG_TRACE("port %d: Cannot add NULL MAC address\n",
2415                         port_id);
2416                 return -EINVAL;
2417         }
2418         if (pool >= ETH_64_POOLS) {
2419                 RTE_PMD_DEBUG_TRACE("pool id must be 0-%d\n", ETH_64_POOLS - 1);
2420                 return -EINVAL;
2421         }
2422
2423         index = get_mac_addr_index(port_id, addr);
2424         if (index < 0) {
2425                 index = get_mac_addr_index(port_id, &null_mac_addr);
2426                 if (index < 0) {
2427                         RTE_PMD_DEBUG_TRACE("port %d: MAC address array full\n",
2428                                 port_id);
2429                         return -ENOSPC;
2430                 }
2431         } else {
2432                 pool_mask = dev->data->mac_pool_sel[index];
2433
2434                 /* Check if both MAC address and pool is already there, and do nothing */
2435                 if (pool_mask & (1ULL << pool))
2436                         return 0;
2437         }
2438
2439         /* Update NIC */
2440         ret = (*dev->dev_ops->mac_addr_add)(dev, addr, index, pool);
2441
2442         if (ret == 0) {
2443                 /* Update address in NIC data structure */
2444                 ether_addr_copy(addr, &dev->data->mac_addrs[index]);
2445
2446                 /* Update pool bitmap in NIC data structure */
2447                 dev->data->mac_pool_sel[index] |= (1ULL << pool);
2448         }
2449
2450         return ret;
2451 }
2452
2453 int
2454 rte_eth_dev_mac_addr_remove(uint8_t port_id, struct ether_addr *addr)
2455 {
2456         struct rte_eth_dev *dev;
2457         int index;
2458
2459         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2460         dev = &rte_eth_devices[port_id];
2461         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_remove, -ENOTSUP);
2462
2463         index = get_mac_addr_index(port_id, addr);
2464         if (index == 0) {
2465                 RTE_PMD_DEBUG_TRACE("port %d: Cannot remove default MAC address\n", port_id);
2466                 return -EADDRINUSE;
2467         } else if (index < 0)
2468                 return 0;  /* Do nothing if address wasn't found */
2469
2470         /* Update NIC */
2471         (*dev->dev_ops->mac_addr_remove)(dev, index);
2472
2473         /* Update address in NIC data structure */
2474         ether_addr_copy(&null_mac_addr, &dev->data->mac_addrs[index]);
2475
2476         /* reset pool bitmap */
2477         dev->data->mac_pool_sel[index] = 0;
2478
2479         return 0;
2480 }
2481
2482 int
2483 rte_eth_dev_default_mac_addr_set(uint8_t port_id, struct ether_addr *addr)
2484 {
2485         struct rte_eth_dev *dev;
2486
2487         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2488
2489         if (!is_valid_assigned_ether_addr(addr))
2490                 return -EINVAL;
2491
2492         dev = &rte_eth_devices[port_id];
2493         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_set, -ENOTSUP);
2494
2495         /* Update default address in NIC data structure */
2496         ether_addr_copy(addr, &dev->data->mac_addrs[0]);
2497
2498         (*dev->dev_ops->mac_addr_set)(dev, addr);
2499
2500         return 0;
2501 }
2502
2503
2504 /*
2505  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
2506  * an empty spot.
2507  */
2508 static int
2509 get_hash_mac_addr_index(uint8_t port_id, const struct ether_addr *addr)
2510 {
2511         struct rte_eth_dev_info dev_info;
2512         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
2513         unsigned i;
2514
2515         rte_eth_dev_info_get(port_id, &dev_info);
2516         if (!dev->data->hash_mac_addrs)
2517                 return -1;
2518
2519         for (i = 0; i < dev_info.max_hash_mac_addrs; i++)
2520                 if (memcmp(addr, &dev->data->hash_mac_addrs[i],
2521                         ETHER_ADDR_LEN) == 0)
2522                         return i;
2523
2524         return -1;
2525 }
2526
2527 int
2528 rte_eth_dev_uc_hash_table_set(uint8_t port_id, struct ether_addr *addr,
2529                                 uint8_t on)
2530 {
2531         int index;
2532         int ret;
2533         struct rte_eth_dev *dev;
2534
2535         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2536
2537         dev = &rte_eth_devices[port_id];
2538         if (is_zero_ether_addr(addr)) {
2539                 RTE_PMD_DEBUG_TRACE("port %d: Cannot add NULL MAC address\n",
2540                         port_id);
2541                 return -EINVAL;
2542         }
2543
2544         index = get_hash_mac_addr_index(port_id, addr);
2545         /* Check if it's already there, and do nothing */
2546         if ((index >= 0) && (on))
2547                 return 0;
2548
2549         if (index < 0) {
2550                 if (!on) {
2551                         RTE_PMD_DEBUG_TRACE("port %d: the MAC address was not "
2552                                 "set in UTA\n", port_id);
2553                         return -EINVAL;
2554                 }
2555
2556                 index = get_hash_mac_addr_index(port_id, &null_mac_addr);
2557                 if (index < 0) {
2558                         RTE_PMD_DEBUG_TRACE("port %d: MAC address array full\n",
2559                                         port_id);
2560                         return -ENOSPC;
2561                 }
2562         }
2563
2564         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->uc_hash_table_set, -ENOTSUP);
2565         ret = (*dev->dev_ops->uc_hash_table_set)(dev, addr, on);
2566         if (ret == 0) {
2567                 /* Update address in NIC data structure */
2568                 if (on)
2569                         ether_addr_copy(addr,
2570                                         &dev->data->hash_mac_addrs[index]);
2571                 else
2572                         ether_addr_copy(&null_mac_addr,
2573                                         &dev->data->hash_mac_addrs[index]);
2574         }
2575
2576         return ret;
2577 }
2578
2579 int
2580 rte_eth_dev_uc_all_hash_table_set(uint8_t port_id, uint8_t on)
2581 {
2582         struct rte_eth_dev *dev;
2583
2584         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2585
2586         dev = &rte_eth_devices[port_id];
2587
2588         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->uc_all_hash_table_set, -ENOTSUP);
2589         return (*dev->dev_ops->uc_all_hash_table_set)(dev, on);
2590 }
2591
2592 int rte_eth_set_queue_rate_limit(uint8_t port_id, uint16_t queue_idx,
2593                                         uint16_t tx_rate)
2594 {
2595         struct rte_eth_dev *dev;
2596         struct rte_eth_dev_info dev_info;
2597         struct rte_eth_link link;
2598
2599         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2600
2601         dev = &rte_eth_devices[port_id];
2602         rte_eth_dev_info_get(port_id, &dev_info);
2603         link = dev->data->dev_link;
2604
2605         if (queue_idx > dev_info.max_tx_queues) {
2606                 RTE_PMD_DEBUG_TRACE("set queue rate limit:port %d: "
2607                                 "invalid queue id=%d\n", port_id, queue_idx);
2608                 return -EINVAL;
2609         }
2610
2611         if (tx_rate > link.link_speed) {
2612                 RTE_PMD_DEBUG_TRACE("set queue rate limit:invalid tx_rate=%d, "
2613                                 "bigger than link speed= %d\n",
2614                         tx_rate, link.link_speed);
2615                 return -EINVAL;
2616         }
2617
2618         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_queue_rate_limit, -ENOTSUP);
2619         return (*dev->dev_ops->set_queue_rate_limit)(dev, queue_idx, tx_rate);
2620 }
2621
2622 int
2623 rte_eth_mirror_rule_set(uint8_t port_id,
2624                         struct rte_eth_mirror_conf *mirror_conf,
2625                         uint8_t rule_id, uint8_t on)
2626 {
2627         struct rte_eth_dev *dev;
2628
2629         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2630         if (mirror_conf->rule_type == 0) {
2631                 RTE_PMD_DEBUG_TRACE("mirror rule type can not be 0.\n");
2632                 return -EINVAL;
2633         }
2634
2635         if (mirror_conf->dst_pool >= ETH_64_POOLS) {
2636                 RTE_PMD_DEBUG_TRACE("Invalid dst pool, pool id must be 0-%d\n",
2637                                 ETH_64_POOLS - 1);
2638                 return -EINVAL;
2639         }
2640
2641         if ((mirror_conf->rule_type & (ETH_MIRROR_VIRTUAL_POOL_UP |
2642              ETH_MIRROR_VIRTUAL_POOL_DOWN)) &&
2643             (mirror_conf->pool_mask == 0)) {
2644                 RTE_PMD_DEBUG_TRACE("Invalid mirror pool, pool mask can not be 0.\n");
2645                 return -EINVAL;
2646         }
2647
2648         if ((mirror_conf->rule_type & ETH_MIRROR_VLAN) &&
2649             mirror_conf->vlan.vlan_mask == 0) {
2650                 RTE_PMD_DEBUG_TRACE("Invalid vlan mask, vlan mask can not be 0.\n");
2651                 return -EINVAL;
2652         }
2653
2654         dev = &rte_eth_devices[port_id];
2655         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mirror_rule_set, -ENOTSUP);
2656
2657         return (*dev->dev_ops->mirror_rule_set)(dev, mirror_conf, rule_id, on);
2658 }
2659
2660 int
2661 rte_eth_mirror_rule_reset(uint8_t port_id, uint8_t rule_id)
2662 {
2663         struct rte_eth_dev *dev;
2664
2665         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2666
2667         dev = &rte_eth_devices[port_id];
2668         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mirror_rule_reset, -ENOTSUP);
2669
2670         return (*dev->dev_ops->mirror_rule_reset)(dev, rule_id);
2671 }
2672
2673 int
2674 rte_eth_dev_callback_register(uint8_t port_id,
2675                         enum rte_eth_event_type event,
2676                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
2677 {
2678         struct rte_eth_dev *dev;
2679         struct rte_eth_dev_callback *user_cb;
2680
2681         if (!cb_fn)
2682                 return -EINVAL;
2683
2684         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
2685
2686         dev = &rte_eth_devices[port_id];
2687         rte_spinlock_lock(&rte_eth_dev_cb_lock);
2688
2689         TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
2690                 if (user_cb->cb_fn == cb_fn &&
2691                         user_cb->cb_arg == cb_arg &&
2692                         user_cb->event == event) {
2693                         break;
2694                 }
2695         }
2696
2697         /* create a new callback. */
2698         if (user_cb == NULL) {
2699                 user_cb = rte_zmalloc("INTR_USER_CALLBACK",
2700                                         sizeof(struct rte_eth_dev_callback), 0);
2701                 if (user_cb != NULL) {
2702                         user_cb->cb_fn = cb_fn;
2703                         user_cb->cb_arg = cb_arg;
2704                         user_cb->event = event;
2705                         TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
2706                 }
2707         }
2708
2709         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2710         return (user_cb == NULL) ? -ENOMEM : 0;
2711 }
2712
2713 int
2714 rte_eth_dev_callback_unregister(uint8_t port_id,
2715                         enum rte_eth_event_type event,
2716                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
2717 {
2718         int ret;
2719         struct rte_eth_dev *dev;
2720         struct rte_eth_dev_callback *cb, *next;
2721
2722         if (!cb_fn)
2723                 return -EINVAL;
2724
2725         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
2726
2727         dev = &rte_eth_devices[port_id];
2728         rte_spinlock_lock(&rte_eth_dev_cb_lock);
2729
2730         ret = 0;
2731         for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL; cb = next) {
2732
2733                 next = TAILQ_NEXT(cb, next);
2734
2735                 if (cb->cb_fn != cb_fn || cb->event != event ||
2736                                 (cb->cb_arg != (void *)-1 &&
2737                                 cb->cb_arg != cb_arg))
2738                         continue;
2739
2740                 /*
2741                  * if this callback is not executing right now,
2742                  * then remove it.
2743                  */
2744                 if (cb->active == 0) {
2745                         TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
2746                         rte_free(cb);
2747                 } else {
2748                         ret = -EAGAIN;
2749                 }
2750         }
2751
2752         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2753         return ret;
2754 }
2755
2756 int
2757 _rte_eth_dev_callback_process(struct rte_eth_dev *dev,
2758         enum rte_eth_event_type event, void *cb_arg, void *ret_param)
2759 {
2760         struct rte_eth_dev_callback *cb_lst;
2761         struct rte_eth_dev_callback dev_cb;
2762         int rc = 0;
2763
2764         rte_spinlock_lock(&rte_eth_dev_cb_lock);
2765         TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
2766                 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
2767                         continue;
2768                 dev_cb = *cb_lst;
2769                 cb_lst->active = 1;
2770                 if (cb_arg != NULL)
2771                         dev_cb.cb_arg = cb_arg;
2772                 if (ret_param != NULL)
2773                         dev_cb.ret_param = ret_param;
2774
2775                 rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2776                 rc = dev_cb.cb_fn(dev->data->port_id, dev_cb.event,
2777                                 dev_cb.cb_arg, dev_cb.ret_param);
2778                 rte_spinlock_lock(&rte_eth_dev_cb_lock);
2779                 cb_lst->active = 0;
2780         }
2781         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2782         return rc;
2783 }
2784
2785 int
2786 rte_eth_dev_rx_intr_ctl(uint8_t port_id, int epfd, int op, void *data)
2787 {
2788         uint32_t vec;
2789         struct rte_eth_dev *dev;
2790         struct rte_intr_handle *intr_handle;
2791         uint16_t qid;
2792         int rc;
2793
2794         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2795
2796         dev = &rte_eth_devices[port_id];
2797
2798         if (!dev->intr_handle) {
2799                 RTE_PMD_DEBUG_TRACE("RX Intr handle unset\n");
2800                 return -ENOTSUP;
2801         }
2802
2803         intr_handle = dev->intr_handle;
2804         if (!intr_handle->intr_vec) {
2805                 RTE_PMD_DEBUG_TRACE("RX Intr vector unset\n");
2806                 return -EPERM;
2807         }
2808
2809         for (qid = 0; qid < dev->data->nb_rx_queues; qid++) {
2810                 vec = intr_handle->intr_vec[qid];
2811                 rc = rte_intr_rx_ctl(intr_handle, epfd, op, vec, data);
2812                 if (rc && rc != -EEXIST) {
2813                         RTE_PMD_DEBUG_TRACE("p %u q %u rx ctl error"
2814                                         " op %d epfd %d vec %u\n",
2815                                         port_id, qid, op, epfd, vec);
2816                 }
2817         }
2818
2819         return 0;
2820 }
2821
2822 const struct rte_memzone *
2823 rte_eth_dma_zone_reserve(const struct rte_eth_dev *dev, const char *ring_name,
2824                          uint16_t queue_id, size_t size, unsigned align,
2825                          int socket_id)
2826 {
2827         char z_name[RTE_MEMZONE_NAMESIZE];
2828         const struct rte_memzone *mz;
2829
2830         snprintf(z_name, sizeof(z_name), "%s_%s_%d_%d",
2831                  dev->device->driver->name, ring_name,
2832                  dev->data->port_id, queue_id);
2833
2834         mz = rte_memzone_lookup(z_name);
2835         if (mz)
2836                 return mz;
2837
2838         if (rte_xen_dom0_supported())
2839                 return rte_memzone_reserve_bounded(z_name, size, socket_id,
2840                                                    0, align, RTE_PGSIZE_2M);
2841         else
2842                 return rte_memzone_reserve_aligned(z_name, size, socket_id,
2843                                                    0, align);
2844 }
2845
2846 int
2847 rte_eth_dev_rx_intr_ctl_q(uint8_t port_id, uint16_t queue_id,
2848                           int epfd, int op, void *data)
2849 {
2850         uint32_t vec;
2851         struct rte_eth_dev *dev;
2852         struct rte_intr_handle *intr_handle;
2853         int rc;
2854
2855         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2856
2857         dev = &rte_eth_devices[port_id];
2858         if (queue_id >= dev->data->nb_rx_queues) {
2859                 RTE_PMD_DEBUG_TRACE("Invalid RX queue_id=%u\n", queue_id);
2860                 return -EINVAL;
2861         }
2862
2863         if (!dev->intr_handle) {
2864                 RTE_PMD_DEBUG_TRACE("RX Intr handle unset\n");
2865                 return -ENOTSUP;
2866         }
2867
2868         intr_handle = dev->intr_handle;
2869         if (!intr_handle->intr_vec) {
2870                 RTE_PMD_DEBUG_TRACE("RX Intr vector unset\n");
2871                 return -EPERM;
2872         }
2873
2874         vec = intr_handle->intr_vec[queue_id];
2875         rc = rte_intr_rx_ctl(intr_handle, epfd, op, vec, data);
2876         if (rc && rc != -EEXIST) {
2877                 RTE_PMD_DEBUG_TRACE("p %u q %u rx ctl error"
2878                                 " op %d epfd %d vec %u\n",
2879                                 port_id, queue_id, op, epfd, vec);
2880                 return rc;
2881         }
2882
2883         return 0;
2884 }
2885
2886 int
2887 rte_eth_dev_rx_intr_enable(uint8_t port_id,
2888                            uint16_t queue_id)
2889 {
2890         struct rte_eth_dev *dev;
2891
2892         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2893
2894         dev = &rte_eth_devices[port_id];
2895
2896         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_intr_enable, -ENOTSUP);
2897         return (*dev->dev_ops->rx_queue_intr_enable)(dev, queue_id);
2898 }
2899
2900 int
2901 rte_eth_dev_rx_intr_disable(uint8_t port_id,
2902                             uint16_t queue_id)
2903 {
2904         struct rte_eth_dev *dev;
2905
2906         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2907
2908         dev = &rte_eth_devices[port_id];
2909
2910         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_intr_disable, -ENOTSUP);
2911         return (*dev->dev_ops->rx_queue_intr_disable)(dev, queue_id);
2912 }
2913
2914
2915 int
2916 rte_eth_dev_filter_supported(uint8_t port_id, enum rte_filter_type filter_type)
2917 {
2918         struct rte_eth_dev *dev;
2919
2920         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2921
2922         dev = &rte_eth_devices[port_id];
2923         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->filter_ctrl, -ENOTSUP);
2924         return (*dev->dev_ops->filter_ctrl)(dev, filter_type,
2925                                 RTE_ETH_FILTER_NOP, NULL);
2926 }
2927
2928 int
2929 rte_eth_dev_filter_ctrl(uint8_t port_id, enum rte_filter_type filter_type,
2930                        enum rte_filter_op filter_op, void *arg)
2931 {
2932         struct rte_eth_dev *dev;
2933
2934         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2935
2936         dev = &rte_eth_devices[port_id];
2937         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->filter_ctrl, -ENOTSUP);
2938         return (*dev->dev_ops->filter_ctrl)(dev, filter_type, filter_op, arg);
2939 }
2940
2941 void *
2942 rte_eth_add_rx_callback(uint8_t port_id, uint16_t queue_id,
2943                 rte_rx_callback_fn fn, void *user_param)
2944 {
2945 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
2946         rte_errno = ENOTSUP;
2947         return NULL;
2948 #endif
2949         /* check input parameters */
2950         if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
2951                     queue_id >= rte_eth_devices[port_id].data->nb_rx_queues) {
2952                 rte_errno = EINVAL;
2953                 return NULL;
2954         }
2955         struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
2956
2957         if (cb == NULL) {
2958                 rte_errno = ENOMEM;
2959                 return NULL;
2960         }
2961
2962         cb->fn.rx = fn;
2963         cb->param = user_param;
2964
2965         rte_spinlock_lock(&rte_eth_rx_cb_lock);
2966         /* Add the callbacks in fifo order. */
2967         struct rte_eth_rxtx_callback *tail =
2968                 rte_eth_devices[port_id].post_rx_burst_cbs[queue_id];
2969
2970         if (!tail) {
2971                 rte_eth_devices[port_id].post_rx_burst_cbs[queue_id] = cb;
2972
2973         } else {
2974                 while (tail->next)
2975                         tail = tail->next;
2976                 tail->next = cb;
2977         }
2978         rte_spinlock_unlock(&rte_eth_rx_cb_lock);
2979
2980         return cb;
2981 }
2982
2983 void *
2984 rte_eth_add_first_rx_callback(uint8_t port_id, uint16_t queue_id,
2985                 rte_rx_callback_fn fn, void *user_param)
2986 {
2987 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
2988         rte_errno = ENOTSUP;
2989         return NULL;
2990 #endif
2991         /* check input parameters */
2992         if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
2993                 queue_id >= rte_eth_devices[port_id].data->nb_rx_queues) {
2994                 rte_errno = EINVAL;
2995                 return NULL;
2996         }
2997
2998         struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
2999
3000         if (cb == NULL) {
3001                 rte_errno = ENOMEM;
3002                 return NULL;
3003         }
3004
3005         cb->fn.rx = fn;
3006         cb->param = user_param;
3007
3008         rte_spinlock_lock(&rte_eth_rx_cb_lock);
3009         /* Add the callbacks at fisrt position*/
3010         cb->next = rte_eth_devices[port_id].post_rx_burst_cbs[queue_id];
3011         rte_smp_wmb();
3012         rte_eth_devices[port_id].post_rx_burst_cbs[queue_id] = cb;
3013         rte_spinlock_unlock(&rte_eth_rx_cb_lock);
3014
3015         return cb;
3016 }
3017
3018 void *
3019 rte_eth_add_tx_callback(uint8_t port_id, uint16_t queue_id,
3020                 rte_tx_callback_fn fn, void *user_param)
3021 {
3022 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
3023         rte_errno = ENOTSUP;
3024         return NULL;
3025 #endif
3026         /* check input parameters */
3027         if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
3028                     queue_id >= rte_eth_devices[port_id].data->nb_tx_queues) {
3029                 rte_errno = EINVAL;
3030                 return NULL;
3031         }
3032
3033         struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
3034
3035         if (cb == NULL) {
3036                 rte_errno = ENOMEM;
3037                 return NULL;
3038         }
3039
3040         cb->fn.tx = fn;
3041         cb->param = user_param;
3042
3043         rte_spinlock_lock(&rte_eth_tx_cb_lock);
3044         /* Add the callbacks in fifo order. */
3045         struct rte_eth_rxtx_callback *tail =
3046                 rte_eth_devices[port_id].pre_tx_burst_cbs[queue_id];
3047
3048         if (!tail) {
3049                 rte_eth_devices[port_id].pre_tx_burst_cbs[queue_id] = cb;
3050
3051         } else {
3052                 while (tail->next)
3053                         tail = tail->next;
3054                 tail->next = cb;
3055         }
3056         rte_spinlock_unlock(&rte_eth_tx_cb_lock);
3057
3058         return cb;
3059 }
3060
3061 int
3062 rte_eth_remove_rx_callback(uint8_t port_id, uint16_t queue_id,
3063                 struct rte_eth_rxtx_callback *user_cb)
3064 {
3065 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
3066         return -ENOTSUP;
3067 #endif
3068         /* Check input parameters. */
3069         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
3070         if (user_cb == NULL ||
3071                         queue_id >= rte_eth_devices[port_id].data->nb_rx_queues)
3072                 return -EINVAL;
3073
3074         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
3075         struct rte_eth_rxtx_callback *cb;
3076         struct rte_eth_rxtx_callback **prev_cb;
3077         int ret = -EINVAL;
3078
3079         rte_spinlock_lock(&rte_eth_rx_cb_lock);
3080         prev_cb = &dev->post_rx_burst_cbs[queue_id];
3081         for (; *prev_cb != NULL; prev_cb = &cb->next) {
3082                 cb = *prev_cb;
3083                 if (cb == user_cb) {
3084                         /* Remove the user cb from the callback list. */
3085                         *prev_cb = cb->next;
3086                         ret = 0;
3087                         break;
3088                 }
3089         }
3090         rte_spinlock_unlock(&rte_eth_rx_cb_lock);
3091
3092         return ret;
3093 }
3094
3095 int
3096 rte_eth_remove_tx_callback(uint8_t port_id, uint16_t queue_id,
3097                 struct rte_eth_rxtx_callback *user_cb)
3098 {
3099 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
3100         return -ENOTSUP;
3101 #endif
3102         /* Check input parameters. */
3103         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
3104         if (user_cb == NULL ||
3105                         queue_id >= rte_eth_devices[port_id].data->nb_tx_queues)
3106                 return -EINVAL;
3107
3108         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
3109         int ret = -EINVAL;
3110         struct rte_eth_rxtx_callback *cb;
3111         struct rte_eth_rxtx_callback **prev_cb;
3112
3113         rte_spinlock_lock(&rte_eth_tx_cb_lock);
3114         prev_cb = &dev->pre_tx_burst_cbs[queue_id];
3115         for (; *prev_cb != NULL; prev_cb = &cb->next) {
3116                 cb = *prev_cb;
3117                 if (cb == user_cb) {
3118                         /* Remove the user cb from the callback list. */
3119                         *prev_cb = cb->next;
3120                         ret = 0;
3121                         break;
3122                 }
3123         }
3124         rte_spinlock_unlock(&rte_eth_tx_cb_lock);
3125
3126         return ret;
3127 }
3128
3129 int
3130 rte_eth_rx_queue_info_get(uint8_t port_id, uint16_t queue_id,
3131         struct rte_eth_rxq_info *qinfo)
3132 {
3133         struct rte_eth_dev *dev;
3134
3135         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3136
3137         if (qinfo == NULL)
3138                 return -EINVAL;
3139
3140         dev = &rte_eth_devices[port_id];
3141         if (queue_id >= dev->data->nb_rx_queues) {
3142                 RTE_PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", queue_id);
3143                 return -EINVAL;
3144         }
3145
3146         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rxq_info_get, -ENOTSUP);
3147
3148         memset(qinfo, 0, sizeof(*qinfo));
3149         dev->dev_ops->rxq_info_get(dev, queue_id, qinfo);
3150         return 0;
3151 }
3152
3153 int
3154 rte_eth_tx_queue_info_get(uint8_t port_id, uint16_t queue_id,
3155         struct rte_eth_txq_info *qinfo)
3156 {
3157         struct rte_eth_dev *dev;
3158
3159         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3160
3161         if (qinfo == NULL)
3162                 return -EINVAL;
3163
3164         dev = &rte_eth_devices[port_id];
3165         if (queue_id >= dev->data->nb_tx_queues) {
3166                 RTE_PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", queue_id);
3167                 return -EINVAL;
3168         }
3169
3170         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->txq_info_get, -ENOTSUP);
3171
3172         memset(qinfo, 0, sizeof(*qinfo));
3173         dev->dev_ops->txq_info_get(dev, queue_id, qinfo);
3174         return 0;
3175 }
3176
3177 int
3178 rte_eth_dev_set_mc_addr_list(uint8_t port_id,
3179                              struct ether_addr *mc_addr_set,
3180                              uint32_t nb_mc_addr)
3181 {
3182         struct rte_eth_dev *dev;
3183
3184         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3185
3186         dev = &rte_eth_devices[port_id];
3187         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_mc_addr_list, -ENOTSUP);
3188         return dev->dev_ops->set_mc_addr_list(dev, mc_addr_set, nb_mc_addr);
3189 }
3190
3191 int
3192 rte_eth_timesync_enable(uint8_t port_id)
3193 {
3194         struct rte_eth_dev *dev;
3195
3196         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3197         dev = &rte_eth_devices[port_id];
3198
3199         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_enable, -ENOTSUP);
3200         return (*dev->dev_ops->timesync_enable)(dev);
3201 }
3202
3203 int
3204 rte_eth_timesync_disable(uint8_t port_id)
3205 {
3206         struct rte_eth_dev *dev;
3207
3208         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3209         dev = &rte_eth_devices[port_id];
3210
3211         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_disable, -ENOTSUP);
3212         return (*dev->dev_ops->timesync_disable)(dev);
3213 }
3214
3215 int
3216 rte_eth_timesync_read_rx_timestamp(uint8_t port_id, struct timespec *timestamp,
3217                                    uint32_t flags)
3218 {
3219         struct rte_eth_dev *dev;
3220
3221         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3222         dev = &rte_eth_devices[port_id];
3223
3224         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_rx_timestamp, -ENOTSUP);
3225         return (*dev->dev_ops->timesync_read_rx_timestamp)(dev, timestamp, flags);
3226 }
3227
3228 int
3229 rte_eth_timesync_read_tx_timestamp(uint8_t port_id, struct timespec *timestamp)
3230 {
3231         struct rte_eth_dev *dev;
3232
3233         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3234         dev = &rte_eth_devices[port_id];
3235
3236         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_tx_timestamp, -ENOTSUP);
3237         return (*dev->dev_ops->timesync_read_tx_timestamp)(dev, timestamp);
3238 }
3239
3240 int
3241 rte_eth_timesync_adjust_time(uint8_t port_id, int64_t delta)
3242 {
3243         struct rte_eth_dev *dev;
3244
3245         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3246         dev = &rte_eth_devices[port_id];
3247
3248         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_adjust_time, -ENOTSUP);
3249         return (*dev->dev_ops->timesync_adjust_time)(dev, delta);
3250 }
3251
3252 int
3253 rte_eth_timesync_read_time(uint8_t port_id, struct timespec *timestamp)
3254 {
3255         struct rte_eth_dev *dev;
3256
3257         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3258         dev = &rte_eth_devices[port_id];
3259
3260         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_time, -ENOTSUP);
3261         return (*dev->dev_ops->timesync_read_time)(dev, timestamp);
3262 }
3263
3264 int
3265 rte_eth_timesync_write_time(uint8_t port_id, const struct timespec *timestamp)
3266 {
3267         struct rte_eth_dev *dev;
3268
3269         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3270         dev = &rte_eth_devices[port_id];
3271
3272         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_write_time, -ENOTSUP);
3273         return (*dev->dev_ops->timesync_write_time)(dev, timestamp);
3274 }
3275
3276 int
3277 rte_eth_dev_get_reg_info(uint8_t port_id, struct rte_dev_reg_info *info)
3278 {
3279         struct rte_eth_dev *dev;
3280
3281         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3282
3283         dev = &rte_eth_devices[port_id];
3284         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_reg, -ENOTSUP);
3285         return (*dev->dev_ops->get_reg)(dev, info);
3286 }
3287
3288 int
3289 rte_eth_dev_get_eeprom_length(uint8_t port_id)
3290 {
3291         struct rte_eth_dev *dev;
3292
3293         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3294
3295         dev = &rte_eth_devices[port_id];
3296         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_eeprom_length, -ENOTSUP);
3297         return (*dev->dev_ops->get_eeprom_length)(dev);
3298 }
3299
3300 int
3301 rte_eth_dev_get_eeprom(uint8_t port_id, struct rte_dev_eeprom_info *info)
3302 {
3303         struct rte_eth_dev *dev;
3304
3305         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3306
3307         dev = &rte_eth_devices[port_id];
3308         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_eeprom, -ENOTSUP);
3309         return (*dev->dev_ops->get_eeprom)(dev, info);
3310 }
3311
3312 int
3313 rte_eth_dev_set_eeprom(uint8_t port_id, struct rte_dev_eeprom_info *info)
3314 {
3315         struct rte_eth_dev *dev;
3316
3317         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3318
3319         dev = &rte_eth_devices[port_id];
3320         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_eeprom, -ENOTSUP);
3321         return (*dev->dev_ops->set_eeprom)(dev, info);
3322 }
3323
3324 int
3325 rte_eth_dev_get_dcb_info(uint8_t port_id,
3326                              struct rte_eth_dcb_info *dcb_info)
3327 {
3328         struct rte_eth_dev *dev;
3329
3330         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3331
3332         dev = &rte_eth_devices[port_id];
3333         memset(dcb_info, 0, sizeof(struct rte_eth_dcb_info));
3334
3335         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_dcb_info, -ENOTSUP);
3336         return (*dev->dev_ops->get_dcb_info)(dev, dcb_info);
3337 }
3338
3339 int
3340 rte_eth_dev_l2_tunnel_eth_type_conf(uint8_t port_id,
3341                                     struct rte_eth_l2_tunnel_conf *l2_tunnel)
3342 {
3343         struct rte_eth_dev *dev;
3344
3345         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3346         if (l2_tunnel == NULL) {
3347                 RTE_PMD_DEBUG_TRACE("Invalid l2_tunnel parameter\n");
3348                 return -EINVAL;
3349         }
3350
3351         if (l2_tunnel->l2_tunnel_type >= RTE_TUNNEL_TYPE_MAX) {
3352                 RTE_PMD_DEBUG_TRACE("Invalid tunnel type\n");
3353                 return -EINVAL;
3354         }
3355
3356         dev = &rte_eth_devices[port_id];
3357         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->l2_tunnel_eth_type_conf,
3358                                 -ENOTSUP);
3359         return (*dev->dev_ops->l2_tunnel_eth_type_conf)(dev, l2_tunnel);
3360 }
3361
3362 int
3363 rte_eth_dev_l2_tunnel_offload_set(uint8_t port_id,
3364                                   struct rte_eth_l2_tunnel_conf *l2_tunnel,
3365                                   uint32_t mask,
3366                                   uint8_t en)
3367 {
3368         struct rte_eth_dev *dev;
3369
3370         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3371
3372         if (l2_tunnel == NULL) {
3373                 RTE_PMD_DEBUG_TRACE("Invalid l2_tunnel parameter\n");
3374                 return -EINVAL;
3375         }
3376
3377         if (l2_tunnel->l2_tunnel_type >= RTE_TUNNEL_TYPE_MAX) {
3378                 RTE_PMD_DEBUG_TRACE("Invalid tunnel type.\n");
3379                 return -EINVAL;
3380         }
3381
3382         if (mask == 0) {
3383                 RTE_PMD_DEBUG_TRACE("Mask should have a value.\n");
3384                 return -EINVAL;
3385         }
3386
3387         dev = &rte_eth_devices[port_id];
3388         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->l2_tunnel_offload_set,
3389                                 -ENOTSUP);
3390         return (*dev->dev_ops->l2_tunnel_offload_set)(dev, l2_tunnel, mask, en);
3391 }
3392
3393 static void
3394 rte_eth_dev_adjust_nb_desc(uint16_t *nb_desc,
3395                            const struct rte_eth_desc_lim *desc_lim)
3396 {
3397         if (desc_lim->nb_align != 0)
3398                 *nb_desc = RTE_ALIGN_CEIL(*nb_desc, desc_lim->nb_align);
3399
3400         if (desc_lim->nb_max != 0)
3401                 *nb_desc = RTE_MIN(*nb_desc, desc_lim->nb_max);
3402
3403         *nb_desc = RTE_MAX(*nb_desc, desc_lim->nb_min);
3404 }
3405
3406 int
3407 rte_eth_dev_adjust_nb_rx_tx_desc(uint8_t port_id,
3408                                  uint16_t *nb_rx_desc,
3409                                  uint16_t *nb_tx_desc)
3410 {
3411         struct rte_eth_dev *dev;
3412         struct rte_eth_dev_info dev_info;
3413
3414         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3415
3416         dev = &rte_eth_devices[port_id];
3417         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
3418
3419         rte_eth_dev_info_get(port_id, &dev_info);
3420
3421         if (nb_rx_desc != NULL)
3422                 rte_eth_dev_adjust_nb_desc(nb_rx_desc, &dev_info.rx_desc_lim);
3423
3424         if (nb_tx_desc != NULL)
3425                 rte_eth_dev_adjust_nb_desc(nb_tx_desc, &dev_info.tx_desc_lim);
3426
3427         return 0;
3428 }