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