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