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