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