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