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