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