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