ethdev: fix build with gcc 5.4.0
[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         int ret;
1985
1986         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1987         dev = &rte_eth_devices[port_id];
1988         if (!(dev->data->dev_conf.rxmode.hw_vlan_filter)) {
1989                 RTE_PMD_DEBUG_TRACE("port %d: vlan-filtering disabled\n", port_id);
1990                 return -ENOSYS;
1991         }
1992
1993         if (vlan_id > 4095) {
1994                 RTE_PMD_DEBUG_TRACE("(port_id=%d) invalid vlan_id=%u > 4095\n",
1995                                 port_id, (unsigned) vlan_id);
1996                 return -EINVAL;
1997         }
1998         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_filter_set, -ENOTSUP);
1999
2000         ret = (*dev->dev_ops->vlan_filter_set)(dev, vlan_id, on);
2001         if (ret == 0) {
2002                 struct rte_vlan_filter_conf *vfc;
2003                 int vidx;
2004                 int vbit;
2005
2006                 vfc = &dev->data->vlan_filter_conf;
2007                 vidx = vlan_id / 64;
2008                 vbit = vlan_id % 64;
2009
2010                 if (on)
2011                         vfc->ids[vidx] |= UINT64_C(1) << vbit;
2012                 else
2013                         vfc->ids[vidx] &= ~(UINT64_C(1) << vbit);
2014         }
2015
2016         return ret;
2017 }
2018
2019 int
2020 rte_eth_dev_set_vlan_strip_on_queue(uint8_t port_id, uint16_t rx_queue_id, int on)
2021 {
2022         struct rte_eth_dev *dev;
2023
2024         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2025         dev = &rte_eth_devices[port_id];
2026         if (rx_queue_id >= dev->data->nb_rx_queues) {
2027                 RTE_PMD_DEBUG_TRACE("Invalid rx_queue_id=%d\n", port_id);
2028                 return -EINVAL;
2029         }
2030
2031         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_strip_queue_set, -ENOTSUP);
2032         (*dev->dev_ops->vlan_strip_queue_set)(dev, rx_queue_id, on);
2033
2034         return 0;
2035 }
2036
2037 int
2038 rte_eth_dev_set_vlan_ether_type(uint8_t port_id,
2039                                 enum rte_vlan_type vlan_type,
2040                                 uint16_t tpid)
2041 {
2042         struct rte_eth_dev *dev;
2043
2044         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2045         dev = &rte_eth_devices[port_id];
2046         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_tpid_set, -ENOTSUP);
2047
2048         return (*dev->dev_ops->vlan_tpid_set)(dev, vlan_type, tpid);
2049 }
2050
2051 int
2052 rte_eth_dev_set_vlan_offload(uint8_t port_id, int offload_mask)
2053 {
2054         struct rte_eth_dev *dev;
2055         int ret = 0;
2056         int mask = 0;
2057         int cur, org = 0;
2058
2059         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2060         dev = &rte_eth_devices[port_id];
2061
2062         /*check which option changed by application*/
2063         cur = !!(offload_mask & ETH_VLAN_STRIP_OFFLOAD);
2064         org = !!(dev->data->dev_conf.rxmode.hw_vlan_strip);
2065         if (cur != org) {
2066                 dev->data->dev_conf.rxmode.hw_vlan_strip = (uint8_t)cur;
2067                 mask |= ETH_VLAN_STRIP_MASK;
2068         }
2069
2070         cur = !!(offload_mask & ETH_VLAN_FILTER_OFFLOAD);
2071         org = !!(dev->data->dev_conf.rxmode.hw_vlan_filter);
2072         if (cur != org) {
2073                 dev->data->dev_conf.rxmode.hw_vlan_filter = (uint8_t)cur;
2074                 mask |= ETH_VLAN_FILTER_MASK;
2075         }
2076
2077         cur = !!(offload_mask & ETH_VLAN_EXTEND_OFFLOAD);
2078         org = !!(dev->data->dev_conf.rxmode.hw_vlan_extend);
2079         if (cur != org) {
2080                 dev->data->dev_conf.rxmode.hw_vlan_extend = (uint8_t)cur;
2081                 mask |= ETH_VLAN_EXTEND_MASK;
2082         }
2083
2084         /*no change*/
2085         if (mask == 0)
2086                 return ret;
2087
2088         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_offload_set, -ENOTSUP);
2089         (*dev->dev_ops->vlan_offload_set)(dev, mask);
2090
2091         return ret;
2092 }
2093
2094 int
2095 rte_eth_dev_get_vlan_offload(uint8_t port_id)
2096 {
2097         struct rte_eth_dev *dev;
2098         int ret = 0;
2099
2100         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2101         dev = &rte_eth_devices[port_id];
2102
2103         if (dev->data->dev_conf.rxmode.hw_vlan_strip)
2104                 ret |= ETH_VLAN_STRIP_OFFLOAD;
2105
2106         if (dev->data->dev_conf.rxmode.hw_vlan_filter)
2107                 ret |= ETH_VLAN_FILTER_OFFLOAD;
2108
2109         if (dev->data->dev_conf.rxmode.hw_vlan_extend)
2110                 ret |= ETH_VLAN_EXTEND_OFFLOAD;
2111
2112         return ret;
2113 }
2114
2115 int
2116 rte_eth_dev_set_vlan_pvid(uint8_t port_id, uint16_t pvid, int on)
2117 {
2118         struct rte_eth_dev *dev;
2119
2120         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2121         dev = &rte_eth_devices[port_id];
2122         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_pvid_set, -ENOTSUP);
2123         (*dev->dev_ops->vlan_pvid_set)(dev, pvid, on);
2124
2125         return 0;
2126 }
2127
2128 int
2129 rte_eth_dev_flow_ctrl_get(uint8_t port_id, struct rte_eth_fc_conf *fc_conf)
2130 {
2131         struct rte_eth_dev *dev;
2132
2133         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2134         dev = &rte_eth_devices[port_id];
2135         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_get, -ENOTSUP);
2136         memset(fc_conf, 0, sizeof(*fc_conf));
2137         return (*dev->dev_ops->flow_ctrl_get)(dev, fc_conf);
2138 }
2139
2140 int
2141 rte_eth_dev_flow_ctrl_set(uint8_t port_id, struct rte_eth_fc_conf *fc_conf)
2142 {
2143         struct rte_eth_dev *dev;
2144
2145         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2146         if ((fc_conf->send_xon != 0) && (fc_conf->send_xon != 1)) {
2147                 RTE_PMD_DEBUG_TRACE("Invalid send_xon, only 0/1 allowed\n");
2148                 return -EINVAL;
2149         }
2150
2151         dev = &rte_eth_devices[port_id];
2152         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_set, -ENOTSUP);
2153         return (*dev->dev_ops->flow_ctrl_set)(dev, fc_conf);
2154 }
2155
2156 int
2157 rte_eth_dev_priority_flow_ctrl_set(uint8_t port_id, struct rte_eth_pfc_conf *pfc_conf)
2158 {
2159         struct rte_eth_dev *dev;
2160
2161         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2162         if (pfc_conf->priority > (ETH_DCB_NUM_USER_PRIORITIES - 1)) {
2163                 RTE_PMD_DEBUG_TRACE("Invalid priority, only 0-7 allowed\n");
2164                 return -EINVAL;
2165         }
2166
2167         dev = &rte_eth_devices[port_id];
2168         /* High water, low water validation are device specific */
2169         if  (*dev->dev_ops->priority_flow_ctrl_set)
2170                 return (*dev->dev_ops->priority_flow_ctrl_set)(dev, pfc_conf);
2171         return -ENOTSUP;
2172 }
2173
2174 static int
2175 rte_eth_check_reta_mask(struct rte_eth_rss_reta_entry64 *reta_conf,
2176                         uint16_t reta_size)
2177 {
2178         uint16_t i, num;
2179
2180         if (!reta_conf)
2181                 return -EINVAL;
2182
2183         num = (reta_size + RTE_RETA_GROUP_SIZE - 1) / RTE_RETA_GROUP_SIZE;
2184         for (i = 0; i < num; i++) {
2185                 if (reta_conf[i].mask)
2186                         return 0;
2187         }
2188
2189         return -EINVAL;
2190 }
2191
2192 static int
2193 rte_eth_check_reta_entry(struct rte_eth_rss_reta_entry64 *reta_conf,
2194                          uint16_t reta_size,
2195                          uint16_t max_rxq)
2196 {
2197         uint16_t i, idx, shift;
2198
2199         if (!reta_conf)
2200                 return -EINVAL;
2201
2202         if (max_rxq == 0) {
2203                 RTE_PMD_DEBUG_TRACE("No receive queue is available\n");
2204                 return -EINVAL;
2205         }
2206
2207         for (i = 0; i < reta_size; i++) {
2208                 idx = i / RTE_RETA_GROUP_SIZE;
2209                 shift = i % RTE_RETA_GROUP_SIZE;
2210                 if ((reta_conf[idx].mask & (1ULL << shift)) &&
2211                         (reta_conf[idx].reta[shift] >= max_rxq)) {
2212                         RTE_PMD_DEBUG_TRACE("reta_conf[%u]->reta[%u]: %u exceeds "
2213                                 "the maximum rxq index: %u\n", idx, shift,
2214                                 reta_conf[idx].reta[shift], max_rxq);
2215                         return -EINVAL;
2216                 }
2217         }
2218
2219         return 0;
2220 }
2221
2222 int
2223 rte_eth_dev_rss_reta_update(uint8_t port_id,
2224                             struct rte_eth_rss_reta_entry64 *reta_conf,
2225                             uint16_t reta_size)
2226 {
2227         struct rte_eth_dev *dev;
2228         int ret;
2229
2230         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2231         /* Check mask bits */
2232         ret = rte_eth_check_reta_mask(reta_conf, reta_size);
2233         if (ret < 0)
2234                 return ret;
2235
2236         dev = &rte_eth_devices[port_id];
2237
2238         /* Check entry value */
2239         ret = rte_eth_check_reta_entry(reta_conf, reta_size,
2240                                 dev->data->nb_rx_queues);
2241         if (ret < 0)
2242                 return ret;
2243
2244         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->reta_update, -ENOTSUP);
2245         return (*dev->dev_ops->reta_update)(dev, reta_conf, reta_size);
2246 }
2247
2248 int
2249 rte_eth_dev_rss_reta_query(uint8_t port_id,
2250                            struct rte_eth_rss_reta_entry64 *reta_conf,
2251                            uint16_t reta_size)
2252 {
2253         struct rte_eth_dev *dev;
2254         int ret;
2255
2256         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2257
2258         /* Check mask bits */
2259         ret = rte_eth_check_reta_mask(reta_conf, reta_size);
2260         if (ret < 0)
2261                 return ret;
2262
2263         dev = &rte_eth_devices[port_id];
2264         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->reta_query, -ENOTSUP);
2265         return (*dev->dev_ops->reta_query)(dev, reta_conf, reta_size);
2266 }
2267
2268 int
2269 rte_eth_dev_rss_hash_update(uint8_t port_id, struct rte_eth_rss_conf *rss_conf)
2270 {
2271         struct rte_eth_dev *dev;
2272         uint16_t rss_hash_protos;
2273
2274         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2275         rss_hash_protos = rss_conf->rss_hf;
2276         if ((rss_hash_protos != 0) &&
2277             ((rss_hash_protos & ETH_RSS_PROTO_MASK) == 0)) {
2278                 RTE_PMD_DEBUG_TRACE("Invalid rss_hash_protos=0x%x\n",
2279                                 rss_hash_protos);
2280                 return -EINVAL;
2281         }
2282         dev = &rte_eth_devices[port_id];
2283         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rss_hash_update, -ENOTSUP);
2284         return (*dev->dev_ops->rss_hash_update)(dev, rss_conf);
2285 }
2286
2287 int
2288 rte_eth_dev_rss_hash_conf_get(uint8_t port_id,
2289                               struct rte_eth_rss_conf *rss_conf)
2290 {
2291         struct rte_eth_dev *dev;
2292
2293         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2294         dev = &rte_eth_devices[port_id];
2295         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rss_hash_conf_get, -ENOTSUP);
2296         return (*dev->dev_ops->rss_hash_conf_get)(dev, rss_conf);
2297 }
2298
2299 int
2300 rte_eth_dev_udp_tunnel_port_add(uint8_t port_id,
2301                                 struct rte_eth_udp_tunnel *udp_tunnel)
2302 {
2303         struct rte_eth_dev *dev;
2304
2305         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2306         if (udp_tunnel == NULL) {
2307                 RTE_PMD_DEBUG_TRACE("Invalid udp_tunnel parameter\n");
2308                 return -EINVAL;
2309         }
2310
2311         if (udp_tunnel->prot_type >= RTE_TUNNEL_TYPE_MAX) {
2312                 RTE_PMD_DEBUG_TRACE("Invalid tunnel type\n");
2313                 return -EINVAL;
2314         }
2315
2316         dev = &rte_eth_devices[port_id];
2317         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->udp_tunnel_port_add, -ENOTSUP);
2318         return (*dev->dev_ops->udp_tunnel_port_add)(dev, udp_tunnel);
2319 }
2320
2321 int
2322 rte_eth_dev_udp_tunnel_port_delete(uint8_t port_id,
2323                                    struct rte_eth_udp_tunnel *udp_tunnel)
2324 {
2325         struct rte_eth_dev *dev;
2326
2327         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2328         dev = &rte_eth_devices[port_id];
2329
2330         if (udp_tunnel == NULL) {
2331                 RTE_PMD_DEBUG_TRACE("Invalid udp_tunnel parameter\n");
2332                 return -EINVAL;
2333         }
2334
2335         if (udp_tunnel->prot_type >= RTE_TUNNEL_TYPE_MAX) {
2336                 RTE_PMD_DEBUG_TRACE("Invalid tunnel type\n");
2337                 return -EINVAL;
2338         }
2339
2340         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->udp_tunnel_port_del, -ENOTSUP);
2341         return (*dev->dev_ops->udp_tunnel_port_del)(dev, udp_tunnel);
2342 }
2343
2344 int
2345 rte_eth_led_on(uint8_t port_id)
2346 {
2347         struct rte_eth_dev *dev;
2348
2349         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2350         dev = &rte_eth_devices[port_id];
2351         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_on, -ENOTSUP);
2352         return (*dev->dev_ops->dev_led_on)(dev);
2353 }
2354
2355 int
2356 rte_eth_led_off(uint8_t port_id)
2357 {
2358         struct rte_eth_dev *dev;
2359
2360         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2361         dev = &rte_eth_devices[port_id];
2362         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_off, -ENOTSUP);
2363         return (*dev->dev_ops->dev_led_off)(dev);
2364 }
2365
2366 /*
2367  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
2368  * an empty spot.
2369  */
2370 static int
2371 get_mac_addr_index(uint8_t port_id, const struct ether_addr *addr)
2372 {
2373         struct rte_eth_dev_info dev_info;
2374         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
2375         unsigned i;
2376
2377         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2378         rte_eth_dev_info_get(port_id, &dev_info);
2379
2380         for (i = 0; i < dev_info.max_mac_addrs; i++)
2381                 if (memcmp(addr, &dev->data->mac_addrs[i], ETHER_ADDR_LEN) == 0)
2382                         return i;
2383
2384         return -1;
2385 }
2386
2387 static const struct ether_addr null_mac_addr;
2388
2389 int
2390 rte_eth_dev_mac_addr_add(uint8_t port_id, struct ether_addr *addr,
2391                         uint32_t pool)
2392 {
2393         struct rte_eth_dev *dev;
2394         int index;
2395         uint64_t pool_mask;
2396         int ret;
2397
2398         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2399         dev = &rte_eth_devices[port_id];
2400         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_add, -ENOTSUP);
2401
2402         if (is_zero_ether_addr(addr)) {
2403                 RTE_PMD_DEBUG_TRACE("port %d: Cannot add NULL MAC address\n",
2404                         port_id);
2405                 return -EINVAL;
2406         }
2407         if (pool >= ETH_64_POOLS) {
2408                 RTE_PMD_DEBUG_TRACE("pool id must be 0-%d\n", ETH_64_POOLS - 1);
2409                 return -EINVAL;
2410         }
2411
2412         index = get_mac_addr_index(port_id, addr);
2413         if (index < 0) {
2414                 index = get_mac_addr_index(port_id, &null_mac_addr);
2415                 if (index < 0) {
2416                         RTE_PMD_DEBUG_TRACE("port %d: MAC address array full\n",
2417                                 port_id);
2418                         return -ENOSPC;
2419                 }
2420         } else {
2421                 pool_mask = dev->data->mac_pool_sel[index];
2422
2423                 /* Check if both MAC address and pool is already there, and do nothing */
2424                 if (pool_mask & (1ULL << pool))
2425                         return 0;
2426         }
2427
2428         /* Update NIC */
2429         ret = (*dev->dev_ops->mac_addr_add)(dev, addr, index, pool);
2430
2431         if (ret == 0) {
2432                 /* Update address in NIC data structure */
2433                 ether_addr_copy(addr, &dev->data->mac_addrs[index]);
2434
2435                 /* Update pool bitmap in NIC data structure */
2436                 dev->data->mac_pool_sel[index] |= (1ULL << pool);
2437         }
2438
2439         return ret;
2440 }
2441
2442 int
2443 rte_eth_dev_mac_addr_remove(uint8_t port_id, struct ether_addr *addr)
2444 {
2445         struct rte_eth_dev *dev;
2446         int index;
2447
2448         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2449         dev = &rte_eth_devices[port_id];
2450         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_remove, -ENOTSUP);
2451
2452         index = get_mac_addr_index(port_id, addr);
2453         if (index == 0) {
2454                 RTE_PMD_DEBUG_TRACE("port %d: Cannot remove default MAC address\n", port_id);
2455                 return -EADDRINUSE;
2456         } else if (index < 0)
2457                 return 0;  /* Do nothing if address wasn't found */
2458
2459         /* Update NIC */
2460         (*dev->dev_ops->mac_addr_remove)(dev, index);
2461
2462         /* Update address in NIC data structure */
2463         ether_addr_copy(&null_mac_addr, &dev->data->mac_addrs[index]);
2464
2465         /* reset pool bitmap */
2466         dev->data->mac_pool_sel[index] = 0;
2467
2468         return 0;
2469 }
2470
2471 int
2472 rte_eth_dev_default_mac_addr_set(uint8_t port_id, struct ether_addr *addr)
2473 {
2474         struct rte_eth_dev *dev;
2475
2476         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2477
2478         if (!is_valid_assigned_ether_addr(addr))
2479                 return -EINVAL;
2480
2481         dev = &rte_eth_devices[port_id];
2482         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_set, -ENOTSUP);
2483
2484         /* Update default address in NIC data structure */
2485         ether_addr_copy(addr, &dev->data->mac_addrs[0]);
2486
2487         (*dev->dev_ops->mac_addr_set)(dev, addr);
2488
2489         return 0;
2490 }
2491
2492
2493 /*
2494  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
2495  * an empty spot.
2496  */
2497 static int
2498 get_hash_mac_addr_index(uint8_t port_id, const struct ether_addr *addr)
2499 {
2500         struct rte_eth_dev_info dev_info;
2501         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
2502         unsigned i;
2503
2504         rte_eth_dev_info_get(port_id, &dev_info);
2505         if (!dev->data->hash_mac_addrs)
2506                 return -1;
2507
2508         for (i = 0; i < dev_info.max_hash_mac_addrs; i++)
2509                 if (memcmp(addr, &dev->data->hash_mac_addrs[i],
2510                         ETHER_ADDR_LEN) == 0)
2511                         return i;
2512
2513         return -1;
2514 }
2515
2516 int
2517 rte_eth_dev_uc_hash_table_set(uint8_t port_id, struct ether_addr *addr,
2518                                 uint8_t on)
2519 {
2520         int index;
2521         int ret;
2522         struct rte_eth_dev *dev;
2523
2524         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2525
2526         dev = &rte_eth_devices[port_id];
2527         if (is_zero_ether_addr(addr)) {
2528                 RTE_PMD_DEBUG_TRACE("port %d: Cannot add NULL MAC address\n",
2529                         port_id);
2530                 return -EINVAL;
2531         }
2532
2533         index = get_hash_mac_addr_index(port_id, addr);
2534         /* Check if it's already there, and do nothing */
2535         if ((index >= 0) && (on))
2536                 return 0;
2537
2538         if (index < 0) {
2539                 if (!on) {
2540                         RTE_PMD_DEBUG_TRACE("port %d: the MAC address was not "
2541                                 "set in UTA\n", port_id);
2542                         return -EINVAL;
2543                 }
2544
2545                 index = get_hash_mac_addr_index(port_id, &null_mac_addr);
2546                 if (index < 0) {
2547                         RTE_PMD_DEBUG_TRACE("port %d: MAC address array full\n",
2548                                         port_id);
2549                         return -ENOSPC;
2550                 }
2551         }
2552
2553         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->uc_hash_table_set, -ENOTSUP);
2554         ret = (*dev->dev_ops->uc_hash_table_set)(dev, addr, on);
2555         if (ret == 0) {
2556                 /* Update address in NIC data structure */
2557                 if (on)
2558                         ether_addr_copy(addr,
2559                                         &dev->data->hash_mac_addrs[index]);
2560                 else
2561                         ether_addr_copy(&null_mac_addr,
2562                                         &dev->data->hash_mac_addrs[index]);
2563         }
2564
2565         return ret;
2566 }
2567
2568 int
2569 rte_eth_dev_uc_all_hash_table_set(uint8_t port_id, uint8_t on)
2570 {
2571         struct rte_eth_dev *dev;
2572
2573         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2574
2575         dev = &rte_eth_devices[port_id];
2576
2577         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->uc_all_hash_table_set, -ENOTSUP);
2578         return (*dev->dev_ops->uc_all_hash_table_set)(dev, on);
2579 }
2580
2581 int rte_eth_set_queue_rate_limit(uint8_t port_id, uint16_t queue_idx,
2582                                         uint16_t tx_rate)
2583 {
2584         struct rte_eth_dev *dev;
2585         struct rte_eth_dev_info dev_info;
2586         struct rte_eth_link link;
2587
2588         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2589
2590         dev = &rte_eth_devices[port_id];
2591         rte_eth_dev_info_get(port_id, &dev_info);
2592         link = dev->data->dev_link;
2593
2594         if (queue_idx > dev_info.max_tx_queues) {
2595                 RTE_PMD_DEBUG_TRACE("set queue rate limit:port %d: "
2596                                 "invalid queue id=%d\n", port_id, queue_idx);
2597                 return -EINVAL;
2598         }
2599
2600         if (tx_rate > link.link_speed) {
2601                 RTE_PMD_DEBUG_TRACE("set queue rate limit:invalid tx_rate=%d, "
2602                                 "bigger than link speed= %d\n",
2603                         tx_rate, link.link_speed);
2604                 return -EINVAL;
2605         }
2606
2607         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_queue_rate_limit, -ENOTSUP);
2608         return (*dev->dev_ops->set_queue_rate_limit)(dev, queue_idx, tx_rate);
2609 }
2610
2611 int
2612 rte_eth_mirror_rule_set(uint8_t port_id,
2613                         struct rte_eth_mirror_conf *mirror_conf,
2614                         uint8_t rule_id, uint8_t on)
2615 {
2616         struct rte_eth_dev *dev;
2617
2618         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2619         if (mirror_conf->rule_type == 0) {
2620                 RTE_PMD_DEBUG_TRACE("mirror rule type can not be 0.\n");
2621                 return -EINVAL;
2622         }
2623
2624         if (mirror_conf->dst_pool >= ETH_64_POOLS) {
2625                 RTE_PMD_DEBUG_TRACE("Invalid dst pool, pool id must be 0-%d\n",
2626                                 ETH_64_POOLS - 1);
2627                 return -EINVAL;
2628         }
2629
2630         if ((mirror_conf->rule_type & (ETH_MIRROR_VIRTUAL_POOL_UP |
2631              ETH_MIRROR_VIRTUAL_POOL_DOWN)) &&
2632             (mirror_conf->pool_mask == 0)) {
2633                 RTE_PMD_DEBUG_TRACE("Invalid mirror pool, pool mask can not be 0.\n");
2634                 return -EINVAL;
2635         }
2636
2637         if ((mirror_conf->rule_type & ETH_MIRROR_VLAN) &&
2638             mirror_conf->vlan.vlan_mask == 0) {
2639                 RTE_PMD_DEBUG_TRACE("Invalid vlan mask, vlan mask can not be 0.\n");
2640                 return -EINVAL;
2641         }
2642
2643         dev = &rte_eth_devices[port_id];
2644         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mirror_rule_set, -ENOTSUP);
2645
2646         return (*dev->dev_ops->mirror_rule_set)(dev, mirror_conf, rule_id, on);
2647 }
2648
2649 int
2650 rte_eth_mirror_rule_reset(uint8_t port_id, uint8_t rule_id)
2651 {
2652         struct rte_eth_dev *dev;
2653
2654         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2655
2656         dev = &rte_eth_devices[port_id];
2657         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mirror_rule_reset, -ENOTSUP);
2658
2659         return (*dev->dev_ops->mirror_rule_reset)(dev, rule_id);
2660 }
2661
2662 int
2663 rte_eth_dev_callback_register(uint8_t port_id,
2664                         enum rte_eth_event_type event,
2665                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
2666 {
2667         struct rte_eth_dev *dev;
2668         struct rte_eth_dev_callback *user_cb;
2669
2670         if (!cb_fn)
2671                 return -EINVAL;
2672
2673         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
2674
2675         dev = &rte_eth_devices[port_id];
2676         rte_spinlock_lock(&rte_eth_dev_cb_lock);
2677
2678         TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
2679                 if (user_cb->cb_fn == cb_fn &&
2680                         user_cb->cb_arg == cb_arg &&
2681                         user_cb->event == event) {
2682                         break;
2683                 }
2684         }
2685
2686         /* create a new callback. */
2687         if (user_cb == NULL) {
2688                 user_cb = rte_zmalloc("INTR_USER_CALLBACK",
2689                                         sizeof(struct rte_eth_dev_callback), 0);
2690                 if (user_cb != NULL) {
2691                         user_cb->cb_fn = cb_fn;
2692                         user_cb->cb_arg = cb_arg;
2693                         user_cb->event = event;
2694                         TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
2695                 }
2696         }
2697
2698         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2699         return (user_cb == NULL) ? -ENOMEM : 0;
2700 }
2701
2702 int
2703 rte_eth_dev_callback_unregister(uint8_t port_id,
2704                         enum rte_eth_event_type event,
2705                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
2706 {
2707         int ret;
2708         struct rte_eth_dev *dev;
2709         struct rte_eth_dev_callback *cb, *next;
2710
2711         if (!cb_fn)
2712                 return -EINVAL;
2713
2714         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
2715
2716         dev = &rte_eth_devices[port_id];
2717         rte_spinlock_lock(&rte_eth_dev_cb_lock);
2718
2719         ret = 0;
2720         for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL; cb = next) {
2721
2722                 next = TAILQ_NEXT(cb, next);
2723
2724                 if (cb->cb_fn != cb_fn || cb->event != event ||
2725                                 (cb->cb_arg != (void *)-1 &&
2726                                 cb->cb_arg != cb_arg))
2727                         continue;
2728
2729                 /*
2730                  * if this callback is not executing right now,
2731                  * then remove it.
2732                  */
2733                 if (cb->active == 0) {
2734                         TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
2735                         rte_free(cb);
2736                 } else {
2737                         ret = -EAGAIN;
2738                 }
2739         }
2740
2741         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2742         return ret;
2743 }
2744
2745 int
2746 _rte_eth_dev_callback_process(struct rte_eth_dev *dev,
2747         enum rte_eth_event_type event, void *cb_arg, void *ret_param)
2748 {
2749         struct rte_eth_dev_callback *cb_lst;
2750         struct rte_eth_dev_callback dev_cb;
2751         int rc = 0;
2752
2753         rte_spinlock_lock(&rte_eth_dev_cb_lock);
2754         TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
2755                 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
2756                         continue;
2757                 dev_cb = *cb_lst;
2758                 cb_lst->active = 1;
2759                 if (cb_arg != NULL)
2760                         dev_cb.cb_arg = cb_arg;
2761                 if (ret_param != NULL)
2762                         dev_cb.ret_param = ret_param;
2763
2764                 rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2765                 rc = dev_cb.cb_fn(dev->data->port_id, dev_cb.event,
2766                                 dev_cb.cb_arg, dev_cb.ret_param);
2767                 rte_spinlock_lock(&rte_eth_dev_cb_lock);
2768                 cb_lst->active = 0;
2769         }
2770         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2771         return rc;
2772 }
2773
2774 int
2775 rte_eth_dev_rx_intr_ctl(uint8_t port_id, int epfd, int op, void *data)
2776 {
2777         uint32_t vec;
2778         struct rte_eth_dev *dev;
2779         struct rte_intr_handle *intr_handle;
2780         uint16_t qid;
2781         int rc;
2782
2783         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2784
2785         dev = &rte_eth_devices[port_id];
2786
2787         if (!dev->intr_handle) {
2788                 RTE_PMD_DEBUG_TRACE("RX Intr handle unset\n");
2789                 return -ENOTSUP;
2790         }
2791
2792         intr_handle = dev->intr_handle;
2793         if (!intr_handle->intr_vec) {
2794                 RTE_PMD_DEBUG_TRACE("RX Intr vector unset\n");
2795                 return -EPERM;
2796         }
2797
2798         for (qid = 0; qid < dev->data->nb_rx_queues; qid++) {
2799                 vec = intr_handle->intr_vec[qid];
2800                 rc = rte_intr_rx_ctl(intr_handle, epfd, op, vec, data);
2801                 if (rc && rc != -EEXIST) {
2802                         RTE_PMD_DEBUG_TRACE("p %u q %u rx ctl error"
2803                                         " op %d epfd %d vec %u\n",
2804                                         port_id, qid, op, epfd, vec);
2805                 }
2806         }
2807
2808         return 0;
2809 }
2810
2811 const struct rte_memzone *
2812 rte_eth_dma_zone_reserve(const struct rte_eth_dev *dev, const char *ring_name,
2813                          uint16_t queue_id, size_t size, unsigned align,
2814                          int socket_id)
2815 {
2816         char z_name[RTE_MEMZONE_NAMESIZE];
2817         const struct rte_memzone *mz;
2818
2819         snprintf(z_name, sizeof(z_name), "%s_%s_%d_%d",
2820                  dev->device->driver->name, ring_name,
2821                  dev->data->port_id, queue_id);
2822
2823         mz = rte_memzone_lookup(z_name);
2824         if (mz)
2825                 return mz;
2826
2827         if (rte_xen_dom0_supported())
2828                 return rte_memzone_reserve_bounded(z_name, size, socket_id,
2829                                                    0, align, RTE_PGSIZE_2M);
2830         else
2831                 return rte_memzone_reserve_aligned(z_name, size, socket_id,
2832                                                    0, align);
2833 }
2834
2835 int
2836 rte_eth_dev_rx_intr_ctl_q(uint8_t port_id, uint16_t queue_id,
2837                           int epfd, int op, void *data)
2838 {
2839         uint32_t vec;
2840         struct rte_eth_dev *dev;
2841         struct rte_intr_handle *intr_handle;
2842         int rc;
2843
2844         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2845
2846         dev = &rte_eth_devices[port_id];
2847         if (queue_id >= dev->data->nb_rx_queues) {
2848                 RTE_PMD_DEBUG_TRACE("Invalid RX queue_id=%u\n", queue_id);
2849                 return -EINVAL;
2850         }
2851
2852         if (!dev->intr_handle) {
2853                 RTE_PMD_DEBUG_TRACE("RX Intr handle unset\n");
2854                 return -ENOTSUP;
2855         }
2856
2857         intr_handle = dev->intr_handle;
2858         if (!intr_handle->intr_vec) {
2859                 RTE_PMD_DEBUG_TRACE("RX Intr vector unset\n");
2860                 return -EPERM;
2861         }
2862
2863         vec = intr_handle->intr_vec[queue_id];
2864         rc = rte_intr_rx_ctl(intr_handle, epfd, op, vec, data);
2865         if (rc && rc != -EEXIST) {
2866                 RTE_PMD_DEBUG_TRACE("p %u q %u rx ctl error"
2867                                 " op %d epfd %d vec %u\n",
2868                                 port_id, queue_id, op, epfd, vec);
2869                 return rc;
2870         }
2871
2872         return 0;
2873 }
2874
2875 int
2876 rte_eth_dev_rx_intr_enable(uint8_t port_id,
2877                            uint16_t queue_id)
2878 {
2879         struct rte_eth_dev *dev;
2880
2881         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2882
2883         dev = &rte_eth_devices[port_id];
2884
2885         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_intr_enable, -ENOTSUP);
2886         return (*dev->dev_ops->rx_queue_intr_enable)(dev, queue_id);
2887 }
2888
2889 int
2890 rte_eth_dev_rx_intr_disable(uint8_t port_id,
2891                             uint16_t queue_id)
2892 {
2893         struct rte_eth_dev *dev;
2894
2895         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2896
2897         dev = &rte_eth_devices[port_id];
2898
2899         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_intr_disable, -ENOTSUP);
2900         return (*dev->dev_ops->rx_queue_intr_disable)(dev, queue_id);
2901 }
2902
2903
2904 int
2905 rte_eth_dev_filter_supported(uint8_t port_id, enum rte_filter_type filter_type)
2906 {
2907         struct rte_eth_dev *dev;
2908
2909         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2910
2911         dev = &rte_eth_devices[port_id];
2912         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->filter_ctrl, -ENOTSUP);
2913         return (*dev->dev_ops->filter_ctrl)(dev, filter_type,
2914                                 RTE_ETH_FILTER_NOP, NULL);
2915 }
2916
2917 int
2918 rte_eth_dev_filter_ctrl(uint8_t port_id, enum rte_filter_type filter_type,
2919                        enum rte_filter_op filter_op, void *arg)
2920 {
2921         struct rte_eth_dev *dev;
2922
2923         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2924
2925         dev = &rte_eth_devices[port_id];
2926         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->filter_ctrl, -ENOTSUP);
2927         return (*dev->dev_ops->filter_ctrl)(dev, filter_type, filter_op, arg);
2928 }
2929
2930 void *
2931 rte_eth_add_rx_callback(uint8_t port_id, uint16_t queue_id,
2932                 rte_rx_callback_fn fn, void *user_param)
2933 {
2934 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
2935         rte_errno = ENOTSUP;
2936         return NULL;
2937 #endif
2938         /* check input parameters */
2939         if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
2940                     queue_id >= rte_eth_devices[port_id].data->nb_rx_queues) {
2941                 rte_errno = EINVAL;
2942                 return NULL;
2943         }
2944         struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
2945
2946         if (cb == NULL) {
2947                 rte_errno = ENOMEM;
2948                 return NULL;
2949         }
2950
2951         cb->fn.rx = fn;
2952         cb->param = user_param;
2953
2954         rte_spinlock_lock(&rte_eth_rx_cb_lock);
2955         /* Add the callbacks in fifo order. */
2956         struct rte_eth_rxtx_callback *tail =
2957                 rte_eth_devices[port_id].post_rx_burst_cbs[queue_id];
2958
2959         if (!tail) {
2960                 rte_eth_devices[port_id].post_rx_burst_cbs[queue_id] = cb;
2961
2962         } else {
2963                 while (tail->next)
2964                         tail = tail->next;
2965                 tail->next = cb;
2966         }
2967         rte_spinlock_unlock(&rte_eth_rx_cb_lock);
2968
2969         return cb;
2970 }
2971
2972 void *
2973 rte_eth_add_first_rx_callback(uint8_t port_id, uint16_t queue_id,
2974                 rte_rx_callback_fn fn, void *user_param)
2975 {
2976 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
2977         rte_errno = ENOTSUP;
2978         return NULL;
2979 #endif
2980         /* check input parameters */
2981         if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
2982                 queue_id >= rte_eth_devices[port_id].data->nb_rx_queues) {
2983                 rte_errno = EINVAL;
2984                 return NULL;
2985         }
2986
2987         struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
2988
2989         if (cb == NULL) {
2990                 rte_errno = ENOMEM;
2991                 return NULL;
2992         }
2993
2994         cb->fn.rx = fn;
2995         cb->param = user_param;
2996
2997         rte_spinlock_lock(&rte_eth_rx_cb_lock);
2998         /* Add the callbacks at fisrt position*/
2999         cb->next = rte_eth_devices[port_id].post_rx_burst_cbs[queue_id];
3000         rte_smp_wmb();
3001         rte_eth_devices[port_id].post_rx_burst_cbs[queue_id] = cb;
3002         rte_spinlock_unlock(&rte_eth_rx_cb_lock);
3003
3004         return cb;
3005 }
3006
3007 void *
3008 rte_eth_add_tx_callback(uint8_t port_id, uint16_t queue_id,
3009                 rte_tx_callback_fn fn, void *user_param)
3010 {
3011 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
3012         rte_errno = ENOTSUP;
3013         return NULL;
3014 #endif
3015         /* check input parameters */
3016         if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
3017                     queue_id >= rte_eth_devices[port_id].data->nb_tx_queues) {
3018                 rte_errno = EINVAL;
3019                 return NULL;
3020         }
3021
3022         struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
3023
3024         if (cb == NULL) {
3025                 rte_errno = ENOMEM;
3026                 return NULL;
3027         }
3028
3029         cb->fn.tx = fn;
3030         cb->param = user_param;
3031
3032         rte_spinlock_lock(&rte_eth_tx_cb_lock);
3033         /* Add the callbacks in fifo order. */
3034         struct rte_eth_rxtx_callback *tail =
3035                 rte_eth_devices[port_id].pre_tx_burst_cbs[queue_id];
3036
3037         if (!tail) {
3038                 rte_eth_devices[port_id].pre_tx_burst_cbs[queue_id] = cb;
3039
3040         } else {
3041                 while (tail->next)
3042                         tail = tail->next;
3043                 tail->next = cb;
3044         }
3045         rte_spinlock_unlock(&rte_eth_tx_cb_lock);
3046
3047         return cb;
3048 }
3049
3050 int
3051 rte_eth_remove_rx_callback(uint8_t port_id, uint16_t queue_id,
3052                 struct rte_eth_rxtx_callback *user_cb)
3053 {
3054 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
3055         return -ENOTSUP;
3056 #endif
3057         /* Check input parameters. */
3058         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
3059         if (user_cb == NULL ||
3060                         queue_id >= rte_eth_devices[port_id].data->nb_rx_queues)
3061                 return -EINVAL;
3062
3063         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
3064         struct rte_eth_rxtx_callback *cb;
3065         struct rte_eth_rxtx_callback **prev_cb;
3066         int ret = -EINVAL;
3067
3068         rte_spinlock_lock(&rte_eth_rx_cb_lock);
3069         prev_cb = &dev->post_rx_burst_cbs[queue_id];
3070         for (; *prev_cb != NULL; prev_cb = &cb->next) {
3071                 cb = *prev_cb;
3072                 if (cb == user_cb) {
3073                         /* Remove the user cb from the callback list. */
3074                         *prev_cb = cb->next;
3075                         ret = 0;
3076                         break;
3077                 }
3078         }
3079         rte_spinlock_unlock(&rte_eth_rx_cb_lock);
3080
3081         return ret;
3082 }
3083
3084 int
3085 rte_eth_remove_tx_callback(uint8_t port_id, uint16_t queue_id,
3086                 struct rte_eth_rxtx_callback *user_cb)
3087 {
3088 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
3089         return -ENOTSUP;
3090 #endif
3091         /* Check input parameters. */
3092         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
3093         if (user_cb == NULL ||
3094                         queue_id >= rte_eth_devices[port_id].data->nb_tx_queues)
3095                 return -EINVAL;
3096
3097         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
3098         int ret = -EINVAL;
3099         struct rte_eth_rxtx_callback *cb;
3100         struct rte_eth_rxtx_callback **prev_cb;
3101
3102         rte_spinlock_lock(&rte_eth_tx_cb_lock);
3103         prev_cb = &dev->pre_tx_burst_cbs[queue_id];
3104         for (; *prev_cb != NULL; prev_cb = &cb->next) {
3105                 cb = *prev_cb;
3106                 if (cb == user_cb) {
3107                         /* Remove the user cb from the callback list. */
3108                         *prev_cb = cb->next;
3109                         ret = 0;
3110                         break;
3111                 }
3112         }
3113         rte_spinlock_unlock(&rte_eth_tx_cb_lock);
3114
3115         return ret;
3116 }
3117
3118 int
3119 rte_eth_rx_queue_info_get(uint8_t port_id, uint16_t queue_id,
3120         struct rte_eth_rxq_info *qinfo)
3121 {
3122         struct rte_eth_dev *dev;
3123
3124         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3125
3126         if (qinfo == NULL)
3127                 return -EINVAL;
3128
3129         dev = &rte_eth_devices[port_id];
3130         if (queue_id >= dev->data->nb_rx_queues) {
3131                 RTE_PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", queue_id);
3132                 return -EINVAL;
3133         }
3134
3135         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rxq_info_get, -ENOTSUP);
3136
3137         memset(qinfo, 0, sizeof(*qinfo));
3138         dev->dev_ops->rxq_info_get(dev, queue_id, qinfo);
3139         return 0;
3140 }
3141
3142 int
3143 rte_eth_tx_queue_info_get(uint8_t port_id, uint16_t queue_id,
3144         struct rte_eth_txq_info *qinfo)
3145 {
3146         struct rte_eth_dev *dev;
3147
3148         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3149
3150         if (qinfo == NULL)
3151                 return -EINVAL;
3152
3153         dev = &rte_eth_devices[port_id];
3154         if (queue_id >= dev->data->nb_tx_queues) {
3155                 RTE_PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", queue_id);
3156                 return -EINVAL;
3157         }
3158
3159         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->txq_info_get, -ENOTSUP);
3160
3161         memset(qinfo, 0, sizeof(*qinfo));
3162         dev->dev_ops->txq_info_get(dev, queue_id, qinfo);
3163         return 0;
3164 }
3165
3166 int
3167 rte_eth_dev_set_mc_addr_list(uint8_t port_id,
3168                              struct ether_addr *mc_addr_set,
3169                              uint32_t nb_mc_addr)
3170 {
3171         struct rte_eth_dev *dev;
3172
3173         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3174
3175         dev = &rte_eth_devices[port_id];
3176         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_mc_addr_list, -ENOTSUP);
3177         return dev->dev_ops->set_mc_addr_list(dev, mc_addr_set, nb_mc_addr);
3178 }
3179
3180 int
3181 rte_eth_timesync_enable(uint8_t port_id)
3182 {
3183         struct rte_eth_dev *dev;
3184
3185         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3186         dev = &rte_eth_devices[port_id];
3187
3188         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_enable, -ENOTSUP);
3189         return (*dev->dev_ops->timesync_enable)(dev);
3190 }
3191
3192 int
3193 rte_eth_timesync_disable(uint8_t port_id)
3194 {
3195         struct rte_eth_dev *dev;
3196
3197         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3198         dev = &rte_eth_devices[port_id];
3199
3200         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_disable, -ENOTSUP);
3201         return (*dev->dev_ops->timesync_disable)(dev);
3202 }
3203
3204 int
3205 rte_eth_timesync_read_rx_timestamp(uint8_t port_id, struct timespec *timestamp,
3206                                    uint32_t flags)
3207 {
3208         struct rte_eth_dev *dev;
3209
3210         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3211         dev = &rte_eth_devices[port_id];
3212
3213         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_rx_timestamp, -ENOTSUP);
3214         return (*dev->dev_ops->timesync_read_rx_timestamp)(dev, timestamp, flags);
3215 }
3216
3217 int
3218 rte_eth_timesync_read_tx_timestamp(uint8_t port_id, struct timespec *timestamp)
3219 {
3220         struct rte_eth_dev *dev;
3221
3222         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3223         dev = &rte_eth_devices[port_id];
3224
3225         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_tx_timestamp, -ENOTSUP);
3226         return (*dev->dev_ops->timesync_read_tx_timestamp)(dev, timestamp);
3227 }
3228
3229 int
3230 rte_eth_timesync_adjust_time(uint8_t port_id, int64_t delta)
3231 {
3232         struct rte_eth_dev *dev;
3233
3234         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3235         dev = &rte_eth_devices[port_id];
3236
3237         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_adjust_time, -ENOTSUP);
3238         return (*dev->dev_ops->timesync_adjust_time)(dev, delta);
3239 }
3240
3241 int
3242 rte_eth_timesync_read_time(uint8_t port_id, struct timespec *timestamp)
3243 {
3244         struct rte_eth_dev *dev;
3245
3246         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3247         dev = &rte_eth_devices[port_id];
3248
3249         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_time, -ENOTSUP);
3250         return (*dev->dev_ops->timesync_read_time)(dev, timestamp);
3251 }
3252
3253 int
3254 rte_eth_timesync_write_time(uint8_t port_id, const struct timespec *timestamp)
3255 {
3256         struct rte_eth_dev *dev;
3257
3258         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3259         dev = &rte_eth_devices[port_id];
3260
3261         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_write_time, -ENOTSUP);
3262         return (*dev->dev_ops->timesync_write_time)(dev, timestamp);
3263 }
3264
3265 int
3266 rte_eth_dev_get_reg_info(uint8_t port_id, struct rte_dev_reg_info *info)
3267 {
3268         struct rte_eth_dev *dev;
3269
3270         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3271
3272         dev = &rte_eth_devices[port_id];
3273         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_reg, -ENOTSUP);
3274         return (*dev->dev_ops->get_reg)(dev, info);
3275 }
3276
3277 int
3278 rte_eth_dev_get_eeprom_length(uint8_t port_id)
3279 {
3280         struct rte_eth_dev *dev;
3281
3282         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3283
3284         dev = &rte_eth_devices[port_id];
3285         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_eeprom_length, -ENOTSUP);
3286         return (*dev->dev_ops->get_eeprom_length)(dev);
3287 }
3288
3289 int
3290 rte_eth_dev_get_eeprom(uint8_t port_id, struct rte_dev_eeprom_info *info)
3291 {
3292         struct rte_eth_dev *dev;
3293
3294         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3295
3296         dev = &rte_eth_devices[port_id];
3297         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_eeprom, -ENOTSUP);
3298         return (*dev->dev_ops->get_eeprom)(dev, info);
3299 }
3300
3301 int
3302 rte_eth_dev_set_eeprom(uint8_t port_id, struct rte_dev_eeprom_info *info)
3303 {
3304         struct rte_eth_dev *dev;
3305
3306         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3307
3308         dev = &rte_eth_devices[port_id];
3309         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_eeprom, -ENOTSUP);
3310         return (*dev->dev_ops->set_eeprom)(dev, info);
3311 }
3312
3313 int
3314 rte_eth_dev_get_dcb_info(uint8_t port_id,
3315                              struct rte_eth_dcb_info *dcb_info)
3316 {
3317         struct rte_eth_dev *dev;
3318
3319         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3320
3321         dev = &rte_eth_devices[port_id];
3322         memset(dcb_info, 0, sizeof(struct rte_eth_dcb_info));
3323
3324         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_dcb_info, -ENOTSUP);
3325         return (*dev->dev_ops->get_dcb_info)(dev, dcb_info);
3326 }
3327
3328 int
3329 rte_eth_dev_l2_tunnel_eth_type_conf(uint8_t port_id,
3330                                     struct rte_eth_l2_tunnel_conf *l2_tunnel)
3331 {
3332         struct rte_eth_dev *dev;
3333
3334         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3335         if (l2_tunnel == NULL) {
3336                 RTE_PMD_DEBUG_TRACE("Invalid l2_tunnel parameter\n");
3337                 return -EINVAL;
3338         }
3339
3340         if (l2_tunnel->l2_tunnel_type >= RTE_TUNNEL_TYPE_MAX) {
3341                 RTE_PMD_DEBUG_TRACE("Invalid tunnel type\n");
3342                 return -EINVAL;
3343         }
3344
3345         dev = &rte_eth_devices[port_id];
3346         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->l2_tunnel_eth_type_conf,
3347                                 -ENOTSUP);
3348         return (*dev->dev_ops->l2_tunnel_eth_type_conf)(dev, l2_tunnel);
3349 }
3350
3351 int
3352 rte_eth_dev_l2_tunnel_offload_set(uint8_t port_id,
3353                                   struct rte_eth_l2_tunnel_conf *l2_tunnel,
3354                                   uint32_t mask,
3355                                   uint8_t en)
3356 {
3357         struct rte_eth_dev *dev;
3358
3359         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3360
3361         if (l2_tunnel == NULL) {
3362                 RTE_PMD_DEBUG_TRACE("Invalid l2_tunnel parameter\n");
3363                 return -EINVAL;
3364         }
3365
3366         if (l2_tunnel->l2_tunnel_type >= RTE_TUNNEL_TYPE_MAX) {
3367                 RTE_PMD_DEBUG_TRACE("Invalid tunnel type.\n");
3368                 return -EINVAL;
3369         }
3370
3371         if (mask == 0) {
3372                 RTE_PMD_DEBUG_TRACE("Mask should have a value.\n");
3373                 return -EINVAL;
3374         }
3375
3376         dev = &rte_eth_devices[port_id];
3377         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->l2_tunnel_offload_set,
3378                                 -ENOTSUP);
3379         return (*dev->dev_ops->l2_tunnel_offload_set)(dev, l2_tunnel, mask, en);
3380 }
3381
3382 static void
3383 rte_eth_dev_adjust_nb_desc(uint16_t *nb_desc,
3384                            const struct rte_eth_desc_lim *desc_lim)
3385 {
3386         if (desc_lim->nb_align != 0)
3387                 *nb_desc = RTE_ALIGN_CEIL(*nb_desc, desc_lim->nb_align);
3388
3389         if (desc_lim->nb_max != 0)
3390                 *nb_desc = RTE_MIN(*nb_desc, desc_lim->nb_max);
3391
3392         *nb_desc = RTE_MAX(*nb_desc, desc_lim->nb_min);
3393 }
3394
3395 int
3396 rte_eth_dev_adjust_nb_rx_tx_desc(uint8_t port_id,
3397                                  uint16_t *nb_rx_desc,
3398                                  uint16_t *nb_tx_desc)
3399 {
3400         struct rte_eth_dev *dev;
3401         struct rte_eth_dev_info dev_info;
3402
3403         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3404
3405         dev = &rte_eth_devices[port_id];
3406         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
3407
3408         rte_eth_dev_info_get(port_id, &dev_info);
3409
3410         if (nb_rx_desc != NULL)
3411                 rte_eth_dev_adjust_nb_desc(nb_rx_desc, &dev_info.rx_desc_lim);
3412
3413         if (nb_tx_desc != NULL)
3414                 rte_eth_dev_adjust_nb_desc(nb_tx_desc, &dev_info.tx_desc_lim);
3415
3416         return 0;
3417 }