ed971b49092b7aeac53d75efecb9049fd268cbda
[dpdk.git] / lib / librte_ether / rte_ethdev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 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_pci.h>
51 #include <rte_memory.h>
52 #include <rte_memcpy.h>
53 #include <rte_memzone.h>
54 #include <rte_launch.h>
55 #include <rte_eal.h>
56 #include <rte_per_lcore.h>
57 #include <rte_lcore.h>
58 #include <rte_atomic.h>
59 #include <rte_branch_prediction.h>
60 #include <rte_common.h>
61 #include <rte_ring.h>
62 #include <rte_mempool.h>
63 #include <rte_malloc.h>
64 #include <rte_mbuf.h>
65 #include <rte_errno.h>
66 #include <rte_spinlock.h>
67 #include <rte_string_fns.h>
68
69 #include "rte_ether.h"
70 #include "rte_ethdev.h"
71
72 static const char *MZ_RTE_ETH_DEV_DATA = "rte_eth_dev_data";
73 struct rte_eth_dev rte_eth_devices[RTE_MAX_ETHPORTS];
74 static struct rte_eth_dev_data *rte_eth_dev_data;
75 static uint8_t nb_ports;
76
77 /* spinlock for eth device callbacks */
78 static rte_spinlock_t rte_eth_dev_cb_lock = RTE_SPINLOCK_INITIALIZER;
79
80 /* store statistics names and its offset in stats structure  */
81 struct rte_eth_xstats_name_off {
82         char name[RTE_ETH_XSTATS_NAME_SIZE];
83         unsigned offset;
84 };
85
86 static const struct rte_eth_xstats_name_off rte_stats_strings[] = {
87         {"rx_good_packets", offsetof(struct rte_eth_stats, ipackets)},
88         {"tx_good_packets", offsetof(struct rte_eth_stats, opackets)},
89         {"rx_good_bytes", offsetof(struct rte_eth_stats, ibytes)},
90         {"tx_good_bytes", offsetof(struct rte_eth_stats, obytes)},
91         {"rx_errors", offsetof(struct rte_eth_stats, ierrors)},
92         {"tx_errors", offsetof(struct rte_eth_stats, oerrors)},
93         {"rx_mbuf_allocation_errors", offsetof(struct rte_eth_stats,
94                 rx_nombuf)},
95 };
96
97 #define RTE_NB_STATS (sizeof(rte_stats_strings) / sizeof(rte_stats_strings[0]))
98
99 static const struct rte_eth_xstats_name_off rte_rxq_stats_strings[] = {
100         {"packets", offsetof(struct rte_eth_stats, q_ipackets)},
101         {"bytes", offsetof(struct rte_eth_stats, q_ibytes)},
102         {"errors", offsetof(struct rte_eth_stats, q_errors)},
103 };
104
105 #define RTE_NB_RXQ_STATS (sizeof(rte_rxq_stats_strings) /       \
106                 sizeof(rte_rxq_stats_strings[0]))
107
108 static const struct rte_eth_xstats_name_off rte_txq_stats_strings[] = {
109         {"packets", offsetof(struct rte_eth_stats, q_opackets)},
110         {"bytes", offsetof(struct rte_eth_stats, q_obytes)},
111 };
112 #define RTE_NB_TXQ_STATS (sizeof(rte_txq_stats_strings) /       \
113                 sizeof(rte_txq_stats_strings[0]))
114
115
116 /**
117  * The user application callback description.
118  *
119  * It contains callback address to be registered by user application,
120  * the pointer to the parameters for callback, and the event type.
121  */
122 struct rte_eth_dev_callback {
123         TAILQ_ENTRY(rte_eth_dev_callback) next; /**< Callbacks list */
124         rte_eth_dev_cb_fn cb_fn;                /**< Callback address */
125         void *cb_arg;                           /**< Parameter for callback */
126         enum rte_eth_event_type event;          /**< Interrupt event type */
127         uint32_t active;                        /**< Callback is executing */
128 };
129
130 enum {
131         STAT_QMAP_TX = 0,
132         STAT_QMAP_RX
133 };
134
135 enum {
136         DEV_DETACHED = 0,
137         DEV_ATTACHED
138 };
139
140 static void
141 rte_eth_dev_data_alloc(void)
142 {
143         const unsigned flags = 0;
144         const struct rte_memzone *mz;
145
146         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
147                 mz = rte_memzone_reserve(MZ_RTE_ETH_DEV_DATA,
148                                 RTE_MAX_ETHPORTS * sizeof(*rte_eth_dev_data),
149                                 rte_socket_id(), flags);
150         } else
151                 mz = rte_memzone_lookup(MZ_RTE_ETH_DEV_DATA);
152         if (mz == NULL)
153                 rte_panic("Cannot allocate memzone for ethernet port data\n");
154
155         rte_eth_dev_data = mz->addr;
156         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
157                 memset(rte_eth_dev_data, 0,
158                                 RTE_MAX_ETHPORTS * sizeof(*rte_eth_dev_data));
159 }
160
161 struct rte_eth_dev *
162 rte_eth_dev_allocated(const char *name)
163 {
164         unsigned i;
165
166         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
167                 if ((rte_eth_devices[i].attached == DEV_ATTACHED) &&
168                     strcmp(rte_eth_devices[i].data->name, name) == 0)
169                         return &rte_eth_devices[i];
170         }
171         return NULL;
172 }
173
174 static uint8_t
175 rte_eth_dev_find_free_port(void)
176 {
177         unsigned i;
178
179         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
180                 if (rte_eth_devices[i].attached == DEV_DETACHED)
181                         return i;
182         }
183         return RTE_MAX_ETHPORTS;
184 }
185
186 struct rte_eth_dev *
187 rte_eth_dev_allocate(const char *name, enum rte_eth_dev_type type)
188 {
189         uint8_t port_id;
190         struct rte_eth_dev *eth_dev;
191
192         port_id = rte_eth_dev_find_free_port();
193         if (port_id == RTE_MAX_ETHPORTS) {
194                 RTE_PMD_DEBUG_TRACE("Reached maximum number of Ethernet ports\n");
195                 return NULL;
196         }
197
198         if (rte_eth_dev_data == NULL)
199                 rte_eth_dev_data_alloc();
200
201         if (rte_eth_dev_allocated(name) != NULL) {
202                 RTE_PMD_DEBUG_TRACE("Ethernet Device with name %s already allocated!\n",
203                                 name);
204                 return NULL;
205         }
206
207         eth_dev = &rte_eth_devices[port_id];
208         eth_dev->data = &rte_eth_dev_data[port_id];
209         snprintf(eth_dev->data->name, sizeof(eth_dev->data->name), "%s", name);
210         eth_dev->data->port_id = port_id;
211         eth_dev->attached = DEV_ATTACHED;
212         eth_dev->dev_type = type;
213         nb_ports++;
214         return eth_dev;
215 }
216
217 static int
218 rte_eth_dev_create_unique_device_name(char *name, size_t size,
219                 struct rte_pci_device *pci_dev)
220 {
221         int ret;
222
223         if ((name == NULL) || (pci_dev == NULL))
224                 return -EINVAL;
225
226         ret = snprintf(name, size, "%d:%d.%d",
227                         pci_dev->addr.bus, pci_dev->addr.devid,
228                         pci_dev->addr.function);
229         if (ret < 0)
230                 return ret;
231         return 0;
232 }
233
234 int
235 rte_eth_dev_release_port(struct rte_eth_dev *eth_dev)
236 {
237         if (eth_dev == NULL)
238                 return -EINVAL;
239
240         eth_dev->attached = DEV_DETACHED;
241         nb_ports--;
242         return 0;
243 }
244
245 static int
246 rte_eth_dev_init(struct rte_pci_driver *pci_drv,
247                  struct rte_pci_device *pci_dev)
248 {
249         struct eth_driver    *eth_drv;
250         struct rte_eth_dev *eth_dev;
251         char ethdev_name[RTE_ETH_NAME_MAX_LEN];
252
253         int diag;
254
255         eth_drv = (struct eth_driver *)pci_drv;
256
257         /* Create unique Ethernet device name using PCI address */
258         rte_eth_dev_create_unique_device_name(ethdev_name,
259                         sizeof(ethdev_name), pci_dev);
260
261         eth_dev = rte_eth_dev_allocate(ethdev_name, RTE_ETH_DEV_PCI);
262         if (eth_dev == NULL)
263                 return -ENOMEM;
264
265         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
266                 eth_dev->data->dev_private = rte_zmalloc("ethdev private structure",
267                                   eth_drv->dev_private_size,
268                                   RTE_CACHE_LINE_SIZE);
269                 if (eth_dev->data->dev_private == NULL)
270                         rte_panic("Cannot allocate memzone for private port data\n");
271         }
272         eth_dev->pci_dev = pci_dev;
273         eth_dev->driver = eth_drv;
274         eth_dev->data->rx_mbuf_alloc_failed = 0;
275
276         /* init user callbacks */
277         TAILQ_INIT(&(eth_dev->link_intr_cbs));
278
279         /*
280          * Set the default MTU.
281          */
282         eth_dev->data->mtu = ETHER_MTU;
283
284         /* Invoke PMD device initialization function */
285         diag = (*eth_drv->eth_dev_init)(eth_dev);
286         if (diag == 0)
287                 return 0;
288
289         RTE_PMD_DEBUG_TRACE("driver %s: eth_dev_init(vendor_id=0x%u device_id=0x%x) failed\n",
290                         pci_drv->name,
291                         (unsigned) pci_dev->id.vendor_id,
292                         (unsigned) pci_dev->id.device_id);
293         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
294                 rte_free(eth_dev->data->dev_private);
295         rte_eth_dev_release_port(eth_dev);
296         return diag;
297 }
298
299 static int
300 rte_eth_dev_uninit(struct rte_pci_device *pci_dev)
301 {
302         const struct eth_driver *eth_drv;
303         struct rte_eth_dev *eth_dev;
304         char ethdev_name[RTE_ETH_NAME_MAX_LEN];
305         int ret;
306
307         if (pci_dev == NULL)
308                 return -EINVAL;
309
310         /* Create unique Ethernet device name using PCI address */
311         rte_eth_dev_create_unique_device_name(ethdev_name,
312                         sizeof(ethdev_name), pci_dev);
313
314         eth_dev = rte_eth_dev_allocated(ethdev_name);
315         if (eth_dev == NULL)
316                 return -ENODEV;
317
318         eth_drv = (const struct eth_driver *)pci_dev->driver;
319
320         /* Invoke PMD device uninit function */
321         if (*eth_drv->eth_dev_uninit) {
322                 ret = (*eth_drv->eth_dev_uninit)(eth_dev);
323                 if (ret)
324                         return ret;
325         }
326
327         /* free ether device */
328         rte_eth_dev_release_port(eth_dev);
329
330         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
331                 rte_free(eth_dev->data->dev_private);
332
333         eth_dev->pci_dev = NULL;
334         eth_dev->driver = NULL;
335         eth_dev->data = NULL;
336
337         return 0;
338 }
339
340 /**
341  * Register an Ethernet [Poll Mode] driver.
342  *
343  * Function invoked by the initialization function of an Ethernet driver
344  * to simultaneously register itself as a PCI driver and as an Ethernet
345  * Poll Mode Driver.
346  * Invokes the rte_eal_pci_register() function to register the *pci_drv*
347  * structure embedded in the *eth_drv* structure, after having stored the
348  * address of the rte_eth_dev_init() function in the *devinit* field of
349  * the *pci_drv* structure.
350  * During the PCI probing phase, the rte_eth_dev_init() function is
351  * invoked for each PCI [Ethernet device] matching the embedded PCI
352  * identifiers provided by the driver.
353  */
354 void
355 rte_eth_driver_register(struct eth_driver *eth_drv)
356 {
357         eth_drv->pci_drv.devinit = rte_eth_dev_init;
358         eth_drv->pci_drv.devuninit = rte_eth_dev_uninit;
359         rte_eal_pci_register(&eth_drv->pci_drv);
360 }
361
362 int
363 rte_eth_dev_is_valid_port(uint8_t port_id)
364 {
365         if (port_id >= RTE_MAX_ETHPORTS ||
366             rte_eth_devices[port_id].attached != DEV_ATTACHED)
367                 return 0;
368         else
369                 return 1;
370 }
371
372 int
373 rte_eth_dev_socket_id(uint8_t port_id)
374 {
375         if (!rte_eth_dev_is_valid_port(port_id))
376                 return -1;
377         return rte_eth_devices[port_id].data->numa_node;
378 }
379
380 uint8_t
381 rte_eth_dev_count(void)
382 {
383         return nb_ports;
384 }
385
386 static enum rte_eth_dev_type
387 rte_eth_dev_get_device_type(uint8_t port_id)
388 {
389         if (!rte_eth_dev_is_valid_port(port_id))
390                 return RTE_ETH_DEV_UNKNOWN;
391         return rte_eth_devices[port_id].dev_type;
392 }
393
394 static int
395 rte_eth_dev_get_addr_by_port(uint8_t port_id, struct rte_pci_addr *addr)
396 {
397         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
398
399         if (addr == NULL) {
400                 RTE_PMD_DEBUG_TRACE("Null pointer is specified\n");
401                 return -EINVAL;
402         }
403
404         *addr = rte_eth_devices[port_id].pci_dev->addr;
405         return 0;
406 }
407
408 static int
409 rte_eth_dev_get_name_by_port(uint8_t port_id, char *name)
410 {
411         char *tmp;
412
413         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
414
415         if (name == NULL) {
416                 RTE_PMD_DEBUG_TRACE("Null pointer is specified\n");
417                 return -EINVAL;
418         }
419
420         /* shouldn't check 'rte_eth_devices[i].data',
421          * because it might be overwritten by VDEV PMD */
422         tmp = rte_eth_dev_data[port_id].name;
423         strcpy(name, tmp);
424         return 0;
425 }
426
427 static int
428 rte_eth_dev_get_port_by_name(const char *name, uint8_t *port_id)
429 {
430         int i;
431
432         if (name == NULL) {
433                 RTE_PMD_DEBUG_TRACE("Null pointer is specified\n");
434                 return -EINVAL;
435         }
436
437         *port_id = RTE_MAX_ETHPORTS;
438
439         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
440
441                 if (!strncmp(name,
442                         rte_eth_dev_data[i].name, strlen(name))) {
443
444                         *port_id = i;
445
446                         return 0;
447                 }
448         }
449         return -ENODEV;
450 }
451
452 static int
453 rte_eth_dev_get_port_by_addr(const struct rte_pci_addr *addr, uint8_t *port_id)
454 {
455         int i;
456         struct rte_pci_device *pci_dev = NULL;
457
458         if (addr == NULL) {
459                 RTE_PMD_DEBUG_TRACE("Null pointer is specified\n");
460                 return -EINVAL;
461         }
462
463         *port_id = RTE_MAX_ETHPORTS;
464
465         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
466
467                 pci_dev = rte_eth_devices[i].pci_dev;
468
469                 if (pci_dev &&
470                         !rte_eal_compare_pci_addr(&pci_dev->addr, addr)) {
471
472                         *port_id = i;
473
474                         return 0;
475                 }
476         }
477         return -ENODEV;
478 }
479
480 static int
481 rte_eth_dev_is_detachable(uint8_t port_id)
482 {
483         uint32_t dev_flags;
484
485         if (!rte_eth_dev_is_valid_port(port_id)) {
486                 RTE_PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
487                 return -EINVAL;
488         }
489
490         switch (rte_eth_devices[port_id].data->kdrv) {
491         case RTE_KDRV_IGB_UIO:
492         case RTE_KDRV_UIO_GENERIC:
493         case RTE_KDRV_NIC_UIO:
494         case RTE_KDRV_NONE:
495                 break;
496         case RTE_KDRV_VFIO:
497         default:
498                 return -ENOTSUP;
499         }
500         dev_flags = rte_eth_devices[port_id].data->dev_flags;
501         return !(dev_flags & RTE_ETH_DEV_DETACHABLE);
502 }
503
504 /* attach the new physical device, then store port_id of the device */
505 static int
506 rte_eth_dev_attach_pdev(struct rte_pci_addr *addr, uint8_t *port_id)
507 {
508         if ((addr == NULL) || (port_id == NULL))
509                 goto err;
510
511         /* re-construct pci_device_list */
512         if (rte_eal_pci_scan())
513                 goto err;
514         /* Invoke probe func of the driver can handle the new device. */
515         if (rte_eal_pci_probe_one(addr))
516                 goto err;
517
518         if (rte_eth_dev_get_port_by_addr(addr, port_id))
519                 goto err;
520
521         return 0;
522 err:
523         RTE_LOG(ERR, EAL, "Driver, cannot attach the device\n");
524         return -1;
525 }
526
527 /* detach the new physical device, then store pci_addr of the device */
528 static int
529 rte_eth_dev_detach_pdev(uint8_t port_id, struct rte_pci_addr *addr)
530 {
531         struct rte_pci_addr freed_addr;
532         struct rte_pci_addr vp;
533
534         if (addr == NULL)
535                 goto err;
536
537         /* check whether the driver supports detach feature, or not */
538         if (rte_eth_dev_is_detachable(port_id))
539                 goto err;
540
541         /* get pci address by port id */
542         if (rte_eth_dev_get_addr_by_port(port_id, &freed_addr))
543                 goto err;
544
545         /* Zeroed pci addr means the port comes from virtual device */
546         vp.domain = vp.bus = vp.devid = vp.function = 0;
547         if (rte_eal_compare_pci_addr(&vp, &freed_addr) == 0)
548                 goto err;
549
550         /* invoke devuninit func of the pci driver,
551          * also remove the device from pci_device_list */
552         if (rte_eal_pci_detach(&freed_addr))
553                 goto err;
554
555         *addr = freed_addr;
556         return 0;
557 err:
558         RTE_LOG(ERR, EAL, "Driver, cannot detach the device\n");
559         return -1;
560 }
561
562 /* attach the new virtual device, then store port_id of the device */
563 static int
564 rte_eth_dev_attach_vdev(const char *vdevargs, uint8_t *port_id)
565 {
566         char *name = NULL, *args = NULL;
567         int ret = -1;
568
569         if ((vdevargs == NULL) || (port_id == NULL))
570                 goto end;
571
572         /* parse vdevargs, then retrieve device name and args */
573         if (rte_eal_parse_devargs_str(vdevargs, &name, &args))
574                 goto end;
575
576         /* walk around dev_driver_list to find the driver of the device,
577          * then invoke probe function of the driver.
578          * rte_eal_vdev_init() updates port_id allocated after
579          * initialization.
580          */
581         if (rte_eal_vdev_init(name, args))
582                 goto end;
583
584         if (rte_eth_dev_get_port_by_name(name, port_id))
585                 goto end;
586
587         ret = 0;
588 end:
589         if (name)
590                 free(name);
591         if (args)
592                 free(args);
593
594         if (ret < 0)
595                 RTE_LOG(ERR, EAL, "Driver, cannot attach the device\n");
596         return ret;
597 }
598
599 /* detach the new virtual device, then store the name of the device */
600 static int
601 rte_eth_dev_detach_vdev(uint8_t port_id, char *vdevname)
602 {
603         char name[RTE_ETH_NAME_MAX_LEN];
604
605         if (vdevname == NULL)
606                 goto err;
607
608         /* check whether the driver supports detach feature, or not */
609         if (rte_eth_dev_is_detachable(port_id))
610                 goto err;
611
612         /* get device name by port id */
613         if (rte_eth_dev_get_name_by_port(port_id, name))
614                 goto err;
615         /* walk around dev_driver_list to find the driver of the device,
616          * then invoke uninit function of the driver */
617         if (rte_eal_vdev_uninit(name))
618                 goto err;
619
620         strncpy(vdevname, name, sizeof(name));
621         return 0;
622 err:
623         RTE_LOG(ERR, EAL, "Driver, cannot detach the device\n");
624         return -1;
625 }
626
627 /* attach the new device, then store port_id of the device */
628 int
629 rte_eth_dev_attach(const char *devargs, uint8_t *port_id)
630 {
631         struct rte_pci_addr addr;
632
633         if ((devargs == NULL) || (port_id == NULL))
634                 return -EINVAL;
635
636         if (eal_parse_pci_DomBDF(devargs, &addr) == 0)
637                 return rte_eth_dev_attach_pdev(&addr, port_id);
638         else
639                 return rte_eth_dev_attach_vdev(devargs, port_id);
640 }
641
642 /* detach the device, then store the name of the device */
643 int
644 rte_eth_dev_detach(uint8_t port_id, char *name)
645 {
646         struct rte_pci_addr addr;
647         int ret;
648
649         if (name == NULL)
650                 return -EINVAL;
651
652         if (rte_eth_dev_get_device_type(port_id) == RTE_ETH_DEV_PCI) {
653                 ret = rte_eth_dev_get_addr_by_port(port_id, &addr);
654                 if (ret < 0)
655                         return ret;
656
657                 ret = rte_eth_dev_detach_pdev(port_id, &addr);
658                 if (ret == 0)
659                         snprintf(name, RTE_ETH_NAME_MAX_LEN,
660                                 "%04x:%02x:%02x.%d",
661                                 addr.domain, addr.bus,
662                                 addr.devid, addr.function);
663
664                 return ret;
665         } else
666                 return rte_eth_dev_detach_vdev(port_id, name);
667 }
668
669 static int
670 rte_eth_dev_rx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
671 {
672         uint16_t old_nb_queues = dev->data->nb_rx_queues;
673         void **rxq;
674         unsigned i;
675
676         if (dev->data->rx_queues == NULL) { /* first time configuration */
677                 dev->data->rx_queues = rte_zmalloc("ethdev->rx_queues",
678                                 sizeof(dev->data->rx_queues[0]) * nb_queues,
679                                 RTE_CACHE_LINE_SIZE);
680                 if (dev->data->rx_queues == NULL) {
681                         dev->data->nb_rx_queues = 0;
682                         return -(ENOMEM);
683                 }
684         } else { /* re-configure */
685                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_release, -ENOTSUP);
686
687                 rxq = dev->data->rx_queues;
688
689                 for (i = nb_queues; i < old_nb_queues; i++)
690                         (*dev->dev_ops->rx_queue_release)(rxq[i]);
691                 rxq = rte_realloc(rxq, sizeof(rxq[0]) * nb_queues,
692                                 RTE_CACHE_LINE_SIZE);
693                 if (rxq == NULL)
694                         return -(ENOMEM);
695                 if (nb_queues > old_nb_queues) {
696                         uint16_t new_qs = nb_queues - old_nb_queues;
697
698                         memset(rxq + old_nb_queues, 0,
699                                 sizeof(rxq[0]) * new_qs);
700                 }
701
702                 dev->data->rx_queues = rxq;
703
704         }
705         dev->data->nb_rx_queues = nb_queues;
706         return 0;
707 }
708
709 int
710 rte_eth_dev_rx_queue_start(uint8_t port_id, uint16_t rx_queue_id)
711 {
712         struct rte_eth_dev *dev;
713
714         /* This function is only safe when called from the primary process
715          * in a multi-process setup*/
716         RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
717
718         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
719
720         dev = &rte_eth_devices[port_id];
721         if (rx_queue_id >= dev->data->nb_rx_queues) {
722                 RTE_PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", rx_queue_id);
723                 return -EINVAL;
724         }
725
726         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_start, -ENOTSUP);
727
728         if (dev->data->rx_queue_state[rx_queue_id] != RTE_ETH_QUEUE_STATE_STOPPED) {
729                 RTE_PMD_DEBUG_TRACE("Queue %" PRIu16" of device with port_id=%" PRIu8
730                         " already started\n",
731                         rx_queue_id, port_id);
732                 return 0;
733         }
734
735         return dev->dev_ops->rx_queue_start(dev, rx_queue_id);
736
737 }
738
739 int
740 rte_eth_dev_rx_queue_stop(uint8_t port_id, uint16_t rx_queue_id)
741 {
742         struct rte_eth_dev *dev;
743
744         /* This function is only safe when called from the primary process
745          * in a multi-process setup*/
746         RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
747
748         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
749
750         dev = &rte_eth_devices[port_id];
751         if (rx_queue_id >= dev->data->nb_rx_queues) {
752                 RTE_PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", rx_queue_id);
753                 return -EINVAL;
754         }
755
756         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_stop, -ENOTSUP);
757
758         if (dev->data->rx_queue_state[rx_queue_id] == RTE_ETH_QUEUE_STATE_STOPPED) {
759                 RTE_PMD_DEBUG_TRACE("Queue %" PRIu16" of device with port_id=%" PRIu8
760                         " already stopped\n",
761                         rx_queue_id, port_id);
762                 return 0;
763         }
764
765         return dev->dev_ops->rx_queue_stop(dev, rx_queue_id);
766
767 }
768
769 int
770 rte_eth_dev_tx_queue_start(uint8_t port_id, uint16_t tx_queue_id)
771 {
772         struct rte_eth_dev *dev;
773
774         /* This function is only safe when called from the primary process
775          * in a multi-process setup*/
776         RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
777
778         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
779
780         dev = &rte_eth_devices[port_id];
781         if (tx_queue_id >= dev->data->nb_tx_queues) {
782                 RTE_PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", tx_queue_id);
783                 return -EINVAL;
784         }
785
786         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_start, -ENOTSUP);
787
788         if (dev->data->tx_queue_state[tx_queue_id] != RTE_ETH_QUEUE_STATE_STOPPED) {
789                 RTE_PMD_DEBUG_TRACE("Queue %" PRIu16" of device with port_id=%" PRIu8
790                         " already started\n",
791                         tx_queue_id, port_id);
792                 return 0;
793         }
794
795         return dev->dev_ops->tx_queue_start(dev, tx_queue_id);
796
797 }
798
799 int
800 rte_eth_dev_tx_queue_stop(uint8_t port_id, uint16_t tx_queue_id)
801 {
802         struct rte_eth_dev *dev;
803
804         /* This function is only safe when called from the primary process
805          * in a multi-process setup*/
806         RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
807
808         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
809
810         dev = &rte_eth_devices[port_id];
811         if (tx_queue_id >= dev->data->nb_tx_queues) {
812                 RTE_PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", tx_queue_id);
813                 return -EINVAL;
814         }
815
816         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_stop, -ENOTSUP);
817
818         if (dev->data->tx_queue_state[tx_queue_id] == RTE_ETH_QUEUE_STATE_STOPPED) {
819                 RTE_PMD_DEBUG_TRACE("Queue %" PRIu16" of device with port_id=%" PRIu8
820                         " already stopped\n",
821                         tx_queue_id, port_id);
822                 return 0;
823         }
824
825         return dev->dev_ops->tx_queue_stop(dev, tx_queue_id);
826
827 }
828
829 static int
830 rte_eth_dev_tx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
831 {
832         uint16_t old_nb_queues = dev->data->nb_tx_queues;
833         void **txq;
834         unsigned i;
835
836         if (dev->data->tx_queues == NULL) { /* first time configuration */
837                 dev->data->tx_queues = rte_zmalloc("ethdev->tx_queues",
838                                                    sizeof(dev->data->tx_queues[0]) * nb_queues,
839                                                    RTE_CACHE_LINE_SIZE);
840                 if (dev->data->tx_queues == NULL) {
841                         dev->data->nb_tx_queues = 0;
842                         return -(ENOMEM);
843                 }
844         } else { /* re-configure */
845                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_release, -ENOTSUP);
846
847                 txq = dev->data->tx_queues;
848
849                 for (i = nb_queues; i < old_nb_queues; i++)
850                         (*dev->dev_ops->tx_queue_release)(txq[i]);
851                 txq = rte_realloc(txq, sizeof(txq[0]) * nb_queues,
852                                   RTE_CACHE_LINE_SIZE);
853                 if (txq == NULL)
854                         return -ENOMEM;
855                 if (nb_queues > old_nb_queues) {
856                         uint16_t new_qs = nb_queues - old_nb_queues;
857
858                         memset(txq + old_nb_queues, 0,
859                                sizeof(txq[0]) * new_qs);
860                 }
861
862                 dev->data->tx_queues = txq;
863
864         }
865         dev->data->nb_tx_queues = nb_queues;
866         return 0;
867 }
868
869 int
870 rte_eth_dev_configure(uint8_t port_id, uint16_t nb_rx_q, uint16_t nb_tx_q,
871                       const struct rte_eth_conf *dev_conf)
872 {
873         struct rte_eth_dev *dev;
874         struct rte_eth_dev_info dev_info;
875         int diag;
876
877         /* This function is only safe when called from the primary process
878          * in a multi-process setup*/
879         RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
880
881         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
882
883         if (nb_rx_q > RTE_MAX_QUEUES_PER_PORT) {
884                 RTE_PMD_DEBUG_TRACE(
885                         "Number of RX queues requested (%u) is greater than max supported(%d)\n",
886                         nb_rx_q, RTE_MAX_QUEUES_PER_PORT);
887                 return -EINVAL;
888         }
889
890         if (nb_tx_q > RTE_MAX_QUEUES_PER_PORT) {
891                 RTE_PMD_DEBUG_TRACE(
892                         "Number of TX queues requested (%u) is greater than max supported(%d)\n",
893                         nb_tx_q, RTE_MAX_QUEUES_PER_PORT);
894                 return -EINVAL;
895         }
896
897         dev = &rte_eth_devices[port_id];
898
899         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
900         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_configure, -ENOTSUP);
901
902         if (dev->data->dev_started) {
903                 RTE_PMD_DEBUG_TRACE(
904                     "port %d must be stopped to allow configuration\n", port_id);
905                 return -EBUSY;
906         }
907
908         /*
909          * Check that the numbers of RX and TX queues are not greater
910          * than the maximum number of RX and TX queues supported by the
911          * configured device.
912          */
913         (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
914         if (nb_rx_q > dev_info.max_rx_queues) {
915                 RTE_PMD_DEBUG_TRACE("ethdev port_id=%d nb_rx_queues=%d > %d\n",
916                                 port_id, nb_rx_q, dev_info.max_rx_queues);
917                 return -EINVAL;
918         }
919         if (nb_rx_q == 0) {
920                 RTE_PMD_DEBUG_TRACE("ethdev port_id=%d nb_rx_q == 0\n", port_id);
921                 return -EINVAL;
922         }
923
924         if (nb_tx_q > dev_info.max_tx_queues) {
925                 RTE_PMD_DEBUG_TRACE("ethdev port_id=%d nb_tx_queues=%d > %d\n",
926                                 port_id, nb_tx_q, dev_info.max_tx_queues);
927                 return -EINVAL;
928         }
929         if (nb_tx_q == 0) {
930                 RTE_PMD_DEBUG_TRACE("ethdev port_id=%d nb_tx_q == 0\n", port_id);
931                 return -EINVAL;
932         }
933
934         /* Copy the dev_conf parameter into the dev structure */
935         memcpy(&dev->data->dev_conf, dev_conf, sizeof(dev->data->dev_conf));
936
937         /*
938          * If link state interrupt is enabled, check that the
939          * device supports it.
940          */
941         if ((dev_conf->intr_conf.lsc == 1) &&
942                 (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC))) {
943                         RTE_PMD_DEBUG_TRACE("driver %s does not support lsc\n",
944                                         dev->data->drv_name);
945                         return -EINVAL;
946         }
947
948         /*
949          * If jumbo frames are enabled, check that the maximum RX packet
950          * length is supported by the configured device.
951          */
952         if (dev_conf->rxmode.jumbo_frame == 1) {
953                 if (dev_conf->rxmode.max_rx_pkt_len >
954                     dev_info.max_rx_pktlen) {
955                         RTE_PMD_DEBUG_TRACE("ethdev port_id=%d max_rx_pkt_len %u"
956                                 " > max valid value %u\n",
957                                 port_id,
958                                 (unsigned)dev_conf->rxmode.max_rx_pkt_len,
959                                 (unsigned)dev_info.max_rx_pktlen);
960                         return -EINVAL;
961                 } else if (dev_conf->rxmode.max_rx_pkt_len < ETHER_MIN_LEN) {
962                         RTE_PMD_DEBUG_TRACE("ethdev port_id=%d max_rx_pkt_len %u"
963                                 " < min valid value %u\n",
964                                 port_id,
965                                 (unsigned)dev_conf->rxmode.max_rx_pkt_len,
966                                 (unsigned)ETHER_MIN_LEN);
967                         return -EINVAL;
968                 }
969         } else {
970                 if (dev_conf->rxmode.max_rx_pkt_len < ETHER_MIN_LEN ||
971                         dev_conf->rxmode.max_rx_pkt_len > ETHER_MAX_LEN)
972                         /* Use default value */
973                         dev->data->dev_conf.rxmode.max_rx_pkt_len =
974                                                         ETHER_MAX_LEN;
975         }
976
977         /*
978          * Setup new number of RX/TX queues and reconfigure device.
979          */
980         diag = rte_eth_dev_rx_queue_config(dev, nb_rx_q);
981         if (diag != 0) {
982                 RTE_PMD_DEBUG_TRACE("port%d rte_eth_dev_rx_queue_config = %d\n",
983                                 port_id, diag);
984                 return diag;
985         }
986
987         diag = rte_eth_dev_tx_queue_config(dev, nb_tx_q);
988         if (diag != 0) {
989                 RTE_PMD_DEBUG_TRACE("port%d rte_eth_dev_tx_queue_config = %d\n",
990                                 port_id, diag);
991                 rte_eth_dev_rx_queue_config(dev, 0);
992                 return diag;
993         }
994
995         diag = (*dev->dev_ops->dev_configure)(dev);
996         if (diag != 0) {
997                 RTE_PMD_DEBUG_TRACE("port%d dev_configure = %d\n",
998                                 port_id, diag);
999                 rte_eth_dev_rx_queue_config(dev, 0);
1000                 rte_eth_dev_tx_queue_config(dev, 0);
1001                 return diag;
1002         }
1003
1004         return 0;
1005 }
1006
1007 static void
1008 rte_eth_dev_config_restore(uint8_t port_id)
1009 {
1010         struct rte_eth_dev *dev;
1011         struct rte_eth_dev_info dev_info;
1012         struct ether_addr addr;
1013         uint16_t i;
1014         uint32_t pool = 0;
1015
1016         dev = &rte_eth_devices[port_id];
1017
1018         rte_eth_dev_info_get(port_id, &dev_info);
1019
1020         if (RTE_ETH_DEV_SRIOV(dev).active)
1021                 pool = RTE_ETH_DEV_SRIOV(dev).def_vmdq_idx;
1022
1023         /* replay MAC address configuration */
1024         for (i = 0; i < dev_info.max_mac_addrs; i++) {
1025                 addr = dev->data->mac_addrs[i];
1026
1027                 /* skip zero address */
1028                 if (is_zero_ether_addr(&addr))
1029                         continue;
1030
1031                 /* add address to the hardware */
1032                 if  (*dev->dev_ops->mac_addr_add &&
1033                         (dev->data->mac_pool_sel[i] & (1ULL << pool)))
1034                         (*dev->dev_ops->mac_addr_add)(dev, &addr, i, pool);
1035                 else {
1036                         RTE_PMD_DEBUG_TRACE("port %d: MAC address array not supported\n",
1037                                         port_id);
1038                         /* exit the loop but not return an error */
1039                         break;
1040                 }
1041         }
1042
1043         /* replay promiscuous configuration */
1044         if (rte_eth_promiscuous_get(port_id) == 1)
1045                 rte_eth_promiscuous_enable(port_id);
1046         else if (rte_eth_promiscuous_get(port_id) == 0)
1047                 rte_eth_promiscuous_disable(port_id);
1048
1049         /* replay all multicast configuration */
1050         if (rte_eth_allmulticast_get(port_id) == 1)
1051                 rte_eth_allmulticast_enable(port_id);
1052         else if (rte_eth_allmulticast_get(port_id) == 0)
1053                 rte_eth_allmulticast_disable(port_id);
1054 }
1055
1056 int
1057 rte_eth_dev_start(uint8_t port_id)
1058 {
1059         struct rte_eth_dev *dev;
1060         int diag;
1061
1062         /* This function is only safe when called from the primary process
1063          * in a multi-process setup*/
1064         RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
1065
1066         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1067
1068         dev = &rte_eth_devices[port_id];
1069
1070         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
1071
1072         if (dev->data->dev_started != 0) {
1073                 RTE_PMD_DEBUG_TRACE("Device with port_id=%" PRIu8
1074                         " already started\n",
1075                         port_id);
1076                 return 0;
1077         }
1078
1079         diag = (*dev->dev_ops->dev_start)(dev);
1080         if (diag == 0)
1081                 dev->data->dev_started = 1;
1082         else
1083                 return diag;
1084
1085         rte_eth_dev_config_restore(port_id);
1086
1087         if (dev->data->dev_conf.intr_conf.lsc == 0) {
1088                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->link_update, -ENOTSUP);
1089                 (*dev->dev_ops->link_update)(dev, 0);
1090         }
1091         return 0;
1092 }
1093
1094 void
1095 rte_eth_dev_stop(uint8_t port_id)
1096 {
1097         struct rte_eth_dev *dev;
1098
1099         /* This function is only safe when called from the primary process
1100          * in a multi-process setup*/
1101         RTE_PROC_PRIMARY_OR_RET();
1102
1103         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1104         dev = &rte_eth_devices[port_id];
1105
1106         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
1107
1108         if (dev->data->dev_started == 0) {
1109                 RTE_PMD_DEBUG_TRACE("Device with port_id=%" PRIu8
1110                         " already stopped\n",
1111                         port_id);
1112                 return;
1113         }
1114
1115         dev->data->dev_started = 0;
1116         (*dev->dev_ops->dev_stop)(dev);
1117 }
1118
1119 int
1120 rte_eth_dev_set_link_up(uint8_t port_id)
1121 {
1122         struct rte_eth_dev *dev;
1123
1124         /* This function is only safe when called from the primary process
1125          * in a multi-process setup*/
1126         RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
1127
1128         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1129
1130         dev = &rte_eth_devices[port_id];
1131
1132         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_set_link_up, -ENOTSUP);
1133         return (*dev->dev_ops->dev_set_link_up)(dev);
1134 }
1135
1136 int
1137 rte_eth_dev_set_link_down(uint8_t port_id)
1138 {
1139         struct rte_eth_dev *dev;
1140
1141         /* This function is only safe when called from the primary process
1142          * in a multi-process setup*/
1143         RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
1144
1145         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1146
1147         dev = &rte_eth_devices[port_id];
1148
1149         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_set_link_down, -ENOTSUP);
1150         return (*dev->dev_ops->dev_set_link_down)(dev);
1151 }
1152
1153 void
1154 rte_eth_dev_close(uint8_t port_id)
1155 {
1156         struct rte_eth_dev *dev;
1157
1158         /* This function is only safe when called from the primary process
1159          * in a multi-process setup*/
1160         RTE_PROC_PRIMARY_OR_RET();
1161
1162         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1163         dev = &rte_eth_devices[port_id];
1164
1165         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_close);
1166         dev->data->dev_started = 0;
1167         (*dev->dev_ops->dev_close)(dev);
1168
1169         rte_free(dev->data->rx_queues);
1170         dev->data->rx_queues = NULL;
1171         rte_free(dev->data->tx_queues);
1172         dev->data->tx_queues = NULL;
1173 }
1174
1175 int
1176 rte_eth_rx_queue_setup(uint8_t port_id, uint16_t rx_queue_id,
1177                        uint16_t nb_rx_desc, unsigned int socket_id,
1178                        const struct rte_eth_rxconf *rx_conf,
1179                        struct rte_mempool *mp)
1180 {
1181         int ret;
1182         uint32_t mbp_buf_size;
1183         struct rte_eth_dev *dev;
1184         struct rte_eth_dev_info dev_info;
1185
1186         /* This function is only safe when called from the primary process
1187          * in a multi-process setup*/
1188         RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
1189
1190         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1191
1192         dev = &rte_eth_devices[port_id];
1193         if (rx_queue_id >= dev->data->nb_rx_queues) {
1194                 RTE_PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", rx_queue_id);
1195                 return -EINVAL;
1196         }
1197
1198         if (dev->data->dev_started) {
1199                 RTE_PMD_DEBUG_TRACE(
1200                     "port %d must be stopped to allow configuration\n", port_id);
1201                 return -EBUSY;
1202         }
1203
1204         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
1205         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_setup, -ENOTSUP);
1206
1207         /*
1208          * Check the size of the mbuf data buffer.
1209          * This value must be provided in the private data of the memory pool.
1210          * First check that the memory pool has a valid private data.
1211          */
1212         rte_eth_dev_info_get(port_id, &dev_info);
1213         if (mp->private_data_size < sizeof(struct rte_pktmbuf_pool_private)) {
1214                 RTE_PMD_DEBUG_TRACE("%s private_data_size %d < %d\n",
1215                                 mp->name, (int) mp->private_data_size,
1216                                 (int) sizeof(struct rte_pktmbuf_pool_private));
1217                 return -ENOSPC;
1218         }
1219         mbp_buf_size = rte_pktmbuf_data_room_size(mp);
1220
1221         if ((mbp_buf_size - RTE_PKTMBUF_HEADROOM) < dev_info.min_rx_bufsize) {
1222                 RTE_PMD_DEBUG_TRACE("%s mbuf_data_room_size %d < %d "
1223                                 "(RTE_PKTMBUF_HEADROOM=%d + min_rx_bufsize(dev)"
1224                                 "=%d)\n",
1225                                 mp->name,
1226                                 (int)mbp_buf_size,
1227                                 (int)(RTE_PKTMBUF_HEADROOM +
1228                                       dev_info.min_rx_bufsize),
1229                                 (int)RTE_PKTMBUF_HEADROOM,
1230                                 (int)dev_info.min_rx_bufsize);
1231                 return -EINVAL;
1232         }
1233
1234         if (nb_rx_desc > dev_info.rx_desc_lim.nb_max ||
1235                         nb_rx_desc < dev_info.rx_desc_lim.nb_min ||
1236                         nb_rx_desc % dev_info.rx_desc_lim.nb_align != 0) {
1237
1238                 RTE_PMD_DEBUG_TRACE("Invalid value for nb_rx_desc(=%hu), "
1239                         "should be: <= %hu, = %hu, and a product of %hu\n",
1240                         nb_rx_desc,
1241                         dev_info.rx_desc_lim.nb_max,
1242                         dev_info.rx_desc_lim.nb_min,
1243                         dev_info.rx_desc_lim.nb_align);
1244                 return -EINVAL;
1245         }
1246
1247         if (rx_conf == NULL)
1248                 rx_conf = &dev_info.default_rxconf;
1249
1250         ret = (*dev->dev_ops->rx_queue_setup)(dev, rx_queue_id, nb_rx_desc,
1251                                               socket_id, rx_conf, mp);
1252         if (!ret) {
1253                 if (!dev->data->min_rx_buf_size ||
1254                     dev->data->min_rx_buf_size > mbp_buf_size)
1255                         dev->data->min_rx_buf_size = mbp_buf_size;
1256         }
1257
1258         return ret;
1259 }
1260
1261 int
1262 rte_eth_tx_queue_setup(uint8_t port_id, uint16_t tx_queue_id,
1263                        uint16_t nb_tx_desc, unsigned int socket_id,
1264                        const struct rte_eth_txconf *tx_conf)
1265 {
1266         struct rte_eth_dev *dev;
1267         struct rte_eth_dev_info dev_info;
1268
1269         /* This function is only safe when called from the primary process
1270          * in a multi-process setup*/
1271         RTE_PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
1272
1273         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1274
1275         dev = &rte_eth_devices[port_id];
1276         if (tx_queue_id >= dev->data->nb_tx_queues) {
1277                 RTE_PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", tx_queue_id);
1278                 return -EINVAL;
1279         }
1280
1281         if (dev->data->dev_started) {
1282                 RTE_PMD_DEBUG_TRACE(
1283                     "port %d must be stopped to allow configuration\n", port_id);
1284                 return -EBUSY;
1285         }
1286
1287         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
1288         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_setup, -ENOTSUP);
1289
1290         rte_eth_dev_info_get(port_id, &dev_info);
1291
1292         if (nb_tx_desc > dev_info.tx_desc_lim.nb_max ||
1293             nb_tx_desc < dev_info.tx_desc_lim.nb_min ||
1294             nb_tx_desc % dev_info.tx_desc_lim.nb_align != 0) {
1295                 RTE_PMD_DEBUG_TRACE("Invalid value for nb_tx_desc(=%hu), "
1296                                 "should be: <= %hu, = %hu, and a product of %hu\n",
1297                                 nb_tx_desc,
1298                                 dev_info.tx_desc_lim.nb_max,
1299                                 dev_info.tx_desc_lim.nb_min,
1300                                 dev_info.tx_desc_lim.nb_align);
1301                 return -EINVAL;
1302         }
1303
1304         if (tx_conf == NULL)
1305                 tx_conf = &dev_info.default_txconf;
1306
1307         return (*dev->dev_ops->tx_queue_setup)(dev, tx_queue_id, nb_tx_desc,
1308                                                socket_id, tx_conf);
1309 }
1310
1311 void
1312 rte_eth_promiscuous_enable(uint8_t port_id)
1313 {
1314         struct rte_eth_dev *dev;
1315
1316         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1317         dev = &rte_eth_devices[port_id];
1318
1319         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->promiscuous_enable);
1320         (*dev->dev_ops->promiscuous_enable)(dev);
1321         dev->data->promiscuous = 1;
1322 }
1323
1324 void
1325 rte_eth_promiscuous_disable(uint8_t port_id)
1326 {
1327         struct rte_eth_dev *dev;
1328
1329         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1330         dev = &rte_eth_devices[port_id];
1331
1332         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->promiscuous_disable);
1333         dev->data->promiscuous = 0;
1334         (*dev->dev_ops->promiscuous_disable)(dev);
1335 }
1336
1337 int
1338 rte_eth_promiscuous_get(uint8_t port_id)
1339 {
1340         struct rte_eth_dev *dev;
1341
1342         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1343
1344         dev = &rte_eth_devices[port_id];
1345         return dev->data->promiscuous;
1346 }
1347
1348 void
1349 rte_eth_allmulticast_enable(uint8_t port_id)
1350 {
1351         struct rte_eth_dev *dev;
1352
1353         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1354         dev = &rte_eth_devices[port_id];
1355
1356         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_enable);
1357         (*dev->dev_ops->allmulticast_enable)(dev);
1358         dev->data->all_multicast = 1;
1359 }
1360
1361 void
1362 rte_eth_allmulticast_disable(uint8_t port_id)
1363 {
1364         struct rte_eth_dev *dev;
1365
1366         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1367         dev = &rte_eth_devices[port_id];
1368
1369         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_disable);
1370         dev->data->all_multicast = 0;
1371         (*dev->dev_ops->allmulticast_disable)(dev);
1372 }
1373
1374 int
1375 rte_eth_allmulticast_get(uint8_t port_id)
1376 {
1377         struct rte_eth_dev *dev;
1378
1379         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1380
1381         dev = &rte_eth_devices[port_id];
1382         return dev->data->all_multicast;
1383 }
1384
1385 static inline int
1386 rte_eth_dev_atomic_read_link_status(struct rte_eth_dev *dev,
1387                                 struct rte_eth_link *link)
1388 {
1389         struct rte_eth_link *dst = link;
1390         struct rte_eth_link *src = &(dev->data->dev_link);
1391
1392         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
1393                                         *(uint64_t *)src) == 0)
1394                 return -1;
1395
1396         return 0;
1397 }
1398
1399 void
1400 rte_eth_link_get(uint8_t port_id, struct rte_eth_link *eth_link)
1401 {
1402         struct rte_eth_dev *dev;
1403
1404         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1405         dev = &rte_eth_devices[port_id];
1406
1407         if (dev->data->dev_conf.intr_conf.lsc != 0)
1408                 rte_eth_dev_atomic_read_link_status(dev, eth_link);
1409         else {
1410                 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
1411                 (*dev->dev_ops->link_update)(dev, 1);
1412                 *eth_link = dev->data->dev_link;
1413         }
1414 }
1415
1416 void
1417 rte_eth_link_get_nowait(uint8_t port_id, struct rte_eth_link *eth_link)
1418 {
1419         struct rte_eth_dev *dev;
1420
1421         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1422         dev = &rte_eth_devices[port_id];
1423
1424         if (dev->data->dev_conf.intr_conf.lsc != 0)
1425                 rte_eth_dev_atomic_read_link_status(dev, eth_link);
1426         else {
1427                 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
1428                 (*dev->dev_ops->link_update)(dev, 0);
1429                 *eth_link = dev->data->dev_link;
1430         }
1431 }
1432
1433 int
1434 rte_eth_stats_get(uint8_t port_id, struct rte_eth_stats *stats)
1435 {
1436         struct rte_eth_dev *dev;
1437
1438         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1439
1440         dev = &rte_eth_devices[port_id];
1441         memset(stats, 0, sizeof(*stats));
1442
1443         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->stats_get, -ENOTSUP);
1444         (*dev->dev_ops->stats_get)(dev, stats);
1445         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
1446         return 0;
1447 }
1448
1449 void
1450 rte_eth_stats_reset(uint8_t port_id)
1451 {
1452         struct rte_eth_dev *dev;
1453
1454         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1455         dev = &rte_eth_devices[port_id];
1456
1457         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
1458         (*dev->dev_ops->stats_reset)(dev);
1459         dev->data->rx_mbuf_alloc_failed = 0;
1460 }
1461
1462 /* retrieve ethdev extended statistics */
1463 int
1464 rte_eth_xstats_get(uint8_t port_id, struct rte_eth_xstats *xstats,
1465         unsigned n)
1466 {
1467         struct rte_eth_stats eth_stats;
1468         struct rte_eth_dev *dev;
1469         unsigned count = 0, i, q;
1470         signed xcount = 0;
1471         uint64_t val, *stats_ptr;
1472
1473         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
1474
1475         dev = &rte_eth_devices[port_id];
1476
1477         /* Return generic statistics */
1478         count = RTE_NB_STATS + (dev->data->nb_rx_queues * RTE_NB_RXQ_STATS) +
1479                 (dev->data->nb_tx_queues * RTE_NB_TXQ_STATS);
1480
1481         /* implemented by the driver */
1482         if (dev->dev_ops->xstats_get != NULL) {
1483                 /* Retrieve the xstats from the driver at the end of the
1484                  * xstats struct.
1485                  */
1486                 xcount = (*dev->dev_ops->xstats_get)(dev, &xstats[count],
1487                          (n > count) ? n - count : 0);
1488
1489                 if (xcount < 0)
1490                         return xcount;
1491         }
1492
1493         if (n < count + xcount)
1494                 return count + xcount;
1495
1496         /* now fill the xstats structure */
1497         count = 0;
1498         rte_eth_stats_get(port_id, &eth_stats);
1499
1500         /* global stats */
1501         for (i = 0; i < RTE_NB_STATS; i++) {
1502                 stats_ptr = RTE_PTR_ADD(&eth_stats,
1503                                         rte_stats_strings[i].offset);
1504                 val = *stats_ptr;
1505                 snprintf(xstats[count].name, sizeof(xstats[count].name),
1506                         "%s", rte_stats_strings[i].name);
1507                 xstats[count++].value = val;
1508         }
1509
1510         /* per-rxq stats */
1511         for (q = 0; q < dev->data->nb_rx_queues; q++) {
1512                 for (i = 0; i < RTE_NB_RXQ_STATS; i++) {
1513                         stats_ptr = RTE_PTR_ADD(&eth_stats,
1514                                         rte_rxq_stats_strings[i].offset +
1515                                         q * sizeof(uint64_t));
1516                         val = *stats_ptr;
1517                         snprintf(xstats[count].name, sizeof(xstats[count].name),
1518                                 "rx_q%u_%s", q,
1519                                 rte_rxq_stats_strings[i].name);
1520                         xstats[count++].value = val;
1521                 }
1522         }
1523
1524         /* per-txq stats */
1525         for (q = 0; q < dev->data->nb_tx_queues; q++) {
1526                 for (i = 0; i < RTE_NB_TXQ_STATS; i++) {
1527                         stats_ptr = RTE_PTR_ADD(&eth_stats,
1528                                         rte_txq_stats_strings[i].offset +
1529                                         q * sizeof(uint64_t));
1530                         val = *stats_ptr;
1531                         snprintf(xstats[count].name, sizeof(xstats[count].name),
1532                                 "tx_q%u_%s", q,
1533                                 rte_txq_stats_strings[i].name);
1534                         xstats[count++].value = val;
1535                 }
1536         }
1537
1538         return count + xcount;
1539 }
1540
1541 /* reset ethdev extended statistics */
1542 void
1543 rte_eth_xstats_reset(uint8_t port_id)
1544 {
1545         struct rte_eth_dev *dev;
1546
1547         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1548         dev = &rte_eth_devices[port_id];
1549
1550         /* implemented by the driver */
1551         if (dev->dev_ops->xstats_reset != NULL) {
1552                 (*dev->dev_ops->xstats_reset)(dev);
1553                 return;
1554         }
1555
1556         /* fallback to default */
1557         rte_eth_stats_reset(port_id);
1558 }
1559
1560 static int
1561 set_queue_stats_mapping(uint8_t port_id, uint16_t queue_id, uint8_t stat_idx,
1562                 uint8_t is_rx)
1563 {
1564         struct rte_eth_dev *dev;
1565
1566         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1567
1568         dev = &rte_eth_devices[port_id];
1569
1570         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_stats_mapping_set, -ENOTSUP);
1571         return (*dev->dev_ops->queue_stats_mapping_set)
1572                         (dev, queue_id, stat_idx, is_rx);
1573 }
1574
1575
1576 int
1577 rte_eth_dev_set_tx_queue_stats_mapping(uint8_t port_id, uint16_t tx_queue_id,
1578                 uint8_t stat_idx)
1579 {
1580         return set_queue_stats_mapping(port_id, tx_queue_id, stat_idx,
1581                         STAT_QMAP_TX);
1582 }
1583
1584
1585 int
1586 rte_eth_dev_set_rx_queue_stats_mapping(uint8_t port_id, uint16_t rx_queue_id,
1587                 uint8_t stat_idx)
1588 {
1589         return set_queue_stats_mapping(port_id, rx_queue_id, stat_idx,
1590                         STAT_QMAP_RX);
1591 }
1592
1593
1594 void
1595 rte_eth_dev_info_get(uint8_t port_id, struct rte_eth_dev_info *dev_info)
1596 {
1597         struct rte_eth_dev *dev;
1598         const struct rte_eth_desc_lim lim = {
1599                 .nb_max = UINT16_MAX,
1600                 .nb_min = 0,
1601                 .nb_align = 1,
1602         };
1603
1604         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1605         dev = &rte_eth_devices[port_id];
1606
1607         memset(dev_info, 0, sizeof(struct rte_eth_dev_info));
1608         dev_info->rx_desc_lim = lim;
1609         dev_info->tx_desc_lim = lim;
1610
1611         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
1612         (*dev->dev_ops->dev_infos_get)(dev, dev_info);
1613         dev_info->pci_dev = dev->pci_dev;
1614         dev_info->driver_name = dev->data->drv_name;
1615 }
1616
1617 void
1618 rte_eth_macaddr_get(uint8_t port_id, struct ether_addr *mac_addr)
1619 {
1620         struct rte_eth_dev *dev;
1621
1622         RTE_ETH_VALID_PORTID_OR_RET(port_id);
1623         dev = &rte_eth_devices[port_id];
1624         ether_addr_copy(&dev->data->mac_addrs[0], mac_addr);
1625 }
1626
1627
1628 int
1629 rte_eth_dev_get_mtu(uint8_t port_id, uint16_t *mtu)
1630 {
1631         struct rte_eth_dev *dev;
1632
1633         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1634
1635         dev = &rte_eth_devices[port_id];
1636         *mtu = dev->data->mtu;
1637         return 0;
1638 }
1639
1640 int
1641 rte_eth_dev_set_mtu(uint8_t port_id, uint16_t mtu)
1642 {
1643         int ret;
1644         struct rte_eth_dev *dev;
1645
1646         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1647         dev = &rte_eth_devices[port_id];
1648         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mtu_set, -ENOTSUP);
1649
1650         ret = (*dev->dev_ops->mtu_set)(dev, mtu);
1651         if (!ret)
1652                 dev->data->mtu = mtu;
1653
1654         return ret;
1655 }
1656
1657 int
1658 rte_eth_dev_vlan_filter(uint8_t port_id, uint16_t vlan_id, int on)
1659 {
1660         struct rte_eth_dev *dev;
1661
1662         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1663         dev = &rte_eth_devices[port_id];
1664         if (!(dev->data->dev_conf.rxmode.hw_vlan_filter)) {
1665                 RTE_PMD_DEBUG_TRACE("port %d: vlan-filtering disabled\n", port_id);
1666                 return -ENOSYS;
1667         }
1668
1669         if (vlan_id > 4095) {
1670                 RTE_PMD_DEBUG_TRACE("(port_id=%d) invalid vlan_id=%u > 4095\n",
1671                                 port_id, (unsigned) vlan_id);
1672                 return -EINVAL;
1673         }
1674         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_filter_set, -ENOTSUP);
1675
1676         return (*dev->dev_ops->vlan_filter_set)(dev, vlan_id, on);
1677 }
1678
1679 int
1680 rte_eth_dev_set_vlan_strip_on_queue(uint8_t port_id, uint16_t rx_queue_id, int on)
1681 {
1682         struct rte_eth_dev *dev;
1683
1684         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1685         dev = &rte_eth_devices[port_id];
1686         if (rx_queue_id >= dev->data->nb_rx_queues) {
1687                 RTE_PMD_DEBUG_TRACE("Invalid rx_queue_id=%d\n", port_id);
1688                 return -EINVAL;
1689         }
1690
1691         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_strip_queue_set, -ENOTSUP);
1692         (*dev->dev_ops->vlan_strip_queue_set)(dev, rx_queue_id, on);
1693
1694         return 0;
1695 }
1696
1697 int
1698 rte_eth_dev_set_vlan_ether_type(uint8_t port_id, uint16_t tpid)
1699 {
1700         struct rte_eth_dev *dev;
1701
1702         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1703         dev = &rte_eth_devices[port_id];
1704         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_tpid_set, -ENOTSUP);
1705         (*dev->dev_ops->vlan_tpid_set)(dev, tpid);
1706
1707         return 0;
1708 }
1709
1710 int
1711 rte_eth_dev_set_vlan_offload(uint8_t port_id, int offload_mask)
1712 {
1713         struct rte_eth_dev *dev;
1714         int ret = 0;
1715         int mask = 0;
1716         int cur, org = 0;
1717
1718         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1719         dev = &rte_eth_devices[port_id];
1720
1721         /*check which option changed by application*/
1722         cur = !!(offload_mask & ETH_VLAN_STRIP_OFFLOAD);
1723         org = !!(dev->data->dev_conf.rxmode.hw_vlan_strip);
1724         if (cur != org) {
1725                 dev->data->dev_conf.rxmode.hw_vlan_strip = (uint8_t)cur;
1726                 mask |= ETH_VLAN_STRIP_MASK;
1727         }
1728
1729         cur = !!(offload_mask & ETH_VLAN_FILTER_OFFLOAD);
1730         org = !!(dev->data->dev_conf.rxmode.hw_vlan_filter);
1731         if (cur != org) {
1732                 dev->data->dev_conf.rxmode.hw_vlan_filter = (uint8_t)cur;
1733                 mask |= ETH_VLAN_FILTER_MASK;
1734         }
1735
1736         cur = !!(offload_mask & ETH_VLAN_EXTEND_OFFLOAD);
1737         org = !!(dev->data->dev_conf.rxmode.hw_vlan_extend);
1738         if (cur != org) {
1739                 dev->data->dev_conf.rxmode.hw_vlan_extend = (uint8_t)cur;
1740                 mask |= ETH_VLAN_EXTEND_MASK;
1741         }
1742
1743         /*no change*/
1744         if (mask == 0)
1745                 return ret;
1746
1747         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_offload_set, -ENOTSUP);
1748         (*dev->dev_ops->vlan_offload_set)(dev, mask);
1749
1750         return ret;
1751 }
1752
1753 int
1754 rte_eth_dev_get_vlan_offload(uint8_t port_id)
1755 {
1756         struct rte_eth_dev *dev;
1757         int ret = 0;
1758
1759         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1760         dev = &rte_eth_devices[port_id];
1761
1762         if (dev->data->dev_conf.rxmode.hw_vlan_strip)
1763                 ret |= ETH_VLAN_STRIP_OFFLOAD;
1764
1765         if (dev->data->dev_conf.rxmode.hw_vlan_filter)
1766                 ret |= ETH_VLAN_FILTER_OFFLOAD;
1767
1768         if (dev->data->dev_conf.rxmode.hw_vlan_extend)
1769                 ret |= ETH_VLAN_EXTEND_OFFLOAD;
1770
1771         return ret;
1772 }
1773
1774 int
1775 rte_eth_dev_set_vlan_pvid(uint8_t port_id, uint16_t pvid, int on)
1776 {
1777         struct rte_eth_dev *dev;
1778
1779         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1780         dev = &rte_eth_devices[port_id];
1781         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_pvid_set, -ENOTSUP);
1782         (*dev->dev_ops->vlan_pvid_set)(dev, pvid, on);
1783
1784         return 0;
1785 }
1786
1787 int
1788 rte_eth_dev_flow_ctrl_get(uint8_t port_id, struct rte_eth_fc_conf *fc_conf)
1789 {
1790         struct rte_eth_dev *dev;
1791
1792         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1793         dev = &rte_eth_devices[port_id];
1794         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_get, -ENOTSUP);
1795         memset(fc_conf, 0, sizeof(*fc_conf));
1796         return (*dev->dev_ops->flow_ctrl_get)(dev, fc_conf);
1797 }
1798
1799 int
1800 rte_eth_dev_flow_ctrl_set(uint8_t port_id, struct rte_eth_fc_conf *fc_conf)
1801 {
1802         struct rte_eth_dev *dev;
1803
1804         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1805         if ((fc_conf->send_xon != 0) && (fc_conf->send_xon != 1)) {
1806                 RTE_PMD_DEBUG_TRACE("Invalid send_xon, only 0/1 allowed\n");
1807                 return -EINVAL;
1808         }
1809
1810         dev = &rte_eth_devices[port_id];
1811         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_set, -ENOTSUP);
1812         return (*dev->dev_ops->flow_ctrl_set)(dev, fc_conf);
1813 }
1814
1815 int
1816 rte_eth_dev_priority_flow_ctrl_set(uint8_t port_id, struct rte_eth_pfc_conf *pfc_conf)
1817 {
1818         struct rte_eth_dev *dev;
1819
1820         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1821         if (pfc_conf->priority > (ETH_DCB_NUM_USER_PRIORITIES - 1)) {
1822                 RTE_PMD_DEBUG_TRACE("Invalid priority, only 0-7 allowed\n");
1823                 return -EINVAL;
1824         }
1825
1826         dev = &rte_eth_devices[port_id];
1827         /* High water, low water validation are device specific */
1828         if  (*dev->dev_ops->priority_flow_ctrl_set)
1829                 return (*dev->dev_ops->priority_flow_ctrl_set)(dev, pfc_conf);
1830         return -ENOTSUP;
1831 }
1832
1833 static int
1834 rte_eth_check_reta_mask(struct rte_eth_rss_reta_entry64 *reta_conf,
1835                         uint16_t reta_size)
1836 {
1837         uint16_t i, num;
1838
1839         if (!reta_conf)
1840                 return -EINVAL;
1841
1842         if (reta_size != RTE_ALIGN(reta_size, RTE_RETA_GROUP_SIZE)) {
1843                 RTE_PMD_DEBUG_TRACE("Invalid reta size, should be %u aligned\n",
1844                                                         RTE_RETA_GROUP_SIZE);
1845                 return -EINVAL;
1846         }
1847
1848         num = reta_size / RTE_RETA_GROUP_SIZE;
1849         for (i = 0; i < num; i++) {
1850                 if (reta_conf[i].mask)
1851                         return 0;
1852         }
1853
1854         return -EINVAL;
1855 }
1856
1857 static int
1858 rte_eth_check_reta_entry(struct rte_eth_rss_reta_entry64 *reta_conf,
1859                          uint16_t reta_size,
1860                          uint8_t max_rxq)
1861 {
1862         uint16_t i, idx, shift;
1863
1864         if (!reta_conf)
1865                 return -EINVAL;
1866
1867         if (max_rxq == 0) {
1868                 RTE_PMD_DEBUG_TRACE("No receive queue is available\n");
1869                 return -EINVAL;
1870         }
1871
1872         for (i = 0; i < reta_size; i++) {
1873                 idx = i / RTE_RETA_GROUP_SIZE;
1874                 shift = i % RTE_RETA_GROUP_SIZE;
1875                 if ((reta_conf[idx].mask & (1ULL << shift)) &&
1876                         (reta_conf[idx].reta[shift] >= max_rxq)) {
1877                         RTE_PMD_DEBUG_TRACE("reta_conf[%u]->reta[%u]: %u exceeds "
1878                                 "the maximum rxq index: %u\n", idx, shift,
1879                                 reta_conf[idx].reta[shift], max_rxq);
1880                         return -EINVAL;
1881                 }
1882         }
1883
1884         return 0;
1885 }
1886
1887 int
1888 rte_eth_dev_rss_reta_update(uint8_t port_id,
1889                             struct rte_eth_rss_reta_entry64 *reta_conf,
1890                             uint16_t reta_size)
1891 {
1892         struct rte_eth_dev *dev;
1893         int ret;
1894
1895         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1896         /* Check mask bits */
1897         ret = rte_eth_check_reta_mask(reta_conf, reta_size);
1898         if (ret < 0)
1899                 return ret;
1900
1901         dev = &rte_eth_devices[port_id];
1902
1903         /* Check entry value */
1904         ret = rte_eth_check_reta_entry(reta_conf, reta_size,
1905                                 dev->data->nb_rx_queues);
1906         if (ret < 0)
1907                 return ret;
1908
1909         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->reta_update, -ENOTSUP);
1910         return (*dev->dev_ops->reta_update)(dev, reta_conf, reta_size);
1911 }
1912
1913 int
1914 rte_eth_dev_rss_reta_query(uint8_t port_id,
1915                            struct rte_eth_rss_reta_entry64 *reta_conf,
1916                            uint16_t reta_size)
1917 {
1918         struct rte_eth_dev *dev;
1919         int ret;
1920
1921         if (port_id >= nb_ports) {
1922                 RTE_PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1923                 return -ENODEV;
1924         }
1925
1926         /* Check mask bits */
1927         ret = rte_eth_check_reta_mask(reta_conf, reta_size);
1928         if (ret < 0)
1929                 return ret;
1930
1931         dev = &rte_eth_devices[port_id];
1932         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->reta_query, -ENOTSUP);
1933         return (*dev->dev_ops->reta_query)(dev, reta_conf, reta_size);
1934 }
1935
1936 int
1937 rte_eth_dev_rss_hash_update(uint8_t port_id, struct rte_eth_rss_conf *rss_conf)
1938 {
1939         struct rte_eth_dev *dev;
1940         uint16_t rss_hash_protos;
1941
1942         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1943         rss_hash_protos = rss_conf->rss_hf;
1944         if ((rss_hash_protos != 0) &&
1945             ((rss_hash_protos & ETH_RSS_PROTO_MASK) == 0)) {
1946                 RTE_PMD_DEBUG_TRACE("Invalid rss_hash_protos=0x%x\n",
1947                                 rss_hash_protos);
1948                 return -EINVAL;
1949         }
1950         dev = &rte_eth_devices[port_id];
1951         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rss_hash_update, -ENOTSUP);
1952         return (*dev->dev_ops->rss_hash_update)(dev, rss_conf);
1953 }
1954
1955 int
1956 rte_eth_dev_rss_hash_conf_get(uint8_t port_id,
1957                               struct rte_eth_rss_conf *rss_conf)
1958 {
1959         struct rte_eth_dev *dev;
1960
1961         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1962         dev = &rte_eth_devices[port_id];
1963         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rss_hash_conf_get, -ENOTSUP);
1964         return (*dev->dev_ops->rss_hash_conf_get)(dev, rss_conf);
1965 }
1966
1967 int
1968 rte_eth_dev_udp_tunnel_add(uint8_t port_id,
1969                            struct rte_eth_udp_tunnel *udp_tunnel)
1970 {
1971         struct rte_eth_dev *dev;
1972
1973         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1974         if (udp_tunnel == NULL) {
1975                 RTE_PMD_DEBUG_TRACE("Invalid udp_tunnel parameter\n");
1976                 return -EINVAL;
1977         }
1978
1979         if (udp_tunnel->prot_type >= RTE_TUNNEL_TYPE_MAX) {
1980                 RTE_PMD_DEBUG_TRACE("Invalid tunnel type\n");
1981                 return -EINVAL;
1982         }
1983
1984         dev = &rte_eth_devices[port_id];
1985         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->udp_tunnel_add, -ENOTSUP);
1986         return (*dev->dev_ops->udp_tunnel_add)(dev, udp_tunnel);
1987 }
1988
1989 int
1990 rte_eth_dev_udp_tunnel_delete(uint8_t port_id,
1991                               struct rte_eth_udp_tunnel *udp_tunnel)
1992 {
1993         struct rte_eth_dev *dev;
1994
1995         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
1996         dev = &rte_eth_devices[port_id];
1997
1998         if (udp_tunnel == NULL) {
1999                 RTE_PMD_DEBUG_TRACE("Invalid udp_tunnel parameter\n");
2000                 return -EINVAL;
2001         }
2002
2003         if (udp_tunnel->prot_type >= RTE_TUNNEL_TYPE_MAX) {
2004                 RTE_PMD_DEBUG_TRACE("Invalid tunnel type\n");
2005                 return -EINVAL;
2006         }
2007
2008         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->udp_tunnel_del, -ENOTSUP);
2009         return (*dev->dev_ops->udp_tunnel_del)(dev, udp_tunnel);
2010 }
2011
2012 int
2013 rte_eth_led_on(uint8_t port_id)
2014 {
2015         struct rte_eth_dev *dev;
2016
2017         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2018         dev = &rte_eth_devices[port_id];
2019         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_on, -ENOTSUP);
2020         return (*dev->dev_ops->dev_led_on)(dev);
2021 }
2022
2023 int
2024 rte_eth_led_off(uint8_t port_id)
2025 {
2026         struct rte_eth_dev *dev;
2027
2028         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2029         dev = &rte_eth_devices[port_id];
2030         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_off, -ENOTSUP);
2031         return (*dev->dev_ops->dev_led_off)(dev);
2032 }
2033
2034 /*
2035  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
2036  * an empty spot.
2037  */
2038 static int
2039 get_mac_addr_index(uint8_t port_id, const struct ether_addr *addr)
2040 {
2041         struct rte_eth_dev_info dev_info;
2042         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
2043         unsigned i;
2044
2045         rte_eth_dev_info_get(port_id, &dev_info);
2046
2047         for (i = 0; i < dev_info.max_mac_addrs; i++)
2048                 if (memcmp(addr, &dev->data->mac_addrs[i], ETHER_ADDR_LEN) == 0)
2049                         return i;
2050
2051         return -1;
2052 }
2053
2054 static const struct ether_addr null_mac_addr;
2055
2056 int
2057 rte_eth_dev_mac_addr_add(uint8_t port_id, struct ether_addr *addr,
2058                         uint32_t pool)
2059 {
2060         struct rte_eth_dev *dev;
2061         int index;
2062         uint64_t pool_mask;
2063
2064         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2065         dev = &rte_eth_devices[port_id];
2066         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_add, -ENOTSUP);
2067
2068         if (is_zero_ether_addr(addr)) {
2069                 RTE_PMD_DEBUG_TRACE("port %d: Cannot add NULL MAC address\n",
2070                         port_id);
2071                 return -EINVAL;
2072         }
2073         if (pool >= ETH_64_POOLS) {
2074                 RTE_PMD_DEBUG_TRACE("pool id must be 0-%d\n", ETH_64_POOLS - 1);
2075                 return -EINVAL;
2076         }
2077
2078         index = get_mac_addr_index(port_id, addr);
2079         if (index < 0) {
2080                 index = get_mac_addr_index(port_id, &null_mac_addr);
2081                 if (index < 0) {
2082                         RTE_PMD_DEBUG_TRACE("port %d: MAC address array full\n",
2083                                 port_id);
2084                         return -ENOSPC;
2085                 }
2086         } else {
2087                 pool_mask = dev->data->mac_pool_sel[index];
2088
2089                 /* Check if both MAC address and pool is already there, and do nothing */
2090                 if (pool_mask & (1ULL << pool))
2091                         return 0;
2092         }
2093
2094         /* Update NIC */
2095         (*dev->dev_ops->mac_addr_add)(dev, addr, index, pool);
2096
2097         /* Update address in NIC data structure */
2098         ether_addr_copy(addr, &dev->data->mac_addrs[index]);
2099
2100         /* Update pool bitmap in NIC data structure */
2101         dev->data->mac_pool_sel[index] |= (1ULL << pool);
2102
2103         return 0;
2104 }
2105
2106 int
2107 rte_eth_dev_mac_addr_remove(uint8_t port_id, struct ether_addr *addr)
2108 {
2109         struct rte_eth_dev *dev;
2110         int index;
2111
2112         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2113         dev = &rte_eth_devices[port_id];
2114         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_remove, -ENOTSUP);
2115
2116         index = get_mac_addr_index(port_id, addr);
2117         if (index == 0) {
2118                 RTE_PMD_DEBUG_TRACE("port %d: Cannot remove default MAC address\n", port_id);
2119                 return -EADDRINUSE;
2120         } else if (index < 0)
2121                 return 0;  /* Do nothing if address wasn't found */
2122
2123         /* Update NIC */
2124         (*dev->dev_ops->mac_addr_remove)(dev, index);
2125
2126         /* Update address in NIC data structure */
2127         ether_addr_copy(&null_mac_addr, &dev->data->mac_addrs[index]);
2128
2129         /* reset pool bitmap */
2130         dev->data->mac_pool_sel[index] = 0;
2131
2132         return 0;
2133 }
2134
2135 int
2136 rte_eth_dev_default_mac_addr_set(uint8_t port_id, struct ether_addr *addr)
2137 {
2138         struct rte_eth_dev *dev;
2139
2140         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2141
2142         if (!is_valid_assigned_ether_addr(addr))
2143                 return -EINVAL;
2144
2145         dev = &rte_eth_devices[port_id];
2146         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_set, -ENOTSUP);
2147
2148         /* Update default address in NIC data structure */
2149         ether_addr_copy(addr, &dev->data->mac_addrs[0]);
2150
2151         (*dev->dev_ops->mac_addr_set)(dev, addr);
2152
2153         return 0;
2154 }
2155
2156 int
2157 rte_eth_dev_set_vf_rxmode(uint8_t port_id,  uint16_t vf,
2158                                 uint16_t rx_mode, uint8_t on)
2159 {
2160         uint16_t num_vfs;
2161         struct rte_eth_dev *dev;
2162         struct rte_eth_dev_info dev_info;
2163
2164         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2165
2166         dev = &rte_eth_devices[port_id];
2167         rte_eth_dev_info_get(port_id, &dev_info);
2168
2169         num_vfs = dev_info.max_vfs;
2170         if (vf > num_vfs) {
2171                 RTE_PMD_DEBUG_TRACE("set VF RX mode:invalid VF id %d\n", vf);
2172                 return -EINVAL;
2173         }
2174
2175         if (rx_mode == 0) {
2176                 RTE_PMD_DEBUG_TRACE("set VF RX mode:mode mask ca not be zero\n");
2177                 return -EINVAL;
2178         }
2179         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_vf_rx_mode, -ENOTSUP);
2180         return (*dev->dev_ops->set_vf_rx_mode)(dev, vf, rx_mode, on);
2181 }
2182
2183 /*
2184  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
2185  * an empty spot.
2186  */
2187 static int
2188 get_hash_mac_addr_index(uint8_t port_id, const struct ether_addr *addr)
2189 {
2190         struct rte_eth_dev_info dev_info;
2191         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
2192         unsigned i;
2193
2194         rte_eth_dev_info_get(port_id, &dev_info);
2195         if (!dev->data->hash_mac_addrs)
2196                 return -1;
2197
2198         for (i = 0; i < dev_info.max_hash_mac_addrs; i++)
2199                 if (memcmp(addr, &dev->data->hash_mac_addrs[i],
2200                         ETHER_ADDR_LEN) == 0)
2201                         return i;
2202
2203         return -1;
2204 }
2205
2206 int
2207 rte_eth_dev_uc_hash_table_set(uint8_t port_id, struct ether_addr *addr,
2208                                 uint8_t on)
2209 {
2210         int index;
2211         int ret;
2212         struct rte_eth_dev *dev;
2213
2214         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2215
2216         dev = &rte_eth_devices[port_id];
2217         if (is_zero_ether_addr(addr)) {
2218                 RTE_PMD_DEBUG_TRACE("port %d: Cannot add NULL MAC address\n",
2219                         port_id);
2220                 return -EINVAL;
2221         }
2222
2223         index = get_hash_mac_addr_index(port_id, addr);
2224         /* Check if it's already there, and do nothing */
2225         if ((index >= 0) && (on))
2226                 return 0;
2227
2228         if (index < 0) {
2229                 if (!on) {
2230                         RTE_PMD_DEBUG_TRACE("port %d: the MAC address was not "
2231                                 "set in UTA\n", port_id);
2232                         return -EINVAL;
2233                 }
2234
2235                 index = get_hash_mac_addr_index(port_id, &null_mac_addr);
2236                 if (index < 0) {
2237                         RTE_PMD_DEBUG_TRACE("port %d: MAC address array full\n",
2238                                         port_id);
2239                         return -ENOSPC;
2240                 }
2241         }
2242
2243         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->uc_hash_table_set, -ENOTSUP);
2244         ret = (*dev->dev_ops->uc_hash_table_set)(dev, addr, on);
2245         if (ret == 0) {
2246                 /* Update address in NIC data structure */
2247                 if (on)
2248                         ether_addr_copy(addr,
2249                                         &dev->data->hash_mac_addrs[index]);
2250                 else
2251                         ether_addr_copy(&null_mac_addr,
2252                                         &dev->data->hash_mac_addrs[index]);
2253         }
2254
2255         return ret;
2256 }
2257
2258 int
2259 rte_eth_dev_uc_all_hash_table_set(uint8_t port_id, uint8_t on)
2260 {
2261         struct rte_eth_dev *dev;
2262
2263         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2264
2265         dev = &rte_eth_devices[port_id];
2266
2267         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->uc_all_hash_table_set, -ENOTSUP);
2268         return (*dev->dev_ops->uc_all_hash_table_set)(dev, on);
2269 }
2270
2271 int
2272 rte_eth_dev_set_vf_rx(uint8_t port_id, uint16_t vf, uint8_t on)
2273 {
2274         uint16_t num_vfs;
2275         struct rte_eth_dev *dev;
2276         struct rte_eth_dev_info dev_info;
2277
2278         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2279
2280         dev = &rte_eth_devices[port_id];
2281         rte_eth_dev_info_get(port_id, &dev_info);
2282
2283         num_vfs = dev_info.max_vfs;
2284         if (vf > num_vfs) {
2285                 RTE_PMD_DEBUG_TRACE("port %d: invalid vf id\n", port_id);
2286                 return -EINVAL;
2287         }
2288
2289         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_vf_rx, -ENOTSUP);
2290         return (*dev->dev_ops->set_vf_rx)(dev, vf, on);
2291 }
2292
2293 int
2294 rte_eth_dev_set_vf_tx(uint8_t port_id, uint16_t vf, uint8_t on)
2295 {
2296         uint16_t num_vfs;
2297         struct rte_eth_dev *dev;
2298         struct rte_eth_dev_info dev_info;
2299
2300         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2301
2302         dev = &rte_eth_devices[port_id];
2303         rte_eth_dev_info_get(port_id, &dev_info);
2304
2305         num_vfs = dev_info.max_vfs;
2306         if (vf > num_vfs) {
2307                 RTE_PMD_DEBUG_TRACE("set pool tx:invalid pool id=%d\n", vf);
2308                 return -EINVAL;
2309         }
2310
2311         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_vf_tx, -ENOTSUP);
2312         return (*dev->dev_ops->set_vf_tx)(dev, vf, on);
2313 }
2314
2315 int
2316 rte_eth_dev_set_vf_vlan_filter(uint8_t port_id, uint16_t vlan_id,
2317                                uint64_t vf_mask, uint8_t vlan_on)
2318 {
2319         struct rte_eth_dev *dev;
2320
2321         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2322
2323         dev = &rte_eth_devices[port_id];
2324
2325         if (vlan_id > ETHER_MAX_VLAN_ID) {
2326                 RTE_PMD_DEBUG_TRACE("VF VLAN filter:invalid VLAN id=%d\n",
2327                         vlan_id);
2328                 return -EINVAL;
2329         }
2330
2331         if (vf_mask == 0) {
2332                 RTE_PMD_DEBUG_TRACE("VF VLAN filter:pool_mask can not be 0\n");
2333                 return -EINVAL;
2334         }
2335
2336         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_vf_vlan_filter, -ENOTSUP);
2337         return (*dev->dev_ops->set_vf_vlan_filter)(dev, vlan_id,
2338                                                    vf_mask, vlan_on);
2339 }
2340
2341 int rte_eth_set_queue_rate_limit(uint8_t port_id, uint16_t queue_idx,
2342                                         uint16_t tx_rate)
2343 {
2344         struct rte_eth_dev *dev;
2345         struct rte_eth_dev_info dev_info;
2346         struct rte_eth_link link;
2347
2348         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2349
2350         dev = &rte_eth_devices[port_id];
2351         rte_eth_dev_info_get(port_id, &dev_info);
2352         link = dev->data->dev_link;
2353
2354         if (queue_idx > dev_info.max_tx_queues) {
2355                 RTE_PMD_DEBUG_TRACE("set queue rate limit:port %d: "
2356                                 "invalid queue id=%d\n", port_id, queue_idx);
2357                 return -EINVAL;
2358         }
2359
2360         if (tx_rate > link.link_speed) {
2361                 RTE_PMD_DEBUG_TRACE("set queue rate limit:invalid tx_rate=%d, "
2362                                 "bigger than link speed= %d\n",
2363                         tx_rate, link.link_speed);
2364                 return -EINVAL;
2365         }
2366
2367         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_queue_rate_limit, -ENOTSUP);
2368         return (*dev->dev_ops->set_queue_rate_limit)(dev, queue_idx, tx_rate);
2369 }
2370
2371 int rte_eth_set_vf_rate_limit(uint8_t port_id, uint16_t vf, uint16_t tx_rate,
2372                                 uint64_t q_msk)
2373 {
2374         struct rte_eth_dev *dev;
2375         struct rte_eth_dev_info dev_info;
2376         struct rte_eth_link link;
2377
2378         if (q_msk == 0)
2379                 return 0;
2380
2381         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2382
2383         dev = &rte_eth_devices[port_id];
2384         rte_eth_dev_info_get(port_id, &dev_info);
2385         link = dev->data->dev_link;
2386
2387         if (vf > dev_info.max_vfs) {
2388                 RTE_PMD_DEBUG_TRACE("set VF rate limit:port %d: "
2389                                 "invalid vf id=%d\n", port_id, vf);
2390                 return -EINVAL;
2391         }
2392
2393         if (tx_rate > link.link_speed) {
2394                 RTE_PMD_DEBUG_TRACE("set VF rate limit:invalid tx_rate=%d, "
2395                                 "bigger than link speed= %d\n",
2396                                 tx_rate, link.link_speed);
2397                 return -EINVAL;
2398         }
2399
2400         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_vf_rate_limit, -ENOTSUP);
2401         return (*dev->dev_ops->set_vf_rate_limit)(dev, vf, tx_rate, q_msk);
2402 }
2403
2404 int
2405 rte_eth_mirror_rule_set(uint8_t port_id,
2406                         struct rte_eth_mirror_conf *mirror_conf,
2407                         uint8_t rule_id, uint8_t on)
2408 {
2409         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
2410
2411         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2412         if (mirror_conf->rule_type == 0) {
2413                 RTE_PMD_DEBUG_TRACE("mirror rule type can not be 0.\n");
2414                 return -EINVAL;
2415         }
2416
2417         if (mirror_conf->dst_pool >= ETH_64_POOLS) {
2418                 RTE_PMD_DEBUG_TRACE("Invalid dst pool, pool id must be 0-%d\n",
2419                                 ETH_64_POOLS - 1);
2420                 return -EINVAL;
2421         }
2422
2423         if ((mirror_conf->rule_type & (ETH_MIRROR_VIRTUAL_POOL_UP |
2424              ETH_MIRROR_VIRTUAL_POOL_DOWN)) &&
2425             (mirror_conf->pool_mask == 0)) {
2426                 RTE_PMD_DEBUG_TRACE("Invalid mirror pool, pool mask can not be 0.\n");
2427                 return -EINVAL;
2428         }
2429
2430         if ((mirror_conf->rule_type & ETH_MIRROR_VLAN) &&
2431             mirror_conf->vlan.vlan_mask == 0) {
2432                 RTE_PMD_DEBUG_TRACE("Invalid vlan mask, vlan mask can not be 0.\n");
2433                 return -EINVAL;
2434         }
2435
2436         dev = &rte_eth_devices[port_id];
2437         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mirror_rule_set, -ENOTSUP);
2438
2439         return (*dev->dev_ops->mirror_rule_set)(dev, mirror_conf, rule_id, on);
2440 }
2441
2442 int
2443 rte_eth_mirror_rule_reset(uint8_t port_id, uint8_t rule_id)
2444 {
2445         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
2446
2447         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2448
2449         dev = &rte_eth_devices[port_id];
2450         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mirror_rule_reset, -ENOTSUP);
2451
2452         return (*dev->dev_ops->mirror_rule_reset)(dev, rule_id);
2453 }
2454
2455 int
2456 rte_eth_dev_callback_register(uint8_t port_id,
2457                         enum rte_eth_event_type event,
2458                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
2459 {
2460         struct rte_eth_dev *dev;
2461         struct rte_eth_dev_callback *user_cb;
2462
2463         if (!cb_fn)
2464                 return -EINVAL;
2465
2466         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
2467
2468         dev = &rte_eth_devices[port_id];
2469         rte_spinlock_lock(&rte_eth_dev_cb_lock);
2470
2471         TAILQ_FOREACH(user_cb, &(dev->link_intr_cbs), next) {
2472                 if (user_cb->cb_fn == cb_fn &&
2473                         user_cb->cb_arg == cb_arg &&
2474                         user_cb->event == event) {
2475                         break;
2476                 }
2477         }
2478
2479         /* create a new callback. */
2480         if (user_cb == NULL)
2481                 user_cb = rte_zmalloc("INTR_USER_CALLBACK",
2482                                       sizeof(struct rte_eth_dev_callback), 0);
2483         if (user_cb != NULL) {
2484                 user_cb->cb_fn = cb_fn;
2485                 user_cb->cb_arg = cb_arg;
2486                 user_cb->event = event;
2487                 TAILQ_INSERT_TAIL(&(dev->link_intr_cbs), user_cb, next);
2488         }
2489
2490         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2491         return (user_cb == NULL) ? -ENOMEM : 0;
2492 }
2493
2494 int
2495 rte_eth_dev_callback_unregister(uint8_t port_id,
2496                         enum rte_eth_event_type event,
2497                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
2498 {
2499         int ret;
2500         struct rte_eth_dev *dev;
2501         struct rte_eth_dev_callback *cb, *next;
2502
2503         if (!cb_fn)
2504                 return -EINVAL;
2505
2506         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
2507
2508         dev = &rte_eth_devices[port_id];
2509         rte_spinlock_lock(&rte_eth_dev_cb_lock);
2510
2511         ret = 0;
2512         for (cb = TAILQ_FIRST(&dev->link_intr_cbs); cb != NULL; cb = next) {
2513
2514                 next = TAILQ_NEXT(cb, next);
2515
2516                 if (cb->cb_fn != cb_fn || cb->event != event ||
2517                                 (cb->cb_arg != (void *)-1 &&
2518                                 cb->cb_arg != cb_arg))
2519                         continue;
2520
2521                 /*
2522                  * if this callback is not executing right now,
2523                  * then remove it.
2524                  */
2525                 if (cb->active == 0) {
2526                         TAILQ_REMOVE(&(dev->link_intr_cbs), cb, next);
2527                         rte_free(cb);
2528                 } else {
2529                         ret = -EAGAIN;
2530                 }
2531         }
2532
2533         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2534         return ret;
2535 }
2536
2537 void
2538 _rte_eth_dev_callback_process(struct rte_eth_dev *dev,
2539         enum rte_eth_event_type event)
2540 {
2541         struct rte_eth_dev_callback *cb_lst;
2542         struct rte_eth_dev_callback dev_cb;
2543
2544         rte_spinlock_lock(&rte_eth_dev_cb_lock);
2545         TAILQ_FOREACH(cb_lst, &(dev->link_intr_cbs), next) {
2546                 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
2547                         continue;
2548                 dev_cb = *cb_lst;
2549                 cb_lst->active = 1;
2550                 rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2551                 dev_cb.cb_fn(dev->data->port_id, dev_cb.event,
2552                                                 dev_cb.cb_arg);
2553                 rte_spinlock_lock(&rte_eth_dev_cb_lock);
2554                 cb_lst->active = 0;
2555         }
2556         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2557 }
2558
2559 int
2560 rte_eth_dev_rx_intr_ctl(uint8_t port_id, int epfd, int op, void *data)
2561 {
2562         uint32_t vec;
2563         struct rte_eth_dev *dev;
2564         struct rte_intr_handle *intr_handle;
2565         uint16_t qid;
2566         int rc;
2567
2568         if (!rte_eth_dev_is_valid_port(port_id)) {
2569                 RTE_PMD_DEBUG_TRACE("Invalid port_id=%u\n", port_id);
2570                 return -ENODEV;
2571         }
2572
2573         dev = &rte_eth_devices[port_id];
2574         intr_handle = &dev->pci_dev->intr_handle;
2575         if (!intr_handle->intr_vec) {
2576                 RTE_PMD_DEBUG_TRACE("RX Intr vector unset\n");
2577                 return -EPERM;
2578         }
2579
2580         for (qid = 0; qid < dev->data->nb_rx_queues; qid++) {
2581                 vec = intr_handle->intr_vec[qid];
2582                 rc = rte_intr_rx_ctl(intr_handle, epfd, op, vec, data);
2583                 if (rc && rc != -EEXIST) {
2584                         RTE_PMD_DEBUG_TRACE("p %u q %u rx ctl error"
2585                                         " op %d epfd %d vec %u\n",
2586                                         port_id, qid, op, epfd, vec);
2587                 }
2588         }
2589
2590         return 0;
2591 }
2592
2593 const struct rte_memzone *
2594 rte_eth_dma_zone_reserve(const struct rte_eth_dev *dev, const char *ring_name,
2595                          uint16_t queue_id, size_t size, unsigned align,
2596                          int socket_id)
2597 {
2598         char z_name[RTE_MEMZONE_NAMESIZE];
2599         const struct rte_memzone *mz;
2600
2601         snprintf(z_name, sizeof(z_name), "%s_%s_%d_%d",
2602                  dev->driver->pci_drv.name, ring_name,
2603                  dev->data->port_id, queue_id);
2604
2605         mz = rte_memzone_lookup(z_name);
2606         if (mz)
2607                 return mz;
2608
2609         if (rte_xen_dom0_supported())
2610                 return rte_memzone_reserve_bounded(z_name, size, socket_id,
2611                                                    0, align, RTE_PGSIZE_2M);
2612         else
2613                 return rte_memzone_reserve_aligned(z_name, size, socket_id,
2614                                                    0, align);
2615 }
2616
2617 int
2618 rte_eth_dev_rx_intr_ctl_q(uint8_t port_id, uint16_t queue_id,
2619                           int epfd, int op, void *data)
2620 {
2621         uint32_t vec;
2622         struct rte_eth_dev *dev;
2623         struct rte_intr_handle *intr_handle;
2624         int rc;
2625
2626         if (!rte_eth_dev_is_valid_port(port_id)) {
2627                 RTE_PMD_DEBUG_TRACE("Invalid port_id=%u\n", port_id);
2628                 return -ENODEV;
2629         }
2630
2631         dev = &rte_eth_devices[port_id];
2632         if (queue_id >= dev->data->nb_rx_queues) {
2633                 RTE_PMD_DEBUG_TRACE("Invalid RX queue_id=%u\n", queue_id);
2634                 return -EINVAL;
2635         }
2636
2637         intr_handle = &dev->pci_dev->intr_handle;
2638         if (!intr_handle->intr_vec) {
2639                 RTE_PMD_DEBUG_TRACE("RX Intr vector unset\n");
2640                 return -EPERM;
2641         }
2642
2643         vec = intr_handle->intr_vec[queue_id];
2644         rc = rte_intr_rx_ctl(intr_handle, epfd, op, vec, data);
2645         if (rc && rc != -EEXIST) {
2646                 RTE_PMD_DEBUG_TRACE("p %u q %u rx ctl error"
2647                                 " op %d epfd %d vec %u\n",
2648                                 port_id, queue_id, op, epfd, vec);
2649                 return rc;
2650         }
2651
2652         return 0;
2653 }
2654
2655 int
2656 rte_eth_dev_rx_intr_enable(uint8_t port_id,
2657                            uint16_t queue_id)
2658 {
2659         struct rte_eth_dev *dev;
2660
2661         if (!rte_eth_dev_is_valid_port(port_id)) {
2662                 RTE_PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
2663                 return -ENODEV;
2664         }
2665
2666         dev = &rte_eth_devices[port_id];
2667
2668         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_intr_enable, -ENOTSUP);
2669         return (*dev->dev_ops->rx_queue_intr_enable)(dev, queue_id);
2670 }
2671
2672 int
2673 rte_eth_dev_rx_intr_disable(uint8_t port_id,
2674                             uint16_t queue_id)
2675 {
2676         struct rte_eth_dev *dev;
2677
2678         if (!rte_eth_dev_is_valid_port(port_id)) {
2679                 RTE_PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
2680                 return -ENODEV;
2681         }
2682
2683         dev = &rte_eth_devices[port_id];
2684
2685         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_intr_disable, -ENOTSUP);
2686         return (*dev->dev_ops->rx_queue_intr_disable)(dev, queue_id);
2687 }
2688
2689 #ifdef RTE_NIC_BYPASS
2690 int rte_eth_dev_bypass_init(uint8_t port_id)
2691 {
2692         struct rte_eth_dev *dev;
2693
2694         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2695
2696         dev = &rte_eth_devices[port_id];
2697         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_init, -ENOTSUP);
2698         (*dev->dev_ops->bypass_init)(dev);
2699         return 0;
2700 }
2701
2702 int
2703 rte_eth_dev_bypass_state_show(uint8_t port_id, uint32_t *state)
2704 {
2705         struct rte_eth_dev *dev;
2706
2707         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2708
2709         dev = &rte_eth_devices[port_id];
2710         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_state_show, -ENOTSUP);
2711         (*dev->dev_ops->bypass_state_show)(dev, state);
2712         return 0;
2713 }
2714
2715 int
2716 rte_eth_dev_bypass_state_set(uint8_t port_id, uint32_t *new_state)
2717 {
2718         struct rte_eth_dev *dev;
2719
2720         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2721
2722         dev = &rte_eth_devices[port_id];
2723         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_state_set, -ENOTSUP);
2724         (*dev->dev_ops->bypass_state_set)(dev, new_state);
2725         return 0;
2726 }
2727
2728 int
2729 rte_eth_dev_bypass_event_show(uint8_t port_id, uint32_t event, uint32_t *state)
2730 {
2731         struct rte_eth_dev *dev;
2732
2733         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2734
2735         dev = &rte_eth_devices[port_id];
2736         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_state_show, -ENOTSUP);
2737         (*dev->dev_ops->bypass_event_show)(dev, event, state);
2738         return 0;
2739 }
2740
2741 int
2742 rte_eth_dev_bypass_event_store(uint8_t port_id, uint32_t event, uint32_t state)
2743 {
2744         struct rte_eth_dev *dev;
2745
2746         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2747
2748         dev = &rte_eth_devices[port_id];
2749
2750         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_event_set, -ENOTSUP);
2751         (*dev->dev_ops->bypass_event_set)(dev, event, state);
2752         return 0;
2753 }
2754
2755 int
2756 rte_eth_dev_wd_timeout_store(uint8_t port_id, uint32_t timeout)
2757 {
2758         struct rte_eth_dev *dev;
2759
2760         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2761
2762         dev = &rte_eth_devices[port_id];
2763
2764         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_wd_timeout_set, -ENOTSUP);
2765         (*dev->dev_ops->bypass_wd_timeout_set)(dev, timeout);
2766         return 0;
2767 }
2768
2769 int
2770 rte_eth_dev_bypass_ver_show(uint8_t port_id, uint32_t *ver)
2771 {
2772         struct rte_eth_dev *dev;
2773
2774         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2775
2776         dev = &rte_eth_devices[port_id];
2777
2778         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_ver_show, -ENOTSUP);
2779         (*dev->dev_ops->bypass_ver_show)(dev, ver);
2780         return 0;
2781 }
2782
2783 int
2784 rte_eth_dev_bypass_wd_timeout_show(uint8_t port_id, uint32_t *wd_timeout)
2785 {
2786         struct rte_eth_dev *dev;
2787
2788         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2789
2790         dev = &rte_eth_devices[port_id];
2791
2792         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_wd_timeout_show, -ENOTSUP);
2793         (*dev->dev_ops->bypass_wd_timeout_show)(dev, wd_timeout);
2794         return 0;
2795 }
2796
2797 int
2798 rte_eth_dev_bypass_wd_reset(uint8_t port_id)
2799 {
2800         struct rte_eth_dev *dev;
2801
2802         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2803
2804         dev = &rte_eth_devices[port_id];
2805
2806         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_wd_reset, -ENOTSUP);
2807         (*dev->dev_ops->bypass_wd_reset)(dev);
2808         return 0;
2809 }
2810 #endif
2811
2812 int
2813 rte_eth_dev_filter_supported(uint8_t port_id, enum rte_filter_type filter_type)
2814 {
2815         struct rte_eth_dev *dev;
2816
2817         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2818
2819         dev = &rte_eth_devices[port_id];
2820         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->filter_ctrl, -ENOTSUP);
2821         return (*dev->dev_ops->filter_ctrl)(dev, filter_type,
2822                                 RTE_ETH_FILTER_NOP, NULL);
2823 }
2824
2825 int
2826 rte_eth_dev_filter_ctrl(uint8_t port_id, enum rte_filter_type filter_type,
2827                        enum rte_filter_op filter_op, void *arg)
2828 {
2829         struct rte_eth_dev *dev;
2830
2831         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
2832
2833         dev = &rte_eth_devices[port_id];
2834         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->filter_ctrl, -ENOTSUP);
2835         return (*dev->dev_ops->filter_ctrl)(dev, filter_type, filter_op, arg);
2836 }
2837
2838 void *
2839 rte_eth_add_rx_callback(uint8_t port_id, uint16_t queue_id,
2840                 rte_rx_callback_fn fn, void *user_param)
2841 {
2842 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
2843         rte_errno = ENOTSUP;
2844         return NULL;
2845 #endif
2846         /* check input parameters */
2847         if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
2848                     queue_id >= rte_eth_devices[port_id].data->nb_rx_queues) {
2849                 rte_errno = EINVAL;
2850                 return NULL;
2851         }
2852
2853         struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
2854
2855         if (cb == NULL) {
2856                 rte_errno = ENOMEM;
2857                 return NULL;
2858         }
2859
2860         cb->fn.rx = fn;
2861         cb->param = user_param;
2862
2863         /* Add the callbacks in fifo order. */
2864         struct rte_eth_rxtx_callback *tail =
2865                 rte_eth_devices[port_id].post_rx_burst_cbs[queue_id];
2866
2867         if (!tail) {
2868                 rte_eth_devices[port_id].post_rx_burst_cbs[queue_id] = cb;
2869
2870         } else {
2871                 while (tail->next)
2872                         tail = tail->next;
2873                 tail->next = cb;
2874         }
2875
2876         return cb;
2877 }
2878
2879 void *
2880 rte_eth_add_tx_callback(uint8_t port_id, uint16_t queue_id,
2881                 rte_tx_callback_fn fn, void *user_param)
2882 {
2883 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
2884         rte_errno = ENOTSUP;
2885         return NULL;
2886 #endif
2887         /* check input parameters */
2888         if (!rte_eth_dev_is_valid_port(port_id) || fn == NULL ||
2889                     queue_id >= rte_eth_devices[port_id].data->nb_tx_queues) {
2890                 rte_errno = EINVAL;
2891                 return NULL;
2892         }
2893
2894         struct rte_eth_rxtx_callback *cb = rte_zmalloc(NULL, sizeof(*cb), 0);
2895
2896         if (cb == NULL) {
2897                 rte_errno = ENOMEM;
2898                 return NULL;
2899         }
2900
2901         cb->fn.tx = fn;
2902         cb->param = user_param;
2903
2904         /* Add the callbacks in fifo order. */
2905         struct rte_eth_rxtx_callback *tail =
2906                 rte_eth_devices[port_id].pre_tx_burst_cbs[queue_id];
2907
2908         if (!tail) {
2909                 rte_eth_devices[port_id].pre_tx_burst_cbs[queue_id] = cb;
2910
2911         } else {
2912                 while (tail->next)
2913                         tail = tail->next;
2914                 tail->next = cb;
2915         }
2916
2917         return cb;
2918 }
2919
2920 int
2921 rte_eth_remove_rx_callback(uint8_t port_id, uint16_t queue_id,
2922                 struct rte_eth_rxtx_callback *user_cb)
2923 {
2924 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
2925         return -ENOTSUP;
2926 #endif
2927         /* Check input parameters. */
2928         if (!rte_eth_dev_is_valid_port(port_id) || user_cb == NULL ||
2929                     queue_id >= rte_eth_devices[port_id].data->nb_rx_queues) {
2930                 return -EINVAL;
2931         }
2932
2933         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
2934         struct rte_eth_rxtx_callback *cb = dev->post_rx_burst_cbs[queue_id];
2935         struct rte_eth_rxtx_callback *prev_cb;
2936
2937         /* Reset head pointer and remove user cb if first in the list. */
2938         if (cb == user_cb) {
2939                 dev->post_rx_burst_cbs[queue_id] = user_cb->next;
2940                 return 0;
2941         }
2942
2943         /* Remove the user cb from the callback list. */
2944         do {
2945                 prev_cb = cb;
2946                 cb = cb->next;
2947
2948                 if (cb == user_cb) {
2949                         prev_cb->next = user_cb->next;
2950                         return 0;
2951                 }
2952
2953         } while (cb != NULL);
2954
2955         /* Callback wasn't found. */
2956         return -EINVAL;
2957 }
2958
2959 int
2960 rte_eth_remove_tx_callback(uint8_t port_id, uint16_t queue_id,
2961                 struct rte_eth_rxtx_callback *user_cb)
2962 {
2963 #ifndef RTE_ETHDEV_RXTX_CALLBACKS
2964         return -ENOTSUP;
2965 #endif
2966         /* Check input parameters. */
2967         if (!rte_eth_dev_is_valid_port(port_id) || user_cb == NULL ||
2968                     queue_id >= rte_eth_devices[port_id].data->nb_tx_queues) {
2969                 return -EINVAL;
2970         }
2971
2972         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
2973         struct rte_eth_rxtx_callback *cb = dev->pre_tx_burst_cbs[queue_id];
2974         struct rte_eth_rxtx_callback *prev_cb;
2975
2976         /* Reset head pointer and remove user cb if first in the list. */
2977         if (cb == user_cb) {
2978                 dev->pre_tx_burst_cbs[queue_id] = user_cb->next;
2979                 return 0;
2980         }
2981
2982         /* Remove the user cb from the callback list. */
2983         do {
2984                 prev_cb = cb;
2985                 cb = cb->next;
2986
2987                 if (cb == user_cb) {
2988                         prev_cb->next = user_cb->next;
2989                         return 0;
2990                 }
2991
2992         } while (cb != NULL);
2993
2994         /* Callback wasn't found. */
2995         return -EINVAL;
2996 }
2997
2998 int
2999 rte_eth_rx_queue_info_get(uint8_t port_id, uint16_t queue_id,
3000         struct rte_eth_rxq_info *qinfo)
3001 {
3002         struct rte_eth_dev *dev;
3003
3004         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3005
3006         if (qinfo == NULL)
3007                 return -EINVAL;
3008
3009         dev = &rte_eth_devices[port_id];
3010         if (queue_id >= dev->data->nb_rx_queues) {
3011                 RTE_PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", queue_id);
3012                 return -EINVAL;
3013         }
3014
3015         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rxq_info_get, -ENOTSUP);
3016
3017         memset(qinfo, 0, sizeof(*qinfo));
3018         dev->dev_ops->rxq_info_get(dev, queue_id, qinfo);
3019         return 0;
3020 }
3021
3022 int
3023 rte_eth_tx_queue_info_get(uint8_t port_id, uint16_t queue_id,
3024         struct rte_eth_txq_info *qinfo)
3025 {
3026         struct rte_eth_dev *dev;
3027
3028         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3029
3030         if (qinfo == NULL)
3031                 return -EINVAL;
3032
3033         dev = &rte_eth_devices[port_id];
3034         if (queue_id >= dev->data->nb_tx_queues) {
3035                 RTE_PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", queue_id);
3036                 return -EINVAL;
3037         }
3038
3039         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->txq_info_get, -ENOTSUP);
3040
3041         memset(qinfo, 0, sizeof(*qinfo));
3042         dev->dev_ops->txq_info_get(dev, queue_id, qinfo);
3043         return 0;
3044 }
3045
3046 int
3047 rte_eth_dev_set_mc_addr_list(uint8_t port_id,
3048                              struct ether_addr *mc_addr_set,
3049                              uint32_t nb_mc_addr)
3050 {
3051         struct rte_eth_dev *dev;
3052
3053         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3054
3055         dev = &rte_eth_devices[port_id];
3056         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_mc_addr_list, -ENOTSUP);
3057         return dev->dev_ops->set_mc_addr_list(dev, mc_addr_set, nb_mc_addr);
3058 }
3059
3060 int
3061 rte_eth_timesync_enable(uint8_t port_id)
3062 {
3063         struct rte_eth_dev *dev;
3064
3065         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3066         dev = &rte_eth_devices[port_id];
3067
3068         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_enable, -ENOTSUP);
3069         return (*dev->dev_ops->timesync_enable)(dev);
3070 }
3071
3072 int
3073 rte_eth_timesync_disable(uint8_t port_id)
3074 {
3075         struct rte_eth_dev *dev;
3076
3077         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3078         dev = &rte_eth_devices[port_id];
3079
3080         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_disable, -ENOTSUP);
3081         return (*dev->dev_ops->timesync_disable)(dev);
3082 }
3083
3084 int
3085 rte_eth_timesync_read_rx_timestamp(uint8_t port_id, struct timespec *timestamp,
3086                                    uint32_t flags)
3087 {
3088         struct rte_eth_dev *dev;
3089
3090         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3091         dev = &rte_eth_devices[port_id];
3092
3093         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_rx_timestamp, -ENOTSUP);
3094         return (*dev->dev_ops->timesync_read_rx_timestamp)(dev, timestamp, flags);
3095 }
3096
3097 int
3098 rte_eth_timesync_read_tx_timestamp(uint8_t port_id, struct timespec *timestamp)
3099 {
3100         struct rte_eth_dev *dev;
3101
3102         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3103         dev = &rte_eth_devices[port_id];
3104
3105         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_tx_timestamp, -ENOTSUP);
3106         return (*dev->dev_ops->timesync_read_tx_timestamp)(dev, timestamp);
3107 }
3108
3109 int
3110 rte_eth_timesync_adjust_time(uint8_t port_id, int64_t delta)
3111 {
3112         struct rte_eth_dev *dev;
3113
3114         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3115         dev = &rte_eth_devices[port_id];
3116
3117         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_adjust_time, -ENOTSUP);
3118         return (*dev->dev_ops->timesync_adjust_time)(dev, delta);
3119 }
3120
3121 int
3122 rte_eth_timesync_read_time(uint8_t port_id, struct timespec *timestamp)
3123 {
3124         struct rte_eth_dev *dev;
3125
3126         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3127         dev = &rte_eth_devices[port_id];
3128
3129         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_read_time, -ENOTSUP);
3130         return (*dev->dev_ops->timesync_read_time)(dev, timestamp);
3131 }
3132
3133 int
3134 rte_eth_timesync_write_time(uint8_t port_id, const struct timespec *timestamp)
3135 {
3136         struct rte_eth_dev *dev;
3137
3138         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3139         dev = &rte_eth_devices[port_id];
3140
3141         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timesync_write_time, -ENOTSUP);
3142         return (*dev->dev_ops->timesync_write_time)(dev, timestamp);
3143 }
3144
3145 int
3146 rte_eth_dev_get_reg_length(uint8_t port_id)
3147 {
3148         struct rte_eth_dev *dev;
3149
3150         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3151
3152         dev = &rte_eth_devices[port_id];
3153         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_reg_length, -ENOTSUP);
3154         return (*dev->dev_ops->get_reg_length)(dev);
3155 }
3156
3157 int
3158 rte_eth_dev_get_reg_info(uint8_t port_id, struct rte_dev_reg_info *info)
3159 {
3160         struct rte_eth_dev *dev;
3161
3162         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3163
3164         dev = &rte_eth_devices[port_id];
3165         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_reg, -ENOTSUP);
3166         return (*dev->dev_ops->get_reg)(dev, info);
3167 }
3168
3169 int
3170 rte_eth_dev_get_eeprom_length(uint8_t port_id)
3171 {
3172         struct rte_eth_dev *dev;
3173
3174         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3175
3176         dev = &rte_eth_devices[port_id];
3177         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_eeprom_length, -ENOTSUP);
3178         return (*dev->dev_ops->get_eeprom_length)(dev);
3179 }
3180
3181 int
3182 rte_eth_dev_get_eeprom(uint8_t port_id, struct rte_dev_eeprom_info *info)
3183 {
3184         struct rte_eth_dev *dev;
3185
3186         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3187
3188         dev = &rte_eth_devices[port_id];
3189         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_eeprom, -ENOTSUP);
3190         return (*dev->dev_ops->get_eeprom)(dev, info);
3191 }
3192
3193 int
3194 rte_eth_dev_set_eeprom(uint8_t port_id, struct rte_dev_eeprom_info *info)
3195 {
3196         struct rte_eth_dev *dev;
3197
3198         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
3199
3200         dev = &rte_eth_devices[port_id];
3201         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_eeprom, -ENOTSUP);
3202         return (*dev->dev_ops->set_eeprom)(dev, info);
3203 }
3204
3205 int
3206 rte_eth_dev_get_dcb_info(uint8_t port_id,
3207                              struct rte_eth_dcb_info *dcb_info)
3208 {
3209         struct rte_eth_dev *dev;
3210
3211         if (!rte_eth_dev_is_valid_port(port_id)) {
3212                 RTE_PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
3213                 return -ENODEV;
3214         }
3215
3216         dev = &rte_eth_devices[port_id];
3217         memset(dcb_info, 0, sizeof(struct rte_eth_dcb_info));
3218
3219         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->get_dcb_info, -ENOTSUP);
3220         return (*dev->dev_ops->get_dcb_info)(dev, dcb_info);
3221 }
3222
3223 void
3224 rte_eth_copy_pci_info(struct rte_eth_dev *eth_dev, struct rte_pci_device *pci_dev)
3225 {
3226         if ((eth_dev == NULL) || (pci_dev == NULL)) {
3227                 RTE_PMD_DEBUG_TRACE("NULL pointer eth_dev=%p pci_dev=%p\n",
3228                                 eth_dev, pci_dev);
3229                 return;
3230         }
3231
3232         eth_dev->data->dev_flags = 0;
3233         if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
3234                 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
3235         if (pci_dev->driver->drv_flags & RTE_PCI_DRV_DETACHABLE)
3236                 eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
3237
3238         eth_dev->data->kdrv = pci_dev->kdrv;
3239         eth_dev->data->numa_node = pci_dev->numa_node;
3240         eth_dev->data->drv_name = pci_dev->driver->name;
3241 }