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