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