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