ethdev: add DCB support
[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 /* 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 };
128
129 enum {
130         STAT_QMAP_TX = 0,
131         STAT_QMAP_RX
132 };
133
134 static inline void
135 rte_eth_dev_data_alloc(void)
136 {
137         const unsigned flags = 0;
138         const struct rte_memzone *mz;
139
140         if (rte_eal_process_type() == RTE_PROC_PRIMARY){
141                 mz = rte_memzone_reserve(MZ_RTE_ETH_DEV_DATA,
142                                 RTE_MAX_ETHPORTS * sizeof(*rte_eth_dev_data),
143                                 rte_socket_id(), flags);
144         } else
145                 mz = rte_memzone_lookup(MZ_RTE_ETH_DEV_DATA);
146         if (mz == NULL)
147                 rte_panic("Cannot allocate memzone for ethernet port data\n");
148
149         rte_eth_dev_data = mz->addr;
150         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
151                 memset(rte_eth_dev_data, 0,
152                                 RTE_MAX_ETHPORTS * sizeof(*rte_eth_dev_data));
153 }
154
155 static inline struct rte_eth_dev *
156 rte_eth_dev_allocate(void)
157 {
158         struct rte_eth_dev *eth_dev;
159
160         if (nb_ports == RTE_MAX_ETHPORTS) {
161                 PMD_DEBUG_TRACE("Reached maximum number of ethernet ports\n");
162                 return NULL;
163         }
164
165         if (rte_eth_dev_data == NULL)
166                 rte_eth_dev_data_alloc();
167
168         eth_dev = &rte_eth_devices[nb_ports];
169         eth_dev->data = &rte_eth_dev_data[nb_ports];
170         eth_dev->data->port_id = nb_ports++;
171         return eth_dev;
172 }
173
174 static int
175 rte_eth_dev_init(struct rte_pci_driver *pci_drv,
176                  struct rte_pci_device *pci_dev)
177 {
178         struct eth_driver    *eth_drv;
179         struct rte_eth_dev *eth_dev;
180         int diag;
181
182         eth_drv = (struct eth_driver *)pci_drv;
183
184         eth_dev = rte_eth_dev_allocate();
185         if (eth_dev == NULL)
186                 return -ENOMEM;
187
188         if (rte_eal_process_type() == RTE_PROC_PRIMARY){
189                 eth_dev->data->dev_private = rte_zmalloc("ethdev private structure",
190                                   eth_drv->dev_private_size,
191                                   CACHE_LINE_SIZE);
192                 if (eth_dev->data->dev_private == NULL)
193                         rte_panic("Cannot allocate memzone for private port data\n");
194         }
195         eth_dev->pci_dev = pci_dev;
196         eth_dev->driver = eth_drv;
197         eth_dev->data->rx_mbuf_alloc_failed = 0;
198
199         /* init user callbacks */
200         TAILQ_INIT(&(eth_dev->callbacks));
201
202         /*
203          * Set the default maximum frame size.
204          */
205         eth_dev->data->max_frame_size = ETHER_MAX_LEN;
206
207         /* Invoke PMD device initialization function */
208         diag = (*eth_drv->eth_dev_init)(eth_drv, eth_dev);
209         if (diag == 0)
210                 return (0);
211
212         PMD_DEBUG_TRACE("driver %s: eth_dev_init(vendor_id=0x%u device_id=0x%x)"
213                         " failed\n", pci_drv->name,
214                         (unsigned) pci_dev->id.vendor_id,
215                         (unsigned) pci_dev->id.device_id);
216         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
217                 rte_free(eth_dev->data->dev_private);
218         nb_ports--;
219         return diag;
220 }
221
222 /**
223  * Register an Ethernet [Poll Mode] driver.
224  *
225  * Function invoked by the initialization function of an Ethernet driver
226  * to simultaneously register itself as a PCI driver and as an Ethernet
227  * Poll Mode Driver.
228  * Invokes the rte_eal_pci_register() function to register the *pci_drv*
229  * structure embedded in the *eth_drv* structure, after having stored the
230  * address of the rte_eth_dev_init() function in the *devinit* field of
231  * the *pci_drv* structure.
232  * During the PCI probing phase, the rte_eth_dev_init() function is
233  * invoked for each PCI [Ethernet device] matching the embedded PCI
234  * identifiers provided by the driver.
235  */
236 void
237 rte_eth_driver_register(struct eth_driver *eth_drv)
238 {
239         eth_drv->pci_drv.devinit = rte_eth_dev_init;
240         rte_eal_pci_register(&eth_drv->pci_drv);
241 }
242
243 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_RET(-E_RTE_SECONDARY);
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\n", 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\n",
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\n", 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\n",
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\n", 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\n",
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\n",
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 must be %d or %d\n",
338                                     port_id, ETH_16_POOLS, ETH_32_POOLS);
339                     return (-EINVAL);
340                 }
341         }
342         if (dev_conf->txmode.mq_mode == ETH_VMDQ_DCB_TX) {
343                 const struct rte_eth_vmdq_dcb_tx_conf *conf;
344
345                 if (nb_tx_q != ETH_VMDQ_DCB_NUM_QUEUES) {
346                         PMD_DEBUG_TRACE("ethdev port_id=%d VMDQ+DCB, nb_tx_q "
347                                         "!= %d\n",
348                                         port_id, ETH_VMDQ_DCB_NUM_QUEUES);
349                         return (-EINVAL);
350                 }
351                 conf = &(dev_conf->tx_adv_conf.vmdq_dcb_tx_conf);
352                 if (! (conf->nb_queue_pools == ETH_16_POOLS ||
353                        conf->nb_queue_pools == ETH_32_POOLS)) {
354                         PMD_DEBUG_TRACE("ethdev port_id=%d VMDQ+DCB selected, "
355                                     "nb_queue_pools != %d or nb_queue_pools "
356                                     "!= %d\n",
357                                     port_id, ETH_16_POOLS, ETH_32_POOLS);
358                         return (-EINVAL);
359                 }
360         }
361         
362         /* For DCB mode check our configuration before we go further */
363         if (dev_conf->rxmode.mq_mode == ETH_DCB_RX) {
364                 const struct rte_eth_dcb_rx_conf *conf;
365
366                 if (nb_rx_q != ETH_DCB_NUM_QUEUES) {
367                         PMD_DEBUG_TRACE("ethdev port_id=%d DCB, nb_rx_q "
368                                         "!= %d\n",
369                                         port_id, ETH_DCB_NUM_QUEUES);
370                         return (-EINVAL);
371                 }
372                 conf = &(dev_conf->rx_adv_conf.dcb_rx_conf);
373                 if (! (conf->nb_tcs == ETH_4_TCS ||
374                        conf->nb_tcs == ETH_8_TCS)) {
375                         PMD_DEBUG_TRACE("ethdev port_id=%d DCB selected, "
376                                     "nb_tcs != %d or nb_tcs "
377                                     "!= %d\n",
378                                     port_id, ETH_4_TCS, ETH_8_TCS);
379                         return (-EINVAL);
380                 }
381         }
382
383         if (dev_conf->txmode.mq_mode == ETH_DCB_TX) {
384                 const struct rte_eth_dcb_tx_conf *conf;
385
386                 if (nb_tx_q != ETH_DCB_NUM_QUEUES) {
387                         PMD_DEBUG_TRACE("ethdev port_id=%d DCB, nb_tx_q "
388                                         "!= %d\n",
389                                         port_id, ETH_DCB_NUM_QUEUES);
390                         return (-EINVAL);
391                 }
392                 conf = &(dev_conf->tx_adv_conf.dcb_tx_conf);
393                 if (! (conf->nb_tcs == ETH_4_TCS ||
394                        conf->nb_tcs == ETH_8_TCS)) {
395                         PMD_DEBUG_TRACE("ethdev port_id=%d DCB selected, "
396                                     "nb_tcs != %d or nb_tcs "
397                                     "!= %d\n",
398                                     port_id, ETH_4_TCS, ETH_8_TCS);
399                         return (-EINVAL);
400                 }
401         }
402
403         diag = (*dev->dev_ops->dev_configure)(dev, nb_rx_q, nb_tx_q);
404         if (diag != 0) {
405                 rte_free(dev->data->rx_queues);
406                 rte_free(dev->data->tx_queues);
407         }
408         return diag;
409 }
410
411 static void
412 rte_eth_dev_config_restore(uint8_t port_id)
413 {
414         struct rte_eth_dev *dev;
415         struct rte_eth_dev_info dev_info;
416         struct ether_addr addr;
417         uint16_t i;
418
419         dev = &rte_eth_devices[port_id];
420
421         rte_eth_dev_info_get(port_id, &dev_info);
422
423         /* replay MAC address configuration */
424         for (i = 0; i < dev_info.max_mac_addrs; i++) {
425                 addr = dev->data->mac_addrs[i];
426
427                 /* skip zero address */
428                 if (is_zero_ether_addr(&addr))
429                         continue;
430
431                 /* add address to the hardware */
432                 if  (*dev->dev_ops->mac_addr_add)
433                         (*dev->dev_ops->mac_addr_add)(dev, &addr, i, 0);
434                 else {
435                         PMD_DEBUG_TRACE("port %d: MAC address array not supported\n",
436                                         port_id);
437                         /* exit the loop but not return an error */
438                         break;
439                 }
440         }
441
442         /* replay promiscuous configuration */
443         if (rte_eth_promiscuous_get(port_id) == 1)
444                 rte_eth_promiscuous_enable(port_id);
445         else if (rte_eth_promiscuous_get(port_id) == 0)
446                 rte_eth_promiscuous_disable(port_id);
447
448         /* replay allmulticast configuration */
449         if (rte_eth_allmulticast_get(port_id) == 1)
450                 rte_eth_allmulticast_enable(port_id);
451         else if (rte_eth_allmulticast_get(port_id) == 0)
452                 rte_eth_allmulticast_disable(port_id);
453 }
454
455 int
456 rte_eth_dev_start(uint8_t port_id)
457 {
458         struct rte_eth_dev *dev;
459         int diag;
460
461         /* This function is only safe when called from the primary process
462          * in a multi-process setup*/
463         PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
464
465         if (port_id >= nb_ports) {
466                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
467                 return (-EINVAL);
468         }
469         dev = &rte_eth_devices[port_id];
470
471         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
472         diag = (*dev->dev_ops->dev_start)(dev);
473         if (diag == 0)
474                 dev->data->dev_started = 1;
475         else
476                 return diag;
477
478         rte_eth_dev_config_restore(port_id);
479
480         return 0;
481 }
482
483 void
484 rte_eth_dev_stop(uint8_t port_id)
485 {
486         struct rte_eth_dev *dev;
487
488         /* This function is only safe when called from the primary process
489          * in a multi-process setup*/
490         PROC_PRIMARY_OR_RET();
491
492         if (port_id >= nb_ports) {
493                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
494                 return;
495         }
496         dev = &rte_eth_devices[port_id];
497
498         FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
499         dev->data->dev_started = 0;
500         (*dev->dev_ops->dev_stop)(dev);
501 }
502
503 void
504 rte_eth_dev_close(uint8_t port_id)
505 {
506         struct rte_eth_dev *dev;
507
508         /* This function is only safe when called from the primary process
509          * in a multi-process setup*/
510         PROC_PRIMARY_OR_RET();
511
512         if (port_id >= nb_ports) {
513                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
514                 return;
515         }
516
517         dev = &rte_eth_devices[port_id];
518
519         FUNC_PTR_OR_RET(*dev->dev_ops->dev_close);
520         dev->data->dev_started = 0;
521         (*dev->dev_ops->dev_close)(dev);
522 }
523
524 int
525 rte_eth_rx_queue_setup(uint8_t port_id, uint16_t rx_queue_id,
526                        uint16_t nb_rx_desc, unsigned int socket_id,
527                        const struct rte_eth_rxconf *rx_conf,
528                        struct rte_mempool *mp)
529 {
530         struct rte_eth_dev *dev;
531         struct rte_pktmbuf_pool_private *mbp_priv;
532         struct rte_eth_dev_info dev_info;
533
534         /* This function is only safe when called from the primary process
535          * in a multi-process setup*/
536         PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
537
538         if (port_id >= nb_ports) {
539                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
540                 return (-EINVAL);
541         }
542         dev = &rte_eth_devices[port_id];
543         if (rx_queue_id >= dev->data->nb_rx_queues) {
544                 PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", rx_queue_id);
545                 return (-EINVAL);
546         }
547
548         if (dev->data->dev_started) {
549                 PMD_DEBUG_TRACE(
550                     "port %d must be stopped to allow configuration\n", port_id);
551                 return -EBUSY;
552         }
553
554         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
555         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_setup, -ENOTSUP);
556
557         /*
558          * Check the size of the mbuf data buffer.
559          * This value must be provided in the private data of the memory pool.
560          * First check that the memory pool has a valid private data.
561          */
562         (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
563         if (mp->private_data_size < sizeof(struct rte_pktmbuf_pool_private)) {
564                 PMD_DEBUG_TRACE("%s private_data_size %d < %d\n",
565                                 mp->name, (int) mp->private_data_size,
566                                 (int) sizeof(struct rte_pktmbuf_pool_private));
567                 return (-ENOSPC);
568         }
569         mbp_priv = (struct rte_pktmbuf_pool_private *)
570                 ((char *)mp + sizeof(struct rte_mempool));
571         if ((uint32_t) (mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM) <
572             dev_info.min_rx_bufsize) {
573                 PMD_DEBUG_TRACE("%s mbuf_data_room_size %d < %d "
574                                 "(RTE_PKTMBUF_HEADROOM=%d + min_rx_bufsize(dev)"
575                                 "=%d)\n",
576                                 mp->name,
577                                 (int)mbp_priv->mbuf_data_room_size,
578                                 (int)(RTE_PKTMBUF_HEADROOM +
579                                       dev_info.min_rx_bufsize),
580                                 (int)RTE_PKTMBUF_HEADROOM,
581                                 (int)dev_info.min_rx_bufsize);
582                 return (-EINVAL);
583         }
584
585         return (*dev->dev_ops->rx_queue_setup)(dev, rx_queue_id, nb_rx_desc,
586                                                socket_id, rx_conf, mp);
587 }
588
589 int
590 rte_eth_tx_queue_setup(uint8_t port_id, uint16_t tx_queue_id,
591                        uint16_t nb_tx_desc, unsigned int socket_id,
592                        const struct rte_eth_txconf *tx_conf)
593 {
594         struct rte_eth_dev *dev;
595
596         /* This function is only safe when called from the primary process
597          * in a multi-process setup*/
598         PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
599
600         if (port_id >= nb_ports) {
601                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
602                 return (-EINVAL);
603         }
604         dev = &rte_eth_devices[port_id];
605         if (tx_queue_id >= dev->data->nb_tx_queues) {
606                 PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", tx_queue_id);
607                 return (-EINVAL);
608         }
609
610         if (dev->data->dev_started) {
611                 PMD_DEBUG_TRACE(
612                     "port %d must be stopped to allow configuration\n", port_id);
613                 return -EBUSY;
614         }
615
616         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_setup, -ENOTSUP);
617         return (*dev->dev_ops->tx_queue_setup)(dev, tx_queue_id, nb_tx_desc,
618                                                socket_id, tx_conf);
619 }
620
621 void
622 rte_eth_promiscuous_enable(uint8_t port_id)
623 {
624         struct rte_eth_dev *dev;
625
626         if (port_id >= nb_ports) {
627                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
628                 return;
629         }
630         dev = &rte_eth_devices[port_id];
631
632         FUNC_PTR_OR_RET(*dev->dev_ops->promiscuous_enable);
633         (*dev->dev_ops->promiscuous_enable)(dev);
634         dev->data->promiscuous = 1;
635 }
636
637 void
638 rte_eth_promiscuous_disable(uint8_t port_id)
639 {
640         struct rte_eth_dev *dev;
641
642         if (port_id >= nb_ports) {
643                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
644                 return;
645         }
646         dev = &rte_eth_devices[port_id];
647
648         FUNC_PTR_OR_RET(*dev->dev_ops->promiscuous_disable);
649         dev->data->promiscuous = 0;
650         (*dev->dev_ops->promiscuous_disable)(dev);
651 }
652
653 int
654 rte_eth_promiscuous_get(uint8_t port_id)
655 {
656         struct rte_eth_dev *dev;
657
658         if (port_id >= nb_ports) {
659                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
660                 return -1;
661         }
662
663         dev = &rte_eth_devices[port_id];
664         return dev->data->promiscuous;
665 }
666
667 void
668 rte_eth_allmulticast_enable(uint8_t port_id)
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
678         FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_enable);
679         (*dev->dev_ops->allmulticast_enable)(dev);
680         dev->data->all_multicast = 1;
681 }
682
683 void
684 rte_eth_allmulticast_disable(uint8_t port_id)
685 {
686         struct rte_eth_dev *dev;
687
688         if (port_id >= nb_ports) {
689                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
690                 return;
691         }
692         dev = &rte_eth_devices[port_id];
693
694         FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_disable);
695         dev->data->all_multicast = 0;
696         (*dev->dev_ops->allmulticast_disable)(dev);
697 }
698
699 int
700 rte_eth_allmulticast_get(uint8_t port_id)
701 {
702         struct rte_eth_dev *dev;
703
704         if (port_id >= nb_ports) {
705                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
706                 return -1;
707         }
708
709         dev = &rte_eth_devices[port_id];
710         return dev->data->all_multicast;
711 }
712
713 static inline int
714 rte_eth_dev_atomic_read_link_status(struct rte_eth_dev *dev,
715                                 struct rte_eth_link *link)
716 {
717         struct rte_eth_link *dst = link;
718         struct rte_eth_link *src = &(dev->data->dev_link);
719
720         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
721                                         *(uint64_t *)src) == 0)
722                 return -1;
723
724         return 0;
725 }
726
727 void
728 rte_eth_link_get(uint8_t port_id, struct rte_eth_link *eth_link)
729 {
730         struct rte_eth_dev *dev;
731
732         if (port_id >= nb_ports) {
733                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
734                 return;
735         }
736         dev = &rte_eth_devices[port_id];
737         FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
738
739         if (dev->data->dev_conf.intr_conf.lsc != 0)
740                 rte_eth_dev_atomic_read_link_status(dev, eth_link);
741         else {
742                 (*dev->dev_ops->link_update)(dev, 1);
743                 *eth_link = dev->data->dev_link;
744         }
745 }
746
747 void
748 rte_eth_link_get_nowait(uint8_t port_id, struct rte_eth_link *eth_link)
749 {
750         struct rte_eth_dev *dev;
751
752         if (port_id >= nb_ports) {
753                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
754                 return;
755         }
756         dev = &rte_eth_devices[port_id];
757         FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
758
759         if (dev->data->dev_conf.intr_conf.lsc != 0)
760                 rte_eth_dev_atomic_read_link_status(dev, eth_link);
761         else {
762                 (*dev->dev_ops->link_update)(dev, 0);
763                 *eth_link = dev->data->dev_link;
764         }
765 }
766
767 void
768 rte_eth_stats_get(uint8_t port_id, struct rte_eth_stats *stats)
769 {
770         struct rte_eth_dev *dev;
771
772         if (port_id >= nb_ports) {
773                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
774                 return;
775         }
776         dev = &rte_eth_devices[port_id];
777
778         FUNC_PTR_OR_RET(*dev->dev_ops->stats_get);
779         (*dev->dev_ops->stats_get)(dev, stats);
780         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
781 }
782
783 void
784 rte_eth_stats_reset(uint8_t port_id)
785 {
786         struct rte_eth_dev *dev;
787
788         if (port_id >= nb_ports) {
789                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
790                 return;
791         }
792         dev = &rte_eth_devices[port_id];
793
794         FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
795         (*dev->dev_ops->stats_reset)(dev);
796 }
797
798
799 static int
800 set_queue_stats_mapping(uint8_t port_id, uint16_t queue_id, uint8_t stat_idx,
801                 uint8_t is_rx)
802 {
803         struct rte_eth_dev *dev;
804
805         if (port_id >= nb_ports) {
806                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
807                 return -ENODEV;
808         }
809         dev = &rte_eth_devices[port_id];
810
811         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_stats_mapping_set, -ENOTSUP);
812         return (*dev->dev_ops->queue_stats_mapping_set)
813                         (dev, queue_id, stat_idx, is_rx);
814 }
815
816
817 int
818 rte_eth_dev_set_tx_queue_stats_mapping(uint8_t port_id, uint16_t tx_queue_id,
819                 uint8_t stat_idx)
820 {
821         return set_queue_stats_mapping(port_id, tx_queue_id, stat_idx,
822                         STAT_QMAP_TX);
823 }
824
825
826 int
827 rte_eth_dev_set_rx_queue_stats_mapping(uint8_t port_id, uint16_t rx_queue_id,
828                 uint8_t stat_idx)
829 {
830         return set_queue_stats_mapping(port_id, rx_queue_id, stat_idx,
831                         STAT_QMAP_RX);
832 }
833
834
835 void
836 rte_eth_dev_info_get(uint8_t port_id, struct rte_eth_dev_info *dev_info)
837 {
838         struct rte_eth_dev *dev;
839
840         if (port_id >= nb_ports) {
841                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
842                 return;
843         }
844         dev = &rte_eth_devices[port_id];
845
846         FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
847         (*dev->dev_ops->dev_infos_get)(dev, dev_info);
848         dev_info->pci_dev = dev->pci_dev;
849         dev_info->driver_name = dev->driver->pci_drv.name;
850 }
851
852 void
853 rte_eth_macaddr_get(uint8_t port_id, struct ether_addr *mac_addr)
854 {
855         struct rte_eth_dev *dev;
856
857         if (port_id >= nb_ports) {
858                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
859                 return;
860         }
861         dev = &rte_eth_devices[port_id];
862         ether_addr_copy(&dev->data->mac_addrs[0], mac_addr);
863 }
864
865 int
866 rte_eth_dev_vlan_filter(uint8_t port_id, uint16_t vlan_id, int on)
867 {
868         struct rte_eth_dev *dev;
869
870         if (port_id >= nb_ports) {
871                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
872                 return (-ENODEV);
873         }
874         dev = &rte_eth_devices[port_id];
875         if (! (dev->data->dev_conf.rxmode.hw_vlan_filter)) {
876                 PMD_DEBUG_TRACE("port %d: vlan-filtering disabled\n", port_id);
877                 return (-ENOSYS);
878         }
879
880         if (vlan_id > 4095) {
881                 PMD_DEBUG_TRACE("(port_id=%d) invalid vlan_id=%u > 4095\n",
882                                 port_id, (unsigned) vlan_id);
883                 return (-EINVAL);
884         }
885         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_filter_set, -ENOTSUP);
886         (*dev->dev_ops->vlan_filter_set)(dev, vlan_id, on);
887         return (0);
888 }
889
890 int
891 rte_eth_dev_fdir_add_signature_filter(uint8_t port_id,
892                                       struct rte_fdir_filter *fdir_filter,
893                                       uint8_t queue)
894 {
895         struct rte_eth_dev *dev;
896
897         if (port_id >= nb_ports) {
898                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
899                 return (-ENODEV);
900         }
901
902         dev = &rte_eth_devices[port_id];
903
904         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
905                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
906                                 port_id, dev->data->dev_conf.fdir_conf.mode);
907                 return (-ENOSYS);
908         }
909
910         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
911              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
912             && (fdir_filter->port_src || fdir_filter->port_dst)) {
913                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
914                                 "None l4type, source & destinations ports " \
915                                 "should be null!\n");
916                 return (-EINVAL);
917         }
918
919         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_add_signature_filter, -ENOTSUP);
920         return (*dev->dev_ops->fdir_add_signature_filter)(dev, fdir_filter,
921                                                                 queue);
922 }
923
924 int
925 rte_eth_dev_fdir_update_signature_filter(uint8_t port_id,
926                                          struct rte_fdir_filter *fdir_filter,
927                                          uint8_t queue)
928 {
929         struct rte_eth_dev *dev;
930
931         if (port_id >= nb_ports) {
932                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
933                 return (-ENODEV);
934         }
935
936         dev = &rte_eth_devices[port_id];
937
938         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
939                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
940                                 port_id, dev->data->dev_conf.fdir_conf.mode);
941                 return (-ENOSYS);
942         }
943
944         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
945              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
946             && (fdir_filter->port_src || fdir_filter->port_dst)) {
947                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
948                                 "None l4type, source & destinations ports " \
949                                 "should be null!\n");
950                 return (-EINVAL);
951         }
952
953         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_update_signature_filter, -ENOTSUP);
954         return (*dev->dev_ops->fdir_update_signature_filter)(dev, fdir_filter,
955                                                                 queue);
956
957 }
958
959 int
960 rte_eth_dev_fdir_remove_signature_filter(uint8_t port_id,
961                                          struct rte_fdir_filter *fdir_filter)
962 {
963         struct rte_eth_dev *dev;
964
965         if (port_id >= nb_ports) {
966                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
967                 return (-ENODEV);
968         }
969
970         dev = &rte_eth_devices[port_id];
971
972         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
973                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
974                                 port_id, dev->data->dev_conf.fdir_conf.mode);
975                 return (-ENOSYS);
976         }
977
978         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
979              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
980             && (fdir_filter->port_src || fdir_filter->port_dst)) {
981                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
982                                 "None l4type source & destinations ports " \
983                                 "should be null!\n");
984                 return (-EINVAL);
985         }
986
987         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_remove_signature_filter, -ENOTSUP);
988         return (*dev->dev_ops->fdir_remove_signature_filter)(dev, fdir_filter);
989 }
990
991 int
992 rte_eth_dev_fdir_get_infos(uint8_t port_id, struct rte_eth_fdir *fdir)
993 {
994         struct rte_eth_dev *dev;
995
996         if (port_id >= nb_ports) {
997                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
998                 return (-ENODEV);
999         }
1000
1001         dev = &rte_eth_devices[port_id];
1002         if (! (dev->data->dev_conf.fdir_conf.mode)) {
1003                 PMD_DEBUG_TRACE("port %d: pkt-filter disabled\n", port_id);
1004                 return (-ENOSYS);
1005         }
1006
1007         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_infos_get, -ENOTSUP);
1008
1009         (*dev->dev_ops->fdir_infos_get)(dev, fdir);
1010         return (0);
1011 }
1012
1013 int
1014 rte_eth_dev_fdir_add_perfect_filter(uint8_t port_id,
1015                                     struct rte_fdir_filter *fdir_filter,
1016                                     uint16_t soft_id, uint8_t queue,
1017                                     uint8_t drop)
1018 {
1019         struct rte_eth_dev *dev;
1020
1021         if (port_id >= nb_ports) {
1022                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1023                 return (-ENODEV);
1024         }
1025
1026         dev = &rte_eth_devices[port_id];
1027
1028         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
1029                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1030                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1031                 return (-ENOSYS);
1032         }
1033
1034         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1035              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1036             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1037                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1038                                 "None l4type, source & destinations ports " \
1039                                 "should be null!\n");
1040                 return (-EINVAL);
1041         }
1042
1043         /* For now IPv6 is not supported with perfect filter */
1044         if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
1045                 return (-ENOTSUP);
1046
1047         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_add_perfect_filter, -ENOTSUP);
1048         return (*dev->dev_ops->fdir_add_perfect_filter)(dev, fdir_filter,
1049                                                                 soft_id, queue,
1050                                                                 drop);
1051 }
1052
1053 int
1054 rte_eth_dev_fdir_update_perfect_filter(uint8_t port_id,
1055                                        struct rte_fdir_filter *fdir_filter,
1056                                        uint16_t soft_id, uint8_t queue,
1057                                        uint8_t drop)
1058 {
1059         struct rte_eth_dev *dev;
1060
1061         if (port_id >= nb_ports) {
1062                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1063                 return (-ENODEV);
1064         }
1065
1066         dev = &rte_eth_devices[port_id];
1067
1068         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
1069                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1070                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1071                 return (-ENOSYS);
1072         }
1073
1074         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1075              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1076             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1077                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1078                                 "None l4type, source & destinations ports " \
1079                                 "should be null!\n");
1080                 return (-EINVAL);
1081         }
1082
1083         /* For now IPv6 is not supported with perfect filter */
1084         if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
1085                 return (-ENOTSUP);
1086
1087         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_update_perfect_filter, -ENOTSUP);
1088         return (*dev->dev_ops->fdir_update_perfect_filter)(dev, fdir_filter,
1089                                                         soft_id, queue, drop);
1090 }
1091
1092 int
1093 rte_eth_dev_fdir_remove_perfect_filter(uint8_t port_id,
1094                                        struct rte_fdir_filter *fdir_filter,
1095                                        uint16_t soft_id)
1096 {
1097         struct rte_eth_dev *dev;
1098
1099         if (port_id >= nb_ports) {
1100                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1101                 return (-ENODEV);
1102         }
1103
1104         dev = &rte_eth_devices[port_id];
1105
1106         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
1107                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1108                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1109                 return (-ENOSYS);
1110         }
1111
1112         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1113              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1114             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1115                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1116                                 "None l4type, source & destinations ports " \
1117                                 "should be null!\n");
1118                 return (-EINVAL);
1119         }
1120
1121         /* For now IPv6 is not supported with perfect filter */
1122         if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
1123                 return (-ENOTSUP);
1124
1125         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_remove_perfect_filter, -ENOTSUP);
1126         return (*dev->dev_ops->fdir_remove_perfect_filter)(dev, fdir_filter,
1127                                                                 soft_id);
1128 }
1129
1130 int
1131 rte_eth_dev_fdir_set_masks(uint8_t port_id, struct rte_fdir_masks *fdir_mask)
1132 {
1133         struct rte_eth_dev *dev;
1134
1135         if (port_id >= nb_ports) {
1136                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1137                 return (-ENODEV);
1138         }
1139
1140         dev = &rte_eth_devices[port_id];
1141         if (! (dev->data->dev_conf.fdir_conf.mode)) {
1142                 PMD_DEBUG_TRACE("port %d: pkt-filter disabled\n", port_id);
1143                 return (-ENOSYS);
1144         }
1145
1146         /* IPv6 mask are not supported */
1147         if (fdir_mask->src_ipv6_mask)
1148                 return (-ENOTSUP);
1149
1150         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_set_masks, -ENOTSUP);
1151         return (*dev->dev_ops->fdir_set_masks)(dev, fdir_mask);
1152 }
1153
1154 int
1155 rte_eth_dev_flow_ctrl_set(uint8_t port_id, struct rte_eth_fc_conf *fc_conf)
1156 {
1157         struct rte_eth_dev *dev;
1158
1159         if (port_id >= nb_ports) {
1160                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1161                 return (-ENODEV);
1162         }
1163
1164         if ((fc_conf->send_xon != 0) && (fc_conf->send_xon != 1)) {
1165                 PMD_DEBUG_TRACE("Invalid send_xon, only 0/1 allowed\n");
1166                 return (-EINVAL);
1167         }
1168
1169         dev = &rte_eth_devices[port_id];
1170
1171         /* High water, low water validation are device specific */
1172         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_set, -ENOTSUP);
1173         if  (*dev->dev_ops->flow_ctrl_set)
1174                 return (*dev->dev_ops->flow_ctrl_set)(dev, fc_conf);
1175
1176         return -ENOTSUP;
1177 }
1178
1179 int
1180 rte_eth_led_on(uint8_t port_id)
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         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_on, -ENOTSUP);
1191         return ((*dev->dev_ops->dev_led_on)(dev));
1192 }
1193
1194 int
1195 rte_eth_led_off(uint8_t port_id)
1196 {
1197         struct rte_eth_dev *dev;
1198
1199         if (port_id >= nb_ports) {
1200                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1201                 return (-ENODEV);
1202         }
1203
1204         dev = &rte_eth_devices[port_id];
1205         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_off, -ENOTSUP);
1206         return ((*dev->dev_ops->dev_led_off)(dev));
1207 }
1208
1209 /*
1210  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
1211  * an empty spot.
1212  */
1213 static inline int
1214 get_mac_addr_index(uint8_t port_id, struct ether_addr *addr)
1215 {
1216         struct rte_eth_dev_info dev_info;
1217         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1218         unsigned i;
1219
1220         rte_eth_dev_info_get(port_id, &dev_info);
1221
1222         for (i = 0; i < dev_info.max_mac_addrs; i++)
1223                 if (memcmp(addr, &dev->data->mac_addrs[i], ETHER_ADDR_LEN) == 0)
1224                         return i;
1225
1226         return -1;
1227 }
1228
1229 static struct ether_addr null_mac_addr = {{0, 0, 0, 0, 0, 0}};
1230
1231 int
1232 rte_eth_dev_mac_addr_add(uint8_t port_id, struct ether_addr *addr,
1233                 uint32_t pool)
1234 {
1235         struct rte_eth_dev *dev;
1236         int index;
1237
1238         if (port_id >= nb_ports) {
1239                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1240                 return (-ENODEV);
1241         }
1242         dev = &rte_eth_devices[port_id];
1243         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_add, -ENOTSUP);
1244
1245         if (is_zero_ether_addr(addr)) {
1246                 PMD_DEBUG_TRACE("port %d: Cannot add NULL MAC address\n", port_id);
1247                 return (-EINVAL);
1248         }
1249
1250         /* Check if it's already there, and do nothing */
1251         index = get_mac_addr_index(port_id, addr);
1252         if (index >= 0)
1253                 return 0;
1254
1255         index = get_mac_addr_index(port_id, &null_mac_addr);
1256         if (index < 0) {
1257                 PMD_DEBUG_TRACE("port %d: MAC address array full\n", port_id);
1258                 return (-ENOSPC);
1259         }
1260
1261         /* Update NIC */
1262         (*dev->dev_ops->mac_addr_add)(dev, addr, index, pool);
1263
1264         /* Update address in NIC data structure */
1265         ether_addr_copy(addr, &dev->data->mac_addrs[index]);
1266
1267         return 0;
1268 }
1269
1270 int
1271 rte_eth_dev_mac_addr_remove(uint8_t port_id, struct ether_addr *addr)
1272 {
1273         struct rte_eth_dev *dev;
1274         int index;
1275
1276         if (port_id >= nb_ports) {
1277                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1278                 return (-ENODEV);
1279         }
1280         dev = &rte_eth_devices[port_id];
1281         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_remove, -ENOTSUP);
1282
1283         index = get_mac_addr_index(port_id, addr);
1284         if (index == 0) {
1285                 PMD_DEBUG_TRACE("port %d: Cannot remove default MAC address\n", port_id);
1286                 return (-EADDRINUSE);
1287         } else if (index < 0)
1288                 return 0;  /* Do nothing if address wasn't found */
1289
1290         /* Update NIC */
1291         (*dev->dev_ops->mac_addr_remove)(dev, index);
1292
1293         /* Update address in NIC data structure */
1294         ether_addr_copy(&null_mac_addr, &dev->data->mac_addrs[index]);
1295
1296         return 0;
1297 }
1298
1299 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
1300 uint16_t
1301 rte_eth_rx_burst(uint8_t port_id, uint16_t queue_id,
1302                  struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1303 {
1304         struct rte_eth_dev *dev;
1305
1306         if (port_id >= nb_ports) {
1307                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1308                 return 0;
1309         }
1310         dev = &rte_eth_devices[port_id];
1311         FUNC_PTR_OR_ERR_RET(*dev->rx_pkt_burst, -ENOTSUP);
1312         if (queue_id >= dev->data->nb_rx_queues) {
1313                 PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", queue_id);
1314                 return 0;
1315         }
1316         return (*dev->rx_pkt_burst)(dev->data->rx_queues[queue_id],
1317                                                 rx_pkts, nb_pkts);
1318 }
1319
1320 uint16_t
1321 rte_eth_tx_burst(uint8_t port_id, uint16_t queue_id,
1322                  struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1323 {
1324         struct rte_eth_dev *dev;
1325
1326         if (port_id >= nb_ports) {
1327                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1328                 return 0;
1329         }
1330         dev = &rte_eth_devices[port_id];
1331
1332         FUNC_PTR_OR_ERR_RET(*dev->tx_pkt_burst, -ENOTSUP);
1333         if (queue_id >= dev->data->nb_tx_queues) {
1334                 PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", queue_id);
1335                 return 0;
1336         }
1337         return (*dev->tx_pkt_burst)(dev->data->tx_queues[queue_id],
1338                                                 tx_pkts, nb_pkts);
1339 }
1340 #endif
1341
1342 int
1343 rte_eth_dev_callback_register(uint8_t port_id,
1344                         enum rte_eth_event_type event,
1345                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
1346 {
1347         int ret = -1;
1348         struct rte_eth_dev *dev;
1349         struct rte_eth_dev_callback *user_cb = NULL;
1350
1351         if (!cb_fn)
1352                 return -1;
1353         if (port_id >= nb_ports) {
1354                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1355                 return -1;
1356         }
1357         dev = &rte_eth_devices[port_id];
1358         rte_spinlock_lock(&rte_eth_dev_cb_lock);
1359         TAILQ_FOREACH(user_cb, &(dev->callbacks), next) {
1360                 if (user_cb->cb_fn == cb_fn &&
1361                         user_cb->cb_arg == cb_arg &&
1362                         user_cb->event == event) {
1363                         ret = 0;
1364                         goto out;
1365                 }
1366         }
1367         user_cb = rte_malloc("INTR_USER_CALLBACK",
1368                 sizeof(struct rte_eth_dev_callback), 0);
1369         if (!user_cb)
1370                 goto out;
1371         user_cb->cb_fn = cb_fn;
1372         user_cb->cb_arg = cb_arg;
1373         user_cb->event = event;
1374         TAILQ_INSERT_TAIL(&(dev->callbacks), user_cb, next);
1375         ret = 0;
1376
1377 out:
1378         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1379
1380         return ret;
1381 }
1382
1383 int
1384 rte_eth_dev_callback_unregister(uint8_t port_id,
1385                         enum rte_eth_event_type event,
1386                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
1387 {
1388         int ret = -1;
1389         struct rte_eth_dev *dev;
1390         struct rte_eth_dev_callback *cb_lst = NULL;
1391
1392         if (!cb_fn)
1393                 return -1;
1394         if (port_id >= nb_ports) {
1395                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1396                 return -1;
1397         }
1398         dev = &rte_eth_devices[port_id];
1399         rte_spinlock_lock(&rte_eth_dev_cb_lock);
1400         TAILQ_FOREACH(cb_lst, &(dev->callbacks), next) {
1401                 if (cb_lst->cb_fn != cb_fn || cb_lst->event != event)
1402                         continue;
1403                 if (cb_lst->cb_arg == (void *)-1 ||
1404                                 cb_lst->cb_arg == cb_arg) {
1405                         TAILQ_REMOVE(&(dev->callbacks), cb_lst, next);
1406                         rte_free(cb_lst);
1407                         ret = 0;
1408                 }
1409         }
1410
1411         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1412
1413         return ret;
1414 }
1415
1416 void
1417 _rte_eth_dev_callback_process(struct rte_eth_dev *dev, enum rte_eth_event_type event)
1418 {
1419         struct rte_eth_dev_callback *cb_lst = NULL;
1420         struct rte_eth_dev_callback dev_cb;
1421
1422         rte_spinlock_lock(&rte_eth_dev_cb_lock);
1423         TAILQ_FOREACH(cb_lst, &(dev->callbacks), next) {
1424                 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
1425                         continue;
1426                 dev_cb = *cb_lst;
1427                 rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1428                 dev_cb.cb_fn(dev->data->port_id, dev_cb.event,
1429                                                 dev_cb.cb_arg);
1430                 rte_spinlock_lock(&rte_eth_dev_cb_lock);
1431         }
1432         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1433 }
1434