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