ethdev: add RX/TX queues configuration
[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 static int
250 rte_eth_dev_rx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
251 {
252         uint16_t old_nb_queues = dev->data->nb_rx_queues;
253         void **rxq;
254         unsigned i;
255
256         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_release, -ENOTSUP);
257
258         if (dev->data->rx_queues == NULL) {
259                 dev->data->rx_queues = rte_zmalloc("ethdev->rx_queues",
260                                 sizeof(dev->data->rx_queues[0]) * nb_queues,
261                                 CACHE_LINE_SIZE);
262                 if (dev->data->rx_queues == NULL) {
263                         dev->data->nb_rx_queues = 0;
264                         return -(ENOMEM);
265                 }
266         } else {
267                 rxq = dev->data->rx_queues;
268
269                 for (i = nb_queues; i < old_nb_queues; i++)
270                         (*dev->dev_ops->rx_queue_release)(rxq[i]);
271                 rxq = rte_realloc(rxq, sizeof(rxq[0]) * nb_queues,
272                                 CACHE_LINE_SIZE);
273                 if (rxq == NULL)
274                         return -(ENOMEM);
275
276                 if (nb_queues > old_nb_queues)
277                         memset(rxq + old_nb_queues, 0,
278                                 sizeof(rxq[0]) * (nb_queues - old_nb_queues));
279
280                 dev->data->rx_queues = rxq;
281
282         }
283         dev->data->nb_rx_queues = nb_queues;
284         return (0);
285 }
286
287 static int
288 rte_eth_dev_tx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
289 {
290         uint16_t old_nb_queues = dev->data->nb_tx_queues;
291         void **txq;
292         unsigned i;
293
294         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_release, -ENOTSUP);
295
296         if (dev->data->tx_queues == NULL) {
297                 dev->data->tx_queues = rte_zmalloc("ethdev->tx_queues",
298                                 sizeof(dev->data->tx_queues[0]) * nb_queues,
299                                 CACHE_LINE_SIZE);
300                 if (dev->data->tx_queues == NULL) {
301                         dev->data->nb_tx_queues = 0;
302                         return -(ENOMEM);
303                 }
304         } else {
305                 txq = dev->data->tx_queues;
306
307                 for (i = nb_queues; i < old_nb_queues; i++)
308                         (*dev->dev_ops->tx_queue_release)(txq[i]);
309                 txq = rte_realloc(txq, sizeof(txq[0]) * nb_queues,
310                                 CACHE_LINE_SIZE);
311                 if (txq == NULL)
312                         return -(ENOMEM);
313
314                 if (nb_queues > old_nb_queues)
315                         memset(txq + old_nb_queues, 0,
316                                 sizeof(txq[0]) * (nb_queues - old_nb_queues));
317
318                 dev->data->tx_queues = txq;
319
320         }
321         dev->data->nb_tx_queues = nb_queues;
322         return (0);
323 }
324
325 int
326 rte_eth_dev_configure(uint8_t port_id, uint16_t nb_rx_q, uint16_t nb_tx_q,
327                       const struct rte_eth_conf *dev_conf)
328 {
329         struct rte_eth_dev *dev;
330         struct rte_eth_dev_info dev_info;
331         int diag;
332
333         /* This function is only safe when called from the primary process
334          * in a multi-process setup*/
335         PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
336
337         if (port_id >= nb_ports || port_id >= RTE_MAX_ETHPORTS) {
338                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
339                 return (-EINVAL);
340         }
341         dev = &rte_eth_devices[port_id];
342
343         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
344         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_configure, -ENOTSUP);
345
346         if (dev->data->dev_started) {
347                 PMD_DEBUG_TRACE(
348                     "port %d must be stopped to allow configuration\n", port_id);
349                 return (-EBUSY);
350         }
351
352         /*
353          * Check that the numbers of RX and TX queues are not greater
354          * than the maximum number of RX and TX queues supported by the
355          * configured device.
356          */
357         (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
358         if (nb_rx_q > dev_info.max_rx_queues) {
359                 PMD_DEBUG_TRACE("ethdev port_id=%d nb_rx_queues=%d > %d\n",
360                                 port_id, nb_rx_q, dev_info.max_rx_queues);
361                 return (-EINVAL);
362         }
363         if (nb_rx_q == 0) {
364                 PMD_DEBUG_TRACE("ethdev port_id=%d nb_rx_q == 0\n", port_id);
365                 return (-EINVAL);
366         }
367
368         if (nb_tx_q > dev_info.max_tx_queues) {
369                 PMD_DEBUG_TRACE("ethdev port_id=%d nb_tx_queues=%d > %d\n",
370                                 port_id, nb_tx_q, dev_info.max_tx_queues);
371                 return (-EINVAL);
372         }
373         if (nb_tx_q == 0) {
374                 PMD_DEBUG_TRACE("ethdev port_id=%d nb_tx_q == 0\n", port_id);
375                 return (-EINVAL);
376         }
377
378         /* Copy the dev_conf parameter into the dev structure */
379         memcpy(&dev->data->dev_conf, dev_conf, sizeof(dev->data->dev_conf));
380
381         /*
382          * If jumbo frames are enabled, check that the maximum RX packet
383          * length is supported by the configured device.
384          */
385         if (dev_conf->rxmode.jumbo_frame == 1) {
386                 if (dev_conf->rxmode.max_rx_pkt_len >
387                     dev_info.max_rx_pktlen) {
388                         PMD_DEBUG_TRACE("ethdev port_id=%d max_rx_pkt_len %u"
389                                 " > max valid value %u\n",
390                                 port_id,
391                                 (unsigned)dev_conf->rxmode.max_rx_pkt_len,
392                                 (unsigned)dev_info.max_rx_pktlen);
393                         return (-EINVAL);
394                 }
395                 else if (dev_conf->rxmode.max_rx_pkt_len < ETHER_MIN_LEN) {
396                         PMD_DEBUG_TRACE("ethdev port_id=%d max_rx_pkt_len %u"
397                                 " < min valid value %u\n",
398                                 port_id,
399                                 (unsigned)dev_conf->rxmode.max_rx_pkt_len,
400                                 (unsigned)ETHER_MIN_LEN);
401                         return (-EINVAL);
402                 }
403         } else
404                 /* Use default value */
405                 dev->data->dev_conf.rxmode.max_rx_pkt_len = ETHER_MAX_LEN;
406
407         /* For vmdb+dcb mode check our configuration before we go further */
408         if (dev_conf->rxmode.mq_mode == ETH_VMDQ_DCB) {
409                 const struct rte_eth_vmdq_dcb_conf *conf;
410
411                 if (nb_rx_q != ETH_VMDQ_DCB_NUM_QUEUES) {
412                         PMD_DEBUG_TRACE("ethdev port_id=%d VMDQ+DCB, nb_rx_q "
413                                         "!= %d\n",
414                                         port_id, ETH_VMDQ_DCB_NUM_QUEUES);
415                         return (-EINVAL);
416                 }
417                 conf = &(dev_conf->rx_adv_conf.vmdq_dcb_conf);
418                 if (! (conf->nb_queue_pools == ETH_16_POOLS ||
419                        conf->nb_queue_pools == ETH_32_POOLS)) {
420                     PMD_DEBUG_TRACE("ethdev port_id=%d VMDQ+DCB selected, "
421                                     "nb_queue_pools must be %d or %d\n",
422                                     port_id, ETH_16_POOLS, ETH_32_POOLS);
423                     return (-EINVAL);
424                 }
425         }
426         if (dev_conf->txmode.mq_mode == ETH_VMDQ_DCB_TX) {
427                 const struct rte_eth_vmdq_dcb_tx_conf *conf;
428
429                 if (nb_tx_q != ETH_VMDQ_DCB_NUM_QUEUES) {
430                         PMD_DEBUG_TRACE("ethdev port_id=%d VMDQ+DCB, nb_tx_q "
431                                         "!= %d\n",
432                                         port_id, ETH_VMDQ_DCB_NUM_QUEUES);
433                         return (-EINVAL);
434                 }
435                 conf = &(dev_conf->tx_adv_conf.vmdq_dcb_tx_conf);
436                 if (! (conf->nb_queue_pools == ETH_16_POOLS ||
437                        conf->nb_queue_pools == ETH_32_POOLS)) {
438                         PMD_DEBUG_TRACE("ethdev port_id=%d VMDQ+DCB selected, "
439                                     "nb_queue_pools != %d or nb_queue_pools "
440                                     "!= %d\n",
441                                     port_id, ETH_16_POOLS, ETH_32_POOLS);
442                         return (-EINVAL);
443                 }
444         }
445         
446         /* For DCB mode check our configuration before we go further */
447         if (dev_conf->rxmode.mq_mode == ETH_DCB_RX) {
448                 const struct rte_eth_dcb_rx_conf *conf;
449
450                 if (nb_rx_q != ETH_DCB_NUM_QUEUES) {
451                         PMD_DEBUG_TRACE("ethdev port_id=%d DCB, nb_rx_q "
452                                         "!= %d\n",
453                                         port_id, ETH_DCB_NUM_QUEUES);
454                         return (-EINVAL);
455                 }
456                 conf = &(dev_conf->rx_adv_conf.dcb_rx_conf);
457                 if (! (conf->nb_tcs == ETH_4_TCS ||
458                        conf->nb_tcs == ETH_8_TCS)) {
459                         PMD_DEBUG_TRACE("ethdev port_id=%d DCB selected, "
460                                     "nb_tcs != %d or nb_tcs "
461                                     "!= %d\n",
462                                     port_id, ETH_4_TCS, ETH_8_TCS);
463                         return (-EINVAL);
464                 }
465         }
466
467         if (dev_conf->txmode.mq_mode == ETH_DCB_TX) {
468                 const struct rte_eth_dcb_tx_conf *conf;
469
470                 if (nb_tx_q != ETH_DCB_NUM_QUEUES) {
471                         PMD_DEBUG_TRACE("ethdev port_id=%d DCB, nb_tx_q "
472                                         "!= %d\n",
473                                         port_id, ETH_DCB_NUM_QUEUES);
474                         return (-EINVAL);
475                 }
476                 conf = &(dev_conf->tx_adv_conf.dcb_tx_conf);
477                 if (! (conf->nb_tcs == ETH_4_TCS ||
478                        conf->nb_tcs == ETH_8_TCS)) {
479                         PMD_DEBUG_TRACE("ethdev port_id=%d DCB selected, "
480                                     "nb_tcs != %d or nb_tcs "
481                                     "!= %d\n",
482                                     port_id, ETH_4_TCS, ETH_8_TCS);
483                         return (-EINVAL);
484                 }
485         }
486
487         /*
488          * Setup new number of RX/TX queues and reconfigure device.
489          */
490         diag = rte_eth_dev_rx_queue_config(dev, nb_rx_q);
491         if (diag != 0) {
492                 PMD_DEBUG_TRACE("port%d rte_eth_dev_rx_queue_config = %d\n",
493                                 port_id, diag);
494                 return diag;
495         }
496
497         diag = rte_eth_dev_tx_queue_config(dev, nb_tx_q);
498         if (diag != 0) {
499                 PMD_DEBUG_TRACE("port%d rte_eth_dev_tx_queue_config = %d\n",
500                                 port_id, diag);
501                 rte_eth_dev_rx_queue_config(dev, 0);
502                 return diag;
503         }
504
505         diag = (*dev->dev_ops->dev_configure)(dev);
506         if (diag != 0) {
507                 PMD_DEBUG_TRACE("port%d dev_configure = %d\n",
508                                 port_id, diag);
509                 rte_eth_dev_rx_queue_config(dev, 0);
510                 rte_eth_dev_tx_queue_config(dev, 0);
511                 return diag;
512         }
513
514         return 0;
515 }
516
517 static void
518 rte_eth_dev_config_restore(uint8_t port_id)
519 {
520         struct rte_eth_dev *dev;
521         struct rte_eth_dev_info dev_info;
522         struct ether_addr addr;
523         uint16_t i;
524
525         dev = &rte_eth_devices[port_id];
526
527         rte_eth_dev_info_get(port_id, &dev_info);
528
529         /* replay MAC address configuration */
530         for (i = 0; i < dev_info.max_mac_addrs; i++) {
531                 addr = dev->data->mac_addrs[i];
532
533                 /* skip zero address */
534                 if (is_zero_ether_addr(&addr))
535                         continue;
536
537                 /* add address to the hardware */
538                 if  (*dev->dev_ops->mac_addr_add)
539                         (*dev->dev_ops->mac_addr_add)(dev, &addr, i, 0);
540                 else {
541                         PMD_DEBUG_TRACE("port %d: MAC address array not supported\n",
542                                         port_id);
543                         /* exit the loop but not return an error */
544                         break;
545                 }
546         }
547
548         /* replay promiscuous configuration */
549         if (rte_eth_promiscuous_get(port_id) == 1)
550                 rte_eth_promiscuous_enable(port_id);
551         else if (rte_eth_promiscuous_get(port_id) == 0)
552                 rte_eth_promiscuous_disable(port_id);
553
554         /* replay allmulticast configuration */
555         if (rte_eth_allmulticast_get(port_id) == 1)
556                 rte_eth_allmulticast_enable(port_id);
557         else if (rte_eth_allmulticast_get(port_id) == 0)
558                 rte_eth_allmulticast_disable(port_id);
559 }
560
561 int
562 rte_eth_dev_start(uint8_t port_id)
563 {
564         struct rte_eth_dev *dev;
565         int diag;
566
567         /* This function is only safe when called from the primary process
568          * in a multi-process setup*/
569         PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
570
571         if (port_id >= nb_ports) {
572                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
573                 return (-EINVAL);
574         }
575         dev = &rte_eth_devices[port_id];
576
577         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
578         diag = (*dev->dev_ops->dev_start)(dev);
579         if (diag == 0)
580                 dev->data->dev_started = 1;
581         else
582                 return diag;
583
584         rte_eth_dev_config_restore(port_id);
585
586         return 0;
587 }
588
589 void
590 rte_eth_dev_stop(uint8_t port_id)
591 {
592         struct rte_eth_dev *dev;
593
594         /* This function is only safe when called from the primary process
595          * in a multi-process setup*/
596         PROC_PRIMARY_OR_RET();
597
598         if (port_id >= nb_ports) {
599                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
600                 return;
601         }
602         dev = &rte_eth_devices[port_id];
603
604         FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
605         dev->data->dev_started = 0;
606         (*dev->dev_ops->dev_stop)(dev);
607 }
608
609 void
610 rte_eth_dev_close(uint8_t port_id)
611 {
612         struct rte_eth_dev *dev;
613
614         /* This function is only safe when called from the primary process
615          * in a multi-process setup*/
616         PROC_PRIMARY_OR_RET();
617
618         if (port_id >= nb_ports) {
619                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
620                 return;
621         }
622
623         dev = &rte_eth_devices[port_id];
624
625         FUNC_PTR_OR_RET(*dev->dev_ops->dev_close);
626         dev->data->dev_started = 0;
627         (*dev->dev_ops->dev_close)(dev);
628 }
629
630 int
631 rte_eth_rx_queue_setup(uint8_t port_id, uint16_t rx_queue_id,
632                        uint16_t nb_rx_desc, unsigned int socket_id,
633                        const struct rte_eth_rxconf *rx_conf,
634                        struct rte_mempool *mp)
635 {
636         struct rte_eth_dev *dev;
637         struct rte_pktmbuf_pool_private *mbp_priv;
638         struct rte_eth_dev_info dev_info;
639
640         /* This function is only safe when called from the primary process
641          * in a multi-process setup*/
642         PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
643
644         if (port_id >= nb_ports) {
645                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
646                 return (-EINVAL);
647         }
648         dev = &rte_eth_devices[port_id];
649         if (rx_queue_id >= dev->data->nb_rx_queues) {
650                 PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", rx_queue_id);
651                 return (-EINVAL);
652         }
653
654         if (dev->data->dev_started) {
655                 PMD_DEBUG_TRACE(
656                     "port %d must be stopped to allow configuration\n", port_id);
657                 return -EBUSY;
658         }
659
660         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
661         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_setup, -ENOTSUP);
662
663         /*
664          * Check the size of the mbuf data buffer.
665          * This value must be provided in the private data of the memory pool.
666          * First check that the memory pool has a valid private data.
667          */
668         (*dev->dev_ops->dev_infos_get)(dev, &dev_info);
669         if (mp->private_data_size < sizeof(struct rte_pktmbuf_pool_private)) {
670                 PMD_DEBUG_TRACE("%s private_data_size %d < %d\n",
671                                 mp->name, (int) mp->private_data_size,
672                                 (int) sizeof(struct rte_pktmbuf_pool_private));
673                 return (-ENOSPC);
674         }
675         mbp_priv = (struct rte_pktmbuf_pool_private *)
676                 ((char *)mp + sizeof(struct rte_mempool));
677         if ((uint32_t) (mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM) <
678             dev_info.min_rx_bufsize) {
679                 PMD_DEBUG_TRACE("%s mbuf_data_room_size %d < %d "
680                                 "(RTE_PKTMBUF_HEADROOM=%d + min_rx_bufsize(dev)"
681                                 "=%d)\n",
682                                 mp->name,
683                                 (int)mbp_priv->mbuf_data_room_size,
684                                 (int)(RTE_PKTMBUF_HEADROOM +
685                                       dev_info.min_rx_bufsize),
686                                 (int)RTE_PKTMBUF_HEADROOM,
687                                 (int)dev_info.min_rx_bufsize);
688                 return (-EINVAL);
689         }
690
691         return (*dev->dev_ops->rx_queue_setup)(dev, rx_queue_id, nb_rx_desc,
692                                                socket_id, rx_conf, mp);
693 }
694
695 int
696 rte_eth_tx_queue_setup(uint8_t port_id, uint16_t tx_queue_id,
697                        uint16_t nb_tx_desc, unsigned int socket_id,
698                        const struct rte_eth_txconf *tx_conf)
699 {
700         struct rte_eth_dev *dev;
701
702         /* This function is only safe when called from the primary process
703          * in a multi-process setup*/
704         PROC_PRIMARY_OR_ERR_RET(-E_RTE_SECONDARY);
705
706         if (port_id >= RTE_MAX_ETHPORTS || port_id >= nb_ports) {
707                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
708                 return (-EINVAL);
709         }
710         dev = &rte_eth_devices[port_id];
711         if (tx_queue_id >= dev->data->nb_tx_queues) {
712                 PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", tx_queue_id);
713                 return (-EINVAL);
714         }
715
716         if (dev->data->dev_started) {
717                 PMD_DEBUG_TRACE(
718                     "port %d must be stopped to allow configuration\n", port_id);
719                 return -EBUSY;
720         }
721
722         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->tx_queue_setup, -ENOTSUP);
723         return (*dev->dev_ops->tx_queue_setup)(dev, tx_queue_id, nb_tx_desc,
724                                                socket_id, tx_conf);
725 }
726
727 void
728 rte_eth_promiscuous_enable(uint8_t port_id)
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
738         FUNC_PTR_OR_RET(*dev->dev_ops->promiscuous_enable);
739         (*dev->dev_ops->promiscuous_enable)(dev);
740         dev->data->promiscuous = 1;
741 }
742
743 void
744 rte_eth_promiscuous_disable(uint8_t port_id)
745 {
746         struct rte_eth_dev *dev;
747
748         if (port_id >= nb_ports) {
749                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
750                 return;
751         }
752         dev = &rte_eth_devices[port_id];
753
754         FUNC_PTR_OR_RET(*dev->dev_ops->promiscuous_disable);
755         dev->data->promiscuous = 0;
756         (*dev->dev_ops->promiscuous_disable)(dev);
757 }
758
759 int
760 rte_eth_promiscuous_get(uint8_t port_id)
761 {
762         struct rte_eth_dev *dev;
763
764         if (port_id >= nb_ports) {
765                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
766                 return -1;
767         }
768
769         dev = &rte_eth_devices[port_id];
770         return dev->data->promiscuous;
771 }
772
773 void
774 rte_eth_allmulticast_enable(uint8_t port_id)
775 {
776         struct rte_eth_dev *dev;
777
778         if (port_id >= nb_ports) {
779                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
780                 return;
781         }
782         dev = &rte_eth_devices[port_id];
783
784         FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_enable);
785         (*dev->dev_ops->allmulticast_enable)(dev);
786         dev->data->all_multicast = 1;
787 }
788
789 void
790 rte_eth_allmulticast_disable(uint8_t port_id)
791 {
792         struct rte_eth_dev *dev;
793
794         if (port_id >= nb_ports) {
795                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
796                 return;
797         }
798         dev = &rte_eth_devices[port_id];
799
800         FUNC_PTR_OR_RET(*dev->dev_ops->allmulticast_disable);
801         dev->data->all_multicast = 0;
802         (*dev->dev_ops->allmulticast_disable)(dev);
803 }
804
805 int
806 rte_eth_allmulticast_get(uint8_t port_id)
807 {
808         struct rte_eth_dev *dev;
809
810         if (port_id >= nb_ports) {
811                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
812                 return -1;
813         }
814
815         dev = &rte_eth_devices[port_id];
816         return dev->data->all_multicast;
817 }
818
819 static inline int
820 rte_eth_dev_atomic_read_link_status(struct rte_eth_dev *dev,
821                                 struct rte_eth_link *link)
822 {
823         struct rte_eth_link *dst = link;
824         struct rte_eth_link *src = &(dev->data->dev_link);
825
826         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
827                                         *(uint64_t *)src) == 0)
828                 return -1;
829
830         return 0;
831 }
832
833 void
834 rte_eth_link_get(uint8_t port_id, struct rte_eth_link *eth_link)
835 {
836         struct rte_eth_dev *dev;
837
838         if (port_id >= nb_ports) {
839                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
840                 return;
841         }
842         dev = &rte_eth_devices[port_id];
843         FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
844
845         if (dev->data->dev_conf.intr_conf.lsc != 0)
846                 rte_eth_dev_atomic_read_link_status(dev, eth_link);
847         else {
848                 (*dev->dev_ops->link_update)(dev, 1);
849                 *eth_link = dev->data->dev_link;
850         }
851 }
852
853 void
854 rte_eth_link_get_nowait(uint8_t port_id, struct rte_eth_link *eth_link)
855 {
856         struct rte_eth_dev *dev;
857
858         if (port_id >= nb_ports) {
859                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
860                 return;
861         }
862         dev = &rte_eth_devices[port_id];
863         FUNC_PTR_OR_RET(*dev->dev_ops->link_update);
864
865         if (dev->data->dev_conf.intr_conf.lsc != 0)
866                 rte_eth_dev_atomic_read_link_status(dev, eth_link);
867         else {
868                 (*dev->dev_ops->link_update)(dev, 0);
869                 *eth_link = dev->data->dev_link;
870         }
871 }
872
873 void
874 rte_eth_stats_get(uint8_t port_id, struct rte_eth_stats *stats)
875 {
876         struct rte_eth_dev *dev;
877
878         if (port_id >= nb_ports) {
879                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
880                 return;
881         }
882         dev = &rte_eth_devices[port_id];
883
884         FUNC_PTR_OR_RET(*dev->dev_ops->stats_get);
885         (*dev->dev_ops->stats_get)(dev, stats);
886         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
887 }
888
889 void
890 rte_eth_stats_reset(uint8_t port_id)
891 {
892         struct rte_eth_dev *dev;
893
894         if (port_id >= nb_ports) {
895                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
896                 return;
897         }
898         dev = &rte_eth_devices[port_id];
899
900         FUNC_PTR_OR_RET(*dev->dev_ops->stats_reset);
901         (*dev->dev_ops->stats_reset)(dev);
902 }
903
904
905 static int
906 set_queue_stats_mapping(uint8_t port_id, uint16_t queue_id, uint8_t stat_idx,
907                 uint8_t is_rx)
908 {
909         struct rte_eth_dev *dev;
910
911         if (port_id >= nb_ports) {
912                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
913                 return -ENODEV;
914         }
915         dev = &rte_eth_devices[port_id];
916
917         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_stats_mapping_set, -ENOTSUP);
918         return (*dev->dev_ops->queue_stats_mapping_set)
919                         (dev, queue_id, stat_idx, is_rx);
920 }
921
922
923 int
924 rte_eth_dev_set_tx_queue_stats_mapping(uint8_t port_id, uint16_t tx_queue_id,
925                 uint8_t stat_idx)
926 {
927         return set_queue_stats_mapping(port_id, tx_queue_id, stat_idx,
928                         STAT_QMAP_TX);
929 }
930
931
932 int
933 rte_eth_dev_set_rx_queue_stats_mapping(uint8_t port_id, uint16_t rx_queue_id,
934                 uint8_t stat_idx)
935 {
936         return set_queue_stats_mapping(port_id, rx_queue_id, stat_idx,
937                         STAT_QMAP_RX);
938 }
939
940
941 void
942 rte_eth_dev_info_get(uint8_t port_id, struct rte_eth_dev_info *dev_info)
943 {
944         struct rte_eth_dev *dev;
945
946         if (port_id >= nb_ports) {
947                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
948                 return;
949         }
950         dev = &rte_eth_devices[port_id];
951
952         FUNC_PTR_OR_RET(*dev->dev_ops->dev_infos_get);
953         (*dev->dev_ops->dev_infos_get)(dev, dev_info);
954         dev_info->pci_dev = dev->pci_dev;
955         dev_info->driver_name = dev->driver->pci_drv.name;
956 }
957
958 void
959 rte_eth_macaddr_get(uint8_t port_id, struct ether_addr *mac_addr)
960 {
961         struct rte_eth_dev *dev;
962
963         if (port_id >= nb_ports) {
964                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
965                 return;
966         }
967         dev = &rte_eth_devices[port_id];
968         ether_addr_copy(&dev->data->mac_addrs[0], mac_addr);
969 }
970
971 int
972 rte_eth_dev_vlan_filter(uint8_t port_id, uint16_t vlan_id, int on)
973 {
974         struct rte_eth_dev *dev;
975
976         if (port_id >= nb_ports) {
977                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
978                 return (-ENODEV);
979         }
980         dev = &rte_eth_devices[port_id];
981         if (! (dev->data->dev_conf.rxmode.hw_vlan_filter)) {
982                 PMD_DEBUG_TRACE("port %d: vlan-filtering disabled\n", port_id);
983                 return (-ENOSYS);
984         }
985
986         if (vlan_id > 4095) {
987                 PMD_DEBUG_TRACE("(port_id=%d) invalid vlan_id=%u > 4095\n",
988                                 port_id, (unsigned) vlan_id);
989                 return (-EINVAL);
990         }
991         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_filter_set, -ENOTSUP);
992         (*dev->dev_ops->vlan_filter_set)(dev, vlan_id, on);
993         return (0);
994 }
995
996 int
997 rte_eth_dev_fdir_add_signature_filter(uint8_t port_id,
998                                       struct rte_fdir_filter *fdir_filter,
999                                       uint8_t queue)
1000 {
1001         struct rte_eth_dev *dev;
1002
1003         if (port_id >= nb_ports) {
1004                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1005                 return (-ENODEV);
1006         }
1007
1008         dev = &rte_eth_devices[port_id];
1009
1010         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
1011                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1012                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1013                 return (-ENOSYS);
1014         }
1015
1016         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1017              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1018             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1019                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1020                                 "None l4type, source & destinations ports " \
1021                                 "should be null!\n");
1022                 return (-EINVAL);
1023         }
1024
1025         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_add_signature_filter, -ENOTSUP);
1026         return (*dev->dev_ops->fdir_add_signature_filter)(dev, fdir_filter,
1027                                                                 queue);
1028 }
1029
1030 int
1031 rte_eth_dev_fdir_update_signature_filter(uint8_t port_id,
1032                                          struct rte_fdir_filter *fdir_filter,
1033                                          uint8_t queue)
1034 {
1035         struct rte_eth_dev *dev;
1036
1037         if (port_id >= nb_ports) {
1038                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1039                 return (-ENODEV);
1040         }
1041
1042         dev = &rte_eth_devices[port_id];
1043
1044         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
1045                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1046                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1047                 return (-ENOSYS);
1048         }
1049
1050         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1051              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1052             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1053                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1054                                 "None l4type, source & destinations ports " \
1055                                 "should be null!\n");
1056                 return (-EINVAL);
1057         }
1058
1059         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_update_signature_filter, -ENOTSUP);
1060         return (*dev->dev_ops->fdir_update_signature_filter)(dev, fdir_filter,
1061                                                                 queue);
1062
1063 }
1064
1065 int
1066 rte_eth_dev_fdir_remove_signature_filter(uint8_t port_id,
1067                                          struct rte_fdir_filter *fdir_filter)
1068 {
1069         struct rte_eth_dev *dev;
1070
1071         if (port_id >= nb_ports) {
1072                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1073                 return (-ENODEV);
1074         }
1075
1076         dev = &rte_eth_devices[port_id];
1077
1078         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
1079                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1080                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1081                 return (-ENOSYS);
1082         }
1083
1084         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1085              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1086             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1087                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1088                                 "None l4type source & destinations ports " \
1089                                 "should be null!\n");
1090                 return (-EINVAL);
1091         }
1092
1093         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_remove_signature_filter, -ENOTSUP);
1094         return (*dev->dev_ops->fdir_remove_signature_filter)(dev, fdir_filter);
1095 }
1096
1097 int
1098 rte_eth_dev_fdir_get_infos(uint8_t port_id, struct rte_eth_fdir *fdir)
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         dev = &rte_eth_devices[port_id];
1108         if (! (dev->data->dev_conf.fdir_conf.mode)) {
1109                 PMD_DEBUG_TRACE("port %d: pkt-filter disabled\n", port_id);
1110                 return (-ENOSYS);
1111         }
1112
1113         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_infos_get, -ENOTSUP);
1114
1115         (*dev->dev_ops->fdir_infos_get)(dev, fdir);
1116         return (0);
1117 }
1118
1119 int
1120 rte_eth_dev_fdir_add_perfect_filter(uint8_t port_id,
1121                                     struct rte_fdir_filter *fdir_filter,
1122                                     uint16_t soft_id, uint8_t queue,
1123                                     uint8_t drop)
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         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
1135                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1136                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1137                 return (-ENOSYS);
1138         }
1139
1140         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1141              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1142             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1143                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1144                                 "None l4type, source & destinations ports " \
1145                                 "should be null!\n");
1146                 return (-EINVAL);
1147         }
1148
1149         /* For now IPv6 is not supported with perfect filter */
1150         if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
1151                 return (-ENOTSUP);
1152
1153         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_add_perfect_filter, -ENOTSUP);
1154         return (*dev->dev_ops->fdir_add_perfect_filter)(dev, fdir_filter,
1155                                                                 soft_id, queue,
1156                                                                 drop);
1157 }
1158
1159 int
1160 rte_eth_dev_fdir_update_perfect_filter(uint8_t port_id,
1161                                        struct rte_fdir_filter *fdir_filter,
1162                                        uint16_t soft_id, uint8_t queue,
1163                                        uint8_t drop)
1164 {
1165         struct rte_eth_dev *dev;
1166
1167         if (port_id >= nb_ports) {
1168                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1169                 return (-ENODEV);
1170         }
1171
1172         dev = &rte_eth_devices[port_id];
1173
1174         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
1175                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1176                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1177                 return (-ENOSYS);
1178         }
1179
1180         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1181              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1182             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1183                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1184                                 "None l4type, source & destinations ports " \
1185                                 "should be null!\n");
1186                 return (-EINVAL);
1187         }
1188
1189         /* For now IPv6 is not supported with perfect filter */
1190         if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
1191                 return (-ENOTSUP);
1192
1193         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_update_perfect_filter, -ENOTSUP);
1194         return (*dev->dev_ops->fdir_update_perfect_filter)(dev, fdir_filter,
1195                                                         soft_id, queue, drop);
1196 }
1197
1198 int
1199 rte_eth_dev_fdir_remove_perfect_filter(uint8_t port_id,
1200                                        struct rte_fdir_filter *fdir_filter,
1201                                        uint16_t soft_id)
1202 {
1203         struct rte_eth_dev *dev;
1204
1205         if (port_id >= nb_ports) {
1206                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1207                 return (-ENODEV);
1208         }
1209
1210         dev = &rte_eth_devices[port_id];
1211
1212         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
1213                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1214                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1215                 return (-ENOSYS);
1216         }
1217
1218         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1219              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1220             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1221                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1222                                 "None l4type, source & destinations ports " \
1223                                 "should be null!\n");
1224                 return (-EINVAL);
1225         }
1226
1227         /* For now IPv6 is not supported with perfect filter */
1228         if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
1229                 return (-ENOTSUP);
1230
1231         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_remove_perfect_filter, -ENOTSUP);
1232         return (*dev->dev_ops->fdir_remove_perfect_filter)(dev, fdir_filter,
1233                                                                 soft_id);
1234 }
1235
1236 int
1237 rte_eth_dev_fdir_set_masks(uint8_t port_id, struct rte_fdir_masks *fdir_mask)
1238 {
1239         struct rte_eth_dev *dev;
1240
1241         if (port_id >= nb_ports) {
1242                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1243                 return (-ENODEV);
1244         }
1245
1246         dev = &rte_eth_devices[port_id];
1247         if (! (dev->data->dev_conf.fdir_conf.mode)) {
1248                 PMD_DEBUG_TRACE("port %d: pkt-filter disabled\n", port_id);
1249                 return (-ENOSYS);
1250         }
1251
1252         /* IPv6 mask are not supported */
1253         if (fdir_mask->src_ipv6_mask)
1254                 return (-ENOTSUP);
1255
1256         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_set_masks, -ENOTSUP);
1257         return (*dev->dev_ops->fdir_set_masks)(dev, fdir_mask);
1258 }
1259
1260 int
1261 rte_eth_dev_flow_ctrl_set(uint8_t port_id, struct rte_eth_fc_conf *fc_conf)
1262 {
1263         struct rte_eth_dev *dev;
1264
1265         if (port_id >= nb_ports) {
1266                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1267                 return (-ENODEV);
1268         }
1269
1270         if ((fc_conf->send_xon != 0) && (fc_conf->send_xon != 1)) {
1271                 PMD_DEBUG_TRACE("Invalid send_xon, only 0/1 allowed\n");
1272                 return (-EINVAL);
1273         }
1274
1275         dev = &rte_eth_devices[port_id];
1276
1277         /* High water, low water validation are device specific */
1278         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_set, -ENOTSUP);
1279         if  (*dev->dev_ops->flow_ctrl_set)
1280                 return (*dev->dev_ops->flow_ctrl_set)(dev, fc_conf);
1281
1282         return -ENOTSUP;
1283 }
1284
1285 int
1286 rte_eth_led_on(uint8_t port_id)
1287 {
1288         struct rte_eth_dev *dev;
1289
1290         if (port_id >= nb_ports) {
1291                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1292                 return (-ENODEV);
1293         }
1294
1295         dev = &rte_eth_devices[port_id];
1296         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_on, -ENOTSUP);
1297         return ((*dev->dev_ops->dev_led_on)(dev));
1298 }
1299
1300 int
1301 rte_eth_led_off(uint8_t port_id)
1302 {
1303         struct rte_eth_dev *dev;
1304
1305         if (port_id >= nb_ports) {
1306                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1307                 return (-ENODEV);
1308         }
1309
1310         dev = &rte_eth_devices[port_id];
1311         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_off, -ENOTSUP);
1312         return ((*dev->dev_ops->dev_led_off)(dev));
1313 }
1314
1315 /*
1316  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
1317  * an empty spot.
1318  */
1319 static inline int
1320 get_mac_addr_index(uint8_t port_id, struct ether_addr *addr)
1321 {
1322         struct rte_eth_dev_info dev_info;
1323         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1324         unsigned i;
1325
1326         rte_eth_dev_info_get(port_id, &dev_info);
1327
1328         for (i = 0; i < dev_info.max_mac_addrs; i++)
1329                 if (memcmp(addr, &dev->data->mac_addrs[i], ETHER_ADDR_LEN) == 0)
1330                         return i;
1331
1332         return -1;
1333 }
1334
1335 static struct ether_addr null_mac_addr = {{0, 0, 0, 0, 0, 0}};
1336
1337 int
1338 rte_eth_dev_mac_addr_add(uint8_t port_id, struct ether_addr *addr,
1339                 uint32_t pool)
1340 {
1341         struct rte_eth_dev *dev;
1342         int index;
1343
1344         if (port_id >= nb_ports) {
1345                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1346                 return (-ENODEV);
1347         }
1348         dev = &rte_eth_devices[port_id];
1349         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_add, -ENOTSUP);
1350
1351         if (is_zero_ether_addr(addr)) {
1352                 PMD_DEBUG_TRACE("port %d: Cannot add NULL MAC address\n", port_id);
1353                 return (-EINVAL);
1354         }
1355
1356         /* Check if it's already there, and do nothing */
1357         index = get_mac_addr_index(port_id, addr);
1358         if (index >= 0)
1359                 return 0;
1360
1361         index = get_mac_addr_index(port_id, &null_mac_addr);
1362         if (index < 0) {
1363                 PMD_DEBUG_TRACE("port %d: MAC address array full\n", port_id);
1364                 return (-ENOSPC);
1365         }
1366
1367         /* Update NIC */
1368         (*dev->dev_ops->mac_addr_add)(dev, addr, index, pool);
1369
1370         /* Update address in NIC data structure */
1371         ether_addr_copy(addr, &dev->data->mac_addrs[index]);
1372
1373         return 0;
1374 }
1375
1376 int
1377 rte_eth_dev_mac_addr_remove(uint8_t port_id, struct ether_addr *addr)
1378 {
1379         struct rte_eth_dev *dev;
1380         int index;
1381
1382         if (port_id >= nb_ports) {
1383                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1384                 return (-ENODEV);
1385         }
1386         dev = &rte_eth_devices[port_id];
1387         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_remove, -ENOTSUP);
1388
1389         index = get_mac_addr_index(port_id, addr);
1390         if (index == 0) {
1391                 PMD_DEBUG_TRACE("port %d: Cannot remove default MAC address\n", port_id);
1392                 return (-EADDRINUSE);
1393         } else if (index < 0)
1394                 return 0;  /* Do nothing if address wasn't found */
1395
1396         /* Update NIC */
1397         (*dev->dev_ops->mac_addr_remove)(dev, index);
1398
1399         /* Update address in NIC data structure */
1400         ether_addr_copy(&null_mac_addr, &dev->data->mac_addrs[index]);
1401
1402         return 0;
1403 }
1404
1405 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
1406 uint16_t
1407 rte_eth_rx_burst(uint8_t port_id, uint16_t queue_id,
1408                  struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1409 {
1410         struct rte_eth_dev *dev;
1411
1412         if (port_id >= nb_ports) {
1413                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1414                 return 0;
1415         }
1416         dev = &rte_eth_devices[port_id];
1417         FUNC_PTR_OR_ERR_RET(*dev->rx_pkt_burst, -ENOTSUP);
1418         if (queue_id >= dev->data->nb_rx_queues) {
1419                 PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", queue_id);
1420                 return 0;
1421         }
1422         return (*dev->rx_pkt_burst)(dev->data->rx_queues[queue_id],
1423                                                 rx_pkts, nb_pkts);
1424 }
1425
1426 uint16_t
1427 rte_eth_tx_burst(uint8_t port_id, uint16_t queue_id,
1428                  struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1429 {
1430         struct rte_eth_dev *dev;
1431
1432         if (port_id >= nb_ports) {
1433                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1434                 return 0;
1435         }
1436         dev = &rte_eth_devices[port_id];
1437
1438         FUNC_PTR_OR_ERR_RET(*dev->tx_pkt_burst, -ENOTSUP);
1439         if (queue_id >= dev->data->nb_tx_queues) {
1440                 PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", queue_id);
1441                 return 0;
1442         }
1443         return (*dev->tx_pkt_burst)(dev->data->tx_queues[queue_id],
1444                                                 tx_pkts, nb_pkts);
1445 }
1446 #endif
1447
1448 int
1449 rte_eth_dev_callback_register(uint8_t port_id,
1450                         enum rte_eth_event_type event,
1451                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
1452 {
1453         int ret = -1;
1454         struct rte_eth_dev *dev;
1455         struct rte_eth_dev_callback *user_cb = NULL;
1456
1457         if (!cb_fn)
1458                 return -1;
1459         if (port_id >= nb_ports) {
1460                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1461                 return -1;
1462         }
1463         dev = &rte_eth_devices[port_id];
1464         rte_spinlock_lock(&rte_eth_dev_cb_lock);
1465         TAILQ_FOREACH(user_cb, &(dev->callbacks), next) {
1466                 if (user_cb->cb_fn == cb_fn &&
1467                         user_cb->cb_arg == cb_arg &&
1468                         user_cb->event == event) {
1469                         ret = 0;
1470                         goto out;
1471                 }
1472         }
1473         user_cb = rte_malloc("INTR_USER_CALLBACK",
1474                 sizeof(struct rte_eth_dev_callback), 0);
1475         if (!user_cb)
1476                 goto out;
1477         user_cb->cb_fn = cb_fn;
1478         user_cb->cb_arg = cb_arg;
1479         user_cb->event = event;
1480         TAILQ_INSERT_TAIL(&(dev->callbacks), user_cb, next);
1481         ret = 0;
1482
1483 out:
1484         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1485
1486         return ret;
1487 }
1488
1489 int
1490 rte_eth_dev_callback_unregister(uint8_t port_id,
1491                         enum rte_eth_event_type event,
1492                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
1493 {
1494         int ret = -1;
1495         struct rte_eth_dev *dev;
1496         struct rte_eth_dev_callback *cb_lst = NULL;
1497
1498         if (!cb_fn)
1499                 return -1;
1500         if (port_id >= nb_ports) {
1501                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1502                 return -1;
1503         }
1504         dev = &rte_eth_devices[port_id];
1505         rte_spinlock_lock(&rte_eth_dev_cb_lock);
1506         TAILQ_FOREACH(cb_lst, &(dev->callbacks), next) {
1507                 if (cb_lst->cb_fn != cb_fn || cb_lst->event != event)
1508                         continue;
1509                 if (cb_lst->cb_arg == (void *)-1 ||
1510                                 cb_lst->cb_arg == cb_arg) {
1511                         TAILQ_REMOVE(&(dev->callbacks), cb_lst, next);
1512                         rte_free(cb_lst);
1513                         ret = 0;
1514                 }
1515         }
1516
1517         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1518
1519         return ret;
1520 }
1521
1522 void
1523 _rte_eth_dev_callback_process(struct rte_eth_dev *dev, enum rte_eth_event_type event)
1524 {
1525         struct rte_eth_dev_callback *cb_lst = NULL;
1526         struct rte_eth_dev_callback dev_cb;
1527
1528         rte_spinlock_lock(&rte_eth_dev_cb_lock);
1529         TAILQ_FOREACH(cb_lst, &(dev->callbacks), next) {
1530                 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
1531                         continue;
1532                 dev_cb = *cb_lst;
1533                 rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1534                 dev_cb.cb_fn(dev->data->port_id, dev_cb.event,
1535                                                 dev_cb.cb_arg);
1536                 rte_spinlock_lock(&rte_eth_dev_cb_lock);
1537         }
1538         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1539 }
1540