eal: fix printf format
[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
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=%hhu SRIOV active, "
347                                         "wrong VMDQ mq_mode rx %u tx %u\n", 
348                                         port_id,
349                                         dev_conf->rxmode.mq_mode,
350                                         dev_conf->txmode.mq_mode);
351                         return (-EINVAL);
352                 }
353
354                 switch (dev_conf->rxmode.mq_mode) {
355                 case ETH_MQ_RX_VMDQ_RSS:
356                 case ETH_MQ_RX_VMDQ_DCB:
357                 case ETH_MQ_RX_VMDQ_DCB_RSS:
358                         /* DCB/RSS VMDQ in SRIOV mode, not implement yet */
359                         PMD_DEBUG_TRACE("ethdev port_id=%hhu SRIOV active, "
360                                         "unsupported VMDQ mq_mode rx %u\n", 
361                                         port_id, dev_conf->rxmode.mq_mode);
362                         return (-EINVAL);
363                 default: /* ETH_MQ_RX_VMDQ_ONLY or ETH_MQ_RX_NONE */
364                         /* if nothing mq mode configure, use default scheme */
365                         dev->data->dev_conf.rxmode.mq_mode = ETH_MQ_RX_VMDQ_ONLY;
366                         if (RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool > 1)
367                                 RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool = 1;
368                         break;
369                 }
370
371                 switch (dev_conf->txmode.mq_mode) {
372                 case ETH_MQ_TX_VMDQ_DCB:
373                         /* DCB VMDQ in SRIOV mode, not implement yet */
374                         PMD_DEBUG_TRACE("ethdev port_id=%hhu SRIOV active, "
375                                         "unsupported VMDQ mq_mode tx %u\n", 
376                                         port_id, dev_conf->txmode.mq_mode);
377                         return (-EINVAL);
378                 default: /* ETH_MQ_TX_VMDQ_ONLY or ETH_MQ_TX_NONE */
379                         /* if nothing mq mode configure, use default scheme */
380                         dev->data->dev_conf.txmode.mq_mode = ETH_MQ_TX_VMDQ_ONLY;
381                         if (RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool > 1)
382                                 RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool = 1;
383                         break;
384                 }
385
386                 /* check valid queue number */
387                 if ((nb_rx_q > RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool) ||
388                     (nb_tx_q > RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool)) {
389                         PMD_DEBUG_TRACE("ethdev port_id=%d SRIOV active, "
390                                     "queue number must less equal to %d\n", 
391                                         port_id, RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool);
392                         return (-EINVAL);
393                 }
394         } else {
395                 /* For vmdb+dcb mode check our configuration before we go further */
396                 if (dev_conf->rxmode.mq_mode == ETH_MQ_RX_VMDQ_DCB) {
397                         const struct rte_eth_vmdq_dcb_conf *conf;
398                         
399                         if (nb_rx_q != ETH_VMDQ_DCB_NUM_QUEUES) {
400                                 PMD_DEBUG_TRACE("ethdev port_id=%d VMDQ+DCB, nb_rx_q "
401                                                 "!= %d\n",
402                                                 port_id, ETH_VMDQ_DCB_NUM_QUEUES);
403                                 return (-EINVAL);
404                         }
405                         conf = &(dev_conf->rx_adv_conf.vmdq_dcb_conf);
406                         if (! (conf->nb_queue_pools == ETH_16_POOLS ||
407                                conf->nb_queue_pools == ETH_32_POOLS)) {
408                                 PMD_DEBUG_TRACE("ethdev port_id=%d VMDQ+DCB selected, "
409                                                 "nb_queue_pools must be %d or %d\n",
410                                                 port_id, ETH_16_POOLS, ETH_32_POOLS);
411                                 return (-EINVAL);
412                         }
413                 }
414                 if (dev_conf->txmode.mq_mode == ETH_MQ_TX_VMDQ_DCB) {
415                         const struct rte_eth_vmdq_dcb_tx_conf *conf;
416                         
417                         if (nb_tx_q != ETH_VMDQ_DCB_NUM_QUEUES) {
418                                 PMD_DEBUG_TRACE("ethdev port_id=%d VMDQ+DCB, nb_tx_q "
419                                                 "!= %d\n",
420                                                 port_id, ETH_VMDQ_DCB_NUM_QUEUES);
421                                 return (-EINVAL);
422                         }
423                         conf = &(dev_conf->tx_adv_conf.vmdq_dcb_tx_conf);
424                         if (! (conf->nb_queue_pools == ETH_16_POOLS ||
425                                conf->nb_queue_pools == ETH_32_POOLS)) {
426                                 PMD_DEBUG_TRACE("ethdev port_id=%d VMDQ+DCB selected, "
427                                                 "nb_queue_pools != %d or nb_queue_pools "
428                                                 "!= %d\n",
429                                                 port_id, ETH_16_POOLS, ETH_32_POOLS);
430                                 return (-EINVAL);
431                         }
432                 }
433                 
434                 /* For DCB mode check our configuration before we go further */
435                 if (dev_conf->rxmode.mq_mode == ETH_MQ_RX_DCB) {
436                         const struct rte_eth_dcb_rx_conf *conf;
437                         
438                         if (nb_rx_q != ETH_DCB_NUM_QUEUES) {
439                                 PMD_DEBUG_TRACE("ethdev port_id=%d DCB, nb_rx_q "
440                                                 "!= %d\n",
441                                                 port_id, ETH_DCB_NUM_QUEUES);
442                                 return (-EINVAL);
443                         }
444                         conf = &(dev_conf->rx_adv_conf.dcb_rx_conf);
445                         if (! (conf->nb_tcs == ETH_4_TCS ||
446                                conf->nb_tcs == ETH_8_TCS)) {
447                                 PMD_DEBUG_TRACE("ethdev port_id=%d DCB selected, "
448                                                 "nb_tcs != %d or nb_tcs "
449                                                 "!= %d\n",
450                                                 port_id, ETH_4_TCS, ETH_8_TCS);
451                                 return (-EINVAL);
452                         }
453                 }
454                 
455                 if (dev_conf->txmode.mq_mode == ETH_MQ_TX_DCB) {
456                         const struct rte_eth_dcb_tx_conf *conf;
457                         
458                         if (nb_tx_q != ETH_DCB_NUM_QUEUES) {
459                                 PMD_DEBUG_TRACE("ethdev port_id=%d DCB, nb_tx_q "
460                                                 "!= %d\n",
461                                                 port_id, ETH_DCB_NUM_QUEUES);
462                                 return (-EINVAL);
463                         }
464                         conf = &(dev_conf->tx_adv_conf.dcb_tx_conf);
465                         if (! (conf->nb_tcs == ETH_4_TCS ||
466                                conf->nb_tcs == ETH_8_TCS)) {
467                                 PMD_DEBUG_TRACE("ethdev port_id=%d DCB selected, "
468                                                 "nb_tcs != %d or nb_tcs "
469                                                 "!= %d\n",
470                                                 port_id, ETH_4_TCS, ETH_8_TCS);
471                                 return (-EINVAL);
472                         }
473                 }
474         }
475         return 0;
476 }
477
478 int
479 rte_eth_dev_configure(uint8_t port_id, uint16_t nb_rx_q, uint16_t nb_tx_q,
480                       const struct rte_eth_conf *dev_conf)
481 {
482         struct rte_eth_dev *dev;
483         struct rte_eth_dev_info dev_info;
484         int diag;
485
486         /* This function is only safe when called from the primary process
487          * in a multi-process setup*/
488         PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
489
490         if (port_id >= nb_ports || port_id >= RTE_MAX_ETHPORTS) {
491                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
492                 return (-EINVAL);
493         }
494         dev = &rte_eth_devices[port_id];
495
496         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
497         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_configure, -ENOTSUP);
498
499         if (dev->data->dev_started) {
500                 PMD_DEBUG_TRACE(
501                     "port %d must be stopped to allow configuration\n", port_id);
502                 return (-EBUSY);
503         }
504
505         /*
506          * Check that the numbers of RX and TX queues are not greater
507          * than the maximum number of RX and TX queues supported by the
508          * configured device.
509          */
510         (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
511         if (nb_rx_q > dev_info.max_rx_queues) {
512                 PMD_DEBUG_TRACE("ethdev port_id=%d nb_rx_queues=%d > %d\n",
513                                 port_id, nb_rx_q, dev_info.max_rx_queues);
514                 return (-EINVAL);
515         }
516         if (nb_rx_q == 0) {
517                 PMD_DEBUG_TRACE("ethdev port_id=%d nb_rx_q == 0\n", port_id);
518                 return (-EINVAL);
519         }
520
521         if (nb_tx_q > dev_info.max_tx_queues) {
522                 PMD_DEBUG_TRACE("ethdev port_id=%d nb_tx_queues=%d > %d\n",
523                                 port_id, nb_tx_q, dev_info.max_tx_queues);
524                 return (-EINVAL);
525         }
526         if (nb_tx_q == 0) {
527                 PMD_DEBUG_TRACE("ethdev port_id=%d nb_tx_q == 0\n", port_id);
528                 return (-EINVAL);
529         }
530
531         /* Copy the dev_conf parameter into the dev structure */
532         memcpy(&dev->data->dev_conf, dev_conf, sizeof(dev->data->dev_conf));
533
534         /*
535          * If jumbo frames are enabled, check that the maximum RX packet
536          * length is supported by the configured device.
537          */
538         if (dev_conf->rxmode.jumbo_frame == 1) {
539                 if (dev_conf->rxmode.max_rx_pkt_len >
540                     dev_info.max_rx_pktlen) {
541                         PMD_DEBUG_TRACE("ethdev port_id=%d max_rx_pkt_len %u"
542                                 " > max valid value %u\n",
543                                 port_id,
544                                 (unsigned)dev_conf->rxmode.max_rx_pkt_len,
545                                 (unsigned)dev_info.max_rx_pktlen);
546                         return (-EINVAL);
547                 }
548                 else if (dev_conf->rxmode.max_rx_pkt_len < ETHER_MIN_LEN) {
549                         PMD_DEBUG_TRACE("ethdev port_id=%d max_rx_pkt_len %u"
550                                 " < min valid value %u\n",
551                                 port_id,
552                                 (unsigned)dev_conf->rxmode.max_rx_pkt_len,
553                                 (unsigned)ETHER_MIN_LEN);
554                         return (-EINVAL);
555                 }
556         } else
557                 /* Use default value */
558                 dev->data->dev_conf.rxmode.max_rx_pkt_len = ETHER_MAX_LEN;
559
560         /* multipe queue mode checking */
561         diag = rte_eth_dev_check_mq_mode(port_id, nb_rx_q, nb_tx_q, dev_conf);
562         if (diag != 0) {
563                 PMD_DEBUG_TRACE("port%d rte_eth_dev_check_mq_mode = %d\n",
564                                 port_id, diag);
565                 return diag;
566         }
567
568         /*
569          * Setup new number of RX/TX queues and reconfigure device.
570          */
571         diag = rte_eth_dev_rx_queue_config(dev, nb_rx_q);
572         if (diag != 0) {
573                 PMD_DEBUG_TRACE("port%d rte_eth_dev_rx_queue_config = %d\n",
574                                 port_id, diag);
575                 return diag;
576         }
577
578         diag = rte_eth_dev_tx_queue_config(dev, nb_tx_q);
579         if (diag != 0) {
580                 PMD_DEBUG_TRACE("port%d rte_eth_dev_tx_queue_config = %d\n",
581                                 port_id, diag);
582                 rte_eth_dev_rx_queue_config(dev, 0);
583                 return diag;
584         }
585
586         diag = (*dev->dev_ops->dev_configure)(dev);
587         if (diag != 0) {
588                 PMD_DEBUG_TRACE("port%d dev_configure = %d\n",
589                                 port_id, diag);
590                 rte_eth_dev_rx_queue_config(dev, 0);
591                 rte_eth_dev_tx_queue_config(dev, 0);
592                 return diag;
593         }
594
595         return 0;
596 }
597
598 static void
599 rte_eth_dev_config_restore(uint8_t port_id)
600 {
601         struct rte_eth_dev *dev;
602         struct rte_eth_dev_info dev_info;
603         struct ether_addr addr;
604         uint16_t i;
605         uint32_t pool = 0;
606
607         dev = &rte_eth_devices[port_id];
608
609         rte_eth_dev_info_get(port_id, &dev_info);
610
611         if (RTE_ETH_DEV_SRIOV(dev).active)
612                 pool = RTE_ETH_DEV_SRIOV(dev).def_vmdq_idx;
613
614         /* replay MAC address configuration */
615         for (i = 0; i < dev_info.max_mac_addrs; i++) {
616                 addr = dev->data->mac_addrs[i];
617
618                 /* skip zero address */
619                 if (is_zero_ether_addr(&addr))
620                         continue;
621
622                 /* add address to the hardware */
623                 if  (*dev->dev_ops->mac_addr_add)
624                         (*dev->dev_ops->mac_addr_add)(dev, &addr, i, pool);
625                 else {
626                         PMD_DEBUG_TRACE("port %d: MAC address array not supported\n",
627                                         port_id);
628                         /* exit the loop but not return an error */
629                         break;
630                 }
631         }
632
633         /* replay promiscuous configuration */
634         if (rte_eth_promiscuous_get(port_id) == 1)
635                 rte_eth_promiscuous_enable(port_id);
636         else if (rte_eth_promiscuous_get(port_id) == 0)
637                 rte_eth_promiscuous_disable(port_id);
638
639         /* replay allmulticast configuration */
640         if (rte_eth_allmulticast_get(port_id) == 1)
641                 rte_eth_allmulticast_enable(port_id);
642         else if (rte_eth_allmulticast_get(port_id) == 0)
643                 rte_eth_allmulticast_disable(port_id);
644 }
645
646 int
647 rte_eth_dev_start(uint8_t port_id)
648 {
649         struct rte_eth_dev *dev;
650         int diag;
651
652         /* This function is only safe when called from the primary process
653          * in a multi-process setup*/
654         PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
655
656         if (port_id >= nb_ports) {
657                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
658                 return (-EINVAL);
659         }
660         dev = &rte_eth_devices[port_id];
661
662         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
663         diag = (*dev->dev_ops->dev_start)(dev);
664         if (diag == 0)
665                 dev->data->dev_started = 1;
666         else
667                 return diag;
668
669         rte_eth_dev_config_restore(port_id);
670
671         return 0;
672 }
673
674 void
675 rte_eth_dev_stop(uint8_t port_id)
676 {
677         struct rte_eth_dev *dev;
678
679         /* This function is only safe when called from the primary process
680          * in a multi-process setup*/
681         PROC_PRIMARY_OR_RET();
682
683         if (port_id >= nb_ports) {
684                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
685                 return;
686         }
687         dev = &rte_eth_devices[port_id];
688
689         FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
690         dev->data->dev_started = 0;
691         (*dev->dev_ops->dev_stop)(dev);
692 }
693
694 void
695 rte_eth_dev_close(uint8_t port_id)
696 {
697         struct rte_eth_dev *dev;
698
699         /* This function is only safe when called from the primary process
700          * in a multi-process setup*/
701         PROC_PRIMARY_OR_RET();
702
703         if (port_id >= nb_ports) {
704                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
705                 return;
706         }
707
708         dev = &rte_eth_devices[port_id];
709
710         FUNC_PTR_OR_RET(*dev->dev_ops->dev_close);
711         dev->data->dev_started = 0;
712         (*dev->dev_ops->dev_close)(dev);
713 }
714
715 int
716 rte_eth_rx_queue_setup(uint8_t port_id, uint16_t rx_queue_id,
717                        uint16_t nb_rx_desc, unsigned int socket_id,
718                        const struct rte_eth_rxconf *rx_conf,
719                        struct rte_mempool *mp)
720 {
721         struct rte_eth_dev *dev;
722         struct rte_pktmbuf_pool_private *mbp_priv;
723         struct rte_eth_dev_info dev_info;
724
725         /* This function is only safe when called from the primary process
726          * in a multi-process setup*/
727         PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
728
729         if (port_id >= nb_ports) {
730                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
731                 return (-EINVAL);
732         }
733         dev = &rte_eth_devices[port_id];
734         if (rx_queue_id >= dev->data->nb_rx_queues) {
735                 PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", rx_queue_id);
736                 return (-EINVAL);
737         }
738
739         if (dev->data->dev_started) {
740                 PMD_DEBUG_TRACE(
741                     "port %d must be stopped to allow configuration\n", port_id);
742                 return -EBUSY;
743         }
744
745         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
746         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_setup, -ENOTSUP);
747
748         /*
749          * Check the size of the mbuf data buffer.
750          * This value must be provided in the private data of the memory pool.
751          * First check that the memory pool has a valid private data.
752          */
753         (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
754         if (mp->private_data_size < sizeof(struct rte_pktmbuf_pool_private)) {
755                 PMD_DEBUG_TRACE("%s private_data_size %d < %d\n",
756                                 mp->name, (int) mp->private_data_size,
757                                 (int) sizeof(struct rte_pktmbuf_pool_private));
758                 return (-ENOSPC);
759         }
760         mbp_priv = (struct rte_pktmbuf_pool_private *)
761                 ((char *)mp + sizeof(struct rte_mempool));
762         if ((uint32_t) (mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM) <
763             dev_info.min_rx_bufsize) {
764                 PMD_DEBUG_TRACE("%s mbuf_data_room_size %d < %d "
765                                 "(RTE_PKTMBUF_HEADROOM=%d + min_rx_bufsize(dev)"
766                                 "=%d)\n",
767                                 mp->name,
768                                 (int)mbp_priv->mbuf_data_room_size,
769                                 (int)(RTE_PKTMBUF_HEADROOM +
770                                       dev_info.min_rx_bufsize),
771                                 (int)RTE_PKTMBUF_HEADROOM,
772                                 (int)dev_info.min_rx_bufsize);
773                 return (-EINVAL);
774         }
775
776         return (*dev->dev_ops->rx_queue_setup)(dev, rx_queue_id, nb_rx_desc,
777                                                socket_id, rx_conf, mp);
778 }
779
780 int
781 rte_eth_tx_queue_setup(uint8_t port_id, uint16_t tx_queue_id,
782                        uint16_t nb_tx_desc, unsigned int socket_id,
783                        const struct rte_eth_txconf *tx_conf)
784 {
785         struct rte_eth_dev *dev;
786
787         /* This function is only safe when called from the primary process
788          * in a multi-process setup*/
789         PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
790
791         if (port_id >= RTE_MAX_ETHPORTS || port_id >= nb_ports) {
792                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
793                 return (-EINVAL);
794         }
795         dev = &rte_eth_devices[port_id];
796         if (tx_queue_id >= dev->data->nb_tx_queues) {
797                 PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", tx_queue_id);
798                 return (-EINVAL);
799         }
800
801         if (dev->data->dev_started) {
802                 PMD_DEBUG_TRACE(
803                     "port %d must be stopped to allow configuration\n", port_id);
804                 return -EBUSY;
805         }
806
807         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_setup, -ENOTSUP);
808         return (*dev->dev_ops->tx_queue_setup)(dev, tx_queue_id, nb_tx_desc,
809                                                socket_id, tx_conf);
810 }
811
812 void
813 rte_eth_promiscuous_enable(uint8_t port_id)
814 {
815         struct rte_eth_dev *dev;
816
817         if (port_id >= nb_ports) {
818                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
819                 return;
820         }
821         dev = &rte_eth_devices[port_id];
822
823         FUNC_PTR_OR_RET(*dev->dev_ops->promiscuous_enable);
824         (*dev->dev_ops->promiscuous_enable)(dev);
825         dev->data->promiscuous = 1;
826 }
827
828 void
829 rte_eth_promiscuous_disable(uint8_t port_id)
830 {
831         struct rte_eth_dev *dev;
832
833         if (port_id >= nb_ports) {
834                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
835                 return;
836         }
837         dev = &rte_eth_devices[port_id];
838
839         FUNC_PTR_OR_RET(*dev->dev_ops->promiscuous_disable);
840         dev->data->promiscuous = 0;
841         (*dev->dev_ops->promiscuous_disable)(dev);
842 }
843
844 int
845 rte_eth_promiscuous_get(uint8_t port_id)
846 {
847         struct rte_eth_dev *dev;
848
849         if (port_id >= nb_ports) {
850                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
851                 return -1;
852         }
853
854         dev = &rte_eth_devices[port_id];
855         return dev->data->promiscuous;
856 }
857
858 void
859 rte_eth_allmulticast_enable(uint8_t port_id)
860 {
861         struct rte_eth_dev *dev;
862
863         if (port_id >= nb_ports) {
864                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
865                 return;
866         }
867         dev = &rte_eth_devices[port_id];
868
869         FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_enable);
870         (*dev->dev_ops->allmulticast_enable)(dev);
871         dev->data->all_multicast = 1;
872 }
873
874 void
875 rte_eth_allmulticast_disable(uint8_t port_id)
876 {
877         struct rte_eth_dev *dev;
878
879         if (port_id >= nb_ports) {
880                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
881                 return;
882         }
883         dev = &rte_eth_devices[port_id];
884
885         FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_disable);
886         dev->data->all_multicast = 0;
887         (*dev->dev_ops->allmulticast_disable)(dev);
888 }
889
890 int
891 rte_eth_allmulticast_get(uint8_t port_id)
892 {
893         struct rte_eth_dev *dev;
894
895         if (port_id >= nb_ports) {
896                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
897                 return -1;
898         }
899
900         dev = &rte_eth_devices[port_id];
901         return dev->data->all_multicast;
902 }
903
904 static inline int
905 rte_eth_dev_atomic_read_link_status(struct rte_eth_dev *dev,
906                                 struct rte_eth_link *link)
907 {
908         struct rte_eth_link *dst = link;
909         struct rte_eth_link *src = &(dev->data->dev_link);
910
911         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
912                                         *(uint64_t *)src) == 0)
913                 return -1;
914
915         return 0;
916 }
917
918 void
919 rte_eth_link_get(uint8_t port_id, struct rte_eth_link *eth_link)
920 {
921         struct rte_eth_dev *dev;
922
923         if (port_id >= nb_ports) {
924                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
925                 return;
926         }
927         dev = &rte_eth_devices[port_id];
928         FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
929
930         if (dev->data->dev_conf.intr_conf.lsc != 0)
931                 rte_eth_dev_atomic_read_link_status(dev, eth_link);
932         else {
933                 (*dev->dev_ops->link_update)(dev, 1);
934                 *eth_link = dev->data->dev_link;
935         }
936 }
937
938 void
939 rte_eth_link_get_nowait(uint8_t port_id, struct rte_eth_link *eth_link)
940 {
941         struct rte_eth_dev *dev;
942
943         if (port_id >= nb_ports) {
944                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
945                 return;
946         }
947         dev = &rte_eth_devices[port_id];
948         FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
949
950         if (dev->data->dev_conf.intr_conf.lsc != 0)
951                 rte_eth_dev_atomic_read_link_status(dev, eth_link);
952         else {
953                 (*dev->dev_ops->link_update)(dev, 0);
954                 *eth_link = dev->data->dev_link;
955         }
956 }
957
958 void
959 rte_eth_stats_get(uint8_t port_id, struct rte_eth_stats *stats)
960 {
961         struct rte_eth_dev *dev;
962
963         if (port_id >= nb_ports) {
964                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
965                 return;
966         }
967         dev = &rte_eth_devices[port_id];
968         memset(stats, 0, sizeof(*stats));
969
970         FUNC_PTR_OR_RET(*dev->dev_ops->stats_get);
971         (*dev->dev_ops->stats_get)(dev, stats);
972         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
973 }
974
975 void
976 rte_eth_stats_reset(uint8_t port_id)
977 {
978         struct rte_eth_dev *dev;
979
980         if (port_id >= nb_ports) {
981                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
982                 return;
983         }
984         dev = &rte_eth_devices[port_id];
985
986         FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
987         (*dev->dev_ops->stats_reset)(dev);
988 }
989
990
991 static int
992 set_queue_stats_mapping(uint8_t port_id, uint16_t queue_id, uint8_t stat_idx,
993                 uint8_t is_rx)
994 {
995         struct rte_eth_dev *dev;
996
997         if (port_id >= nb_ports) {
998                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
999                 return -ENODEV;
1000         }
1001         dev = &rte_eth_devices[port_id];
1002
1003         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_stats_mapping_set, -ENOTSUP);
1004         return (*dev->dev_ops->queue_stats_mapping_set)
1005                         (dev, queue_id, stat_idx, is_rx);
1006 }
1007
1008
1009 int
1010 rte_eth_dev_set_tx_queue_stats_mapping(uint8_t port_id, uint16_t tx_queue_id,
1011                 uint8_t stat_idx)
1012 {
1013         return set_queue_stats_mapping(port_id, tx_queue_id, stat_idx,
1014                         STAT_QMAP_TX);
1015 }
1016
1017
1018 int
1019 rte_eth_dev_set_rx_queue_stats_mapping(uint8_t port_id, uint16_t rx_queue_id,
1020                 uint8_t stat_idx)
1021 {
1022         return set_queue_stats_mapping(port_id, rx_queue_id, stat_idx,
1023                         STAT_QMAP_RX);
1024 }
1025
1026
1027 void
1028 rte_eth_dev_info_get(uint8_t port_id, struct rte_eth_dev_info *dev_info)
1029 {
1030         struct rte_eth_dev *dev;
1031
1032         if (port_id >= nb_ports) {
1033                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1034                 return;
1035         }
1036         dev = &rte_eth_devices[port_id];
1037
1038         FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
1039         (*dev->dev_ops->dev_infos_get)(dev, dev_info);
1040         dev_info->pci_dev = dev->pci_dev;
1041         if (dev->driver)
1042                 dev_info->driver_name = dev->driver->pci_drv.name;
1043 }
1044
1045 void
1046 rte_eth_macaddr_get(uint8_t port_id, struct ether_addr *mac_addr)
1047 {
1048         struct rte_eth_dev *dev;
1049
1050         if (port_id >= nb_ports) {
1051                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1052                 return;
1053         }
1054         dev = &rte_eth_devices[port_id];
1055         ether_addr_copy(&dev->data->mac_addrs[0], mac_addr);
1056 }
1057
1058 int
1059 rte_eth_dev_vlan_filter(uint8_t port_id, uint16_t vlan_id, int on)
1060 {
1061         struct rte_eth_dev *dev;
1062
1063         if (port_id >= nb_ports) {
1064                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1065                 return (-ENODEV);
1066         }
1067         dev = &rte_eth_devices[port_id];
1068         if (! (dev->data->dev_conf.rxmode.hw_vlan_filter)) {
1069                 PMD_DEBUG_TRACE("port %d: vlan-filtering disabled\n", port_id);
1070                 return (-ENOSYS);
1071         }
1072
1073         if (vlan_id > 4095) {
1074                 PMD_DEBUG_TRACE("(port_id=%d) invalid vlan_id=%u > 4095\n",
1075                                 port_id, (unsigned) vlan_id);
1076                 return (-EINVAL);
1077         }
1078         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_filter_set, -ENOTSUP);
1079         (*dev->dev_ops->vlan_filter_set)(dev, vlan_id, on);
1080         return (0);
1081 }
1082
1083 int
1084 rte_eth_dev_set_vlan_strip_on_queue(uint8_t port_id, uint16_t rx_queue_id, int on)
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 (-ENODEV);
1091         }
1092
1093         dev = &rte_eth_devices[port_id];
1094         if (rx_queue_id >= dev->data->nb_rx_queues) {
1095                 PMD_DEBUG_TRACE("Invalid rx_queue_id=%d\n", port_id);
1096                 return (-EINVAL);
1097         }
1098
1099         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_strip_queue_set, -ENOTSUP);
1100         (*dev->dev_ops->vlan_strip_queue_set)(dev, rx_queue_id, on);
1101
1102         return (0);
1103 }
1104
1105 int
1106 rte_eth_dev_set_vlan_ether_type(uint8_t port_id, uint16_t tpid)
1107 {
1108         struct rte_eth_dev *dev;
1109
1110         if (port_id >= nb_ports) {
1111                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1112                 return (-ENODEV);
1113         }
1114
1115         dev = &rte_eth_devices[port_id];
1116         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_tpid_set, -ENOTSUP);
1117         (*dev->dev_ops->vlan_tpid_set)(dev, tpid);
1118
1119         return (0);
1120 }
1121
1122 int
1123 rte_eth_dev_set_vlan_offload(uint8_t port_id, int offload_mask)
1124 {
1125         struct rte_eth_dev *dev;
1126         int ret = 0;
1127         int mask = 0;
1128         int cur, org = 0;
1129         
1130         if (port_id >= nb_ports) {
1131                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1132                 return (-ENODEV);
1133         }
1134
1135         dev = &rte_eth_devices[port_id];
1136
1137         /*check which option changed by application*/
1138         cur = !!(offload_mask & ETH_VLAN_STRIP_OFFLOAD);
1139         org = !!(dev->data->dev_conf.rxmode.hw_vlan_strip);
1140         if (cur != org){
1141                 dev->data->dev_conf.rxmode.hw_vlan_strip = (uint8_t)cur;
1142                 mask |= ETH_VLAN_STRIP_MASK;
1143         }
1144         
1145         cur = !!(offload_mask & ETH_VLAN_FILTER_OFFLOAD);
1146         org = !!(dev->data->dev_conf.rxmode.hw_vlan_filter);
1147         if (cur != org){
1148                 dev->data->dev_conf.rxmode.hw_vlan_filter = (uint8_t)cur;
1149                 mask |= ETH_VLAN_FILTER_MASK;
1150         }
1151
1152         cur = !!(offload_mask & ETH_VLAN_EXTEND_OFFLOAD);
1153         org = !!(dev->data->dev_conf.rxmode.hw_vlan_extend);
1154         if (cur != org){
1155                 dev->data->dev_conf.rxmode.hw_vlan_extend = (uint8_t)cur;
1156                 mask |= ETH_VLAN_EXTEND_MASK;
1157         }
1158
1159         /*no change*/
1160         if(mask == 0)
1161                 return ret;
1162         
1163         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_offload_set, -ENOTSUP);
1164         (*dev->dev_ops->vlan_offload_set)(dev, mask);
1165
1166         return ret;
1167 }
1168
1169 int
1170 rte_eth_dev_get_vlan_offload(uint8_t port_id)
1171 {
1172         struct rte_eth_dev *dev;
1173         int ret = 0;
1174
1175         if (port_id >= nb_ports) {
1176                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1177                 return (-ENODEV);
1178         }
1179
1180         dev = &rte_eth_devices[port_id];
1181
1182         if (dev->data->dev_conf.rxmode.hw_vlan_strip)
1183                 ret |= ETH_VLAN_STRIP_OFFLOAD ;
1184
1185         if (dev->data->dev_conf.rxmode.hw_vlan_filter)
1186                 ret |= ETH_VLAN_FILTER_OFFLOAD ;
1187
1188         if (dev->data->dev_conf.rxmode.hw_vlan_extend)
1189                 ret |= ETH_VLAN_EXTEND_OFFLOAD ;
1190
1191         return ret;
1192 }
1193
1194
1195 int
1196 rte_eth_dev_fdir_add_signature_filter(uint8_t port_id,
1197                                       struct rte_fdir_filter *fdir_filter,
1198                                       uint8_t queue)
1199 {
1200         struct rte_eth_dev *dev;
1201
1202         if (port_id >= nb_ports) {
1203                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1204                 return (-ENODEV);
1205         }
1206
1207         dev = &rte_eth_devices[port_id];
1208
1209         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
1210                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1211                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1212                 return (-ENOSYS);
1213         }
1214
1215         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1216              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1217             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1218                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1219                                 "None l4type, source & destinations ports " \
1220                                 "should be null!\n");
1221                 return (-EINVAL);
1222         }
1223
1224         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_add_signature_filter, -ENOTSUP);
1225         return (*dev->dev_ops->fdir_add_signature_filter)(dev, fdir_filter,
1226                                                                 queue);
1227 }
1228
1229 int
1230 rte_eth_dev_fdir_update_signature_filter(uint8_t port_id,
1231                                          struct rte_fdir_filter *fdir_filter,
1232                                          uint8_t queue)
1233 {
1234         struct rte_eth_dev *dev;
1235
1236         if (port_id >= nb_ports) {
1237                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1238                 return (-ENODEV);
1239         }
1240
1241         dev = &rte_eth_devices[port_id];
1242
1243         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
1244                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1245                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1246                 return (-ENOSYS);
1247         }
1248
1249         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1250              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1251             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1252                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1253                                 "None l4type, source & destinations ports " \
1254                                 "should be null!\n");
1255                 return (-EINVAL);
1256         }
1257
1258         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_update_signature_filter, -ENOTSUP);
1259         return (*dev->dev_ops->fdir_update_signature_filter)(dev, fdir_filter,
1260                                                                 queue);
1261
1262 }
1263
1264 int
1265 rte_eth_dev_fdir_remove_signature_filter(uint8_t port_id,
1266                                          struct rte_fdir_filter *fdir_filter)
1267 {
1268         struct rte_eth_dev *dev;
1269
1270         if (port_id >= nb_ports) {
1271                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1272                 return (-ENODEV);
1273         }
1274
1275         dev = &rte_eth_devices[port_id];
1276
1277         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
1278                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1279                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1280                 return (-ENOSYS);
1281         }
1282
1283         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1284              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1285             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1286                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1287                                 "None l4type source & destinations ports " \
1288                                 "should be null!\n");
1289                 return (-EINVAL);
1290         }
1291
1292         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_remove_signature_filter, -ENOTSUP);
1293         return (*dev->dev_ops->fdir_remove_signature_filter)(dev, fdir_filter);
1294 }
1295
1296 int
1297 rte_eth_dev_fdir_get_infos(uint8_t port_id, struct rte_eth_fdir *fdir)
1298 {
1299         struct rte_eth_dev *dev;
1300
1301         if (port_id >= nb_ports) {
1302                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1303                 return (-ENODEV);
1304         }
1305
1306         dev = &rte_eth_devices[port_id];
1307         if (! (dev->data->dev_conf.fdir_conf.mode)) {
1308                 PMD_DEBUG_TRACE("port %d: pkt-filter disabled\n", port_id);
1309                 return (-ENOSYS);
1310         }
1311
1312         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_infos_get, -ENOTSUP);
1313
1314         (*dev->dev_ops->fdir_infos_get)(dev, fdir);
1315         return (0);
1316 }
1317
1318 int
1319 rte_eth_dev_fdir_add_perfect_filter(uint8_t port_id,
1320                                     struct rte_fdir_filter *fdir_filter,
1321                                     uint16_t soft_id, uint8_t queue,
1322                                     uint8_t drop)
1323 {
1324         struct rte_eth_dev *dev;
1325
1326         if (port_id >= nb_ports) {
1327                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1328                 return (-ENODEV);
1329         }
1330
1331         dev = &rte_eth_devices[port_id];
1332
1333         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
1334                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1335                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1336                 return (-ENOSYS);
1337         }
1338
1339         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1340              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1341             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1342                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1343                                 "None l4type, source & destinations ports " \
1344                                 "should be null!\n");
1345                 return (-EINVAL);
1346         }
1347
1348         /* For now IPv6 is not supported with perfect filter */
1349         if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
1350                 return (-ENOTSUP);
1351
1352         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_add_perfect_filter, -ENOTSUP);
1353         return (*dev->dev_ops->fdir_add_perfect_filter)(dev, fdir_filter,
1354                                                                 soft_id, queue,
1355                                                                 drop);
1356 }
1357
1358 int
1359 rte_eth_dev_fdir_update_perfect_filter(uint8_t port_id,
1360                                        struct rte_fdir_filter *fdir_filter,
1361                                        uint16_t soft_id, uint8_t queue,
1362                                        uint8_t drop)
1363 {
1364         struct rte_eth_dev *dev;
1365
1366         if (port_id >= nb_ports) {
1367                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1368                 return (-ENODEV);
1369         }
1370
1371         dev = &rte_eth_devices[port_id];
1372
1373         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
1374                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1375                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1376                 return (-ENOSYS);
1377         }
1378
1379         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1380              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1381             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1382                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1383                                 "None l4type, source & destinations ports " \
1384                                 "should be null!\n");
1385                 return (-EINVAL);
1386         }
1387
1388         /* For now IPv6 is not supported with perfect filter */
1389         if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
1390                 return (-ENOTSUP);
1391
1392         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_update_perfect_filter, -ENOTSUP);
1393         return (*dev->dev_ops->fdir_update_perfect_filter)(dev, fdir_filter,
1394                                                         soft_id, queue, drop);
1395 }
1396
1397 int
1398 rte_eth_dev_fdir_remove_perfect_filter(uint8_t port_id,
1399                                        struct rte_fdir_filter *fdir_filter,
1400                                        uint16_t soft_id)
1401 {
1402         struct rte_eth_dev *dev;
1403
1404         if (port_id >= nb_ports) {
1405                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1406                 return (-ENODEV);
1407         }
1408
1409         dev = &rte_eth_devices[port_id];
1410
1411         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
1412                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1413                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1414                 return (-ENOSYS);
1415         }
1416
1417         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1418              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1419             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1420                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1421                                 "None l4type, source & destinations ports " \
1422                                 "should be null!\n");
1423                 return (-EINVAL);
1424         }
1425
1426         /* For now IPv6 is not supported with perfect filter */
1427         if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
1428                 return (-ENOTSUP);
1429
1430         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_remove_perfect_filter, -ENOTSUP);
1431         return (*dev->dev_ops->fdir_remove_perfect_filter)(dev, fdir_filter,
1432                                                                 soft_id);
1433 }
1434
1435 int
1436 rte_eth_dev_fdir_set_masks(uint8_t port_id, struct rte_fdir_masks *fdir_mask)
1437 {
1438         struct rte_eth_dev *dev;
1439
1440         if (port_id >= nb_ports) {
1441                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1442                 return (-ENODEV);
1443         }
1444
1445         dev = &rte_eth_devices[port_id];
1446         if (! (dev->data->dev_conf.fdir_conf.mode)) {
1447                 PMD_DEBUG_TRACE("port %d: pkt-filter disabled\n", port_id);
1448                 return (-ENOSYS);
1449         }
1450
1451         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_set_masks, -ENOTSUP);
1452         return (*dev->dev_ops->fdir_set_masks)(dev, fdir_mask);
1453 }
1454
1455 int
1456 rte_eth_dev_flow_ctrl_set(uint8_t port_id, struct rte_eth_fc_conf *fc_conf)
1457 {
1458         struct rte_eth_dev *dev;
1459
1460         if (port_id >= nb_ports) {
1461                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1462                 return (-ENODEV);
1463         }
1464
1465         if ((fc_conf->send_xon != 0) && (fc_conf->send_xon != 1)) {
1466                 PMD_DEBUG_TRACE("Invalid send_xon, only 0/1 allowed\n");
1467                 return (-EINVAL);
1468         }
1469
1470         dev = &rte_eth_devices[port_id];
1471         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_set, -ENOTSUP);
1472         return (*dev->dev_ops->flow_ctrl_set)(dev, fc_conf);
1473 }
1474
1475 int
1476 rte_eth_dev_priority_flow_ctrl_set(uint8_t port_id, struct rte_eth_pfc_conf *pfc_conf)
1477 {
1478         struct rte_eth_dev *dev;
1479
1480         if (port_id >= nb_ports) {
1481                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1482                 return (-ENODEV);
1483         }
1484
1485         if (pfc_conf->priority > (ETH_DCB_NUM_USER_PRIORITIES - 1)) {
1486                 PMD_DEBUG_TRACE("Invalid priority, only 0-7 allowed\n");
1487                 return (-EINVAL);
1488         }
1489
1490         dev = &rte_eth_devices[port_id];
1491         /* High water, low water validation are device specific */
1492         if  (*dev->dev_ops->priority_flow_ctrl_set)
1493                 return (*dev->dev_ops->priority_flow_ctrl_set)(dev, pfc_conf);
1494         return (-ENOTSUP);
1495 }
1496
1497 int
1498 rte_eth_dev_rss_reta_update(uint8_t port_id, struct rte_eth_rss_reta *reta_conf)
1499 {
1500         struct rte_eth_dev *dev;
1501         uint8_t i,j;
1502
1503         if (port_id >= nb_ports) {
1504                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1505                 return (-ENODEV);
1506         }
1507
1508         /* Invalid mask bit(s) setting */
1509         if ((reta_conf->mask_lo == 0) && (reta_conf->mask_hi == 0)) {
1510                 PMD_DEBUG_TRACE("Invalid update mask bits for port=%d\n",port_id);
1511                 return (-EINVAL);
1512         }
1513
1514         if (reta_conf->mask_lo != 0) {
1515                 for (i = 0; i < ETH_RSS_RETA_NUM_ENTRIES/2; i++) {
1516                         if ((reta_conf->mask_lo & (1ULL << i)) &&
1517                                 (reta_conf->reta[i] >= ETH_RSS_RETA_MAX_QUEUE)) {
1518                                 PMD_DEBUG_TRACE("RETA hash index output"
1519                                         "configration for port=%d,invalid"
1520                                         "queue=%d\n",port_id,reta_conf->reta[i]);
1521
1522                                 return (-EINVAL);
1523                         } 
1524                 }
1525         }
1526
1527         if (reta_conf->mask_hi != 0) {
1528                 for (i = 0; i< ETH_RSS_RETA_NUM_ENTRIES/2; i++) {       
1529                         j = (uint8_t)(i + ETH_RSS_RETA_NUM_ENTRIES/2);
1530
1531                         /* Check if the max entry >= 128 */
1532                         if ((reta_conf->mask_hi & (1ULL << i)) && 
1533                                 (reta_conf->reta[j] >= ETH_RSS_RETA_MAX_QUEUE)) {
1534                                 PMD_DEBUG_TRACE("RETA hash index output"
1535                                         "configration for port=%d,invalid"
1536                                         "queue=%d\n",port_id,reta_conf->reta[j]);
1537
1538                                 return (-EINVAL);
1539                         }
1540                 }
1541         }
1542
1543         dev = &rte_eth_devices[port_id];
1544
1545         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->reta_update, -ENOTSUP);
1546         return (*dev->dev_ops->reta_update)(dev, reta_conf);
1547 }
1548
1549 int 
1550 rte_eth_dev_rss_reta_query(uint8_t port_id, struct rte_eth_rss_reta *reta_conf)
1551 {
1552         struct rte_eth_dev *dev;
1553         
1554         if (port_id >= nb_ports) {
1555                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1556                 return (-ENODEV);
1557         }
1558
1559         if((reta_conf->mask_lo == 0) && (reta_conf->mask_hi == 0)) {
1560                 PMD_DEBUG_TRACE("Invalid update mask bits for the port=%d\n",port_id);
1561                 return (-EINVAL);
1562         }
1563
1564         dev = &rte_eth_devices[port_id];
1565         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->reta_query, -ENOTSUP);
1566         return (*dev->dev_ops->reta_query)(dev, reta_conf);
1567 }
1568
1569 int
1570 rte_eth_led_on(uint8_t port_id)
1571 {
1572         struct rte_eth_dev *dev;
1573
1574         if (port_id >= nb_ports) {
1575                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1576                 return (-ENODEV);
1577         }
1578
1579         dev = &rte_eth_devices[port_id];
1580         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_on, -ENOTSUP);
1581         return ((*dev->dev_ops->dev_led_on)(dev));
1582 }
1583
1584 int
1585 rte_eth_led_off(uint8_t port_id)
1586 {
1587         struct rte_eth_dev *dev;
1588
1589         if (port_id >= nb_ports) {
1590                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1591                 return (-ENODEV);
1592         }
1593
1594         dev = &rte_eth_devices[port_id];
1595         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_off, -ENOTSUP);
1596         return ((*dev->dev_ops->dev_led_off)(dev));
1597 }
1598
1599 /*
1600  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
1601  * an empty spot.
1602  */
1603 static inline int
1604 get_mac_addr_index(uint8_t port_id, struct ether_addr *addr)
1605 {
1606         struct rte_eth_dev_info dev_info;
1607         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1608         unsigned i;
1609
1610         rte_eth_dev_info_get(port_id, &dev_info);
1611
1612         for (i = 0; i < dev_info.max_mac_addrs; i++)
1613                 if (memcmp(addr, &dev->data->mac_addrs[i], ETHER_ADDR_LEN) == 0)
1614                         return i;
1615
1616         return -1;
1617 }
1618
1619 static struct ether_addr null_mac_addr = {{0, 0, 0, 0, 0, 0}};
1620
1621 int
1622 rte_eth_dev_mac_addr_add(uint8_t port_id, struct ether_addr *addr,
1623                         uint32_t pool)
1624 {
1625         struct rte_eth_dev *dev;
1626         int index;
1627         uint64_t pool_mask;
1628
1629         if (port_id >= nb_ports) {
1630                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1631                 return (-ENODEV);
1632         }
1633         dev = &rte_eth_devices[port_id];
1634         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_add, -ENOTSUP);
1635
1636         if (is_zero_ether_addr(addr)) {
1637                 PMD_DEBUG_TRACE("port %d: Cannot add NULL MAC address\n", 
1638                         port_id);
1639                 return (-EINVAL);
1640         }
1641         if (pool >= ETH_64_POOLS) {
1642                 PMD_DEBUG_TRACE("pool id must be 0-%d\n",ETH_64_POOLS - 1);
1643                 return (-EINVAL);
1644         }
1645         
1646         index = get_mac_addr_index(port_id, addr);
1647         if (index < 0) {
1648                 index = get_mac_addr_index(port_id, &null_mac_addr);
1649                 if (index < 0) {
1650                         PMD_DEBUG_TRACE("port %d: MAC address array full\n",
1651                                 port_id);
1652                         return (-ENOSPC);
1653                 }
1654         } else {
1655                 pool_mask = dev->data->mac_pool_sel[index];
1656                 
1657                 /* Check if both MAC address and pool is alread there, and do nothing */
1658                 if (pool_mask & (1ULL << pool))
1659                         return 0;
1660         }
1661
1662         /* Update NIC */
1663         (*dev->dev_ops->mac_addr_add)(dev, addr, index, pool);
1664
1665         /* Update address in NIC data structure */
1666         ether_addr_copy(addr, &dev->data->mac_addrs[index]);
1667         
1668         /* Update pool bitmap in NIC data structure */
1669         dev->data->mac_pool_sel[index] |= (1ULL << pool);
1670
1671         return 0;
1672 }
1673
1674 int
1675 rte_eth_dev_mac_addr_remove(uint8_t port_id, struct ether_addr *addr)
1676 {
1677         struct rte_eth_dev *dev;
1678         int index;
1679
1680         if (port_id >= nb_ports) {
1681                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1682                 return (-ENODEV);
1683         }
1684         dev = &rte_eth_devices[port_id];
1685         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_remove, -ENOTSUP);
1686
1687         index = get_mac_addr_index(port_id, addr);
1688         if (index == 0) {
1689                 PMD_DEBUG_TRACE("port %d: Cannot remove default MAC address\n", port_id);
1690                 return (-EADDRINUSE);
1691         } else if (index < 0)
1692                 return 0;  /* Do nothing if address wasn't found */
1693
1694         /* Update NIC */
1695         (*dev->dev_ops->mac_addr_remove)(dev, index);
1696
1697         /* Update address in NIC data structure */
1698         ether_addr_copy(&null_mac_addr, &dev->data->mac_addrs[index]);
1699
1700         return 0;
1701 }
1702
1703 int 
1704 rte_eth_dev_set_vf_rxmode(uint8_t port_id,  uint16_t vf,
1705                                 uint16_t rx_mode, uint8_t on)
1706 {
1707         uint16_t num_vfs;
1708         struct rte_eth_dev *dev;
1709         struct rte_eth_dev_info dev_info;
1710
1711         if (port_id >= nb_ports) {
1712                 PMD_DEBUG_TRACE("set VF RX mode:Invalid port_id=%d\n",
1713                                 port_id);
1714                 return (-ENODEV);
1715         }       
1716         
1717         dev = &rte_eth_devices[port_id];
1718         rte_eth_dev_info_get(port_id, &dev_info);
1719
1720         num_vfs = dev_info.max_vfs;
1721         if (vf > num_vfs)
1722         {
1723                 PMD_DEBUG_TRACE("set VF RX mode:invalid VF id %d\n", vf);
1724                 return (-EINVAL);
1725         }
1726         if (rx_mode == 0)
1727         {
1728                 PMD_DEBUG_TRACE("set VF RX mode:mode mask ca not be zero\n");
1729                 return (-EINVAL);       
1730         }
1731         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_vf_rx_mode, -ENOTSUP);
1732         return (*dev->dev_ops->set_vf_rx_mode)(dev, vf, rx_mode, on);
1733 }
1734
1735 /*
1736  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
1737  * an empty spot.
1738  */
1739 static inline int
1740 get_hash_mac_addr_index(uint8_t port_id, struct ether_addr *addr)
1741 {
1742         struct rte_eth_dev_info dev_info;
1743         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1744         unsigned i;
1745
1746         rte_eth_dev_info_get(port_id, &dev_info);
1747         if (!dev->data->hash_mac_addrs)
1748                 return -1;
1749
1750         for (i = 0; i < dev_info.max_hash_mac_addrs; i++)
1751                 if (memcmp(addr, &dev->data->hash_mac_addrs[i],
1752                         ETHER_ADDR_LEN) == 0)
1753                         return i;
1754
1755         return -1;
1756 }
1757
1758 int
1759 rte_eth_dev_uc_hash_table_set(uint8_t port_id, struct ether_addr *addr,
1760                                 uint8_t on)
1761 {
1762         int index;
1763         int ret;
1764         struct rte_eth_dev *dev;
1765         
1766         if (port_id >= nb_ports) {
1767                 PMD_DEBUG_TRACE("unicast hash setting:Invalid port_id=%d\n",
1768                         port_id);
1769                 return (-ENODEV);
1770         }
1771         
1772         dev = &rte_eth_devices[port_id];
1773         if (is_zero_ether_addr(addr)) {
1774                 PMD_DEBUG_TRACE("port %d: Cannot add NULL MAC address\n", 
1775                         port_id);
1776                 return (-EINVAL);
1777         }
1778
1779         index = get_hash_mac_addr_index(port_id, addr);
1780         /* Check if it's already there, and do nothing */
1781         if ((index >= 0) && (on))
1782                 return 0;
1783         
1784         if (index < 0) {
1785                 if (!on) {
1786                         PMD_DEBUG_TRACE("port %d: the MAC address was not" 
1787                                 "set in UTA\n", port_id);
1788                         return (-EINVAL);
1789                 }
1790                         
1791                 index = get_hash_mac_addr_index(port_id, &null_mac_addr);
1792                 if (index < 0) {
1793                         PMD_DEBUG_TRACE("port %d: MAC address array full\n",
1794                                         port_id);
1795                         return (-ENOSPC);
1796                 }
1797         } 
1798          
1799         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->uc_hash_table_set, -ENOTSUP);
1800         ret = (*dev->dev_ops->uc_hash_table_set)(dev, addr, on);
1801         if (ret == 0) {
1802                 /* Update address in NIC data structure */
1803                 if (on)
1804                         ether_addr_copy(addr,
1805                                         &dev->data->hash_mac_addrs[index]);
1806                 else 
1807                         ether_addr_copy(&null_mac_addr,
1808                                         &dev->data->hash_mac_addrs[index]);
1809         }
1810         
1811         return ret;
1812 }
1813
1814 int
1815 rte_eth_dev_uc_all_hash_table_set(uint8_t port_id, uint8_t on)
1816 {
1817         struct rte_eth_dev *dev;
1818         
1819         if (port_id >= nb_ports) {
1820                 PMD_DEBUG_TRACE("unicast hash setting:Invalid port_id=%d\n",
1821                         port_id);
1822                 return (-ENODEV);
1823         }
1824         
1825         dev = &rte_eth_devices[port_id];
1826
1827         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->uc_all_hash_table_set, -ENOTSUP);
1828         return (*dev->dev_ops->uc_all_hash_table_set)(dev, on);
1829 }
1830
1831 int 
1832 rte_eth_dev_set_vf_rx(uint8_t port_id,uint16_t vf, uint8_t on)
1833 {
1834         uint16_t num_vfs;
1835         struct rte_eth_dev *dev;
1836         struct rte_eth_dev_info dev_info;
1837
1838         if (port_id >= nb_ports) {
1839                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1840                 return (-ENODEV);
1841         }
1842         
1843         dev = &rte_eth_devices[port_id];
1844         rte_eth_dev_info_get(port_id, &dev_info);
1845         
1846         num_vfs = dev_info.max_vfs;
1847         if (vf > num_vfs) 
1848         {
1849                 PMD_DEBUG_TRACE("port %d: invalid vf id\n", port_id);
1850                 return (-EINVAL);
1851         }       
1852         
1853         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_vf_rx, -ENOTSUP);
1854         return (*dev->dev_ops->set_vf_rx)(dev, vf,on);
1855 }
1856
1857 int 
1858 rte_eth_dev_set_vf_tx(uint8_t port_id,uint16_t vf, uint8_t on)
1859 {
1860         uint16_t num_vfs;
1861         struct rte_eth_dev *dev;
1862         struct rte_eth_dev_info dev_info;
1863
1864         if (port_id >= nb_ports) {
1865                 PMD_DEBUG_TRACE("set pool tx:Invalid port_id=%d\n", port_id);
1866                 return (-ENODEV);
1867         }
1868         
1869         dev = &rte_eth_devices[port_id];
1870         rte_eth_dev_info_get(port_id, &dev_info);
1871
1872         num_vfs = dev_info.max_vfs;
1873         if (vf > num_vfs) 
1874         {
1875                 PMD_DEBUG_TRACE("set pool tx:invalid pool id=%d\n", vf);
1876                 return (-EINVAL);
1877         }
1878         
1879         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_vf_tx, -ENOTSUP);
1880         return (*dev->dev_ops->set_vf_tx)(dev, vf,on);
1881 }
1882
1883 int
1884 rte_eth_dev_set_vf_vlan_filter(uint8_t port_id, uint16_t vlan_id, 
1885                                  uint64_t vf_mask,uint8_t vlan_on)
1886 {
1887         struct rte_eth_dev *dev;
1888
1889         if (port_id >= nb_ports) {
1890                 PMD_DEBUG_TRACE("VF VLAN filter:invalid port id=%d\n",
1891                                 port_id);
1892                 return (-ENODEV);
1893         }
1894         dev = &rte_eth_devices[port_id];
1895
1896         if(vlan_id > ETHER_MAX_VLAN_ID)
1897         {
1898                 PMD_DEBUG_TRACE("VF VLAN filter:invalid VLAN id=%d\n",
1899                         vlan_id);
1900                 return (-EINVAL);
1901         }
1902         if (vf_mask == 0)
1903         {
1904                 PMD_DEBUG_TRACE("VF VLAN filter:pool_mask can not be 0\n");
1905                 return (-EINVAL);
1906         }
1907         
1908         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->set_vf_vlan_filter, -ENOTSUP);
1909         return (*dev->dev_ops->set_vf_vlan_filter)(dev, vlan_id,
1910                                                 vf_mask,vlan_on);
1911 }
1912
1913 int
1914 rte_eth_mirror_rule_set(uint8_t port_id, 
1915                         struct rte_eth_vmdq_mirror_conf *mirror_conf,
1916                         uint8_t rule_id, uint8_t on)
1917 {
1918         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1919
1920         if (port_id >= nb_ports) {
1921                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1922                 return (-ENODEV);
1923         }
1924         
1925         if (mirror_conf->rule_type_mask == 0) {
1926                 PMD_DEBUG_TRACE("mirror rule type can not be 0.\n");
1927                 return (-EINVAL);
1928         }
1929         
1930         if (mirror_conf->dst_pool >= ETH_64_POOLS) {
1931                 PMD_DEBUG_TRACE("Invalid dst pool, pool id must"
1932                         "be 0-%d\n",ETH_64_POOLS - 1);
1933                 return (-EINVAL);
1934         }
1935         
1936         if ((mirror_conf->rule_type_mask & ETH_VMDQ_POOL_MIRROR) && 
1937                 (mirror_conf->pool_mask == 0)) {
1938                 PMD_DEBUG_TRACE("Invalid mirror pool, pool mask can not"
1939                                 "be 0.\n");             
1940                 return (-EINVAL);
1941         }
1942         
1943         if(rule_id >= ETH_VMDQ_NUM_MIRROR_RULE)
1944         {
1945                 PMD_DEBUG_TRACE("Invalid rule_id, rule_id must be 0-%d\n",
1946                         ETH_VMDQ_NUM_MIRROR_RULE - 1);
1947                 return (-EINVAL);
1948         }
1949
1950         dev = &rte_eth_devices[port_id];
1951         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mirror_rule_set, -ENOTSUP);
1952
1953         return (*dev->dev_ops->mirror_rule_set)(dev, mirror_conf, rule_id, on);
1954 }
1955
1956 int
1957 rte_eth_mirror_rule_reset(uint8_t port_id, uint8_t rule_id)
1958 {
1959         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1960
1961         if (port_id >= nb_ports) {
1962                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1963                 return (-ENODEV);
1964         }
1965
1966         if(rule_id >= ETH_VMDQ_NUM_MIRROR_RULE)
1967         {
1968                 PMD_DEBUG_TRACE("Invalid rule_id, rule_id must be 0-%d\n",
1969                         ETH_VMDQ_NUM_MIRROR_RULE-1);
1970                 return (-EINVAL);
1971         }
1972
1973         dev = &rte_eth_devices[port_id];
1974         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mirror_rule_reset, -ENOTSUP);
1975
1976         return (*dev->dev_ops->mirror_rule_reset)(dev, rule_id);
1977 }
1978
1979 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
1980 uint16_t
1981 rte_eth_rx_burst(uint8_t port_id, uint16_t queue_id,
1982                  struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1983 {
1984         struct rte_eth_dev *dev;
1985
1986         if (port_id >= nb_ports) {
1987                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1988                 return 0;
1989         }
1990         dev = &rte_eth_devices[port_id];
1991         FUNC_PTR_OR_ERR_RET(*dev->rx_pkt_burst, -ENOTSUP);
1992         if (queue_id >= dev->data->nb_rx_queues) {
1993                 PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", queue_id);
1994                 return 0;
1995         }
1996         return (*dev->rx_pkt_burst)(dev->data->rx_queues[queue_id],
1997                                                 rx_pkts, nb_pkts);
1998 }
1999
2000 uint16_t
2001 rte_eth_tx_burst(uint8_t port_id, uint16_t queue_id,
2002                  struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
2003 {
2004         struct rte_eth_dev *dev;
2005
2006         if (port_id >= nb_ports) {
2007                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
2008                 return 0;
2009         }
2010         dev = &rte_eth_devices[port_id];
2011
2012         FUNC_PTR_OR_ERR_RET(*dev->tx_pkt_burst, -ENOTSUP);
2013         if (queue_id >= dev->data->nb_tx_queues) {
2014                 PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", queue_id);
2015                 return 0;
2016         }
2017         return (*dev->tx_pkt_burst)(dev->data->tx_queues[queue_id],
2018                                                 tx_pkts, nb_pkts);
2019 }
2020
2021 uint32_t
2022 rte_eth_rx_queue_count(uint8_t port_id, uint16_t queue_id)
2023 {
2024         struct rte_eth_dev *dev;
2025
2026         if (port_id >= nb_ports) {
2027                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
2028                 return 0;
2029         }
2030         dev = &rte_eth_devices[port_id];
2031         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_count, -ENOTSUP);
2032         return (*dev->dev_ops->rx_queue_count)(dev, queue_id);  
2033 }
2034
2035 int
2036 rte_eth_rx_descriptor_done(uint8_t port_id, uint16_t queue_id, uint16_t offset)
2037 {
2038         struct rte_eth_dev *dev;
2039
2040         if (port_id >= nb_ports) {
2041                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
2042                 return (-ENODEV);
2043         }
2044         dev = &rte_eth_devices[port_id];
2045         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_descriptor_done, -ENOTSUP);
2046         return (*dev->dev_ops->rx_descriptor_done)( \
2047                 dev->data->rx_queues[queue_id], offset);
2048 }
2049 #endif
2050
2051 int
2052 rte_eth_dev_callback_register(uint8_t port_id,
2053                         enum rte_eth_event_type event,
2054                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
2055 {
2056         struct rte_eth_dev *dev;
2057         struct rte_eth_dev_callback *user_cb;
2058
2059         if (!cb_fn)
2060                 return (-EINVAL);
2061         if (port_id >= nb_ports) {
2062                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
2063                 return (-EINVAL);
2064         }
2065
2066         dev = &rte_eth_devices[port_id];
2067         rte_spinlock_lock(&rte_eth_dev_cb_lock);
2068
2069         TAILQ_FOREACH(user_cb, &(dev->callbacks), next) {
2070                 if (user_cb->cb_fn == cb_fn &&
2071                         user_cb->cb_arg == cb_arg &&
2072                         user_cb->event == event) {
2073                         break;
2074                 }
2075         }
2076
2077         /* create a new callback. */
2078         if (user_cb == NULL && (user_cb = rte_zmalloc("INTR_USER_CALLBACK",
2079                         sizeof(struct rte_eth_dev_callback), 0)) != NULL) {
2080                 user_cb->cb_fn = cb_fn;
2081                 user_cb->cb_arg = cb_arg;
2082                 user_cb->event = event;
2083                 TAILQ_INSERT_TAIL(&(dev->callbacks), user_cb, next);
2084         }
2085
2086         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2087         return ((user_cb == NULL) ? -ENOMEM : 0);
2088 }
2089
2090 int
2091 rte_eth_dev_callback_unregister(uint8_t port_id,
2092                         enum rte_eth_event_type event,
2093                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
2094 {
2095         int ret;
2096         struct rte_eth_dev *dev;
2097         struct rte_eth_dev_callback *cb, *next;
2098
2099         if (!cb_fn)
2100                 return (-EINVAL);
2101         if (port_id >= nb_ports) {
2102                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
2103                 return (-EINVAL);
2104         }
2105
2106         dev = &rte_eth_devices[port_id];
2107         rte_spinlock_lock(&rte_eth_dev_cb_lock);
2108
2109         ret = 0;
2110         for (cb = TAILQ_FIRST(&dev->callbacks); cb != NULL; cb = next) {
2111
2112                 next = TAILQ_NEXT(cb, next);
2113
2114                 if (cb->cb_fn != cb_fn || cb->event != event ||
2115                                 (cb->cb_arg != (void *)-1 &&
2116                                 cb->cb_arg != cb_arg))
2117                         continue;
2118
2119                 /*
2120                  * if this callback is not executing right now,
2121                  * then remove it.
2122                  */
2123                 if (cb->active == 0) {
2124                         TAILQ_REMOVE(&(dev->callbacks), cb, next);
2125                         rte_free(cb);
2126                 } else {
2127                         ret = -EAGAIN;
2128                 }
2129         }
2130
2131         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2132         return (ret);
2133 }
2134
2135 void
2136 _rte_eth_dev_callback_process(struct rte_eth_dev *dev,
2137         enum rte_eth_event_type event)
2138 {
2139         struct rte_eth_dev_callback *cb_lst;
2140         struct rte_eth_dev_callback dev_cb;
2141
2142         rte_spinlock_lock(&rte_eth_dev_cb_lock);
2143         TAILQ_FOREACH(cb_lst, &(dev->callbacks), next) {
2144                 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
2145                         continue;
2146                 dev_cb = *cb_lst;
2147                 cb_lst->active = 1;
2148                 rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2149                 dev_cb.cb_fn(dev->data->port_id, dev_cb.event,
2150                                                 dev_cb.cb_arg);
2151                 rte_spinlock_lock(&rte_eth_dev_cb_lock);
2152                 cb_lst->active = 0;
2153         }
2154         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
2155 }
2156 #ifdef RTE_NIC_BYPASS
2157 int rte_eth_dev_bypass_init(uint8_t port_id)
2158 {
2159         struct rte_eth_dev *dev;
2160
2161         if (port_id >= nb_ports) {
2162                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
2163                 return (-ENODEV);
2164         }
2165
2166         if ((dev= &rte_eth_devices[port_id]) == NULL) {
2167                 PMD_DEBUG_TRACE("Invalid port device\n");
2168                 return (-ENODEV);
2169         }
2170
2171         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_init, -ENOTSUP);
2172         (*dev->dev_ops->bypass_init)(dev);
2173         return 0;
2174 }
2175
2176 int
2177 rte_eth_dev_bypass_state_show(uint8_t port_id, uint32_t *state)
2178 {
2179         struct rte_eth_dev *dev;
2180
2181         if (port_id >= nb_ports) {
2182                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
2183                 return (-ENODEV);
2184         }
2185
2186         if ((dev= &rte_eth_devices[port_id]) == NULL) {
2187                 PMD_DEBUG_TRACE("Invalid port device\n");
2188                 return (-ENODEV);
2189         }
2190         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_state_show, -ENOTSUP);
2191         (*dev->dev_ops->bypass_state_show)(dev, state);
2192         return 0;
2193 }
2194
2195 int
2196 rte_eth_dev_bypass_state_set(uint8_t port_id, uint32_t *new_state)
2197 {
2198         struct rte_eth_dev *dev;
2199
2200         if (port_id >= nb_ports) {
2201                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
2202                 return (-ENODEV);
2203         }
2204
2205         if ((dev= &rte_eth_devices[port_id]) == NULL) {
2206                 PMD_DEBUG_TRACE("Invalid port device\n");
2207                 return (-ENODEV);
2208         }
2209
2210         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_state_set, -ENOTSUP);
2211         (*dev->dev_ops->bypass_state_set)(dev, new_state);
2212         return 0;
2213 }
2214
2215 int
2216 rte_eth_dev_bypass_event_show(uint8_t port_id, uint32_t event, uint32_t *state)
2217 {
2218         struct rte_eth_dev *dev;
2219
2220         if (port_id >= nb_ports) {
2221                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
2222                 return (-ENODEV);
2223         }
2224
2225         if ((dev= &rte_eth_devices[port_id]) == NULL) {
2226                 PMD_DEBUG_TRACE("Invalid port device\n");
2227                 return (-ENODEV);
2228         }
2229
2230         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_state_show, -ENOTSUP);
2231         (*dev->dev_ops->bypass_event_show)(dev, event, state);
2232         return 0;
2233 }
2234
2235 int
2236 rte_eth_dev_bypass_event_store(uint8_t port_id, uint32_t event, uint32_t state)
2237 {
2238         struct rte_eth_dev *dev;
2239
2240         if (port_id >= nb_ports) {
2241                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
2242                 return (-ENODEV);
2243         }
2244
2245         if ((dev= &rte_eth_devices[port_id]) == NULL) {
2246                 PMD_DEBUG_TRACE("Invalid port device\n");
2247                 return (-ENODEV);
2248         }
2249
2250         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_event_set, -ENOTSUP);
2251         (*dev->dev_ops->bypass_event_set)(dev, event, state);
2252         return 0;
2253 }
2254
2255 int
2256 rte_eth_dev_wd_timeout_store(uint8_t port_id, uint32_t timeout)
2257 {
2258         struct rte_eth_dev *dev;
2259
2260         if (port_id >= nb_ports) {
2261                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
2262                 return (-ENODEV);
2263         }
2264
2265         if ((dev= &rte_eth_devices[port_id]) == NULL) {
2266                 PMD_DEBUG_TRACE("Invalid port device\n");
2267                 return (-ENODEV);
2268         }
2269
2270         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_wd_timeout_set, -ENOTSUP);
2271         (*dev->dev_ops->bypass_wd_timeout_set)(dev, timeout);
2272         return 0;
2273 }
2274
2275 int
2276 rte_eth_dev_bypass_ver_show(uint8_t port_id, uint32_t *ver)
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 (-ENODEV);
2283         }
2284
2285         if ((dev= &rte_eth_devices[port_id]) == NULL) {
2286                 PMD_DEBUG_TRACE("Invalid port device\n");
2287                 return (-ENODEV);
2288         }
2289
2290         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_ver_show, -ENOTSUP);
2291         (*dev->dev_ops->bypass_ver_show)(dev, ver);
2292         return 0;
2293 }
2294
2295 int
2296 rte_eth_dev_bypass_wd_timeout_show(uint8_t port_id, uint32_t *wd_timeout)
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 (-ENODEV);
2303         }
2304
2305         if ((dev= &rte_eth_devices[port_id]) == NULL) {
2306                 PMD_DEBUG_TRACE("Invalid port device\n");
2307                 return (-ENODEV);
2308         }
2309
2310         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_wd_timeout_show, -ENOTSUP);
2311         (*dev->dev_ops->bypass_wd_timeout_show)(dev, wd_timeout);
2312         return 0;
2313 }
2314
2315 int
2316 rte_eth_dev_bypass_wd_reset(uint8_t port_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 (-ENODEV);
2323         }
2324
2325         if ((dev= &rte_eth_devices[port_id]) == NULL) {
2326                 PMD_DEBUG_TRACE("Invalid port device\n");
2327                 return (-ENODEV);
2328         }
2329
2330         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->bypass_wd_reset, -ENOTSUP);
2331         (*dev->dev_ops->bypass_wd_reset)(dev);
2332         return 0;
2333 }
2334 #endif