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