update copyright date to 2013
[dpdk.git] / lib / librte_ether / rte_ethdev.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright 
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright 
14  *       notice, this list of conditions and the following disclaimer in 
15  *       the documentation and/or other materials provided with the 
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its 
18  *       contributors may be used to endorse or promote products derived 
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  * 
33  */
34
35 #include <sys/types.h>
36 #include <sys/queue.h>
37 #include <ctype.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <stdarg.h>
42 #include <errno.h>
43 #include <stdint.h>
44 #include <inttypes.h>
45
46 #include <rte_byteorder.h>
47 #include <rte_log.h>
48 #include <rte_debug.h>
49 #include <rte_interrupts.h>
50 #include <rte_pci.h>
51 #include <rte_memory.h>
52 #include <rte_memcpy.h>
53 #include <rte_memzone.h>
54 #include <rte_launch.h>
55 #include <rte_tailq.h>
56 #include <rte_eal.h>
57 #include <rte_per_lcore.h>
58 #include <rte_lcore.h>
59 #include <rte_atomic.h>
60 #include <rte_branch_prediction.h>
61 #include <rte_common.h>
62 #include <rte_ring.h>
63 #include <rte_mempool.h>
64 #include <rte_malloc.h>
65 #include <rte_mbuf.h>
66 #include <rte_errno.h>
67 #include <rte_spinlock.h>
68
69 #include "rte_ether.h"
70 #include "rte_ethdev.h"
71
72 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
73 #define PMD_DEBUG_TRACE(fmt, args...) do {                        \
74                 RTE_LOG(ERR, PMD, "%s: " fmt, __func__, ## args); \
75         } while (0)
76 #else
77 #define PMD_DEBUG_TRACE(fmt, args...)
78 #endif
79
80 /* Macros for checking for restricting functions to primary instance only */
81 #define PROC_PRIMARY_OR_ERR_RET(retval) do { \
82         if (rte_eal_process_type() != RTE_PROC_PRIMARY) { \
83                 PMD_DEBUG_TRACE("Cannot run in secondary processes\n"); \
84                 return (retval); \
85         } \
86 } while(0)
87 #define PROC_PRIMARY_OR_RET() do { \
88         if (rte_eal_process_type() != RTE_PROC_PRIMARY) { \
89                 PMD_DEBUG_TRACE("Cannot run in secondary processes\n"); \
90                 return; \
91         } \
92 } while(0)
93
94 /* Macros to check for invlaid function pointers in dev_ops structure */
95 #define FUNC_PTR_OR_ERR_RET(func, retval) do { \
96         if ((func) == NULL) { \
97                 PMD_DEBUG_TRACE("Function not supported\n"); \
98                 return (retval); \
99         } \
100 } while(0)
101 #define FUNC_PTR_OR_RET(func) do { \
102         if ((func) == NULL) { \
103                 PMD_DEBUG_TRACE("Function not supported\n"); \
104                 return; \
105         } \
106 } while(0)
107
108 static const char *MZ_RTE_ETH_DEV_DATA = "rte_eth_dev_data";
109 struct rte_eth_dev rte_eth_devices[RTE_MAX_ETHPORTS];
110 static struct rte_eth_dev_data *rte_eth_dev_data = NULL;
111 static uint8_t nb_ports = 0;
112
113 /* spinlock for eth device callbacks */
114 static rte_spinlock_t rte_eth_dev_cb_lock = RTE_SPINLOCK_INITIALIZER;
115
116 /**
117  * The user application callback description.
118  *
119  * It contains callback address to be registered by user application,
120  * the pointer to the parameters for callback, and the event type.
121  */
122 struct rte_eth_dev_callback {
123         TAILQ_ENTRY(rte_eth_dev_callback) next; /**< Callbacks list */
124         rte_eth_dev_cb_fn cb_fn;                /**< Callback address */
125         void *cb_arg;                           /**< Parameter for callback */
126         enum rte_eth_event_type event;          /**< Interrupt event type */
127 };
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_set_vlan_strip_on_queue(uint8_t port_id, uint16_t rx_queue_id, int on)
998 {
999         struct rte_eth_dev *dev;
1000
1001         if (port_id >= nb_ports) {
1002                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1003                 return (-ENODEV);
1004         }
1005
1006         dev = &rte_eth_devices[port_id];
1007         if (rx_queue_id >= dev->data->nb_rx_queues) {
1008                 PMD_DEBUG_TRACE("Invalid rx_queue_id=%d\n", port_id);
1009                 return (-EINVAL);
1010         }
1011
1012         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_strip_queue_set, -ENOTSUP);
1013         (*dev->dev_ops->vlan_strip_queue_set)(dev, rx_queue_id, on);
1014
1015         return (0);
1016 }
1017
1018 int
1019 rte_eth_dev_set_vlan_ether_type(uint8_t port_id, uint16_t tpid)
1020 {
1021         struct rte_eth_dev *dev;
1022
1023         if (port_id >= nb_ports) {
1024                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1025                 return (-ENODEV);
1026         }
1027
1028         dev = &rte_eth_devices[port_id];
1029         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_tpid_set, -ENOTSUP);
1030         (*dev->dev_ops->vlan_tpid_set)(dev, tpid);
1031
1032         return (0);
1033 }
1034
1035 int
1036 rte_eth_dev_set_vlan_offload(uint8_t port_id, int offload_mask)
1037 {
1038         struct rte_eth_dev *dev;
1039         int ret = 0;
1040         int mask = 0;
1041         int cur, org = 0;
1042         
1043         if (port_id >= nb_ports) {
1044                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1045                 return (-ENODEV);
1046         }
1047
1048         dev = &rte_eth_devices[port_id];
1049
1050         /*check which option changed by application*/
1051         cur = !!(offload_mask & ETH_VLAN_STRIP_OFFLOAD);
1052         org = !!(dev->data->dev_conf.rxmode.hw_vlan_strip);
1053         if (cur != org){
1054                 dev->data->dev_conf.rxmode.hw_vlan_strip = (uint8_t)cur;
1055                 mask |= ETH_VLAN_STRIP_MASK;
1056         }
1057         
1058         cur = !!(offload_mask & ETH_VLAN_FILTER_OFFLOAD);
1059         org = !!(dev->data->dev_conf.rxmode.hw_vlan_filter);
1060         if (cur != org){
1061                 dev->data->dev_conf.rxmode.hw_vlan_filter = (uint8_t)cur;
1062                 mask |= ETH_VLAN_FILTER_MASK;
1063         }
1064
1065         cur = !!(offload_mask & ETH_VLAN_EXTEND_OFFLOAD);
1066         org = !!(dev->data->dev_conf.rxmode.hw_vlan_extend);
1067         if (cur != org){
1068                 dev->data->dev_conf.rxmode.hw_vlan_extend = (uint8_t)cur;
1069                 mask |= ETH_VLAN_EXTEND_MASK;
1070         }
1071
1072         /*no change*/
1073         if(mask == 0)
1074                 return ret;
1075         
1076         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->vlan_offload_set, -ENOTSUP);
1077         (*dev->dev_ops->vlan_offload_set)(dev, mask);
1078
1079         return ret;
1080 }
1081
1082 int
1083 rte_eth_dev_get_vlan_offload(uint8_t port_id)
1084 {
1085         struct rte_eth_dev *dev;
1086         int ret = 0;
1087
1088         if (port_id >= nb_ports) {
1089                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1090                 return (-ENODEV);
1091         }
1092
1093         dev = &rte_eth_devices[port_id];
1094
1095         if (dev->data->dev_conf.rxmode.hw_vlan_strip)
1096                 ret |= ETH_VLAN_STRIP_OFFLOAD ;
1097
1098         if (dev->data->dev_conf.rxmode.hw_vlan_filter)
1099                 ret |= ETH_VLAN_FILTER_OFFLOAD ;
1100
1101         if (dev->data->dev_conf.rxmode.hw_vlan_extend)
1102                 ret |= ETH_VLAN_EXTEND_OFFLOAD ;
1103
1104         return ret;
1105 }
1106
1107
1108 int
1109 rte_eth_dev_fdir_add_signature_filter(uint8_t port_id,
1110                                       struct rte_fdir_filter *fdir_filter,
1111                                       uint8_t queue)
1112 {
1113         struct rte_eth_dev *dev;
1114
1115         if (port_id >= nb_ports) {
1116                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1117                 return (-ENODEV);
1118         }
1119
1120         dev = &rte_eth_devices[port_id];
1121
1122         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
1123                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1124                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1125                 return (-ENOSYS);
1126         }
1127
1128         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1129              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1130             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1131                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1132                                 "None l4type, source & destinations ports " \
1133                                 "should be null!\n");
1134                 return (-EINVAL);
1135         }
1136
1137         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_add_signature_filter, -ENOTSUP);
1138         return (*dev->dev_ops->fdir_add_signature_filter)(dev, fdir_filter,
1139                                                                 queue);
1140 }
1141
1142 int
1143 rte_eth_dev_fdir_update_signature_filter(uint8_t port_id,
1144                                          struct rte_fdir_filter *fdir_filter,
1145                                          uint8_t queue)
1146 {
1147         struct rte_eth_dev *dev;
1148
1149         if (port_id >= nb_ports) {
1150                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1151                 return (-ENODEV);
1152         }
1153
1154         dev = &rte_eth_devices[port_id];
1155
1156         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
1157                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1158                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1159                 return (-ENOSYS);
1160         }
1161
1162         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1163              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1164             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1165                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1166                                 "None l4type, source & destinations ports " \
1167                                 "should be null!\n");
1168                 return (-EINVAL);
1169         }
1170
1171         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_update_signature_filter, -ENOTSUP);
1172         return (*dev->dev_ops->fdir_update_signature_filter)(dev, fdir_filter,
1173                                                                 queue);
1174
1175 }
1176
1177 int
1178 rte_eth_dev_fdir_remove_signature_filter(uint8_t port_id,
1179                                          struct rte_fdir_filter *fdir_filter)
1180 {
1181         struct rte_eth_dev *dev;
1182
1183         if (port_id >= nb_ports) {
1184                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1185                 return (-ENODEV);
1186         }
1187
1188         dev = &rte_eth_devices[port_id];
1189
1190         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_SIGNATURE) {
1191                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1192                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1193                 return (-ENOSYS);
1194         }
1195
1196         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1197              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1198             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1199                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1200                                 "None l4type source & destinations ports " \
1201                                 "should be null!\n");
1202                 return (-EINVAL);
1203         }
1204
1205         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_remove_signature_filter, -ENOTSUP);
1206         return (*dev->dev_ops->fdir_remove_signature_filter)(dev, fdir_filter);
1207 }
1208
1209 int
1210 rte_eth_dev_fdir_get_infos(uint8_t port_id, struct rte_eth_fdir *fdir)
1211 {
1212         struct rte_eth_dev *dev;
1213
1214         if (port_id >= nb_ports) {
1215                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1216                 return (-ENODEV);
1217         }
1218
1219         dev = &rte_eth_devices[port_id];
1220         if (! (dev->data->dev_conf.fdir_conf.mode)) {
1221                 PMD_DEBUG_TRACE("port %d: pkt-filter disabled\n", port_id);
1222                 return (-ENOSYS);
1223         }
1224
1225         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_infos_get, -ENOTSUP);
1226
1227         (*dev->dev_ops->fdir_infos_get)(dev, fdir);
1228         return (0);
1229 }
1230
1231 int
1232 rte_eth_dev_fdir_add_perfect_filter(uint8_t port_id,
1233                                     struct rte_fdir_filter *fdir_filter,
1234                                     uint16_t soft_id, uint8_t queue,
1235                                     uint8_t drop)
1236 {
1237         struct rte_eth_dev *dev;
1238
1239         if (port_id >= nb_ports) {
1240                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1241                 return (-ENODEV);
1242         }
1243
1244         dev = &rte_eth_devices[port_id];
1245
1246         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
1247                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1248                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1249                 return (-ENOSYS);
1250         }
1251
1252         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1253              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1254             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1255                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1256                                 "None l4type, source & destinations ports " \
1257                                 "should be null!\n");
1258                 return (-EINVAL);
1259         }
1260
1261         /* For now IPv6 is not supported with perfect filter */
1262         if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
1263                 return (-ENOTSUP);
1264
1265         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_add_perfect_filter, -ENOTSUP);
1266         return (*dev->dev_ops->fdir_add_perfect_filter)(dev, fdir_filter,
1267                                                                 soft_id, queue,
1268                                                                 drop);
1269 }
1270
1271 int
1272 rte_eth_dev_fdir_update_perfect_filter(uint8_t port_id,
1273                                        struct rte_fdir_filter *fdir_filter,
1274                                        uint16_t soft_id, uint8_t queue,
1275                                        uint8_t drop)
1276 {
1277         struct rte_eth_dev *dev;
1278
1279         if (port_id >= nb_ports) {
1280                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1281                 return (-ENODEV);
1282         }
1283
1284         dev = &rte_eth_devices[port_id];
1285
1286         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
1287                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1288                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1289                 return (-ENOSYS);
1290         }
1291
1292         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1293              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1294             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1295                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1296                                 "None l4type, source & destinations ports " \
1297                                 "should be null!\n");
1298                 return (-EINVAL);
1299         }
1300
1301         /* For now IPv6 is not supported with perfect filter */
1302         if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
1303                 return (-ENOTSUP);
1304
1305         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_update_perfect_filter, -ENOTSUP);
1306         return (*dev->dev_ops->fdir_update_perfect_filter)(dev, fdir_filter,
1307                                                         soft_id, queue, drop);
1308 }
1309
1310 int
1311 rte_eth_dev_fdir_remove_perfect_filter(uint8_t port_id,
1312                                        struct rte_fdir_filter *fdir_filter,
1313                                        uint16_t soft_id)
1314 {
1315         struct rte_eth_dev *dev;
1316
1317         if (port_id >= nb_ports) {
1318                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1319                 return (-ENODEV);
1320         }
1321
1322         dev = &rte_eth_devices[port_id];
1323
1324         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_PERFECT) {
1325                 PMD_DEBUG_TRACE("port %d: invalid FDIR mode=%u\n",
1326                                 port_id, dev->data->dev_conf.fdir_conf.mode);
1327                 return (-ENOSYS);
1328         }
1329
1330         if ((fdir_filter->l4type == RTE_FDIR_L4TYPE_SCTP
1331              || fdir_filter->l4type == RTE_FDIR_L4TYPE_NONE)
1332             && (fdir_filter->port_src || fdir_filter->port_dst)) {
1333                 PMD_DEBUG_TRACE(" Port are meaningless for SCTP and " \
1334                                 "None l4type, source & destinations ports " \
1335                                 "should be null!\n");
1336                 return (-EINVAL);
1337         }
1338
1339         /* For now IPv6 is not supported with perfect filter */
1340         if (fdir_filter->iptype == RTE_FDIR_IPTYPE_IPV6)
1341                 return (-ENOTSUP);
1342
1343         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_remove_perfect_filter, -ENOTSUP);
1344         return (*dev->dev_ops->fdir_remove_perfect_filter)(dev, fdir_filter,
1345                                                                 soft_id);
1346 }
1347
1348 int
1349 rte_eth_dev_fdir_set_masks(uint8_t port_id, struct rte_fdir_masks *fdir_mask)
1350 {
1351         struct rte_eth_dev *dev;
1352
1353         if (port_id >= nb_ports) {
1354                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1355                 return (-ENODEV);
1356         }
1357
1358         dev = &rte_eth_devices[port_id];
1359         if (! (dev->data->dev_conf.fdir_conf.mode)) {
1360                 PMD_DEBUG_TRACE("port %d: pkt-filter disabled\n", port_id);
1361                 return (-ENOSYS);
1362         }
1363
1364         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->fdir_set_masks, -ENOTSUP);
1365         return (*dev->dev_ops->fdir_set_masks)(dev, fdir_mask);
1366 }
1367
1368 int
1369 rte_eth_dev_flow_ctrl_set(uint8_t port_id, struct rte_eth_fc_conf *fc_conf)
1370 {
1371         struct rte_eth_dev *dev;
1372
1373         if (port_id >= nb_ports) {
1374                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1375                 return (-ENODEV);
1376         }
1377
1378         if ((fc_conf->send_xon != 0) && (fc_conf->send_xon != 1)) {
1379                 PMD_DEBUG_TRACE("Invalid send_xon, only 0/1 allowed\n");
1380                 return (-EINVAL);
1381         }
1382
1383         dev = &rte_eth_devices[port_id];
1384         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->flow_ctrl_set, -ENOTSUP);
1385         return (*dev->dev_ops->flow_ctrl_set)(dev, fc_conf);
1386 }
1387
1388 int
1389 rte_eth_dev_priority_flow_ctrl_set(uint8_t port_id, struct rte_eth_pfc_conf *pfc_conf)
1390 {
1391         struct rte_eth_dev *dev;
1392
1393         if (port_id >= nb_ports) {
1394                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1395                 return (-ENODEV);
1396         }
1397
1398         if (pfc_conf->priority > (ETH_DCB_NUM_USER_PRIORITIES - 1)) {
1399                 PMD_DEBUG_TRACE("Invalid priority, only 0-7 allowed\n");
1400                 return (-EINVAL);
1401         }
1402
1403         dev = &rte_eth_devices[port_id];
1404         /* High water, low water validation are device specific */
1405         if  (*dev->dev_ops->priority_flow_ctrl_set)
1406                 return (*dev->dev_ops->priority_flow_ctrl_set)(dev, pfc_conf);
1407         return (-ENOTSUP);
1408 }
1409
1410 int
1411 rte_eth_led_on(uint8_t port_id)
1412 {
1413         struct rte_eth_dev *dev;
1414
1415         if (port_id >= nb_ports) {
1416                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1417                 return (-ENODEV);
1418         }
1419
1420         dev = &rte_eth_devices[port_id];
1421         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_on, -ENOTSUP);
1422         return ((*dev->dev_ops->dev_led_on)(dev));
1423 }
1424
1425 int
1426 rte_eth_led_off(uint8_t port_id)
1427 {
1428         struct rte_eth_dev *dev;
1429
1430         if (port_id >= nb_ports) {
1431                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1432                 return (-ENODEV);
1433         }
1434
1435         dev = &rte_eth_devices[port_id];
1436         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_led_off, -ENOTSUP);
1437         return ((*dev->dev_ops->dev_led_off)(dev));
1438 }
1439
1440 /*
1441  * Returns index into MAC address array of addr. Use 00:00:00:00:00:00 to find
1442  * an empty spot.
1443  */
1444 static inline int
1445 get_mac_addr_index(uint8_t port_id, struct ether_addr *addr)
1446 {
1447         struct rte_eth_dev_info dev_info;
1448         struct rte_eth_dev *dev = &rte_eth_devices[port_id];
1449         unsigned i;
1450
1451         rte_eth_dev_info_get(port_id, &dev_info);
1452
1453         for (i = 0; i < dev_info.max_mac_addrs; i++)
1454                 if (memcmp(addr, &dev->data->mac_addrs[i], ETHER_ADDR_LEN) == 0)
1455                         return i;
1456
1457         return -1;
1458 }
1459
1460 static struct ether_addr null_mac_addr = {{0, 0, 0, 0, 0, 0}};
1461
1462 int
1463 rte_eth_dev_mac_addr_add(uint8_t port_id, struct ether_addr *addr,
1464                 uint32_t pool)
1465 {
1466         struct rte_eth_dev *dev;
1467         int index;
1468
1469         if (port_id >= nb_ports) {
1470                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1471                 return (-ENODEV);
1472         }
1473         dev = &rte_eth_devices[port_id];
1474         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_add, -ENOTSUP);
1475
1476         if (is_zero_ether_addr(addr)) {
1477                 PMD_DEBUG_TRACE("port %d: Cannot add NULL MAC address\n", port_id);
1478                 return (-EINVAL);
1479         }
1480
1481         /* Check if it's already there, and do nothing */
1482         index = get_mac_addr_index(port_id, addr);
1483         if (index >= 0)
1484                 return 0;
1485
1486         index = get_mac_addr_index(port_id, &null_mac_addr);
1487         if (index < 0) {
1488                 PMD_DEBUG_TRACE("port %d: MAC address array full\n", port_id);
1489                 return (-ENOSPC);
1490         }
1491
1492         /* Update NIC */
1493         (*dev->dev_ops->mac_addr_add)(dev, addr, index, pool);
1494
1495         /* Update address in NIC data structure */
1496         ether_addr_copy(addr, &dev->data->mac_addrs[index]);
1497
1498         return 0;
1499 }
1500
1501 int
1502 rte_eth_dev_mac_addr_remove(uint8_t port_id, struct ether_addr *addr)
1503 {
1504         struct rte_eth_dev *dev;
1505         int index;
1506
1507         if (port_id >= nb_ports) {
1508                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1509                 return (-ENODEV);
1510         }
1511         dev = &rte_eth_devices[port_id];
1512         FUNC_PTR_OR_ERR_RET(*dev->dev_ops->mac_addr_remove, -ENOTSUP);
1513
1514         index = get_mac_addr_index(port_id, addr);
1515         if (index == 0) {
1516                 PMD_DEBUG_TRACE("port %d: Cannot remove default MAC address\n", port_id);
1517                 return (-EADDRINUSE);
1518         } else if (index < 0)
1519                 return 0;  /* Do nothing if address wasn't found */
1520
1521         /* Update NIC */
1522         (*dev->dev_ops->mac_addr_remove)(dev, index);
1523
1524         /* Update address in NIC data structure */
1525         ether_addr_copy(&null_mac_addr, &dev->data->mac_addrs[index]);
1526
1527         return 0;
1528 }
1529
1530 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
1531 uint16_t
1532 rte_eth_rx_burst(uint8_t port_id, uint16_t queue_id,
1533                  struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1534 {
1535         struct rte_eth_dev *dev;
1536
1537         if (port_id >= nb_ports) {
1538                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1539                 return 0;
1540         }
1541         dev = &rte_eth_devices[port_id];
1542         FUNC_PTR_OR_ERR_RET(*dev->rx_pkt_burst, -ENOTSUP);
1543         if (queue_id >= dev->data->nb_rx_queues) {
1544                 PMD_DEBUG_TRACE("Invalid RX queue_id=%d\n", queue_id);
1545                 return 0;
1546         }
1547         return (*dev->rx_pkt_burst)(dev->data->rx_queues[queue_id],
1548                                                 rx_pkts, nb_pkts);
1549 }
1550
1551 uint16_t
1552 rte_eth_tx_burst(uint8_t port_id, uint16_t queue_id,
1553                  struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1554 {
1555         struct rte_eth_dev *dev;
1556
1557         if (port_id >= nb_ports) {
1558                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1559                 return 0;
1560         }
1561         dev = &rte_eth_devices[port_id];
1562
1563         FUNC_PTR_OR_ERR_RET(*dev->tx_pkt_burst, -ENOTSUP);
1564         if (queue_id >= dev->data->nb_tx_queues) {
1565                 PMD_DEBUG_TRACE("Invalid TX queue_id=%d\n", queue_id);
1566                 return 0;
1567         }
1568         return (*dev->tx_pkt_burst)(dev->data->tx_queues[queue_id],
1569                                                 tx_pkts, nb_pkts);
1570 }
1571 #endif
1572
1573 int
1574 rte_eth_dev_callback_register(uint8_t port_id,
1575                         enum rte_eth_event_type event,
1576                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
1577 {
1578         int ret = -1;
1579         struct rte_eth_dev *dev;
1580         struct rte_eth_dev_callback *user_cb = NULL;
1581
1582         if (!cb_fn)
1583                 return -1;
1584         if (port_id >= nb_ports) {
1585                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1586                 return -1;
1587         }
1588         dev = &rte_eth_devices[port_id];
1589         rte_spinlock_lock(&rte_eth_dev_cb_lock);
1590         TAILQ_FOREACH(user_cb, &(dev->callbacks), next) {
1591                 if (user_cb->cb_fn == cb_fn &&
1592                         user_cb->cb_arg == cb_arg &&
1593                         user_cb->event == event) {
1594                         ret = 0;
1595                         goto out;
1596                 }
1597         }
1598         user_cb = rte_malloc("INTR_USER_CALLBACK",
1599                 sizeof(struct rte_eth_dev_callback), 0);
1600         if (!user_cb)
1601                 goto out;
1602         user_cb->cb_fn = cb_fn;
1603         user_cb->cb_arg = cb_arg;
1604         user_cb->event = event;
1605         TAILQ_INSERT_TAIL(&(dev->callbacks), user_cb, next);
1606         ret = 0;
1607
1608 out:
1609         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1610
1611         return ret;
1612 }
1613
1614 int
1615 rte_eth_dev_callback_unregister(uint8_t port_id,
1616                         enum rte_eth_event_type event,
1617                         rte_eth_dev_cb_fn cb_fn, void *cb_arg)
1618 {
1619         int ret = -1;
1620         struct rte_eth_dev *dev;
1621         struct rte_eth_dev_callback *cb_lst = NULL;
1622
1623         if (!cb_fn)
1624                 return -1;
1625         if (port_id >= nb_ports) {
1626                 PMD_DEBUG_TRACE("Invalid port_id=%d\n", port_id);
1627                 return -1;
1628         }
1629         dev = &rte_eth_devices[port_id];
1630         rte_spinlock_lock(&rte_eth_dev_cb_lock);
1631         TAILQ_FOREACH(cb_lst, &(dev->callbacks), next) {
1632                 if (cb_lst->cb_fn != cb_fn || cb_lst->event != event)
1633                         continue;
1634                 if (cb_lst->cb_arg == (void *)-1 ||
1635                                 cb_lst->cb_arg == cb_arg) {
1636                         TAILQ_REMOVE(&(dev->callbacks), cb_lst, next);
1637                         rte_free(cb_lst);
1638                         ret = 0;
1639                 }
1640         }
1641
1642         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1643
1644         return ret;
1645 }
1646
1647 void
1648 _rte_eth_dev_callback_process(struct rte_eth_dev *dev, enum rte_eth_event_type event)
1649 {
1650         struct rte_eth_dev_callback *cb_lst = NULL;
1651         struct rte_eth_dev_callback dev_cb;
1652
1653         rte_spinlock_lock(&rte_eth_dev_cb_lock);
1654         TAILQ_FOREACH(cb_lst, &(dev->callbacks), next) {
1655                 if (cb_lst->cb_fn == NULL || cb_lst->event != event)
1656                         continue;
1657                 dev_cb = *cb_lst;
1658                 rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1659                 dev_cb.cb_fn(dev->data->port_id, dev_cb.event,
1660                                                 dev_cb.cb_arg);
1661                 rte_spinlock_lock(&rte_eth_dev_cb_lock);
1662         }
1663         rte_spinlock_unlock(&rte_eth_dev_cb_lock);
1664 }
1665