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