ethdev: add devop to check removal status
[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                rte_eth_devices[port_id].state != RTE_ETH_DEV_REMOVED)
175                 port_id++;
176
177         if (port_id >= RTE_MAX_ETHPORTS)
178                 return RTE_MAX_ETHPORTS;
179
180         return port_id;
181 }
182
183 static void
184 rte_eth_dev_data_alloc(void)
185 {
186         const unsigned flags = 0;
187         const struct rte_memzone *mz;
188
189         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
190                 mz = rte_memzone_reserve(MZ_RTE_ETH_DEV_DATA,
191                                 RTE_MAX_ETHPORTS * sizeof(*rte_eth_dev_data),
192                                 rte_socket_id(), flags);
193         } else
194                 mz = rte_memzone_lookup(MZ_RTE_ETH_DEV_DATA);
195         if (mz == NULL)
196                 rte_panic("Cannot allocate memzone for ethernet port data\n");
197
198         rte_eth_dev_data = mz->addr;
199         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
200                 memset(rte_eth_dev_data, 0,
201                                 RTE_MAX_ETHPORTS * sizeof(*rte_eth_dev_data));
202 }
203
204 struct rte_eth_dev *
205 rte_eth_dev_allocated(const char *name)
206 {
207         unsigned i;
208
209         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
210                 if ((rte_eth_devices[i].state == RTE_ETH_DEV_ATTACHED) &&
211                     strcmp(rte_eth_devices[i].data->name, name) == 0)
212                         return &rte_eth_devices[i];
213         }
214         return NULL;
215 }
216
217 static uint16_t
218 rte_eth_dev_find_free_port(void)
219 {
220         unsigned i;
221
222         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
223                 if (rte_eth_devices[i].state == RTE_ETH_DEV_UNUSED)
224                         return i;
225         }
226         return RTE_MAX_ETHPORTS;
227 }
228
229 static struct rte_eth_dev *
230 eth_dev_get(uint16_t port_id)
231 {
232         struct rte_eth_dev *eth_dev = &rte_eth_devices[port_id];
233
234         eth_dev->data = &rte_eth_dev_data[port_id];
235         eth_dev->state = RTE_ETH_DEV_ATTACHED;
236
237         eth_dev_last_created_port = port_id;
238
239         return eth_dev;
240 }
241
242 struct rte_eth_dev *
243 rte_eth_dev_allocate(const char *name)
244 {
245         uint16_t port_id;
246         struct rte_eth_dev *eth_dev;
247
248         port_id = rte_eth_dev_find_free_port();
249         if (port_id == RTE_MAX_ETHPORTS) {
250                 RTE_PMD_DEBUG_TRACE("Reached maximum number of Ethernet ports\n");
251                 return NULL;
252         }
253
254         if (rte_eth_dev_data == NULL)
255                 rte_eth_dev_data_alloc();
256
257         if (rte_eth_dev_allocated(name) != NULL) {
258                 RTE_PMD_DEBUG_TRACE("Ethernet Device with name %s already allocated!\n",
259                                 name);
260                 return NULL;
261         }
262
263         memset(&rte_eth_dev_data[port_id], 0, sizeof(struct rte_eth_dev_data));
264         eth_dev = eth_dev_get(port_id);
265         snprintf(eth_dev->data->name, sizeof(eth_dev->data->name), "%s", name);
266         eth_dev->data->port_id = port_id;
267         eth_dev->data->mtu = ETHER_MTU;
268
269         _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_NEW, NULL);
270
271         return eth_dev;
272 }
273
274 /*
275  * Attach to a port already registered by the primary process, which
276  * makes sure that the same device would have the same port id both
277  * in the primary and secondary process.
278  */
279 struct rte_eth_dev *
280 rte_eth_dev_attach_secondary(const char *name)
281 {
282         uint16_t i;
283         struct rte_eth_dev *eth_dev;
284
285         if (rte_eth_dev_data == NULL)
286                 rte_eth_dev_data_alloc();
287
288         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
289                 if (strcmp(rte_eth_dev_data[i].name, name) == 0)
290                         break;
291         }
292         if (i == RTE_MAX_ETHPORTS) {
293                 RTE_PMD_DEBUG_TRACE(
294                         "device %s is not driven by the primary process\n",
295                         name);
296                 return NULL;
297         }
298
299         eth_dev = eth_dev_get(i);
300         RTE_ASSERT(eth_dev->data->port_id == i);
301
302         return eth_dev;
303 }
304
305 int
306 rte_eth_dev_release_port(struct rte_eth_dev *eth_dev)
307 {
308         if (eth_dev == NULL)
309                 return -EINVAL;
310
311         eth_dev->state = RTE_ETH_DEV_UNUSED;
312
313         _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_DESTROY, NULL);
314
315         return 0;
316 }
317
318 int
319 rte_eth_dev_is_valid_port(uint16_t port_id)
320 {
321         if (port_id >= RTE_MAX_ETHPORTS ||
322             (rte_eth_devices[port_id].state == RTE_ETH_DEV_UNUSED))
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_dev_is_removed(uint16_t port_id)
1186 {
1187         struct rte_eth_dev *dev;
1188         int ret;
1189
1190         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, 0);
1191
1192         dev = &rte_eth_devices[port_id];
1193
1194         if (dev->state == RTE_ETH_DEV_REMOVED)
1195                 return 1;
1196
1197         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->is_removed, 0);
1198
1199         ret = dev->dev_ops->is_removed(dev);
1200         if (ret != 0)
1201                 /* Device is physically removed. */
1202                 dev->state = RTE_ETH_DEV_REMOVED;
1203
1204         return ret;
1205 }
1206
1207 int
1208 rte_eth_rx_queue_setup(uint16_t port_id, uint16_t rx_queue_id,
1209                        uint16_t nb_rx_desc, unsigned int socket_id,
1210                        const struct rte_eth_rxconf *rx_conf,
1211                        struct rte_mempool *mp)
1212 {
1213         int ret;
1214         uint32_t mbp_buf_size;
1215         struct rte_eth_dev *dev;
1216         struct rte_eth_dev_info dev_info;
1217         struct rte_eth_rxconf local_conf;
1218         void **rxq;
1219
1220         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1221
1222         dev = &rte_eth_devices[port_id];
1223         if (rx_queue_id >= dev->data->nb_rx_queues) {
1224                 RTE_PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", rx_queue_id);
1225                 return -EINVAL;
1226         }
1227
1228         if (dev->data->dev_started) {
1229                 RTE_PMD_DEBUG_TRACE(
1230                     "port %d must be stopped to allow configuration\n", port_id);
1231                 return -EBUSY;
1232         }
1233
1234         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
1235         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_setup, -ENOTSUP);
1236
1237         /*
1238          * Check the size of the mbuf data buffer.
1239          * This value must be provided in the private data of the memory pool.
1240          * First check that the memory pool has a valid private data.
1241          */
1242         rte_eth_dev_info_get(port_id, &dev_info);
1243         if (mp->private_data_size < sizeof(struct rte_pktmbuf_pool_private)) {
1244                 RTE_PMD_DEBUG_TRACE("%s private_data_size %d < %d\n",
1245                                 mp->name, (int) mp->private_data_size,
1246                                 (int) sizeof(struct rte_pktmbuf_pool_private));
1247                 return -ENOSPC;
1248         }
1249         mbp_buf_size = rte_pktmbuf_data_room_size(mp);
1250
1251         if ((mbp_buf_size - RTE_PKTMBUF_HEADROOM) < dev_info.min_rx_bufsize) {
1252                 RTE_PMD_DEBUG_TRACE("%s mbuf_data_room_size %d < %d "
1253                                 "(RTE_PKTMBUF_HEADROOM=%d + min_rx_bufsize(dev)"
1254                                 "=%d)\n",
1255                                 mp->name,
1256                                 (int)mbp_buf_size,
1257                                 (int)(RTE_PKTMBUF_HEADROOM +
1258                                       dev_info.min_rx_bufsize),
1259                                 (int)RTE_PKTMBUF_HEADROOM,
1260                                 (int)dev_info.min_rx_bufsize);
1261                 return -EINVAL;
1262         }
1263
1264         if (nb_rx_desc > dev_info.rx_desc_lim.nb_max ||
1265                         nb_rx_desc < dev_info.rx_desc_lim.nb_min ||
1266                         nb_rx_desc % dev_info.rx_desc_lim.nb_align != 0) {
1267
1268                 RTE_PMD_DEBUG_TRACE("Invalid value for nb_rx_desc(=%hu), "
1269                         "should be: <= %hu, = %hu, and a product of %hu\n",
1270                         nb_rx_desc,
1271                         dev_info.rx_desc_lim.nb_max,
1272                         dev_info.rx_desc_lim.nb_min,
1273                         dev_info.rx_desc_lim.nb_align);
1274                 return -EINVAL;
1275         }
1276
1277         rxq = dev->data->rx_queues;
1278         if (rxq[rx_queue_id]) {
1279                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_release,
1280                                         -ENOTSUP);
1281                 (*dev->dev_ops->rx_queue_release)(rxq[rx_queue_id]);
1282                 rxq[rx_queue_id] = NULL;
1283         }
1284
1285         if (rx_conf == NULL)
1286                 rx_conf = &dev_info.default_rxconf;
1287
1288         local_conf = *rx_conf;
1289         if (dev->data->dev_conf.rxmode.ignore_offload_bitfield == 0) {
1290                 /**
1291                  * Reflect port offloads to queue offloads in order for
1292                  * offloads to not be discarded.
1293                  */
1294                 rte_eth_convert_rx_offload_bitfield(&dev->data->dev_conf.rxmode,
1295                                                     &local_conf.offloads);
1296         }
1297
1298         ret = (*dev->dev_ops->rx_queue_setup)(dev, rx_queue_id, nb_rx_desc,
1299                                               socket_id, &local_conf, mp);
1300         if (!ret) {
1301                 if (!dev->data->min_rx_buf_size ||
1302                     dev->data->min_rx_buf_size > mbp_buf_size)
1303                         dev->data->min_rx_buf_size = mbp_buf_size;
1304         }
1305
1306         return ret;
1307 }
1308
1309 /**
1310  * A conversion function from txq_flags API.
1311  */
1312 static void
1313 rte_eth_convert_txq_flags(const uint32_t txq_flags, uint64_t *tx_offloads)
1314 {
1315         uint64_t offloads = 0;
1316
1317         if (!(txq_flags & ETH_TXQ_FLAGS_NOMULTSEGS))
1318                 offloads |= DEV_TX_OFFLOAD_MULTI_SEGS;
1319         if (!(txq_flags & ETH_TXQ_FLAGS_NOVLANOFFL))
1320                 offloads |= DEV_TX_OFFLOAD_VLAN_INSERT;
1321         if (!(txq_flags & ETH_TXQ_FLAGS_NOXSUMSCTP))
1322                 offloads |= DEV_TX_OFFLOAD_SCTP_CKSUM;
1323         if (!(txq_flags & ETH_TXQ_FLAGS_NOXSUMUDP))
1324                 offloads |= DEV_TX_OFFLOAD_UDP_CKSUM;
1325         if (!(txq_flags & ETH_TXQ_FLAGS_NOXSUMTCP))
1326                 offloads |= DEV_TX_OFFLOAD_TCP_CKSUM;
1327         if ((txq_flags & ETH_TXQ_FLAGS_NOREFCOUNT) &&
1328             (txq_flags & ETH_TXQ_FLAGS_NOMULTMEMP))
1329                 offloads |= DEV_TX_OFFLOAD_MBUF_FAST_FREE;
1330
1331         *tx_offloads = offloads;
1332 }
1333
1334 /**
1335  * A conversion function from offloads API.
1336  */
1337 static void
1338 rte_eth_convert_txq_offloads(const uint64_t tx_offloads, uint32_t *txq_flags)
1339 {
1340         uint32_t flags = 0;
1341
1342         if (!(tx_offloads & DEV_TX_OFFLOAD_MULTI_SEGS))
1343                 flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
1344         if (!(tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT))
1345                 flags |= ETH_TXQ_FLAGS_NOVLANOFFL;
1346         if (!(tx_offloads & DEV_TX_OFFLOAD_SCTP_CKSUM))
1347                 flags |= ETH_TXQ_FLAGS_NOXSUMSCTP;
1348         if (!(tx_offloads & DEV_TX_OFFLOAD_UDP_CKSUM))
1349                 flags |= ETH_TXQ_FLAGS_NOXSUMUDP;
1350         if (!(tx_offloads & DEV_TX_OFFLOAD_TCP_CKSUM))
1351                 flags |= ETH_TXQ_FLAGS_NOXSUMTCP;
1352         if (tx_offloads & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
1353                 flags |= (ETH_TXQ_FLAGS_NOREFCOUNT | ETH_TXQ_FLAGS_NOMULTMEMP);
1354
1355         *txq_flags = flags;
1356 }
1357
1358 int
1359 rte_eth_tx_queue_setup(uint16_t port_id, uint16_t tx_queue_id,
1360                        uint16_t nb_tx_desc, unsigned int socket_id,
1361                        const struct rte_eth_txconf *tx_conf)
1362 {
1363         struct rte_eth_dev *dev;
1364         struct rte_eth_dev_info dev_info;
1365         struct rte_eth_txconf local_conf;
1366         void **txq;
1367
1368         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1369
1370         dev = &rte_eth_devices[port_id];
1371         if (tx_queue_id >= dev->data->nb_tx_queues) {
1372                 RTE_PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", tx_queue_id);
1373                 return -EINVAL;
1374         }
1375
1376         if (dev->data->dev_started) {
1377                 RTE_PMD_DEBUG_TRACE(
1378                     "port %d must be stopped to allow configuration\n", port_id);
1379                 return -EBUSY;
1380         }
1381
1382         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
1383         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_setup, -ENOTSUP);
1384
1385         rte_eth_dev_info_get(port_id, &dev_info);
1386
1387         if (nb_tx_desc > dev_info.tx_desc_lim.nb_max ||
1388             nb_tx_desc < dev_info.tx_desc_lim.nb_min ||
1389             nb_tx_desc % dev_info.tx_desc_lim.nb_align != 0) {
1390                 RTE_PMD_DEBUG_TRACE("Invalid value for nb_tx_desc(=%hu), "
1391                                 "should be: <= %hu, = %hu, and a product of %hu\n",
1392                                 nb_tx_desc,
1393                                 dev_info.tx_desc_lim.nb_max,
1394                                 dev_info.tx_desc_lim.nb_min,
1395                                 dev_info.tx_desc_lim.nb_align);
1396                 return -EINVAL;
1397         }
1398
1399         txq = dev->data->tx_queues;
1400         if (txq[tx_queue_id]) {
1401                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_release,
1402                                         -ENOTSUP);
1403                 (*dev->dev_ops->tx_queue_release)(txq[tx_queue_id]);
1404                 txq[tx_queue_id] = NULL;
1405         }
1406
1407         if (tx_conf == NULL)
1408                 tx_conf = &dev_info.default_txconf;
1409
1410         /*
1411          * Convert between the offloads API to enable PMDs to support
1412          * only one of them.
1413          */
1414         local_conf = *tx_conf;
1415         if (tx_conf->txq_flags & ETH_TXQ_FLAGS_IGNORE) {
1416                 rte_eth_convert_txq_offloads(tx_conf->offloads,
1417                                              &local_conf.txq_flags);
1418                 /* Keep the ignore flag. */
1419                 local_conf.txq_flags |= ETH_TXQ_FLAGS_IGNORE;
1420         } else {
1421                 rte_eth_convert_txq_flags(tx_conf->txq_flags,
1422                                           &local_conf.offloads);
1423         }
1424
1425         return (*dev->dev_ops->tx_queue_setup)(dev, tx_queue_id, nb_tx_desc,
1426                                                socket_id, &local_conf);
1427 }
1428
1429 void
1430 rte_eth_tx_buffer_drop_callback(struct rte_mbuf **pkts, uint16_t unsent,
1431                 void *userdata __rte_unused)
1432 {
1433         unsigned i;
1434
1435         for (i = 0; i < unsent; i++)
1436                 rte_pktmbuf_free(pkts[i]);
1437 }
1438
1439 void
1440 rte_eth_tx_buffer_count_callback(struct rte_mbuf **pkts, uint16_t unsent,
1441                 void *userdata)
1442 {
1443         uint64_t *count = userdata;
1444         unsigned i;
1445
1446         for (i = 0; i < unsent; i++)
1447                 rte_pktmbuf_free(pkts[i]);
1448
1449         *count += unsent;
1450 }
1451
1452 int
1453 rte_eth_tx_buffer_set_err_callback(struct rte_eth_dev_tx_buffer *buffer,
1454                 buffer_tx_error_fn cbfn, void *userdata)
1455 {
1456         buffer->error_callback = cbfn;
1457         buffer->error_userdata = userdata;
1458         return 0;
1459 }
1460
1461 int
1462 rte_eth_tx_buffer_init(struct rte_eth_dev_tx_buffer *buffer, uint16_t size)
1463 {
1464         int ret = 0;
1465
1466         if (buffer == NULL)
1467                 return -EINVAL;
1468
1469         buffer->size = size;
1470         if (buffer->error_callback == NULL) {
1471                 ret = rte_eth_tx_buffer_set_err_callback(
1472                         buffer, rte_eth_tx_buffer_drop_callback, NULL);
1473         }
1474
1475         return ret;
1476 }
1477
1478 int
1479 rte_eth_tx_done_cleanup(uint16_t port_id, uint16_t queue_id, uint32_t free_cnt)
1480 {
1481         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1482
1483         /* Validate Input Data. Bail if not valid or not supported. */
1484         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1485         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_done_cleanup, -ENOTSUP);
1486
1487         /* Call driver to free pending mbufs. */
1488         return (*dev->dev_ops->tx_done_cleanup)(dev->data->tx_queues[queue_id],
1489                         free_cnt);
1490 }
1491
1492 void
1493 rte_eth_promiscuous_enable(uint16_t port_id)
1494 {
1495         struct rte_eth_dev *dev;
1496
1497         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1498         dev = &rte_eth_devices[port_id];
1499
1500         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->promiscuous_enable);
1501         (*dev->dev_ops->promiscuous_enable)(dev);
1502         dev->data->promiscuous = 1;
1503 }
1504
1505 void
1506 rte_eth_promiscuous_disable(uint16_t port_id)
1507 {
1508         struct rte_eth_dev *dev;
1509
1510         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1511         dev = &rte_eth_devices[port_id];
1512
1513         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->promiscuous_disable);
1514         dev->data->promiscuous = 0;
1515         (*dev->dev_ops->promiscuous_disable)(dev);
1516 }
1517
1518 int
1519 rte_eth_promiscuous_get(uint16_t port_id)
1520 {
1521         struct rte_eth_dev *dev;
1522
1523         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1524
1525         dev = &rte_eth_devices[port_id];
1526         return dev->data->promiscuous;
1527 }
1528
1529 void
1530 rte_eth_allmulticast_enable(uint16_t port_id)
1531 {
1532         struct rte_eth_dev *dev;
1533
1534         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1535         dev = &rte_eth_devices[port_id];
1536
1537         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_enable);
1538         (*dev->dev_ops->allmulticast_enable)(dev);
1539         dev->data->all_multicast = 1;
1540 }
1541
1542 void
1543 rte_eth_allmulticast_disable(uint16_t port_id)
1544 {
1545         struct rte_eth_dev *dev;
1546
1547         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1548         dev = &rte_eth_devices[port_id];
1549
1550         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_disable);
1551         dev->data->all_multicast = 0;
1552         (*dev->dev_ops->allmulticast_disable)(dev);
1553 }
1554
1555 int
1556 rte_eth_allmulticast_get(uint16_t port_id)
1557 {
1558         struct rte_eth_dev *dev;
1559
1560         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1561
1562         dev = &rte_eth_devices[port_id];
1563         return dev->data->all_multicast;
1564 }
1565
1566 static inline int
1567 rte_eth_dev_atomic_read_link_status(struct rte_eth_dev *dev,
1568                                 struct rte_eth_link *link)
1569 {
1570         struct rte_eth_link *dst = link;
1571         struct rte_eth_link *src = &(dev->data->dev_link);
1572
1573         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
1574                                         *(uint64_t *)src) == 0)
1575                 return -1;
1576
1577         return 0;
1578 }
1579
1580 void
1581 rte_eth_link_get(uint16_t port_id, struct rte_eth_link *eth_link)
1582 {
1583         struct rte_eth_dev *dev;
1584
1585         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1586         dev = &rte_eth_devices[port_id];
1587
1588         if (dev->data->dev_conf.intr_conf.lsc != 0)
1589                 rte_eth_dev_atomic_read_link_status(dev, eth_link);
1590         else {
1591                 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
1592                 (*dev->dev_ops->link_update)(dev, 1);
1593                 *eth_link = dev->data->dev_link;
1594         }
1595 }
1596
1597 void
1598 rte_eth_link_get_nowait(uint16_t port_id, struct rte_eth_link *eth_link)
1599 {
1600         struct rte_eth_dev *dev;
1601
1602         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1603         dev = &rte_eth_devices[port_id];
1604
1605         if (dev->data->dev_conf.intr_conf.lsc != 0)
1606                 rte_eth_dev_atomic_read_link_status(dev, eth_link);
1607         else {
1608                 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
1609                 (*dev->dev_ops->link_update)(dev, 0);
1610                 *eth_link = dev->data->dev_link;
1611         }
1612 }
1613
1614 int
1615 rte_eth_stats_get(uint16_t port_id, struct rte_eth_stats *stats)
1616 {
1617         struct rte_eth_dev *dev;
1618
1619         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1620
1621         dev = &rte_eth_devices[port_id];
1622         memset(stats, 0, sizeof(*stats));
1623
1624         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
1625         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
1626         return (*dev->dev_ops->stats_get)(dev, stats);
1627 }
1628
1629 int
1630 rte_eth_stats_reset(uint16_t port_id)
1631 {
1632         struct rte_eth_dev *dev;
1633
1634         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1635         dev = &rte_eth_devices[port_id];
1636
1637         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_reset, -ENOTSUP);
1638         (*dev->dev_ops->stats_reset)(dev);
1639         dev->data->rx_mbuf_alloc_failed = 0;
1640
1641         return 0;
1642 }
1643
1644 static inline int
1645 get_xstats_basic_count(struct rte_eth_dev *dev)
1646 {
1647         uint16_t nb_rxqs, nb_txqs;
1648         int count;
1649
1650         nb_rxqs = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
1651         nb_txqs = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
1652
1653         count = RTE_NB_STATS;
1654         count += nb_rxqs * RTE_NB_RXQ_STATS;
1655         count += nb_txqs * RTE_NB_TXQ_STATS;
1656
1657         return count;
1658 }
1659
1660 static int
1661 get_xstats_count(uint16_t port_id)
1662 {
1663         struct rte_eth_dev *dev;
1664         int count;
1665
1666         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1667         dev = &rte_eth_devices[port_id];
1668         if (dev->dev_ops->xstats_get_names_by_id != NULL) {
1669                 count = (*dev->dev_ops->xstats_get_names_by_id)(dev, NULL,
1670                                 NULL, 0);
1671                 if (count < 0)
1672                         return count;
1673         }
1674         if (dev->dev_ops->xstats_get_names != NULL) {
1675                 count = (*dev->dev_ops->xstats_get_names)(dev, NULL, 0);
1676                 if (count < 0)
1677                         return count;
1678         } else
1679                 count = 0;
1680
1681
1682         count += get_xstats_basic_count(dev);
1683
1684         return count;
1685 }
1686
1687 int
1688 rte_eth_xstats_get_id_by_name(uint16_t port_id, const char *xstat_name,
1689                 uint64_t *id)
1690 {
1691         int cnt_xstats, idx_xstat;
1692
1693         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1694
1695         if (!id) {
1696                 RTE_PMD_DEBUG_TRACE("Error: id pointer is NULL\n");
1697                 return -ENOMEM;
1698         }
1699
1700         if (!xstat_name) {
1701                 RTE_PMD_DEBUG_TRACE("Error: xstat_name pointer is NULL\n");
1702                 return -ENOMEM;
1703         }
1704
1705         /* Get count */
1706         cnt_xstats = rte_eth_xstats_get_names_by_id(port_id, NULL, 0, NULL);
1707         if (cnt_xstats  < 0) {
1708                 RTE_PMD_DEBUG_TRACE("Error: Cannot get count of xstats\n");
1709                 return -ENODEV;
1710         }
1711
1712         /* Get id-name lookup table */
1713         struct rte_eth_xstat_name xstats_names[cnt_xstats];
1714
1715         if (cnt_xstats != rte_eth_xstats_get_names_by_id(
1716                         port_id, xstats_names, cnt_xstats, NULL)) {
1717                 RTE_PMD_DEBUG_TRACE("Error: Cannot get xstats lookup\n");
1718                 return -1;
1719         }
1720
1721         for (idx_xstat = 0; idx_xstat < cnt_xstats; idx_xstat++) {
1722                 if (!strcmp(xstats_names[idx_xstat].name, xstat_name)) {
1723                         *id = idx_xstat;
1724                         return 0;
1725                 };
1726         }
1727
1728         return -EINVAL;
1729 }
1730
1731 /* retrieve basic stats names */
1732 static int
1733 rte_eth_basic_stats_get_names(struct rte_eth_dev *dev,
1734         struct rte_eth_xstat_name *xstats_names)
1735 {
1736         int cnt_used_entries = 0;
1737         uint32_t idx, id_queue;
1738         uint16_t num_q;
1739
1740         for (idx = 0; idx < RTE_NB_STATS; idx++) {
1741                 snprintf(xstats_names[cnt_used_entries].name,
1742                         sizeof(xstats_names[0].name),
1743                         "%s", rte_stats_strings[idx].name);
1744                 cnt_used_entries++;
1745         }
1746         num_q = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
1747         for (id_queue = 0; id_queue < num_q; id_queue++) {
1748                 for (idx = 0; idx < RTE_NB_RXQ_STATS; idx++) {
1749                         snprintf(xstats_names[cnt_used_entries].name,
1750                                 sizeof(xstats_names[0].name),
1751                                 "rx_q%u%s",
1752                                 id_queue, rte_rxq_stats_strings[idx].name);
1753                         cnt_used_entries++;
1754                 }
1755
1756         }
1757         num_q = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
1758         for (id_queue = 0; id_queue < num_q; id_queue++) {
1759                 for (idx = 0; idx < RTE_NB_TXQ_STATS; idx++) {
1760                         snprintf(xstats_names[cnt_used_entries].name,
1761                                 sizeof(xstats_names[0].name),
1762                                 "tx_q%u%s",
1763                                 id_queue, rte_txq_stats_strings[idx].name);
1764                         cnt_used_entries++;
1765                 }
1766         }
1767         return cnt_used_entries;
1768 }
1769
1770 /* retrieve ethdev extended statistics names */
1771 int
1772 rte_eth_xstats_get_names_by_id(uint16_t port_id,
1773         struct rte_eth_xstat_name *xstats_names, unsigned int size,
1774         uint64_t *ids)
1775 {
1776         struct rte_eth_xstat_name *xstats_names_copy;
1777         unsigned int no_basic_stat_requested = 1;
1778         unsigned int no_ext_stat_requested = 1;
1779         unsigned int expected_entries;
1780         unsigned int basic_count;
1781         struct rte_eth_dev *dev;
1782         unsigned int i;
1783         int ret;
1784
1785         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1786         dev = &rte_eth_devices[port_id];
1787
1788         basic_count = get_xstats_basic_count(dev);
1789         ret = get_xstats_count(port_id);
1790         if (ret < 0)
1791                 return ret;
1792         expected_entries = (unsigned int)ret;
1793
1794         /* Return max number of stats if no ids given */
1795         if (!ids) {
1796                 if (!xstats_names)
1797                         return expected_entries;
1798                 else if (xstats_names && size < expected_entries)
1799                         return expected_entries;
1800         }
1801
1802         if (ids && !xstats_names)
1803                 return -EINVAL;
1804
1805         if (ids && dev->dev_ops->xstats_get_names_by_id != NULL && size > 0) {
1806                 uint64_t ids_copy[size];
1807
1808                 for (i = 0; i < size; i++) {
1809                         if (ids[i] < basic_count) {
1810                                 no_basic_stat_requested = 0;
1811                                 break;
1812                         }
1813
1814                         /*
1815                          * Convert ids to xstats ids that PMD knows.
1816                          * ids known by user are basic + extended stats.
1817                          */
1818                         ids_copy[i] = ids[i] - basic_count;
1819                 }
1820
1821                 if (no_basic_stat_requested)
1822                         return (*dev->dev_ops->xstats_get_names_by_id)(dev,
1823                                         xstats_names, ids_copy, size);
1824         }
1825
1826         /* Retrieve all stats */
1827         if (!ids) {
1828                 int num_stats = rte_eth_xstats_get_names(port_id, xstats_names,
1829                                 expected_entries);
1830                 if (num_stats < 0 || num_stats > (int)expected_entries)
1831                         return num_stats;
1832                 else
1833                         return expected_entries;
1834         }
1835
1836         xstats_names_copy = calloc(expected_entries,
1837                 sizeof(struct rte_eth_xstat_name));
1838
1839         if (!xstats_names_copy) {
1840                 RTE_PMD_DEBUG_TRACE("ERROR: can't allocate memory");
1841                 return -ENOMEM;
1842         }
1843
1844         if (ids) {
1845                 for (i = 0; i < size; i++) {
1846                         if (ids[i] > basic_count) {
1847                                 no_ext_stat_requested = 0;
1848                                 break;
1849                         }
1850                 }
1851         }
1852
1853         /* Fill xstats_names_copy structure */
1854         if (ids && no_ext_stat_requested) {
1855                 rte_eth_basic_stats_get_names(dev, xstats_names_copy);
1856         } else {
1857                 rte_eth_xstats_get_names(port_id, xstats_names_copy,
1858                         expected_entries);
1859         }
1860
1861         /* Filter stats */
1862         for (i = 0; i < size; i++) {
1863                 if (ids[i] >= expected_entries) {
1864                         RTE_PMD_DEBUG_TRACE("ERROR: id value isn't valid\n");
1865                         free(xstats_names_copy);
1866                         return -1;
1867                 }
1868                 xstats_names[i] = xstats_names_copy[ids[i]];
1869         }
1870
1871         free(xstats_names_copy);
1872         return size;
1873 }
1874
1875 int
1876 rte_eth_xstats_get_names(uint16_t port_id,
1877         struct rte_eth_xstat_name *xstats_names,
1878         unsigned int size)
1879 {
1880         struct rte_eth_dev *dev;
1881         int cnt_used_entries;
1882         int cnt_expected_entries;
1883         int cnt_driver_entries;
1884
1885         cnt_expected_entries = get_xstats_count(port_id);
1886         if (xstats_names == NULL || cnt_expected_entries < 0 ||
1887                         (int)size < cnt_expected_entries)
1888                 return cnt_expected_entries;
1889
1890         /* port_id checked in get_xstats_count() */
1891         dev = &rte_eth_devices[port_id];
1892
1893         cnt_used_entries = rte_eth_basic_stats_get_names(
1894                 dev, xstats_names);
1895
1896         if (dev->dev_ops->xstats_get_names != NULL) {
1897                 /* If there are any driver-specific xstats, append them
1898                  * to end of list.
1899                  */
1900                 cnt_driver_entries = (*dev->dev_ops->xstats_get_names)(
1901                         dev,
1902                         xstats_names + cnt_used_entries,
1903                         size - cnt_used_entries);
1904                 if (cnt_driver_entries < 0)
1905                         return cnt_driver_entries;
1906                 cnt_used_entries += cnt_driver_entries;
1907         }
1908
1909         return cnt_used_entries;
1910 }
1911
1912
1913 static int
1914 rte_eth_basic_stats_get(uint16_t port_id, struct rte_eth_xstat *xstats)
1915 {
1916         struct rte_eth_dev *dev;
1917         struct rte_eth_stats eth_stats;
1918         unsigned int count = 0, i, q;
1919         uint64_t val, *stats_ptr;
1920         uint16_t nb_rxqs, nb_txqs;
1921
1922         rte_eth_stats_get(port_id, &eth_stats);
1923         dev = &rte_eth_devices[port_id];
1924
1925         nb_rxqs = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
1926         nb_txqs = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
1927
1928         /* global stats */
1929         for (i = 0; i < RTE_NB_STATS; i++) {
1930                 stats_ptr = RTE_PTR_ADD(&eth_stats,
1931                                         rte_stats_strings[i].offset);
1932                 val = *stats_ptr;
1933                 xstats[count++].value = val;
1934         }
1935
1936         /* per-rxq stats */
1937         for (q = 0; q < nb_rxqs; q++) {
1938                 for (i = 0; i < RTE_NB_RXQ_STATS; i++) {
1939                         stats_ptr = RTE_PTR_ADD(&eth_stats,
1940                                         rte_rxq_stats_strings[i].offset +
1941                                         q * sizeof(uint64_t));
1942                         val = *stats_ptr;
1943                         xstats[count++].value = val;
1944                 }
1945         }
1946
1947         /* per-txq stats */
1948         for (q = 0; q < nb_txqs; q++) {
1949                 for (i = 0; i < RTE_NB_TXQ_STATS; i++) {
1950                         stats_ptr = RTE_PTR_ADD(&eth_stats,
1951                                         rte_txq_stats_strings[i].offset +
1952                                         q * sizeof(uint64_t));
1953                         val = *stats_ptr;
1954                         xstats[count++].value = val;
1955                 }
1956         }
1957         return count;
1958 }
1959
1960 /* retrieve ethdev extended statistics */
1961 int
1962 rte_eth_xstats_get_by_id(uint16_t port_id, const uint64_t *ids,
1963                          uint64_t *values, unsigned int size)
1964 {
1965         unsigned int no_basic_stat_requested = 1;
1966         unsigned int no_ext_stat_requested = 1;
1967         unsigned int num_xstats_filled;
1968         unsigned int basic_count;
1969         uint16_t expected_entries;
1970         struct rte_eth_dev *dev;
1971         unsigned int i;
1972         int ret;
1973
1974         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1975         expected_entries = get_xstats_count(port_id);
1976         struct rte_eth_xstat xstats[expected_entries];
1977         dev = &rte_eth_devices[port_id];
1978         basic_count = get_xstats_basic_count(dev);
1979
1980         /* Return max number of stats if no ids given */
1981         if (!ids) {
1982                 if (!values)
1983                         return expected_entries;
1984                 else if (values && size < expected_entries)
1985                         return expected_entries;
1986         }
1987
1988         if (ids && !values)
1989                 return -EINVAL;
1990
1991         if (ids && dev->dev_ops->xstats_get_by_id != NULL && size) {
1992                 unsigned int basic_count = get_xstats_basic_count(dev);
1993                 uint64_t ids_copy[size];
1994
1995                 for (i = 0; i < size; i++) {
1996                         if (ids[i] < basic_count) {
1997                                 no_basic_stat_requested = 0;
1998                                 break;
1999                         }
2000
2001                         /*
2002                          * Convert ids to xstats ids that PMD knows.
2003                          * ids known by user are basic + extended stats.
2004                          */
2005                         ids_copy[i] = ids[i] - basic_count;
2006                 }
2007
2008                 if (no_basic_stat_requested)
2009                         return (*dev->dev_ops->xstats_get_by_id)(dev, ids_copy,
2010                                         values, size);
2011         }
2012
2013         if (ids) {
2014                 for (i = 0; i < size; i++) {
2015                         if (ids[i] > basic_count) {
2016                                 no_ext_stat_requested = 0;
2017                                 break;
2018                         }
2019                 }
2020         }
2021
2022         /* Fill the xstats structure */
2023         if (ids && no_ext_stat_requested)
2024                 ret = rte_eth_basic_stats_get(port_id, xstats);
2025         else
2026                 ret = rte_eth_xstats_get(port_id, xstats, expected_entries);
2027
2028         if (ret < 0)
2029                 return ret;
2030         num_xstats_filled = (unsigned int)ret;
2031
2032         /* Return all stats */
2033         if (!ids) {
2034                 for (i = 0; i < num_xstats_filled; i++)
2035                         values[i] = xstats[i].value;
2036                 return expected_entries;
2037         }
2038
2039         /* Filter stats */
2040         for (i = 0; i < size; i++) {
2041                 if (ids[i] >= expected_entries) {
2042                         RTE_PMD_DEBUG_TRACE("ERROR: id value isn't valid\n");
2043                         return -1;
2044                 }
2045                 values[i] = xstats[ids[i]].value;
2046         }
2047         return size;
2048 }
2049
2050 int
2051 rte_eth_xstats_get(uint16_t port_id, struct rte_eth_xstat *xstats,
2052         unsigned int n)
2053 {
2054         struct rte_eth_dev *dev;
2055         unsigned int count = 0, i;
2056         signed int xcount = 0;
2057         uint16_t nb_rxqs, nb_txqs;
2058
2059         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
2060
2061         dev = &rte_eth_devices[port_id];
2062
2063         nb_rxqs = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
2064         nb_txqs = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
2065
2066         /* Return generic statistics */
2067         count = RTE_NB_STATS + (nb_rxqs * RTE_NB_RXQ_STATS) +
2068                 (nb_txqs * RTE_NB_TXQ_STATS);
2069
2070         /* implemented by the driver */
2071         if (dev->dev_ops->xstats_get != NULL) {
2072                 /* Retrieve the xstats from the driver at the end of the
2073                  * xstats struct.
2074                  */
2075                 xcount = (*dev->dev_ops->xstats_get)(dev,
2076                                      xstats ? xstats + count : NULL,
2077                                      (n > count) ? n - count : 0);
2078
2079                 if (xcount < 0)
2080                         return xcount;
2081         }
2082
2083         if (n < count + xcount || xstats == NULL)
2084                 return count + xcount;
2085
2086         /* now fill the xstats structure */
2087         count = rte_eth_basic_stats_get(port_id, xstats);
2088
2089         for (i = 0; i < count; i++)
2090                 xstats[i].id = i;
2091         /* add an offset to driver-specific stats */
2092         for ( ; i < count + xcount; i++)
2093                 xstats[i].id += count;
2094
2095         return count + xcount;
2096 }
2097
2098 /* reset ethdev extended statistics */
2099 void
2100 rte_eth_xstats_reset(uint16_t port_id)
2101 {
2102         struct rte_eth_dev *dev;
2103
2104         RTE_ETH_VALID_PORTID_OR_RET(port_id);
2105         dev = &rte_eth_devices[port_id];
2106
2107         /* implemented by the driver */
2108         if (dev->dev_ops->xstats_reset != NULL) {
2109                 (*dev->dev_ops->xstats_reset)(dev);
2110                 return;
2111         }
2112
2113         /* fallback to default */
2114         rte_eth_stats_reset(port_id);
2115 }
2116
2117 static int
2118 set_queue_stats_mapping(uint16_t port_id, uint16_t queue_id, uint8_t stat_idx,
2119                 uint8_t is_rx)
2120 {
2121         struct rte_eth_dev *dev;
2122
2123         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2124
2125         dev = &rte_eth_devices[port_id];
2126
2127         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_stats_mapping_set, -ENOTSUP);
2128         return (*dev->dev_ops->queue_stats_mapping_set)
2129                         (dev, queue_id, stat_idx, is_rx);
2130 }
2131
2132
2133 int
2134 rte_eth_dev_set_tx_queue_stats_mapping(uint16_t port_id, uint16_t tx_queue_id,
2135                 uint8_t stat_idx)
2136 {
2137         return set_queue_stats_mapping(port_id, tx_queue_id, stat_idx,
2138                         STAT_QMAP_TX);
2139 }
2140
2141
2142 int
2143 rte_eth_dev_set_rx_queue_stats_mapping(uint16_t port_id, uint16_t rx_queue_id,
2144                 uint8_t stat_idx)
2145 {
2146         return set_queue_stats_mapping(port_id, rx_queue_id, stat_idx,
2147                         STAT_QMAP_RX);
2148 }
2149
2150 int
2151 rte_eth_dev_fw_version_get(uint16_t port_id, char *fw_version, size_t fw_size)
2152 {
2153         struct rte_eth_dev *dev;
2154
2155         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2156         dev = &rte_eth_devices[port_id];
2157
2158         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fw_version_get, -ENOTSUP);
2159         return (*dev->dev_ops->fw_version_get)(dev, fw_version, fw_size);
2160 }
2161
2162 void
2163 rte_eth_dev_info_get(uint16_t port_id, struct rte_eth_dev_info *dev_info)
2164 {
2165         struct rte_eth_dev *dev;
2166         const struct rte_eth_desc_lim lim = {
2167                 .nb_max = UINT16_MAX,
2168                 .nb_min = 0,
2169                 .nb_align = 1,
2170         };
2171
2172         RTE_ETH_VALID_PORTID_OR_RET(port_id);
2173         dev = &rte_eth_devices[port_id];
2174
2175         memset(dev_info, 0, sizeof(struct rte_eth_dev_info));
2176         dev_info->rx_desc_lim = lim;
2177         dev_info->tx_desc_lim = lim;
2178
2179         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
2180         (*dev->dev_ops->dev_infos_get)(dev, dev_info);
2181         dev_info->driver_name = dev->device->driver->name;
2182         dev_info->nb_rx_queues = dev->data->nb_rx_queues;
2183         dev_info->nb_tx_queues = dev->data->nb_tx_queues;
2184 }
2185
2186 int
2187 rte_eth_dev_get_supported_ptypes(uint16_t port_id, uint32_t ptype_mask,
2188                                  uint32_t *ptypes, int num)
2189 {
2190         int i, j;
2191         struct rte_eth_dev *dev;
2192         const uint32_t *all_ptypes;
2193
2194         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2195         dev = &rte_eth_devices[port_id];
2196         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_supported_ptypes_get, 0);
2197         all_ptypes = (*dev->dev_ops->dev_supported_ptypes_get)(dev);
2198
2199         if (!all_ptypes)
2200                 return 0;
2201
2202         for (i = 0, j = 0; all_ptypes[i] != RTE_PTYPE_UNKNOWN; ++i)
2203                 if (all_ptypes[i] & ptype_mask) {
2204                         if (j < num)
2205                                 ptypes[j] = all_ptypes[i];
2206                         j++;
2207                 }
2208
2209         return j;
2210 }
2211
2212 void
2213 rte_eth_macaddr_get(uint16_t port_id, struct ether_addr *mac_addr)
2214 {
2215         struct rte_eth_dev *dev;
2216
2217         RTE_ETH_VALID_PORTID_OR_RET(port_id);
2218         dev = &rte_eth_devices[port_id];
2219         ether_addr_copy(&dev->data->mac_addrs[0], mac_addr);
2220 }
2221
2222
2223 int
2224 rte_eth_dev_get_mtu(uint16_t port_id, uint16_t *mtu)
2225 {
2226         struct rte_eth_dev *dev;
2227
2228         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2229
2230         dev = &rte_eth_devices[port_id];
2231         *mtu = dev->data->mtu;
2232         return 0;
2233 }
2234
2235 int
2236 rte_eth_dev_set_mtu(uint16_t port_id, uint16_t mtu)
2237 {
2238         int ret;
2239         struct rte_eth_dev *dev;
2240
2241         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2242         dev = &rte_eth_devices[port_id];
2243         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mtu_set, -ENOTSUP);
2244
2245         ret = (*dev->dev_ops->mtu_set)(dev, mtu);
2246         if (!ret)
2247                 dev->data->mtu = mtu;
2248
2249         return ret;
2250 }
2251
2252 int
2253 rte_eth_dev_vlan_filter(uint16_t port_id, uint16_t vlan_id, int on)
2254 {
2255         struct rte_eth_dev *dev;
2256         int ret;
2257
2258         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2259         dev = &rte_eth_devices[port_id];
2260         if (!(dev->data->dev_conf.rxmode.offloads &
2261               DEV_RX_OFFLOAD_VLAN_FILTER)) {
2262                 RTE_PMD_DEBUG_TRACE("port %d: vlan-filtering disabled\n", port_id);
2263                 return -ENOSYS;
2264         }
2265
2266         if (vlan_id > 4095) {
2267                 RTE_PMD_DEBUG_TRACE("(port_id=%d) invalid vlan_id=%u > 4095\n",
2268                                 port_id, (unsigned) vlan_id);
2269                 return -EINVAL;
2270         }
2271         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_filter_set, -ENOTSUP);
2272
2273         ret = (*dev->dev_ops->vlan_filter_set)(dev, vlan_id, on);
2274         if (ret == 0) {
2275                 struct rte_vlan_filter_conf *vfc;
2276                 int vidx;
2277                 int vbit;
2278
2279                 vfc = &dev->data->vlan_filter_conf;
2280                 vidx = vlan_id / 64;
2281                 vbit = vlan_id % 64;
2282
2283                 if (on)
2284                         vfc->ids[vidx] |= UINT64_C(1) << vbit;
2285                 else
2286                         vfc->ids[vidx] &= ~(UINT64_C(1) << vbit);
2287         }
2288
2289         return ret;
2290 }
2291
2292 int
2293 rte_eth_dev_set_vlan_strip_on_queue(uint16_t port_id, uint16_t rx_queue_id,
2294                                     int on)
2295 {
2296         struct rte_eth_dev *dev;
2297
2298         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2299         dev = &rte_eth_devices[port_id];
2300         if (rx_queue_id >= dev->data->nb_rx_queues) {
2301                 RTE_PMD_DEBUG_TRACE("Invalid rx_queue_id=%d\n", port_id);
2302                 return -EINVAL;
2303         }
2304
2305         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_strip_queue_set, -ENOTSUP);
2306         (*dev->dev_ops->vlan_strip_queue_set)(dev, rx_queue_id, on);
2307
2308         return 0;
2309 }
2310
2311 int
2312 rte_eth_dev_set_vlan_ether_type(uint16_t port_id,
2313                                 enum rte_vlan_type vlan_type,
2314                                 uint16_t tpid)
2315 {
2316         struct rte_eth_dev *dev;
2317
2318         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2319         dev = &rte_eth_devices[port_id];
2320         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_tpid_set, -ENOTSUP);
2321
2322         return (*dev->dev_ops->vlan_tpid_set)(dev, vlan_type, tpid);
2323 }
2324
2325 int
2326 rte_eth_dev_set_vlan_offload(uint16_t port_id, int offload_mask)
2327 {
2328         struct rte_eth_dev *dev;
2329         int ret = 0;
2330         int mask = 0;
2331         int cur, org = 0;
2332         uint64_t orig_offloads;
2333
2334         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2335         dev = &rte_eth_devices[port_id];
2336
2337         /* save original values in case of failure */
2338         orig_offloads = dev->data->dev_conf.rxmode.offloads;
2339
2340         /*check which option changed by application*/
2341         cur = !!(offload_mask & ETH_VLAN_STRIP_OFFLOAD);
2342         org = !!(dev->data->dev_conf.rxmode.offloads &
2343                  DEV_RX_OFFLOAD_VLAN_STRIP);
2344         if (cur != org) {
2345                 if (cur)
2346                         dev->data->dev_conf.rxmode.offloads |=
2347                                 DEV_RX_OFFLOAD_VLAN_STRIP;
2348                 else
2349                         dev->data->dev_conf.rxmode.offloads &=
2350                                 ~DEV_RX_OFFLOAD_VLAN_STRIP;
2351                 mask |= ETH_VLAN_STRIP_MASK;
2352         }
2353
2354         cur = !!(offload_mask & ETH_VLAN_FILTER_OFFLOAD);
2355         org = !!(dev->data->dev_conf.rxmode.offloads &
2356                  DEV_RX_OFFLOAD_VLAN_FILTER);
2357         if (cur != org) {
2358                 if (cur)
2359                         dev->data->dev_conf.rxmode.offloads |=
2360                                 DEV_RX_OFFLOAD_VLAN_FILTER;
2361                 else
2362                         dev->data->dev_conf.rxmode.offloads &=
2363                                 ~DEV_RX_OFFLOAD_VLAN_FILTER;
2364                 mask |= ETH_VLAN_FILTER_MASK;
2365         }
2366
2367         cur = !!(offload_mask & ETH_VLAN_EXTEND_OFFLOAD);
2368         org = !!(dev->data->dev_conf.rxmode.offloads &
2369                  DEV_RX_OFFLOAD_VLAN_EXTEND);
2370         if (cur != org) {
2371                 if (cur)
2372                         dev->data->dev_conf.rxmode.offloads |=
2373                                 DEV_RX_OFFLOAD_VLAN_EXTEND;
2374                 else
2375                         dev->data->dev_conf.rxmode.offloads &=
2376                                 ~DEV_RX_OFFLOAD_VLAN_EXTEND;
2377                 mask |= ETH_VLAN_EXTEND_MASK;
2378         }
2379
2380         /*no change*/
2381         if (mask == 0)
2382                 return ret;
2383
2384         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_offload_set, -ENOTSUP);
2385
2386         /*
2387          * Convert to the offload bitfield API just in case the underlying PMD
2388          * still supporting it.
2389          */
2390         rte_eth_convert_rx_offloads(dev->data->dev_conf.rxmode.offloads,
2391                                     &dev->data->dev_conf.rxmode);
2392         ret = (*dev->dev_ops->vlan_offload_set)(dev, mask);
2393         if (ret) {
2394                 /* hit an error restore  original values */
2395                 dev->data->dev_conf.rxmode.offloads = orig_offloads;
2396                 rte_eth_convert_rx_offloads(dev->data->dev_conf.rxmode.offloads,
2397                                             &dev->data->dev_conf.rxmode);
2398         }
2399
2400         return ret;
2401 }
2402
2403 int
2404 rte_eth_dev_get_vlan_offload(uint16_t port_id)
2405 {
2406         struct rte_eth_dev *dev;
2407         int ret = 0;
2408
2409         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2410         dev = &rte_eth_devices[port_id];
2411
2412         if (dev->data->dev_conf.rxmode.offloads &
2413             DEV_RX_OFFLOAD_VLAN_STRIP)
2414                 ret |= ETH_VLAN_STRIP_OFFLOAD;
2415
2416         if (dev->data->dev_conf.rxmode.offloads &
2417             DEV_RX_OFFLOAD_VLAN_FILTER)
2418                 ret |= ETH_VLAN_FILTER_OFFLOAD;
2419
2420         if (dev->data->dev_conf.rxmode.offloads &
2421             DEV_RX_OFFLOAD_VLAN_EXTEND)
2422                 ret |= ETH_VLAN_EXTEND_OFFLOAD;
2423
2424         return ret;
2425 }
2426
2427 int
2428 rte_eth_dev_set_vlan_pvid(uint16_t port_id, uint16_t pvid, int on)
2429 {
2430         struct rte_eth_dev *dev;
2431
2432         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2433         dev = &rte_eth_devices[port_id];
2434         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_pvid_set, -ENOTSUP);
2435         (*dev->dev_ops->vlan_pvid_set)(dev, pvid, on);
2436
2437         return 0;
2438 }
2439
2440 int
2441 rte_eth_dev_flow_ctrl_get(uint16_t port_id, struct rte_eth_fc_conf *fc_conf)
2442 {
2443         struct rte_eth_dev *dev;
2444
2445         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2446         dev = &rte_eth_devices[port_id];
2447         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_get, -ENOTSUP);
2448         memset(fc_conf, 0, sizeof(*fc_conf));
2449         return (*dev->dev_ops->flow_ctrl_get)(dev, fc_conf);
2450 }
2451
2452 int
2453 rte_eth_dev_flow_ctrl_set(uint16_t port_id, struct rte_eth_fc_conf *fc_conf)
2454 {
2455         struct rte_eth_dev *dev;
2456
2457         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2458         if ((fc_conf->send_xon != 0) && (fc_conf->send_xon != 1)) {
2459                 RTE_PMD_DEBUG_TRACE("Invalid send_xon, only 0/1 allowed\n");
2460                 return -EINVAL;
2461         }
2462
2463         dev = &rte_eth_devices[port_id];
2464         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_set, -ENOTSUP);
2465         return (*dev->dev_ops->flow_ctrl_set)(dev, fc_conf);
2466 }
2467
2468 int
2469 rte_eth_dev_priority_flow_ctrl_set(uint16_t port_id,
2470                                    struct rte_eth_pfc_conf *pfc_conf)
2471 {
2472         struct rte_eth_dev *dev;
2473
2474         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2475         if (pfc_conf->priority > (ETH_DCB_NUM_USER_PRIORITIES - 1)) {
2476                 RTE_PMD_DEBUG_TRACE("Invalid priority, only 0-7 allowed\n");
2477                 return -EINVAL;
2478         }
2479
2480         dev = &rte_eth_devices[port_id];
2481         /* High water, low water validation are device specific */
2482         if  (*dev->dev_ops->priority_flow_ctrl_set)
2483                 return (*dev->dev_ops->priority_flow_ctrl_set)(dev, pfc_conf);
2484         return -ENOTSUP;
2485 }
2486
2487 static int
2488 rte_eth_check_reta_mask(struct rte_eth_rss_reta_entry64 *reta_conf,
2489                         uint16_t reta_size)
2490 {
2491         uint16_t i, num;
2492
2493         if (!reta_conf)
2494                 return -EINVAL;
2495
2496         num = (reta_size + RTE_RETA_GROUP_SIZE - 1) / RTE_RETA_GROUP_SIZE;
2497         for (i = 0; i < num; i++) {
2498                 if (reta_conf[i].mask)
2499                         return 0;
2500         }
2501
2502         return -EINVAL;
2503 }
2504
2505 static int
2506 rte_eth_check_reta_entry(struct rte_eth_rss_reta_entry64 *reta_conf,
2507                          uint16_t reta_size,
2508                          uint16_t max_rxq)
2509 {
2510         uint16_t i, idx, shift;
2511
2512         if (!reta_conf)
2513                 return -EINVAL;
2514
2515         if (max_rxq == 0) {
2516                 RTE_PMD_DEBUG_TRACE("No receive queue is available\n");
2517                 return -EINVAL;
2518         }
2519
2520         for (i = 0; i < reta_size; i++) {
2521                 idx = i / RTE_RETA_GROUP_SIZE;
2522                 shift = i % RTE_RETA_GROUP_SIZE;
2523                 if ((reta_conf[idx].mask & (1ULL << shift)) &&
2524                         (reta_conf[idx].reta[shift] >= max_rxq)) {
2525                         RTE_PMD_DEBUG_TRACE("reta_conf[%u]->reta[%u]: %u exceeds "
2526                                 "the maximum rxq index: %u\n", idx, shift,
2527                                 reta_conf[idx].reta[shift], max_rxq);
2528                         return -EINVAL;
2529                 }
2530         }
2531
2532         return 0;
2533 }
2534
2535 int
2536 rte_eth_dev_rss_reta_update(uint16_t port_id,
2537                             struct rte_eth_rss_reta_entry64 *reta_conf,
2538                             uint16_t reta_size)
2539 {
2540         struct rte_eth_dev *dev;
2541         int ret;
2542
2543         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2544         /* Check mask bits */
2545         ret = rte_eth_check_reta_mask(reta_conf, reta_size);
2546         if (ret < 0)
2547                 return ret;
2548
2549         dev = &rte_eth_devices[port_id];
2550
2551         /* Check entry value */
2552         ret = rte_eth_check_reta_entry(reta_conf, reta_size,
2553                                 dev->data->nb_rx_queues);
2554         if (ret < 0)
2555                 return ret;
2556
2557         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->reta_update, -ENOTSUP);
2558         return (*dev->dev_ops->reta_update)(dev, reta_conf, reta_size);
2559 }
2560
2561 int
2562 rte_eth_dev_rss_reta_query(uint16_t port_id,
2563                            struct rte_eth_rss_reta_entry64 *reta_conf,
2564                            uint16_t reta_size)
2565 {
2566         struct rte_eth_dev *dev;
2567         int ret;
2568
2569         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2570
2571         /* Check mask bits */
2572         ret = rte_eth_check_reta_mask(reta_conf, reta_size);
2573         if (ret < 0)
2574                 return ret;
2575
2576         dev = &rte_eth_devices[port_id];
2577         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->reta_query, -ENOTSUP);
2578         return (*dev->dev_ops->reta_query)(dev, reta_conf, reta_size);
2579 }
2580
2581 int
2582 rte_eth_dev_rss_hash_update(uint16_t port_id,
2583                             struct rte_eth_rss_conf *rss_conf)
2584 {
2585         struct rte_eth_dev *dev;
2586
2587         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2588         dev = &rte_eth_devices[port_id];
2589         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rss_hash_update, -ENOTSUP);
2590         return (*dev->dev_ops->rss_hash_update)(dev, rss_conf);
2591 }
2592
2593 int
2594 rte_eth_dev_rss_hash_conf_get(uint16_t port_id,
2595                               struct rte_eth_rss_conf *rss_conf)
2596 {
2597         struct rte_eth_dev *dev;
2598
2599         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2600         dev = &rte_eth_devices[port_id];
2601         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rss_hash_conf_get, -ENOTSUP);
2602         return (*dev->dev_ops->rss_hash_conf_get)(dev, rss_conf);
2603 }
2604
2605 int
2606 rte_eth_dev_udp_tunnel_port_add(uint16_t port_id,
2607                                 struct rte_eth_udp_tunnel *udp_tunnel)
2608 {
2609         struct rte_eth_dev *dev;
2610
2611         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2612         if (udp_tunnel == NULL) {
2613                 RTE_PMD_DEBUG_TRACE("Invalid udp_tunnel parameter\n");
2614                 return -EINVAL;
2615         }
2616
2617         if (udp_tunnel->prot_type >= RTE_TUNNEL_TYPE_MAX) {
2618                 RTE_PMD_DEBUG_TRACE("Invalid tunnel type\n");
2619                 return -EINVAL;
2620         }
2621
2622         dev = &rte_eth_devices[port_id];
2623         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->udp_tunnel_port_add, -ENOTSUP);
2624         return (*dev->dev_ops->udp_tunnel_port_add)(dev, udp_tunnel);
2625 }
2626
2627 int
2628 rte_eth_dev_udp_tunnel_port_delete(uint16_t port_id,
2629                                    struct rte_eth_udp_tunnel *udp_tunnel)
2630 {
2631         struct rte_eth_dev *dev;
2632
2633         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2634         dev = &rte_eth_devices[port_id];
2635
2636         if (udp_tunnel == NULL) {
2637                 RTE_PMD_DEBUG_TRACE("Invalid udp_tunnel parameter\n");
2638                 return -EINVAL;
2639         }
2640
2641         if (udp_tunnel->prot_type >= RTE_TUNNEL_TYPE_MAX) {
2642                 RTE_PMD_DEBUG_TRACE("Invalid tunnel type\n");
2643                 return -EINVAL;
2644         }
2645
2646         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->udp_tunnel_port_del, -ENOTSUP);
2647         return (*dev->dev_ops->udp_tunnel_port_del)(dev, udp_tunnel);
2648 }
2649
2650 int
2651 rte_eth_led_on(uint16_t port_id)
2652 {
2653         struct rte_eth_dev *dev;
2654
2655         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2656         dev = &rte_eth_devices[port_id];
2657         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_on, -ENOTSUP);
2658         return (*dev->dev_ops->dev_led_on)(dev);
2659 }
2660
2661 int
2662 rte_eth_led_off(uint16_t port_id)
2663 {
2664         struct rte_eth_dev *dev;
2665
2666         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2667         dev = &rte_eth_devices[port_id];
2668         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_off, -ENOTSUP);
2669         return (*dev->dev_ops->dev_led_off)(dev);
2670 }
2671
2672 /*
2673  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
2674  * an empty spot.
2675  */
2676 static int
2677 get_mac_addr_index(uint16_t port_id, const struct ether_addr *addr)
2678 {
2679         struct rte_eth_dev_info dev_info;
2680         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
2681         unsigned i;
2682
2683         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2684         rte_eth_dev_info_get(port_id, &dev_info);
2685
2686         for (i = 0; i < dev_info.max_mac_addrs; i++)
2687                 if (memcmp(addr, &dev->data->mac_addrs[i], ETHER_ADDR_LEN) == 0)
2688                         return i;
2689
2690         return -1;
2691 }
2692
2693 static const struct ether_addr null_mac_addr;
2694
2695 int
2696 rte_eth_dev_mac_addr_add(uint16_t port_id, struct ether_addr *addr,
2697                         uint32_t pool)
2698 {
2699         struct rte_eth_dev *dev;
2700         int index;
2701         uint64_t pool_mask;
2702         int ret;
2703
2704         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2705         dev = &rte_eth_devices[port_id];
2706         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_add, -ENOTSUP);
2707
2708         if (is_zero_ether_addr(addr)) {
2709                 RTE_PMD_DEBUG_TRACE("port %d: Cannot add NULL MAC address\n",
2710                         port_id);
2711                 return -EINVAL;
2712         }
2713         if (pool >= ETH_64_POOLS) {
2714                 RTE_PMD_DEBUG_TRACE("pool id must be 0-%d\n", ETH_64_POOLS - 1);
2715                 return -EINVAL;
2716         }
2717
2718         index = get_mac_addr_index(port_id, addr);
2719         if (index < 0) {
2720                 index = get_mac_addr_index(port_id, &null_mac_addr);
2721                 if (index < 0) {
2722                         RTE_PMD_DEBUG_TRACE("port %d: MAC address array full\n",
2723                                 port_id);
2724                         return -ENOSPC;
2725                 }
2726         } else {
2727                 pool_mask = dev->data->mac_pool_sel[index];
2728
2729                 /* Check if both MAC address and pool is already there, and do nothing */
2730                 if (pool_mask & (1ULL << pool))
2731                         return 0;
2732         }
2733
2734         /* Update NIC */
2735         ret = (*dev->dev_ops->mac_addr_add)(dev, addr, index, pool);
2736
2737         if (ret == 0) {
2738                 /* Update address in NIC data structure */
2739                 ether_addr_copy(addr, &dev->data->mac_addrs[index]);
2740
2741                 /* Update pool bitmap in NIC data structure */
2742                 dev->data->mac_pool_sel[index] |= (1ULL << pool);
2743         }
2744
2745         return ret;
2746 }
2747
2748 int
2749 rte_eth_dev_mac_addr_remove(uint16_t port_id, struct ether_addr *addr)
2750 {
2751         struct rte_eth_dev *dev;
2752         int index;
2753
2754         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2755         dev = &rte_eth_devices[port_id];
2756         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_remove, -ENOTSUP);
2757
2758         index = get_mac_addr_index(port_id, addr);
2759         if (index == 0) {
2760                 RTE_PMD_DEBUG_TRACE("port %d: Cannot remove default MAC address\n", port_id);
2761                 return -EADDRINUSE;
2762         } else if (index < 0)
2763                 return 0;  /* Do nothing if address wasn't found */
2764
2765         /* Update NIC */
2766         (*dev->dev_ops->mac_addr_remove)(dev, index);
2767
2768         /* Update address in NIC data structure */
2769         ether_addr_copy(&null_mac_addr, &dev->data->mac_addrs[index]);
2770
2771         /* reset pool bitmap */
2772         dev->data->mac_pool_sel[index] = 0;
2773
2774         return 0;
2775 }
2776
2777 int
2778 rte_eth_dev_default_mac_addr_set(uint16_t port_id, struct ether_addr *addr)
2779 {
2780         struct rte_eth_dev *dev;
2781
2782         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2783
2784         if (!is_valid_assigned_ether_addr(addr))
2785                 return -EINVAL;
2786
2787         dev = &rte_eth_devices[port_id];
2788         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_set, -ENOTSUP);
2789
2790         /* Update default address in NIC data structure */
2791         ether_addr_copy(addr, &dev->data->mac_addrs[0]);
2792
2793         (*dev->dev_ops->mac_addr_set)(dev, addr);
2794
2795         return 0;
2796 }
2797
2798
2799 /*
2800  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
2801  * an empty spot.
2802  */
2803 static int
2804 get_hash_mac_addr_index(uint16_t port_id, const struct ether_addr *addr)
2805 {
2806         struct rte_eth_dev_info dev_info;
2807         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
2808         unsigned i;
2809
2810         rte_eth_dev_info_get(port_id, &dev_info);
2811         if (!dev->data->hash_mac_addrs)
2812                 return -1;
2813
2814         for (i = 0; i < dev_info.max_hash_mac_addrs; i++)
2815                 if (memcmp(addr, &dev->data->hash_mac_addrs[i],
2816                         ETHER_ADDR_LEN) == 0)
2817                         return i;
2818
2819         return -1;
2820 }
2821
2822 int
2823 rte_eth_dev_uc_hash_table_set(uint16_t port_id, struct ether_addr *addr,
2824                                 uint8_t on)
2825 {
2826         int index;
2827         int ret;
2828         struct rte_eth_dev *dev;
2829
2830         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2831
2832         dev = &rte_eth_devices[port_id];
2833         if (is_zero_ether_addr(addr)) {
2834                 RTE_PMD_DEBUG_TRACE("port %d: Cannot add NULL MAC address\n",
2835                         port_id);
2836                 return -EINVAL;
2837         }
2838
2839         index = get_hash_mac_addr_index(port_id, addr);
2840         /* Check if it's already there, and do nothing */
2841         if ((index >= 0) && on)
2842                 return 0;
2843
2844         if (index < 0) {
2845                 if (!on) {
2846                         RTE_PMD_DEBUG_TRACE("port %d: the MAC address was not "
2847                                 "set in UTA\n", port_id);
2848                         return -EINVAL;
2849                 }
2850
2851                 index = get_hash_mac_addr_index(port_id, &null_mac_addr);
2852                 if (index < 0) {
2853                         RTE_PMD_DEBUG_TRACE("port %d: MAC address array full\n",
2854                                         port_id);
2855                         return -ENOSPC;
2856                 }
2857         }
2858
2859         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->uc_hash_table_set, -ENOTSUP);
2860         ret = (*dev->dev_ops->uc_hash_table_set)(dev, addr, on);
2861         if (ret == 0) {
2862                 /* Update address in NIC data structure */
2863                 if (on)
2864                         ether_addr_copy(addr,
2865                                         &dev->data->hash_mac_addrs[index]);
2866                 else
2867                         ether_addr_copy(&null_mac_addr,
2868                                         &dev->data->hash_mac_addrs[index]);
2869         }
2870
2871         return ret;
2872 }
2873
2874 int
2875 rte_eth_dev_uc_all_hash_table_set(uint16_t port_id, uint8_t on)
2876 {
2877         struct rte_eth_dev *dev;
2878
2879         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2880
2881         dev = &rte_eth_devices[port_id];
2882
2883         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->uc_all_hash_table_set, -ENOTSUP);
2884         return (*dev->dev_ops->uc_all_hash_table_set)(dev, on);
2885 }
2886
2887 int rte_eth_set_queue_rate_limit(uint16_t port_id, uint16_t queue_idx,
2888                                         uint16_t tx_rate)
2889 {
2890         struct rte_eth_dev *dev;
2891         struct rte_eth_dev_info dev_info;
2892         struct rte_eth_link link;
2893
2894         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2895
2896         dev = &rte_eth_devices[port_id];
2897         rte_eth_dev_info_get(port_id, &dev_info);
2898         link = dev->data->dev_link;
2899
2900         if (queue_idx > dev_info.max_tx_queues) {
2901                 RTE_PMD_DEBUG_TRACE("set queue rate limit:port %d: "
2902                                 "invalid queue id=%d\n", port_id, queue_idx);
2903                 return -EINVAL;
2904         }
2905
2906         if (tx_rate > link.link_speed) {
2907                 RTE_PMD_DEBUG_TRACE("set queue rate limit:invalid tx_rate=%d, "
2908                                 "bigger than link speed= %d\n",
2909                         tx_rate, link.link_speed);
2910                 return -EINVAL;
2911         }
2912
2913         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_queue_rate_limit, -ENOTSUP);
2914         return (*dev->dev_ops->set_queue_rate_limit)(dev, queue_idx, tx_rate);
2915 }
2916
2917 int
2918 rte_eth_mirror_rule_set(uint16_t port_id,
2919                         struct rte_eth_mirror_conf *mirror_conf,
2920                         uint8_t rule_id, uint8_t on)
2921 {
2922         struct rte_eth_dev *dev;
2923
2924         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2925         if (mirror_conf->rule_type == 0) {
2926                 RTE_PMD_DEBUG_TRACE("mirror rule type can not be 0.\n");
2927                 return -EINVAL;
2928         }
2929
2930         if (mirror_conf->dst_pool >= ETH_64_POOLS) {
2931                 RTE_PMD_DEBUG_TRACE("Invalid dst pool, pool id must be 0-%d\n",
2932                                 ETH_64_POOLS - 1);
2933                 return -EINVAL;
2934         }
2935
2936         if ((mirror_conf->rule_type & (ETH_MIRROR_VIRTUAL_POOL_UP |
2937              ETH_MIRROR_VIRTUAL_POOL_DOWN)) &&
2938             (mirror_conf->pool_mask == 0)) {
2939                 RTE_PMD_DEBUG_TRACE("Invalid mirror pool, pool mask can not be 0.\n");
2940                 return -EINVAL;
2941         }
2942
2943         if ((mirror_conf->rule_type & ETH_MIRROR_VLAN) &&
2944             mirror_conf->vlan.vlan_mask == 0) {
2945                 RTE_PMD_DEBUG_TRACE("Invalid vlan mask, vlan mask can not be 0.\n");
2946                 return -EINVAL;
2947         }
2948
2949         dev = &rte_eth_devices[port_id];
2950         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mirror_rule_set, -ENOTSUP);
2951
2952         return (*dev->dev_ops->mirror_rule_set)(dev, mirror_conf, rule_id, on);
2953 }
2954
2955 int
2956 rte_eth_mirror_rule_reset(uint16_t port_id, uint8_t rule_id)
2957 {
2958         struct rte_eth_dev *dev;
2959
2960         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2961
2962         dev = &rte_eth_devices[port_id];
2963         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mirror_rule_reset, -ENOTSUP);
2964
2965         return (*dev->dev_ops->mirror_rule_reset)(dev, rule_id);
2966 }
2967
2968 RTE_INIT(eth_dev_init_cb_lists)
2969 {
2970         int i;
2971
2972         for (i = 0; i < RTE_MAX_ETHPORTS; i++)
2973                 TAILQ_INIT(&rte_eth_devices[i].link_intr_cbs);
2974 }
2975
2976 int
2977 rte_eth_dev_callback_register(uint16_t port_id,
2978                         enum rte_eth_event_type event,
2979                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
2980 {
2981         struct rte_eth_dev *dev;
2982         struct rte_eth_dev_callback *user_cb;
2983         uint32_t next_port; /* size is 32-bit to prevent loop wrap-around */
2984         uint16_t last_port;
2985
2986         if (!cb_fn)
2987                 return -EINVAL;
2988
2989         if (!rte_eth_dev_is_valid_port(port_id) && port_id != RTE_ETH_ALL) {
2990                 RTE_LOG(ERR, EAL, "Invalid port_id=%d\n", port_id);
2991                 return -EINVAL;
2992         }
2993
2994         if (port_id == RTE_ETH_ALL) {
2995                 next_port = 0;
2996                 last_port = RTE_MAX_ETHPORTS - 1;
2997         } else {
2998                 next_port = last_port = port_id;
2999         }
3000
3001         rte_spinlock_lock(&rte_eth_dev_cb_lock);
3002
3003         do {
3004                 dev = &rte_eth_devices[next_port];
3005
3006                 TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
3007                         if (user_cb->cb_fn == cb_fn &&
3008                                 user_cb->cb_arg == cb_arg &&
3009                                 user_cb->event == event) {
3010                                 break;
3011                         }
3012                 }
3013
3014                 /* create a new callback. */
3015                 if (user_cb == NULL) {
3016                         user_cb = rte_zmalloc("INTR_USER_CALLBACK",
3017                                 sizeof(struct rte_eth_dev_callback), 0);
3018                         if (user_cb != NULL) {
3019                                 user_cb->cb_fn = cb_fn;
3020                                 user_cb->cb_arg = cb_arg;
3021                                 user_cb->event = event;
3022                                 TAILQ_INSERT_TAIL(&(dev->link_intr_cbs),
3023                                                   user_cb, next);
3024                         } else {
3025                                 rte_spinlock_unlock(&rte_eth_dev_cb_lock);
3026                                 rte_eth_dev_callback_unregister(port_id, event,
3027                                                                 cb_fn, cb_arg);
3028                                 return -ENOMEM;
3029                         }
3030
3031                 }
3032         } while (++next_port <= last_port);
3033
3034         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
3035         return 0;
3036 }
3037
3038 int
3039 rte_eth_dev_callback_unregister(uint16_t port_id,
3040                         enum rte_eth_event_type event,
3041                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
3042 {
3043         int ret;
3044         struct rte_eth_dev *dev;
3045         struct rte_eth_dev_callback *cb, *next;
3046         uint32_t next_port; /* size is 32-bit to prevent loop wrap-around */
3047         uint16_t last_port;
3048
3049         if (!cb_fn)
3050                 return -EINVAL;
3051
3052         if (!rte_eth_dev_is_valid_port(port_id) && port_id != RTE_ETH_ALL) {
3053                 RTE_LOG(ERR, EAL, "Invalid port_id=%d\n", port_id);
3054                 return -EINVAL;
3055         }
3056
3057         if (port_id == RTE_ETH_ALL) {
3058                 next_port = 0;
3059                 last_port = RTE_MAX_ETHPORTS - 1;
3060         } else {
3061                 next_port = last_port = port_id;
3062         }
3063
3064         rte_spinlock_lock(&rte_eth_dev_cb_lock);
3065
3066         do {
3067                 dev = &rte_eth_devices[next_port];
3068                 ret = 0;
3069                 for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL;
3070                      cb = next) {
3071
3072                         next = TAILQ_NEXT(cb, next);
3073
3074                         if (cb->cb_fn != cb_fn || cb->event != event ||
3075                             (cb->cb_arg != (void *)-1 && cb->cb_arg != cb_arg))
3076                                 continue;
3077
3078                         /*
3079                          * if this callback is not executing right now,
3080                          * then remove it.
3081                          */
3082                         if (cb->active == 0) {
3083                                 TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
3084                                 rte_free(cb);
3085                         } else {
3086                                 ret = -EAGAIN;
3087                         }
3088                 }
3089         } while (++next_port <= last_port);
3090
3091         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
3092         return ret;
3093 }
3094
3095 int
3096 _rte_eth_dev_callback_process(struct rte_eth_dev *dev,
3097         enum rte_eth_event_type event, void *ret_param)
3098 {
3099         struct rte_eth_dev_callback *cb_lst;
3100         struct rte_eth_dev_callback dev_cb;
3101         int rc = 0;
3102
3103         rte_spinlock_lock(&rte_eth_dev_cb_lock);
3104         TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
3105                 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
3106                         continue;
3107                 dev_cb = *cb_lst;
3108                 cb_lst->active = 1;
3109                 if (ret_param != NULL)
3110                         dev_cb.ret_param = ret_param;
3111
3112                 rte_spinlock_unlock(&rte_eth_dev_cb_lock);
3113                 rc = dev_cb.cb_fn(dev->data->port_id, dev_cb.event,
3114                                 dev_cb.cb_arg, dev_cb.ret_param);
3115                 rte_spinlock_lock(&rte_eth_dev_cb_lock);
3116                 cb_lst->active = 0;
3117         }
3118         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
3119         return rc;
3120 }
3121
3122 int
3123 rte_eth_dev_rx_intr_ctl(uint16_t port_id, int epfd, int op, void *data)
3124 {
3125         uint32_t vec;
3126         struct rte_eth_dev *dev;
3127         struct rte_intr_handle *intr_handle;
3128         uint16_t qid;
3129         int rc;
3130
3131         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3132
3133         dev = &rte_eth_devices[port_id];
3134
3135         if (!dev->intr_handle) {
3136                 RTE_PMD_DEBUG_TRACE("RX Intr handle unset\n");
3137                 return -ENOTSUP;
3138         }
3139
3140         intr_handle = dev->intr_handle;
3141         if (!intr_handle->intr_vec) {
3142                 RTE_PMD_DEBUG_TRACE("RX Intr vector unset\n");
3143                 return -EPERM;
3144         }
3145
3146         for (qid = 0; qid < dev->data->nb_rx_queues; qid++) {
3147                 vec = intr_handle->intr_vec[qid];
3148                 rc = rte_intr_rx_ctl(intr_handle, epfd, op, vec, data);
3149                 if (rc && rc != -EEXIST) {
3150                         RTE_PMD_DEBUG_TRACE("p %u q %u rx ctl error"
3151                                         " op %d epfd %d vec %u\n",
3152                                         port_id, qid, op, epfd, vec);
3153                 }
3154         }
3155
3156         return 0;
3157 }
3158
3159 const struct rte_memzone *
3160 rte_eth_dma_zone_reserve(const struct rte_eth_dev *dev, const char *ring_name,
3161                          uint16_t queue_id, size_t size, unsigned align,
3162                          int socket_id)
3163 {
3164         char z_name[RTE_MEMZONE_NAMESIZE];
3165         const struct rte_memzone *mz;
3166
3167         snprintf(z_name, sizeof(z_name), "%s_%s_%d_%d",
3168                  dev->device->driver->name, ring_name,
3169                  dev->data->port_id, queue_id);
3170
3171         mz = rte_memzone_lookup(z_name);
3172         if (mz)
3173                 return mz;
3174
3175         return rte_memzone_reserve_aligned(z_name, size, socket_id, 0, align);
3176 }
3177
3178 int
3179 rte_eth_dev_rx_intr_ctl_q(uint16_t port_id, uint16_t queue_id,
3180                           int epfd, int op, void *data)
3181 {
3182         uint32_t vec;
3183         struct rte_eth_dev *dev;
3184         struct rte_intr_handle *intr_handle;
3185         int rc;
3186
3187         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3188
3189         dev = &rte_eth_devices[port_id];
3190         if (queue_id >= dev->data->nb_rx_queues) {
3191                 RTE_PMD_DEBUG_TRACE("Invalid RX queue_id=%u\n", queue_id);
3192                 return -EINVAL;
3193         }
3194
3195         if (!dev->intr_handle) {
3196                 RTE_PMD_DEBUG_TRACE("RX Intr handle unset\n");
3197                 return -ENOTSUP;
3198         }
3199
3200         intr_handle = dev->intr_handle;
3201         if (!intr_handle->intr_vec) {
3202                 RTE_PMD_DEBUG_TRACE("RX Intr vector unset\n");
3203                 return -EPERM;
3204         }
3205
3206         vec = intr_handle->intr_vec[queue_id];
3207         rc = rte_intr_rx_ctl(intr_handle, epfd, op, vec, data);
3208         if (rc && rc != -EEXIST) {
3209                 RTE_PMD_DEBUG_TRACE("p %u q %u rx ctl error"
3210                                 " op %d epfd %d vec %u\n",
3211                                 port_id, queue_id, op, epfd, vec);
3212                 return rc;
3213         }
3214
3215         return 0;
3216 }
3217
3218 int
3219 rte_eth_dev_rx_intr_enable(uint16_t port_id,
3220                            uint16_t queue_id)
3221 {
3222         struct rte_eth_dev *dev;
3223
3224         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3225
3226         dev = &rte_eth_devices[port_id];
3227
3228         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_intr_enable, -ENOTSUP);
3229         return (*dev->dev_ops->rx_queue_intr_enable)(dev, queue_id);
3230 }
3231
3232 int
3233 rte_eth_dev_rx_intr_disable(uint16_t port_id,
3234                             uint16_t queue_id)
3235 {
3236         struct rte_eth_dev *dev;
3237
3238         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3239
3240         dev = &rte_eth_devices[port_id];
3241
3242         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_intr_disable, -ENOTSUP);
3243         return (*dev->dev_ops->rx_queue_intr_disable)(dev, queue_id);
3244 }
3245
3246
3247 int
3248 rte_eth_dev_filter_supported(uint16_t port_id,
3249                              enum rte_filter_type filter_type)
3250 {
3251         struct rte_eth_dev *dev;
3252
3253         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3254
3255         dev = &rte_eth_devices[port_id];
3256         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->filter_ctrl, -ENOTSUP);
3257         return (*dev->dev_ops->filter_ctrl)(dev, filter_type,
3258                                 RTE_ETH_FILTER_NOP, NULL);
3259 }
3260
3261 int
3262 rte_eth_dev_filter_ctrl_v22(uint16_t port_id,
3263                             enum rte_filter_type filter_type,
3264                             enum rte_filter_op filter_op, void *arg);
3265
3266 int
3267 rte_eth_dev_filter_ctrl_v22(uint16_t port_id,
3268                             enum rte_filter_type filter_type,
3269                             enum rte_filter_op filter_op, void *arg)
3270 {
3271         struct rte_eth_fdir_info_v22 {
3272                 enum rte_fdir_mode mode;
3273                 struct rte_eth_fdir_masks mask;
3274                 struct rte_eth_fdir_flex_conf flex_conf;
3275                 uint32_t guarant_spc;
3276                 uint32_t best_spc;
3277                 uint32_t flow_types_mask[1];
3278                 uint32_t max_flexpayload;
3279                 uint32_t flex_payload_unit;
3280                 uint32_t max_flex_payload_segment_num;
3281                 uint16_t flex_payload_limit;
3282                 uint32_t flex_bitmask_unit;
3283                 uint32_t max_flex_bitmask_num;
3284         };
3285
3286         struct rte_eth_hash_global_conf_v22 {
3287                 enum rte_eth_hash_function hash_func;
3288                 uint32_t sym_hash_enable_mask[1];
3289                 uint32_t valid_bit_mask[1];
3290         };
3291
3292         struct rte_eth_hash_filter_info_v22 {
3293                 enum rte_eth_hash_filter_info_type info_type;
3294                 union {
3295                         uint8_t enable;
3296                         struct rte_eth_hash_global_conf_v22 global_conf;
3297                         struct rte_eth_input_set_conf input_set_conf;
3298                 } info;
3299         };
3300
3301         struct rte_eth_dev *dev;
3302
3303         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3304
3305         dev = &rte_eth_devices[port_id];
3306         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->filter_ctrl, -ENOTSUP);
3307         if (filter_op == RTE_ETH_FILTER_INFO) {
3308                 int retval;
3309                 struct rte_eth_fdir_info_v22 *fdir_info_v22;
3310                 struct rte_eth_fdir_info fdir_info;
3311
3312                 fdir_info_v22 = (struct rte_eth_fdir_info_v22 *)arg;
3313
3314                 retval = (*dev->dev_ops->filter_ctrl)(dev, filter_type,
3315                           filter_op, (void *)&fdir_info);
3316                 fdir_info_v22->mode = fdir_info.mode;
3317                 fdir_info_v22->mask = fdir_info.mask;
3318                 fdir_info_v22->flex_conf = fdir_info.flex_conf;
3319                 fdir_info_v22->guarant_spc = fdir_info.guarant_spc;
3320                 fdir_info_v22->best_spc = fdir_info.best_spc;
3321                 fdir_info_v22->flow_types_mask[0] =
3322                         (uint32_t)fdir_info.flow_types_mask[0];
3323                 fdir_info_v22->max_flexpayload = fdir_info.max_flexpayload;
3324                 fdir_info_v22->flex_payload_unit = fdir_info.flex_payload_unit;
3325                 fdir_info_v22->max_flex_payload_segment_num =
3326                         fdir_info.max_flex_payload_segment_num;
3327                 fdir_info_v22->flex_payload_limit =
3328                         fdir_info.flex_payload_limit;
3329                 fdir_info_v22->flex_bitmask_unit = fdir_info.flex_bitmask_unit;
3330                 fdir_info_v22->max_flex_bitmask_num =
3331                         fdir_info.max_flex_bitmask_num;
3332                 return retval;
3333         } else if (filter_op == RTE_ETH_FILTER_GET) {
3334                 int retval;
3335                 struct rte_eth_hash_filter_info f_info;
3336                 struct rte_eth_hash_filter_info_v22 *f_info_v22 =
3337                         (struct rte_eth_hash_filter_info_v22 *)arg;
3338
3339                 f_info.info_type = f_info_v22->info_type;
3340                 retval = (*dev->dev_ops->filter_ctrl)(dev, filter_type,
3341                           filter_op, (void *)&f_info);
3342
3343                 switch (f_info_v22->info_type) {
3344                 case RTE_ETH_HASH_FILTER_SYM_HASH_ENA_PER_PORT:
3345                         f_info_v22->info.enable = f_info.info.enable;
3346                         break;
3347                 case RTE_ETH_HASH_FILTER_GLOBAL_CONFIG:
3348                         f_info_v22->info.global_conf.hash_func =
3349                                 f_info.info.global_conf.hash_func;
3350                         f_info_v22->info.global_conf.sym_hash_enable_mask[0] =
3351                                 (uint32_t)
3352                                 f_info.info.global_conf.sym_hash_enable_mask[0];
3353                         f_info_v22->info.global_conf.valid_bit_mask[0] =
3354                                 (uint32_t)
3355                                 f_info.info.global_conf.valid_bit_mask[0];
3356                         break;
3357                 case RTE_ETH_HASH_FILTER_INPUT_SET_SELECT:
3358                         f_info_v22->info.input_set_conf =
3359                                 f_info.info.input_set_conf;
3360                         break;
3361                 default:
3362                         break;
3363                 }
3364                 return retval;
3365         } else if (filter_op == RTE_ETH_FILTER_SET) {
3366                 struct rte_eth_hash_filter_info f_info;
3367                 struct rte_eth_hash_filter_info_v22 *f_v22 =
3368                         (struct rte_eth_hash_filter_info_v22 *)arg;
3369
3370                 f_info.info_type = f_v22->info_type;
3371                 switch (f_v22->info_type) {
3372                 case RTE_ETH_HASH_FILTER_SYM_HASH_ENA_PER_PORT:
3373                         f_info.info.enable = f_v22->info.enable;
3374                         break;
3375                 case RTE_ETH_HASH_FILTER_GLOBAL_CONFIG:
3376                         f_info.info.global_conf.hash_func =
3377                                 f_v22->info.global_conf.hash_func;
3378                         f_info.info.global_conf.sym_hash_enable_mask[0] =
3379                                 (uint32_t)
3380                                 f_v22->info.global_conf.sym_hash_enable_mask[0];
3381                         f_info.info.global_conf.valid_bit_mask[0] =
3382                                 (uint32_t)
3383                                 f_v22->info.global_conf.valid_bit_mask[0];
3384                         break;
3385                 case RTE_ETH_HASH_FILTER_INPUT_SET_SELECT:
3386                         f_info.info.input_set_conf =
3387                                 f_v22->info.input_set_conf;
3388                         break;
3389                 default:
3390                         break;
3391                 }
3392                 return (*dev->dev_ops->filter_ctrl)(dev, filter_type, filter_op,
3393                                                     (void *)&f_info);
3394         } else
3395                 return (*dev->dev_ops->filter_ctrl)(dev, filter_type, filter_op,
3396                                                     arg);
3397 }
3398 VERSION_SYMBOL(rte_eth_dev_filter_ctrl, _v22, 2.2);
3399
3400 int
3401 rte_eth_dev_filter_ctrl_v1802(uint16_t port_id,
3402                               enum rte_filter_type filter_type,
3403                               enum rte_filter_op filter_op, void *arg);
3404
3405 int
3406 rte_eth_dev_filter_ctrl_v1802(uint16_t port_id,
3407                               enum rte_filter_type filter_type,
3408                               enum rte_filter_op filter_op, void *arg)
3409 {
3410         struct rte_eth_dev *dev;
3411
3412         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3413
3414         dev = &rte_eth_devices[port_id];
3415         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->filter_ctrl, -ENOTSUP);
3416         return (*dev->dev_ops->filter_ctrl)(dev, filter_type, filter_op, arg);
3417 }
3418 BIND_DEFAULT_SYMBOL(rte_eth_dev_filter_ctrl, _v1802, 18.02);
3419 MAP_STATIC_SYMBOL(int rte_eth_dev_filter_ctrl(uint16_t port_id,
3420                   enum rte_filter_type filter_type,
3421                   enum rte_filter_op filter_op, void *arg),
3422                   rte_eth_dev_filter_ctrl_v1802);
3423
3424 void *
3425 rte_eth_add_rx_callback(uint16_t port_id, uint16_t queue_id,
3426                 rte_rx_callback_fn fn, void *user_param)
3427 {
3428 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
3429         rte_errno = ENOTSUP;
3430         return NULL;
3431 #endif
3432         /* check input parameters */
3433         if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
3434                     queue_id >= rte_eth_devices[port_id].data->nb_rx_queues) {
3435                 rte_errno = EINVAL;
3436                 return NULL;
3437         }
3438         struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
3439
3440         if (cb == NULL) {
3441                 rte_errno = ENOMEM;
3442                 return NULL;
3443         }
3444
3445         cb->fn.rx = fn;
3446         cb->param = user_param;
3447
3448         rte_spinlock_lock(&rte_eth_rx_cb_lock);
3449         /* Add the callbacks in fifo order. */
3450         struct rte_eth_rxtx_callback *tail =
3451                 rte_eth_devices[port_id].post_rx_burst_cbs[queue_id];
3452
3453         if (!tail) {
3454                 rte_eth_devices[port_id].post_rx_burst_cbs[queue_id] = cb;
3455
3456         } else {
3457                 while (tail->next)
3458                         tail = tail->next;
3459                 tail->next = cb;
3460         }
3461         rte_spinlock_unlock(&rte_eth_rx_cb_lock);
3462
3463         return cb;
3464 }
3465
3466 void *
3467 rte_eth_add_first_rx_callback(uint16_t port_id, uint16_t queue_id,
3468                 rte_rx_callback_fn fn, void *user_param)
3469 {
3470 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
3471         rte_errno = ENOTSUP;
3472         return NULL;
3473 #endif
3474         /* check input parameters */
3475         if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
3476                 queue_id >= rte_eth_devices[port_id].data->nb_rx_queues) {
3477                 rte_errno = EINVAL;
3478                 return NULL;
3479         }
3480
3481         struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
3482
3483         if (cb == NULL) {
3484                 rte_errno = ENOMEM;
3485                 return NULL;
3486         }
3487
3488         cb->fn.rx = fn;
3489         cb->param = user_param;
3490
3491         rte_spinlock_lock(&rte_eth_rx_cb_lock);
3492         /* Add the callbacks at fisrt position*/
3493         cb->next = rte_eth_devices[port_id].post_rx_burst_cbs[queue_id];
3494         rte_smp_wmb();
3495         rte_eth_devices[port_id].post_rx_burst_cbs[queue_id] = cb;
3496         rte_spinlock_unlock(&rte_eth_rx_cb_lock);
3497
3498         return cb;
3499 }
3500
3501 void *
3502 rte_eth_add_tx_callback(uint16_t port_id, uint16_t queue_id,
3503                 rte_tx_callback_fn fn, void *user_param)
3504 {
3505 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
3506         rte_errno = ENOTSUP;
3507         return NULL;
3508 #endif
3509         /* check input parameters */
3510         if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
3511                     queue_id >= rte_eth_devices[port_id].data->nb_tx_queues) {
3512                 rte_errno = EINVAL;
3513                 return NULL;
3514         }
3515
3516         struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
3517
3518         if (cb == NULL) {
3519                 rte_errno = ENOMEM;
3520                 return NULL;
3521         }
3522
3523         cb->fn.tx = fn;
3524         cb->param = user_param;
3525
3526         rte_spinlock_lock(&rte_eth_tx_cb_lock);
3527         /* Add the callbacks in fifo order. */
3528         struct rte_eth_rxtx_callback *tail =
3529                 rte_eth_devices[port_id].pre_tx_burst_cbs[queue_id];
3530
3531         if (!tail) {
3532                 rte_eth_devices[port_id].pre_tx_burst_cbs[queue_id] = cb;
3533
3534         } else {
3535                 while (tail->next)
3536                         tail = tail->next;
3537                 tail->next = cb;
3538         }
3539         rte_spinlock_unlock(&rte_eth_tx_cb_lock);
3540
3541         return cb;
3542 }
3543
3544 int
3545 rte_eth_remove_rx_callback(uint16_t port_id, uint16_t queue_id,
3546                 struct rte_eth_rxtx_callback *user_cb)
3547 {
3548 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
3549         return -ENOTSUP;
3550 #endif
3551         /* Check input parameters. */
3552         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
3553         if (user_cb == NULL ||
3554                         queue_id >= rte_eth_devices[port_id].data->nb_rx_queues)
3555                 return -EINVAL;
3556
3557         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
3558         struct rte_eth_rxtx_callback *cb;
3559         struct rte_eth_rxtx_callback **prev_cb;
3560         int ret = -EINVAL;
3561
3562         rte_spinlock_lock(&rte_eth_rx_cb_lock);
3563         prev_cb = &dev->post_rx_burst_cbs[queue_id];
3564         for (; *prev_cb != NULL; prev_cb = &cb->next) {
3565                 cb = *prev_cb;
3566                 if (cb == user_cb) {
3567                         /* Remove the user cb from the callback list. */
3568                         *prev_cb = cb->next;
3569                         ret = 0;
3570                         break;
3571                 }
3572         }
3573         rte_spinlock_unlock(&rte_eth_rx_cb_lock);
3574
3575         return ret;
3576 }
3577
3578 int
3579 rte_eth_remove_tx_callback(uint16_t port_id, uint16_t queue_id,
3580                 struct rte_eth_rxtx_callback *user_cb)
3581 {
3582 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
3583         return -ENOTSUP;
3584 #endif
3585         /* Check input parameters. */
3586         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
3587         if (user_cb == NULL ||
3588                         queue_id >= rte_eth_devices[port_id].data->nb_tx_queues)
3589                 return -EINVAL;
3590
3591         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
3592         int ret = -EINVAL;
3593         struct rte_eth_rxtx_callback *cb;
3594         struct rte_eth_rxtx_callback **prev_cb;
3595
3596         rte_spinlock_lock(&rte_eth_tx_cb_lock);
3597         prev_cb = &dev->pre_tx_burst_cbs[queue_id];
3598         for (; *prev_cb != NULL; prev_cb = &cb->next) {
3599                 cb = *prev_cb;
3600                 if (cb == user_cb) {
3601                         /* Remove the user cb from the callback list. */
3602                         *prev_cb = cb->next;
3603                         ret = 0;
3604                         break;
3605                 }
3606         }
3607         rte_spinlock_unlock(&rte_eth_tx_cb_lock);
3608
3609         return ret;
3610 }
3611
3612 int
3613 rte_eth_rx_queue_info_get(uint16_t port_id, uint16_t queue_id,
3614         struct rte_eth_rxq_info *qinfo)
3615 {
3616         struct rte_eth_dev *dev;
3617
3618         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3619
3620         if (qinfo == NULL)
3621                 return -EINVAL;
3622
3623         dev = &rte_eth_devices[port_id];
3624         if (queue_id >= dev->data->nb_rx_queues) {
3625                 RTE_PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", queue_id);
3626                 return -EINVAL;
3627         }
3628
3629         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rxq_info_get, -ENOTSUP);
3630
3631         memset(qinfo, 0, sizeof(*qinfo));
3632         dev->dev_ops->rxq_info_get(dev, queue_id, qinfo);
3633         return 0;
3634 }
3635
3636 int
3637 rte_eth_tx_queue_info_get(uint16_t port_id, uint16_t queue_id,
3638         struct rte_eth_txq_info *qinfo)
3639 {
3640         struct rte_eth_dev *dev;
3641
3642         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3643
3644         if (qinfo == NULL)
3645                 return -EINVAL;
3646
3647         dev = &rte_eth_devices[port_id];
3648         if (queue_id >= dev->data->nb_tx_queues) {
3649                 RTE_PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", queue_id);
3650                 return -EINVAL;
3651         }
3652
3653         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->txq_info_get, -ENOTSUP);
3654
3655         memset(qinfo, 0, sizeof(*qinfo));
3656         dev->dev_ops->txq_info_get(dev, queue_id, qinfo);
3657         return 0;
3658 }
3659
3660 int
3661 rte_eth_dev_set_mc_addr_list(uint16_t port_id,
3662                              struct ether_addr *mc_addr_set,
3663                              uint32_t nb_mc_addr)
3664 {
3665         struct rte_eth_dev *dev;
3666
3667         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3668
3669         dev = &rte_eth_devices[port_id];
3670         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_mc_addr_list, -ENOTSUP);
3671         return dev->dev_ops->set_mc_addr_list(dev, mc_addr_set, nb_mc_addr);
3672 }
3673
3674 int
3675 rte_eth_timesync_enable(uint16_t port_id)
3676 {
3677         struct rte_eth_dev *dev;
3678
3679         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3680         dev = &rte_eth_devices[port_id];
3681
3682         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_enable, -ENOTSUP);
3683         return (*dev->dev_ops->timesync_enable)(dev);
3684 }
3685
3686 int
3687 rte_eth_timesync_disable(uint16_t port_id)
3688 {
3689         struct rte_eth_dev *dev;
3690
3691         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3692         dev = &rte_eth_devices[port_id];
3693
3694         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_disable, -ENOTSUP);
3695         return (*dev->dev_ops->timesync_disable)(dev);
3696 }
3697
3698 int
3699 rte_eth_timesync_read_rx_timestamp(uint16_t port_id, struct timespec *timestamp,
3700                                    uint32_t flags)
3701 {
3702         struct rte_eth_dev *dev;
3703
3704         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3705         dev = &rte_eth_devices[port_id];
3706
3707         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_rx_timestamp, -ENOTSUP);
3708         return (*dev->dev_ops->timesync_read_rx_timestamp)(dev, timestamp, flags);
3709 }
3710
3711 int
3712 rte_eth_timesync_read_tx_timestamp(uint16_t port_id,
3713                                    struct timespec *timestamp)
3714 {
3715         struct rte_eth_dev *dev;
3716
3717         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3718         dev = &rte_eth_devices[port_id];
3719
3720         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_tx_timestamp, -ENOTSUP);
3721         return (*dev->dev_ops->timesync_read_tx_timestamp)(dev, timestamp);
3722 }
3723
3724 int
3725 rte_eth_timesync_adjust_time(uint16_t port_id, int64_t delta)
3726 {
3727         struct rte_eth_dev *dev;
3728
3729         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3730         dev = &rte_eth_devices[port_id];
3731
3732         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_adjust_time, -ENOTSUP);
3733         return (*dev->dev_ops->timesync_adjust_time)(dev, delta);
3734 }
3735
3736 int
3737 rte_eth_timesync_read_time(uint16_t port_id, struct timespec *timestamp)
3738 {
3739         struct rte_eth_dev *dev;
3740
3741         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3742         dev = &rte_eth_devices[port_id];
3743
3744         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_time, -ENOTSUP);
3745         return (*dev->dev_ops->timesync_read_time)(dev, timestamp);
3746 }
3747
3748 int
3749 rte_eth_timesync_write_time(uint16_t port_id, const struct timespec *timestamp)
3750 {
3751         struct rte_eth_dev *dev;
3752
3753         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3754         dev = &rte_eth_devices[port_id];
3755
3756         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_write_time, -ENOTSUP);
3757         return (*dev->dev_ops->timesync_write_time)(dev, timestamp);
3758 }
3759
3760 int
3761 rte_eth_dev_get_reg_info(uint16_t port_id, struct rte_dev_reg_info *info)
3762 {
3763         struct rte_eth_dev *dev;
3764
3765         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3766
3767         dev = &rte_eth_devices[port_id];
3768         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_reg, -ENOTSUP);
3769         return (*dev->dev_ops->get_reg)(dev, info);
3770 }
3771
3772 int
3773 rte_eth_dev_get_eeprom_length(uint16_t port_id)
3774 {
3775         struct rte_eth_dev *dev;
3776
3777         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3778
3779         dev = &rte_eth_devices[port_id];
3780         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_eeprom_length, -ENOTSUP);
3781         return (*dev->dev_ops->get_eeprom_length)(dev);
3782 }
3783
3784 int
3785 rte_eth_dev_get_eeprom(uint16_t port_id, struct rte_dev_eeprom_info *info)
3786 {
3787         struct rte_eth_dev *dev;
3788
3789         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3790
3791         dev = &rte_eth_devices[port_id];
3792         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_eeprom, -ENOTSUP);
3793         return (*dev->dev_ops->get_eeprom)(dev, info);
3794 }
3795
3796 int
3797 rte_eth_dev_set_eeprom(uint16_t port_id, struct rte_dev_eeprom_info *info)
3798 {
3799         struct rte_eth_dev *dev;
3800
3801         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3802
3803         dev = &rte_eth_devices[port_id];
3804         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_eeprom, -ENOTSUP);
3805         return (*dev->dev_ops->set_eeprom)(dev, info);
3806 }
3807
3808 int
3809 rte_eth_dev_get_dcb_info(uint16_t port_id,
3810                              struct rte_eth_dcb_info *dcb_info)
3811 {
3812         struct rte_eth_dev *dev;
3813
3814         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3815
3816         dev = &rte_eth_devices[port_id];
3817         memset(dcb_info, 0, sizeof(struct rte_eth_dcb_info));
3818
3819         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_dcb_info, -ENOTSUP);
3820         return (*dev->dev_ops->get_dcb_info)(dev, dcb_info);
3821 }
3822
3823 int
3824 rte_eth_dev_l2_tunnel_eth_type_conf(uint16_t port_id,
3825                                     struct rte_eth_l2_tunnel_conf *l2_tunnel)
3826 {
3827         struct rte_eth_dev *dev;
3828
3829         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3830         if (l2_tunnel == NULL) {
3831                 RTE_PMD_DEBUG_TRACE("Invalid l2_tunnel parameter\n");
3832                 return -EINVAL;
3833         }
3834
3835         if (l2_tunnel->l2_tunnel_type >= RTE_TUNNEL_TYPE_MAX) {
3836                 RTE_PMD_DEBUG_TRACE("Invalid tunnel type\n");
3837                 return -EINVAL;
3838         }
3839
3840         dev = &rte_eth_devices[port_id];
3841         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->l2_tunnel_eth_type_conf,
3842                                 -ENOTSUP);
3843         return (*dev->dev_ops->l2_tunnel_eth_type_conf)(dev, l2_tunnel);
3844 }
3845
3846 int
3847 rte_eth_dev_l2_tunnel_offload_set(uint16_t port_id,
3848                                   struct rte_eth_l2_tunnel_conf *l2_tunnel,
3849                                   uint32_t mask,
3850                                   uint8_t en)
3851 {
3852         struct rte_eth_dev *dev;
3853
3854         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3855
3856         if (l2_tunnel == NULL) {
3857                 RTE_PMD_DEBUG_TRACE("Invalid l2_tunnel parameter\n");
3858                 return -EINVAL;
3859         }
3860
3861         if (l2_tunnel->l2_tunnel_type >= RTE_TUNNEL_TYPE_MAX) {
3862                 RTE_PMD_DEBUG_TRACE("Invalid tunnel type.\n");
3863                 return -EINVAL;
3864         }
3865
3866         if (mask == 0) {
3867                 RTE_PMD_DEBUG_TRACE("Mask should have a value.\n");
3868                 return -EINVAL;
3869         }
3870
3871         dev = &rte_eth_devices[port_id];
3872         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->l2_tunnel_offload_set,
3873                                 -ENOTSUP);
3874         return (*dev->dev_ops->l2_tunnel_offload_set)(dev, l2_tunnel, mask, en);
3875 }
3876
3877 static void
3878 rte_eth_dev_adjust_nb_desc(uint16_t *nb_desc,
3879                            const struct rte_eth_desc_lim *desc_lim)
3880 {
3881         if (desc_lim->nb_align != 0)
3882                 *nb_desc = RTE_ALIGN_CEIL(*nb_desc, desc_lim->nb_align);
3883
3884         if (desc_lim->nb_max != 0)
3885                 *nb_desc = RTE_MIN(*nb_desc, desc_lim->nb_max);
3886
3887         *nb_desc = RTE_MAX(*nb_desc, desc_lim->nb_min);
3888 }
3889
3890 int
3891 rte_eth_dev_adjust_nb_rx_tx_desc(uint16_t port_id,
3892                                  uint16_t *nb_rx_desc,
3893                                  uint16_t *nb_tx_desc)
3894 {
3895         struct rte_eth_dev *dev;
3896         struct rte_eth_dev_info dev_info;
3897
3898         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3899
3900         dev = &rte_eth_devices[port_id];
3901         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
3902
3903         rte_eth_dev_info_get(port_id, &dev_info);
3904
3905         if (nb_rx_desc != NULL)
3906                 rte_eth_dev_adjust_nb_desc(nb_rx_desc, &dev_info.rx_desc_lim);
3907
3908         if (nb_tx_desc != NULL)
3909                 rte_eth_dev_adjust_nb_desc(nb_tx_desc, &dev_info.tx_desc_lim);
3910
3911         return 0;
3912 }
3913
3914 int
3915 rte_eth_dev_pool_ops_supported(uint16_t port_id, const char *pool)
3916 {
3917         struct rte_eth_dev *dev;
3918
3919         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3920
3921         if (pool == NULL)
3922                 return -EINVAL;
3923
3924         dev = &rte_eth_devices[port_id];
3925
3926         if (*dev->dev_ops->pool_ops_supported == NULL)
3927                 return 1; /* all pools are supported */
3928
3929         return (*dev->dev_ops->pool_ops_supported)(dev, pool);
3930 }