pci: rework interrupt handling
[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
35 #include <sys/types.h>
36 #include <sys/queue.h>
37 #include <ctype.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <stdarg.h>
42 #include <errno.h>
43 #include <stdint.h>
44 #include <inttypes.h>
45
46 #include <rte_byteorder.h>
47 #include <rte_log.h>
48 #include <rte_debug.h>
49 #include <rte_interrupts.h>
50 #include <rte_pci.h>
51 #include <rte_memory.h>
52 #include <rte_memcpy.h>
53 #include <rte_memzone.h>
54 #include <rte_launch.h>
55 #include <rte_tailq.h>
56 #include <rte_eal.h>
57 #include <rte_per_lcore.h>
58 #include <rte_lcore.h>
59 #include <rte_atomic.h>
60 #include <rte_branch_prediction.h>
61 #include <rte_common.h>
62 #include <rte_ring.h>
63 #include <rte_mempool.h>
64 #include <rte_malloc.h>
65 #include <rte_mbuf.h>
66 #include <rte_errno.h>
67 #include <rte_spinlock.h>
68
69 #include "rte_ether.h"
70 #include "rte_ethdev.h"
71
72 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
73 #define PMD_DEBUG_TRACE(fmt, args...) do {                        \
74                 RTE_LOG(ERR, PMD, "%s: " fmt, __func__, ## args); \
75         } while (0)
76 #else
77 #define PMD_DEBUG_TRACE(fmt, args...)
78 #endif
79
80 /* Macros for checking for restricting functions to primary instance only */
81 #define PROC_PRIMARY_OR_ERR_RET(retval) do { \
82         if (rte_eal_process_type() != RTE_PROC_PRIMARY) { \
83                 PMD_DEBUG_TRACE("Cannot run in secondary processes\n"); \
84                 return (retval); \
85         } \
86 } while(0)
87 #define PROC_PRIMARY_OR_RET() do { \
88         if (rte_eal_process_type() != RTE_PROC_PRIMARY) { \
89                 PMD_DEBUG_TRACE("Cannot run in secondary processes\n"); \
90                 return; \
91         } \
92 } while(0)
93
94 /* Macros to check for invlaid function pointers in dev_ops structure */
95 #define FUNC_PTR_OR_ERR_RET(func, retval) do { \
96         if ((func) == NULL) { \
97                 PMD_DEBUG_TRACE("Function not supported\n"); \
98                 return (retval); \
99         } \
100 } while(0)
101 #define FUNC_PTR_OR_RET(func) do { \
102         if ((func) == NULL) { \
103                 PMD_DEBUG_TRACE("Function not supported\n"); \
104                 return; \
105         } \
106 } while(0)
107
108 static const char *MZ_RTE_ETH_DEV_DATA = "rte_eth_dev_data";
109 struct rte_eth_dev rte_eth_devices[RTE_MAX_ETHPORTS];
110 static struct rte_eth_dev_data *rte_eth_dev_data = NULL;
111 static uint8_t nb_ports = 0;
112
113 /* spinlock for eth device callbacks */
114 static rte_spinlock_t rte_eth_dev_cb_lock = RTE_SPINLOCK_INITIALIZER;
115
116 /**
117  * The user application callback description.
118  *
119  * It contains callback address to be registered by user application,
120  * the pointer to the parameters for callback, and the event type.
121  */
122 struct rte_eth_dev_callback {
123         TAILQ_ENTRY(rte_eth_dev_callback) next; /**< Callbacks list */
124         rte_eth_dev_cb_fn cb_fn;                /**< Callback address */
125         void *cb_arg;                           /**< Parameter for callback */
126         enum rte_eth_event_type event;          /**< Interrupt event type */
127         uint32_t active;                        /**< Callback is executing */
128 };
129
130 enum {
131         STAT_QMAP_TX = 0,
132         STAT_QMAP_RX
133 };
134
135 static inline void
136 rte_eth_dev_data_alloc(void)
137 {
138         const unsigned flags = 0;
139         const struct rte_memzone *mz;
140
141         if (rte_eal_process_type() == RTE_PROC_PRIMARY){
142                 mz = rte_memzone_reserve(MZ_RTE_ETH_DEV_DATA,
143                                 RTE_MAX_ETHPORTS * sizeof(*rte_eth_dev_data),
144                                 rte_socket_id(), flags);
145         } else
146                 mz = rte_memzone_lookup(MZ_RTE_ETH_DEV_DATA);
147         if (mz == NULL)
148                 rte_panic("Cannot allocate memzone for ethernet port data\n");
149
150         rte_eth_dev_data = mz->addr;
151         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
152                 memset(rte_eth_dev_data, 0,
153                                 RTE_MAX_ETHPORTS * sizeof(*rte_eth_dev_data));
154 }
155
156 static inline struct rte_eth_dev *
157 rte_eth_dev_allocate(void)
158 {
159         struct rte_eth_dev *eth_dev;
160
161         if (nb_ports == RTE_MAX_ETHPORTS) {
162                 PMD_DEBUG_TRACE("Reached maximum number of ethernet ports\n");
163                 return NULL;
164         }
165
166         if (rte_eth_dev_data == NULL)
167                 rte_eth_dev_data_alloc();
168
169         eth_dev = &rte_eth_devices[nb_ports];
170         eth_dev->data = &rte_eth_dev_data[nb_ports];
171         eth_dev->data->port_id = nb_ports++;
172         return eth_dev;
173 }
174
175 static int
176 rte_eth_dev_init(struct rte_pci_driver *pci_drv,
177                  struct rte_pci_device *pci_dev)
178 {
179         struct eth_driver    *eth_drv;
180         struct rte_eth_dev *eth_dev;
181         int diag;
182
183         eth_drv = (struct eth_driver *)pci_drv;
184
185         eth_dev = rte_eth_dev_allocate();
186         if (eth_dev == NULL)
187                 return -ENOMEM;
188
189         if (rte_eal_process_type() == RTE_PROC_PRIMARY){
190                 eth_dev->data->dev_private = rte_zmalloc("ethdev private structure",
191                                   eth_drv->dev_private_size,
192                                   CACHE_LINE_SIZE);
193                 if (eth_dev->data->dev_private == NULL)
194                         rte_panic("Cannot allocate memzone for private port data\n");
195         }
196         eth_dev->pci_dev = pci_dev;
197         eth_dev->driver = eth_drv;
198         eth_dev->data->rx_mbuf_alloc_failed = 0;
199
200         /* init user callbacks */
201         TAILQ_INIT(&(eth_dev->callbacks));
202
203         /*
204          * Set the default maximum frame size.
205          */
206         eth_dev->data->max_frame_size = ETHER_MAX_LEN;
207
208         /* Invoke PMD device initialization function */
209         diag = (*eth_drv->eth_dev_init)(eth_drv, eth_dev);
210         if (diag == 0)
211                 return (0);
212
213         PMD_DEBUG_TRACE("driver %s: eth_dev_init(vendor_id=0x%u device_id=0x%x)"
214                         " failed\n", pci_drv->name,
215                         (unsigned) pci_dev->id.vendor_id,
216                         (unsigned) pci_dev->id.device_id);
217         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
218                 rte_free(eth_dev->data->dev_private);
219         nb_ports--;
220         return diag;
221 }
222
223 /**
224  * Register an Ethernet [Poll Mode] driver.
225  *
226  * Function invoked by the initialization function of an Ethernet driver
227  * to simultaneously register itself as a PCI driver and as an Ethernet
228  * Poll Mode Driver.
229  * Invokes the rte_eal_pci_register() function to register the *pci_drv*
230  * structure embedded in the *eth_drv* structure, after having stored the
231  * address of the rte_eth_dev_init() function in the *devinit* field of
232  * the *pci_drv* structure.
233  * During the PCI probing phase, the rte_eth_dev_init() function is
234  * invoked for each PCI [Ethernet device] matching the embedded PCI
235  * identifiers provided by the driver.
236  */
237 void
238 rte_eth_driver_register(struct eth_driver *eth_drv)
239 {
240         eth_drv->pci_drv.devinit = rte_eth_dev_init;
241         rte_eal_pci_register(&eth_drv->pci_drv);
242 }
243
244 uint8_t
245 rte_eth_dev_count(void)
246 {
247         return (nb_ports);
248 }
249
250 static int
251 rte_eth_dev_rx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
252 {
253         uint16_t old_nb_queues = dev->data->nb_rx_queues;
254         void **rxq;
255         unsigned i;
256
257         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_release, -ENOTSUP);
258
259         if (dev->data->rx_queues == NULL) {
260                 dev->data->rx_queues = rte_zmalloc("ethdev->rx_queues",
261                                 sizeof(dev->data->rx_queues[0]) * nb_queues,
262                                 CACHE_LINE_SIZE);
263                 if (dev->data->rx_queues == NULL) {
264                         dev->data->nb_rx_queues = 0;
265                         return -(ENOMEM);
266                 }
267         } else {
268                 rxq = dev->data->rx_queues;
269
270                 for (i = nb_queues; i < old_nb_queues; i++)
271                         (*dev->dev_ops->rx_queue_release)(rxq[i]);
272                 rxq = rte_realloc(rxq, sizeof(rxq[0]) * nb_queues,
273                                 CACHE_LINE_SIZE);
274                 if (rxq == NULL)
275                         return -(ENOMEM);
276
277                 if (nb_queues > old_nb_queues)
278                         memset(rxq + old_nb_queues, 0,
279                                 sizeof(rxq[0]) * (nb_queues - old_nb_queues));
280
281                 dev->data->rx_queues = rxq;
282
283         }
284         dev->data->nb_rx_queues = nb_queues;
285         return (0);
286 }
287
288 static int
289 rte_eth_dev_tx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
290 {
291         uint16_t old_nb_queues = dev->data->nb_tx_queues;
292         void **txq;
293         unsigned i;
294
295         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_release, -ENOTSUP);
296
297         if (dev->data->tx_queues == NULL) {
298                 dev->data->tx_queues = rte_zmalloc("ethdev->tx_queues",
299                                 sizeof(dev->data->tx_queues[0]) * nb_queues,
300                                 CACHE_LINE_SIZE);
301                 if (dev->data->tx_queues == NULL) {
302                         dev->data->nb_tx_queues = 0;
303                         return -(ENOMEM);
304                 }
305         } else {
306                 txq = dev->data->tx_queues;
307
308                 for (i = nb_queues; i < old_nb_queues; i++)
309                         (*dev->dev_ops->tx_queue_release)(txq[i]);
310                 txq = rte_realloc(txq, sizeof(txq[0]) * nb_queues,
311                                 CACHE_LINE_SIZE);
312                 if (txq == NULL)
313                         return -(ENOMEM);
314
315                 if (nb_queues > old_nb_queues)
316                         memset(txq + old_nb_queues, 0,
317                                 sizeof(txq[0]) * (nb_queues - old_nb_queues));
318
319                 dev->data->tx_queues = txq;
320
321         }
322         dev->data->nb_tx_queues = nb_queues;
323         return (0);
324 }
325
326 int
327 rte_eth_dev_configure(uint8_t port_id, uint16_t nb_rx_q, uint16_t nb_tx_q,
328                       const struct rte_eth_conf *dev_conf)
329 {
330         struct rte_eth_dev *dev;
331         struct rte_eth_dev_info dev_info;
332         int diag;
333
334         /* This function is only safe when called from the primary process
335          * in a multi-process setup*/
336         PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
337
338         if (port_id >= nb_ports || port_id >= RTE_MAX_ETHPORTS) {
339                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
340                 return (-EINVAL);
341         }
342         dev = &rte_eth_devices[port_id];
343
344         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
345         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_configure, -ENOTSUP);
346
347         if (dev->data->dev_started) {
348                 PMD_DEBUG_TRACE(
349                     "port %d must be stopped to allow configuration\n", port_id);
350                 return (-EBUSY);
351         }
352
353         /*
354          * Check that the numbers of RX and TX queues are not greater
355          * than the maximum number of RX and TX queues supported by the
356          * configured device.
357          */
358         (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
359         if (nb_rx_q > dev_info.max_rx_queues) {
360                 PMD_DEBUG_TRACE("ethdev port_id=%d nb_rx_queues=%d > %d\n",
361                                 port_id, nb_rx_q, dev_info.max_rx_queues);
362                 return (-EINVAL);
363         }
364         if (nb_rx_q == 0) {
365                 PMD_DEBUG_TRACE("ethdev port_id=%d nb_rx_q == 0\n", port_id);
366                 return (-EINVAL);
367         }
368
369         if (nb_tx_q > dev_info.max_tx_queues) {
370                 PMD_DEBUG_TRACE("ethdev port_id=%d nb_tx_queues=%d > %d\n",
371                                 port_id, nb_tx_q, dev_info.max_tx_queues);
372                 return (-EINVAL);
373         }
374         if (nb_tx_q == 0) {
375                 PMD_DEBUG_TRACE("ethdev port_id=%d nb_tx_q == 0\n", port_id);
376                 return (-EINVAL);
377         }
378
379         /* Copy the dev_conf parameter into the dev structure */
380         memcpy(&dev->data->dev_conf, dev_conf, sizeof(dev->data->dev_conf));
381
382         /*
383          * If jumbo frames are enabled, check that the maximum RX packet
384          * length is supported by the configured device.
385          */
386         if (dev_conf->rxmode.jumbo_frame == 1) {
387                 if (dev_conf->rxmode.max_rx_pkt_len >
388                     dev_info.max_rx_pktlen) {
389                         PMD_DEBUG_TRACE("ethdev port_id=%d max_rx_pkt_len %u"
390                                 " > max valid value %u\n",
391                                 port_id,
392                                 (unsigned)dev_conf->rxmode.max_rx_pkt_len,
393                                 (unsigned)dev_info.max_rx_pktlen);
394                         return (-EINVAL);
395                 }
396                 else if (dev_conf->rxmode.max_rx_pkt_len < ETHER_MIN_LEN) {
397                         PMD_DEBUG_TRACE("ethdev port_id=%d max_rx_pkt_len %u"
398                                 " < min valid value %u\n",
399                                 port_id,
400                                 (unsigned)dev_conf->rxmode.max_rx_pkt_len,
401                                 (unsigned)ETHER_MIN_LEN);
402                         return (-EINVAL);
403                 }
404         } else
405                 /* Use default value */
406                 dev->data->dev_conf.rxmode.max_rx_pkt_len = ETHER_MAX_LEN;
407
408         /* For vmdb+dcb mode check our configuration before we go further */
409         if (dev_conf->rxmode.mq_mode == ETH_VMDQ_DCB) {
410                 const struct rte_eth_vmdq_dcb_conf *conf;
411
412                 if (nb_rx_q != ETH_VMDQ_DCB_NUM_QUEUES) {
413                         PMD_DEBUG_TRACE("ethdev port_id=%d VMDQ+DCB, nb_rx_q "
414                                         "!= %d\n",
415                                         port_id, ETH_VMDQ_DCB_NUM_QUEUES);
416                         return (-EINVAL);
417                 }
418                 conf = &(dev_conf->rx_adv_conf.vmdq_dcb_conf);
419                 if (! (conf->nb_queue_pools == ETH_16_POOLS ||
420                        conf->nb_queue_pools == ETH_32_POOLS)) {
421                     PMD_DEBUG_TRACE("ethdev port_id=%d VMDQ+DCB selected, "
422                                     "nb_queue_pools must be %d or %d\n",
423                                     port_id, ETH_16_POOLS, ETH_32_POOLS);
424                     return (-EINVAL);
425                 }
426         }
427         if (dev_conf->txmode.mq_mode == ETH_VMDQ_DCB_TX) {
428                 const struct rte_eth_vmdq_dcb_tx_conf *conf;
429
430                 if (nb_tx_q != ETH_VMDQ_DCB_NUM_QUEUES) {
431                         PMD_DEBUG_TRACE("ethdev port_id=%d VMDQ+DCB, nb_tx_q "
432                                         "!= %d\n",
433                                         port_id, ETH_VMDQ_DCB_NUM_QUEUES);
434                         return (-EINVAL);
435                 }
436                 conf = &(dev_conf->tx_adv_conf.vmdq_dcb_tx_conf);
437                 if (! (conf->nb_queue_pools == ETH_16_POOLS ||
438                        conf->nb_queue_pools == ETH_32_POOLS)) {
439                         PMD_DEBUG_TRACE("ethdev port_id=%d VMDQ+DCB selected, "
440                                     "nb_queue_pools != %d or nb_queue_pools "
441                                     "!= %d\n",
442                                     port_id, ETH_16_POOLS, ETH_32_POOLS);
443                         return (-EINVAL);
444                 }
445         }
446         
447         /* For DCB mode check our configuration before we go further */
448         if (dev_conf->rxmode.mq_mode == ETH_DCB_RX) {
449                 const struct rte_eth_dcb_rx_conf *conf;
450
451                 if (nb_rx_q != ETH_DCB_NUM_QUEUES) {
452                         PMD_DEBUG_TRACE("ethdev port_id=%d DCB, nb_rx_q "
453                                         "!= %d\n",
454                                         port_id, ETH_DCB_NUM_QUEUES);
455                         return (-EINVAL);
456                 }
457                 conf = &(dev_conf->rx_adv_conf.dcb_rx_conf);
458                 if (! (conf->nb_tcs == ETH_4_TCS ||
459                        conf->nb_tcs == ETH_8_TCS)) {
460                         PMD_DEBUG_TRACE("ethdev port_id=%d DCB selected, "
461                                     "nb_tcs != %d or nb_tcs "
462                                     "!= %d\n",
463                                     port_id, ETH_4_TCS, ETH_8_TCS);
464                         return (-EINVAL);
465                 }
466         }
467
468         if (dev_conf->txmode.mq_mode == ETH_DCB_TX) {
469                 const struct rte_eth_dcb_tx_conf *conf;
470
471                 if (nb_tx_q != ETH_DCB_NUM_QUEUES) {
472                         PMD_DEBUG_TRACE("ethdev port_id=%d DCB, nb_tx_q "
473                                         "!= %d\n",
474                                         port_id, ETH_DCB_NUM_QUEUES);
475                         return (-EINVAL);
476                 }
477                 conf = &(dev_conf->tx_adv_conf.dcb_tx_conf);
478                 if (! (conf->nb_tcs == ETH_4_TCS ||
479                        conf->nb_tcs == ETH_8_TCS)) {
480                         PMD_DEBUG_TRACE("ethdev port_id=%d DCB selected, "
481                                     "nb_tcs != %d or nb_tcs "
482                                     "!= %d\n",
483                                     port_id, ETH_4_TCS, ETH_8_TCS);
484                         return (-EINVAL);
485                 }
486         }
487
488         /*
489          * Setup new number of RX/TX queues and reconfigure device.
490          */
491         diag = rte_eth_dev_rx_queue_config(dev, nb_rx_q);
492         if (diag != 0) {
493                 PMD_DEBUG_TRACE("port%d rte_eth_dev_rx_queue_config = %d\n",
494                                 port_id, diag);
495                 return diag;
496         }
497
498         diag = rte_eth_dev_tx_queue_config(dev, nb_tx_q);
499         if (diag != 0) {
500                 PMD_DEBUG_TRACE("port%d rte_eth_dev_tx_queue_config = %d\n",
501                                 port_id, diag);
502                 rte_eth_dev_rx_queue_config(dev, 0);
503                 return diag;
504         }
505
506         diag = (*dev->dev_ops->dev_configure)(dev);
507         if (diag != 0) {
508                 PMD_DEBUG_TRACE("port%d dev_configure = %d\n",
509                                 port_id, diag);
510                 rte_eth_dev_rx_queue_config(dev, 0);
511                 rte_eth_dev_tx_queue_config(dev, 0);
512                 return diag;
513         }
514
515         return 0;
516 }
517
518 static void
519 rte_eth_dev_config_restore(uint8_t port_id)
520 {
521         struct rte_eth_dev *dev;
522         struct rte_eth_dev_info dev_info;
523         struct ether_addr addr;
524         uint16_t i;
525
526         dev = &rte_eth_devices[port_id];
527
528         rte_eth_dev_info_get(port_id, &dev_info);
529
530         /* replay MAC address configuration */
531         for (i = 0; i < dev_info.max_mac_addrs; i++) {
532                 addr = dev->data->mac_addrs[i];
533
534                 /* skip zero address */
535                 if (is_zero_ether_addr(&addr))
536                         continue;
537
538                 /* add address to the hardware */
539                 if  (*dev->dev_ops->mac_addr_add)
540                         (*dev->dev_ops->mac_addr_add)(dev, &addr, i, 0);
541                 else {
542                         PMD_DEBUG_TRACE("port %d: MAC address array not supported\n",
543                                         port_id);
544                         /* exit the loop but not return an error */
545                         break;
546                 }
547         }
548
549         /* replay promiscuous configuration */
550         if (rte_eth_promiscuous_get(port_id) == 1)
551                 rte_eth_promiscuous_enable(port_id);
552         else if (rte_eth_promiscuous_get(port_id) == 0)
553                 rte_eth_promiscuous_disable(port_id);
554
555         /* replay allmulticast configuration */
556         if (rte_eth_allmulticast_get(port_id) == 1)
557                 rte_eth_allmulticast_enable(port_id);
558         else if (rte_eth_allmulticast_get(port_id) == 0)
559                 rte_eth_allmulticast_disable(port_id);
560 }
561
562 int
563 rte_eth_dev_start(uint8_t port_id)
564 {
565         struct rte_eth_dev *dev;
566         int diag;
567
568         /* This function is only safe when called from the primary process
569          * in a multi-process setup*/
570         PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
571
572         if (port_id >= nb_ports) {
573                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
574                 return (-EINVAL);
575         }
576         dev = &rte_eth_devices[port_id];
577
578         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
579         diag = (*dev->dev_ops->dev_start)(dev);
580         if (diag == 0)
581                 dev->data->dev_started = 1;
582         else
583                 return diag;
584
585         rte_eth_dev_config_restore(port_id);
586
587         return 0;
588 }
589
590 void
591 rte_eth_dev_stop(uint8_t port_id)
592 {
593         struct rte_eth_dev *dev;
594
595         /* This function is only safe when called from the primary process
596          * in a multi-process setup*/
597         PROC_PRIMARY_OR_RET();
598
599         if (port_id >= nb_ports) {
600                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
601                 return;
602         }
603         dev = &rte_eth_devices[port_id];
604
605         FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
606         dev->data->dev_started = 0;
607         (*dev->dev_ops->dev_stop)(dev);
608 }
609
610 void
611 rte_eth_dev_close(uint8_t port_id)
612 {
613         struct rte_eth_dev *dev;
614
615         /* This function is only safe when called from the primary process
616          * in a multi-process setup*/
617         PROC_PRIMARY_OR_RET();
618
619         if (port_id >= nb_ports) {
620                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
621                 return;
622         }
623
624         dev = &rte_eth_devices[port_id];
625
626         FUNC_PTR_OR_RET(*dev->dev_ops->dev_close);
627         dev->data->dev_started = 0;
628         (*dev->dev_ops->dev_close)(dev);
629 }
630
631 int
632 rte_eth_rx_queue_setup(uint8_t port_id, uint16_t rx_queue_id,
633                        uint16_t nb_rx_desc, unsigned int socket_id,
634                        const struct rte_eth_rxconf *rx_conf,
635                        struct rte_mempool *mp)
636 {
637         struct rte_eth_dev *dev;
638         struct rte_pktmbuf_pool_private *mbp_priv;
639         struct rte_eth_dev_info dev_info;
640
641         /* This function is only safe when called from the primary process
642          * in a multi-process setup*/
643         PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
644
645         if (port_id >= nb_ports) {
646                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
647                 return (-EINVAL);
648         }
649         dev = &rte_eth_devices[port_id];
650         if (rx_queue_id >= dev->data->nb_rx_queues) {
651                 PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", rx_queue_id);
652                 return (-EINVAL);
653         }
654
655         if (dev->data->dev_started) {
656                 PMD_DEBUG_TRACE(
657                     "port %d must be stopped to allow configuration\n", port_id);
658                 return -EBUSY;
659         }
660
661         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
662         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_setup, -ENOTSUP);
663
664         /*
665          * Check the size of the mbuf data buffer.
666          * This value must be provided in the private data of the memory pool.
667          * First check that the memory pool has a valid private data.
668          */
669         (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
670         if (mp->private_data_size < sizeof(struct rte_pktmbuf_pool_private)) {
671                 PMD_DEBUG_TRACE("%s private_data_size %d < %d\n",
672                                 mp->name, (int) mp->private_data_size,
673                                 (int) sizeof(struct rte_pktmbuf_pool_private));
674                 return (-ENOSPC);
675         }
676         mbp_priv = (struct rte_pktmbuf_pool_private *)
677                 ((char *)mp + sizeof(struct rte_mempool));
678         if ((uint32_t) (mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM) <
679             dev_info.min_rx_bufsize) {
680                 PMD_DEBUG_TRACE("%s mbuf_data_room_size %d < %d "
681                                 "(RTE_PKTMBUF_HEADROOM=%d + min_rx_bufsize(dev)"
682                                 "=%d)\n",
683                                 mp->name,
684                                 (int)mbp_priv->mbuf_data_room_size,
685                                 (int)(RTE_PKTMBUF_HEADROOM +
686                                       dev_info.min_rx_bufsize),
687                                 (int)RTE_PKTMBUF_HEADROOM,
688                                 (int)dev_info.min_rx_bufsize);
689                 return (-EINVAL);
690         }
691
692         return (*dev->dev_ops->rx_queue_setup)(dev, rx_queue_id, nb_rx_desc,
693                                                socket_id, rx_conf, mp);
694 }
695
696 int
697 rte_eth_tx_queue_setup(uint8_t port_id, uint16_t tx_queue_id,
698                        uint16_t nb_tx_desc, unsigned int socket_id,
699                        const struct rte_eth_txconf *tx_conf)
700 {
701         struct rte_eth_dev *dev;
702
703         /* This function is only safe when called from the primary process
704          * in a multi-process setup*/
705         PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
706
707         if (port_id >= RTE_MAX_ETHPORTS || port_id >= nb_ports) {
708                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
709                 return (-EINVAL);
710         }
711         dev = &rte_eth_devices[port_id];
712         if (tx_queue_id >= dev->data->nb_tx_queues) {
713                 PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", tx_queue_id);
714                 return (-EINVAL);
715         }
716
717         if (dev->data->dev_started) {
718                 PMD_DEBUG_TRACE(
719                     "port %d must be stopped to allow configuration\n", port_id);
720                 return -EBUSY;
721         }
722
723         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_setup, -ENOTSUP);
724         return (*dev->dev_ops->tx_queue_setup)(dev, tx_queue_id, nb_tx_desc,
725                                                socket_id, tx_conf);
726 }
727
728 void
729 rte_eth_promiscuous_enable(uint8_t port_id)
730 {
731         struct rte_eth_dev *dev;
732
733         if (port_id >= nb_ports) {
734                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
735                 return;
736         }
737         dev = &rte_eth_devices[port_id];
738
739         FUNC_PTR_OR_RET(*dev->dev_ops->promiscuous_enable);
740         (*dev->dev_ops->promiscuous_enable)(dev);
741         dev->data->promiscuous = 1;
742 }
743
744 void
745 rte_eth_promiscuous_disable(uint8_t port_id)
746 {
747         struct rte_eth_dev *dev;
748
749         if (port_id >= nb_ports) {
750                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
751                 return;
752         }
753         dev = &rte_eth_devices[port_id];
754
755         FUNC_PTR_OR_RET(*dev->dev_ops->promiscuous_disable);
756         dev->data->promiscuous = 0;
757         (*dev->dev_ops->promiscuous_disable)(dev);
758 }
759
760 int
761 rte_eth_promiscuous_get(uint8_t port_id)
762 {
763         struct rte_eth_dev *dev;
764
765         if (port_id >= nb_ports) {
766                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
767                 return -1;
768         }
769
770         dev = &rte_eth_devices[port_id];
771         return dev->data->promiscuous;
772 }
773
774 void
775 rte_eth_allmulticast_enable(uint8_t port_id)
776 {
777         struct rte_eth_dev *dev;
778
779         if (port_id >= nb_ports) {
780                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
781                 return;
782         }
783         dev = &rte_eth_devices[port_id];
784
785         FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_enable);
786         (*dev->dev_ops->allmulticast_enable)(dev);
787         dev->data->all_multicast = 1;
788 }
789
790 void
791 rte_eth_allmulticast_disable(uint8_t port_id)
792 {
793         struct rte_eth_dev *dev;
794
795         if (port_id >= nb_ports) {
796                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
797                 return;
798         }
799         dev = &rte_eth_devices[port_id];
800
801         FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_disable);
802         dev->data->all_multicast = 0;
803         (*dev->dev_ops->allmulticast_disable)(dev);
804 }
805
806 int
807 rte_eth_allmulticast_get(uint8_t port_id)
808 {
809         struct rte_eth_dev *dev;
810
811         if (port_id >= nb_ports) {
812                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
813                 return -1;
814         }
815
816         dev = &rte_eth_devices[port_id];
817         return dev->data->all_multicast;
818 }
819
820 static inline int
821 rte_eth_dev_atomic_read_link_status(struct rte_eth_dev *dev,
822                                 struct rte_eth_link *link)
823 {
824         struct rte_eth_link *dst = link;
825         struct rte_eth_link *src = &(dev->data->dev_link);
826
827         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
828                                         *(uint64_t *)src) == 0)
829                 return -1;
830
831         return 0;
832 }
833
834 void
835 rte_eth_link_get(uint8_t port_id, struct rte_eth_link *eth_link)
836 {
837         struct rte_eth_dev *dev;
838
839         if (port_id >= nb_ports) {
840                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
841                 return;
842         }
843         dev = &rte_eth_devices[port_id];
844         FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
845
846         if (dev->data->dev_conf.intr_conf.lsc != 0)
847                 rte_eth_dev_atomic_read_link_status(dev, eth_link);
848         else {
849                 (*dev->dev_ops->link_update)(dev, 1);
850                 *eth_link = dev->data->dev_link;
851         }
852 }
853
854 void
855 rte_eth_link_get_nowait(uint8_t port_id, struct rte_eth_link *eth_link)
856 {
857         struct rte_eth_dev *dev;
858
859         if (port_id >= nb_ports) {
860                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
861                 return;
862         }
863         dev = &rte_eth_devices[port_id];
864         FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
865
866         if (dev->data->dev_conf.intr_conf.lsc != 0)
867                 rte_eth_dev_atomic_read_link_status(dev, eth_link);
868         else {
869                 (*dev->dev_ops->link_update)(dev, 0);
870                 *eth_link = dev->data->dev_link;
871         }
872 }
873
874 void
875 rte_eth_stats_get(uint8_t port_id, struct rte_eth_stats *stats)
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->stats_get);
886         (*dev->dev_ops->stats_get)(dev, stats);
887         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
888 }
889
890 void
891 rte_eth_stats_reset(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;
898         }
899         dev = &rte_eth_devices[port_id];
900
901         FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
902         (*dev->dev_ops->stats_reset)(dev);
903 }
904
905
906 static int
907 set_queue_stats_mapping(uint8_t port_id, uint16_t queue_id, uint8_t stat_idx,
908                 uint8_t is_rx)
909 {
910         struct rte_eth_dev *dev;
911
912         if (port_id >= nb_ports) {
913                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
914                 return -ENODEV;
915         }
916         dev = &rte_eth_devices[port_id];
917
918         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_stats_mapping_set, -ENOTSUP);
919         return (*dev->dev_ops->queue_stats_mapping_set)
920                         (dev, queue_id, stat_idx, is_rx);
921 }
922
923
924 int
925 rte_eth_dev_set_tx_queue_stats_mapping(uint8_t port_id, uint16_t tx_queue_id,
926                 uint8_t stat_idx)
927 {
928         return set_queue_stats_mapping(port_id, tx_queue_id, stat_idx,
929                         STAT_QMAP_TX);
930 }
931
932
933 int
934 rte_eth_dev_set_rx_queue_stats_mapping(uint8_t port_id, uint16_t rx_queue_id,
935                 uint8_t stat_idx)
936 {
937         return set_queue_stats_mapping(port_id, rx_queue_id, stat_idx,
938                         STAT_QMAP_RX);
939 }
940
941
942 void
943 rte_eth_dev_info_get(uint8_t port_id, struct rte_eth_dev_info *dev_info)
944 {
945         struct rte_eth_dev *dev;
946
947         if (port_id >= nb_ports) {
948                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
949                 return;
950         }
951         dev = &rte_eth_devices[port_id];
952
953         FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
954         (*dev->dev_ops->dev_infos_get)(dev, dev_info);
955         dev_info->pci_dev = dev->pci_dev;
956         dev_info->driver_name = dev->driver->pci_drv.name;
957 }
958
959 void
960 rte_eth_macaddr_get(uint8_t port_id, struct ether_addr *mac_addr)
961 {
962         struct rte_eth_dev *dev;
963
964         if (port_id >= nb_ports) {
965                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
966                 return;
967         }
968         dev = &rte_eth_devices[port_id];
969         ether_addr_copy(&dev->data->mac_addrs[0], mac_addr);
970 }
971
972 int
973 rte_eth_dev_vlan_filter(uint8_t port_id, uint16_t vlan_id, int on)
974 {
975         struct rte_eth_dev *dev;
976
977         if (port_id >= nb_ports) {
978                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
979                 return (-ENODEV);
980         }
981         dev = &rte_eth_devices[port_id];
982         if (! (dev->data->dev_conf.rxmode.hw_vlan_filter)) {
983                 PMD_DEBUG_TRACE("port %d: vlan-filtering disabled\n", port_id);
984                 return (-ENOSYS);
985         }
986
987         if (vlan_id > 4095) {
988                 PMD_DEBUG_TRACE("(port_id=%d) invalid vlan_id=%u > 4095\n",
989                                 port_id, (unsigned) vlan_id);
990                 return (-EINVAL);
991         }
992         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_filter_set, -ENOTSUP);
993         (*dev->dev_ops->vlan_filter_set)(dev, vlan_id, on);
994         return (0);
995 }
996
997 int
998 rte_eth_dev_set_vlan_strip_on_queue(uint8_t port_id, uint16_t rx_queue_id, int on)
999 {
1000         struct rte_eth_dev *dev;
1001
1002         if (port_id >= nb_ports) {
1003                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1004                 return (-ENODEV);
1005         }
1006
1007         dev = &rte_eth_devices[port_id];
1008         if (rx_queue_id >= dev->data->nb_rx_queues) {
1009                 PMD_DEBUG_TRACE("Invalid rx_queue_id=%d\n", port_id);
1010                 return (-EINVAL);
1011         }
1012
1013         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_strip_queue_set, -ENOTSUP);
1014         (*dev->dev_ops->vlan_strip_queue_set)(dev, rx_queue_id, on);
1015
1016         return (0);
1017 }
1018
1019 int
1020 rte_eth_dev_set_vlan_ether_type(uint8_t port_id, uint16_t tpid)
1021 {
1022         struct rte_eth_dev *dev;
1023
1024         if (port_id >= nb_ports) {
1025                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1026                 return (-ENODEV);
1027         }
1028
1029         dev = &rte_eth_devices[port_id];
1030         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_tpid_set, -ENOTSUP);
1031         (*dev->dev_ops->vlan_tpid_set)(dev, tpid);
1032
1033         return (0);
1034 }
1035
1036 int
1037 rte_eth_dev_set_vlan_offload(uint8_t port_id, int offload_mask)
1038 {
1039         struct rte_eth_dev *dev;
1040         int ret = 0;
1041         int mask = 0;
1042         int cur, org = 0;
1043         
1044         if (port_id >= nb_ports) {
1045                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1046                 return (-ENODEV);
1047         }
1048
1049         dev = &rte_eth_devices[port_id];
1050
1051         /*check which option changed by application*/
1052         cur = !!(offload_mask & ETH_VLAN_STRIP_OFFLOAD);
1053         org = !!(dev->data->dev_conf.rxmode.hw_vlan_strip);
1054         if (cur != org){
1055                 dev->data->dev_conf.rxmode.hw_vlan_strip = (uint8_t)cur;
1056                 mask |= ETH_VLAN_STRIP_MASK;
1057         }
1058         
1059         cur = !!(offload_mask & ETH_VLAN_FILTER_OFFLOAD);
1060         org = !!(dev->data->dev_conf.rxmode.hw_vlan_filter);
1061         if (cur != org){
1062                 dev->data->dev_conf.rxmode.hw_vlan_filter = (uint8_t)cur;
1063                 mask |= ETH_VLAN_FILTER_MASK;
1064         }
1065
1066         cur = !!(offload_mask & ETH_VLAN_EXTEND_OFFLOAD);
1067         org = !!(dev->data->dev_conf.rxmode.hw_vlan_extend);
1068         if (cur != org){
1069                 dev->data->dev_conf.rxmode.hw_vlan_extend = (uint8_t)cur;
1070                 mask |= ETH_VLAN_EXTEND_MASK;
1071         }
1072
1073         /*no change*/
1074         if(mask == 0)
1075                 return ret;
1076         
1077         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_offload_set, -ENOTSUP);
1078         (*dev->dev_ops->vlan_offload_set)(dev, mask);
1079
1080         return ret;
1081 }
1082
1083 int
1084 rte_eth_dev_get_vlan_offload(uint8_t port_id)
1085 {
1086         struct rte_eth_dev *dev;
1087         int ret = 0;
1088
1089         if (port_id >= nb_ports) {
1090                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1091                 return (-ENODEV);
1092         }
1093
1094         dev = &rte_eth_devices[port_id];
1095
1096         if (dev->data->dev_conf.rxmode.hw_vlan_strip)
1097                 ret |= ETH_VLAN_STRIP_OFFLOAD ;
1098
1099         if (dev->data->dev_conf.rxmode.hw_vlan_filter)
1100                 ret |= ETH_VLAN_FILTER_OFFLOAD ;
1101
1102         if (dev->data->dev_conf.rxmode.hw_vlan_extend)
1103                 ret |= ETH_VLAN_EXTEND_OFFLOAD ;
1104
1105         return ret;
1106 }
1107
1108
1109 int
1110 rte_eth_dev_fdir_add_signature_filter(uint8_t port_id,
1111                                       struct rte_fdir_filter *fdir_filter,
1112                                       uint8_t queue)
1113 {
1114         struct rte_eth_dev *dev;
1115
1116         if (port_id >= nb_ports) {
1117                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1118                 return (-ENODEV);
1119         }
1120
1121         dev = &rte_eth_devices[port_id];
1122
1123         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
1124                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1125                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1126                 return (-ENOSYS);
1127         }
1128
1129         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1130              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1131             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1132                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1133                                 "None l4type, source & destinations ports " \
1134                                 "should be null!\n");
1135                 return (-EINVAL);
1136         }
1137
1138         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_add_signature_filter, -ENOTSUP);
1139         return (*dev->dev_ops->fdir_add_signature_filter)(dev, fdir_filter,
1140                                                                 queue);
1141 }
1142
1143 int
1144 rte_eth_dev_fdir_update_signature_filter(uint8_t port_id,
1145                                          struct rte_fdir_filter *fdir_filter,
1146                                          uint8_t queue)
1147 {
1148         struct rte_eth_dev *dev;
1149
1150         if (port_id >= nb_ports) {
1151                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1152                 return (-ENODEV);
1153         }
1154
1155         dev = &rte_eth_devices[port_id];
1156
1157         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
1158                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1159                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1160                 return (-ENOSYS);
1161         }
1162
1163         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1164              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1165             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1166                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1167                                 "None l4type, source & destinations ports " \
1168                                 "should be null!\n");
1169                 return (-EINVAL);
1170         }
1171
1172         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_update_signature_filter, -ENOTSUP);
1173         return (*dev->dev_ops->fdir_update_signature_filter)(dev, fdir_filter,
1174                                                                 queue);
1175
1176 }
1177
1178 int
1179 rte_eth_dev_fdir_remove_signature_filter(uint8_t port_id,
1180                                          struct rte_fdir_filter *fdir_filter)
1181 {
1182         struct rte_eth_dev *dev;
1183
1184         if (port_id >= nb_ports) {
1185                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1186                 return (-ENODEV);
1187         }
1188
1189         dev = &rte_eth_devices[port_id];
1190
1191         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
1192                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1193                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1194                 return (-ENOSYS);
1195         }
1196
1197         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1198              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1199             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1200                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1201                                 "None l4type source & destinations ports " \
1202                                 "should be null!\n");
1203                 return (-EINVAL);
1204         }
1205
1206         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_remove_signature_filter, -ENOTSUP);
1207         return (*dev->dev_ops->fdir_remove_signature_filter)(dev, fdir_filter);
1208 }
1209
1210 int
1211 rte_eth_dev_fdir_get_infos(uint8_t port_id, struct rte_eth_fdir *fdir)
1212 {
1213         struct rte_eth_dev *dev;
1214
1215         if (port_id >= nb_ports) {
1216                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1217                 return (-ENODEV);
1218         }
1219
1220         dev = &rte_eth_devices[port_id];
1221         if (! (dev->data->dev_conf.fdir_conf.mode)) {
1222                 PMD_DEBUG_TRACE("port %d: pkt-filter disabled\n", port_id);
1223                 return (-ENOSYS);
1224         }
1225
1226         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_infos_get, -ENOTSUP);
1227
1228         (*dev->dev_ops->fdir_infos_get)(dev, fdir);
1229         return (0);
1230 }
1231
1232 int
1233 rte_eth_dev_fdir_add_perfect_filter(uint8_t port_id,
1234                                     struct rte_fdir_filter *fdir_filter,
1235                                     uint16_t soft_id, uint8_t queue,
1236                                     uint8_t drop)
1237 {
1238         struct rte_eth_dev *dev;
1239
1240         if (port_id >= nb_ports) {
1241                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1242                 return (-ENODEV);
1243         }
1244
1245         dev = &rte_eth_devices[port_id];
1246
1247         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
1248                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1249                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1250                 return (-ENOSYS);
1251         }
1252
1253         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1254              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1255             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1256                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1257                                 "None l4type, source & destinations ports " \
1258                                 "should be null!\n");
1259                 return (-EINVAL);
1260         }
1261
1262         /* For now IPv6 is not supported with perfect filter */
1263         if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
1264                 return (-ENOTSUP);
1265
1266         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_add_perfect_filter, -ENOTSUP);
1267         return (*dev->dev_ops->fdir_add_perfect_filter)(dev, fdir_filter,
1268                                                                 soft_id, queue,
1269                                                                 drop);
1270 }
1271
1272 int
1273 rte_eth_dev_fdir_update_perfect_filter(uint8_t port_id,
1274                                        struct rte_fdir_filter *fdir_filter,
1275                                        uint16_t soft_id, uint8_t queue,
1276                                        uint8_t drop)
1277 {
1278         struct rte_eth_dev *dev;
1279
1280         if (port_id >= nb_ports) {
1281                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1282                 return (-ENODEV);
1283         }
1284
1285         dev = &rte_eth_devices[port_id];
1286
1287         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
1288                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1289                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1290                 return (-ENOSYS);
1291         }
1292
1293         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1294              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1295             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1296                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1297                                 "None l4type, source & destinations ports " \
1298                                 "should be null!\n");
1299                 return (-EINVAL);
1300         }
1301
1302         /* For now IPv6 is not supported with perfect filter */
1303         if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
1304                 return (-ENOTSUP);
1305
1306         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_update_perfect_filter, -ENOTSUP);
1307         return (*dev->dev_ops->fdir_update_perfect_filter)(dev, fdir_filter,
1308                                                         soft_id, queue, drop);
1309 }
1310
1311 int
1312 rte_eth_dev_fdir_remove_perfect_filter(uint8_t port_id,
1313                                        struct rte_fdir_filter *fdir_filter,
1314                                        uint16_t soft_id)
1315 {
1316         struct rte_eth_dev *dev;
1317
1318         if (port_id >= nb_ports) {
1319                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1320                 return (-ENODEV);
1321         }
1322
1323         dev = &rte_eth_devices[port_id];
1324
1325         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
1326                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1327                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1328                 return (-ENOSYS);
1329         }
1330
1331         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1332              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1333             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1334                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1335                                 "None l4type, source & destinations ports " \
1336                                 "should be null!\n");
1337                 return (-EINVAL);
1338         }
1339
1340         /* For now IPv6 is not supported with perfect filter */
1341         if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
1342                 return (-ENOTSUP);
1343
1344         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_remove_perfect_filter, -ENOTSUP);
1345         return (*dev->dev_ops->fdir_remove_perfect_filter)(dev, fdir_filter,
1346                                                                 soft_id);
1347 }
1348
1349 int
1350 rte_eth_dev_fdir_set_masks(uint8_t port_id, struct rte_fdir_masks *fdir_mask)
1351 {
1352         struct rte_eth_dev *dev;
1353
1354         if (port_id >= nb_ports) {
1355                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1356                 return (-ENODEV);
1357         }
1358
1359         dev = &rte_eth_devices[port_id];
1360         if (! (dev->data->dev_conf.fdir_conf.mode)) {
1361                 PMD_DEBUG_TRACE("port %d: pkt-filter disabled\n", port_id);
1362                 return (-ENOSYS);
1363         }
1364
1365         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_set_masks, -ENOTSUP);
1366         return (*dev->dev_ops->fdir_set_masks)(dev, fdir_mask);
1367 }
1368
1369 int
1370 rte_eth_dev_flow_ctrl_set(uint8_t port_id, struct rte_eth_fc_conf *fc_conf)
1371 {
1372         struct rte_eth_dev *dev;
1373
1374         if (port_id >= nb_ports) {
1375                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1376                 return (-ENODEV);
1377         }
1378
1379         if ((fc_conf->send_xon != 0) && (fc_conf->send_xon != 1)) {
1380                 PMD_DEBUG_TRACE("Invalid send_xon, only 0/1 allowed\n");
1381                 return (-EINVAL);
1382         }
1383
1384         dev = &rte_eth_devices[port_id];
1385         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_set, -ENOTSUP);
1386         return (*dev->dev_ops->flow_ctrl_set)(dev, fc_conf);
1387 }
1388
1389 int
1390 rte_eth_dev_priority_flow_ctrl_set(uint8_t port_id, struct rte_eth_pfc_conf *pfc_conf)
1391 {
1392         struct rte_eth_dev *dev;
1393
1394         if (port_id >= nb_ports) {
1395                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1396                 return (-ENODEV);
1397         }
1398
1399         if (pfc_conf->priority > (ETH_DCB_NUM_USER_PRIORITIES - 1)) {
1400                 PMD_DEBUG_TRACE("Invalid priority, only 0-7 allowed\n");
1401                 return (-EINVAL);
1402         }
1403
1404         dev = &rte_eth_devices[port_id];
1405         /* High water, low water validation are device specific */
1406         if  (*dev->dev_ops->priority_flow_ctrl_set)
1407                 return (*dev->dev_ops->priority_flow_ctrl_set)(dev, pfc_conf);
1408         return (-ENOTSUP);
1409 }
1410
1411 int
1412 rte_eth_led_on(uint8_t port_id)
1413 {
1414         struct rte_eth_dev *dev;
1415
1416         if (port_id >= nb_ports) {
1417                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1418                 return (-ENODEV);
1419         }
1420
1421         dev = &rte_eth_devices[port_id];
1422         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_on, -ENOTSUP);
1423         return ((*dev->dev_ops->dev_led_on)(dev));
1424 }
1425
1426 int
1427 rte_eth_led_off(uint8_t port_id)
1428 {
1429         struct rte_eth_dev *dev;
1430
1431         if (port_id >= nb_ports) {
1432                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1433                 return (-ENODEV);
1434         }
1435
1436         dev = &rte_eth_devices[port_id];
1437         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_off, -ENOTSUP);
1438         return ((*dev->dev_ops->dev_led_off)(dev));
1439 }
1440
1441 /*
1442  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
1443  * an empty spot.
1444  */
1445 static inline int
1446 get_mac_addr_index(uint8_t port_id, struct ether_addr *addr)
1447 {
1448         struct rte_eth_dev_info dev_info;
1449         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1450         unsigned i;
1451
1452         rte_eth_dev_info_get(port_id, &dev_info);
1453
1454         for (i = 0; i < dev_info.max_mac_addrs; i++)
1455                 if (memcmp(addr, &dev->data->mac_addrs[i], ETHER_ADDR_LEN) == 0)
1456                         return i;
1457
1458         return -1;
1459 }
1460
1461 static struct ether_addr null_mac_addr = {{0, 0, 0, 0, 0, 0}};
1462
1463 int
1464 rte_eth_dev_mac_addr_add(uint8_t port_id, struct ether_addr *addr,
1465                 uint32_t pool)
1466 {
1467         struct rte_eth_dev *dev;
1468         int index;
1469
1470         if (port_id >= nb_ports) {
1471                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1472                 return (-ENODEV);
1473         }
1474         dev = &rte_eth_devices[port_id];
1475         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_add, -ENOTSUP);
1476
1477         if (is_zero_ether_addr(addr)) {
1478                 PMD_DEBUG_TRACE("port %d: Cannot add NULL MAC address\n", port_id);
1479                 return (-EINVAL);
1480         }
1481
1482         /* Check if it's already there, and do nothing */
1483         index = get_mac_addr_index(port_id, addr);
1484         if (index >= 0)
1485                 return 0;
1486
1487         index = get_mac_addr_index(port_id, &null_mac_addr);
1488         if (index < 0) {
1489                 PMD_DEBUG_TRACE("port %d: MAC address array full\n", port_id);
1490                 return (-ENOSPC);
1491         }
1492
1493         /* Update NIC */
1494         (*dev->dev_ops->mac_addr_add)(dev, addr, index, pool);
1495
1496         /* Update address in NIC data structure */
1497         ether_addr_copy(addr, &dev->data->mac_addrs[index]);
1498
1499         return 0;
1500 }
1501
1502 int
1503 rte_eth_dev_mac_addr_remove(uint8_t port_id, struct ether_addr *addr)
1504 {
1505         struct rte_eth_dev *dev;
1506         int index;
1507
1508         if (port_id >= nb_ports) {
1509                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1510                 return (-ENODEV);
1511         }
1512         dev = &rte_eth_devices[port_id];
1513         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_remove, -ENOTSUP);
1514
1515         index = get_mac_addr_index(port_id, addr);
1516         if (index == 0) {
1517                 PMD_DEBUG_TRACE("port %d: Cannot remove default MAC address\n", port_id);
1518                 return (-EADDRINUSE);
1519         } else if (index < 0)
1520                 return 0;  /* Do nothing if address wasn't found */
1521
1522         /* Update NIC */
1523         (*dev->dev_ops->mac_addr_remove)(dev, index);
1524
1525         /* Update address in NIC data structure */
1526         ether_addr_copy(&null_mac_addr, &dev->data->mac_addrs[index]);
1527
1528         return 0;
1529 }
1530
1531 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
1532 uint16_t
1533 rte_eth_rx_burst(uint8_t port_id, uint16_t queue_id,
1534                  struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1535 {
1536         struct rte_eth_dev *dev;
1537
1538         if (port_id >= nb_ports) {
1539                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1540                 return 0;
1541         }
1542         dev = &rte_eth_devices[port_id];
1543         FUNC_PTR_OR_ERR_RET(*dev->rx_pkt_burst, -ENOTSUP);
1544         if (queue_id >= dev->data->nb_rx_queues) {
1545                 PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", queue_id);
1546                 return 0;
1547         }
1548         return (*dev->rx_pkt_burst)(dev->data->rx_queues[queue_id],
1549                                                 rx_pkts, nb_pkts);
1550 }
1551
1552 uint16_t
1553 rte_eth_tx_burst(uint8_t port_id, uint16_t queue_id,
1554                  struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1555 {
1556         struct rte_eth_dev *dev;
1557
1558         if (port_id >= nb_ports) {
1559                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1560                 return 0;
1561         }
1562         dev = &rte_eth_devices[port_id];
1563
1564         FUNC_PTR_OR_ERR_RET(*dev->tx_pkt_burst, -ENOTSUP);
1565         if (queue_id >= dev->data->nb_tx_queues) {
1566                 PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", queue_id);
1567                 return 0;
1568         }
1569         return (*dev->tx_pkt_burst)(dev->data->tx_queues[queue_id],
1570                                                 tx_pkts, nb_pkts);
1571 }
1572 #endif
1573
1574 int
1575 rte_eth_dev_callback_register(uint8_t port_id,
1576                         enum rte_eth_event_type event,
1577                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
1578 {
1579         struct rte_eth_dev *dev;
1580         struct rte_eth_dev_callback *user_cb;
1581
1582         if (!cb_fn)
1583                 return (-EINVAL);
1584         if (port_id >= nb_ports) {
1585                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1586                 return (-EINVAL);
1587         }
1588
1589         dev = &rte_eth_devices[port_id];
1590         rte_spinlock_lock(&rte_eth_dev_cb_lock);
1591
1592         TAILQ_FOREACH(user_cb, &(dev->callbacks), next) {
1593                 if (user_cb->cb_fn == cb_fn &&
1594                         user_cb->cb_arg == cb_arg &&
1595                         user_cb->event == event) {
1596                         break;
1597                 }
1598         }
1599
1600         /* create a new callback. */
1601         if (user_cb == NULL && (user_cb = rte_zmalloc("INTR_USER_CALLBACK",
1602                         sizeof(struct rte_eth_dev_callback), 0)) != NULL) {
1603                 user_cb->cb_fn = cb_fn;
1604                 user_cb->cb_arg = cb_arg;
1605                 user_cb->event = event;
1606                 TAILQ_INSERT_TAIL(&(dev->callbacks), user_cb, next);
1607         }
1608
1609         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1610         return ((user_cb == NULL) ? -ENOMEM : 0);
1611 }
1612
1613 int
1614 rte_eth_dev_callback_unregister(uint8_t port_id,
1615                         enum rte_eth_event_type event,
1616                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
1617 {
1618         int ret;
1619         struct rte_eth_dev *dev;
1620         struct rte_eth_dev_callback *cb, *next;
1621
1622         if (!cb_fn)
1623                 return (-EINVAL);
1624         if (port_id >= nb_ports) {
1625                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1626                 return (-EINVAL);
1627         }
1628
1629         dev = &rte_eth_devices[port_id];
1630         rte_spinlock_lock(&rte_eth_dev_cb_lock);
1631
1632         ret = 0;
1633         for (cb = TAILQ_FIRST(&dev->callbacks); cb != NULL; cb = next) {
1634
1635                 next = TAILQ_NEXT(cb, next);
1636
1637                 if (cb->cb_fn != cb_fn || cb->event != event ||
1638                                 (cb->cb_arg != (void *)-1 &&
1639                                 cb->cb_arg != cb_arg))
1640                         continue;
1641
1642                 /*
1643                  * if this callback is not executing right now,
1644                  * then remove it.
1645                  */
1646                 if (cb->active == 0) {
1647                         TAILQ_REMOVE(&(dev->callbacks), cb, next);
1648                         rte_free(cb);
1649                 } else {
1650                         ret = -EAGAIN;
1651                 }
1652         }
1653
1654         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1655         return (ret);
1656 }
1657
1658 void
1659 _rte_eth_dev_callback_process(struct rte_eth_dev *dev,
1660         enum rte_eth_event_type event)
1661 {
1662         struct rte_eth_dev_callback *cb_lst;
1663         struct rte_eth_dev_callback dev_cb;
1664
1665         rte_spinlock_lock(&rte_eth_dev_cb_lock);
1666         TAILQ_FOREACH(cb_lst, &(dev->callbacks), next) {
1667                 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
1668                         continue;
1669                 dev_cb = *cb_lst;
1670                 cb_lst->active = 1;
1671                 rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1672                 dev_cb.cb_fn(dev->data->port_id, dev_cb.event,
1673                                                 dev_cb.cb_arg);
1674                 rte_spinlock_lock(&rte_eth_dev_cb_lock);
1675                 cb_lst->active = 0;
1676         }
1677         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1678 }