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