mlx5: refactor Rx code for the new verbs RSS API
[dpdk.git] / drivers / net / mlx5 / mlx5_vlan.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2015 6WIND S.A.
5  *   Copyright 2015 Mellanox.
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 6WIND S.A. 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 #include <stddef.h>
35 #include <errno.h>
36 #include <assert.h>
37 #include <stdint.h>
38
39 /* DPDK headers don't like -pedantic. */
40 #ifdef PEDANTIC
41 #pragma GCC diagnostic ignored "-pedantic"
42 #endif
43 #include <rte_ethdev.h>
44 #include <rte_common.h>
45 #ifdef PEDANTIC
46 #pragma GCC diagnostic error "-pedantic"
47 #endif
48
49 #include "mlx5_utils.h"
50 #include "mlx5.h"
51
52 /**
53  * Configure a VLAN filter.
54  *
55  * @param dev
56  *   Pointer to Ethernet device structure.
57  * @param vlan_id
58  *   VLAN ID to filter.
59  * @param on
60  *   Toggle filter.
61  *
62  * @return
63  *   0 on success, errno value on failure.
64  */
65 static int
66 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
67 {
68         struct priv *priv = dev->data->dev_private;
69         unsigned int i;
70
71         DEBUG("%p: %s VLAN filter ID %" PRIu16,
72               (void *)dev, (on ? "enable" : "disable"), vlan_id);
73         assert(priv->vlan_filter_n <= RTE_DIM(priv->vlan_filter));
74         for (i = 0; (i != priv->vlan_filter_n); ++i)
75                 if (priv->vlan_filter[i] == vlan_id)
76                         break;
77         /* Check if there's room for another VLAN filter. */
78         if (i == RTE_DIM(priv->vlan_filter))
79                 return ENOMEM;
80         if (i < priv->vlan_filter_n) {
81                 assert(priv->vlan_filter_n != 0);
82                 /* Enabling an existing VLAN filter has no effect. */
83                 if (on)
84                         return 0;
85                 /* Remove VLAN filter from list. */
86                 --priv->vlan_filter_n;
87                 memmove(&priv->vlan_filter[i],
88                         &priv->vlan_filter[i + 1],
89                         priv->vlan_filter_n - i);
90                 priv->vlan_filter[priv->vlan_filter_n] = 0;
91         } else {
92                 assert(i == priv->vlan_filter_n);
93                 /* Disabling an unknown VLAN filter has no effect. */
94                 if (!on)
95                         return 0;
96                 /* Add new VLAN filter. */
97                 priv->vlan_filter[priv->vlan_filter_n] = vlan_id;
98                 ++priv->vlan_filter_n;
99         }
100         /* Rehash MAC flows in all hash RX queues. */
101         priv_mac_addrs_disable(priv);
102         return priv_mac_addrs_enable(priv);
103 }
104
105 /**
106  * DPDK callback to configure a VLAN filter.
107  *
108  * @param dev
109  *   Pointer to Ethernet device structure.
110  * @param vlan_id
111  *   VLAN ID to filter.
112  * @param on
113  *   Toggle filter.
114  *
115  * @return
116  *   0 on success, negative errno value on failure.
117  */
118 int
119 mlx5_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
120 {
121         struct priv *priv = dev->data->dev_private;
122         int ret;
123
124         priv_lock(priv);
125         ret = vlan_filter_set(dev, vlan_id, on);
126         priv_unlock(priv);
127         assert(ret >= 0);
128         return -ret;
129 }