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