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