acc4b748d3072121b04a3d0ccefdc544549dc571
[dpdk.git] / lib / librte_ethdev / 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 <stdbool.h>
14 #include <stdint.h>
15 #include <inttypes.h>
16 #include <netinet/in.h>
17
18 #include <rte_byteorder.h>
19 #include <rte_log.h>
20 #include <rte_debug.h>
21 #include <rte_interrupts.h>
22 #include <rte_memory.h>
23 #include <rte_memcpy.h>
24 #include <rte_memzone.h>
25 #include <rte_launch.h>
26 #include <rte_eal.h>
27 #include <rte_per_lcore.h>
28 #include <rte_lcore.h>
29 #include <rte_atomic.h>
30 #include <rte_branch_prediction.h>
31 #include <rte_common.h>
32 #include <rte_mempool.h>
33 #include <rte_malloc.h>
34 #include <rte_mbuf.h>
35 #include <rte_errno.h>
36 #include <rte_spinlock.h>
37 #include <rte_string_fns.h>
38 #include <rte_kvargs.h>
39 #include <rte_class.h>
40
41 #include "rte_ether.h"
42 #include "rte_ethdev.h"
43 #include "rte_ethdev_driver.h"
44 #include "ethdev_profile.h"
45 #include "ethdev_private.h"
46
47 int rte_eth_dev_logtype;
48
49 static const char *MZ_RTE_ETH_DEV_DATA = "rte_eth_dev_data";
50 struct rte_eth_dev rte_eth_devices[RTE_MAX_ETHPORTS];
51
52 /* spinlock for eth device callbacks */
53 static rte_spinlock_t rte_eth_dev_cb_lock = RTE_SPINLOCK_INITIALIZER;
54
55 /* spinlock for add/remove rx callbacks */
56 static rte_spinlock_t rte_eth_rx_cb_lock = RTE_SPINLOCK_INITIALIZER;
57
58 /* spinlock for add/remove tx callbacks */
59 static rte_spinlock_t rte_eth_tx_cb_lock = RTE_SPINLOCK_INITIALIZER;
60
61 /* spinlock for shared data allocation */
62 static rte_spinlock_t rte_eth_shared_data_lock = RTE_SPINLOCK_INITIALIZER;
63
64 /* store statistics names and its offset in stats structure  */
65 struct rte_eth_xstats_name_off {
66         char name[RTE_ETH_XSTATS_NAME_SIZE];
67         unsigned offset;
68 };
69
70 /* Shared memory between primary and secondary processes. */
71 static struct {
72         uint64_t next_owner_id;
73         rte_spinlock_t ownership_lock;
74         struct rte_eth_dev_data data[RTE_MAX_ETHPORTS];
75 } *rte_eth_dev_shared_data;
76
77 static const struct rte_eth_xstats_name_off rte_stats_strings[] = {
78         {"rx_good_packets", offsetof(struct rte_eth_stats, ipackets)},
79         {"tx_good_packets", offsetof(struct rte_eth_stats, opackets)},
80         {"rx_good_bytes", offsetof(struct rte_eth_stats, ibytes)},
81         {"tx_good_bytes", offsetof(struct rte_eth_stats, obytes)},
82         {"rx_missed_errors", offsetof(struct rte_eth_stats, imissed)},
83         {"rx_errors", offsetof(struct rte_eth_stats, ierrors)},
84         {"tx_errors", offsetof(struct rte_eth_stats, oerrors)},
85         {"rx_mbuf_allocation_errors", offsetof(struct rte_eth_stats,
86                 rx_nombuf)},
87 };
88
89 #define RTE_NB_STATS (sizeof(rte_stats_strings) / sizeof(rte_stats_strings[0]))
90
91 static const struct rte_eth_xstats_name_off rte_rxq_stats_strings[] = {
92         {"packets", offsetof(struct rte_eth_stats, q_ipackets)},
93         {"bytes", offsetof(struct rte_eth_stats, q_ibytes)},
94         {"errors", offsetof(struct rte_eth_stats, q_errors)},
95 };
96
97 #define RTE_NB_RXQ_STATS (sizeof(rte_rxq_stats_strings) /       \
98                 sizeof(rte_rxq_stats_strings[0]))
99
100 static const struct rte_eth_xstats_name_off rte_txq_stats_strings[] = {
101         {"packets", offsetof(struct rte_eth_stats, q_opackets)},
102         {"bytes", offsetof(struct rte_eth_stats, q_obytes)},
103 };
104 #define RTE_NB_TXQ_STATS (sizeof(rte_txq_stats_strings) /       \
105                 sizeof(rte_txq_stats_strings[0]))
106
107 #define RTE_RX_OFFLOAD_BIT2STR(_name)   \
108         { DEV_RX_OFFLOAD_##_name, #_name }
109
110 static const struct {
111         uint64_t offload;
112         const char *name;
113 } rte_rx_offload_names[] = {
114         RTE_RX_OFFLOAD_BIT2STR(VLAN_STRIP),
115         RTE_RX_OFFLOAD_BIT2STR(IPV4_CKSUM),
116         RTE_RX_OFFLOAD_BIT2STR(UDP_CKSUM),
117         RTE_RX_OFFLOAD_BIT2STR(TCP_CKSUM),
118         RTE_RX_OFFLOAD_BIT2STR(TCP_LRO),
119         RTE_RX_OFFLOAD_BIT2STR(QINQ_STRIP),
120         RTE_RX_OFFLOAD_BIT2STR(OUTER_IPV4_CKSUM),
121         RTE_RX_OFFLOAD_BIT2STR(MACSEC_STRIP),
122         RTE_RX_OFFLOAD_BIT2STR(HEADER_SPLIT),
123         RTE_RX_OFFLOAD_BIT2STR(VLAN_FILTER),
124         RTE_RX_OFFLOAD_BIT2STR(VLAN_EXTEND),
125         RTE_RX_OFFLOAD_BIT2STR(JUMBO_FRAME),
126         RTE_RX_OFFLOAD_BIT2STR(SCATTER),
127         RTE_RX_OFFLOAD_BIT2STR(TIMESTAMP),
128         RTE_RX_OFFLOAD_BIT2STR(SECURITY),
129         RTE_RX_OFFLOAD_BIT2STR(KEEP_CRC),
130         RTE_RX_OFFLOAD_BIT2STR(SCTP_CKSUM),
131         RTE_RX_OFFLOAD_BIT2STR(OUTER_UDP_CKSUM),
132 };
133
134 #undef RTE_RX_OFFLOAD_BIT2STR
135
136 #define RTE_TX_OFFLOAD_BIT2STR(_name)   \
137         { DEV_TX_OFFLOAD_##_name, #_name }
138
139 static const struct {
140         uint64_t offload;
141         const char *name;
142 } rte_tx_offload_names[] = {
143         RTE_TX_OFFLOAD_BIT2STR(VLAN_INSERT),
144         RTE_TX_OFFLOAD_BIT2STR(IPV4_CKSUM),
145         RTE_TX_OFFLOAD_BIT2STR(UDP_CKSUM),
146         RTE_TX_OFFLOAD_BIT2STR(TCP_CKSUM),
147         RTE_TX_OFFLOAD_BIT2STR(SCTP_CKSUM),
148         RTE_TX_OFFLOAD_BIT2STR(TCP_TSO),
149         RTE_TX_OFFLOAD_BIT2STR(UDP_TSO),
150         RTE_TX_OFFLOAD_BIT2STR(OUTER_IPV4_CKSUM),
151         RTE_TX_OFFLOAD_BIT2STR(QINQ_INSERT),
152         RTE_TX_OFFLOAD_BIT2STR(VXLAN_TNL_TSO),
153         RTE_TX_OFFLOAD_BIT2STR(GRE_TNL_TSO),
154         RTE_TX_OFFLOAD_BIT2STR(IPIP_TNL_TSO),
155         RTE_TX_OFFLOAD_BIT2STR(GENEVE_TNL_TSO),
156         RTE_TX_OFFLOAD_BIT2STR(MACSEC_INSERT),
157         RTE_TX_OFFLOAD_BIT2STR(MT_LOCKFREE),
158         RTE_TX_OFFLOAD_BIT2STR(MULTI_SEGS),
159         RTE_TX_OFFLOAD_BIT2STR(MBUF_FAST_FREE),
160         RTE_TX_OFFLOAD_BIT2STR(SECURITY),
161         RTE_TX_OFFLOAD_BIT2STR(UDP_TNL_TSO),
162         RTE_TX_OFFLOAD_BIT2STR(IP_TNL_TSO),
163         RTE_TX_OFFLOAD_BIT2STR(OUTER_UDP_CKSUM),
164         RTE_TX_OFFLOAD_BIT2STR(MATCH_METADATA),
165 };
166
167 #undef RTE_TX_OFFLOAD_BIT2STR
168
169 static const struct {
170         uint64_t option;
171         const char *name;
172 } rte_burst_option_names[] = {
173         { RTE_ETH_BURST_SCALAR, "Scalar" },
174         { RTE_ETH_BURST_VECTOR, "Vector" },
175
176         { RTE_ETH_BURST_ALTIVEC, "AltiVec" },
177         { RTE_ETH_BURST_NEON, "Neon" },
178         { RTE_ETH_BURST_SSE, "SSE" },
179         { RTE_ETH_BURST_AVX2, "AVX2" },
180         { RTE_ETH_BURST_AVX512, "AVX512" },
181
182         { RTE_ETH_BURST_SCATTERED, "Scattered" },
183         { RTE_ETH_BURST_BULK_ALLOC, "Bulk Alloc" },
184         { RTE_ETH_BURST_SIMPLE, "Simple" },
185         { RTE_ETH_BURST_PER_QUEUE, "Per Queue" },
186 };
187
188 /**
189  * The user application callback description.
190  *
191  * It contains callback address to be registered by user application,
192  * the pointer to the parameters for callback, and the event type.
193  */
194 struct rte_eth_dev_callback {
195         TAILQ_ENTRY(rte_eth_dev_callback) next; /**< Callbacks list */
196         rte_eth_dev_cb_fn cb_fn;                /**< Callback address */
197         void *cb_arg;                           /**< Parameter for callback */
198         void *ret_param;                        /**< Return parameter */
199         enum rte_eth_event_type event;          /**< Interrupt event type */
200         uint32_t active;                        /**< Callback is executing */
201 };
202
203 enum {
204         STAT_QMAP_TX = 0,
205         STAT_QMAP_RX
206 };
207
208 int
209 rte_eth_iterator_init(struct rte_dev_iterator *iter, const char *devargs_str)
210 {
211         int ret;
212         struct rte_devargs devargs = {.args = NULL};
213         const char *bus_param_key;
214         char *bus_str = NULL;
215         char *cls_str = NULL;
216         int str_size;
217
218         memset(iter, 0, sizeof(*iter));
219
220         /*
221          * The devargs string may use various syntaxes:
222          *   - 0000:08:00.0,representor=[1-3]
223          *   - pci:0000:06:00.0,representor=[0,5]
224          *   - class=eth,mac=00:11:22:33:44:55
225          * A new syntax is in development (not yet supported):
226          *   - bus=X,paramX=x/class=Y,paramY=y/driver=Z,paramZ=z
227          */
228
229         /*
230          * Handle pure class filter (i.e. without any bus-level argument),
231          * from future new syntax.
232          * rte_devargs_parse() is not yet supporting the new syntax,
233          * that's why this simple case is temporarily parsed here.
234          */
235 #define iter_anybus_str "class=eth,"
236         if (strncmp(devargs_str, iter_anybus_str,
237                         strlen(iter_anybus_str)) == 0) {
238                 iter->cls_str = devargs_str + strlen(iter_anybus_str);
239                 goto end;
240         }
241
242         /* Split bus, device and parameters. */
243         ret = rte_devargs_parse(&devargs, devargs_str);
244         if (ret != 0)
245                 goto error;
246
247         /*
248          * Assume parameters of old syntax can match only at ethdev level.
249          * Extra parameters will be ignored, thanks to "+" prefix.
250          */
251         str_size = strlen(devargs.args) + 2;
252         cls_str = malloc(str_size);
253         if (cls_str == NULL) {
254                 ret = -ENOMEM;
255                 goto error;
256         }
257         ret = snprintf(cls_str, str_size, "+%s", devargs.args);
258         if (ret != str_size - 1) {
259                 ret = -EINVAL;
260                 goto error;
261         }
262         iter->cls_str = cls_str;
263         free(devargs.args); /* allocated by rte_devargs_parse() */
264         devargs.args = NULL;
265
266         iter->bus = devargs.bus;
267         if (iter->bus->dev_iterate == NULL) {
268                 ret = -ENOTSUP;
269                 goto error;
270         }
271
272         /* Convert bus args to new syntax for use with new API dev_iterate. */
273         if (strcmp(iter->bus->name, "vdev") == 0) {
274                 bus_param_key = "name";
275         } else if (strcmp(iter->bus->name, "pci") == 0) {
276                 bus_param_key = "addr";
277         } else {
278                 ret = -ENOTSUP;
279                 goto error;
280         }
281         str_size = strlen(bus_param_key) + strlen(devargs.name) + 2;
282         bus_str = malloc(str_size);
283         if (bus_str == NULL) {
284                 ret = -ENOMEM;
285                 goto error;
286         }
287         ret = snprintf(bus_str, str_size, "%s=%s",
288                         bus_param_key, devargs.name);
289         if (ret != str_size - 1) {
290                 ret = -EINVAL;
291                 goto error;
292         }
293         iter->bus_str = bus_str;
294
295 end:
296         iter->cls = rte_class_find_by_name("eth");
297         return 0;
298
299 error:
300         if (ret == -ENOTSUP)
301                 RTE_LOG(ERR, EAL, "Bus %s does not support iterating.\n",
302                                 iter->bus->name);
303         free(devargs.args);
304         free(bus_str);
305         free(cls_str);
306         return ret;
307 }
308
309 uint16_t
310 rte_eth_iterator_next(struct rte_dev_iterator *iter)
311 {
312         if (iter->cls == NULL) /* invalid ethdev iterator */
313                 return RTE_MAX_ETHPORTS;
314
315         do { /* loop to try all matching rte_device */
316                 /* If not pure ethdev filter and */
317                 if (iter->bus != NULL &&
318                                 /* not in middle of rte_eth_dev iteration, */
319                                 iter->class_device == NULL) {
320                         /* get next rte_device to try. */
321                         iter->device = iter->bus->dev_iterate(
322                                         iter->device, iter->bus_str, iter);
323                         if (iter->device == NULL)
324                                 break; /* no more rte_device candidate */
325                 }
326                 /* A device is matching bus part, need to check ethdev part. */
327                 iter->class_device = iter->cls->dev_iterate(
328                                 iter->class_device, iter->cls_str, iter);
329                 if (iter->class_device != NULL)
330                         return eth_dev_to_id(iter->class_device); /* match */
331         } while (iter->bus != NULL); /* need to try next rte_device */
332
333         /* No more ethdev port to iterate. */
334         rte_eth_iterator_cleanup(iter);
335         return RTE_MAX_ETHPORTS;
336 }
337
338 void
339 rte_eth_iterator_cleanup(struct rte_dev_iterator *iter)
340 {
341         if (iter->bus_str == NULL)
342                 return; /* nothing to free in pure class filter */
343         free(RTE_CAST_FIELD(iter, bus_str, char *)); /* workaround const */
344         free(RTE_CAST_FIELD(iter, cls_str, char *)); /* workaround const */
345         memset(iter, 0, sizeof(*iter));
346 }
347
348 uint16_t
349 rte_eth_find_next(uint16_t port_id)
350 {
351         while (port_id < RTE_MAX_ETHPORTS &&
352                         rte_eth_devices[port_id].state == RTE_ETH_DEV_UNUSED)
353                 port_id++;
354
355         if (port_id >= RTE_MAX_ETHPORTS)
356                 return RTE_MAX_ETHPORTS;
357
358         return port_id;
359 }
360
361 /*
362  * Macro to iterate over all valid ports for internal usage.
363  * Note: RTE_ETH_FOREACH_DEV is different because filtering owned ports.
364  */
365 #define RTE_ETH_FOREACH_VALID_DEV(port_id) \
366         for (port_id = rte_eth_find_next(0); \
367              port_id < RTE_MAX_ETHPORTS; \
368              port_id = rte_eth_find_next(port_id + 1))
369
370 uint16_t
371 rte_eth_find_next_of(uint16_t port_id, const struct rte_device *parent)
372 {
373         port_id = rte_eth_find_next(port_id);
374         while (port_id < RTE_MAX_ETHPORTS &&
375                         rte_eth_devices[port_id].device != parent)
376                 port_id = rte_eth_find_next(port_id + 1);
377
378         return port_id;
379 }
380
381 uint16_t
382 rte_eth_find_next_sibling(uint16_t port_id, uint16_t ref_port_id)
383 {
384         RTE_ETH_VALID_PORTID_OR_ERR_RET(ref_port_id, RTE_MAX_ETHPORTS);
385         return rte_eth_find_next_of(port_id,
386                         rte_eth_devices[ref_port_id].device);
387 }
388
389 static void
390 rte_eth_dev_shared_data_prepare(void)
391 {
392         const unsigned flags = 0;
393         const struct rte_memzone *mz;
394
395         rte_spinlock_lock(&rte_eth_shared_data_lock);
396
397         if (rte_eth_dev_shared_data == NULL) {
398                 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
399                         /* Allocate port data and ownership shared memory. */
400                         mz = rte_memzone_reserve(MZ_RTE_ETH_DEV_DATA,
401                                         sizeof(*rte_eth_dev_shared_data),
402                                         rte_socket_id(), flags);
403                 } else
404                         mz = rte_memzone_lookup(MZ_RTE_ETH_DEV_DATA);
405                 if (mz == NULL)
406                         rte_panic("Cannot allocate ethdev shared data\n");
407
408                 rte_eth_dev_shared_data = mz->addr;
409                 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
410                         rte_eth_dev_shared_data->next_owner_id =
411                                         RTE_ETH_DEV_NO_OWNER + 1;
412                         rte_spinlock_init(&rte_eth_dev_shared_data->ownership_lock);
413                         memset(rte_eth_dev_shared_data->data, 0,
414                                sizeof(rte_eth_dev_shared_data->data));
415                 }
416         }
417
418         rte_spinlock_unlock(&rte_eth_shared_data_lock);
419 }
420
421 static bool
422 is_allocated(const struct rte_eth_dev *ethdev)
423 {
424         return ethdev->data->name[0] != '\0';
425 }
426
427 static struct rte_eth_dev *
428 _rte_eth_dev_allocated(const char *name)
429 {
430         unsigned i;
431
432         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
433                 if (rte_eth_devices[i].data != NULL &&
434                     strcmp(rte_eth_devices[i].data->name, name) == 0)
435                         return &rte_eth_devices[i];
436         }
437         return NULL;
438 }
439
440 struct rte_eth_dev *
441 rte_eth_dev_allocated(const char *name)
442 {
443         struct rte_eth_dev *ethdev;
444
445         rte_eth_dev_shared_data_prepare();
446
447         rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock);
448
449         ethdev = _rte_eth_dev_allocated(name);
450
451         rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock);
452
453         return ethdev;
454 }
455
456 static uint16_t
457 rte_eth_dev_find_free_port(void)
458 {
459         unsigned i;
460
461         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
462                 /* Using shared name field to find a free port. */
463                 if (rte_eth_dev_shared_data->data[i].name[0] == '\0') {
464                         RTE_ASSERT(rte_eth_devices[i].state ==
465                                    RTE_ETH_DEV_UNUSED);
466                         return i;
467                 }
468         }
469         return RTE_MAX_ETHPORTS;
470 }
471
472 static struct rte_eth_dev *
473 eth_dev_get(uint16_t port_id)
474 {
475         struct rte_eth_dev *eth_dev = &rte_eth_devices[port_id];
476
477         eth_dev->data = &rte_eth_dev_shared_data->data[port_id];
478
479         return eth_dev;
480 }
481
482 struct rte_eth_dev *
483 rte_eth_dev_allocate(const char *name)
484 {
485         uint16_t port_id;
486         struct rte_eth_dev *eth_dev = NULL;
487         size_t name_len;
488
489         name_len = strnlen(name, RTE_ETH_NAME_MAX_LEN);
490         if (name_len == 0) {
491                 RTE_ETHDEV_LOG(ERR, "Zero length Ethernet device name\n");
492                 return NULL;
493         }
494
495         if (name_len >= RTE_ETH_NAME_MAX_LEN) {
496                 RTE_ETHDEV_LOG(ERR, "Ethernet device name is too long\n");
497                 return NULL;
498         }
499
500         rte_eth_dev_shared_data_prepare();
501
502         /* Synchronize port creation between primary and secondary threads. */
503         rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock);
504
505         if (_rte_eth_dev_allocated(name) != NULL) {
506                 RTE_ETHDEV_LOG(ERR,
507                         "Ethernet device with name %s already allocated\n",
508                         name);
509                 goto unlock;
510         }
511
512         port_id = rte_eth_dev_find_free_port();
513         if (port_id == RTE_MAX_ETHPORTS) {
514                 RTE_ETHDEV_LOG(ERR,
515                         "Reached maximum number of Ethernet ports\n");
516                 goto unlock;
517         }
518
519         eth_dev = eth_dev_get(port_id);
520         strlcpy(eth_dev->data->name, name, sizeof(eth_dev->data->name));
521         eth_dev->data->port_id = port_id;
522         eth_dev->data->mtu = RTE_ETHER_MTU;
523
524 unlock:
525         rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock);
526
527         return eth_dev;
528 }
529
530 /*
531  * Attach to a port already registered by the primary process, which
532  * makes sure that the same device would have the same port id both
533  * in the primary and secondary process.
534  */
535 struct rte_eth_dev *
536 rte_eth_dev_attach_secondary(const char *name)
537 {
538         uint16_t i;
539         struct rte_eth_dev *eth_dev = NULL;
540
541         rte_eth_dev_shared_data_prepare();
542
543         /* Synchronize port attachment to primary port creation and release. */
544         rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock);
545
546         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
547                 if (strcmp(rte_eth_dev_shared_data->data[i].name, name) == 0)
548                         break;
549         }
550         if (i == RTE_MAX_ETHPORTS) {
551                 RTE_ETHDEV_LOG(ERR,
552                         "Device %s is not driven by the primary process\n",
553                         name);
554         } else {
555                 eth_dev = eth_dev_get(i);
556                 RTE_ASSERT(eth_dev->data->port_id == i);
557         }
558
559         rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock);
560         return eth_dev;
561 }
562
563 int
564 rte_eth_dev_release_port(struct rte_eth_dev *eth_dev)
565 {
566         if (eth_dev == NULL)
567                 return -EINVAL;
568
569         rte_eth_dev_shared_data_prepare();
570
571         if (eth_dev->state != RTE_ETH_DEV_UNUSED)
572                 _rte_eth_dev_callback_process(eth_dev,
573                                 RTE_ETH_EVENT_DESTROY, NULL);
574
575         rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock);
576
577         eth_dev->state = RTE_ETH_DEV_UNUSED;
578
579         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
580                 rte_free(eth_dev->data->rx_queues);
581                 rte_free(eth_dev->data->tx_queues);
582                 rte_free(eth_dev->data->mac_addrs);
583                 rte_free(eth_dev->data->hash_mac_addrs);
584                 rte_free(eth_dev->data->dev_private);
585                 memset(eth_dev->data, 0, sizeof(struct rte_eth_dev_data));
586         }
587
588         rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock);
589
590         return 0;
591 }
592
593 int
594 rte_eth_dev_is_valid_port(uint16_t port_id)
595 {
596         if (port_id >= RTE_MAX_ETHPORTS ||
597             (rte_eth_devices[port_id].state == RTE_ETH_DEV_UNUSED))
598                 return 0;
599         else
600                 return 1;
601 }
602
603 static int
604 rte_eth_is_valid_owner_id(uint64_t owner_id)
605 {
606         if (owner_id == RTE_ETH_DEV_NO_OWNER ||
607             rte_eth_dev_shared_data->next_owner_id <= owner_id)
608                 return 0;
609         return 1;
610 }
611
612 uint64_t
613 rte_eth_find_next_owned_by(uint16_t port_id, const uint64_t owner_id)
614 {
615         port_id = rte_eth_find_next(port_id);
616         while (port_id < RTE_MAX_ETHPORTS &&
617                         rte_eth_devices[port_id].data->owner.id != owner_id)
618                 port_id = rte_eth_find_next(port_id + 1);
619
620         return port_id;
621 }
622
623 int
624 rte_eth_dev_owner_new(uint64_t *owner_id)
625 {
626         rte_eth_dev_shared_data_prepare();
627
628         rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock);
629
630         *owner_id = rte_eth_dev_shared_data->next_owner_id++;
631
632         rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock);
633         return 0;
634 }
635
636 static int
637 _rte_eth_dev_owner_set(const uint16_t port_id, const uint64_t old_owner_id,
638                        const struct rte_eth_dev_owner *new_owner)
639 {
640         struct rte_eth_dev *ethdev = &rte_eth_devices[port_id];
641         struct rte_eth_dev_owner *port_owner;
642
643         if (port_id >= RTE_MAX_ETHPORTS || !is_allocated(ethdev)) {
644                 RTE_ETHDEV_LOG(ERR, "Port id %"PRIu16" is not allocated\n",
645                         port_id);
646                 return -ENODEV;
647         }
648
649         if (!rte_eth_is_valid_owner_id(new_owner->id) &&
650             !rte_eth_is_valid_owner_id(old_owner_id)) {
651                 RTE_ETHDEV_LOG(ERR,
652                         "Invalid owner old_id=%016"PRIx64" new_id=%016"PRIx64"\n",
653                        old_owner_id, new_owner->id);
654                 return -EINVAL;
655         }
656
657         port_owner = &rte_eth_devices[port_id].data->owner;
658         if (port_owner->id != old_owner_id) {
659                 RTE_ETHDEV_LOG(ERR,
660                         "Cannot set owner to port %u already owned by %s_%016"PRIX64"\n",
661                         port_id, port_owner->name, port_owner->id);
662                 return -EPERM;
663         }
664
665         /* can not truncate (same structure) */
666         strlcpy(port_owner->name, new_owner->name, RTE_ETH_MAX_OWNER_NAME_LEN);
667
668         port_owner->id = new_owner->id;
669
670         RTE_ETHDEV_LOG(DEBUG, "Port %u owner is %s_%016"PRIx64"\n",
671                 port_id, new_owner->name, new_owner->id);
672
673         return 0;
674 }
675
676 int
677 rte_eth_dev_owner_set(const uint16_t port_id,
678                       const struct rte_eth_dev_owner *owner)
679 {
680         int ret;
681
682         rte_eth_dev_shared_data_prepare();
683
684         rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock);
685
686         ret = _rte_eth_dev_owner_set(port_id, RTE_ETH_DEV_NO_OWNER, owner);
687
688         rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock);
689         return ret;
690 }
691
692 int
693 rte_eth_dev_owner_unset(const uint16_t port_id, const uint64_t owner_id)
694 {
695         const struct rte_eth_dev_owner new_owner = (struct rte_eth_dev_owner)
696                         {.id = RTE_ETH_DEV_NO_OWNER, .name = ""};
697         int ret;
698
699         rte_eth_dev_shared_data_prepare();
700
701         rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock);
702
703         ret = _rte_eth_dev_owner_set(port_id, owner_id, &new_owner);
704
705         rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock);
706         return ret;
707 }
708
709 int
710 rte_eth_dev_owner_delete(const uint64_t owner_id)
711 {
712         uint16_t port_id;
713         int ret = 0;
714
715         rte_eth_dev_shared_data_prepare();
716
717         rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock);
718
719         if (rte_eth_is_valid_owner_id(owner_id)) {
720                 for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++)
721                         if (rte_eth_devices[port_id].data->owner.id == owner_id)
722                                 memset(&rte_eth_devices[port_id].data->owner, 0,
723                                        sizeof(struct rte_eth_dev_owner));
724                 RTE_ETHDEV_LOG(NOTICE,
725                         "All port owners owned by %016"PRIx64" identifier have removed\n",
726                         owner_id);
727         } else {
728                 RTE_ETHDEV_LOG(ERR,
729                                "Invalid owner id=%016"PRIx64"\n",
730                                owner_id);
731                 ret = -EINVAL;
732         }
733
734         rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock);
735
736         return ret;
737 }
738
739 int
740 rte_eth_dev_owner_get(const uint16_t port_id, struct rte_eth_dev_owner *owner)
741 {
742         int ret = 0;
743         struct rte_eth_dev *ethdev = &rte_eth_devices[port_id];
744
745         rte_eth_dev_shared_data_prepare();
746
747         rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock);
748
749         if (port_id >= RTE_MAX_ETHPORTS || !is_allocated(ethdev)) {
750                 RTE_ETHDEV_LOG(ERR, "Port id %"PRIu16" is not allocated\n",
751                         port_id);
752                 ret = -ENODEV;
753         } else {
754                 rte_memcpy(owner, &ethdev->data->owner, sizeof(*owner));
755         }
756
757         rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock);
758         return ret;
759 }
760
761 int
762 rte_eth_dev_socket_id(uint16_t port_id)
763 {
764         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -1);
765         return rte_eth_devices[port_id].data->numa_node;
766 }
767
768 void *
769 rte_eth_dev_get_sec_ctx(uint16_t port_id)
770 {
771         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, NULL);
772         return rte_eth_devices[port_id].security_ctx;
773 }
774
775 uint16_t
776 rte_eth_dev_count(void)
777 {
778         return rte_eth_dev_count_avail();
779 }
780
781 uint16_t
782 rte_eth_dev_count_avail(void)
783 {
784         uint16_t p;
785         uint16_t count;
786
787         count = 0;
788
789         RTE_ETH_FOREACH_DEV(p)
790                 count++;
791
792         return count;
793 }
794
795 uint16_t
796 rte_eth_dev_count_total(void)
797 {
798         uint16_t port, count = 0;
799
800         RTE_ETH_FOREACH_VALID_DEV(port)
801                 count++;
802
803         return count;
804 }
805
806 int
807 rte_eth_dev_get_name_by_port(uint16_t port_id, char *name)
808 {
809         char *tmp;
810
811         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
812
813         if (name == NULL) {
814                 RTE_ETHDEV_LOG(ERR, "Null pointer is specified\n");
815                 return -EINVAL;
816         }
817
818         /* shouldn't check 'rte_eth_devices[i].data',
819          * because it might be overwritten by VDEV PMD */
820         tmp = rte_eth_dev_shared_data->data[port_id].name;
821         strcpy(name, tmp);
822         return 0;
823 }
824
825 int
826 rte_eth_dev_get_port_by_name(const char *name, uint16_t *port_id)
827 {
828         uint32_t pid;
829
830         if (name == NULL) {
831                 RTE_ETHDEV_LOG(ERR, "Null pointer is specified\n");
832                 return -EINVAL;
833         }
834
835         RTE_ETH_FOREACH_VALID_DEV(pid)
836                 if (!strcmp(name, rte_eth_dev_shared_data->data[pid].name)) {
837                         *port_id = pid;
838                         return 0;
839                 }
840
841         return -ENODEV;
842 }
843
844 static int
845 eth_err(uint16_t port_id, int ret)
846 {
847         if (ret == 0)
848                 return 0;
849         if (rte_eth_dev_is_removed(port_id))
850                 return -EIO;
851         return ret;
852 }
853
854 static int
855 rte_eth_dev_rx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
856 {
857         uint16_t old_nb_queues = dev->data->nb_rx_queues;
858         void **rxq;
859         unsigned i;
860
861         if (dev->data->rx_queues == NULL && nb_queues != 0) { /* first time configuration */
862                 dev->data->rx_queues = rte_zmalloc("ethdev->rx_queues",
863                                 sizeof(dev->data->rx_queues[0]) * nb_queues,
864                                 RTE_CACHE_LINE_SIZE);
865                 if (dev->data->rx_queues == NULL) {
866                         dev->data->nb_rx_queues = 0;
867                         return -(ENOMEM);
868                 }
869         } else if (dev->data->rx_queues != NULL && nb_queues != 0) { /* re-configure */
870                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_release, -ENOTSUP);
871
872                 rxq = dev->data->rx_queues;
873
874                 for (i = nb_queues; i < old_nb_queues; i++)
875                         (*dev->dev_ops->rx_queue_release)(rxq[i]);
876                 rxq = rte_realloc(rxq, sizeof(rxq[0]) * nb_queues,
877                                 RTE_CACHE_LINE_SIZE);
878                 if (rxq == NULL)
879                         return -(ENOMEM);
880                 if (nb_queues > old_nb_queues) {
881                         uint16_t new_qs = nb_queues - old_nb_queues;
882
883                         memset(rxq + old_nb_queues, 0,
884                                 sizeof(rxq[0]) * new_qs);
885                 }
886
887                 dev->data->rx_queues = rxq;
888
889         } else if (dev->data->rx_queues != NULL && nb_queues == 0) {
890                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_release, -ENOTSUP);
891
892                 rxq = dev->data->rx_queues;
893
894                 for (i = nb_queues; i < old_nb_queues; i++)
895                         (*dev->dev_ops->rx_queue_release)(rxq[i]);
896
897                 rte_free(dev->data->rx_queues);
898                 dev->data->rx_queues = NULL;
899         }
900         dev->data->nb_rx_queues = nb_queues;
901         return 0;
902 }
903
904 int
905 rte_eth_dev_rx_queue_start(uint16_t port_id, uint16_t rx_queue_id)
906 {
907         struct rte_eth_dev *dev;
908
909         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
910
911         dev = &rte_eth_devices[port_id];
912         if (!dev->data->dev_started) {
913                 RTE_ETHDEV_LOG(ERR,
914                         "Port %u must be started before start any queue\n",
915                         port_id);
916                 return -EINVAL;
917         }
918
919         if (rx_queue_id >= dev->data->nb_rx_queues) {
920                 RTE_ETHDEV_LOG(ERR, "Invalid RX queue_id=%u\n", rx_queue_id);
921                 return -EINVAL;
922         }
923
924         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_start, -ENOTSUP);
925
926         if (dev->data->rx_queue_state[rx_queue_id] != RTE_ETH_QUEUE_STATE_STOPPED) {
927                 RTE_ETHDEV_LOG(INFO,
928                         "Queue %"PRIu16" of device with port_id=%"PRIu16" already started\n",
929                         rx_queue_id, port_id);
930                 return 0;
931         }
932
933         return eth_err(port_id, dev->dev_ops->rx_queue_start(dev,
934                                                              rx_queue_id));
935
936 }
937
938 int
939 rte_eth_dev_rx_queue_stop(uint16_t port_id, uint16_t rx_queue_id)
940 {
941         struct rte_eth_dev *dev;
942
943         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
944
945         dev = &rte_eth_devices[port_id];
946         if (rx_queue_id >= dev->data->nb_rx_queues) {
947                 RTE_ETHDEV_LOG(ERR, "Invalid RX queue_id=%u\n", rx_queue_id);
948                 return -EINVAL;
949         }
950
951         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_stop, -ENOTSUP);
952
953         if (dev->data->rx_queue_state[rx_queue_id] == RTE_ETH_QUEUE_STATE_STOPPED) {
954                 RTE_ETHDEV_LOG(INFO,
955                         "Queue %"PRIu16" of device with port_id=%"PRIu16" already stopped\n",
956                         rx_queue_id, port_id);
957                 return 0;
958         }
959
960         return eth_err(port_id, dev->dev_ops->rx_queue_stop(dev, rx_queue_id));
961
962 }
963
964 int
965 rte_eth_dev_tx_queue_start(uint16_t port_id, uint16_t tx_queue_id)
966 {
967         struct rte_eth_dev *dev;
968
969         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
970
971         dev = &rte_eth_devices[port_id];
972         if (!dev->data->dev_started) {
973                 RTE_ETHDEV_LOG(ERR,
974                         "Port %u must be started before start any queue\n",
975                         port_id);
976                 return -EINVAL;
977         }
978
979         if (tx_queue_id >= dev->data->nb_tx_queues) {
980                 RTE_ETHDEV_LOG(ERR, "Invalid TX queue_id=%u\n", tx_queue_id);
981                 return -EINVAL;
982         }
983
984         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_start, -ENOTSUP);
985
986         if (dev->data->tx_queue_state[tx_queue_id] != RTE_ETH_QUEUE_STATE_STOPPED) {
987                 RTE_ETHDEV_LOG(INFO,
988                         "Queue %"PRIu16" of device with port_id=%"PRIu16" already started\n",
989                         tx_queue_id, port_id);
990                 return 0;
991         }
992
993         return eth_err(port_id, dev->dev_ops->tx_queue_start(dev, tx_queue_id));
994 }
995
996 int
997 rte_eth_dev_tx_queue_stop(uint16_t port_id, uint16_t tx_queue_id)
998 {
999         struct rte_eth_dev *dev;
1000
1001         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1002
1003         dev = &rte_eth_devices[port_id];
1004         if (tx_queue_id >= dev->data->nb_tx_queues) {
1005                 RTE_ETHDEV_LOG(ERR, "Invalid TX queue_id=%u\n", tx_queue_id);
1006                 return -EINVAL;
1007         }
1008
1009         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_stop, -ENOTSUP);
1010
1011         if (dev->data->tx_queue_state[tx_queue_id] == RTE_ETH_QUEUE_STATE_STOPPED) {
1012                 RTE_ETHDEV_LOG(INFO,
1013                         "Queue %"PRIu16" of device with port_id=%"PRIu16" already stopped\n",
1014                         tx_queue_id, port_id);
1015                 return 0;
1016         }
1017
1018         return eth_err(port_id, dev->dev_ops->tx_queue_stop(dev, tx_queue_id));
1019
1020 }
1021
1022 static int
1023 rte_eth_dev_tx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
1024 {
1025         uint16_t old_nb_queues = dev->data->nb_tx_queues;
1026         void **txq;
1027         unsigned i;
1028
1029         if (dev->data->tx_queues == NULL && nb_queues != 0) { /* first time configuration */
1030                 dev->data->tx_queues = rte_zmalloc("ethdev->tx_queues",
1031                                                    sizeof(dev->data->tx_queues[0]) * nb_queues,
1032                                                    RTE_CACHE_LINE_SIZE);
1033                 if (dev->data->tx_queues == NULL) {
1034                         dev->data->nb_tx_queues = 0;
1035                         return -(ENOMEM);
1036                 }
1037         } else if (dev->data->tx_queues != NULL && nb_queues != 0) { /* re-configure */
1038                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_release, -ENOTSUP);
1039
1040                 txq = dev->data->tx_queues;
1041
1042                 for (i = nb_queues; i < old_nb_queues; i++)
1043                         (*dev->dev_ops->tx_queue_release)(txq[i]);
1044                 txq = rte_realloc(txq, sizeof(txq[0]) * nb_queues,
1045                                   RTE_CACHE_LINE_SIZE);
1046                 if (txq == NULL)
1047                         return -ENOMEM;
1048                 if (nb_queues > old_nb_queues) {
1049                         uint16_t new_qs = nb_queues - old_nb_queues;
1050
1051                         memset(txq + old_nb_queues, 0,
1052                                sizeof(txq[0]) * new_qs);
1053                 }
1054
1055                 dev->data->tx_queues = txq;
1056
1057         } else if (dev->data->tx_queues != NULL && nb_queues == 0) {
1058                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_release, -ENOTSUP);
1059
1060                 txq = dev->data->tx_queues;
1061
1062                 for (i = nb_queues; i < old_nb_queues; i++)
1063                         (*dev->dev_ops->tx_queue_release)(txq[i]);
1064
1065                 rte_free(dev->data->tx_queues);
1066                 dev->data->tx_queues = NULL;
1067         }
1068         dev->data->nb_tx_queues = nb_queues;
1069         return 0;
1070 }
1071
1072 uint32_t
1073 rte_eth_speed_bitflag(uint32_t speed, int duplex)
1074 {
1075         switch (speed) {
1076         case ETH_SPEED_NUM_10M:
1077                 return duplex ? ETH_LINK_SPEED_10M : ETH_LINK_SPEED_10M_HD;
1078         case ETH_SPEED_NUM_100M:
1079                 return duplex ? ETH_LINK_SPEED_100M : ETH_LINK_SPEED_100M_HD;
1080         case ETH_SPEED_NUM_1G:
1081                 return ETH_LINK_SPEED_1G;
1082         case ETH_SPEED_NUM_2_5G:
1083                 return ETH_LINK_SPEED_2_5G;
1084         case ETH_SPEED_NUM_5G:
1085                 return ETH_LINK_SPEED_5G;
1086         case ETH_SPEED_NUM_10G:
1087                 return ETH_LINK_SPEED_10G;
1088         case ETH_SPEED_NUM_20G:
1089                 return ETH_LINK_SPEED_20G;
1090         case ETH_SPEED_NUM_25G:
1091                 return ETH_LINK_SPEED_25G;
1092         case ETH_SPEED_NUM_40G:
1093                 return ETH_LINK_SPEED_40G;
1094         case ETH_SPEED_NUM_50G:
1095                 return ETH_LINK_SPEED_50G;
1096         case ETH_SPEED_NUM_56G:
1097                 return ETH_LINK_SPEED_56G;
1098         case ETH_SPEED_NUM_100G:
1099                 return ETH_LINK_SPEED_100G;
1100         default:
1101                 return 0;
1102         }
1103 }
1104
1105 const char *
1106 rte_eth_dev_rx_offload_name(uint64_t offload)
1107 {
1108         const char *name = "UNKNOWN";
1109         unsigned int i;
1110
1111         for (i = 0; i < RTE_DIM(rte_rx_offload_names); ++i) {
1112                 if (offload == rte_rx_offload_names[i].offload) {
1113                         name = rte_rx_offload_names[i].name;
1114                         break;
1115                 }
1116         }
1117
1118         return name;
1119 }
1120
1121 const char *
1122 rte_eth_dev_tx_offload_name(uint64_t offload)
1123 {
1124         const char *name = "UNKNOWN";
1125         unsigned int i;
1126
1127         for (i = 0; i < RTE_DIM(rte_tx_offload_names); ++i) {
1128                 if (offload == rte_tx_offload_names[i].offload) {
1129                         name = rte_tx_offload_names[i].name;
1130                         break;
1131                 }
1132         }
1133
1134         return name;
1135 }
1136
1137 int
1138 rte_eth_dev_configure(uint16_t port_id, uint16_t nb_rx_q, uint16_t nb_tx_q,
1139                       const struct rte_eth_conf *dev_conf)
1140 {
1141         struct rte_eth_dev *dev;
1142         struct rte_eth_dev_info dev_info;
1143         struct rte_eth_conf orig_conf;
1144         int diag;
1145         int ret;
1146
1147         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1148
1149         dev = &rte_eth_devices[port_id];
1150
1151         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_configure, -ENOTSUP);
1152
1153         if (dev->data->dev_started) {
1154                 RTE_ETHDEV_LOG(ERR,
1155                         "Port %u must be stopped to allow configuration\n",
1156                         port_id);
1157                 return -EBUSY;
1158         }
1159
1160          /* Store original config, as rollback required on failure */
1161         memcpy(&orig_conf, &dev->data->dev_conf, sizeof(dev->data->dev_conf));
1162
1163         /*
1164          * Copy the dev_conf parameter into the dev structure.
1165          * rte_eth_dev_info_get() requires dev_conf, copy it before dev_info get
1166          */
1167         memcpy(&dev->data->dev_conf, dev_conf, sizeof(dev->data->dev_conf));
1168
1169         ret = rte_eth_dev_info_get(port_id, &dev_info);
1170         if (ret != 0)
1171                 goto rollback;
1172
1173         /* If number of queues specified by application for both Rx and Tx is
1174          * zero, use driver preferred values. This cannot be done individually
1175          * as it is valid for either Tx or Rx (but not both) to be zero.
1176          * If driver does not provide any preferred valued, fall back on
1177          * EAL defaults.
1178          */
1179         if (nb_rx_q == 0 && nb_tx_q == 0) {
1180                 nb_rx_q = dev_info.default_rxportconf.nb_queues;
1181                 if (nb_rx_q == 0)
1182                         nb_rx_q = RTE_ETH_DEV_FALLBACK_RX_NBQUEUES;
1183                 nb_tx_q = dev_info.default_txportconf.nb_queues;
1184                 if (nb_tx_q == 0)
1185                         nb_tx_q = RTE_ETH_DEV_FALLBACK_TX_NBQUEUES;
1186         }
1187
1188         if (nb_rx_q > RTE_MAX_QUEUES_PER_PORT) {
1189                 RTE_ETHDEV_LOG(ERR,
1190                         "Number of RX queues requested (%u) is greater than max supported(%d)\n",
1191                         nb_rx_q, RTE_MAX_QUEUES_PER_PORT);
1192                 ret = -EINVAL;
1193                 goto rollback;
1194         }
1195
1196         if (nb_tx_q > RTE_MAX_QUEUES_PER_PORT) {
1197                 RTE_ETHDEV_LOG(ERR,
1198                         "Number of TX queues requested (%u) is greater than max supported(%d)\n",
1199                         nb_tx_q, RTE_MAX_QUEUES_PER_PORT);
1200                 ret = -EINVAL;
1201                 goto rollback;
1202         }
1203
1204         /*
1205          * Check that the numbers of RX and TX queues are not greater
1206          * than the maximum number of RX and TX queues supported by the
1207          * configured device.
1208          */
1209         if (nb_rx_q > dev_info.max_rx_queues) {
1210                 RTE_ETHDEV_LOG(ERR, "Ethdev port_id=%u nb_rx_queues=%u > %u\n",
1211                         port_id, nb_rx_q, dev_info.max_rx_queues);
1212                 ret = -EINVAL;
1213                 goto rollback;
1214         }
1215
1216         if (nb_tx_q > dev_info.max_tx_queues) {
1217                 RTE_ETHDEV_LOG(ERR, "Ethdev port_id=%u nb_tx_queues=%u > %u\n",
1218                         port_id, nb_tx_q, dev_info.max_tx_queues);
1219                 ret = -EINVAL;
1220                 goto rollback;
1221         }
1222
1223         /* Check that the device supports requested interrupts */
1224         if ((dev_conf->intr_conf.lsc == 1) &&
1225                         (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC))) {
1226                 RTE_ETHDEV_LOG(ERR, "Driver %s does not support lsc\n",
1227                         dev->device->driver->name);
1228                 ret = -EINVAL;
1229                 goto rollback;
1230         }
1231         if ((dev_conf->intr_conf.rmv == 1) &&
1232                         (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_RMV))) {
1233                 RTE_ETHDEV_LOG(ERR, "Driver %s does not support rmv\n",
1234                         dev->device->driver->name);
1235                 ret = -EINVAL;
1236                 goto rollback;
1237         }
1238
1239         /*
1240          * If jumbo frames are enabled, check that the maximum RX packet
1241          * length is supported by the configured device.
1242          */
1243         if (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
1244                 if (dev_conf->rxmode.max_rx_pkt_len > dev_info.max_rx_pktlen) {
1245                         RTE_ETHDEV_LOG(ERR,
1246                                 "Ethdev port_id=%u max_rx_pkt_len %u > max valid value %u\n",
1247                                 port_id, dev_conf->rxmode.max_rx_pkt_len,
1248                                 dev_info.max_rx_pktlen);
1249                         ret = -EINVAL;
1250                         goto rollback;
1251                 } else if (dev_conf->rxmode.max_rx_pkt_len < RTE_ETHER_MIN_LEN) {
1252                         RTE_ETHDEV_LOG(ERR,
1253                                 "Ethdev port_id=%u max_rx_pkt_len %u < min valid value %u\n",
1254                                 port_id, dev_conf->rxmode.max_rx_pkt_len,
1255                                 (unsigned int)RTE_ETHER_MIN_LEN);
1256                         ret = -EINVAL;
1257                         goto rollback;
1258                 }
1259         } else {
1260                 if (dev_conf->rxmode.max_rx_pkt_len < RTE_ETHER_MIN_LEN ||
1261                         dev_conf->rxmode.max_rx_pkt_len > RTE_ETHER_MAX_LEN)
1262                         /* Use default value */
1263                         dev->data->dev_conf.rxmode.max_rx_pkt_len =
1264                                                         RTE_ETHER_MAX_LEN;
1265         }
1266
1267         /* Any requested offloading must be within its device capabilities */
1268         if ((dev_conf->rxmode.offloads & dev_info.rx_offload_capa) !=
1269              dev_conf->rxmode.offloads) {
1270                 RTE_ETHDEV_LOG(ERR,
1271                         "Ethdev port_id=%u requested Rx offloads 0x%"PRIx64" doesn't match Rx offloads "
1272                         "capabilities 0x%"PRIx64" in %s()\n",
1273                         port_id, dev_conf->rxmode.offloads,
1274                         dev_info.rx_offload_capa,
1275                         __func__);
1276                 ret = -EINVAL;
1277                 goto rollback;
1278         }
1279         if ((dev_conf->txmode.offloads & dev_info.tx_offload_capa) !=
1280              dev_conf->txmode.offloads) {
1281                 RTE_ETHDEV_LOG(ERR,
1282                         "Ethdev port_id=%u requested Tx offloads 0x%"PRIx64" doesn't match Tx offloads "
1283                         "capabilities 0x%"PRIx64" in %s()\n",
1284                         port_id, dev_conf->txmode.offloads,
1285                         dev_info.tx_offload_capa,
1286                         __func__);
1287                 ret = -EINVAL;
1288                 goto rollback;
1289         }
1290
1291         dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf =
1292                 rte_eth_rss_hf_refine(dev_conf->rx_adv_conf.rss_conf.rss_hf);
1293
1294         /* Check that device supports requested rss hash functions. */
1295         if ((dev_info.flow_type_rss_offloads |
1296              dev_conf->rx_adv_conf.rss_conf.rss_hf) !=
1297             dev_info.flow_type_rss_offloads) {
1298                 RTE_ETHDEV_LOG(ERR,
1299                         "Ethdev port_id=%u invalid rss_hf: 0x%"PRIx64", valid value: 0x%"PRIx64"\n",
1300                         port_id, dev_conf->rx_adv_conf.rss_conf.rss_hf,
1301                         dev_info.flow_type_rss_offloads);
1302                 ret = -EINVAL;
1303                 goto rollback;
1304         }
1305
1306         /*
1307          * Setup new number of RX/TX queues and reconfigure device.
1308          */
1309         diag = rte_eth_dev_rx_queue_config(dev, nb_rx_q);
1310         if (diag != 0) {
1311                 RTE_ETHDEV_LOG(ERR,
1312                         "Port%u rte_eth_dev_rx_queue_config = %d\n",
1313                         port_id, diag);
1314                 ret = diag;
1315                 goto rollback;
1316         }
1317
1318         diag = rte_eth_dev_tx_queue_config(dev, nb_tx_q);
1319         if (diag != 0) {
1320                 RTE_ETHDEV_LOG(ERR,
1321                         "Port%u rte_eth_dev_tx_queue_config = %d\n",
1322                         port_id, diag);
1323                 rte_eth_dev_rx_queue_config(dev, 0);
1324                 ret = diag;
1325                 goto rollback;
1326         }
1327
1328         diag = (*dev->dev_ops->dev_configure)(dev);
1329         if (diag != 0) {
1330                 RTE_ETHDEV_LOG(ERR, "Port%u dev_configure = %d\n",
1331                         port_id, diag);
1332                 rte_eth_dev_rx_queue_config(dev, 0);
1333                 rte_eth_dev_tx_queue_config(dev, 0);
1334                 ret = eth_err(port_id, diag);
1335                 goto rollback;
1336         }
1337
1338         /* Initialize Rx profiling if enabled at compilation time. */
1339         diag = __rte_eth_dev_profile_init(port_id, dev);
1340         if (diag != 0) {
1341                 RTE_ETHDEV_LOG(ERR, "Port%u __rte_eth_dev_profile_init = %d\n",
1342                         port_id, diag);
1343                 rte_eth_dev_rx_queue_config(dev, 0);
1344                 rte_eth_dev_tx_queue_config(dev, 0);
1345                 ret = eth_err(port_id, diag);
1346                 goto rollback;
1347         }
1348
1349         return 0;
1350
1351 rollback:
1352         memcpy(&dev->data->dev_conf, &orig_conf, sizeof(dev->data->dev_conf));
1353
1354         return ret;
1355 }
1356
1357 void
1358 _rte_eth_dev_reset(struct rte_eth_dev *dev)
1359 {
1360         if (dev->data->dev_started) {
1361                 RTE_ETHDEV_LOG(ERR, "Port %u must be stopped to allow reset\n",
1362                         dev->data->port_id);
1363                 return;
1364         }
1365
1366         rte_eth_dev_rx_queue_config(dev, 0);
1367         rte_eth_dev_tx_queue_config(dev, 0);
1368
1369         memset(&dev->data->dev_conf, 0, sizeof(dev->data->dev_conf));
1370 }
1371
1372 static void
1373 rte_eth_dev_mac_restore(struct rte_eth_dev *dev,
1374                         struct rte_eth_dev_info *dev_info)
1375 {
1376         struct rte_ether_addr *addr;
1377         uint16_t i;
1378         uint32_t pool = 0;
1379         uint64_t pool_mask;
1380
1381         /* replay MAC address configuration including default MAC */
1382         addr = &dev->data->mac_addrs[0];
1383         if (*dev->dev_ops->mac_addr_set != NULL)
1384                 (*dev->dev_ops->mac_addr_set)(dev, addr);
1385         else if (*dev->dev_ops->mac_addr_add != NULL)
1386                 (*dev->dev_ops->mac_addr_add)(dev, addr, 0, pool);
1387
1388         if (*dev->dev_ops->mac_addr_add != NULL) {
1389                 for (i = 1; i < dev_info->max_mac_addrs; i++) {
1390                         addr = &dev->data->mac_addrs[i];
1391
1392                         /* skip zero address */
1393                         if (rte_is_zero_ether_addr(addr))
1394                                 continue;
1395
1396                         pool = 0;
1397                         pool_mask = dev->data->mac_pool_sel[i];
1398
1399                         do {
1400                                 if (pool_mask & 1ULL)
1401                                         (*dev->dev_ops->mac_addr_add)(dev,
1402                                                 addr, i, pool);
1403                                 pool_mask >>= 1;
1404                                 pool++;
1405                         } while (pool_mask);
1406                 }
1407         }
1408 }
1409
1410 static int
1411 rte_eth_dev_config_restore(struct rte_eth_dev *dev,
1412                            struct rte_eth_dev_info *dev_info, uint16_t port_id)
1413 {
1414         int ret;
1415
1416         if (!(*dev_info->dev_flags & RTE_ETH_DEV_NOLIVE_MAC_ADDR))
1417                 rte_eth_dev_mac_restore(dev, dev_info);
1418
1419         /* replay promiscuous configuration */
1420         /*
1421          * use callbacks directly since we don't need port_id check and
1422          * would like to bypass the same value set
1423          */
1424         if (rte_eth_promiscuous_get(port_id) == 1 &&
1425             *dev->dev_ops->promiscuous_enable != NULL) {
1426                 ret = eth_err(port_id,
1427                               (*dev->dev_ops->promiscuous_enable)(dev));
1428                 if (ret != 0 && ret != -ENOTSUP) {
1429                         RTE_ETHDEV_LOG(ERR,
1430                                 "Failed to enable promiscuous mode for device (port %u): %s\n",
1431                                 port_id, rte_strerror(-ret));
1432                         return ret;
1433                 }
1434         } else if (rte_eth_promiscuous_get(port_id) == 0 &&
1435                    *dev->dev_ops->promiscuous_disable != NULL) {
1436                 ret = eth_err(port_id,
1437                               (*dev->dev_ops->promiscuous_disable)(dev));
1438                 if (ret != 0 && ret != -ENOTSUP) {
1439                         RTE_ETHDEV_LOG(ERR,
1440                                 "Failed to disable promiscuous mode for device (port %u): %s\n",
1441                                 port_id, rte_strerror(-ret));
1442                         return ret;
1443                 }
1444         }
1445
1446         /* replay all multicast configuration */
1447         /*
1448          * use callbacks directly since we don't need port_id check and
1449          * would like to bypass the same value set
1450          */
1451         if (rte_eth_allmulticast_get(port_id) == 1 &&
1452             *dev->dev_ops->allmulticast_enable != NULL) {
1453                 ret = eth_err(port_id,
1454                               (*dev->dev_ops->allmulticast_enable)(dev));
1455                 if (ret != 0 && ret != -ENOTSUP) {
1456                         RTE_ETHDEV_LOG(ERR,
1457                                 "Failed to enable allmulticast mode for device (port %u): %s\n",
1458                                 port_id, rte_strerror(-ret));
1459                         return ret;
1460                 }
1461         } else if (rte_eth_allmulticast_get(port_id) == 0 &&
1462                    *dev->dev_ops->allmulticast_disable != NULL) {
1463                 ret = eth_err(port_id,
1464                               (*dev->dev_ops->allmulticast_disable)(dev));
1465                 if (ret != 0 && ret != -ENOTSUP) {
1466                         RTE_ETHDEV_LOG(ERR,
1467                                 "Failed to disable allmulticast mode for device (port %u): %s\n",
1468                                 port_id, rte_strerror(-ret));
1469                         return ret;
1470                 }
1471         }
1472
1473         return 0;
1474 }
1475
1476 int
1477 rte_eth_dev_start(uint16_t port_id)
1478 {
1479         struct rte_eth_dev *dev;
1480         struct rte_eth_dev_info dev_info;
1481         int diag;
1482         int ret;
1483
1484         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1485
1486         dev = &rte_eth_devices[port_id];
1487
1488         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
1489
1490         if (dev->data->dev_started != 0) {
1491                 RTE_ETHDEV_LOG(INFO,
1492                         "Device with port_id=%"PRIu16" already started\n",
1493                         port_id);
1494                 return 0;
1495         }
1496
1497         ret = rte_eth_dev_info_get(port_id, &dev_info);
1498         if (ret != 0)
1499                 return ret;
1500
1501         /* Lets restore MAC now if device does not support live change */
1502         if (*dev_info.dev_flags & RTE_ETH_DEV_NOLIVE_MAC_ADDR)
1503                 rte_eth_dev_mac_restore(dev, &dev_info);
1504
1505         diag = (*dev->dev_ops->dev_start)(dev);
1506         if (diag == 0)
1507                 dev->data->dev_started = 1;
1508         else
1509                 return eth_err(port_id, diag);
1510
1511         ret = rte_eth_dev_config_restore(dev, &dev_info, port_id);
1512         if (ret != 0) {
1513                 RTE_ETHDEV_LOG(ERR,
1514                         "Error during restoring configuration for device (port %u): %s\n",
1515                         port_id, rte_strerror(-ret));
1516                 rte_eth_dev_stop(port_id);
1517                 return ret;
1518         }
1519
1520         if (dev->data->dev_conf.intr_conf.lsc == 0) {
1521                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->link_update, -ENOTSUP);
1522                 (*dev->dev_ops->link_update)(dev, 0);
1523         }
1524         return 0;
1525 }
1526
1527 void
1528 rte_eth_dev_stop(uint16_t port_id)
1529 {
1530         struct rte_eth_dev *dev;
1531
1532         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1533         dev = &rte_eth_devices[port_id];
1534
1535         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
1536
1537         if (dev->data->dev_started == 0) {
1538                 RTE_ETHDEV_LOG(INFO,
1539                         "Device with port_id=%"PRIu16" already stopped\n",
1540                         port_id);
1541                 return;
1542         }
1543
1544         dev->data->dev_started = 0;
1545         (*dev->dev_ops->dev_stop)(dev);
1546 }
1547
1548 int
1549 rte_eth_dev_set_link_up(uint16_t port_id)
1550 {
1551         struct rte_eth_dev *dev;
1552
1553         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1554
1555         dev = &rte_eth_devices[port_id];
1556
1557         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_set_link_up, -ENOTSUP);
1558         return eth_err(port_id, (*dev->dev_ops->dev_set_link_up)(dev));
1559 }
1560
1561 int
1562 rte_eth_dev_set_link_down(uint16_t port_id)
1563 {
1564         struct rte_eth_dev *dev;
1565
1566         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1567
1568         dev = &rte_eth_devices[port_id];
1569
1570         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_set_link_down, -ENOTSUP);
1571         return eth_err(port_id, (*dev->dev_ops->dev_set_link_down)(dev));
1572 }
1573
1574 void
1575 rte_eth_dev_close(uint16_t port_id)
1576 {
1577         struct rte_eth_dev *dev;
1578
1579         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1580         dev = &rte_eth_devices[port_id];
1581
1582         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_close);
1583         dev->data->dev_started = 0;
1584         (*dev->dev_ops->dev_close)(dev);
1585
1586         /* check behaviour flag - temporary for PMD migration */
1587         if ((dev->data->dev_flags & RTE_ETH_DEV_CLOSE_REMOVE) != 0) {
1588                 /* new behaviour: send event + reset state + free all data */
1589                 rte_eth_dev_release_port(dev);
1590                 return;
1591         }
1592         RTE_ETHDEV_LOG(DEBUG, "Port closing is using an old behaviour.\n"
1593                         "The driver %s should migrate to the new behaviour.\n",
1594                         dev->device->driver->name);
1595         /* old behaviour: only free queue arrays */
1596         dev->data->nb_rx_queues = 0;
1597         rte_free(dev->data->rx_queues);
1598         dev->data->rx_queues = NULL;
1599         dev->data->nb_tx_queues = 0;
1600         rte_free(dev->data->tx_queues);
1601         dev->data->tx_queues = NULL;
1602 }
1603
1604 int
1605 rte_eth_dev_reset(uint16_t port_id)
1606 {
1607         struct rte_eth_dev *dev;
1608         int ret;
1609
1610         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1611         dev = &rte_eth_devices[port_id];
1612
1613         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_reset, -ENOTSUP);
1614
1615         rte_eth_dev_stop(port_id);
1616         ret = dev->dev_ops->dev_reset(dev);
1617
1618         return eth_err(port_id, ret);
1619 }
1620
1621 int
1622 rte_eth_dev_is_removed(uint16_t port_id)
1623 {
1624         struct rte_eth_dev *dev;
1625         int ret;
1626
1627         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, 0);
1628
1629         dev = &rte_eth_devices[port_id];
1630
1631         if (dev->state == RTE_ETH_DEV_REMOVED)
1632                 return 1;
1633
1634         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->is_removed, 0);
1635
1636         ret = dev->dev_ops->is_removed(dev);
1637         if (ret != 0)
1638                 /* Device is physically removed. */
1639                 dev->state = RTE_ETH_DEV_REMOVED;
1640
1641         return ret;
1642 }
1643
1644 int
1645 rte_eth_rx_queue_setup(uint16_t port_id, uint16_t rx_queue_id,
1646                        uint16_t nb_rx_desc, unsigned int socket_id,
1647                        const struct rte_eth_rxconf *rx_conf,
1648                        struct rte_mempool *mp)
1649 {
1650         int ret;
1651         uint32_t mbp_buf_size;
1652         struct rte_eth_dev *dev;
1653         struct rte_eth_dev_info dev_info;
1654         struct rte_eth_rxconf local_conf;
1655         void **rxq;
1656
1657         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1658
1659         dev = &rte_eth_devices[port_id];
1660         if (rx_queue_id >= dev->data->nb_rx_queues) {
1661                 RTE_ETHDEV_LOG(ERR, "Invalid RX queue_id=%u\n", rx_queue_id);
1662                 return -EINVAL;
1663         }
1664
1665         if (mp == NULL) {
1666                 RTE_ETHDEV_LOG(ERR, "Invalid null mempool pointer\n");
1667                 return -EINVAL;
1668         }
1669
1670         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_setup, -ENOTSUP);
1671
1672         /*
1673          * Check the size of the mbuf data buffer.
1674          * This value must be provided in the private data of the memory pool.
1675          * First check that the memory pool has a valid private data.
1676          */
1677         ret = rte_eth_dev_info_get(port_id, &dev_info);
1678         if (ret != 0)
1679                 return ret;
1680
1681         if (mp->private_data_size < sizeof(struct rte_pktmbuf_pool_private)) {
1682                 RTE_ETHDEV_LOG(ERR, "%s private_data_size %d < %d\n",
1683                         mp->name, (int)mp->private_data_size,
1684                         (int)sizeof(struct rte_pktmbuf_pool_private));
1685                 return -ENOSPC;
1686         }
1687         mbp_buf_size = rte_pktmbuf_data_room_size(mp);
1688
1689         if ((mbp_buf_size - RTE_PKTMBUF_HEADROOM) < dev_info.min_rx_bufsize) {
1690                 RTE_ETHDEV_LOG(ERR,
1691                         "%s mbuf_data_room_size %d < %d (RTE_PKTMBUF_HEADROOM=%d + min_rx_bufsize(dev)=%d)\n",
1692                         mp->name, (int)mbp_buf_size,
1693                         (int)(RTE_PKTMBUF_HEADROOM + dev_info.min_rx_bufsize),
1694                         (int)RTE_PKTMBUF_HEADROOM,
1695                         (int)dev_info.min_rx_bufsize);
1696                 return -EINVAL;
1697         }
1698
1699         /* Use default specified by driver, if nb_rx_desc is zero */
1700         if (nb_rx_desc == 0) {
1701                 nb_rx_desc = dev_info.default_rxportconf.ring_size;
1702                 /* If driver default is also zero, fall back on EAL default */
1703                 if (nb_rx_desc == 0)
1704                         nb_rx_desc = RTE_ETH_DEV_FALLBACK_RX_RINGSIZE;
1705         }
1706
1707         if (nb_rx_desc > dev_info.rx_desc_lim.nb_max ||
1708                         nb_rx_desc < dev_info.rx_desc_lim.nb_min ||
1709                         nb_rx_desc % dev_info.rx_desc_lim.nb_align != 0) {
1710
1711                 RTE_ETHDEV_LOG(ERR,
1712                         "Invalid value for nb_rx_desc(=%hu), should be: <= %hu, >= %hu, and a product of %hu\n",
1713                         nb_rx_desc, dev_info.rx_desc_lim.nb_max,
1714                         dev_info.rx_desc_lim.nb_min,
1715                         dev_info.rx_desc_lim.nb_align);
1716                 return -EINVAL;
1717         }
1718
1719         if (dev->data->dev_started &&
1720                 !(dev_info.dev_capa &
1721                         RTE_ETH_DEV_CAPA_RUNTIME_RX_QUEUE_SETUP))
1722                 return -EBUSY;
1723
1724         if (dev->data->dev_started &&
1725                 (dev->data->rx_queue_state[rx_queue_id] !=
1726                         RTE_ETH_QUEUE_STATE_STOPPED))
1727                 return -EBUSY;
1728
1729         rxq = dev->data->rx_queues;
1730         if (rxq[rx_queue_id]) {
1731                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_release,
1732                                         -ENOTSUP);
1733                 (*dev->dev_ops->rx_queue_release)(rxq[rx_queue_id]);
1734                 rxq[rx_queue_id] = NULL;
1735         }
1736
1737         if (rx_conf == NULL)
1738                 rx_conf = &dev_info.default_rxconf;
1739
1740         local_conf = *rx_conf;
1741
1742         /*
1743          * If an offloading has already been enabled in
1744          * rte_eth_dev_configure(), it has been enabled on all queues,
1745          * so there is no need to enable it in this queue again.
1746          * The local_conf.offloads input to underlying PMD only carries
1747          * those offloadings which are only enabled on this queue and
1748          * not enabled on all queues.
1749          */
1750         local_conf.offloads &= ~dev->data->dev_conf.rxmode.offloads;
1751
1752         /*
1753          * New added offloadings for this queue are those not enabled in
1754          * rte_eth_dev_configure() and they must be per-queue type.
1755          * A pure per-port offloading can't be enabled on a queue while
1756          * disabled on another queue. A pure per-port offloading can't
1757          * be enabled for any queue as new added one if it hasn't been
1758          * enabled in rte_eth_dev_configure().
1759          */
1760         if ((local_conf.offloads & dev_info.rx_queue_offload_capa) !=
1761              local_conf.offloads) {
1762                 RTE_ETHDEV_LOG(ERR,
1763                         "Ethdev port_id=%d rx_queue_id=%d, new added offloads 0x%"PRIx64" must be "
1764                         "within per-queue offload capabilities 0x%"PRIx64" in %s()\n",
1765                         port_id, rx_queue_id, local_conf.offloads,
1766                         dev_info.rx_queue_offload_capa,
1767                         __func__);
1768                 return -EINVAL;
1769         }
1770
1771         ret = (*dev->dev_ops->rx_queue_setup)(dev, rx_queue_id, nb_rx_desc,
1772                                               socket_id, &local_conf, mp);
1773         if (!ret) {
1774                 if (!dev->data->min_rx_buf_size ||
1775                     dev->data->min_rx_buf_size > mbp_buf_size)
1776                         dev->data->min_rx_buf_size = mbp_buf_size;
1777         }
1778
1779         return eth_err(port_id, ret);
1780 }
1781
1782 int
1783 rte_eth_tx_queue_setup(uint16_t port_id, uint16_t tx_queue_id,
1784                        uint16_t nb_tx_desc, unsigned int socket_id,
1785                        const struct rte_eth_txconf *tx_conf)
1786 {
1787         struct rte_eth_dev *dev;
1788         struct rte_eth_dev_info dev_info;
1789         struct rte_eth_txconf local_conf;
1790         void **txq;
1791         int ret;
1792
1793         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1794
1795         dev = &rte_eth_devices[port_id];
1796         if (tx_queue_id >= dev->data->nb_tx_queues) {
1797                 RTE_ETHDEV_LOG(ERR, "Invalid TX queue_id=%u\n", tx_queue_id);
1798                 return -EINVAL;
1799         }
1800
1801         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_setup, -ENOTSUP);
1802
1803         ret = rte_eth_dev_info_get(port_id, &dev_info);
1804         if (ret != 0)
1805                 return ret;
1806
1807         /* Use default specified by driver, if nb_tx_desc is zero */
1808         if (nb_tx_desc == 0) {
1809                 nb_tx_desc = dev_info.default_txportconf.ring_size;
1810                 /* If driver default is zero, fall back on EAL default */
1811                 if (nb_tx_desc == 0)
1812                         nb_tx_desc = RTE_ETH_DEV_FALLBACK_TX_RINGSIZE;
1813         }
1814         if (nb_tx_desc > dev_info.tx_desc_lim.nb_max ||
1815             nb_tx_desc < dev_info.tx_desc_lim.nb_min ||
1816             nb_tx_desc % dev_info.tx_desc_lim.nb_align != 0) {
1817                 RTE_ETHDEV_LOG(ERR,
1818                         "Invalid value for nb_tx_desc(=%hu), should be: <= %hu, >= %hu, and a product of %hu\n",
1819                         nb_tx_desc, dev_info.tx_desc_lim.nb_max,
1820                         dev_info.tx_desc_lim.nb_min,
1821                         dev_info.tx_desc_lim.nb_align);
1822                 return -EINVAL;
1823         }
1824
1825         if (dev->data->dev_started &&
1826                 !(dev_info.dev_capa &
1827                         RTE_ETH_DEV_CAPA_RUNTIME_TX_QUEUE_SETUP))
1828                 return -EBUSY;
1829
1830         if (dev->data->dev_started &&
1831                 (dev->data->tx_queue_state[tx_queue_id] !=
1832                         RTE_ETH_QUEUE_STATE_STOPPED))
1833                 return -EBUSY;
1834
1835         txq = dev->data->tx_queues;
1836         if (txq[tx_queue_id]) {
1837                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_release,
1838                                         -ENOTSUP);
1839                 (*dev->dev_ops->tx_queue_release)(txq[tx_queue_id]);
1840                 txq[tx_queue_id] = NULL;
1841         }
1842
1843         if (tx_conf == NULL)
1844                 tx_conf = &dev_info.default_txconf;
1845
1846         local_conf = *tx_conf;
1847
1848         /*
1849          * If an offloading has already been enabled in
1850          * rte_eth_dev_configure(), it has been enabled on all queues,
1851          * so there is no need to enable it in this queue again.
1852          * The local_conf.offloads input to underlying PMD only carries
1853          * those offloadings which are only enabled on this queue and
1854          * not enabled on all queues.
1855          */
1856         local_conf.offloads &= ~dev->data->dev_conf.txmode.offloads;
1857
1858         /*
1859          * New added offloadings for this queue are those not enabled in
1860          * rte_eth_dev_configure() and they must be per-queue type.
1861          * A pure per-port offloading can't be enabled on a queue while
1862          * disabled on another queue. A pure per-port offloading can't
1863          * be enabled for any queue as new added one if it hasn't been
1864          * enabled in rte_eth_dev_configure().
1865          */
1866         if ((local_conf.offloads & dev_info.tx_queue_offload_capa) !=
1867              local_conf.offloads) {
1868                 RTE_ETHDEV_LOG(ERR,
1869                         "Ethdev port_id=%d tx_queue_id=%d, new added offloads 0x%"PRIx64" must be "
1870                         "within per-queue offload capabilities 0x%"PRIx64" in %s()\n",
1871                         port_id, tx_queue_id, local_conf.offloads,
1872                         dev_info.tx_queue_offload_capa,
1873                         __func__);
1874                 return -EINVAL;
1875         }
1876
1877         return eth_err(port_id, (*dev->dev_ops->tx_queue_setup)(dev,
1878                        tx_queue_id, nb_tx_desc, socket_id, &local_conf));
1879 }
1880
1881 void
1882 rte_eth_tx_buffer_drop_callback(struct rte_mbuf **pkts, uint16_t unsent,
1883                 void *userdata __rte_unused)
1884 {
1885         unsigned i;
1886
1887         for (i = 0; i < unsent; i++)
1888                 rte_pktmbuf_free(pkts[i]);
1889 }
1890
1891 void
1892 rte_eth_tx_buffer_count_callback(struct rte_mbuf **pkts, uint16_t unsent,
1893                 void *userdata)
1894 {
1895         uint64_t *count = userdata;
1896         unsigned i;
1897
1898         for (i = 0; i < unsent; i++)
1899                 rte_pktmbuf_free(pkts[i]);
1900
1901         *count += unsent;
1902 }
1903
1904 int
1905 rte_eth_tx_buffer_set_err_callback(struct rte_eth_dev_tx_buffer *buffer,
1906                 buffer_tx_error_fn cbfn, void *userdata)
1907 {
1908         buffer->error_callback = cbfn;
1909         buffer->error_userdata = userdata;
1910         return 0;
1911 }
1912
1913 int
1914 rte_eth_tx_buffer_init(struct rte_eth_dev_tx_buffer *buffer, uint16_t size)
1915 {
1916         int ret = 0;
1917
1918         if (buffer == NULL)
1919                 return -EINVAL;
1920
1921         buffer->size = size;
1922         if (buffer->error_callback == NULL) {
1923                 ret = rte_eth_tx_buffer_set_err_callback(
1924                         buffer, rte_eth_tx_buffer_drop_callback, NULL);
1925         }
1926
1927         return ret;
1928 }
1929
1930 int
1931 rte_eth_tx_done_cleanup(uint16_t port_id, uint16_t queue_id, uint32_t free_cnt)
1932 {
1933         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1934         int ret;
1935
1936         /* Validate Input Data. Bail if not valid or not supported. */
1937         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1938         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_done_cleanup, -ENOTSUP);
1939
1940         /* Call driver to free pending mbufs. */
1941         ret = (*dev->dev_ops->tx_done_cleanup)(dev->data->tx_queues[queue_id],
1942                                                free_cnt);
1943         return eth_err(port_id, ret);
1944 }
1945
1946 int
1947 rte_eth_promiscuous_enable(uint16_t port_id)
1948 {
1949         struct rte_eth_dev *dev;
1950         int diag = 0;
1951
1952         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1953         dev = &rte_eth_devices[port_id];
1954
1955         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->promiscuous_enable, -ENOTSUP);
1956
1957         if (dev->data->promiscuous == 0) {
1958                 diag = (*dev->dev_ops->promiscuous_enable)(dev);
1959                 dev->data->promiscuous = (diag == 0) ? 1 : 0;
1960         }
1961
1962         return eth_err(port_id, diag);
1963 }
1964
1965 int
1966 rte_eth_promiscuous_disable(uint16_t port_id)
1967 {
1968         struct rte_eth_dev *dev;
1969         int diag = 0;
1970
1971         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1972         dev = &rte_eth_devices[port_id];
1973
1974         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->promiscuous_disable, -ENOTSUP);
1975
1976         if (dev->data->promiscuous == 1) {
1977                 dev->data->promiscuous = 0;
1978                 diag = (*dev->dev_ops->promiscuous_disable)(dev);
1979                 if (diag != 0)
1980                         dev->data->promiscuous = 1;
1981         }
1982
1983         return eth_err(port_id, diag);
1984 }
1985
1986 int
1987 rte_eth_promiscuous_get(uint16_t port_id)
1988 {
1989         struct rte_eth_dev *dev;
1990
1991         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1992
1993         dev = &rte_eth_devices[port_id];
1994         return dev->data->promiscuous;
1995 }
1996
1997 int
1998 rte_eth_allmulticast_enable(uint16_t port_id)
1999 {
2000         struct rte_eth_dev *dev;
2001         int diag;
2002
2003         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2004         dev = &rte_eth_devices[port_id];
2005
2006         if (dev->data->all_multicast == 1)
2007                 return 0;
2008
2009         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->allmulticast_enable, -ENOTSUP);
2010         diag = (*dev->dev_ops->allmulticast_enable)(dev);
2011         dev->data->all_multicast = (diag == 0) ? 1 : 0;
2012
2013         return eth_err(port_id, diag);
2014 }
2015
2016 int
2017 rte_eth_allmulticast_disable(uint16_t port_id)
2018 {
2019         struct rte_eth_dev *dev;
2020         int diag;
2021
2022         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2023         dev = &rte_eth_devices[port_id];
2024
2025         if (dev->data->all_multicast == 0)
2026                 return 0;
2027
2028         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->allmulticast_disable, -ENOTSUP);
2029         dev->data->all_multicast = 0;
2030         diag = (*dev->dev_ops->allmulticast_disable)(dev);
2031         if (diag != 0)
2032                 dev->data->all_multicast = 1;
2033
2034         return eth_err(port_id, diag);
2035 }
2036
2037 int
2038 rte_eth_allmulticast_get(uint16_t port_id)
2039 {
2040         struct rte_eth_dev *dev;
2041
2042         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
2043
2044         dev = &rte_eth_devices[port_id];
2045         return dev->data->all_multicast;
2046 }
2047
2048 int
2049 rte_eth_link_get(uint16_t port_id, struct rte_eth_link *eth_link)
2050 {
2051         struct rte_eth_dev *dev;
2052
2053         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2054         dev = &rte_eth_devices[port_id];
2055
2056         if (dev->data->dev_conf.intr_conf.lsc &&
2057             dev->data->dev_started)
2058                 rte_eth_linkstatus_get(dev, eth_link);
2059         else {
2060                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->link_update, -ENOTSUP);
2061                 (*dev->dev_ops->link_update)(dev, 1);
2062                 *eth_link = dev->data->dev_link;
2063         }
2064
2065         return 0;
2066 }
2067
2068 int
2069 rte_eth_link_get_nowait(uint16_t port_id, struct rte_eth_link *eth_link)
2070 {
2071         struct rte_eth_dev *dev;
2072
2073         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2074         dev = &rte_eth_devices[port_id];
2075
2076         if (dev->data->dev_conf.intr_conf.lsc &&
2077             dev->data->dev_started)
2078                 rte_eth_linkstatus_get(dev, eth_link);
2079         else {
2080                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->link_update, -ENOTSUP);
2081                 (*dev->dev_ops->link_update)(dev, 0);
2082                 *eth_link = dev->data->dev_link;
2083         }
2084
2085         return 0;
2086 }
2087
2088 int
2089 rte_eth_stats_get(uint16_t port_id, struct rte_eth_stats *stats)
2090 {
2091         struct rte_eth_dev *dev;
2092
2093         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
2094
2095         dev = &rte_eth_devices[port_id];
2096         memset(stats, 0, sizeof(*stats));
2097
2098         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
2099         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
2100         return eth_err(port_id, (*dev->dev_ops->stats_get)(dev, stats));
2101 }
2102
2103 int
2104 rte_eth_stats_reset(uint16_t port_id)
2105 {
2106         struct rte_eth_dev *dev;
2107         int ret;
2108
2109         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2110         dev = &rte_eth_devices[port_id];
2111
2112         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_reset, -ENOTSUP);
2113         ret = (*dev->dev_ops->stats_reset)(dev);
2114         if (ret != 0)
2115                 return eth_err(port_id, ret);
2116
2117         dev->data->rx_mbuf_alloc_failed = 0;
2118
2119         return 0;
2120 }
2121
2122 static inline int
2123 get_xstats_basic_count(struct rte_eth_dev *dev)
2124 {
2125         uint16_t nb_rxqs, nb_txqs;
2126         int count;
2127
2128         nb_rxqs = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
2129         nb_txqs = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
2130
2131         count = RTE_NB_STATS;
2132         count += nb_rxqs * RTE_NB_RXQ_STATS;
2133         count += nb_txqs * RTE_NB_TXQ_STATS;
2134
2135         return count;
2136 }
2137
2138 static int
2139 get_xstats_count(uint16_t port_id)
2140 {
2141         struct rte_eth_dev *dev;
2142         int count;
2143
2144         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
2145         dev = &rte_eth_devices[port_id];
2146         if (dev->dev_ops->xstats_get_names_by_id != NULL) {
2147                 count = (*dev->dev_ops->xstats_get_names_by_id)(dev, NULL,
2148                                 NULL, 0);
2149                 if (count < 0)
2150                         return eth_err(port_id, count);
2151         }
2152         if (dev->dev_ops->xstats_get_names != NULL) {
2153                 count = (*dev->dev_ops->xstats_get_names)(dev, NULL, 0);
2154                 if (count < 0)
2155                         return eth_err(port_id, count);
2156         } else
2157                 count = 0;
2158
2159
2160         count += get_xstats_basic_count(dev);
2161
2162         return count;
2163 }
2164
2165 int
2166 rte_eth_xstats_get_id_by_name(uint16_t port_id, const char *xstat_name,
2167                 uint64_t *id)
2168 {
2169         int cnt_xstats, idx_xstat;
2170
2171         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2172
2173         if (!id) {
2174                 RTE_ETHDEV_LOG(ERR, "Id pointer is NULL\n");
2175                 return -ENOMEM;
2176         }
2177
2178         if (!xstat_name) {
2179                 RTE_ETHDEV_LOG(ERR, "xstat_name pointer is NULL\n");
2180                 return -ENOMEM;
2181         }
2182
2183         /* Get count */
2184         cnt_xstats = rte_eth_xstats_get_names_by_id(port_id, NULL, 0, NULL);
2185         if (cnt_xstats  < 0) {
2186                 RTE_ETHDEV_LOG(ERR, "Cannot get count of xstats\n");
2187                 return -ENODEV;
2188         }
2189
2190         /* Get id-name lookup table */
2191         struct rte_eth_xstat_name xstats_names[cnt_xstats];
2192
2193         if (cnt_xstats != rte_eth_xstats_get_names_by_id(
2194                         port_id, xstats_names, cnt_xstats, NULL)) {
2195                 RTE_ETHDEV_LOG(ERR, "Cannot get xstats lookup\n");
2196                 return -1;
2197         }
2198
2199         for (idx_xstat = 0; idx_xstat < cnt_xstats; idx_xstat++) {
2200                 if (!strcmp(xstats_names[idx_xstat].name, xstat_name)) {
2201                         *id = idx_xstat;
2202                         return 0;
2203                 };
2204         }
2205
2206         return -EINVAL;
2207 }
2208
2209 /* retrieve basic stats names */
2210 static int
2211 rte_eth_basic_stats_get_names(struct rte_eth_dev *dev,
2212         struct rte_eth_xstat_name *xstats_names)
2213 {
2214         int cnt_used_entries = 0;
2215         uint32_t idx, id_queue;
2216         uint16_t num_q;
2217
2218         for (idx = 0; idx < RTE_NB_STATS; idx++) {
2219                 strlcpy(xstats_names[cnt_used_entries].name,
2220                         rte_stats_strings[idx].name,
2221                         sizeof(xstats_names[0].name));
2222                 cnt_used_entries++;
2223         }
2224         num_q = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
2225         for (id_queue = 0; id_queue < num_q; id_queue++) {
2226                 for (idx = 0; idx < RTE_NB_RXQ_STATS; idx++) {
2227                         snprintf(xstats_names[cnt_used_entries].name,
2228                                 sizeof(xstats_names[0].name),
2229                                 "rx_q%u%s",
2230                                 id_queue, rte_rxq_stats_strings[idx].name);
2231                         cnt_used_entries++;
2232                 }
2233
2234         }
2235         num_q = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
2236         for (id_queue = 0; id_queue < num_q; id_queue++) {
2237                 for (idx = 0; idx < RTE_NB_TXQ_STATS; idx++) {
2238                         snprintf(xstats_names[cnt_used_entries].name,
2239                                 sizeof(xstats_names[0].name),
2240                                 "tx_q%u%s",
2241                                 id_queue, rte_txq_stats_strings[idx].name);
2242                         cnt_used_entries++;
2243                 }
2244         }
2245         return cnt_used_entries;
2246 }
2247
2248 /* retrieve ethdev extended statistics names */
2249 int
2250 rte_eth_xstats_get_names_by_id(uint16_t port_id,
2251         struct rte_eth_xstat_name *xstats_names, unsigned int size,
2252         uint64_t *ids)
2253 {
2254         struct rte_eth_xstat_name *xstats_names_copy;
2255         unsigned int no_basic_stat_requested = 1;
2256         unsigned int no_ext_stat_requested = 1;
2257         unsigned int expected_entries;
2258         unsigned int basic_count;
2259         struct rte_eth_dev *dev;
2260         unsigned int i;
2261         int ret;
2262
2263         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2264         dev = &rte_eth_devices[port_id];
2265
2266         basic_count = get_xstats_basic_count(dev);
2267         ret = get_xstats_count(port_id);
2268         if (ret < 0)
2269                 return ret;
2270         expected_entries = (unsigned int)ret;
2271
2272         /* Return max number of stats if no ids given */
2273         if (!ids) {
2274                 if (!xstats_names)
2275                         return expected_entries;
2276                 else if (xstats_names && size < expected_entries)
2277                         return expected_entries;
2278         }
2279
2280         if (ids && !xstats_names)
2281                 return -EINVAL;
2282
2283         if (ids && dev->dev_ops->xstats_get_names_by_id != NULL && size > 0) {
2284                 uint64_t ids_copy[size];
2285
2286                 for (i = 0; i < size; i++) {
2287                         if (ids[i] < basic_count) {
2288                                 no_basic_stat_requested = 0;
2289                                 break;
2290                         }
2291
2292                         /*
2293                          * Convert ids to xstats ids that PMD knows.
2294                          * ids known by user are basic + extended stats.
2295                          */
2296                         ids_copy[i] = ids[i] - basic_count;
2297                 }
2298
2299                 if (no_basic_stat_requested)
2300                         return (*dev->dev_ops->xstats_get_names_by_id)(dev,
2301                                         xstats_names, ids_copy, size);
2302         }
2303
2304         /* Retrieve all stats */
2305         if (!ids) {
2306                 int num_stats = rte_eth_xstats_get_names(port_id, xstats_names,
2307                                 expected_entries);
2308                 if (num_stats < 0 || num_stats > (int)expected_entries)
2309                         return num_stats;
2310                 else
2311                         return expected_entries;
2312         }
2313
2314         xstats_names_copy = calloc(expected_entries,
2315                 sizeof(struct rte_eth_xstat_name));
2316
2317         if (!xstats_names_copy) {
2318                 RTE_ETHDEV_LOG(ERR, "Can't allocate memory\n");
2319                 return -ENOMEM;
2320         }
2321
2322         if (ids) {
2323                 for (i = 0; i < size; i++) {
2324                         if (ids[i] >= basic_count) {
2325                                 no_ext_stat_requested = 0;
2326                                 break;
2327                         }
2328                 }
2329         }
2330
2331         /* Fill xstats_names_copy structure */
2332         if (ids && no_ext_stat_requested) {
2333                 rte_eth_basic_stats_get_names(dev, xstats_names_copy);
2334         } else {
2335                 ret = rte_eth_xstats_get_names(port_id, xstats_names_copy,
2336                         expected_entries);
2337                 if (ret < 0) {
2338                         free(xstats_names_copy);
2339                         return ret;
2340                 }
2341         }
2342
2343         /* Filter stats */
2344         for (i = 0; i < size; i++) {
2345                 if (ids[i] >= expected_entries) {
2346                         RTE_ETHDEV_LOG(ERR, "Id value isn't valid\n");
2347                         free(xstats_names_copy);
2348                         return -1;
2349                 }
2350                 xstats_names[i] = xstats_names_copy[ids[i]];
2351         }
2352
2353         free(xstats_names_copy);
2354         return size;
2355 }
2356
2357 int
2358 rte_eth_xstats_get_names(uint16_t port_id,
2359         struct rte_eth_xstat_name *xstats_names,
2360         unsigned int size)
2361 {
2362         struct rte_eth_dev *dev;
2363         int cnt_used_entries;
2364         int cnt_expected_entries;
2365         int cnt_driver_entries;
2366
2367         cnt_expected_entries = get_xstats_count(port_id);
2368         if (xstats_names == NULL || cnt_expected_entries < 0 ||
2369                         (int)size < cnt_expected_entries)
2370                 return cnt_expected_entries;
2371
2372         /* port_id checked in get_xstats_count() */
2373         dev = &rte_eth_devices[port_id];
2374
2375         cnt_used_entries = rte_eth_basic_stats_get_names(
2376                 dev, xstats_names);
2377
2378         if (dev->dev_ops->xstats_get_names != NULL) {
2379                 /* If there are any driver-specific xstats, append them
2380                  * to end of list.
2381                  */
2382                 cnt_driver_entries = (*dev->dev_ops->xstats_get_names)(
2383                         dev,
2384                         xstats_names + cnt_used_entries,
2385                         size - cnt_used_entries);
2386                 if (cnt_driver_entries < 0)
2387                         return eth_err(port_id, cnt_driver_entries);
2388                 cnt_used_entries += cnt_driver_entries;
2389         }
2390
2391         return cnt_used_entries;
2392 }
2393
2394
2395 static int
2396 rte_eth_basic_stats_get(uint16_t port_id, struct rte_eth_xstat *xstats)
2397 {
2398         struct rte_eth_dev *dev;
2399         struct rte_eth_stats eth_stats;
2400         unsigned int count = 0, i, q;
2401         uint64_t val, *stats_ptr;
2402         uint16_t nb_rxqs, nb_txqs;
2403         int ret;
2404
2405         ret = rte_eth_stats_get(port_id, &eth_stats);
2406         if (ret < 0)
2407                 return ret;
2408
2409         dev = &rte_eth_devices[port_id];
2410
2411         nb_rxqs = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
2412         nb_txqs = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
2413
2414         /* global stats */
2415         for (i = 0; i < RTE_NB_STATS; i++) {
2416                 stats_ptr = RTE_PTR_ADD(&eth_stats,
2417                                         rte_stats_strings[i].offset);
2418                 val = *stats_ptr;
2419                 xstats[count++].value = val;
2420         }
2421
2422         /* per-rxq stats */
2423         for (q = 0; q < nb_rxqs; q++) {
2424                 for (i = 0; i < RTE_NB_RXQ_STATS; i++) {
2425                         stats_ptr = RTE_PTR_ADD(&eth_stats,
2426                                         rte_rxq_stats_strings[i].offset +
2427                                         q * sizeof(uint64_t));
2428                         val = *stats_ptr;
2429                         xstats[count++].value = val;
2430                 }
2431         }
2432
2433         /* per-txq stats */
2434         for (q = 0; q < nb_txqs; q++) {
2435                 for (i = 0; i < RTE_NB_TXQ_STATS; i++) {
2436                         stats_ptr = RTE_PTR_ADD(&eth_stats,
2437                                         rte_txq_stats_strings[i].offset +
2438                                         q * sizeof(uint64_t));
2439                         val = *stats_ptr;
2440                         xstats[count++].value = val;
2441                 }
2442         }
2443         return count;
2444 }
2445
2446 /* retrieve ethdev extended statistics */
2447 int
2448 rte_eth_xstats_get_by_id(uint16_t port_id, const uint64_t *ids,
2449                          uint64_t *values, unsigned int size)
2450 {
2451         unsigned int no_basic_stat_requested = 1;
2452         unsigned int no_ext_stat_requested = 1;
2453         unsigned int num_xstats_filled;
2454         unsigned int basic_count;
2455         uint16_t expected_entries;
2456         struct rte_eth_dev *dev;
2457         unsigned int i;
2458         int ret;
2459
2460         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2461         ret = get_xstats_count(port_id);
2462         if (ret < 0)
2463                 return ret;
2464         expected_entries = (uint16_t)ret;
2465         struct rte_eth_xstat xstats[expected_entries];
2466         dev = &rte_eth_devices[port_id];
2467         basic_count = get_xstats_basic_count(dev);
2468
2469         /* Return max number of stats if no ids given */
2470         if (!ids) {
2471                 if (!values)
2472                         return expected_entries;
2473                 else if (values && size < expected_entries)
2474                         return expected_entries;
2475         }
2476
2477         if (ids && !values)
2478                 return -EINVAL;
2479
2480         if (ids && dev->dev_ops->xstats_get_by_id != NULL && size) {
2481                 unsigned int basic_count = get_xstats_basic_count(dev);
2482                 uint64_t ids_copy[size];
2483
2484                 for (i = 0; i < size; i++) {
2485                         if (ids[i] < basic_count) {
2486                                 no_basic_stat_requested = 0;
2487                                 break;
2488                         }
2489
2490                         /*
2491                          * Convert ids to xstats ids that PMD knows.
2492                          * ids known by user are basic + extended stats.
2493                          */
2494                         ids_copy[i] = ids[i] - basic_count;
2495                 }
2496
2497                 if (no_basic_stat_requested)
2498                         return (*dev->dev_ops->xstats_get_by_id)(dev, ids_copy,
2499                                         values, size);
2500         }
2501
2502         if (ids) {
2503                 for (i = 0; i < size; i++) {
2504                         if (ids[i] >= basic_count) {
2505                                 no_ext_stat_requested = 0;
2506                                 break;
2507                         }
2508                 }
2509         }
2510
2511         /* Fill the xstats structure */
2512         if (ids && no_ext_stat_requested)
2513                 ret = rte_eth_basic_stats_get(port_id, xstats);
2514         else
2515                 ret = rte_eth_xstats_get(port_id, xstats, expected_entries);
2516
2517         if (ret < 0)
2518                 return ret;
2519         num_xstats_filled = (unsigned int)ret;
2520
2521         /* Return all stats */
2522         if (!ids) {
2523                 for (i = 0; i < num_xstats_filled; i++)
2524                         values[i] = xstats[i].value;
2525                 return expected_entries;
2526         }
2527
2528         /* Filter stats */
2529         for (i = 0; i < size; i++) {
2530                 if (ids[i] >= expected_entries) {
2531                         RTE_ETHDEV_LOG(ERR, "Id value isn't valid\n");
2532                         return -1;
2533                 }
2534                 values[i] = xstats[ids[i]].value;
2535         }
2536         return size;
2537 }
2538
2539 int
2540 rte_eth_xstats_get(uint16_t port_id, struct rte_eth_xstat *xstats,
2541         unsigned int n)
2542 {
2543         struct rte_eth_dev *dev;
2544         unsigned int count = 0, i;
2545         signed int xcount = 0;
2546         uint16_t nb_rxqs, nb_txqs;
2547         int ret;
2548
2549         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
2550
2551         dev = &rte_eth_devices[port_id];
2552
2553         nb_rxqs = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
2554         nb_txqs = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS);
2555
2556         /* Return generic statistics */
2557         count = RTE_NB_STATS + (nb_rxqs * RTE_NB_RXQ_STATS) +
2558                 (nb_txqs * RTE_NB_TXQ_STATS);
2559
2560         /* implemented by the driver */
2561         if (dev->dev_ops->xstats_get != NULL) {
2562                 /* Retrieve the xstats from the driver at the end of the
2563                  * xstats struct.
2564                  */
2565                 xcount = (*dev->dev_ops->xstats_get)(dev,
2566                                      xstats ? xstats + count : NULL,
2567                                      (n > count) ? n - count : 0);
2568
2569                 if (xcount < 0)
2570                         return eth_err(port_id, xcount);
2571         }
2572
2573         if (n < count + xcount || xstats == NULL)
2574                 return count + xcount;
2575
2576         /* now fill the xstats structure */
2577         ret = rte_eth_basic_stats_get(port_id, xstats);
2578         if (ret < 0)
2579                 return ret;
2580         count = ret;
2581
2582         for (i = 0; i < count; i++)
2583                 xstats[i].id = i;
2584         /* add an offset to driver-specific stats */
2585         for ( ; i < count + xcount; i++)
2586                 xstats[i].id += count;
2587
2588         return count + xcount;
2589 }
2590
2591 /* reset ethdev extended statistics */
2592 int
2593 rte_eth_xstats_reset(uint16_t port_id)
2594 {
2595         struct rte_eth_dev *dev;
2596
2597         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2598         dev = &rte_eth_devices[port_id];
2599
2600         /* implemented by the driver */
2601         if (dev->dev_ops->xstats_reset != NULL)
2602                 return eth_err(port_id, (*dev->dev_ops->xstats_reset)(dev));
2603
2604         /* fallback to default */
2605         return rte_eth_stats_reset(port_id);
2606 }
2607
2608 static int
2609 set_queue_stats_mapping(uint16_t port_id, uint16_t queue_id, uint8_t stat_idx,
2610                 uint8_t is_rx)
2611 {
2612         struct rte_eth_dev *dev;
2613
2614         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2615
2616         dev = &rte_eth_devices[port_id];
2617
2618         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_stats_mapping_set, -ENOTSUP);
2619
2620         if (is_rx && (queue_id >= dev->data->nb_rx_queues))
2621                 return -EINVAL;
2622
2623         if (!is_rx && (queue_id >= dev->data->nb_tx_queues))
2624                 return -EINVAL;
2625
2626         if (stat_idx >= RTE_ETHDEV_QUEUE_STAT_CNTRS)
2627                 return -EINVAL;
2628
2629         return (*dev->dev_ops->queue_stats_mapping_set)
2630                         (dev, queue_id, stat_idx, is_rx);
2631 }
2632
2633
2634 int
2635 rte_eth_dev_set_tx_queue_stats_mapping(uint16_t port_id, uint16_t tx_queue_id,
2636                 uint8_t stat_idx)
2637 {
2638         return eth_err(port_id, set_queue_stats_mapping(port_id, tx_queue_id,
2639                                                 stat_idx, STAT_QMAP_TX));
2640 }
2641
2642
2643 int
2644 rte_eth_dev_set_rx_queue_stats_mapping(uint16_t port_id, uint16_t rx_queue_id,
2645                 uint8_t stat_idx)
2646 {
2647         return eth_err(port_id, set_queue_stats_mapping(port_id, rx_queue_id,
2648                                                 stat_idx, STAT_QMAP_RX));
2649 }
2650
2651 int
2652 rte_eth_dev_fw_version_get(uint16_t port_id, char *fw_version, size_t fw_size)
2653 {
2654         struct rte_eth_dev *dev;
2655
2656         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2657         dev = &rte_eth_devices[port_id];
2658
2659         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fw_version_get, -ENOTSUP);
2660         return eth_err(port_id, (*dev->dev_ops->fw_version_get)(dev,
2661                                                         fw_version, fw_size));
2662 }
2663
2664 int
2665 rte_eth_dev_info_get(uint16_t port_id, struct rte_eth_dev_info *dev_info)
2666 {
2667         struct rte_eth_dev *dev;
2668         const struct rte_eth_desc_lim lim = {
2669                 .nb_max = UINT16_MAX,
2670                 .nb_min = 0,
2671                 .nb_align = 1,
2672                 .nb_seg_max = UINT16_MAX,
2673                 .nb_mtu_seg_max = UINT16_MAX,
2674         };
2675         int diag;
2676
2677         /*
2678          * Init dev_info before port_id check since caller does not have
2679          * return status and does not know if get is successful or not.
2680          */
2681         memset(dev_info, 0, sizeof(struct rte_eth_dev_info));
2682
2683         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2684         dev = &rte_eth_devices[port_id];
2685
2686         dev_info->rx_desc_lim = lim;
2687         dev_info->tx_desc_lim = lim;
2688         dev_info->device = dev->device;
2689         dev_info->min_mtu = RTE_ETHER_MIN_MTU;
2690         dev_info->max_mtu = UINT16_MAX;
2691
2692         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
2693         diag = (*dev->dev_ops->dev_infos_get)(dev, dev_info);
2694         if (diag != 0) {
2695                 /* Cleanup already filled in device information */
2696                 memset(dev_info, 0, sizeof(struct rte_eth_dev_info));
2697                 return eth_err(port_id, diag);
2698         }
2699
2700         dev_info->driver_name = dev->device->driver->name;
2701         dev_info->nb_rx_queues = dev->data->nb_rx_queues;
2702         dev_info->nb_tx_queues = dev->data->nb_tx_queues;
2703
2704         dev_info->dev_flags = &dev->data->dev_flags;
2705
2706         return 0;
2707 }
2708
2709 int
2710 rte_eth_dev_get_supported_ptypes(uint16_t port_id, uint32_t ptype_mask,
2711                                  uint32_t *ptypes, int num)
2712 {
2713         int i, j;
2714         struct rte_eth_dev *dev;
2715         const uint32_t *all_ptypes;
2716
2717         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2718         dev = &rte_eth_devices[port_id];
2719         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_supported_ptypes_get, 0);
2720         all_ptypes = (*dev->dev_ops->dev_supported_ptypes_get)(dev);
2721
2722         if (!all_ptypes)
2723                 return 0;
2724
2725         for (i = 0, j = 0; all_ptypes[i] != RTE_PTYPE_UNKNOWN; ++i)
2726                 if (all_ptypes[i] & ptype_mask) {
2727                         if (j < num)
2728                                 ptypes[j] = all_ptypes[i];
2729                         j++;
2730                 }
2731
2732         return j;
2733 }
2734
2735 int
2736 rte_eth_macaddr_get(uint16_t port_id, struct rte_ether_addr *mac_addr)
2737 {
2738         struct rte_eth_dev *dev;
2739
2740         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2741         dev = &rte_eth_devices[port_id];
2742         rte_ether_addr_copy(&dev->data->mac_addrs[0], mac_addr);
2743
2744         return 0;
2745 }
2746
2747
2748 int
2749 rte_eth_dev_get_mtu(uint16_t port_id, uint16_t *mtu)
2750 {
2751         struct rte_eth_dev *dev;
2752
2753         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2754
2755         dev = &rte_eth_devices[port_id];
2756         *mtu = dev->data->mtu;
2757         return 0;
2758 }
2759
2760 int
2761 rte_eth_dev_set_mtu(uint16_t port_id, uint16_t mtu)
2762 {
2763         int ret;
2764         struct rte_eth_dev_info dev_info;
2765         struct rte_eth_dev *dev;
2766
2767         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2768         dev = &rte_eth_devices[port_id];
2769         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mtu_set, -ENOTSUP);
2770
2771         /*
2772          * Check if the device supports dev_infos_get, if it does not
2773          * skip min_mtu/max_mtu validation here as this requires values
2774          * that are populated within the call to rte_eth_dev_info_get()
2775          * which relies on dev->dev_ops->dev_infos_get.
2776          */
2777         if (*dev->dev_ops->dev_infos_get != NULL) {
2778                 ret = rte_eth_dev_info_get(port_id, &dev_info);
2779                 if (ret != 0)
2780                         return ret;
2781
2782                 if (mtu < dev_info.min_mtu || mtu > dev_info.max_mtu)
2783                         return -EINVAL;
2784         }
2785
2786         ret = (*dev->dev_ops->mtu_set)(dev, mtu);
2787         if (!ret)
2788                 dev->data->mtu = mtu;
2789
2790         return eth_err(port_id, ret);
2791 }
2792
2793 int
2794 rte_eth_dev_vlan_filter(uint16_t port_id, uint16_t vlan_id, int on)
2795 {
2796         struct rte_eth_dev *dev;
2797         int ret;
2798
2799         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2800         dev = &rte_eth_devices[port_id];
2801         if (!(dev->data->dev_conf.rxmode.offloads &
2802               DEV_RX_OFFLOAD_VLAN_FILTER)) {
2803                 RTE_ETHDEV_LOG(ERR, "Port %u: vlan-filtering disabled\n",
2804                         port_id);
2805                 return -ENOSYS;
2806         }
2807
2808         if (vlan_id > 4095) {
2809                 RTE_ETHDEV_LOG(ERR, "Port_id=%u invalid vlan_id=%u > 4095\n",
2810                         port_id, vlan_id);
2811                 return -EINVAL;
2812         }
2813         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_filter_set, -ENOTSUP);
2814
2815         ret = (*dev->dev_ops->vlan_filter_set)(dev, vlan_id, on);
2816         if (ret == 0) {
2817                 struct rte_vlan_filter_conf *vfc;
2818                 int vidx;
2819                 int vbit;
2820
2821                 vfc = &dev->data->vlan_filter_conf;
2822                 vidx = vlan_id / 64;
2823                 vbit = vlan_id % 64;
2824
2825                 if (on)
2826                         vfc->ids[vidx] |= UINT64_C(1) << vbit;
2827                 else
2828                         vfc->ids[vidx] &= ~(UINT64_C(1) << vbit);
2829         }
2830
2831         return eth_err(port_id, ret);
2832 }
2833
2834 int
2835 rte_eth_dev_set_vlan_strip_on_queue(uint16_t port_id, uint16_t rx_queue_id,
2836                                     int on)
2837 {
2838         struct rte_eth_dev *dev;
2839
2840         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2841         dev = &rte_eth_devices[port_id];
2842         if (rx_queue_id >= dev->data->nb_rx_queues) {
2843                 RTE_ETHDEV_LOG(ERR, "Invalid rx_queue_id=%u\n", rx_queue_id);
2844                 return -EINVAL;
2845         }
2846
2847         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_strip_queue_set, -ENOTSUP);
2848         (*dev->dev_ops->vlan_strip_queue_set)(dev, rx_queue_id, on);
2849
2850         return 0;
2851 }
2852
2853 int
2854 rte_eth_dev_set_vlan_ether_type(uint16_t port_id,
2855                                 enum rte_vlan_type vlan_type,
2856                                 uint16_t tpid)
2857 {
2858         struct rte_eth_dev *dev;
2859
2860         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2861         dev = &rte_eth_devices[port_id];
2862         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_tpid_set, -ENOTSUP);
2863
2864         return eth_err(port_id, (*dev->dev_ops->vlan_tpid_set)(dev, vlan_type,
2865                                                                tpid));
2866 }
2867
2868 int
2869 rte_eth_dev_set_vlan_offload(uint16_t port_id, int offload_mask)
2870 {
2871         struct rte_eth_dev *dev;
2872         int ret = 0;
2873         int mask = 0;
2874         int cur, org = 0;
2875         uint64_t orig_offloads;
2876         uint64_t *dev_offloads;
2877
2878         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2879         dev = &rte_eth_devices[port_id];
2880
2881         /* save original values in case of failure */
2882         orig_offloads = dev->data->dev_conf.rxmode.offloads;
2883         dev_offloads = &dev->data->dev_conf.rxmode.offloads;
2884
2885         /*check which option changed by application*/
2886         cur = !!(offload_mask & ETH_VLAN_STRIP_OFFLOAD);
2887         org = !!(*dev_offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
2888         if (cur != org) {
2889                 if (cur)
2890                         *dev_offloads |= DEV_RX_OFFLOAD_VLAN_STRIP;
2891                 else
2892                         *dev_offloads &= ~DEV_RX_OFFLOAD_VLAN_STRIP;
2893                 mask |= ETH_VLAN_STRIP_MASK;
2894         }
2895
2896         cur = !!(offload_mask & ETH_VLAN_FILTER_OFFLOAD);
2897         org = !!(*dev_offloads & DEV_RX_OFFLOAD_VLAN_FILTER);
2898         if (cur != org) {
2899                 if (cur)
2900                         *dev_offloads |= DEV_RX_OFFLOAD_VLAN_FILTER;
2901                 else
2902                         *dev_offloads &= ~DEV_RX_OFFLOAD_VLAN_FILTER;
2903                 mask |= ETH_VLAN_FILTER_MASK;
2904         }
2905
2906         cur = !!(offload_mask & ETH_VLAN_EXTEND_OFFLOAD);
2907         org = !!(*dev_offloads & DEV_RX_OFFLOAD_VLAN_EXTEND);
2908         if (cur != org) {
2909                 if (cur)
2910                         *dev_offloads |= DEV_RX_OFFLOAD_VLAN_EXTEND;
2911                 else
2912                         *dev_offloads &= ~DEV_RX_OFFLOAD_VLAN_EXTEND;
2913                 mask |= ETH_VLAN_EXTEND_MASK;
2914         }
2915
2916         cur = !!(offload_mask & ETH_QINQ_STRIP_OFFLOAD);
2917         org = !!(*dev_offloads & DEV_RX_OFFLOAD_QINQ_STRIP);
2918         if (cur != org) {
2919                 if (cur)
2920                         *dev_offloads |= DEV_RX_OFFLOAD_QINQ_STRIP;
2921                 else
2922                         *dev_offloads &= ~DEV_RX_OFFLOAD_QINQ_STRIP;
2923                 mask |= ETH_QINQ_STRIP_MASK;
2924         }
2925
2926         /*no change*/
2927         if (mask == 0)
2928                 return ret;
2929
2930         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_offload_set, -ENOTSUP);
2931         ret = (*dev->dev_ops->vlan_offload_set)(dev, mask);
2932         if (ret) {
2933                 /* hit an error restore  original values */
2934                 *dev_offloads = orig_offloads;
2935         }
2936
2937         return eth_err(port_id, ret);
2938 }
2939
2940 int
2941 rte_eth_dev_get_vlan_offload(uint16_t port_id)
2942 {
2943         struct rte_eth_dev *dev;
2944         uint64_t *dev_offloads;
2945         int ret = 0;
2946
2947         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2948         dev = &rte_eth_devices[port_id];
2949         dev_offloads = &dev->data->dev_conf.rxmode.offloads;
2950
2951         if (*dev_offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
2952                 ret |= ETH_VLAN_STRIP_OFFLOAD;
2953
2954         if (*dev_offloads & DEV_RX_OFFLOAD_VLAN_FILTER)
2955                 ret |= ETH_VLAN_FILTER_OFFLOAD;
2956
2957         if (*dev_offloads & DEV_RX_OFFLOAD_VLAN_EXTEND)
2958                 ret |= ETH_VLAN_EXTEND_OFFLOAD;
2959
2960         if (*dev_offloads & DEV_RX_OFFLOAD_QINQ_STRIP)
2961                 ret |= ETH_QINQ_STRIP_OFFLOAD;
2962
2963         return ret;
2964 }
2965
2966 int
2967 rte_eth_dev_set_vlan_pvid(uint16_t port_id, uint16_t pvid, int on)
2968 {
2969         struct rte_eth_dev *dev;
2970
2971         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2972         dev = &rte_eth_devices[port_id];
2973         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_pvid_set, -ENOTSUP);
2974
2975         return eth_err(port_id, (*dev->dev_ops->vlan_pvid_set)(dev, pvid, on));
2976 }
2977
2978 int
2979 rte_eth_dev_flow_ctrl_get(uint16_t port_id, struct rte_eth_fc_conf *fc_conf)
2980 {
2981         struct rte_eth_dev *dev;
2982
2983         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2984         dev = &rte_eth_devices[port_id];
2985         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_get, -ENOTSUP);
2986         memset(fc_conf, 0, sizeof(*fc_conf));
2987         return eth_err(port_id, (*dev->dev_ops->flow_ctrl_get)(dev, fc_conf));
2988 }
2989
2990 int
2991 rte_eth_dev_flow_ctrl_set(uint16_t port_id, struct rte_eth_fc_conf *fc_conf)
2992 {
2993         struct rte_eth_dev *dev;
2994
2995         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2996         if ((fc_conf->send_xon != 0) && (fc_conf->send_xon != 1)) {
2997                 RTE_ETHDEV_LOG(ERR, "Invalid send_xon, only 0/1 allowed\n");
2998                 return -EINVAL;
2999         }
3000
3001         dev = &rte_eth_devices[port_id];
3002         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_set, -ENOTSUP);
3003         return eth_err(port_id, (*dev->dev_ops->flow_ctrl_set)(dev, fc_conf));
3004 }
3005
3006 int
3007 rte_eth_dev_priority_flow_ctrl_set(uint16_t port_id,
3008                                    struct rte_eth_pfc_conf *pfc_conf)
3009 {
3010         struct rte_eth_dev *dev;
3011
3012         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3013         if (pfc_conf->priority > (ETH_DCB_NUM_USER_PRIORITIES - 1)) {
3014                 RTE_ETHDEV_LOG(ERR, "Invalid priority, only 0-7 allowed\n");
3015                 return -EINVAL;
3016         }
3017
3018         dev = &rte_eth_devices[port_id];
3019         /* High water, low water validation are device specific */
3020         if  (*dev->dev_ops->priority_flow_ctrl_set)
3021                 return eth_err(port_id, (*dev->dev_ops->priority_flow_ctrl_set)
3022                                         (dev, pfc_conf));
3023         return -ENOTSUP;
3024 }
3025
3026 static int
3027 rte_eth_check_reta_mask(struct rte_eth_rss_reta_entry64 *reta_conf,
3028                         uint16_t reta_size)
3029 {
3030         uint16_t i, num;
3031
3032         if (!reta_conf)
3033                 return -EINVAL;
3034
3035         num = (reta_size + RTE_RETA_GROUP_SIZE - 1) / RTE_RETA_GROUP_SIZE;
3036         for (i = 0; i < num; i++) {
3037                 if (reta_conf[i].mask)
3038                         return 0;
3039         }
3040
3041         return -EINVAL;
3042 }
3043
3044 static int
3045 rte_eth_check_reta_entry(struct rte_eth_rss_reta_entry64 *reta_conf,
3046                          uint16_t reta_size,
3047                          uint16_t max_rxq)
3048 {
3049         uint16_t i, idx, shift;
3050
3051         if (!reta_conf)
3052                 return -EINVAL;
3053
3054         if (max_rxq == 0) {
3055                 RTE_ETHDEV_LOG(ERR, "No receive queue is available\n");
3056                 return -EINVAL;
3057         }
3058
3059         for (i = 0; i < reta_size; i++) {
3060                 idx = i / RTE_RETA_GROUP_SIZE;
3061                 shift = i % RTE_RETA_GROUP_SIZE;
3062                 if ((reta_conf[idx].mask & (1ULL << shift)) &&
3063                         (reta_conf[idx].reta[shift] >= max_rxq)) {
3064                         RTE_ETHDEV_LOG(ERR,
3065                                 "reta_conf[%u]->reta[%u]: %u exceeds the maximum rxq index: %u\n",
3066                                 idx, shift,
3067                                 reta_conf[idx].reta[shift], max_rxq);
3068                         return -EINVAL;
3069                 }
3070         }
3071
3072         return 0;
3073 }
3074
3075 int
3076 rte_eth_dev_rss_reta_update(uint16_t port_id,
3077                             struct rte_eth_rss_reta_entry64 *reta_conf,
3078                             uint16_t reta_size)
3079 {
3080         struct rte_eth_dev *dev;
3081         int ret;
3082
3083         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3084         /* Check mask bits */
3085         ret = rte_eth_check_reta_mask(reta_conf, reta_size);
3086         if (ret < 0)
3087                 return ret;
3088
3089         dev = &rte_eth_devices[port_id];
3090
3091         /* Check entry value */
3092         ret = rte_eth_check_reta_entry(reta_conf, reta_size,
3093                                 dev->data->nb_rx_queues);
3094         if (ret < 0)
3095                 return ret;
3096
3097         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->reta_update, -ENOTSUP);
3098         return eth_err(port_id, (*dev->dev_ops->reta_update)(dev, reta_conf,
3099                                                              reta_size));
3100 }
3101
3102 int
3103 rte_eth_dev_rss_reta_query(uint16_t port_id,
3104                            struct rte_eth_rss_reta_entry64 *reta_conf,
3105                            uint16_t reta_size)
3106 {
3107         struct rte_eth_dev *dev;
3108         int ret;
3109
3110         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3111
3112         /* Check mask bits */
3113         ret = rte_eth_check_reta_mask(reta_conf, reta_size);
3114         if (ret < 0)
3115                 return ret;
3116
3117         dev = &rte_eth_devices[port_id];
3118         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->reta_query, -ENOTSUP);
3119         return eth_err(port_id, (*dev->dev_ops->reta_query)(dev, reta_conf,
3120                                                             reta_size));
3121 }
3122
3123 int
3124 rte_eth_dev_rss_hash_update(uint16_t port_id,
3125                             struct rte_eth_rss_conf *rss_conf)
3126 {
3127         struct rte_eth_dev *dev;
3128         struct rte_eth_dev_info dev_info = { .flow_type_rss_offloads = 0, };
3129         int ret;
3130
3131         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3132
3133         ret = rte_eth_dev_info_get(port_id, &dev_info);
3134         if (ret != 0)
3135                 return ret;
3136
3137         rss_conf->rss_hf = rte_eth_rss_hf_refine(rss_conf->rss_hf);
3138
3139         dev = &rte_eth_devices[port_id];
3140         if ((dev_info.flow_type_rss_offloads | rss_conf->rss_hf) !=
3141             dev_info.flow_type_rss_offloads) {
3142                 RTE_ETHDEV_LOG(ERR,
3143                         "Ethdev port_id=%u invalid rss_hf: 0x%"PRIx64", valid value: 0x%"PRIx64"\n",
3144                         port_id, rss_conf->rss_hf,
3145                         dev_info.flow_type_rss_offloads);
3146                 return -EINVAL;
3147         }
3148         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rss_hash_update, -ENOTSUP);
3149         return eth_err(port_id, (*dev->dev_ops->rss_hash_update)(dev,
3150                                                                  rss_conf));
3151 }
3152
3153 int
3154 rte_eth_dev_rss_hash_conf_get(uint16_t port_id,
3155                               struct rte_eth_rss_conf *rss_conf)
3156 {
3157         struct rte_eth_dev *dev;
3158
3159         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3160         dev = &rte_eth_devices[port_id];
3161         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rss_hash_conf_get, -ENOTSUP);
3162         return eth_err(port_id, (*dev->dev_ops->rss_hash_conf_get)(dev,
3163                                                                    rss_conf));
3164 }
3165
3166 int
3167 rte_eth_dev_udp_tunnel_port_add(uint16_t port_id,
3168                                 struct rte_eth_udp_tunnel *udp_tunnel)
3169 {
3170         struct rte_eth_dev *dev;
3171
3172         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3173         if (udp_tunnel == NULL) {
3174                 RTE_ETHDEV_LOG(ERR, "Invalid udp_tunnel parameter\n");
3175                 return -EINVAL;
3176         }
3177
3178         if (udp_tunnel->prot_type >= RTE_TUNNEL_TYPE_MAX) {
3179                 RTE_ETHDEV_LOG(ERR, "Invalid tunnel type\n");
3180                 return -EINVAL;
3181         }
3182
3183         dev = &rte_eth_devices[port_id];
3184         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->udp_tunnel_port_add, -ENOTSUP);
3185         return eth_err(port_id, (*dev->dev_ops->udp_tunnel_port_add)(dev,
3186                                                                 udp_tunnel));
3187 }
3188
3189 int
3190 rte_eth_dev_udp_tunnel_port_delete(uint16_t port_id,
3191                                    struct rte_eth_udp_tunnel *udp_tunnel)
3192 {
3193         struct rte_eth_dev *dev;
3194
3195         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3196         dev = &rte_eth_devices[port_id];
3197
3198         if (udp_tunnel == NULL) {
3199                 RTE_ETHDEV_LOG(ERR, "Invalid udp_tunnel parameter\n");
3200                 return -EINVAL;
3201         }
3202
3203         if (udp_tunnel->prot_type >= RTE_TUNNEL_TYPE_MAX) {
3204                 RTE_ETHDEV_LOG(ERR, "Invalid tunnel type\n");
3205                 return -EINVAL;
3206         }
3207
3208         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->udp_tunnel_port_del, -ENOTSUP);
3209         return eth_err(port_id, (*dev->dev_ops->udp_tunnel_port_del)(dev,
3210                                                                 udp_tunnel));
3211 }
3212
3213 int
3214 rte_eth_led_on(uint16_t port_id)
3215 {
3216         struct rte_eth_dev *dev;
3217
3218         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3219         dev = &rte_eth_devices[port_id];
3220         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_on, -ENOTSUP);
3221         return eth_err(port_id, (*dev->dev_ops->dev_led_on)(dev));
3222 }
3223
3224 int
3225 rte_eth_led_off(uint16_t port_id)
3226 {
3227         struct rte_eth_dev *dev;
3228
3229         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3230         dev = &rte_eth_devices[port_id];
3231         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_off, -ENOTSUP);
3232         return eth_err(port_id, (*dev->dev_ops->dev_led_off)(dev));
3233 }
3234
3235 /*
3236  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
3237  * an empty spot.
3238  */
3239 static int
3240 get_mac_addr_index(uint16_t port_id, const struct rte_ether_addr *addr)
3241 {
3242         struct rte_eth_dev_info dev_info;
3243         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
3244         unsigned i;
3245         int ret;
3246
3247         ret = rte_eth_dev_info_get(port_id, &dev_info);
3248         if (ret != 0)
3249                 return -1;
3250
3251         for (i = 0; i < dev_info.max_mac_addrs; i++)
3252                 if (memcmp(addr, &dev->data->mac_addrs[i],
3253                                 RTE_ETHER_ADDR_LEN) == 0)
3254                         return i;
3255
3256         return -1;
3257 }
3258
3259 static const struct rte_ether_addr null_mac_addr;
3260
3261 int
3262 rte_eth_dev_mac_addr_add(uint16_t port_id, struct rte_ether_addr *addr,
3263                         uint32_t pool)
3264 {
3265         struct rte_eth_dev *dev;
3266         int index;
3267         uint64_t pool_mask;
3268         int ret;
3269
3270         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3271         dev = &rte_eth_devices[port_id];
3272         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_add, -ENOTSUP);
3273
3274         if (rte_is_zero_ether_addr(addr)) {
3275                 RTE_ETHDEV_LOG(ERR, "Port %u: Cannot add NULL MAC address\n",
3276                         port_id);
3277                 return -EINVAL;
3278         }
3279         if (pool >= ETH_64_POOLS) {
3280                 RTE_ETHDEV_LOG(ERR, "Pool id must be 0-%d\n", ETH_64_POOLS - 1);
3281                 return -EINVAL;
3282         }
3283
3284         index = get_mac_addr_index(port_id, addr);
3285         if (index < 0) {
3286                 index = get_mac_addr_index(port_id, &null_mac_addr);
3287                 if (index < 0) {
3288                         RTE_ETHDEV_LOG(ERR, "Port %u: MAC address array full\n",
3289                                 port_id);
3290                         return -ENOSPC;
3291                 }
3292         } else {
3293                 pool_mask = dev->data->mac_pool_sel[index];
3294
3295                 /* Check if both MAC address and pool is already there, and do nothing */
3296                 if (pool_mask & (1ULL << pool))
3297                         return 0;
3298         }
3299
3300         /* Update NIC */
3301         ret = (*dev->dev_ops->mac_addr_add)(dev, addr, index, pool);
3302
3303         if (ret == 0) {
3304                 /* Update address in NIC data structure */
3305                 rte_ether_addr_copy(addr, &dev->data->mac_addrs[index]);
3306
3307                 /* Update pool bitmap in NIC data structure */
3308                 dev->data->mac_pool_sel[index] |= (1ULL << pool);
3309         }
3310
3311         return eth_err(port_id, ret);
3312 }
3313
3314 int
3315 rte_eth_dev_mac_addr_remove(uint16_t port_id, struct rte_ether_addr *addr)
3316 {
3317         struct rte_eth_dev *dev;
3318         int index;
3319
3320         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3321         dev = &rte_eth_devices[port_id];
3322         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_remove, -ENOTSUP);
3323
3324         index = get_mac_addr_index(port_id, addr);
3325         if (index == 0) {
3326                 RTE_ETHDEV_LOG(ERR,
3327                         "Port %u: Cannot remove default MAC address\n",
3328                         port_id);
3329                 return -EADDRINUSE;
3330         } else if (index < 0)
3331                 return 0;  /* Do nothing if address wasn't found */
3332
3333         /* Update NIC */
3334         (*dev->dev_ops->mac_addr_remove)(dev, index);
3335
3336         /* Update address in NIC data structure */
3337         rte_ether_addr_copy(&null_mac_addr, &dev->data->mac_addrs[index]);
3338
3339         /* reset pool bitmap */
3340         dev->data->mac_pool_sel[index] = 0;
3341
3342         return 0;
3343 }
3344
3345 int
3346 rte_eth_dev_default_mac_addr_set(uint16_t port_id, struct rte_ether_addr *addr)
3347 {
3348         struct rte_eth_dev *dev;
3349         int ret;
3350
3351         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3352
3353         if (!rte_is_valid_assigned_ether_addr(addr))
3354                 return -EINVAL;
3355
3356         dev = &rte_eth_devices[port_id];
3357         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_set, -ENOTSUP);
3358
3359         ret = (*dev->dev_ops->mac_addr_set)(dev, addr);
3360         if (ret < 0)
3361                 return ret;
3362
3363         /* Update default address in NIC data structure */
3364         rte_ether_addr_copy(addr, &dev->data->mac_addrs[0]);
3365
3366         return 0;
3367 }
3368
3369
3370 /*
3371  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
3372  * an empty spot.
3373  */
3374 static int
3375 get_hash_mac_addr_index(uint16_t port_id, const struct rte_ether_addr *addr)
3376 {
3377         struct rte_eth_dev_info dev_info;
3378         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
3379         unsigned i;
3380         int ret;
3381
3382         ret = rte_eth_dev_info_get(port_id, &dev_info);
3383         if (ret != 0)
3384                 return -1;
3385
3386         if (!dev->data->hash_mac_addrs)
3387                 return -1;
3388
3389         for (i = 0; i < dev_info.max_hash_mac_addrs; i++)
3390                 if (memcmp(addr, &dev->data->hash_mac_addrs[i],
3391                         RTE_ETHER_ADDR_LEN) == 0)
3392                         return i;
3393
3394         return -1;
3395 }
3396
3397 int
3398 rte_eth_dev_uc_hash_table_set(uint16_t port_id, struct rte_ether_addr *addr,
3399                                 uint8_t on)
3400 {
3401         int index;
3402         int ret;
3403         struct rte_eth_dev *dev;
3404
3405         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3406
3407         dev = &rte_eth_devices[port_id];
3408         if (rte_is_zero_ether_addr(addr)) {
3409                 RTE_ETHDEV_LOG(ERR, "Port %u: Cannot add NULL MAC address\n",
3410                         port_id);
3411                 return -EINVAL;
3412         }
3413
3414         index = get_hash_mac_addr_index(port_id, addr);
3415         /* Check if it's already there, and do nothing */
3416         if ((index >= 0) && on)
3417                 return 0;
3418
3419         if (index < 0) {
3420                 if (!on) {
3421                         RTE_ETHDEV_LOG(ERR,
3422                                 "Port %u: the MAC address was not set in UTA\n",
3423                                 port_id);
3424                         return -EINVAL;
3425                 }
3426
3427                 index = get_hash_mac_addr_index(port_id, &null_mac_addr);
3428                 if (index < 0) {
3429                         RTE_ETHDEV_LOG(ERR, "Port %u: MAC address array full\n",
3430                                 port_id);
3431                         return -ENOSPC;
3432                 }
3433         }
3434
3435         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->uc_hash_table_set, -ENOTSUP);
3436         ret = (*dev->dev_ops->uc_hash_table_set)(dev, addr, on);
3437         if (ret == 0) {
3438                 /* Update address in NIC data structure */
3439                 if (on)
3440                         rte_ether_addr_copy(addr,
3441                                         &dev->data->hash_mac_addrs[index]);
3442                 else
3443                         rte_ether_addr_copy(&null_mac_addr,
3444                                         &dev->data->hash_mac_addrs[index]);
3445         }
3446
3447         return eth_err(port_id, ret);
3448 }
3449
3450 int
3451 rte_eth_dev_uc_all_hash_table_set(uint16_t port_id, uint8_t on)
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
3459         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->uc_all_hash_table_set, -ENOTSUP);
3460         return eth_err(port_id, (*dev->dev_ops->uc_all_hash_table_set)(dev,
3461                                                                        on));
3462 }
3463
3464 int rte_eth_set_queue_rate_limit(uint16_t port_id, uint16_t queue_idx,
3465                                         uint16_t tx_rate)
3466 {
3467         struct rte_eth_dev *dev;
3468         struct rte_eth_dev_info dev_info;
3469         struct rte_eth_link link;
3470         int ret;
3471
3472         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3473
3474         ret = rte_eth_dev_info_get(port_id, &dev_info);
3475         if (ret != 0)
3476                 return ret;
3477
3478         dev = &rte_eth_devices[port_id];
3479         link = dev->data->dev_link;
3480
3481         if (queue_idx > dev_info.max_tx_queues) {
3482                 RTE_ETHDEV_LOG(ERR,
3483                         "Set queue rate limit:port %u: invalid queue id=%u\n",
3484                         port_id, queue_idx);
3485                 return -EINVAL;
3486         }
3487
3488         if (tx_rate > link.link_speed) {
3489                 RTE_ETHDEV_LOG(ERR,
3490                         "Set queue rate limit:invalid tx_rate=%u, bigger than link speed= %d\n",
3491                         tx_rate, link.link_speed);
3492                 return -EINVAL;
3493         }
3494
3495         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_queue_rate_limit, -ENOTSUP);
3496         return eth_err(port_id, (*dev->dev_ops->set_queue_rate_limit)(dev,
3497                                                         queue_idx, tx_rate));
3498 }
3499
3500 int
3501 rte_eth_mirror_rule_set(uint16_t port_id,
3502                         struct rte_eth_mirror_conf *mirror_conf,
3503                         uint8_t rule_id, uint8_t on)
3504 {
3505         struct rte_eth_dev *dev;
3506
3507         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3508         if (mirror_conf->rule_type == 0) {
3509                 RTE_ETHDEV_LOG(ERR, "Mirror rule type can not be 0\n");
3510                 return -EINVAL;
3511         }
3512
3513         if (mirror_conf->dst_pool >= ETH_64_POOLS) {
3514                 RTE_ETHDEV_LOG(ERR, "Invalid dst pool, pool id must be 0-%d\n",
3515                         ETH_64_POOLS - 1);
3516                 return -EINVAL;
3517         }
3518
3519         if ((mirror_conf->rule_type & (ETH_MIRROR_VIRTUAL_POOL_UP |
3520              ETH_MIRROR_VIRTUAL_POOL_DOWN)) &&
3521             (mirror_conf->pool_mask == 0)) {
3522                 RTE_ETHDEV_LOG(ERR,
3523                         "Invalid mirror pool, pool mask can not be 0\n");
3524                 return -EINVAL;
3525         }
3526
3527         if ((mirror_conf->rule_type & ETH_MIRROR_VLAN) &&
3528             mirror_conf->vlan.vlan_mask == 0) {
3529                 RTE_ETHDEV_LOG(ERR,
3530                         "Invalid vlan mask, vlan mask can not be 0\n");
3531                 return -EINVAL;
3532         }
3533
3534         dev = &rte_eth_devices[port_id];
3535         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mirror_rule_set, -ENOTSUP);
3536
3537         return eth_err(port_id, (*dev->dev_ops->mirror_rule_set)(dev,
3538                                                 mirror_conf, rule_id, on));
3539 }
3540
3541 int
3542 rte_eth_mirror_rule_reset(uint16_t port_id, uint8_t rule_id)
3543 {
3544         struct rte_eth_dev *dev;
3545
3546         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3547
3548         dev = &rte_eth_devices[port_id];
3549         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mirror_rule_reset, -ENOTSUP);
3550
3551         return eth_err(port_id, (*dev->dev_ops->mirror_rule_reset)(dev,
3552                                                                    rule_id));
3553 }
3554
3555 RTE_INIT(eth_dev_init_cb_lists)
3556 {
3557         int i;
3558
3559         for (i = 0; i < RTE_MAX_ETHPORTS; i++)
3560                 TAILQ_INIT(&rte_eth_devices[i].link_intr_cbs);
3561 }
3562
3563 int
3564 rte_eth_dev_callback_register(uint16_t port_id,
3565                         enum rte_eth_event_type event,
3566                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
3567 {
3568         struct rte_eth_dev *dev;
3569         struct rte_eth_dev_callback *user_cb;
3570         uint32_t next_port; /* size is 32-bit to prevent loop wrap-around */
3571         uint16_t last_port;
3572
3573         if (!cb_fn)
3574                 return -EINVAL;
3575
3576         if (!rte_eth_dev_is_valid_port(port_id) && port_id != RTE_ETH_ALL) {
3577                 RTE_ETHDEV_LOG(ERR, "Invalid port_id=%d\n", port_id);
3578                 return -EINVAL;
3579         }
3580
3581         if (port_id == RTE_ETH_ALL) {
3582                 next_port = 0;
3583                 last_port = RTE_MAX_ETHPORTS - 1;
3584         } else {
3585                 next_port = last_port = port_id;
3586         }
3587
3588         rte_spinlock_lock(&rte_eth_dev_cb_lock);
3589
3590         do {
3591                 dev = &rte_eth_devices[next_port];
3592
3593                 TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
3594                         if (user_cb->cb_fn == cb_fn &&
3595                                 user_cb->cb_arg == cb_arg &&
3596                                 user_cb->event == event) {
3597                                 break;
3598                         }
3599                 }
3600
3601                 /* create a new callback. */
3602                 if (user_cb == NULL) {
3603                         user_cb = rte_zmalloc("INTR_USER_CALLBACK",
3604                                 sizeof(struct rte_eth_dev_callback), 0);
3605                         if (user_cb != NULL) {
3606                                 user_cb->cb_fn = cb_fn;
3607                                 user_cb->cb_arg = cb_arg;
3608                                 user_cb->event = event;
3609                                 TAILQ_INSERT_TAIL(&(dev->link_intr_cbs),
3610                                                   user_cb, next);
3611                         } else {
3612                                 rte_spinlock_unlock(&rte_eth_dev_cb_lock);
3613                                 rte_eth_dev_callback_unregister(port_id, event,
3614                                                                 cb_fn, cb_arg);
3615                                 return -ENOMEM;
3616                         }
3617
3618                 }
3619         } while (++next_port <= last_port);
3620
3621         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
3622         return 0;
3623 }
3624
3625 int
3626 rte_eth_dev_callback_unregister(uint16_t port_id,
3627                         enum rte_eth_event_type event,
3628                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
3629 {
3630         int ret;
3631         struct rte_eth_dev *dev;
3632         struct rte_eth_dev_callback *cb, *next;
3633         uint32_t next_port; /* size is 32-bit to prevent loop wrap-around */
3634         uint16_t last_port;
3635
3636         if (!cb_fn)
3637                 return -EINVAL;
3638
3639         if (!rte_eth_dev_is_valid_port(port_id) && port_id != RTE_ETH_ALL) {
3640                 RTE_ETHDEV_LOG(ERR, "Invalid port_id=%d\n", port_id);
3641                 return -EINVAL;
3642         }
3643
3644         if (port_id == RTE_ETH_ALL) {
3645                 next_port = 0;
3646                 last_port = RTE_MAX_ETHPORTS - 1;
3647         } else {
3648                 next_port = last_port = port_id;
3649         }
3650
3651         rte_spinlock_lock(&rte_eth_dev_cb_lock);
3652
3653         do {
3654                 dev = &rte_eth_devices[next_port];
3655                 ret = 0;
3656                 for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL;
3657                      cb = next) {
3658
3659                         next = TAILQ_NEXT(cb, next);
3660
3661                         if (cb->cb_fn != cb_fn || cb->event != event ||
3662                             (cb->cb_arg != (void *)-1 && cb->cb_arg != cb_arg))
3663                                 continue;
3664
3665                         /*
3666                          * if this callback is not executing right now,
3667                          * then remove it.
3668                          */
3669                         if (cb->active == 0) {
3670                                 TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
3671                                 rte_free(cb);
3672                         } else {
3673                                 ret = -EAGAIN;
3674                         }
3675                 }
3676         } while (++next_port <= last_port);
3677
3678         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
3679         return ret;
3680 }
3681
3682 int
3683 _rte_eth_dev_callback_process(struct rte_eth_dev *dev,
3684         enum rte_eth_event_type event, void *ret_param)
3685 {
3686         struct rte_eth_dev_callback *cb_lst;
3687         struct rte_eth_dev_callback dev_cb;
3688         int rc = 0;
3689
3690         rte_spinlock_lock(&rte_eth_dev_cb_lock);
3691         TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
3692                 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
3693                         continue;
3694                 dev_cb = *cb_lst;
3695                 cb_lst->active = 1;
3696                 if (ret_param != NULL)
3697                         dev_cb.ret_param = ret_param;
3698
3699                 rte_spinlock_unlock(&rte_eth_dev_cb_lock);
3700                 rc = dev_cb.cb_fn(dev->data->port_id, dev_cb.event,
3701                                 dev_cb.cb_arg, dev_cb.ret_param);
3702                 rte_spinlock_lock(&rte_eth_dev_cb_lock);
3703                 cb_lst->active = 0;
3704         }
3705         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
3706         return rc;
3707 }
3708
3709 void
3710 rte_eth_dev_probing_finish(struct rte_eth_dev *dev)
3711 {
3712         if (dev == NULL)
3713                 return;
3714
3715         _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_NEW, NULL);
3716
3717         dev->state = RTE_ETH_DEV_ATTACHED;
3718 }
3719
3720 int
3721 rte_eth_dev_rx_intr_ctl(uint16_t port_id, int epfd, int op, void *data)
3722 {
3723         uint32_t vec;
3724         struct rte_eth_dev *dev;
3725         struct rte_intr_handle *intr_handle;
3726         uint16_t qid;
3727         int rc;
3728
3729         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3730
3731         dev = &rte_eth_devices[port_id];
3732
3733         if (!dev->intr_handle) {
3734                 RTE_ETHDEV_LOG(ERR, "RX Intr handle unset\n");
3735                 return -ENOTSUP;
3736         }
3737
3738         intr_handle = dev->intr_handle;
3739         if (!intr_handle->intr_vec) {
3740                 RTE_ETHDEV_LOG(ERR, "RX Intr vector unset\n");
3741                 return -EPERM;
3742         }
3743
3744         for (qid = 0; qid < dev->data->nb_rx_queues; qid++) {
3745                 vec = intr_handle->intr_vec[qid];
3746                 rc = rte_intr_rx_ctl(intr_handle, epfd, op, vec, data);
3747                 if (rc && rc != -EEXIST) {
3748                         RTE_ETHDEV_LOG(ERR,
3749                                 "p %u q %u rx ctl error op %d epfd %d vec %u\n",
3750                                 port_id, qid, op, epfd, vec);
3751                 }
3752         }
3753
3754         return 0;
3755 }
3756
3757 int
3758 rte_eth_dev_rx_intr_ctl_q_get_fd(uint16_t port_id, uint16_t queue_id)
3759 {
3760         struct rte_intr_handle *intr_handle;
3761         struct rte_eth_dev *dev;
3762         unsigned int efd_idx;
3763         uint32_t vec;
3764         int fd;
3765
3766         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -1);
3767
3768         dev = &rte_eth_devices[port_id];
3769
3770         if (queue_id >= dev->data->nb_rx_queues) {
3771                 RTE_ETHDEV_LOG(ERR, "Invalid RX queue_id=%u\n", queue_id);
3772                 return -1;
3773         }
3774
3775         if (!dev->intr_handle) {
3776                 RTE_ETHDEV_LOG(ERR, "RX Intr handle unset\n");
3777                 return -1;
3778         }
3779
3780         intr_handle = dev->intr_handle;
3781         if (!intr_handle->intr_vec) {
3782                 RTE_ETHDEV_LOG(ERR, "RX Intr vector unset\n");
3783                 return -1;
3784         }
3785
3786         vec = intr_handle->intr_vec[queue_id];
3787         efd_idx = (vec >= RTE_INTR_VEC_RXTX_OFFSET) ?
3788                 (vec - RTE_INTR_VEC_RXTX_OFFSET) : vec;
3789         fd = intr_handle->efds[efd_idx];
3790
3791         return fd;
3792 }
3793
3794 const struct rte_memzone *
3795 rte_eth_dma_zone_reserve(const struct rte_eth_dev *dev, const char *ring_name,
3796                          uint16_t queue_id, size_t size, unsigned align,
3797                          int socket_id)
3798 {
3799         char z_name[RTE_MEMZONE_NAMESIZE];
3800         const struct rte_memzone *mz;
3801         int rc;
3802
3803         rc = snprintf(z_name, sizeof(z_name), "eth_p%d_q%d_%s",
3804                       dev->data->port_id, queue_id, ring_name);
3805         if (rc >= RTE_MEMZONE_NAMESIZE) {
3806                 RTE_ETHDEV_LOG(ERR, "ring name too long\n");
3807                 rte_errno = ENAMETOOLONG;
3808                 return NULL;
3809         }
3810
3811         mz = rte_memzone_lookup(z_name);
3812         if (mz)
3813                 return mz;
3814
3815         return rte_memzone_reserve_aligned(z_name, size, socket_id,
3816                         RTE_MEMZONE_IOVA_CONTIG, align);
3817 }
3818
3819 int
3820 rte_eth_dev_create(struct rte_device *device, const char *name,
3821         size_t priv_data_size,
3822         ethdev_bus_specific_init ethdev_bus_specific_init,
3823         void *bus_init_params,
3824         ethdev_init_t ethdev_init, void *init_params)
3825 {
3826         struct rte_eth_dev *ethdev;
3827         int retval;
3828
3829         RTE_FUNC_PTR_OR_ERR_RET(*ethdev_init, -EINVAL);
3830
3831         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
3832                 ethdev = rte_eth_dev_allocate(name);
3833                 if (!ethdev)
3834                         return -ENODEV;
3835
3836                 if (priv_data_size) {
3837                         ethdev->data->dev_private = rte_zmalloc_socket(
3838                                 name, priv_data_size, RTE_CACHE_LINE_SIZE,
3839                                 device->numa_node);
3840
3841                         if (!ethdev->data->dev_private) {
3842                                 RTE_LOG(ERR, EAL, "failed to allocate private data");
3843                                 retval = -ENOMEM;
3844                                 goto probe_failed;
3845                         }
3846                 }
3847         } else {
3848                 ethdev = rte_eth_dev_attach_secondary(name);
3849                 if (!ethdev) {
3850                         RTE_LOG(ERR, EAL, "secondary process attach failed, "
3851                                 "ethdev doesn't exist");
3852                         return  -ENODEV;
3853                 }
3854         }
3855
3856         ethdev->device = device;
3857
3858         if (ethdev_bus_specific_init) {
3859                 retval = ethdev_bus_specific_init(ethdev, bus_init_params);
3860                 if (retval) {
3861                         RTE_LOG(ERR, EAL,
3862                                 "ethdev bus specific initialisation failed");
3863                         goto probe_failed;
3864                 }
3865         }
3866
3867         retval = ethdev_init(ethdev, init_params);
3868         if (retval) {
3869                 RTE_LOG(ERR, EAL, "ethdev initialisation failed");
3870                 goto probe_failed;
3871         }
3872
3873         rte_eth_dev_probing_finish(ethdev);
3874
3875         return retval;
3876
3877 probe_failed:
3878         rte_eth_dev_release_port(ethdev);
3879         return retval;
3880 }
3881
3882 int
3883 rte_eth_dev_destroy(struct rte_eth_dev *ethdev,
3884         ethdev_uninit_t ethdev_uninit)
3885 {
3886         int ret;
3887
3888         ethdev = rte_eth_dev_allocated(ethdev->data->name);
3889         if (!ethdev)
3890                 return -ENODEV;
3891
3892         RTE_FUNC_PTR_OR_ERR_RET(*ethdev_uninit, -EINVAL);
3893
3894         ret = ethdev_uninit(ethdev);
3895         if (ret)
3896                 return ret;
3897
3898         return rte_eth_dev_release_port(ethdev);
3899 }
3900
3901 int
3902 rte_eth_dev_rx_intr_ctl_q(uint16_t port_id, uint16_t queue_id,
3903                           int epfd, int op, void *data)
3904 {
3905         uint32_t vec;
3906         struct rte_eth_dev *dev;
3907         struct rte_intr_handle *intr_handle;
3908         int rc;
3909
3910         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3911
3912         dev = &rte_eth_devices[port_id];
3913         if (queue_id >= dev->data->nb_rx_queues) {
3914                 RTE_ETHDEV_LOG(ERR, "Invalid RX queue_id=%u\n", queue_id);
3915                 return -EINVAL;
3916         }
3917
3918         if (!dev->intr_handle) {
3919                 RTE_ETHDEV_LOG(ERR, "RX Intr handle unset\n");
3920                 return -ENOTSUP;
3921         }
3922
3923         intr_handle = dev->intr_handle;
3924         if (!intr_handle->intr_vec) {
3925                 RTE_ETHDEV_LOG(ERR, "RX Intr vector unset\n");
3926                 return -EPERM;
3927         }
3928
3929         vec = intr_handle->intr_vec[queue_id];
3930         rc = rte_intr_rx_ctl(intr_handle, epfd, op, vec, data);
3931         if (rc && rc != -EEXIST) {
3932                 RTE_ETHDEV_LOG(ERR,
3933                         "p %u q %u rx ctl error op %d epfd %d vec %u\n",
3934                         port_id, queue_id, op, epfd, vec);
3935                 return rc;
3936         }
3937
3938         return 0;
3939 }
3940
3941 int
3942 rte_eth_dev_rx_intr_enable(uint16_t port_id,
3943                            uint16_t queue_id)
3944 {
3945         struct rte_eth_dev *dev;
3946
3947         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3948
3949         dev = &rte_eth_devices[port_id];
3950
3951         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_intr_enable, -ENOTSUP);
3952         return eth_err(port_id, (*dev->dev_ops->rx_queue_intr_enable)(dev,
3953                                                                 queue_id));
3954 }
3955
3956 int
3957 rte_eth_dev_rx_intr_disable(uint16_t port_id,
3958                             uint16_t queue_id)
3959 {
3960         struct rte_eth_dev *dev;
3961
3962         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3963
3964         dev = &rte_eth_devices[port_id];
3965
3966         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_intr_disable, -ENOTSUP);
3967         return eth_err(port_id, (*dev->dev_ops->rx_queue_intr_disable)(dev,
3968                                                                 queue_id));
3969 }
3970
3971
3972 int
3973 rte_eth_dev_filter_supported(uint16_t port_id,
3974                              enum rte_filter_type filter_type)
3975 {
3976         struct rte_eth_dev *dev;
3977
3978         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3979
3980         dev = &rte_eth_devices[port_id];
3981         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->filter_ctrl, -ENOTSUP);
3982         return (*dev->dev_ops->filter_ctrl)(dev, filter_type,
3983                                 RTE_ETH_FILTER_NOP, NULL);
3984 }
3985
3986 int
3987 rte_eth_dev_filter_ctrl(uint16_t port_id, enum rte_filter_type filter_type,
3988                         enum rte_filter_op filter_op, void *arg)
3989 {
3990         struct rte_eth_dev *dev;
3991
3992         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3993
3994         dev = &rte_eth_devices[port_id];
3995         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->filter_ctrl, -ENOTSUP);
3996         return eth_err(port_id, (*dev->dev_ops->filter_ctrl)(dev, filter_type,
3997                                                              filter_op, arg));
3998 }
3999
4000 const struct rte_eth_rxtx_callback *
4001 rte_eth_add_rx_callback(uint16_t port_id, uint16_t queue_id,
4002                 rte_rx_callback_fn fn, void *user_param)
4003 {
4004 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
4005         rte_errno = ENOTSUP;
4006         return NULL;
4007 #endif
4008         /* check input parameters */
4009         if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
4010                     queue_id >= rte_eth_devices[port_id].data->nb_rx_queues) {
4011                 rte_errno = EINVAL;
4012                 return NULL;
4013         }
4014         struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
4015
4016         if (cb == NULL) {
4017                 rte_errno = ENOMEM;
4018                 return NULL;
4019         }
4020
4021         cb->fn.rx = fn;
4022         cb->param = user_param;
4023
4024         rte_spinlock_lock(&rte_eth_rx_cb_lock);
4025         /* Add the callbacks in fifo order. */
4026         struct rte_eth_rxtx_callback *tail =
4027                 rte_eth_devices[port_id].post_rx_burst_cbs[queue_id];
4028
4029         if (!tail) {
4030                 rte_eth_devices[port_id].post_rx_burst_cbs[queue_id] = cb;
4031
4032         } else {
4033                 while (tail->next)
4034                         tail = tail->next;
4035                 tail->next = cb;
4036         }
4037         rte_spinlock_unlock(&rte_eth_rx_cb_lock);
4038
4039         return cb;
4040 }
4041
4042 const struct rte_eth_rxtx_callback *
4043 rte_eth_add_first_rx_callback(uint16_t port_id, uint16_t queue_id,
4044                 rte_rx_callback_fn fn, void *user_param)
4045 {
4046 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
4047         rte_errno = ENOTSUP;
4048         return NULL;
4049 #endif
4050         /* check input parameters */
4051         if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
4052                 queue_id >= rte_eth_devices[port_id].data->nb_rx_queues) {
4053                 rte_errno = EINVAL;
4054                 return NULL;
4055         }
4056
4057         struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
4058
4059         if (cb == NULL) {
4060                 rte_errno = ENOMEM;
4061                 return NULL;
4062         }
4063
4064         cb->fn.rx = fn;
4065         cb->param = user_param;
4066
4067         rte_spinlock_lock(&rte_eth_rx_cb_lock);
4068         /* Add the callbacks at fisrt position*/
4069         cb->next = rte_eth_devices[port_id].post_rx_burst_cbs[queue_id];
4070         rte_smp_wmb();
4071         rte_eth_devices[port_id].post_rx_burst_cbs[queue_id] = cb;
4072         rte_spinlock_unlock(&rte_eth_rx_cb_lock);
4073
4074         return cb;
4075 }
4076
4077 const struct rte_eth_rxtx_callback *
4078 rte_eth_add_tx_callback(uint16_t port_id, uint16_t queue_id,
4079                 rte_tx_callback_fn fn, void *user_param)
4080 {
4081 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
4082         rte_errno = ENOTSUP;
4083         return NULL;
4084 #endif
4085         /* check input parameters */
4086         if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
4087                     queue_id >= rte_eth_devices[port_id].data->nb_tx_queues) {
4088                 rte_errno = EINVAL;
4089                 return NULL;
4090         }
4091
4092         struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
4093
4094         if (cb == NULL) {
4095                 rte_errno = ENOMEM;
4096                 return NULL;
4097         }
4098
4099         cb->fn.tx = fn;
4100         cb->param = user_param;
4101
4102         rte_spinlock_lock(&rte_eth_tx_cb_lock);
4103         /* Add the callbacks in fifo order. */
4104         struct rte_eth_rxtx_callback *tail =
4105                 rte_eth_devices[port_id].pre_tx_burst_cbs[queue_id];
4106
4107         if (!tail) {
4108                 rte_eth_devices[port_id].pre_tx_burst_cbs[queue_id] = cb;
4109
4110         } else {
4111                 while (tail->next)
4112                         tail = tail->next;
4113                 tail->next = cb;
4114         }
4115         rte_spinlock_unlock(&rte_eth_tx_cb_lock);
4116
4117         return cb;
4118 }
4119
4120 int
4121 rte_eth_remove_rx_callback(uint16_t port_id, uint16_t queue_id,
4122                 const struct rte_eth_rxtx_callback *user_cb)
4123 {
4124 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
4125         return -ENOTSUP;
4126 #endif
4127         /* Check input parameters. */
4128         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
4129         if (user_cb == NULL ||
4130                         queue_id >= rte_eth_devices[port_id].data->nb_rx_queues)
4131                 return -EINVAL;
4132
4133         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
4134         struct rte_eth_rxtx_callback *cb;
4135         struct rte_eth_rxtx_callback **prev_cb;
4136         int ret = -EINVAL;
4137
4138         rte_spinlock_lock(&rte_eth_rx_cb_lock);
4139         prev_cb = &dev->post_rx_burst_cbs[queue_id];
4140         for (; *prev_cb != NULL; prev_cb = &cb->next) {
4141                 cb = *prev_cb;
4142                 if (cb == user_cb) {
4143                         /* Remove the user cb from the callback list. */
4144                         *prev_cb = cb->next;
4145                         ret = 0;
4146                         break;
4147                 }
4148         }
4149         rte_spinlock_unlock(&rte_eth_rx_cb_lock);
4150
4151         return ret;
4152 }
4153
4154 int
4155 rte_eth_remove_tx_callback(uint16_t port_id, uint16_t queue_id,
4156                 const struct rte_eth_rxtx_callback *user_cb)
4157 {
4158 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
4159         return -ENOTSUP;
4160 #endif
4161         /* Check input parameters. */
4162         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
4163         if (user_cb == NULL ||
4164                         queue_id >= rte_eth_devices[port_id].data->nb_tx_queues)
4165                 return -EINVAL;
4166
4167         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
4168         int ret = -EINVAL;
4169         struct rte_eth_rxtx_callback *cb;
4170         struct rte_eth_rxtx_callback **prev_cb;
4171
4172         rte_spinlock_lock(&rte_eth_tx_cb_lock);
4173         prev_cb = &dev->pre_tx_burst_cbs[queue_id];
4174         for (; *prev_cb != NULL; prev_cb = &cb->next) {
4175                 cb = *prev_cb;
4176                 if (cb == user_cb) {
4177                         /* Remove the user cb from the callback list. */
4178                         *prev_cb = cb->next;
4179                         ret = 0;
4180                         break;
4181                 }
4182         }
4183         rte_spinlock_unlock(&rte_eth_tx_cb_lock);
4184
4185         return ret;
4186 }
4187
4188 int
4189 rte_eth_rx_queue_info_get(uint16_t port_id, uint16_t queue_id,
4190         struct rte_eth_rxq_info *qinfo)
4191 {
4192         struct rte_eth_dev *dev;
4193
4194         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4195
4196         if (qinfo == NULL)
4197                 return -EINVAL;
4198
4199         dev = &rte_eth_devices[port_id];
4200         if (queue_id >= dev->data->nb_rx_queues) {
4201                 RTE_ETHDEV_LOG(ERR, "Invalid RX queue_id=%u\n", queue_id);
4202                 return -EINVAL;
4203         }
4204
4205         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rxq_info_get, -ENOTSUP);
4206
4207         memset(qinfo, 0, sizeof(*qinfo));
4208         dev->dev_ops->rxq_info_get(dev, queue_id, qinfo);
4209         return 0;
4210 }
4211
4212 int
4213 rte_eth_tx_queue_info_get(uint16_t port_id, uint16_t queue_id,
4214         struct rte_eth_txq_info *qinfo)
4215 {
4216         struct rte_eth_dev *dev;
4217
4218         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4219
4220         if (qinfo == NULL)
4221                 return -EINVAL;
4222
4223         dev = &rte_eth_devices[port_id];
4224         if (queue_id >= dev->data->nb_tx_queues) {
4225                 RTE_ETHDEV_LOG(ERR, "Invalid TX queue_id=%u\n", queue_id);
4226                 return -EINVAL;
4227         }
4228
4229         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->txq_info_get, -ENOTSUP);
4230
4231         memset(qinfo, 0, sizeof(*qinfo));
4232         dev->dev_ops->txq_info_get(dev, queue_id, qinfo);
4233
4234         return 0;
4235 }
4236
4237 int
4238 rte_eth_rx_burst_mode_get(uint16_t port_id, uint16_t queue_id,
4239                           struct rte_eth_burst_mode *mode)
4240 {
4241         struct rte_eth_dev *dev;
4242
4243         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4244
4245         if (mode == NULL)
4246                 return -EINVAL;
4247
4248         dev = &rte_eth_devices[port_id];
4249
4250         if (queue_id >= dev->data->nb_rx_queues) {
4251                 RTE_ETHDEV_LOG(ERR, "Invalid RX queue_id=%u\n", queue_id);
4252                 return -EINVAL;
4253         }
4254
4255         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_burst_mode_get, -ENOTSUP);
4256         memset(mode, 0, sizeof(*mode));
4257         return eth_err(port_id,
4258                        dev->dev_ops->rx_burst_mode_get(dev, queue_id, mode));
4259 }
4260
4261 int
4262 rte_eth_tx_burst_mode_get(uint16_t port_id, uint16_t queue_id,
4263                           struct rte_eth_burst_mode *mode)
4264 {
4265         struct rte_eth_dev *dev;
4266
4267         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4268
4269         if (mode == NULL)
4270                 return -EINVAL;
4271
4272         dev = &rte_eth_devices[port_id];
4273
4274         if (queue_id >= dev->data->nb_tx_queues) {
4275                 RTE_ETHDEV_LOG(ERR, "Invalid TX queue_id=%u\n", queue_id);
4276                 return -EINVAL;
4277         }
4278
4279         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_burst_mode_get, -ENOTSUP);
4280         memset(mode, 0, sizeof(*mode));
4281         return eth_err(port_id,
4282                        dev->dev_ops->tx_burst_mode_get(dev, queue_id, mode));
4283 }
4284
4285 const char *
4286 rte_eth_burst_mode_option_name(uint64_t option)
4287 {
4288         const char *name = "";
4289         unsigned int i;
4290
4291         for (i = 0; i < RTE_DIM(rte_burst_option_names); ++i) {
4292                 if (option == rte_burst_option_names[i].option) {
4293                         name = rte_burst_option_names[i].name;
4294                         break;
4295                 }
4296         }
4297
4298         return name;
4299 }
4300
4301 int
4302 rte_eth_dev_set_mc_addr_list(uint16_t port_id,
4303                              struct rte_ether_addr *mc_addr_set,
4304                              uint32_t nb_mc_addr)
4305 {
4306         struct rte_eth_dev *dev;
4307
4308         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4309
4310         dev = &rte_eth_devices[port_id];
4311         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_mc_addr_list, -ENOTSUP);
4312         return eth_err(port_id, dev->dev_ops->set_mc_addr_list(dev,
4313                                                 mc_addr_set, nb_mc_addr));
4314 }
4315
4316 int
4317 rte_eth_timesync_enable(uint16_t port_id)
4318 {
4319         struct rte_eth_dev *dev;
4320
4321         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4322         dev = &rte_eth_devices[port_id];
4323
4324         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_enable, -ENOTSUP);
4325         return eth_err(port_id, (*dev->dev_ops->timesync_enable)(dev));
4326 }
4327
4328 int
4329 rte_eth_timesync_disable(uint16_t port_id)
4330 {
4331         struct rte_eth_dev *dev;
4332
4333         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4334         dev = &rte_eth_devices[port_id];
4335
4336         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_disable, -ENOTSUP);
4337         return eth_err(port_id, (*dev->dev_ops->timesync_disable)(dev));
4338 }
4339
4340 int
4341 rte_eth_timesync_read_rx_timestamp(uint16_t port_id, struct timespec *timestamp,
4342                                    uint32_t flags)
4343 {
4344         struct rte_eth_dev *dev;
4345
4346         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4347         dev = &rte_eth_devices[port_id];
4348
4349         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_rx_timestamp, -ENOTSUP);
4350         return eth_err(port_id, (*dev->dev_ops->timesync_read_rx_timestamp)
4351                                 (dev, timestamp, flags));
4352 }
4353
4354 int
4355 rte_eth_timesync_read_tx_timestamp(uint16_t port_id,
4356                                    struct timespec *timestamp)
4357 {
4358         struct rte_eth_dev *dev;
4359
4360         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4361         dev = &rte_eth_devices[port_id];
4362
4363         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_tx_timestamp, -ENOTSUP);
4364         return eth_err(port_id, (*dev->dev_ops->timesync_read_tx_timestamp)
4365                                 (dev, timestamp));
4366 }
4367
4368 int
4369 rte_eth_timesync_adjust_time(uint16_t port_id, int64_t delta)
4370 {
4371         struct rte_eth_dev *dev;
4372
4373         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4374         dev = &rte_eth_devices[port_id];
4375
4376         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_adjust_time, -ENOTSUP);
4377         return eth_err(port_id, (*dev->dev_ops->timesync_adjust_time)(dev,
4378                                                                       delta));
4379 }
4380
4381 int
4382 rte_eth_timesync_read_time(uint16_t port_id, struct timespec *timestamp)
4383 {
4384         struct rte_eth_dev *dev;
4385
4386         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4387         dev = &rte_eth_devices[port_id];
4388
4389         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_time, -ENOTSUP);
4390         return eth_err(port_id, (*dev->dev_ops->timesync_read_time)(dev,
4391                                                                 timestamp));
4392 }
4393
4394 int
4395 rte_eth_timesync_write_time(uint16_t port_id, const struct timespec *timestamp)
4396 {
4397         struct rte_eth_dev *dev;
4398
4399         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4400         dev = &rte_eth_devices[port_id];
4401
4402         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_write_time, -ENOTSUP);
4403         return eth_err(port_id, (*dev->dev_ops->timesync_write_time)(dev,
4404                                                                 timestamp));
4405 }
4406
4407 int
4408 rte_eth_read_clock(uint16_t port_id, uint64_t *clock)
4409 {
4410         struct rte_eth_dev *dev;
4411
4412         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4413         dev = &rte_eth_devices[port_id];
4414
4415         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->read_clock, -ENOTSUP);
4416         return eth_err(port_id, (*dev->dev_ops->read_clock)(dev, clock));
4417 }
4418
4419 int
4420 rte_eth_dev_get_reg_info(uint16_t port_id, struct rte_dev_reg_info *info)
4421 {
4422         struct rte_eth_dev *dev;
4423
4424         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4425
4426         dev = &rte_eth_devices[port_id];
4427         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_reg, -ENOTSUP);
4428         return eth_err(port_id, (*dev->dev_ops->get_reg)(dev, info));
4429 }
4430
4431 int
4432 rte_eth_dev_get_eeprom_length(uint16_t port_id)
4433 {
4434         struct rte_eth_dev *dev;
4435
4436         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4437
4438         dev = &rte_eth_devices[port_id];
4439         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_eeprom_length, -ENOTSUP);
4440         return eth_err(port_id, (*dev->dev_ops->get_eeprom_length)(dev));
4441 }
4442
4443 int
4444 rte_eth_dev_get_eeprom(uint16_t port_id, struct rte_dev_eeprom_info *info)
4445 {
4446         struct rte_eth_dev *dev;
4447
4448         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4449
4450         dev = &rte_eth_devices[port_id];
4451         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_eeprom, -ENOTSUP);
4452         return eth_err(port_id, (*dev->dev_ops->get_eeprom)(dev, info));
4453 }
4454
4455 int
4456 rte_eth_dev_set_eeprom(uint16_t port_id, struct rte_dev_eeprom_info *info)
4457 {
4458         struct rte_eth_dev *dev;
4459
4460         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4461
4462         dev = &rte_eth_devices[port_id];
4463         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_eeprom, -ENOTSUP);
4464         return eth_err(port_id, (*dev->dev_ops->set_eeprom)(dev, info));
4465 }
4466
4467 int
4468 rte_eth_dev_get_module_info(uint16_t port_id,
4469                             struct rte_eth_dev_module_info *modinfo)
4470 {
4471         struct rte_eth_dev *dev;
4472
4473         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4474
4475         dev = &rte_eth_devices[port_id];
4476         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_module_info, -ENOTSUP);
4477         return (*dev->dev_ops->get_module_info)(dev, modinfo);
4478 }
4479
4480 int
4481 rte_eth_dev_get_module_eeprom(uint16_t port_id,
4482                               struct rte_dev_eeprom_info *info)
4483 {
4484         struct rte_eth_dev *dev;
4485
4486         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4487
4488         dev = &rte_eth_devices[port_id];
4489         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_module_eeprom, -ENOTSUP);
4490         return (*dev->dev_ops->get_module_eeprom)(dev, info);
4491 }
4492
4493 int
4494 rte_eth_dev_get_dcb_info(uint16_t port_id,
4495                              struct rte_eth_dcb_info *dcb_info)
4496 {
4497         struct rte_eth_dev *dev;
4498
4499         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4500
4501         dev = &rte_eth_devices[port_id];
4502         memset(dcb_info, 0, sizeof(struct rte_eth_dcb_info));
4503
4504         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_dcb_info, -ENOTSUP);
4505         return eth_err(port_id, (*dev->dev_ops->get_dcb_info)(dev, dcb_info));
4506 }
4507
4508 int
4509 rte_eth_dev_l2_tunnel_eth_type_conf(uint16_t port_id,
4510                                     struct rte_eth_l2_tunnel_conf *l2_tunnel)
4511 {
4512         struct rte_eth_dev *dev;
4513
4514         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4515         if (l2_tunnel == NULL) {
4516                 RTE_ETHDEV_LOG(ERR, "Invalid l2_tunnel parameter\n");
4517                 return -EINVAL;
4518         }
4519
4520         if (l2_tunnel->l2_tunnel_type >= RTE_TUNNEL_TYPE_MAX) {
4521                 RTE_ETHDEV_LOG(ERR, "Invalid tunnel type\n");
4522                 return -EINVAL;
4523         }
4524
4525         dev = &rte_eth_devices[port_id];
4526         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->l2_tunnel_eth_type_conf,
4527                                 -ENOTSUP);
4528         return eth_err(port_id, (*dev->dev_ops->l2_tunnel_eth_type_conf)(dev,
4529                                                                 l2_tunnel));
4530 }
4531
4532 int
4533 rte_eth_dev_l2_tunnel_offload_set(uint16_t port_id,
4534                                   struct rte_eth_l2_tunnel_conf *l2_tunnel,
4535                                   uint32_t mask,
4536                                   uint8_t en)
4537 {
4538         struct rte_eth_dev *dev;
4539
4540         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4541
4542         if (l2_tunnel == NULL) {
4543                 RTE_ETHDEV_LOG(ERR, "Invalid l2_tunnel parameter\n");
4544                 return -EINVAL;
4545         }
4546
4547         if (l2_tunnel->l2_tunnel_type >= RTE_TUNNEL_TYPE_MAX) {
4548                 RTE_ETHDEV_LOG(ERR, "Invalid tunnel type\n");
4549                 return -EINVAL;
4550         }
4551
4552         if (mask == 0) {
4553                 RTE_ETHDEV_LOG(ERR, "Mask should have a value\n");
4554                 return -EINVAL;
4555         }
4556
4557         dev = &rte_eth_devices[port_id];
4558         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->l2_tunnel_offload_set,
4559                                 -ENOTSUP);
4560         return eth_err(port_id, (*dev->dev_ops->l2_tunnel_offload_set)(dev,
4561                                                         l2_tunnel, mask, en));
4562 }
4563
4564 static void
4565 rte_eth_dev_adjust_nb_desc(uint16_t *nb_desc,
4566                            const struct rte_eth_desc_lim *desc_lim)
4567 {
4568         if (desc_lim->nb_align != 0)
4569                 *nb_desc = RTE_ALIGN_CEIL(*nb_desc, desc_lim->nb_align);
4570
4571         if (desc_lim->nb_max != 0)
4572                 *nb_desc = RTE_MIN(*nb_desc, desc_lim->nb_max);
4573
4574         *nb_desc = RTE_MAX(*nb_desc, desc_lim->nb_min);
4575 }
4576
4577 int
4578 rte_eth_dev_adjust_nb_rx_tx_desc(uint16_t port_id,
4579                                  uint16_t *nb_rx_desc,
4580                                  uint16_t *nb_tx_desc)
4581 {
4582         struct rte_eth_dev_info dev_info;
4583         int ret;
4584
4585         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4586
4587         ret = rte_eth_dev_info_get(port_id, &dev_info);
4588         if (ret != 0)
4589                 return ret;
4590
4591         if (nb_rx_desc != NULL)
4592                 rte_eth_dev_adjust_nb_desc(nb_rx_desc, &dev_info.rx_desc_lim);
4593
4594         if (nb_tx_desc != NULL)
4595                 rte_eth_dev_adjust_nb_desc(nb_tx_desc, &dev_info.tx_desc_lim);
4596
4597         return 0;
4598 }
4599
4600 int
4601 rte_eth_dev_pool_ops_supported(uint16_t port_id, const char *pool)
4602 {
4603         struct rte_eth_dev *dev;
4604
4605         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
4606
4607         if (pool == NULL)
4608                 return -EINVAL;
4609
4610         dev = &rte_eth_devices[port_id];
4611
4612         if (*dev->dev_ops->pool_ops_supported == NULL)
4613                 return 1; /* all pools are supported */
4614
4615         return (*dev->dev_ops->pool_ops_supported)(dev, pool);
4616 }
4617
4618 /**
4619  * A set of values to describe the possible states of a switch domain.
4620  */
4621 enum rte_eth_switch_domain_state {
4622         RTE_ETH_SWITCH_DOMAIN_UNUSED = 0,
4623         RTE_ETH_SWITCH_DOMAIN_ALLOCATED
4624 };
4625
4626 /**
4627  * Array of switch domains available for allocation. Array is sized to
4628  * RTE_MAX_ETHPORTS elements as there cannot be more active switch domains than
4629  * ethdev ports in a single process.
4630  */
4631 static struct rte_eth_dev_switch {
4632         enum rte_eth_switch_domain_state state;
4633 } rte_eth_switch_domains[RTE_MAX_ETHPORTS];
4634
4635 int
4636 rte_eth_switch_domain_alloc(uint16_t *domain_id)
4637 {
4638         unsigned int i;
4639
4640         *domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
4641
4642         for (i = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID + 1;
4643                 i < RTE_MAX_ETHPORTS; i++) {
4644                 if (rte_eth_switch_domains[i].state ==
4645                         RTE_ETH_SWITCH_DOMAIN_UNUSED) {
4646                         rte_eth_switch_domains[i].state =
4647                                 RTE_ETH_SWITCH_DOMAIN_ALLOCATED;
4648                         *domain_id = i;
4649                         return 0;
4650                 }
4651         }
4652
4653         return -ENOSPC;
4654 }
4655
4656 int
4657 rte_eth_switch_domain_free(uint16_t domain_id)
4658 {
4659         if (domain_id == RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID ||
4660                 domain_id >= RTE_MAX_ETHPORTS)
4661                 return -EINVAL;
4662
4663         if (rte_eth_switch_domains[domain_id].state !=
4664                 RTE_ETH_SWITCH_DOMAIN_ALLOCATED)
4665                 return -EINVAL;
4666
4667         rte_eth_switch_domains[domain_id].state = RTE_ETH_SWITCH_DOMAIN_UNUSED;
4668
4669         return 0;
4670 }
4671
4672 static int
4673 rte_eth_devargs_tokenise(struct rte_kvargs *arglist, const char *str_in)
4674 {
4675         int state;
4676         struct rte_kvargs_pair *pair;
4677         char *letter;
4678
4679         arglist->str = strdup(str_in);
4680         if (arglist->str == NULL)
4681                 return -ENOMEM;
4682
4683         letter = arglist->str;
4684         state = 0;
4685         arglist->count = 0;
4686         pair = &arglist->pairs[0];
4687         while (1) {
4688                 switch (state) {
4689                 case 0: /* Initial */
4690                         if (*letter == '=')
4691                                 return -EINVAL;
4692                         else if (*letter == '\0')
4693                                 return 0;
4694
4695                         state = 1;
4696                         pair->key = letter;
4697                         /* fall-thru */
4698
4699                 case 1: /* Parsing key */
4700                         if (*letter == '=') {
4701                                 *letter = '\0';
4702                                 pair->value = letter + 1;
4703                                 state = 2;
4704                         } else if (*letter == ',' || *letter == '\0')
4705                                 return -EINVAL;
4706                         break;
4707
4708
4709                 case 2: /* Parsing value */
4710                         if (*letter == '[')
4711                                 state = 3;
4712                         else if (*letter == ',') {
4713                                 *letter = '\0';
4714                                 arglist->count++;
4715                                 pair = &arglist->pairs[arglist->count];
4716                                 state = 0;
4717                         } else if (*letter == '\0') {
4718                                 letter--;
4719                                 arglist->count++;
4720                                 pair = &arglist->pairs[arglist->count];
4721                                 state = 0;
4722                         }
4723                         break;
4724
4725                 case 3: /* Parsing list */
4726                         if (*letter == ']')
4727                                 state = 2;
4728                         else if (*letter == '\0')
4729                                 return -EINVAL;
4730                         break;
4731                 }
4732                 letter++;
4733         }
4734 }
4735
4736 int
4737 rte_eth_devargs_parse(const char *dargs, struct rte_eth_devargs *eth_da)
4738 {
4739         struct rte_kvargs args;
4740         struct rte_kvargs_pair *pair;
4741         unsigned int i;
4742         int result = 0;
4743
4744         memset(eth_da, 0, sizeof(*eth_da));
4745
4746         result = rte_eth_devargs_tokenise(&args, dargs);
4747         if (result < 0)
4748                 goto parse_cleanup;
4749
4750         for (i = 0; i < args.count; i++) {
4751                 pair = &args.pairs[i];
4752                 if (strcmp("representor", pair->key) == 0) {
4753                         result = rte_eth_devargs_parse_list(pair->value,
4754                                 rte_eth_devargs_parse_representor_ports,
4755                                 eth_da);
4756                         if (result < 0)
4757                                 goto parse_cleanup;
4758                 }
4759         }
4760
4761 parse_cleanup:
4762         if (args.str)
4763                 free(args.str);
4764
4765         return result;
4766 }
4767
4768 RTE_INIT(ethdev_init_log)
4769 {
4770         rte_eth_dev_logtype = rte_log_register("lib.ethdev");
4771         if (rte_eth_dev_logtype >= 0)
4772                 rte_log_set_level(rte_eth_dev_logtype, RTE_LOG_INFO);
4773 }