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