ethdev: export allocate function
[dpdk.git] / lib / librte_ether / rte_ethdev.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 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
45 #include <rte_byteorder.h>
46 #include <rte_log.h>
47 #include <rte_debug.h>
48 #include <rte_interrupts.h>
49 #include <rte_pci.h>
50 #include <rte_memory.h>
51 #include <rte_memcpy.h>
52 #include <rte_memzone.h>
53 #include <rte_launch.h>
54 #include <rte_tailq.h>
55 #include <rte_eal.h>
56 #include <rte_per_lcore.h>
57 #include <rte_lcore.h>
58 #include <rte_atomic.h>
59 #include <rte_branch_prediction.h>
60 #include <rte_common.h>
61 #include <rte_ring.h>
62 #include <rte_mempool.h>
63 #include <rte_malloc.h>
64 #include <rte_mbuf.h>
65 #include <rte_errno.h>
66 #include <rte_spinlock.h>
67
68 #include "rte_ether.h"
69 #include "rte_ethdev.h"
70
71 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
72 #define PMD_DEBUG_TRACE(fmt, args...) do {                        \
73                 RTE_LOG(ERR, PMD, "%s: " fmt, __func__, ## args); \
74         } while (0)
75 #else
76 #define PMD_DEBUG_TRACE(fmt, args...)
77 #endif
78
79 /* Macros for checking for restricting functions to primary instance only */
80 #define PROC_PRIMARY_OR_ERR_RET(retval) do { \
81         if (rte_eal_process_type() != RTE_PROC_PRIMARY) { \
82                 PMD_DEBUG_TRACE("Cannot run in secondary processes\n"); \
83                 return (retval); \
84         } \
85 } while(0)
86 #define PROC_PRIMARY_OR_RET() do { \
87         if (rte_eal_process_type() != RTE_PROC_PRIMARY) { \
88                 PMD_DEBUG_TRACE("Cannot run in secondary processes\n"); \
89                 return; \
90         } \
91 } while(0)
92
93 /* Macros to check for invlaid function pointers in dev_ops structure */
94 #define FUNC_PTR_OR_ERR_RET(func, retval) do { \
95         if ((func) == NULL) { \
96                 PMD_DEBUG_TRACE("Function not supported\n"); \
97                 return (retval); \
98         } \
99 } while(0)
100 #define FUNC_PTR_OR_RET(func) do { \
101         if ((func) == NULL) { \
102                 PMD_DEBUG_TRACE("Function not supported\n"); \
103                 return; \
104         } \
105 } while(0)
106
107 static const char *MZ_RTE_ETH_DEV_DATA = "rte_eth_dev_data";
108 struct rte_eth_dev rte_eth_devices[RTE_MAX_ETHPORTS];
109 static struct rte_eth_dev_data *rte_eth_dev_data = NULL;
110 static uint8_t nb_ports = 0;
111
112 /* spinlock for eth device callbacks */
113 static rte_spinlock_t rte_eth_dev_cb_lock = RTE_SPINLOCK_INITIALIZER;
114
115 /**
116  * The user application callback description.
117  *
118  * It contains callback address to be registered by user application,
119  * the pointer to the parameters for callback, and the event type.
120  */
121 struct rte_eth_dev_callback {
122         TAILQ_ENTRY(rte_eth_dev_callback) next; /**< Callbacks list */
123         rte_eth_dev_cb_fn cb_fn;                /**< Callback address */
124         void *cb_arg;                           /**< Parameter for callback */
125         enum rte_eth_event_type event;          /**< Interrupt event type */
126         uint32_t active;                        /**< Callback is executing */
127 };
128
129 enum {
130         STAT_QMAP_TX = 0,
131         STAT_QMAP_RX
132 };
133
134 static inline void
135 rte_eth_dev_data_alloc(void)
136 {
137         const unsigned flags = 0;
138         const struct rte_memzone *mz;
139
140         if (rte_eal_process_type() == RTE_PROC_PRIMARY){
141                 mz = rte_memzone_reserve(MZ_RTE_ETH_DEV_DATA,
142                                 RTE_MAX_ETHPORTS * sizeof(*rte_eth_dev_data),
143                                 rte_socket_id(), flags);
144         } else
145                 mz = rte_memzone_lookup(MZ_RTE_ETH_DEV_DATA);
146         if (mz == NULL)
147                 rte_panic("Cannot allocate memzone for ethernet port data\n");
148
149         rte_eth_dev_data = mz->addr;
150         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
151                 memset(rte_eth_dev_data, 0,
152                                 RTE_MAX_ETHPORTS * sizeof(*rte_eth_dev_data));
153 }
154
155 struct rte_eth_dev *
156 rte_eth_dev_allocate(void)
157 {
158         struct rte_eth_dev *eth_dev;
159
160         if (nb_ports == RTE_MAX_ETHPORTS) {
161                 PMD_DEBUG_TRACE("Reached maximum number of ethernet ports\n");
162                 return NULL;
163         }
164
165         if (rte_eth_dev_data == NULL)
166                 rte_eth_dev_data_alloc();
167
168         eth_dev = &rte_eth_devices[nb_ports];
169         eth_dev->data = &rte_eth_dev_data[nb_ports];
170         eth_dev->data->port_id = nb_ports++;
171         return eth_dev;
172 }
173
174 static int
175 rte_eth_dev_init(struct rte_pci_driver *pci_drv,
176                  struct rte_pci_device *pci_dev)
177 {
178         struct eth_driver    *eth_drv;
179         struct rte_eth_dev *eth_dev;
180         int diag;
181
182         eth_drv = (struct eth_driver *)pci_drv;
183
184         eth_dev = rte_eth_dev_allocate();
185         if (eth_dev == NULL)
186                 return -ENOMEM;
187
188         if (rte_eal_process_type() == RTE_PROC_PRIMARY){
189                 eth_dev->data->dev_private = rte_zmalloc("ethdev private structure",
190                                   eth_drv->dev_private_size,
191                                   CACHE_LINE_SIZE);
192                 if (eth_dev->data->dev_private == NULL)
193                         rte_panic("Cannot allocate memzone for private port data\n");
194         }
195         eth_dev->pci_dev = pci_dev;
196         eth_dev->driver = eth_drv;
197         eth_dev->data->rx_mbuf_alloc_failed = 0;
198
199         /* init user callbacks */
200         TAILQ_INIT(&(eth_dev->callbacks));
201
202         /*
203          * Set the default maximum frame size.
204          */
205         eth_dev->data->max_frame_size = ETHER_MAX_LEN;
206
207         /* Invoke PMD device initialization function */
208         diag = (*eth_drv->eth_dev_init)(eth_drv, eth_dev);
209         if (diag == 0)
210                 return (0);
211
212         PMD_DEBUG_TRACE("driver %s: eth_dev_init(vendor_id=0x%u device_id=0x%x)"
213                         " failed\n", pci_drv->name,
214                         (unsigned) pci_dev->id.vendor_id,
215                         (unsigned) pci_dev->id.device_id);
216         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
217                 rte_free(eth_dev->data->dev_private);
218         nb_ports--;
219         return diag;
220 }
221
222 /**
223  * Register an Ethernet [Poll Mode] driver.
224  *
225  * Function invoked by the initialization function of an Ethernet driver
226  * to simultaneously register itself as a PCI driver and as an Ethernet
227  * Poll Mode Driver.
228  * Invokes the rte_eal_pci_register() function to register the *pci_drv*
229  * structure embedded in the *eth_drv* structure, after having stored the
230  * address of the rte_eth_dev_init() function in the *devinit* field of
231  * the *pci_drv* structure.
232  * During the PCI probing phase, the rte_eth_dev_init() function is
233  * invoked for each PCI [Ethernet device] matching the embedded PCI
234  * identifiers provided by the driver.
235  */
236 void
237 rte_eth_driver_register(struct eth_driver *eth_drv)
238 {
239         eth_drv->pci_drv.devinit = rte_eth_dev_init;
240         rte_eal_pci_register(&eth_drv->pci_drv);
241 }
242
243 int
244 rte_eth_dev_socket_id(uint8_t port_id)
245 {
246         if (port_id >= nb_ports)
247                 return -1;
248         return rte_eth_devices[port_id].pci_dev->numa_node;
249 }
250
251 uint8_t
252 rte_eth_dev_count(void)
253 {
254         return (nb_ports);
255 }
256
257 static int
258 rte_eth_dev_rx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
259 {
260         uint16_t old_nb_queues = dev->data->nb_rx_queues;
261         void **rxq;
262         unsigned i;
263
264         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_release, -ENOTSUP);
265
266         if (dev->data->rx_queues == NULL) {
267                 dev->data->rx_queues = rte_zmalloc("ethdev->rx_queues",
268                                 sizeof(dev->data->rx_queues[0]) * nb_queues,
269                                 CACHE_LINE_SIZE);
270                 if (dev->data->rx_queues == NULL) {
271                         dev->data->nb_rx_queues = 0;
272                         return -(ENOMEM);
273                 }
274         } else {
275                 rxq = dev->data->rx_queues;
276
277                 for (i = nb_queues; i < old_nb_queues; i++)
278                         (*dev->dev_ops->rx_queue_release)(rxq[i]);
279                 rxq = rte_realloc(rxq, sizeof(rxq[0]) * nb_queues,
280                                 CACHE_LINE_SIZE);
281                 if (rxq == NULL)
282                         return -(ENOMEM);
283
284                 if (nb_queues > old_nb_queues)
285                         memset(rxq + old_nb_queues, 0,
286                                 sizeof(rxq[0]) * (nb_queues - old_nb_queues));
287
288                 dev->data->rx_queues = rxq;
289
290         }
291         dev->data->nb_rx_queues = nb_queues;
292         return (0);
293 }
294
295 static int
296 rte_eth_dev_tx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
297 {
298         uint16_t old_nb_queues = dev->data->nb_tx_queues;
299         void **txq;
300         unsigned i;
301
302         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_release, -ENOTSUP);
303
304         if (dev->data->tx_queues == NULL) {
305                 dev->data->tx_queues = rte_zmalloc("ethdev->tx_queues",
306                                 sizeof(dev->data->tx_queues[0]) * nb_queues,
307                                 CACHE_LINE_SIZE);
308                 if (dev->data->tx_queues == NULL) {
309                         dev->data->nb_tx_queues = 0;
310                         return -(ENOMEM);
311                 }
312         } else {
313                 txq = dev->data->tx_queues;
314
315                 for (i = nb_queues; i < old_nb_queues; i++)
316                         (*dev->dev_ops->tx_queue_release)(txq[i]);
317                 txq = rte_realloc(txq, sizeof(txq[0]) * nb_queues,
318                                 CACHE_LINE_SIZE);
319                 if (txq == NULL)
320                         return -(ENOMEM);
321
322                 if (nb_queues > old_nb_queues)
323                         memset(txq + old_nb_queues, 0,
324                                 sizeof(txq[0]) * (nb_queues - old_nb_queues));
325
326                 dev->data->tx_queues = txq;
327
328         }
329         dev->data->nb_tx_queues = nb_queues;
330         return (0);
331 }
332
333 static int
334 rte_eth_dev_check_mq_mode(uint8_t port_id, uint16_t nb_rx_q, uint16_t nb_tx_q,
335                       const struct rte_eth_conf *dev_conf)
336 {
337         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
338
339         if (RTE_ETH_DEV_SRIOV(dev).active != 0) {
340                 /* check multi-queue mode */
341                 if ((dev_conf->rxmode.mq_mode == ETH_MQ_RX_RSS) || 
342                     (dev_conf->rxmode.mq_mode == ETH_MQ_RX_DCB) ||
343                     (dev_conf->rxmode.mq_mode == ETH_MQ_RX_DCB_RSS) ||
344                     (dev_conf->txmode.mq_mode == ETH_MQ_TX_DCB)) {
345                         /* SRIOV only works in VMDq enable mode */
346                         PMD_DEBUG_TRACE("ethdev port_id=%d SRIOV active, "
347                                         "wrong VMDQ mq_mode rx %d tx %d\n", 
348                                         port_id, dev_conf->rxmode.mq_mode,
349                                         dev_conf->txmode.mq_mode);
350                         return (-EINVAL);
351                 }
352
353                 switch (dev_conf->rxmode.mq_mode) {
354                 case ETH_MQ_RX_VMDQ_RSS:
355                 case ETH_MQ_RX_VMDQ_DCB:
356                 case ETH_MQ_RX_VMDQ_DCB_RSS:
357                         /* DCB/RSS VMDQ in SRIOV mode, not implement yet */
358                         PMD_DEBUG_TRACE("ethdev port_id=%d SRIOV active, "
359                                         "unsupported VMDQ mq_mode rx %d\n", 
360                                         port_id, dev_conf->rxmode.mq_mode);
361                         return (-EINVAL);
362                 default: /* ETH_MQ_RX_VMDQ_ONLY or ETH_MQ_RX_NONE */
363                         /* if nothing mq mode configure, use default scheme */
364                         dev->data->dev_conf.rxmode.mq_mode = ETH_MQ_RX_VMDQ_ONLY;
365                         if (RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool > 1)
366                                 RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool = 1;
367                         break;
368                 }
369
370                 switch (dev_conf->txmode.mq_mode) {
371                 case ETH_MQ_TX_VMDQ_DCB:
372                         /* DCB VMDQ in SRIOV mode, not implement yet */
373                         PMD_DEBUG_TRACE("ethdev port_id=%d SRIOV active, "
374                                         "unsupported VMDQ mq_mode tx %d\n", 
375                                         port_id, dev_conf->txmode.mq_mode);
376                         return (-EINVAL);
377                 default: /* ETH_MQ_TX_VMDQ_ONLY or ETH_MQ_TX_NONE */
378                         /* if nothing mq mode configure, use default scheme */
379                         dev->data->dev_conf.txmode.mq_mode = ETH_MQ_TX_VMDQ_ONLY;
380                         if (RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool > 1)
381                                 RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool = 1;
382                         break;
383                 }
384
385                 /* check valid queue number */
386                 if ((nb_rx_q > RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool) ||
387                     (nb_tx_q > RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool)) {
388                         PMD_DEBUG_TRACE("ethdev port_id=%d SRIOV active, "
389                                     "queue number must less equal to %d\n", 
390                                         port_id, RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool);
391                         return (-EINVAL);
392                 }
393         } else {
394                 /* For vmdb+dcb mode check our configuration before we go further */
395                 if (dev_conf->rxmode.mq_mode == ETH_MQ_RX_VMDQ_DCB) {
396                         const struct rte_eth_vmdq_dcb_conf *conf;
397                         
398                         if (nb_rx_q != ETH_VMDQ_DCB_NUM_QUEUES) {
399                                 PMD_DEBUG_TRACE("ethdev port_id=%d VMDQ+DCB, nb_rx_q "
400                                                 "!= %d\n",
401                                                 port_id, ETH_VMDQ_DCB_NUM_QUEUES);
402                                 return (-EINVAL);
403                         }
404                         conf = &(dev_conf->rx_adv_conf.vmdq_dcb_conf);
405                         if (! (conf->nb_queue_pools == ETH_16_POOLS ||
406                                conf->nb_queue_pools == ETH_32_POOLS)) {
407                                 PMD_DEBUG_TRACE("ethdev port_id=%d VMDQ+DCB selected, "
408                                                 "nb_queue_pools must be %d or %d\n",
409                                                 port_id, ETH_16_POOLS, ETH_32_POOLS);
410                                 return (-EINVAL);
411                         }
412                 }
413                 if (dev_conf->txmode.mq_mode == ETH_MQ_TX_VMDQ_DCB) {
414                         const struct rte_eth_vmdq_dcb_tx_conf *conf;
415                         
416                         if (nb_tx_q != ETH_VMDQ_DCB_NUM_QUEUES) {
417                                 PMD_DEBUG_TRACE("ethdev port_id=%d VMDQ+DCB, nb_tx_q "
418                                                 "!= %d\n",
419                                                 port_id, ETH_VMDQ_DCB_NUM_QUEUES);
420                                 return (-EINVAL);
421                         }
422                         conf = &(dev_conf->tx_adv_conf.vmdq_dcb_tx_conf);
423                         if (! (conf->nb_queue_pools == ETH_16_POOLS ||
424                                conf->nb_queue_pools == ETH_32_POOLS)) {
425                                 PMD_DEBUG_TRACE("ethdev port_id=%d VMDQ+DCB selected, "
426                                                 "nb_queue_pools != %d or nb_queue_pools "
427                                                 "!= %d\n",
428                                                 port_id, ETH_16_POOLS, ETH_32_POOLS);
429                                 return (-EINVAL);
430                         }
431                 }
432                 
433                 /* For DCB mode check our configuration before we go further */
434                 if (dev_conf->rxmode.mq_mode == ETH_MQ_RX_DCB) {
435                         const struct rte_eth_dcb_rx_conf *conf;
436                         
437                         if (nb_rx_q != ETH_DCB_NUM_QUEUES) {
438                                 PMD_DEBUG_TRACE("ethdev port_id=%d DCB, nb_rx_q "
439                                                 "!= %d\n",
440                                                 port_id, ETH_DCB_NUM_QUEUES);
441                                 return (-EINVAL);
442                         }
443                         conf = &(dev_conf->rx_adv_conf.dcb_rx_conf);
444                         if (! (conf->nb_tcs == ETH_4_TCS ||
445                                conf->nb_tcs == ETH_8_TCS)) {
446                                 PMD_DEBUG_TRACE("ethdev port_id=%d DCB selected, "
447                                                 "nb_tcs != %d or nb_tcs "
448                                                 "!= %d\n",
449                                                 port_id, ETH_4_TCS, ETH_8_TCS);
450                                 return (-EINVAL);
451                         }
452                 }
453                 
454                 if (dev_conf->txmode.mq_mode == ETH_MQ_TX_DCB) {
455                         const struct rte_eth_dcb_tx_conf *conf;
456                         
457                         if (nb_tx_q != ETH_DCB_NUM_QUEUES) {
458                                 PMD_DEBUG_TRACE("ethdev port_id=%d DCB, nb_tx_q "
459                                                 "!= %d\n",
460                                                 port_id, ETH_DCB_NUM_QUEUES);
461                                 return (-EINVAL);
462                         }
463                         conf = &(dev_conf->tx_adv_conf.dcb_tx_conf);
464                         if (! (conf->nb_tcs == ETH_4_TCS ||
465                                conf->nb_tcs == ETH_8_TCS)) {
466                                 PMD_DEBUG_TRACE("ethdev port_id=%d DCB selected, "
467                                                 "nb_tcs != %d or nb_tcs "
468                                                 "!= %d\n",
469                                                 port_id, ETH_4_TCS, ETH_8_TCS);
470                                 return (-EINVAL);
471                         }
472                 }
473         }
474         return 0;
475 }
476
477 int
478 rte_eth_dev_configure(uint8_t port_id, uint16_t nb_rx_q, uint16_t nb_tx_q,
479                       const struct rte_eth_conf *dev_conf)
480 {
481         struct rte_eth_dev *dev;
482         struct rte_eth_dev_info dev_info;
483         int diag;
484
485         /* This function is only safe when called from the primary process
486          * in a multi-process setup*/
487         PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
488
489         if (port_id >= nb_ports || port_id >= RTE_MAX_ETHPORTS) {
490                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
491                 return (-EINVAL);
492         }
493         dev = &rte_eth_devices[port_id];
494
495         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
496         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_configure, -ENOTSUP);
497
498         if (dev->data->dev_started) {
499                 PMD_DEBUG_TRACE(
500                     "port %d must be stopped to allow configuration\n", port_id);
501                 return (-EBUSY);
502         }
503
504         /*
505          * Check that the numbers of RX and TX queues are not greater
506          * than the maximum number of RX and TX queues supported by the
507          * configured device.
508          */
509         (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
510         if (nb_rx_q > dev_info.max_rx_queues) {
511                 PMD_DEBUG_TRACE("ethdev port_id=%d nb_rx_queues=%d > %d\n",
512                                 port_id, nb_rx_q, dev_info.max_rx_queues);
513                 return (-EINVAL);
514         }
515         if (nb_rx_q == 0) {
516                 PMD_DEBUG_TRACE("ethdev port_id=%d nb_rx_q == 0\n", port_id);
517                 return (-EINVAL);
518         }
519
520         if (nb_tx_q > dev_info.max_tx_queues) {
521                 PMD_DEBUG_TRACE("ethdev port_id=%d nb_tx_queues=%d > %d\n",
522                                 port_id, nb_tx_q, dev_info.max_tx_queues);
523                 return (-EINVAL);
524         }
525         if (nb_tx_q == 0) {
526                 PMD_DEBUG_TRACE("ethdev port_id=%d nb_tx_q == 0\n", port_id);
527                 return (-EINVAL);
528         }
529
530         /* Copy the dev_conf parameter into the dev structure */
531         memcpy(&dev->data->dev_conf, dev_conf, sizeof(dev->data->dev_conf));
532
533         /*
534          * If jumbo frames are enabled, check that the maximum RX packet
535          * length is supported by the configured device.
536          */
537         if (dev_conf->rxmode.jumbo_frame == 1) {
538                 if (dev_conf->rxmode.max_rx_pkt_len >
539                     dev_info.max_rx_pktlen) {
540                         PMD_DEBUG_TRACE("ethdev port_id=%d max_rx_pkt_len %u"
541                                 " > max valid value %u\n",
542                                 port_id,
543                                 (unsigned)dev_conf->rxmode.max_rx_pkt_len,
544                                 (unsigned)dev_info.max_rx_pktlen);
545                         return (-EINVAL);
546                 }
547                 else if (dev_conf->rxmode.max_rx_pkt_len < ETHER_MIN_LEN) {
548                         PMD_DEBUG_TRACE("ethdev port_id=%d max_rx_pkt_len %u"
549                                 " < min valid value %u\n",
550                                 port_id,
551                                 (unsigned)dev_conf->rxmode.max_rx_pkt_len,
552                                 (unsigned)ETHER_MIN_LEN);
553                         return (-EINVAL);
554                 }
555         } else
556                 /* Use default value */
557                 dev->data->dev_conf.rxmode.max_rx_pkt_len = ETHER_MAX_LEN;
558
559         /* multipe queue mode checking */
560         diag = rte_eth_dev_check_mq_mode(port_id, nb_rx_q, nb_tx_q, dev_conf);
561         if (diag != 0) {
562                 PMD_DEBUG_TRACE("port%d rte_eth_dev_check_mq_mode = %d\n",
563                                 port_id, diag);
564                 return diag;
565         }
566
567         /*
568          * Setup new number of RX/TX queues and reconfigure device.
569          */
570         diag = rte_eth_dev_rx_queue_config(dev, nb_rx_q);
571         if (diag != 0) {
572                 PMD_DEBUG_TRACE("port%d rte_eth_dev_rx_queue_config = %d\n",
573                                 port_id, diag);
574                 return diag;
575         }
576
577         diag = rte_eth_dev_tx_queue_config(dev, nb_tx_q);
578         if (diag != 0) {
579                 PMD_DEBUG_TRACE("port%d rte_eth_dev_tx_queue_config = %d\n",
580                                 port_id, diag);
581                 rte_eth_dev_rx_queue_config(dev, 0);
582                 return diag;
583         }
584
585         diag = (*dev->dev_ops->dev_configure)(dev);
586         if (diag != 0) {
587                 PMD_DEBUG_TRACE("port%d dev_configure = %d\n",
588                                 port_id, diag);
589                 rte_eth_dev_rx_queue_config(dev, 0);
590                 rte_eth_dev_tx_queue_config(dev, 0);
591                 return diag;
592         }
593
594         return 0;
595 }
596
597 static void
598 rte_eth_dev_config_restore(uint8_t port_id)
599 {
600         struct rte_eth_dev *dev;
601         struct rte_eth_dev_info dev_info;
602         struct ether_addr addr;
603         uint16_t i;
604         uint32_t pool = 0;
605
606         dev = &rte_eth_devices[port_id];
607
608         rte_eth_dev_info_get(port_id, &dev_info);
609
610         if (RTE_ETH_DEV_SRIOV(dev).active)
611                 pool = RTE_ETH_DEV_SRIOV(dev).def_vmdq_idx;
612
613         /* replay MAC address configuration */
614         for (i = 0; i < dev_info.max_mac_addrs; i++) {
615                 addr = dev->data->mac_addrs[i];
616
617                 /* skip zero address */
618                 if (is_zero_ether_addr(&addr))
619                         continue;
620
621                 /* add address to the hardware */
622                 if  (*dev->dev_ops->mac_addr_add)
623                         (*dev->dev_ops->mac_addr_add)(dev, &addr, i, pool);
624                 else {
625                         PMD_DEBUG_TRACE("port %d: MAC address array not supported\n",
626                                         port_id);
627                         /* exit the loop but not return an error */
628                         break;
629                 }
630         }
631
632         /* replay promiscuous configuration */
633         if (rte_eth_promiscuous_get(port_id) == 1)
634                 rte_eth_promiscuous_enable(port_id);
635         else if (rte_eth_promiscuous_get(port_id) == 0)
636                 rte_eth_promiscuous_disable(port_id);
637
638         /* replay allmulticast configuration */
639         if (rte_eth_allmulticast_get(port_id) == 1)
640                 rte_eth_allmulticast_enable(port_id);
641         else if (rte_eth_allmulticast_get(port_id) == 0)
642                 rte_eth_allmulticast_disable(port_id);
643 }
644
645 int
646 rte_eth_dev_start(uint8_t port_id)
647 {
648         struct rte_eth_dev *dev;
649         int diag;
650
651         /* This function is only safe when called from the primary process
652          * in a multi-process setup*/
653         PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
654
655         if (port_id >= nb_ports) {
656                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
657                 return (-EINVAL);
658         }
659         dev = &rte_eth_devices[port_id];
660
661         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
662         diag = (*dev->dev_ops->dev_start)(dev);
663         if (diag == 0)
664                 dev->data->dev_started = 1;
665         else
666                 return diag;
667
668         rte_eth_dev_config_restore(port_id);
669
670         return 0;
671 }
672
673 void
674 rte_eth_dev_stop(uint8_t port_id)
675 {
676         struct rte_eth_dev *dev;
677
678         /* This function is only safe when called from the primary process
679          * in a multi-process setup*/
680         PROC_PRIMARY_OR_RET();
681
682         if (port_id >= nb_ports) {
683                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
684                 return;
685         }
686         dev = &rte_eth_devices[port_id];
687
688         FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
689         dev->data->dev_started = 0;
690         (*dev->dev_ops->dev_stop)(dev);
691 }
692
693 void
694 rte_eth_dev_close(uint8_t port_id)
695 {
696         struct rte_eth_dev *dev;
697
698         /* This function is only safe when called from the primary process
699          * in a multi-process setup*/
700         PROC_PRIMARY_OR_RET();
701
702         if (port_id >= nb_ports) {
703                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
704                 return;
705         }
706
707         dev = &rte_eth_devices[port_id];
708
709         FUNC_PTR_OR_RET(*dev->dev_ops->dev_close);
710         dev->data->dev_started = 0;
711         (*dev->dev_ops->dev_close)(dev);
712 }
713
714 int
715 rte_eth_rx_queue_setup(uint8_t port_id, uint16_t rx_queue_id,
716                        uint16_t nb_rx_desc, unsigned int socket_id,
717                        const struct rte_eth_rxconf *rx_conf,
718                        struct rte_mempool *mp)
719 {
720         struct rte_eth_dev *dev;
721         struct rte_pktmbuf_pool_private *mbp_priv;
722         struct rte_eth_dev_info dev_info;
723
724         /* This function is only safe when called from the primary process
725          * in a multi-process setup*/
726         PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
727
728         if (port_id >= nb_ports) {
729                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
730                 return (-EINVAL);
731         }
732         dev = &rte_eth_devices[port_id];
733         if (rx_queue_id >= dev->data->nb_rx_queues) {
734                 PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", rx_queue_id);
735                 return (-EINVAL);
736         }
737
738         if (dev->data->dev_started) {
739                 PMD_DEBUG_TRACE(
740                     "port %d must be stopped to allow configuration\n", port_id);
741                 return -EBUSY;
742         }
743
744         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
745         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_setup, -ENOTSUP);
746
747         /*
748          * Check the size of the mbuf data buffer.
749          * This value must be provided in the private data of the memory pool.
750          * First check that the memory pool has a valid private data.
751          */
752         (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
753         if (mp->private_data_size < sizeof(struct rte_pktmbuf_pool_private)) {
754                 PMD_DEBUG_TRACE("%s private_data_size %d < %d\n",
755                                 mp->name, (int) mp->private_data_size,
756                                 (int) sizeof(struct rte_pktmbuf_pool_private));
757                 return (-ENOSPC);
758         }
759         mbp_priv = (struct rte_pktmbuf_pool_private *)
760                 ((char *)mp + sizeof(struct rte_mempool));
761         if ((uint32_t) (mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM) <
762             dev_info.min_rx_bufsize) {
763                 PMD_DEBUG_TRACE("%s mbuf_data_room_size %d < %d "
764                                 "(RTE_PKTMBUF_HEADROOM=%d + min_rx_bufsize(dev)"
765                                 "=%d)\n",
766                                 mp->name,
767                                 (int)mbp_priv->mbuf_data_room_size,
768                                 (int)(RTE_PKTMBUF_HEADROOM +
769                                       dev_info.min_rx_bufsize),
770                                 (int)RTE_PKTMBUF_HEADROOM,
771                                 (int)dev_info.min_rx_bufsize);
772                 return (-EINVAL);
773         }
774
775         return (*dev->dev_ops->rx_queue_setup)(dev, rx_queue_id, nb_rx_desc,
776                                                socket_id, rx_conf, mp);
777 }
778
779 int
780 rte_eth_tx_queue_setup(uint8_t port_id, uint16_t tx_queue_id,
781                        uint16_t nb_tx_desc, unsigned int socket_id,
782                        const struct rte_eth_txconf *tx_conf)
783 {
784         struct rte_eth_dev *dev;
785
786         /* This function is only safe when called from the primary process
787          * in a multi-process setup*/
788         PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
789
790         if (port_id >= RTE_MAX_ETHPORTS || port_id >= nb_ports) {
791                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
792                 return (-EINVAL);
793         }
794         dev = &rte_eth_devices[port_id];
795         if (tx_queue_id >= dev->data->nb_tx_queues) {
796                 PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", tx_queue_id);
797                 return (-EINVAL);
798         }
799
800         if (dev->data->dev_started) {
801                 PMD_DEBUG_TRACE(
802                     "port %d must be stopped to allow configuration\n", port_id);
803                 return -EBUSY;
804         }
805
806         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_setup, -ENOTSUP);
807         return (*dev->dev_ops->tx_queue_setup)(dev, tx_queue_id, nb_tx_desc,
808                                                socket_id, tx_conf);
809 }
810
811 void
812 rte_eth_promiscuous_enable(uint8_t port_id)
813 {
814         struct rte_eth_dev *dev;
815
816         if (port_id >= nb_ports) {
817                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
818                 return;
819         }
820         dev = &rte_eth_devices[port_id];
821
822         FUNC_PTR_OR_RET(*dev->dev_ops->promiscuous_enable);
823         (*dev->dev_ops->promiscuous_enable)(dev);
824         dev->data->promiscuous = 1;
825 }
826
827 void
828 rte_eth_promiscuous_disable(uint8_t port_id)
829 {
830         struct rte_eth_dev *dev;
831
832         if (port_id >= nb_ports) {
833                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
834                 return;
835         }
836         dev = &rte_eth_devices[port_id];
837
838         FUNC_PTR_OR_RET(*dev->dev_ops->promiscuous_disable);
839         dev->data->promiscuous = 0;
840         (*dev->dev_ops->promiscuous_disable)(dev);
841 }
842
843 int
844 rte_eth_promiscuous_get(uint8_t port_id)
845 {
846         struct rte_eth_dev *dev;
847
848         if (port_id >= nb_ports) {
849                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
850                 return -1;
851         }
852
853         dev = &rte_eth_devices[port_id];
854         return dev->data->promiscuous;
855 }
856
857 void
858 rte_eth_allmulticast_enable(uint8_t port_id)
859 {
860         struct rte_eth_dev *dev;
861
862         if (port_id >= nb_ports) {
863                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
864                 return;
865         }
866         dev = &rte_eth_devices[port_id];
867
868         FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_enable);
869         (*dev->dev_ops->allmulticast_enable)(dev);
870         dev->data->all_multicast = 1;
871 }
872
873 void
874 rte_eth_allmulticast_disable(uint8_t port_id)
875 {
876         struct rte_eth_dev *dev;
877
878         if (port_id >= nb_ports) {
879                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
880                 return;
881         }
882         dev = &rte_eth_devices[port_id];
883
884         FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_disable);
885         dev->data->all_multicast = 0;
886         (*dev->dev_ops->allmulticast_disable)(dev);
887 }
888
889 int
890 rte_eth_allmulticast_get(uint8_t port_id)
891 {
892         struct rte_eth_dev *dev;
893
894         if (port_id >= nb_ports) {
895                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
896                 return -1;
897         }
898
899         dev = &rte_eth_devices[port_id];
900         return dev->data->all_multicast;
901 }
902
903 static inline int
904 rte_eth_dev_atomic_read_link_status(struct rte_eth_dev *dev,
905                                 struct rte_eth_link *link)
906 {
907         struct rte_eth_link *dst = link;
908         struct rte_eth_link *src = &(dev->data->dev_link);
909
910         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
911                                         *(uint64_t *)src) == 0)
912                 return -1;
913
914         return 0;
915 }
916
917 void
918 rte_eth_link_get(uint8_t port_id, struct rte_eth_link *eth_link)
919 {
920         struct rte_eth_dev *dev;
921
922         if (port_id >= nb_ports) {
923                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
924                 return;
925         }
926         dev = &rte_eth_devices[port_id];
927         FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
928
929         if (dev->data->dev_conf.intr_conf.lsc != 0)
930                 rte_eth_dev_atomic_read_link_status(dev, eth_link);
931         else {
932                 (*dev->dev_ops->link_update)(dev, 1);
933                 *eth_link = dev->data->dev_link;
934         }
935 }
936
937 void
938 rte_eth_link_get_nowait(uint8_t port_id, struct rte_eth_link *eth_link)
939 {
940         struct rte_eth_dev *dev;
941
942         if (port_id >= nb_ports) {
943                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
944                 return;
945         }
946         dev = &rte_eth_devices[port_id];
947         FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
948
949         if (dev->data->dev_conf.intr_conf.lsc != 0)
950                 rte_eth_dev_atomic_read_link_status(dev, eth_link);
951         else {
952                 (*dev->dev_ops->link_update)(dev, 0);
953                 *eth_link = dev->data->dev_link;
954         }
955 }
956
957 void
958 rte_eth_stats_get(uint8_t port_id, struct rte_eth_stats *stats)
959 {
960         struct rte_eth_dev *dev;
961
962         if (port_id >= nb_ports) {
963                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
964                 return;
965         }
966         dev = &rte_eth_devices[port_id];
967         memset(stats, 0, sizeof(*stats));
968
969         FUNC_PTR_OR_RET(*dev->dev_ops->stats_get);
970         (*dev->dev_ops->stats_get)(dev, stats);
971         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
972 }
973
974 void
975 rte_eth_stats_reset(uint8_t port_id)
976 {
977         struct rte_eth_dev *dev;
978
979         if (port_id >= nb_ports) {
980                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
981                 return;
982         }
983         dev = &rte_eth_devices[port_id];
984
985         FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
986         (*dev->dev_ops->stats_reset)(dev);
987 }
988
989
990 static int
991 set_queue_stats_mapping(uint8_t port_id, uint16_t queue_id, uint8_t stat_idx,
992                 uint8_t is_rx)
993 {
994         struct rte_eth_dev *dev;
995
996         if (port_id >= nb_ports) {
997                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
998                 return -ENODEV;
999         }
1000         dev = &rte_eth_devices[port_id];
1001
1002         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_stats_mapping_set, -ENOTSUP);
1003         return (*dev->dev_ops->queue_stats_mapping_set)
1004                         (dev, queue_id, stat_idx, is_rx);
1005 }
1006
1007
1008 int
1009 rte_eth_dev_set_tx_queue_stats_mapping(uint8_t port_id, uint16_t tx_queue_id,
1010                 uint8_t stat_idx)
1011 {
1012         return set_queue_stats_mapping(port_id, tx_queue_id, stat_idx,
1013                         STAT_QMAP_TX);
1014 }
1015
1016
1017 int
1018 rte_eth_dev_set_rx_queue_stats_mapping(uint8_t port_id, uint16_t rx_queue_id,
1019                 uint8_t stat_idx)
1020 {
1021         return set_queue_stats_mapping(port_id, rx_queue_id, stat_idx,
1022                         STAT_QMAP_RX);
1023 }
1024
1025
1026 void
1027 rte_eth_dev_info_get(uint8_t port_id, struct rte_eth_dev_info *dev_info)
1028 {
1029         struct rte_eth_dev *dev;
1030
1031         if (port_id >= nb_ports) {
1032                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1033                 return;
1034         }
1035         dev = &rte_eth_devices[port_id];
1036
1037         FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
1038         (*dev->dev_ops->dev_infos_get)(dev, dev_info);
1039         dev_info->pci_dev = dev->pci_dev;
1040         dev_info->driver_name = dev->driver->pci_drv.name;
1041 }
1042
1043 void
1044 rte_eth_macaddr_get(uint8_t port_id, struct ether_addr *mac_addr)
1045 {
1046         struct rte_eth_dev *dev;
1047
1048         if (port_id >= nb_ports) {
1049                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1050                 return;
1051         }
1052         dev = &rte_eth_devices[port_id];
1053         ether_addr_copy(&dev->data->mac_addrs[0], mac_addr);
1054 }
1055
1056 int
1057 rte_eth_dev_vlan_filter(uint8_t port_id, uint16_t vlan_id, int on)
1058 {
1059         struct rte_eth_dev *dev;
1060
1061         if (port_id >= nb_ports) {
1062                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1063                 return (-ENODEV);
1064         }
1065         dev = &rte_eth_devices[port_id];
1066         if (! (dev->data->dev_conf.rxmode.hw_vlan_filter)) {
1067                 PMD_DEBUG_TRACE("port %d: vlan-filtering disabled\n", port_id);
1068                 return (-ENOSYS);
1069         }
1070
1071         if (vlan_id > 4095) {
1072                 PMD_DEBUG_TRACE("(port_id=%d) invalid vlan_id=%u > 4095\n",
1073                                 port_id, (unsigned) vlan_id);
1074                 return (-EINVAL);
1075         }
1076         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_filter_set, -ENOTSUP);
1077         (*dev->dev_ops->vlan_filter_set)(dev, vlan_id, on);
1078         return (0);
1079 }
1080
1081 int
1082 rte_eth_dev_set_vlan_strip_on_queue(uint8_t port_id, uint16_t rx_queue_id, int on)
1083 {
1084         struct rte_eth_dev *dev;
1085
1086         if (port_id >= nb_ports) {
1087                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1088                 return (-ENODEV);
1089         }
1090
1091         dev = &rte_eth_devices[port_id];
1092         if (rx_queue_id >= dev->data->nb_rx_queues) {
1093                 PMD_DEBUG_TRACE("Invalid rx_queue_id=%d\n", port_id);
1094                 return (-EINVAL);
1095         }
1096
1097         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_strip_queue_set, -ENOTSUP);
1098         (*dev->dev_ops->vlan_strip_queue_set)(dev, rx_queue_id, on);
1099
1100         return (0);
1101 }
1102
1103 int
1104 rte_eth_dev_set_vlan_ether_type(uint8_t port_id, uint16_t tpid)
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 (-ENODEV);
1111         }
1112
1113         dev = &rte_eth_devices[port_id];
1114         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_tpid_set, -ENOTSUP);
1115         (*dev->dev_ops->vlan_tpid_set)(dev, tpid);
1116
1117         return (0);
1118 }
1119
1120 int
1121 rte_eth_dev_set_vlan_offload(uint8_t port_id, int offload_mask)
1122 {
1123         struct rte_eth_dev *dev;
1124         int ret = 0;
1125         int mask = 0;
1126         int cur, org = 0;
1127         
1128         if (port_id >= nb_ports) {
1129                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1130                 return (-ENODEV);
1131         }
1132
1133         dev = &rte_eth_devices[port_id];
1134
1135         /*check which option changed by application*/
1136         cur = !!(offload_mask & ETH_VLAN_STRIP_OFFLOAD);
1137         org = !!(dev->data->dev_conf.rxmode.hw_vlan_strip);
1138         if (cur != org){
1139                 dev->data->dev_conf.rxmode.hw_vlan_strip = (uint8_t)cur;
1140                 mask |= ETH_VLAN_STRIP_MASK;
1141         }
1142         
1143         cur = !!(offload_mask & ETH_VLAN_FILTER_OFFLOAD);
1144         org = !!(dev->data->dev_conf.rxmode.hw_vlan_filter);
1145         if (cur != org){
1146                 dev->data->dev_conf.rxmode.hw_vlan_filter = (uint8_t)cur;
1147                 mask |= ETH_VLAN_FILTER_MASK;
1148         }
1149
1150         cur = !!(offload_mask & ETH_VLAN_EXTEND_OFFLOAD);
1151         org = !!(dev->data->dev_conf.rxmode.hw_vlan_extend);
1152         if (cur != org){
1153                 dev->data->dev_conf.rxmode.hw_vlan_extend = (uint8_t)cur;
1154                 mask |= ETH_VLAN_EXTEND_MASK;
1155         }
1156
1157         /*no change*/
1158         if(mask == 0)
1159                 return ret;
1160         
1161         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_offload_set, -ENOTSUP);
1162         (*dev->dev_ops->vlan_offload_set)(dev, mask);
1163
1164         return ret;
1165 }
1166
1167 int
1168 rte_eth_dev_get_vlan_offload(uint8_t port_id)
1169 {
1170         struct rte_eth_dev *dev;
1171         int ret = 0;
1172
1173         if (port_id >= nb_ports) {
1174                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1175                 return (-ENODEV);
1176         }
1177
1178         dev = &rte_eth_devices[port_id];
1179
1180         if (dev->data->dev_conf.rxmode.hw_vlan_strip)
1181                 ret |= ETH_VLAN_STRIP_OFFLOAD ;
1182
1183         if (dev->data->dev_conf.rxmode.hw_vlan_filter)
1184                 ret |= ETH_VLAN_FILTER_OFFLOAD ;
1185
1186         if (dev->data->dev_conf.rxmode.hw_vlan_extend)
1187                 ret |= ETH_VLAN_EXTEND_OFFLOAD ;
1188
1189         return ret;
1190 }
1191
1192
1193 int
1194 rte_eth_dev_fdir_add_signature_filter(uint8_t port_id,
1195                                       struct rte_fdir_filter *fdir_filter,
1196                                       uint8_t queue)
1197 {
1198         struct rte_eth_dev *dev;
1199
1200         if (port_id >= nb_ports) {
1201                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1202                 return (-ENODEV);
1203         }
1204
1205         dev = &rte_eth_devices[port_id];
1206
1207         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
1208                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1209                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1210                 return (-ENOSYS);
1211         }
1212
1213         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1214              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1215             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1216                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1217                                 "None l4type, source & destinations ports " \
1218                                 "should be null!\n");
1219                 return (-EINVAL);
1220         }
1221
1222         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_add_signature_filter, -ENOTSUP);
1223         return (*dev->dev_ops->fdir_add_signature_filter)(dev, fdir_filter,
1224                                                                 queue);
1225 }
1226
1227 int
1228 rte_eth_dev_fdir_update_signature_filter(uint8_t port_id,
1229                                          struct rte_fdir_filter *fdir_filter,
1230                                          uint8_t queue)
1231 {
1232         struct rte_eth_dev *dev;
1233
1234         if (port_id >= nb_ports) {
1235                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1236                 return (-ENODEV);
1237         }
1238
1239         dev = &rte_eth_devices[port_id];
1240
1241         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
1242                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1243                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1244                 return (-ENOSYS);
1245         }
1246
1247         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1248              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1249             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1250                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1251                                 "None l4type, source & destinations ports " \
1252                                 "should be null!\n");
1253                 return (-EINVAL);
1254         }
1255
1256         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_update_signature_filter, -ENOTSUP);
1257         return (*dev->dev_ops->fdir_update_signature_filter)(dev, fdir_filter,
1258                                                                 queue);
1259
1260 }
1261
1262 int
1263 rte_eth_dev_fdir_remove_signature_filter(uint8_t port_id,
1264                                          struct rte_fdir_filter *fdir_filter)
1265 {
1266         struct rte_eth_dev *dev;
1267
1268         if (port_id >= nb_ports) {
1269                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1270                 return (-ENODEV);
1271         }
1272
1273         dev = &rte_eth_devices[port_id];
1274
1275         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
1276                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1277                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1278                 return (-ENOSYS);
1279         }
1280
1281         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1282              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1283             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1284                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1285                                 "None l4type source & destinations ports " \
1286                                 "should be null!\n");
1287                 return (-EINVAL);
1288         }
1289
1290         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_remove_signature_filter, -ENOTSUP);
1291         return (*dev->dev_ops->fdir_remove_signature_filter)(dev, fdir_filter);
1292 }
1293
1294 int
1295 rte_eth_dev_fdir_get_infos(uint8_t port_id, struct rte_eth_fdir *fdir)
1296 {
1297         struct rte_eth_dev *dev;
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         if (! (dev->data->dev_conf.fdir_conf.mode)) {
1306                 PMD_DEBUG_TRACE("port %d: pkt-filter disabled\n", port_id);
1307                 return (-ENOSYS);
1308         }
1309
1310         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_infos_get, -ENOTSUP);
1311
1312         (*dev->dev_ops->fdir_infos_get)(dev, fdir);
1313         return (0);
1314 }
1315
1316 int
1317 rte_eth_dev_fdir_add_perfect_filter(uint8_t port_id,
1318                                     struct rte_fdir_filter *fdir_filter,
1319                                     uint16_t soft_id, uint8_t queue,
1320                                     uint8_t drop)
1321 {
1322         struct rte_eth_dev *dev;
1323
1324         if (port_id >= nb_ports) {
1325                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1326                 return (-ENODEV);
1327         }
1328
1329         dev = &rte_eth_devices[port_id];
1330
1331         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
1332                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1333                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1334                 return (-ENOSYS);
1335         }
1336
1337         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1338              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1339             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1340                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1341                                 "None l4type, source & destinations ports " \
1342                                 "should be null!\n");
1343                 return (-EINVAL);
1344         }
1345
1346         /* For now IPv6 is not supported with perfect filter */
1347         if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
1348                 return (-ENOTSUP);
1349
1350         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_add_perfect_filter, -ENOTSUP);
1351         return (*dev->dev_ops->fdir_add_perfect_filter)(dev, fdir_filter,
1352                                                                 soft_id, queue,
1353                                                                 drop);
1354 }
1355
1356 int
1357 rte_eth_dev_fdir_update_perfect_filter(uint8_t port_id,
1358                                        struct rte_fdir_filter *fdir_filter,
1359                                        uint16_t soft_id, uint8_t queue,
1360                                        uint8_t drop)
1361 {
1362         struct rte_eth_dev *dev;
1363
1364         if (port_id >= nb_ports) {
1365                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1366                 return (-ENODEV);
1367         }
1368
1369         dev = &rte_eth_devices[port_id];
1370
1371         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
1372                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1373                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1374                 return (-ENOSYS);
1375         }
1376
1377         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1378              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1379             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1380                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1381                                 "None l4type, source & destinations ports " \
1382                                 "should be null!\n");
1383                 return (-EINVAL);
1384         }
1385
1386         /* For now IPv6 is not supported with perfect filter */
1387         if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
1388                 return (-ENOTSUP);
1389
1390         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_update_perfect_filter, -ENOTSUP);
1391         return (*dev->dev_ops->fdir_update_perfect_filter)(dev, fdir_filter,
1392                                                         soft_id, queue, drop);
1393 }
1394
1395 int
1396 rte_eth_dev_fdir_remove_perfect_filter(uint8_t port_id,
1397                                        struct rte_fdir_filter *fdir_filter,
1398                                        uint16_t soft_id)
1399 {
1400         struct rte_eth_dev *dev;
1401
1402         if (port_id >= nb_ports) {
1403                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1404                 return (-ENODEV);
1405         }
1406
1407         dev = &rte_eth_devices[port_id];
1408
1409         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
1410                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1411                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1412                 return (-ENOSYS);
1413         }
1414
1415         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1416              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1417             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1418                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1419                                 "None l4type, source & destinations ports " \
1420                                 "should be null!\n");
1421                 return (-EINVAL);
1422         }
1423
1424         /* For now IPv6 is not supported with perfect filter */
1425         if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
1426                 return (-ENOTSUP);
1427
1428         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_remove_perfect_filter, -ENOTSUP);
1429         return (*dev->dev_ops->fdir_remove_perfect_filter)(dev, fdir_filter,
1430                                                                 soft_id);
1431 }
1432
1433 int
1434 rte_eth_dev_fdir_set_masks(uint8_t port_id, struct rte_fdir_masks *fdir_mask)
1435 {
1436         struct rte_eth_dev *dev;
1437
1438         if (port_id >= nb_ports) {
1439                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1440                 return (-ENODEV);
1441         }
1442
1443         dev = &rte_eth_devices[port_id];
1444         if (! (dev->data->dev_conf.fdir_conf.mode)) {
1445                 PMD_DEBUG_TRACE("port %d: pkt-filter disabled\n", port_id);
1446                 return (-ENOSYS);
1447         }
1448
1449         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_set_masks, -ENOTSUP);
1450         return (*dev->dev_ops->fdir_set_masks)(dev, fdir_mask);
1451 }
1452
1453 int
1454 rte_eth_dev_flow_ctrl_set(uint8_t port_id, struct rte_eth_fc_conf *fc_conf)
1455 {
1456         struct rte_eth_dev *dev;
1457
1458         if (port_id >= nb_ports) {
1459                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1460                 return (-ENODEV);
1461         }
1462
1463         if ((fc_conf->send_xon != 0) && (fc_conf->send_xon != 1)) {
1464                 PMD_DEBUG_TRACE("Invalid send_xon, only 0/1 allowed\n");
1465                 return (-EINVAL);
1466         }
1467
1468         dev = &rte_eth_devices[port_id];
1469         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_set, -ENOTSUP);
1470         return (*dev->dev_ops->flow_ctrl_set)(dev, fc_conf);
1471 }
1472
1473 int
1474 rte_eth_dev_priority_flow_ctrl_set(uint8_t port_id, struct rte_eth_pfc_conf *pfc_conf)
1475 {
1476         struct rte_eth_dev *dev;
1477
1478         if (port_id >= nb_ports) {
1479                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1480                 return (-ENODEV);
1481         }
1482
1483         if (pfc_conf->priority > (ETH_DCB_NUM_USER_PRIORITIES - 1)) {
1484                 PMD_DEBUG_TRACE("Invalid priority, only 0-7 allowed\n");
1485                 return (-EINVAL);
1486         }
1487
1488         dev = &rte_eth_devices[port_id];
1489         /* High water, low water validation are device specific */
1490         if  (*dev->dev_ops->priority_flow_ctrl_set)
1491                 return (*dev->dev_ops->priority_flow_ctrl_set)(dev, pfc_conf);
1492         return (-ENOTSUP);
1493 }
1494
1495 int
1496 rte_eth_dev_rss_reta_update(uint8_t port_id, struct rte_eth_rss_reta *reta_conf)
1497 {
1498         struct rte_eth_dev *dev;
1499         uint8_t i,j;
1500
1501         if (port_id >= nb_ports) {
1502                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1503                 return (-ENODEV);
1504         }
1505
1506         /* Invalid mask bit(s) setting */
1507         if ((reta_conf->mask_lo == 0) && (reta_conf->mask_hi == 0)) {
1508                 PMD_DEBUG_TRACE("Invalid update mask bits for port=%d\n",port_id);
1509                 return (-EINVAL);
1510         }
1511
1512         if (reta_conf->mask_lo != 0) {
1513                 for (i = 0; i < ETH_RSS_RETA_NUM_ENTRIES/2; i++) {
1514                         if ((reta_conf->mask_lo & (1ULL << i)) &&
1515                                 (reta_conf->reta[i] >= ETH_RSS_RETA_MAX_QUEUE)) {
1516                                 PMD_DEBUG_TRACE("RETA hash index output"
1517                                         "configration for port=%d,invalid"
1518                                         "queue=%d\n",port_id,reta_conf->reta[i]);
1519
1520                                 return (-EINVAL);
1521                         } 
1522                 }
1523         }
1524
1525         if (reta_conf->mask_hi != 0) {
1526                 for (i = 0; i< ETH_RSS_RETA_NUM_ENTRIES/2; i++) {       
1527                         j = (uint8_t)(i + ETH_RSS_RETA_NUM_ENTRIES/2);
1528
1529                         /* Check if the max entry >= 128 */
1530                         if ((reta_conf->mask_hi & (1ULL << i)) && 
1531                                 (reta_conf->reta[j] >= ETH_RSS_RETA_MAX_QUEUE)) {
1532                                 PMD_DEBUG_TRACE("RETA hash index output"
1533                                         "configration for port=%d,invalid"
1534                                         "queue=%d\n",port_id,reta_conf->reta[j]);
1535
1536                                 return (-EINVAL);
1537                         }
1538                 }
1539         }
1540
1541         dev = &rte_eth_devices[port_id];
1542
1543         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->reta_update, -ENOTSUP);
1544         return (*dev->dev_ops->reta_update)(dev, reta_conf);
1545 }
1546
1547 int 
1548 rte_eth_dev_rss_reta_query(uint8_t port_id, struct rte_eth_rss_reta *reta_conf)
1549 {
1550         struct rte_eth_dev *dev;
1551         
1552         if (port_id >= nb_ports) {
1553                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1554                 return (-ENODEV);
1555         }
1556
1557         if((reta_conf->mask_lo == 0) && (reta_conf->mask_hi == 0)) {
1558                 PMD_DEBUG_TRACE("Invalid update mask bits for the port=%d\n",port_id);
1559                 return (-EINVAL);
1560         }
1561
1562         dev = &rte_eth_devices[port_id];
1563         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->reta_query, -ENOTSUP);
1564         return (*dev->dev_ops->reta_query)(dev, reta_conf);
1565 }
1566
1567 int
1568 rte_eth_led_on(uint8_t port_id)
1569 {
1570         struct rte_eth_dev *dev;
1571
1572         if (port_id >= nb_ports) {
1573                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1574                 return (-ENODEV);
1575         }
1576
1577         dev = &rte_eth_devices[port_id];
1578         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_on, -ENOTSUP);
1579         return ((*dev->dev_ops->dev_led_on)(dev));
1580 }
1581
1582 int
1583 rte_eth_led_off(uint8_t port_id)
1584 {
1585         struct rte_eth_dev *dev;
1586
1587         if (port_id >= nb_ports) {
1588                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1589                 return (-ENODEV);
1590         }
1591
1592         dev = &rte_eth_devices[port_id];
1593         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_off, -ENOTSUP);
1594         return ((*dev->dev_ops->dev_led_off)(dev));
1595 }
1596
1597 /*
1598  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
1599  * an empty spot.
1600  */
1601 static inline int
1602 get_mac_addr_index(uint8_t port_id, struct ether_addr *addr)
1603 {
1604         struct rte_eth_dev_info dev_info;
1605         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1606         unsigned i;
1607
1608         rte_eth_dev_info_get(port_id, &dev_info);
1609
1610         for (i = 0; i < dev_info.max_mac_addrs; i++)
1611                 if (memcmp(addr, &dev->data->mac_addrs[i], ETHER_ADDR_LEN) == 0)
1612                         return i;
1613
1614         return -1;
1615 }
1616
1617 static struct ether_addr null_mac_addr = {{0, 0, 0, 0, 0, 0}};
1618
1619 int
1620 rte_eth_dev_mac_addr_add(uint8_t port_id, struct ether_addr *addr,
1621                         uint32_t pool)
1622 {
1623         struct rte_eth_dev *dev;
1624         int index;
1625         uint64_t pool_mask;
1626
1627         if (port_id >= nb_ports) {
1628                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1629                 return (-ENODEV);
1630         }
1631         dev = &rte_eth_devices[port_id];
1632         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_add, -ENOTSUP);
1633
1634         if (is_zero_ether_addr(addr)) {
1635                 PMD_DEBUG_TRACE("port %d: Cannot add NULL MAC address\n", 
1636                         port_id);
1637                 return (-EINVAL);
1638         }
1639         if (pool >= ETH_64_POOLS) {
1640                 PMD_DEBUG_TRACE("pool id must be 0-%d\n",ETH_64_POOLS - 1);
1641                 return (-EINVAL);
1642         }
1643         
1644         index = get_mac_addr_index(port_id, addr);
1645         if (index < 0) {
1646                 index = get_mac_addr_index(port_id, &null_mac_addr);
1647                 if (index < 0) {
1648                         PMD_DEBUG_TRACE("port %d: MAC address array full\n",
1649                                 port_id);
1650                         return (-ENOSPC);
1651                 }
1652         } else {
1653                 pool_mask = dev->data->mac_pool_sel[index];
1654                 
1655                 /* Check if both MAC address and pool is alread there, and do nothing */
1656                 if (pool_mask & (1ULL << pool))
1657                         return 0;
1658         }
1659
1660         /* Update NIC */
1661         (*dev->dev_ops->mac_addr_add)(dev, addr, index, pool);
1662
1663         /* Update address in NIC data structure */
1664         ether_addr_copy(addr, &dev->data->mac_addrs[index]);
1665         
1666         /* Update pool bitmap in NIC data structure */
1667         dev->data->mac_pool_sel[index] |= (1ULL << pool);
1668
1669         return 0;
1670 }
1671
1672 int
1673 rte_eth_dev_mac_addr_remove(uint8_t port_id, struct ether_addr *addr)
1674 {
1675         struct rte_eth_dev *dev;
1676         int index;
1677
1678         if (port_id >= nb_ports) {
1679                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1680                 return (-ENODEV);
1681         }
1682         dev = &rte_eth_devices[port_id];
1683         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_remove, -ENOTSUP);
1684
1685         index = get_mac_addr_index(port_id, addr);
1686         if (index == 0) {
1687                 PMD_DEBUG_TRACE("port %d: Cannot remove default MAC address\n", port_id);
1688                 return (-EADDRINUSE);
1689         } else if (index < 0)
1690                 return 0;  /* Do nothing if address wasn't found */
1691
1692         /* Update NIC */
1693         (*dev->dev_ops->mac_addr_remove)(dev, index);
1694
1695         /* Update address in NIC data structure */
1696         ether_addr_copy(&null_mac_addr, &dev->data->mac_addrs[index]);
1697
1698         return 0;
1699 }
1700
1701 int 
1702 rte_eth_dev_set_vf_rxmode(uint8_t port_id,  uint16_t vf,
1703                                 uint16_t rx_mode, uint8_t on)
1704 {
1705         uint16_t num_vfs;
1706         struct rte_eth_dev *dev;
1707         struct rte_eth_dev_info dev_info;
1708
1709         if (port_id >= nb_ports) {
1710                 PMD_DEBUG_TRACE("set VF RX mode:Invalid port_id=%d\n",
1711                                 port_id);
1712                 return (-ENODEV);
1713         }       
1714         
1715         dev = &rte_eth_devices[port_id];
1716         rte_eth_dev_info_get(port_id, &dev_info);
1717
1718         num_vfs = dev_info.max_vfs;
1719         if (vf > num_vfs)
1720         {
1721                 PMD_DEBUG_TRACE("set VF RX mode:invalid VF id %d\n", vf);
1722                 return (-EINVAL);
1723         }
1724         if (rx_mode == 0)
1725         {
1726                 PMD_DEBUG_TRACE("set VF RX mode:mode mask ca not be zero\n");
1727                 return (-EINVAL);       
1728         }
1729         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_vf_rx_mode, -ENOTSUP);
1730         return (*dev->dev_ops->set_vf_rx_mode)(dev, vf, rx_mode, on);
1731 }
1732
1733 /*
1734  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
1735  * an empty spot.
1736  */
1737 static inline int
1738 get_hash_mac_addr_index(uint8_t port_id, struct ether_addr *addr)
1739 {
1740         struct rte_eth_dev_info dev_info;
1741         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1742         unsigned i;
1743
1744         rte_eth_dev_info_get(port_id, &dev_info);
1745         if (!dev->data->hash_mac_addrs)
1746                 return -1;
1747
1748         for (i = 0; i < dev_info.max_hash_mac_addrs; i++)
1749                 if (memcmp(addr, &dev->data->hash_mac_addrs[i],
1750                         ETHER_ADDR_LEN) == 0)
1751                         return i;
1752
1753         return -1;
1754 }
1755
1756 int
1757 rte_eth_dev_uc_hash_table_set(uint8_t port_id, struct ether_addr *addr,
1758                                 uint8_t on)
1759 {
1760         int index;
1761         int ret;
1762         struct rte_eth_dev *dev;
1763         
1764         if (port_id >= nb_ports) {
1765                 PMD_DEBUG_TRACE("unicast hash setting:Invalid port_id=%d\n",
1766                         port_id);
1767                 return (-ENODEV);
1768         }
1769         
1770         dev = &rte_eth_devices[port_id];
1771         if (is_zero_ether_addr(addr)) {
1772                 PMD_DEBUG_TRACE("port %d: Cannot add NULL MAC address\n", 
1773                         port_id);
1774                 return (-EINVAL);
1775         }
1776
1777         index = get_hash_mac_addr_index(port_id, addr);
1778         /* Check if it's already there, and do nothing */
1779         if ((index >= 0) && (on))
1780                 return 0;
1781         
1782         if (index < 0) {
1783                 if (!on) {
1784                         PMD_DEBUG_TRACE("port %d: the MAC address was not" 
1785                                 "set in UTA\n", port_id);
1786                         return (-EINVAL);
1787                 }
1788                         
1789                 index = get_hash_mac_addr_index(port_id, &null_mac_addr);
1790                 if (index < 0) {
1791                         PMD_DEBUG_TRACE("port %d: MAC address array full\n",
1792                                         port_id);
1793                         return (-ENOSPC);
1794                 }
1795         } 
1796          
1797         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->uc_hash_table_set, -ENOTSUP);
1798         ret = (*dev->dev_ops->uc_hash_table_set)(dev, addr, on);
1799         if (ret == 0) {
1800                 /* Update address in NIC data structure */
1801                 if (on)
1802                         ether_addr_copy(addr,
1803                                         &dev->data->hash_mac_addrs[index]);
1804                 else 
1805                         ether_addr_copy(&null_mac_addr,
1806                                         &dev->data->hash_mac_addrs[index]);
1807         }
1808         
1809         return ret;
1810 }
1811
1812 int
1813 rte_eth_dev_uc_all_hash_table_set(uint8_t port_id, uint8_t on)
1814 {
1815         struct rte_eth_dev *dev;
1816         
1817         if (port_id >= nb_ports) {
1818                 PMD_DEBUG_TRACE("unicast hash setting:Invalid port_id=%d\n",
1819                         port_id);
1820                 return (-ENODEV);
1821         }
1822         
1823         dev = &rte_eth_devices[port_id];
1824
1825         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->uc_all_hash_table_set, -ENOTSUP);
1826         return (*dev->dev_ops->uc_all_hash_table_set)(dev, on);
1827 }
1828
1829 int 
1830 rte_eth_dev_set_vf_rx(uint8_t port_id,uint16_t vf, uint8_t on)
1831 {
1832         uint16_t num_vfs;
1833         struct rte_eth_dev *dev;
1834         struct rte_eth_dev_info dev_info;
1835
1836         if (port_id >= nb_ports) {
1837                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1838                 return (-ENODEV);
1839         }
1840         
1841         dev = &rte_eth_devices[port_id];
1842         rte_eth_dev_info_get(port_id, &dev_info);
1843         
1844         num_vfs = dev_info.max_vfs;
1845         if (vf > num_vfs) 
1846         {
1847                 PMD_DEBUG_TRACE("port %d: invalid vf id\n", port_id);
1848                 return (-EINVAL);
1849         }       
1850         
1851         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_vf_rx, -ENOTSUP);
1852         return (*dev->dev_ops->set_vf_rx)(dev, vf,on);
1853 }
1854
1855 int 
1856 rte_eth_dev_set_vf_tx(uint8_t port_id,uint16_t vf, uint8_t on)
1857 {
1858         uint16_t num_vfs;
1859         struct rte_eth_dev *dev;
1860         struct rte_eth_dev_info dev_info;
1861
1862         if (port_id >= nb_ports) {
1863                 PMD_DEBUG_TRACE("set pool tx:Invalid port_id=%d\n", port_id);
1864                 return (-ENODEV);
1865         }
1866         
1867         dev = &rte_eth_devices[port_id];
1868         rte_eth_dev_info_get(port_id, &dev_info);
1869
1870         num_vfs = dev_info.max_vfs;
1871         if (vf > num_vfs) 
1872         {
1873                 PMD_DEBUG_TRACE("set pool tx:invalid pool id=%d\n", vf);
1874                 return (-EINVAL);
1875         }
1876         
1877         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_vf_tx, -ENOTSUP);
1878         return (*dev->dev_ops->set_vf_tx)(dev, vf,on);
1879 }
1880
1881 int
1882 rte_eth_dev_set_vf_vlan_filter(uint8_t port_id, uint16_t vlan_id, 
1883                                  uint64_t vf_mask,uint8_t vlan_on)
1884 {
1885         struct rte_eth_dev *dev;
1886
1887         if (port_id >= nb_ports) {
1888                 PMD_DEBUG_TRACE("VF VLAN filter:invalid port id=%d\n",
1889                                 port_id);
1890                 return (-ENODEV);
1891         }
1892         dev = &rte_eth_devices[port_id];
1893
1894         if(vlan_id > ETHER_MAX_VLAN_ID)
1895         {
1896                 PMD_DEBUG_TRACE("VF VLAN filter:invalid VLAN id=%d\n",
1897                         vlan_id);
1898                 return (-EINVAL);
1899         }
1900         if (vf_mask == 0)
1901         {
1902                 PMD_DEBUG_TRACE("VF VLAN filter:pool_mask can not be 0\n");
1903                 return (-EINVAL);
1904         }
1905         
1906         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_vf_vlan_filter, -ENOTSUP);
1907         return (*dev->dev_ops->set_vf_vlan_filter)(dev, vlan_id,
1908                                                 vf_mask,vlan_on);
1909 }
1910
1911 int
1912 rte_eth_mirror_rule_set(uint8_t port_id, 
1913                         struct rte_eth_vmdq_mirror_conf *mirror_conf,
1914                         uint8_t rule_id, uint8_t on)
1915 {
1916         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1917
1918         if (port_id >= nb_ports) {
1919                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1920                 return (-ENODEV);
1921         }
1922         
1923         if (mirror_conf->rule_type_mask == 0) {
1924                 PMD_DEBUG_TRACE("mirror rule type can not be 0.\n");
1925                 return (-EINVAL);
1926         }
1927         
1928         if (mirror_conf->dst_pool >= ETH_64_POOLS) {
1929                 PMD_DEBUG_TRACE("Invalid dst pool, pool id must"
1930                         "be 0-%d\n",ETH_64_POOLS - 1);
1931                 return (-EINVAL);
1932         }
1933         
1934         if ((mirror_conf->rule_type_mask & ETH_VMDQ_POOL_MIRROR) && 
1935                 (mirror_conf->pool_mask == 0)) {
1936                 PMD_DEBUG_TRACE("Invalid mirror pool, pool mask can not"
1937                                 "be 0.\n");             
1938                 return (-EINVAL);
1939         }
1940         
1941         if(rule_id >= ETH_VMDQ_NUM_MIRROR_RULE)
1942         {
1943                 PMD_DEBUG_TRACE("Invalid rule_id, rule_id must be 0-%d\n",
1944                         ETH_VMDQ_NUM_MIRROR_RULE - 1);
1945                 return (-EINVAL);
1946         }
1947
1948         dev = &rte_eth_devices[port_id];
1949         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mirror_rule_set, -ENOTSUP);
1950
1951         return (*dev->dev_ops->mirror_rule_set)(dev, mirror_conf, rule_id, on);
1952 }
1953
1954 int
1955 rte_eth_mirror_rule_reset(uint8_t port_id, uint8_t rule_id)
1956 {
1957         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1958
1959         if (port_id >= nb_ports) {
1960                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1961                 return (-ENODEV);
1962         }
1963
1964         if(rule_id >= ETH_VMDQ_NUM_MIRROR_RULE)
1965         {
1966                 PMD_DEBUG_TRACE("Invalid rule_id, rule_id must be 0-%d\n",
1967                         ETH_VMDQ_NUM_MIRROR_RULE-1);
1968                 return (-EINVAL);
1969         }
1970
1971         dev = &rte_eth_devices[port_id];
1972         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mirror_rule_reset, -ENOTSUP);
1973
1974         return (*dev->dev_ops->mirror_rule_reset)(dev, rule_id);
1975 }
1976
1977 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
1978 uint16_t
1979 rte_eth_rx_burst(uint8_t port_id, uint16_t queue_id,
1980                  struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1981 {
1982         struct rte_eth_dev *dev;
1983
1984         if (port_id >= nb_ports) {
1985                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1986                 return 0;
1987         }
1988         dev = &rte_eth_devices[port_id];
1989         FUNC_PTR_OR_ERR_RET(*dev->rx_pkt_burst, -ENOTSUP);
1990         if (queue_id >= dev->data->nb_rx_queues) {
1991                 PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", queue_id);
1992                 return 0;
1993         }
1994         return (*dev->rx_pkt_burst)(dev->data->rx_queues[queue_id],
1995                                                 rx_pkts, nb_pkts);
1996 }
1997
1998 uint16_t
1999 rte_eth_tx_burst(uint8_t port_id, uint16_t queue_id,
2000                  struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
2001 {
2002         struct rte_eth_dev *dev;
2003
2004         if (port_id >= nb_ports) {
2005                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
2006                 return 0;
2007         }
2008         dev = &rte_eth_devices[port_id];
2009
2010         FUNC_PTR_OR_ERR_RET(*dev->tx_pkt_burst, -ENOTSUP);
2011         if (queue_id >= dev->data->nb_tx_queues) {
2012                 PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", queue_id);
2013                 return 0;
2014         }
2015         return (*dev->tx_pkt_burst)(dev->data->tx_queues[queue_id],
2016                                                 tx_pkts, nb_pkts);
2017 }
2018
2019 uint32_t
2020 rte_eth_rx_queue_count(uint8_t port_id, uint16_t queue_id)
2021 {
2022         struct rte_eth_dev *dev;
2023
2024         if (port_id >= nb_ports) {
2025                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
2026                 return 0;
2027         }
2028         dev = &rte_eth_devices[port_id];
2029         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_count, -ENOTSUP);
2030         return (*dev->dev_ops->rx_queue_count)(dev, queue_id);  
2031 }
2032
2033 int
2034 rte_eth_rx_descriptor_done(uint8_t port_id, uint16_t queue_id, uint16_t offset)
2035 {
2036         struct rte_eth_dev *dev;
2037
2038         if (port_id >= nb_ports) {
2039                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
2040                 return (-ENODEV);
2041         }
2042         dev = &rte_eth_devices[port_id];
2043         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_descriptor_done, -ENOTSUP);
2044         return (*dev->dev_ops->rx_descriptor_done)( \
2045                 dev->data->rx_queues[queue_id], offset);
2046 }
2047 #endif
2048
2049 int
2050 rte_eth_dev_callback_register(uint8_t port_id,
2051                         enum rte_eth_event_type event,
2052                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
2053 {
2054         struct rte_eth_dev *dev;
2055         struct rte_eth_dev_callback *user_cb;
2056
2057         if (!cb_fn)
2058                 return (-EINVAL);
2059         if (port_id >= nb_ports) {
2060                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
2061                 return (-EINVAL);
2062         }
2063
2064         dev = &rte_eth_devices[port_id];
2065         rte_spinlock_lock(&rte_eth_dev_cb_lock);
2066
2067         TAILQ_FOREACH(user_cb, &(dev->callbacks), next) {
2068                 if (user_cb->cb_fn == cb_fn &&
2069                         user_cb->cb_arg == cb_arg &&
2070                         user_cb->event == event) {
2071                         break;
2072                 }
2073         }
2074
2075         /* create a new callback. */
2076         if (user_cb == NULL && (user_cb = rte_zmalloc("INTR_USER_CALLBACK",
2077                         sizeof(struct rte_eth_dev_callback), 0)) != NULL) {
2078                 user_cb->cb_fn = cb_fn;
2079                 user_cb->cb_arg = cb_arg;
2080                 user_cb->event = event;
2081                 TAILQ_INSERT_TAIL(&(dev->callbacks), user_cb, next);
2082         }
2083
2084         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2085         return ((user_cb == NULL) ? -ENOMEM : 0);
2086 }
2087
2088 int
2089 rte_eth_dev_callback_unregister(uint8_t port_id,
2090                         enum rte_eth_event_type event,
2091                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
2092 {
2093         int ret;
2094         struct rte_eth_dev *dev;
2095         struct rte_eth_dev_callback *cb, *next;
2096
2097         if (!cb_fn)
2098                 return (-EINVAL);
2099         if (port_id >= nb_ports) {
2100                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
2101                 return (-EINVAL);
2102         }
2103
2104         dev = &rte_eth_devices[port_id];
2105         rte_spinlock_lock(&rte_eth_dev_cb_lock);
2106
2107         ret = 0;
2108         for (cb = TAILQ_FIRST(&dev->callbacks); cb != NULL; cb = next) {
2109
2110                 next = TAILQ_NEXT(cb, next);
2111
2112                 if (cb->cb_fn != cb_fn || cb->event != event ||
2113                                 (cb->cb_arg != (void *)-1 &&
2114                                 cb->cb_arg != cb_arg))
2115                         continue;
2116
2117                 /*
2118                  * if this callback is not executing right now,
2119                  * then remove it.
2120                  */
2121                 if (cb->active == 0) {
2122                         TAILQ_REMOVE(&(dev->callbacks), cb, next);
2123                         rte_free(cb);
2124                 } else {
2125                         ret = -EAGAIN;
2126                 }
2127         }
2128
2129         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2130         return (ret);
2131 }
2132
2133 void
2134 _rte_eth_dev_callback_process(struct rte_eth_dev *dev,
2135         enum rte_eth_event_type event)
2136 {
2137         struct rte_eth_dev_callback *cb_lst;
2138         struct rte_eth_dev_callback dev_cb;
2139
2140         rte_spinlock_lock(&rte_eth_dev_cb_lock);
2141         TAILQ_FOREACH(cb_lst, &(dev->callbacks), next) {
2142                 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
2143                         continue;
2144                 dev_cb = *cb_lst;
2145                 cb_lst->active = 1;
2146                 rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2147                 dev_cb.cb_fn(dev->data->port_id, dev_cb.event,
2148                                                 dev_cb.cb_arg);
2149                 rte_spinlock_lock(&rte_eth_dev_cb_lock);
2150                 cb_lst->active = 0;
2151         }
2152         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2153 }