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