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