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