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