remove version in all files
[dpdk.git] / lib / librte_ether / rte_ethdev.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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 /* define two macros for quick checking for restricting functions to primary
81  * instance only. First macro is for functions returning an int - and therefore
82  * an error code, second macro is for functions returning null.
83  */
84 #define PROC_PRIMARY_OR_ERR() do { \
85         if (rte_eal_process_type() != RTE_PROC_PRIMARY) { \
86                 PMD_DEBUG_TRACE("Cannot run %s in secondary processes\n", \
87                                                         __func__); \
88                         return (-E_RTE_SECONDARY); \
89                 } \
90 } while(0)
91
92 #define PROC_PRIMARY_OR_RET() do { \
93         if (rte_eal_process_type() != RTE_PROC_PRIMARY) { \
94                 PMD_DEBUG_TRACE("Cannot run %s in secondary processes\n", \
95                                                         __func__); \
96                 return; \
97         } \
98 } while(0)
99
100 /* Macros to check for invlaid function pointers in dev_ops structure */
101 #define FUNC_PTR_OR_ERR_RET(func, retval) do { \
102         if ((func) == NULL) { \
103                 PMD_DEBUG_TRACE("Function not supported\n"); \
104                 return (retval); \
105         } \
106 } while(0)
107 #define FUNC_PTR_OR_RET(func) do { \
108         if ((func) == NULL) { \
109                 PMD_DEBUG_TRACE("Function not supported\n"); \
110                 return; \
111         } \
112 } while(0)
113
114 static const char *MZ_RTE_ETH_DEV_DATA = "rte_eth_dev_data";
115 struct rte_eth_dev rte_eth_devices[RTE_MAX_ETHPORTS];
116 static struct rte_eth_dev_data *rte_eth_dev_data = NULL;
117 static uint8_t nb_ports = 0;
118
119 /* spinlock for eth device callbacks */
120 static rte_spinlock_t rte_eth_dev_cb_lock = RTE_SPINLOCK_INITIALIZER;
121
122 /**
123  * The user application callback description.
124  *
125  * It contains callback address to be registered by user application,
126  * the pointer to the parameters for callback, and the event type.
127  */
128 struct rte_eth_dev_callback {
129         TAILQ_ENTRY(rte_eth_dev_callback) next; /**< Callbacks list */
130         rte_eth_dev_cb_fn cb_fn;                /**< Callback address */
131         void *cb_arg;                           /**< Parameter for callback */
132         enum rte_eth_event_type event;          /**< Interrupt event type */
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                 return NULL;
163
164         if (rte_eth_dev_data == NULL)
165                 rte_eth_dev_data_alloc();
166
167         eth_dev = &rte_eth_devices[nb_ports];
168         eth_dev->data = &rte_eth_dev_data[nb_ports];
169         eth_dev->data->port_id = nb_ports++;
170         return eth_dev;
171 }
172
173 static int
174 rte_eth_dev_init(struct rte_pci_driver *pci_drv,
175                  struct rte_pci_device *pci_dev)
176 {
177         struct eth_driver    *eth_drv;
178         struct rte_eth_dev *eth_dev;
179         int diag;
180
181         eth_drv = (struct eth_driver *)pci_drv;
182
183         eth_dev = rte_eth_dev_allocate();
184         if (eth_dev == NULL)
185                 return -ENOMEM;
186
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                         return -ENOMEM;
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 uint8_t
244 rte_eth_dev_count(void)
245 {
246         return (nb_ports);
247 }
248
249 int
250 rte_eth_dev_configure(uint8_t port_id, uint16_t nb_rx_q, uint16_t nb_tx_q,
251                       const struct rte_eth_conf *dev_conf)
252 {
253         struct rte_eth_dev *dev;
254         struct rte_eth_dev_info dev_info;
255         int diag;
256
257         /* This function is only safe when called from the primary process
258          * in a multi-process setup*/
259         PROC_PRIMARY_OR_ERR();
260
261         if (port_id >= nb_ports || port_id >= RTE_MAX_ETHPORTS) {
262                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
263                 return (-EINVAL);
264         }
265         dev = &rte_eth_devices[port_id];
266
267         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
268         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_configure, -ENOTSUP);
269
270         if (dev->data->dev_started) {
271                 PMD_DEBUG_TRACE(
272                     "port %d must be stopped to allow configuration", port_id);
273                 return -EBUSY;
274         }
275
276         /*
277          * Check that the numbers of RX and TX queues are not greater
278          * than the maximum number of RX and TX queues supported by the
279          * configured device.
280          */
281         (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
282         if (nb_rx_q > dev_info.max_rx_queues) {
283                 PMD_DEBUG_TRACE("ethdev port_id=%d nb_rx_queues=%d > %d",
284                                 port_id, nb_rx_q, dev_info.max_rx_queues);
285                 return (-EINVAL);
286         }
287         if (nb_rx_q == 0) {
288                 PMD_DEBUG_TRACE("ethdev port_id=%d nb_rx_q == 0", port_id);
289                 return (-EINVAL);
290         }
291
292         if (nb_tx_q > dev_info.max_tx_queues) {
293                 PMD_DEBUG_TRACE("ethdev port_id=%d nb_tx_queues=%d > %d",
294                                 port_id, nb_tx_q, dev_info.max_tx_queues);
295                 return (-EINVAL);
296         }
297         if (nb_tx_q == 0) {
298                 PMD_DEBUG_TRACE("ethdev port_id=%d nb_tx_q == 0", port_id);
299                 return (-EINVAL);
300         }
301
302         /* Copy the dev_conf parameter into the dev structure */
303         memcpy(&dev->data->dev_conf, dev_conf, sizeof(dev->data->dev_conf));
304
305         /*
306          * If jumbo frames are enabled, check that the maximum RX packet
307          * length is supported by the configured device.
308          */
309         if (dev_conf->rxmode.jumbo_frame == 1) {
310                 if (dev_conf->rxmode.max_rx_pkt_len >
311                     dev_info.max_rx_pktlen) {
312                         PMD_DEBUG_TRACE("ethdev port_id=%d max_rx_pkt_len %u"
313                                 " > max valid value %u",
314                                 port_id,
315                                 (unsigned)dev_conf->rxmode.max_rx_pkt_len,
316                                 (unsigned)dev_info.max_rx_pktlen);
317                         return (-EINVAL);
318                 }
319         } else
320                 /* Use default value */
321                 dev->data->dev_conf.rxmode.max_rx_pkt_len = ETHER_MAX_LEN;
322
323         /* For vmdb+dcb mode check our configuration before we go further */
324         if (dev_conf->rxmode.mq_mode == ETH_VMDQ_DCB) {
325                 const struct rte_eth_vmdq_dcb_conf *conf;
326
327                 if (nb_rx_q != ETH_VMDQ_DCB_NUM_QUEUES) {
328                         PMD_DEBUG_TRACE("ethdev port_id=%d VMDQ+DCB, nb_rx_q "
329                                         "!= %d",
330                                         port_id, ETH_VMDQ_DCB_NUM_QUEUES);
331                         return (-EINVAL);
332                 }
333                 conf = &(dev_conf->rx_adv_conf.vmdq_dcb_conf);
334                 if (! (conf->nb_queue_pools == ETH_16_POOLS ||
335                        conf->nb_queue_pools == ETH_32_POOLS)) {
336                     PMD_DEBUG_TRACE("ethdev port_id=%d VMDQ+DCB selected, "
337                                     "nb_queue_pools != %d or nb_queue_pools "
338                                     "!= %d",
339                                     port_id, ETH_16_POOLS, ETH_32_POOLS);
340                     return (-EINVAL);
341                 }
342         }
343
344         diag = (*dev->dev_ops->dev_configure)(dev, nb_rx_q, nb_tx_q);
345         if (diag != 0) {
346                 rte_free(dev->data->rx_queues);
347                 rte_free(dev->data->tx_queues);
348         }
349         return diag;
350 }
351
352 static void
353 rte_eth_dev_config_restore(uint8_t port_id)
354 {
355         struct rte_eth_dev *dev;
356         struct rte_eth_dev_info dev_info;
357         struct ether_addr addr;
358         uint16_t i;
359
360         dev = &rte_eth_devices[port_id];
361
362         rte_eth_dev_info_get(port_id, &dev_info);
363
364         /* replay MAC address configuration */
365         for (i = 0; i < dev_info.max_mac_addrs; i++) {
366                 addr = dev->data->mac_addrs[i];
367
368                 /* skip zero address */
369                 if (is_zero_ether_addr(&addr))
370                         continue;
371
372                 /* add address to the hardware */
373                 if  (*dev->dev_ops->mac_addr_add)
374                         (*dev->dev_ops->mac_addr_add)(dev, &addr, i, 0);
375                 else {
376                         PMD_DEBUG_TRACE("port %d: MAC address array not supported\n",
377                                         port_id);
378                         /* exit the loop but not return an error */
379                         break;
380                 }
381         }
382
383         /* replay promiscuous configuration */
384         if (rte_eth_promiscuous_get(port_id) == 1)
385                 rte_eth_promiscuous_enable(port_id);
386         else if (rte_eth_promiscuous_get(port_id) == 0)
387                 rte_eth_promiscuous_disable(port_id);
388
389         /* replay allmulticast configuration */
390         if (rte_eth_allmulticast_get(port_id) == 1)
391                 rte_eth_allmulticast_enable(port_id);
392         else if (rte_eth_allmulticast_get(port_id) == 0)
393                 rte_eth_allmulticast_disable(port_id);
394 }
395
396 int
397 rte_eth_dev_start(uint8_t port_id)
398 {
399         struct rte_eth_dev *dev;
400         int diag;
401
402         /* This function is only safe when called from the primary process
403          * in a multi-process setup*/
404         PROC_PRIMARY_OR_ERR();
405
406         if (port_id >= nb_ports) {
407                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
408                 return (-EINVAL);
409         }
410         dev = &rte_eth_devices[port_id];
411
412         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
413         diag = (*dev->dev_ops->dev_start)(dev);
414         if (diag == 0)
415                 dev->data->dev_started = 1;
416         else
417                 return diag;
418
419         rte_eth_dev_config_restore(port_id);
420
421         return 0;
422 }
423
424 void
425 rte_eth_dev_stop(uint8_t port_id)
426 {
427         struct rte_eth_dev *dev;
428
429         /* This function is only safe when called from the primary process
430          * in a multi-process setup*/
431         PROC_PRIMARY_OR_RET();
432
433         if (port_id >= nb_ports) {
434                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
435                 return;
436         }
437         dev = &rte_eth_devices[port_id];
438
439         FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
440         dev->data->dev_started = 0;
441         (*dev->dev_ops->dev_stop)(dev);
442 }
443
444 void
445 rte_eth_dev_close(uint8_t port_id)
446 {
447         struct rte_eth_dev *dev;
448
449         /* This function is only safe when called from the primary process
450          * in a multi-process setup*/
451         PROC_PRIMARY_OR_RET();
452
453         if (port_id >= nb_ports) {
454                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
455                 return;
456         }
457
458         dev = &rte_eth_devices[port_id];
459         FUNC_PTR_OR_RET(*dev->dev_ops->dev_close);
460         dev->data->dev_started = 0;
461         (*dev->dev_ops->dev_close)(dev);
462 }
463
464 int
465 rte_eth_rx_queue_setup(uint8_t port_id, uint16_t rx_queue_id,
466                        uint16_t nb_rx_desc, unsigned int socket_id,
467                        const struct rte_eth_rxconf *rx_conf,
468                        struct rte_mempool *mp)
469 {
470         struct rte_eth_dev *dev;
471         struct rte_pktmbuf_pool_private *mbp_priv;
472         struct rte_eth_dev_info dev_info;
473
474         /* This function is only safe when called from the primary process
475          * in a multi-process setup*/
476         PROC_PRIMARY_OR_ERR();
477
478         if (port_id >= nb_ports) {
479                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
480                 return (-EINVAL);
481         }
482         dev = &rte_eth_devices[port_id];
483         if (rx_queue_id >= dev->data->nb_rx_queues) {
484                 PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", rx_queue_id);
485                 return (-EINVAL);
486         }
487
488         if (dev->data->dev_started) {
489                 PMD_DEBUG_TRACE(
490                     "port %d must be stopped to allow configuration", port_id);
491                 return -EBUSY;
492         }
493
494         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
495         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_setup, -ENOTSUP);
496
497         /*
498          * Check the size of the mbuf data buffer.
499          * This value must be provided in the private data of the memory pool.
500          * First check that the memory pool has a valid private data.
501          */
502         (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
503         if (mp->private_data_size < sizeof(struct rte_pktmbuf_pool_private)) {
504                 PMD_DEBUG_TRACE("%s private_data_size %d < %d\n",
505                                 mp->name, (int) mp->private_data_size,
506                                 (int) sizeof(struct rte_pktmbuf_pool_private));
507                 return (-ENOSPC);
508         }
509         mbp_priv = (struct rte_pktmbuf_pool_private *)
510                 ((char *)mp + sizeof(struct rte_mempool));
511         if ((uint32_t) (mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM) <
512             dev_info.min_rx_bufsize) {
513                 PMD_DEBUG_TRACE("%s mbuf_data_room_size %d < %d "
514                                 "(RTE_PKTMBUF_HEADROOM=%d + min_rx_bufsize(dev)"
515                                 "=%d)\n",
516                                 mp->name,
517                                 (int)mbp_priv->mbuf_data_room_size,
518                                 (int)(RTE_PKTMBUF_HEADROOM +
519                                       dev_info.min_rx_bufsize),
520                                 (int)RTE_PKTMBUF_HEADROOM,
521                                 (int)dev_info.min_rx_bufsize);
522                 return (-EINVAL);
523         }
524
525         return (*dev->dev_ops->rx_queue_setup)(dev, rx_queue_id, nb_rx_desc,
526                                                socket_id, rx_conf, mp);
527 }
528
529 int
530 rte_eth_tx_queue_setup(uint8_t port_id, uint16_t tx_queue_id,
531                        uint16_t nb_tx_desc, unsigned int socket_id,
532                        const struct rte_eth_txconf *tx_conf)
533 {
534         struct rte_eth_dev *dev;
535
536         /* This function is only safe when called from the primary process
537          * in a multi-process setup*/
538         PROC_PRIMARY_OR_ERR();
539
540         if (port_id >= nb_ports) {
541                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
542                 return (-EINVAL);
543         }
544         dev = &rte_eth_devices[port_id];
545         if (tx_queue_id >= dev->data->nb_tx_queues) {
546                 PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", tx_queue_id);
547                 return (-EINVAL);
548         }
549
550         if (dev->data->dev_started) {
551                 PMD_DEBUG_TRACE(
552                     "port %d must be stopped to allow configuration", port_id);
553                 return -EBUSY;
554         }
555
556         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_setup, -ENOTSUP);
557         return (*dev->dev_ops->tx_queue_setup)(dev, tx_queue_id, nb_tx_desc,
558                                                socket_id, tx_conf);
559 }
560
561 void
562 rte_eth_promiscuous_enable(uint8_t port_id)
563 {
564         struct rte_eth_dev *dev;
565
566         if (port_id >= nb_ports) {
567                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
568                 return;
569         }
570         dev = &rte_eth_devices[port_id];
571
572         FUNC_PTR_OR_RET(*dev->dev_ops->promiscuous_enable);
573         (*dev->dev_ops->promiscuous_enable)(dev);
574         dev->data->promiscuous = 1;
575 }
576
577 void
578 rte_eth_promiscuous_disable(uint8_t port_id)
579 {
580         struct rte_eth_dev *dev;
581
582         if (port_id >= nb_ports) {
583                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
584                 return;
585         }
586         dev = &rte_eth_devices[port_id];
587
588         FUNC_PTR_OR_RET(*dev->dev_ops->promiscuous_disable);
589         dev->data->promiscuous = 0;
590         (*dev->dev_ops->promiscuous_disable)(dev);
591 }
592
593 int
594 rte_eth_promiscuous_get(uint8_t port_id)
595 {
596         struct rte_eth_dev *dev;
597
598         if (port_id >= nb_ports) {
599                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
600                 return -1;
601         }
602
603         dev = &rte_eth_devices[port_id];
604         return dev->data->promiscuous;
605 }
606
607 void
608 rte_eth_allmulticast_enable(uint8_t port_id)
609 {
610         struct rte_eth_dev *dev;
611
612         if (port_id >= nb_ports) {
613                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
614                 return;
615         }
616         dev = &rte_eth_devices[port_id];
617
618         FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_enable);
619         (*dev->dev_ops->allmulticast_enable)(dev);
620         dev->data->all_multicast = 1;
621 }
622
623 void
624 rte_eth_allmulticast_disable(uint8_t port_id)
625 {
626         struct rte_eth_dev *dev;
627
628         if (port_id >= nb_ports) {
629                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
630                 return;
631         }
632         dev = &rte_eth_devices[port_id];
633
634         FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_disable);
635         dev->data->all_multicast = 0;
636         (*dev->dev_ops->allmulticast_disable)(dev);
637 }
638
639 int
640 rte_eth_allmulticast_get(uint8_t port_id)
641 {
642         struct rte_eth_dev *dev;
643
644         if (port_id >= nb_ports) {
645                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
646                 return -1;
647         }
648
649         dev = &rte_eth_devices[port_id];
650         return dev->data->all_multicast;
651 }
652
653 static inline int
654 rte_eth_dev_atomic_read_link_status(struct rte_eth_dev *dev,
655                                 struct rte_eth_link *link)
656 {
657         struct rte_eth_link *dst = link;
658         struct rte_eth_link *src = &(dev->data->dev_link);
659
660         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
661                                         *(uint64_t *)src) == 0)
662                 return -1;
663
664         return 0;
665 }
666
667 void
668 rte_eth_link_get(uint8_t port_id, struct rte_eth_link *eth_link)
669 {
670         struct rte_eth_dev *dev;
671
672         if (port_id >= nb_ports) {
673                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
674                 return;
675         }
676         dev = &rte_eth_devices[port_id];
677         FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
678
679         if (dev->data->dev_conf.intr_conf.lsc != 0)
680                 rte_eth_dev_atomic_read_link_status(dev, eth_link);
681         else {
682                 (*dev->dev_ops->link_update)(dev, 1);
683                 *eth_link = dev->data->dev_link;
684         }
685 }
686
687 void
688 rte_eth_link_get_nowait(uint8_t port_id, struct rte_eth_link *eth_link)
689 {
690         struct rte_eth_dev *dev;
691
692         if (port_id >= nb_ports) {
693                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
694                 return;
695         }
696         dev = &rte_eth_devices[port_id];
697         FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
698
699         if (dev->data->dev_conf.intr_conf.lsc != 0)
700                 rte_eth_dev_atomic_read_link_status(dev, eth_link);
701         else {
702                 (*dev->dev_ops->link_update)(dev, 0);
703                 *eth_link = dev->data->dev_link;
704         }
705 }
706
707 void
708 rte_eth_stats_get(uint8_t port_id, struct rte_eth_stats *stats)
709 {
710         struct rte_eth_dev *dev;
711
712         if (port_id >= nb_ports) {
713                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
714                 return;
715         }
716         dev = &rte_eth_devices[port_id];
717
718         FUNC_PTR_OR_RET(*dev->dev_ops->stats_get);
719         (*dev->dev_ops->stats_get)(dev, stats);
720         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
721 }
722
723 void
724 rte_eth_stats_reset(uint8_t port_id)
725 {
726         struct rte_eth_dev *dev;
727
728         if (port_id >= nb_ports) {
729                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
730                 return;
731         }
732         dev = &rte_eth_devices[port_id];
733
734         FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
735         (*dev->dev_ops->stats_reset)(dev);
736 }
737
738 void
739 rte_eth_dev_info_get(uint8_t port_id, struct rte_eth_dev_info *dev_info)
740 {
741         struct rte_eth_dev *dev;
742
743         if (port_id >= nb_ports) {
744                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
745                 return;
746         }
747         dev = &rte_eth_devices[port_id];
748
749         FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
750         (*dev->dev_ops->dev_infos_get)(dev, dev_info);
751         dev_info->pci_dev = dev->pci_dev;
752         dev_info->driver_name = dev->driver->pci_drv.name;
753 }
754
755 void
756 rte_eth_macaddr_get(uint8_t port_id, struct ether_addr *mac_addr)
757 {
758         struct rte_eth_dev *dev;
759
760         if (port_id >= nb_ports) {
761                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
762                 return;
763         }
764         dev = &rte_eth_devices[port_id];
765         ether_addr_copy(&dev->data->mac_addrs[0], mac_addr);
766 }
767
768 int
769 rte_eth_dev_vlan_filter(uint8_t port_id, uint16_t vlan_id, int on)
770 {
771         struct rte_eth_dev *dev;
772
773         if (port_id >= nb_ports) {
774                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
775                 return (-ENODEV);
776         }
777         dev = &rte_eth_devices[port_id];
778         if (! (dev->data->dev_conf.rxmode.hw_vlan_filter)) {
779                 PMD_DEBUG_TRACE("port %d: vlan-filtering disabled\n", port_id);
780                 return (-ENOSYS);
781         }
782         if (vlan_id > 4095) {
783                 PMD_DEBUG_TRACE("(port_id=%d) invalid vlan_id=%u > 4095\n",
784                                 port_id, (unsigned) vlan_id);
785                 return (-EINVAL);
786         }
787
788         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_filter_set, -ENOTSUP);
789         (*dev->dev_ops->vlan_filter_set)(dev, vlan_id, on);
790         return (0);
791 }
792
793 int
794 rte_eth_dev_fdir_add_signature_filter(uint8_t port_id,
795                                       struct rte_fdir_filter *fdir_filter,
796                                       uint8_t queue)
797 {
798         struct rte_eth_dev *dev;
799
800         if (port_id >= nb_ports) {
801                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
802                 return (-ENODEV);
803         }
804
805         dev = &rte_eth_devices[port_id];
806
807         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
808                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
809                                 port_id, dev->data->dev_conf.fdir_conf.mode);
810                 return (-ENOSYS);
811         }
812
813         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
814              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
815             && (fdir_filter->port_src || fdir_filter->port_dst)) {
816                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
817                                 "None l4type source & destinations ports " \
818                                 "should be null!");
819                 return (-EINVAL);
820         }
821
822         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_add_signature_filter, -ENOTSUP);
823         if (*dev->dev_ops->fdir_add_signature_filter)
824                 return (*dev->dev_ops->fdir_add_signature_filter)(dev,
825                                                                   fdir_filter,
826                                                                   queue);
827
828         PMD_DEBUG_TRACE("port %d: FDIR feature not supported\n", port_id);
829         return (-ENOTSUP);
830 }
831
832 int
833 rte_eth_dev_fdir_update_signature_filter(uint8_t port_id,
834                                          struct rte_fdir_filter *fdir_filter,
835                                          uint8_t queue)
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 (-ENODEV);
842         }
843
844         dev = &rte_eth_devices[port_id];
845
846         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
847                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
848                                 port_id, dev->data->dev_conf.fdir_conf.mode);
849                 return (-ENOSYS);
850         }
851
852         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
853              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
854             && (fdir_filter->port_src || fdir_filter->port_dst)) {
855                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
856                                 "None l4type source & destinations ports " \
857                                 "should be null!");
858                 return (-EINVAL);
859         }
860
861         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_update_signature_filter, -ENOTSUP);
862         if (*dev->dev_ops->fdir_update_signature_filter)
863                 return (*dev->dev_ops->fdir_update_signature_filter)(dev,
864                                                                      fdir_filter,
865                                                                      queue);
866
867
868         PMD_DEBUG_TRACE("port %d: FDIR feature not supported\n", port_id);
869         return (-ENOTSUP);
870 }
871
872 int
873 rte_eth_dev_fdir_remove_signature_filter(uint8_t port_id,
874                                          struct rte_fdir_filter *fdir_filter)
875 {
876         struct rte_eth_dev *dev;
877
878         if (port_id >= nb_ports) {
879                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
880                 return (-ENODEV);
881         }
882
883         dev = &rte_eth_devices[port_id];
884
885         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
886                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
887                                 port_id, dev->data->dev_conf.fdir_conf.mode);
888                 return (-ENOSYS);
889         }
890
891         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
892              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
893             && (fdir_filter->port_src || fdir_filter->port_dst)) {
894                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
895                                 "None l4type source & destinations ports " \
896                                 "should be null!");
897                 return (-EINVAL);
898         }
899
900         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_remove_signature_filter, -ENOTSUP);
901         if (*dev->dev_ops->fdir_remove_signature_filter)
902                 return (*dev->dev_ops->fdir_remove_signature_filter)(dev,
903                                                                      fdir_filter);
904
905         PMD_DEBUG_TRACE("port %d: FDIR feature not supported\n", port_id);
906         return (-ENOTSUP);
907 }
908
909 int
910 rte_eth_dev_fdir_get_infos(uint8_t port_id, struct rte_eth_fdir *fdir)
911 {
912         struct rte_eth_dev *dev;
913
914         if (port_id >= nb_ports) {
915                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
916                 return (-ENODEV);
917         }
918
919         dev = &rte_eth_devices[port_id];
920         if (! (dev->data->dev_conf.fdir_conf.mode)) {
921                 PMD_DEBUG_TRACE("port %d: pkt-filter disabled\n", port_id);
922                 return (-ENOSYS);
923         }
924
925         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_infos_get, -ENOTSUP);
926         if (*dev->dev_ops->fdir_infos_get) {
927                 (*dev->dev_ops->fdir_infos_get)(dev, fdir);
928                 return (0);
929         }
930
931         PMD_DEBUG_TRACE("port %d: FDIR feature not supported\n", port_id);
932         return (-ENOTSUP);
933 }
934
935 int
936 rte_eth_dev_fdir_add_perfect_filter(uint8_t port_id,
937                                     struct rte_fdir_filter *fdir_filter,
938                                     uint16_t soft_id, uint8_t queue,
939                                     uint8_t drop)
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 (-ENODEV);
946         }
947
948         dev = &rte_eth_devices[port_id];
949
950         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
951                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
952                                 port_id, dev->data->dev_conf.fdir_conf.mode);
953                 return (-ENOSYS);
954         }
955
956         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
957              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
958             && (fdir_filter->port_src || fdir_filter->port_dst)) {
959                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
960                                 "None l4type source & destinations ports " \
961                                 "should be null!");
962                 return (-EINVAL);
963         }
964
965         /* For now IPv6 is not supported with perfect filter */
966         if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
967                 return (-ENOTSUP);
968
969         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_add_perfect_filter, -ENOTSUP);
970         if  (*dev->dev_ops->fdir_add_perfect_filter)
971                 return (*dev->dev_ops->fdir_add_perfect_filter)(dev, fdir_filter,
972                                                                 soft_id, queue,
973                                                                 drop);
974
975         PMD_DEBUG_TRACE("port %d: FDIR feature not supported\n", port_id);
976         return (-ENOTSUP);
977 }
978
979 int
980 rte_eth_dev_fdir_update_perfect_filter(uint8_t port_id,
981                                        struct rte_fdir_filter *fdir_filter,
982                                        uint16_t soft_id, uint8_t queue,
983                                        uint8_t drop)
984 {
985         struct rte_eth_dev *dev;
986
987         if (port_id >= nb_ports) {
988                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
989                 return (-ENODEV);
990         }
991
992         dev = &rte_eth_devices[port_id];
993
994         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
995                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
996                                 port_id, dev->data->dev_conf.fdir_conf.mode);
997                 return (-ENOSYS);
998         }
999
1000         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1001              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1002             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1003                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1004                                 "None l4type source & destinations ports " \
1005                                 "should be null!");
1006                 return (-EINVAL);
1007         }
1008
1009         /* For now IPv6 is not supported with perfect filter */
1010         if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
1011                 return (-ENOTSUP);
1012
1013         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_update_perfect_filter, -ENOTSUP);
1014         if  (*dev->dev_ops->fdir_update_perfect_filter)
1015                 return (*dev->dev_ops->fdir_update_perfect_filter)(dev,
1016                                                                    fdir_filter,
1017                                                                    soft_id,
1018                                                                    queue,
1019                                                                    drop);
1020
1021         PMD_DEBUG_TRACE("port %d: FDIR feature not supported\n", port_id);
1022         return (-ENOTSUP);
1023 }
1024
1025 int
1026 rte_eth_dev_fdir_remove_perfect_filter(uint8_t port_id,
1027                                        struct rte_fdir_filter *fdir_filter,
1028                                        uint16_t soft_id)
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 (-ENODEV);
1035         }
1036
1037         dev = &rte_eth_devices[port_id];
1038
1039         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
1040                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1041                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1042                 return (-ENOSYS);
1043         }
1044
1045         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1046              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1047             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1048                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1049                                 "None l4type source & destinations ports " \
1050                                 "should be null!");
1051                 return (-EINVAL);
1052         }
1053
1054         /* For now IPv6 is not supported with perfect filter */
1055         if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
1056                 return (-ENOTSUP);
1057
1058         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_remove_perfect_filter, -ENOTSUP);
1059         if  (*dev->dev_ops->fdir_remove_perfect_filter)
1060                 return (*dev->dev_ops->fdir_remove_perfect_filter)(dev,
1061                                                                    fdir_filter,
1062                                                                    soft_id);
1063
1064         PMD_DEBUG_TRACE("port %d: FDIR feature not supported\n", port_id);
1065         return -ENOTSUP;
1066 }
1067
1068 int
1069 rte_eth_dev_fdir_set_masks(uint8_t port_id, struct rte_fdir_masks *fdir_mask)
1070 {
1071         struct rte_eth_dev *dev;
1072
1073         if (port_id >= nb_ports) {
1074                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1075                 return (-ENODEV);
1076         }
1077
1078         dev = &rte_eth_devices[port_id];
1079         if (! (dev->data->dev_conf.fdir_conf.mode)) {
1080                 PMD_DEBUG_TRACE("port %d: pkt-filter disabled\n", port_id);
1081                 return (-ENOSYS);
1082         }
1083
1084         /* IPv6 mask are not supported */
1085         if (fdir_mask->src_ipv6_mask)
1086                 return (-ENOTSUP);
1087
1088         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_set_masks, -ENOTSUP);
1089         if  (*dev->dev_ops->fdir_set_masks)
1090                 return (*dev->dev_ops->fdir_set_masks)(dev, fdir_mask);
1091
1092         PMD_DEBUG_TRACE("port %d: FDIR feature not supported\n",
1093                         port_id);
1094         return -ENOTSUP;
1095 }
1096
1097 int
1098 rte_eth_dev_flow_ctrl_set(uint8_t port_id, struct rte_eth_fc_conf *fc_conf)
1099 {
1100         struct rte_eth_dev *dev;
1101
1102         if (port_id >= nb_ports) {
1103                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1104                 return (-ENODEV);
1105         }
1106
1107         if ((fc_conf->send_xon != 0) && (fc_conf->send_xon != 1)) {
1108                 PMD_DEBUG_TRACE("Invalid send_xon, only 0/1 allowed\n");
1109                 return (-EINVAL);
1110         }
1111
1112         dev = &rte_eth_devices[port_id];
1113
1114         /* High water, low water validation are device specific */
1115         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_set, -ENOTSUP);
1116         if  (*dev->dev_ops->flow_ctrl_set)
1117                 return (*dev->dev_ops->flow_ctrl_set)(dev, fc_conf);
1118
1119         return -ENOTSUP;
1120 }
1121
1122 int
1123 rte_eth_led_on(uint8_t port_id)
1124 {
1125         struct rte_eth_dev *dev;
1126
1127         if (port_id >= nb_ports) {
1128                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1129                 return (-ENODEV);
1130         }
1131
1132         dev = &rte_eth_devices[port_id];
1133
1134         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_on, -ENOTSUP);
1135         return ((*dev->dev_ops->dev_led_on)(dev));
1136 }
1137
1138 int
1139 rte_eth_led_off(uint8_t port_id)
1140 {
1141         struct rte_eth_dev *dev;
1142
1143         if (port_id >= nb_ports) {
1144                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1145                 return (-ENODEV);
1146         }
1147
1148         dev = &rte_eth_devices[port_id];
1149
1150         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_off, -ENOTSUP);
1151         return ((*dev->dev_ops->dev_led_off)(dev));
1152 }
1153
1154 /*
1155  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
1156  * an empty spot.
1157  */
1158 static inline int
1159 get_mac_addr_index(uint8_t port_id, struct ether_addr *addr)
1160 {
1161         struct rte_eth_dev_info dev_info;
1162         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1163         unsigned i;
1164
1165         rte_eth_dev_info_get(port_id, &dev_info);
1166
1167         for (i = 0; i < dev_info.max_mac_addrs; i++)
1168                 if (memcmp(addr, &dev->data->mac_addrs[i], ETHER_ADDR_LEN) == 0)
1169                         return i;
1170
1171         return -1;
1172 }
1173
1174 static struct ether_addr null_mac_addr = {{0, 0, 0, 0, 0, 0}};
1175
1176 int
1177 rte_eth_dev_mac_addr_add(uint8_t port_id, struct ether_addr *addr,
1178                 uint32_t pool)
1179 {
1180         struct rte_eth_dev *dev;
1181         int index;
1182
1183         if (port_id >= nb_ports) {
1184                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1185                 return (-ENODEV);
1186         }
1187         dev = &rte_eth_devices[port_id];
1188
1189         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_add, -ENOTSUP);
1190         if (is_zero_ether_addr(addr)) {
1191                 PMD_DEBUG_TRACE("port %d: Cannot add NULL MAC address\n", port_id);
1192                 return (-EINVAL);
1193         }
1194
1195         /* Check if it's already there, and do nothing */
1196         index = get_mac_addr_index(port_id, addr);
1197         if (index >= 0)
1198                 return 0;
1199
1200         index = get_mac_addr_index(port_id, &null_mac_addr);
1201         if (index < 0) {
1202                 PMD_DEBUG_TRACE("port %d: MAC address array full\n", port_id);
1203                 return (-ENOSPC);
1204         }
1205
1206         /* Update NIC */
1207         (*dev->dev_ops->mac_addr_add)(dev, addr, index, pool);
1208
1209         /* Update address in NIC data structure */
1210         ether_addr_copy(addr, &dev->data->mac_addrs[index]);
1211
1212         return 0;
1213 }
1214
1215 int
1216 rte_eth_dev_mac_addr_remove(uint8_t port_id, struct ether_addr *addr)
1217 {
1218         struct rte_eth_dev *dev;
1219         int index;
1220
1221         if (port_id >= nb_ports) {
1222                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1223                 return (-ENODEV);
1224         }
1225         dev = &rte_eth_devices[port_id];
1226
1227         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_remove, -ENOTSUP);
1228         index = get_mac_addr_index(port_id, addr);
1229         if (index == 0) {
1230                 PMD_DEBUG_TRACE("port %d: Cannot remove default MAC address\n", port_id);
1231                 return (-EADDRINUSE);
1232         } else if (index < 0)
1233                 return 0;  /* Do nothing if address wasn't found */
1234
1235         /* Update NIC */
1236         (*dev->dev_ops->mac_addr_remove)(dev, index);
1237
1238         /* Update address in NIC data structure */
1239         ether_addr_copy(&null_mac_addr, &dev->data->mac_addrs[index]);
1240
1241         return 0;
1242 }
1243
1244 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
1245 uint16_t
1246 rte_eth_rx_burst(uint8_t port_id, uint16_t queue_id,
1247                  struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1248 {
1249         struct rte_eth_dev *dev;
1250
1251         if (port_id >= nb_ports) {
1252                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1253                 return 0;
1254         }
1255         dev = &rte_eth_devices[port_id];
1256
1257         FUNC_PTR_OR_ERR_RET(*dev->rx_pkt_burst, -ENOTSUP);
1258         if (queue_id >= dev->data->nb_rx_queues) {
1259                 PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", queue_id);
1260                 return 0;
1261         }
1262         return (*dev->rx_pkt_burst)(dev->data->rx_queues[queue_id],
1263                                                 rx_pkts, nb_pkts);
1264 }
1265
1266 uint16_t
1267 rte_eth_tx_burst(uint8_t port_id, uint16_t queue_id,
1268                  struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1269 {
1270         struct rte_eth_dev *dev;
1271
1272         if (port_id >= nb_ports) {
1273                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1274                 return 0;
1275         }
1276         dev = &rte_eth_devices[port_id];
1277
1278         FUNC_PTR_OR_ERR_RET(*dev->tx_pkt_burst, -ENOTSUP);
1279         if (queue_id >= dev->data->nb_tx_queues) {
1280                 PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", queue_id);
1281                 return 0;
1282         }
1283         return (*dev->tx_pkt_burst)(dev->data->tx_queues[queue_id],
1284                                                 tx_pkts, nb_pkts);
1285 }
1286 #endif
1287
1288 int
1289 rte_eth_dev_callback_register(uint8_t port_id,
1290                         enum rte_eth_event_type event,
1291                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
1292 {
1293         int ret = -1;
1294         struct rte_eth_dev *dev;
1295         struct rte_eth_dev_callback *user_cb = NULL;
1296
1297         if (!cb_fn)
1298                 return -1;
1299         if (port_id >= nb_ports) {
1300                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1301                 return -1;
1302         }
1303         dev = &rte_eth_devices[port_id];
1304         rte_spinlock_lock(&rte_eth_dev_cb_lock);
1305         TAILQ_FOREACH(user_cb, &(dev->callbacks), next) {
1306                 if (user_cb->cb_fn == cb_fn &&
1307                         user_cb->cb_arg == cb_arg &&
1308                         user_cb->event == event) {
1309                         ret = 0;
1310                         goto out;
1311                 }
1312         }
1313         user_cb = rte_malloc("INTR_USER_CALLBACK",
1314                 sizeof(struct rte_eth_dev_callback), 0);
1315         if (!user_cb)
1316                 goto out;
1317         user_cb->cb_fn = cb_fn;
1318         user_cb->cb_arg = cb_arg;
1319         user_cb->event = event;
1320         TAILQ_INSERT_TAIL(&(dev->callbacks), user_cb, next);
1321         ret = 0;
1322
1323 out:
1324         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1325
1326         return ret;
1327 }
1328
1329 int
1330 rte_eth_dev_callback_unregister(uint8_t port_id,
1331                         enum rte_eth_event_type event,
1332                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
1333 {
1334         int ret = -1;
1335         struct rte_eth_dev *dev;
1336         struct rte_eth_dev_callback *cb_lst = NULL;
1337
1338         if (!cb_fn)
1339                 return -1;
1340         if (port_id >= nb_ports) {
1341                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1342                 return -1;
1343         }
1344         dev = &rte_eth_devices[port_id];
1345         rte_spinlock_lock(&rte_eth_dev_cb_lock);
1346         TAILQ_FOREACH(cb_lst, &(dev->callbacks), next) {
1347                 if (cb_lst->cb_fn != cb_fn || cb_lst->event != event)
1348                         continue;
1349                 if (cb_lst->cb_arg == (void *)-1 ||
1350                                 cb_lst->cb_arg == cb_arg) {
1351                         TAILQ_REMOVE(&(dev->callbacks), cb_lst, next);
1352                         rte_free(cb_lst);
1353                         ret = 0;
1354                 }
1355         }
1356
1357         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1358
1359         return ret;
1360 }
1361
1362 void
1363 _rte_eth_dev_callback_process(struct rte_eth_dev *dev, enum rte_eth_event_type event)
1364 {
1365         struct rte_eth_dev_callback *cb_lst = NULL;
1366         struct rte_eth_dev_callback dev_cb;
1367
1368         rte_spinlock_lock(&rte_eth_dev_cb_lock);
1369         TAILQ_FOREACH(cb_lst, &(dev->callbacks), next) {
1370                 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
1371                         continue;
1372                 dev_cb = *cb_lst;
1373                 rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1374                 dev_cb.cb_fn(dev->data->port_id, dev_cb.event,
1375                                                 dev_cb.cb_arg);
1376                 rte_spinlock_lock(&rte_eth_dev_cb_lock);
1377         }
1378         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1379 }
1380