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