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