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