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