1b0fa40ac06e4f508a297eb7d3bc7e7f3f5c3eab
[deb_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 "-Wpedantic"
42 #endif
43 #include <rte_ethdev.h>
44 #include <rte_common.h>
45 #ifdef PEDANTIC
46 #pragma GCC diagnostic error "-Wpedantic"
47 #endif
48
49 #include "mlx5_utils.h"
50 #include "mlx5.h"
51 #include "mlx5_autoconf.h"
52
53 /**
54  * Configure a VLAN filter.
55  *
56  * @param dev
57  *   Pointer to Ethernet device structure.
58  * @param vlan_id
59  *   VLAN ID to filter.
60  * @param on
61  *   Toggle filter.
62  *
63  * @return
64  *   0 on success, errno value on failure.
65  */
66 static int
67 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
68 {
69         struct priv *priv = dev->data->dev_private;
70         unsigned int i;
71
72         DEBUG("%p: %s VLAN filter ID %" PRIu16,
73               (void *)dev, (on ? "enable" : "disable"), vlan_id);
74         assert(priv->vlan_filter_n <= RTE_DIM(priv->vlan_filter));
75         for (i = 0; (i != priv->vlan_filter_n); ++i)
76                 if (priv->vlan_filter[i] == vlan_id)
77                         break;
78         /* Check if there's room for another VLAN filter. */
79         if (i == RTE_DIM(priv->vlan_filter))
80                 return ENOMEM;
81         if (i < priv->vlan_filter_n) {
82                 assert(priv->vlan_filter_n != 0);
83                 /* Enabling an existing VLAN filter has no effect. */
84                 if (on)
85                         return 0;
86                 /* Remove VLAN filter from list. */
87                 --priv->vlan_filter_n;
88                 memmove(&priv->vlan_filter[i],
89                         &priv->vlan_filter[i + 1],
90                         sizeof(priv->vlan_filter[i]) *
91                         (priv->vlan_filter_n - i));
92                 priv->vlan_filter[priv->vlan_filter_n] = 0;
93         } else {
94                 assert(i == priv->vlan_filter_n);
95                 /* Disabling an unknown VLAN filter has no effect. */
96                 if (!on)
97                         return 0;
98                 /* Add new VLAN filter. */
99                 priv->vlan_filter[priv->vlan_filter_n] = vlan_id;
100                 ++priv->vlan_filter_n;
101         }
102         /* Rehash flows in all hash RX queues. */
103         priv_mac_addrs_disable(priv);
104         priv_special_flow_disable_all(priv);
105         return priv_rehash_flows(priv);
106 }
107
108 /**
109  * DPDK callback to configure a VLAN filter.
110  *
111  * @param dev
112  *   Pointer to Ethernet device structure.
113  * @param vlan_id
114  *   VLAN ID to filter.
115  * @param on
116  *   Toggle filter.
117  *
118  * @return
119  *   0 on success, negative errno value on failure.
120  */
121 int
122 mlx5_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
123 {
124         struct priv *priv = dev->data->dev_private;
125         int ret;
126
127         priv_lock(priv);
128         ret = vlan_filter_set(dev, vlan_id, on);
129         priv_unlock(priv);
130         assert(ret >= 0);
131         return -ret;
132 }
133
134 /**
135  * Set/reset VLAN stripping for a specific queue.
136  *
137  * @param priv
138  *   Pointer to private structure.
139  * @param idx
140  *   RX queue index.
141  * @param on
142  *   Enable/disable VLAN stripping.
143  */
144 static void
145 priv_vlan_strip_queue_set(struct priv *priv, uint16_t idx, int on)
146 {
147         struct rxq *rxq = (*priv->rxqs)[idx];
148         struct rxq_ctrl *rxq_ctrl = container_of(rxq, struct rxq_ctrl, rxq);
149         struct ibv_exp_wq_attr mod;
150         uint16_t vlan_offloads =
151                 (on ? IBV_EXP_RECEIVE_WQ_CVLAN_STRIP : 0) |
152                 0;
153         int err;
154
155         DEBUG("set VLAN offloads 0x%x for port %d queue %d",
156               vlan_offloads, rxq->port_id, idx);
157         mod = (struct ibv_exp_wq_attr){
158                 .attr_mask = IBV_EXP_WQ_ATTR_VLAN_OFFLOADS,
159                 .vlan_offloads = vlan_offloads,
160         };
161
162         err = ibv_exp_modify_wq(rxq_ctrl->wq, &mod);
163         if (err) {
164                 ERROR("%p: failed to modified stripping mode: %s",
165                       (void *)priv, strerror(err));
166                 return;
167         }
168
169         /* Update related bits in RX queue. */
170         rxq->vlan_strip = !!on;
171 }
172
173 /**
174  * Callback to set/reset VLAN stripping for a specific queue.
175  *
176  * @param dev
177  *   Pointer to Ethernet device structure.
178  * @param queue
179  *   RX queue index.
180  * @param on
181  *   Enable/disable VLAN stripping.
182  */
183 void
184 mlx5_vlan_strip_queue_set(struct rte_eth_dev *dev, uint16_t queue, int on)
185 {
186         struct priv *priv = dev->data->dev_private;
187
188         /* Validate hw support */
189         if (!priv->hw_vlan_strip) {
190                 ERROR("VLAN stripping is not supported");
191                 return;
192         }
193
194         /* Validate queue number */
195         if (queue >= priv->rxqs_n) {
196                 ERROR("VLAN stripping, invalid queue number %d", queue);
197                 return;
198         }
199
200         priv_lock(priv);
201         priv_vlan_strip_queue_set(priv, queue, on);
202         priv_unlock(priv);
203 }
204
205 /**
206  * Callback to set/reset VLAN offloads for a port.
207  *
208  * @param dev
209  *   Pointer to Ethernet device structure.
210  * @param mask
211  *   VLAN offload bit mask.
212  */
213 void
214 mlx5_vlan_offload_set(struct rte_eth_dev *dev, int mask)
215 {
216         struct priv *priv = dev->data->dev_private;
217         unsigned int i;
218
219         if (mask & ETH_VLAN_STRIP_MASK) {
220                 int hw_vlan_strip = !!dev->data->dev_conf.rxmode.hw_vlan_strip;
221
222                 if (!priv->hw_vlan_strip) {
223                         ERROR("VLAN stripping is not supported");
224                         return;
225                 }
226
227                 /* Run on every RX queue and set/reset VLAN stripping. */
228                 priv_lock(priv);
229                 for (i = 0; (i != priv->rxqs_n); i++)
230                         priv_vlan_strip_queue_set(priv, i, hw_vlan_strip);
231                 priv_unlock(priv);
232         }
233 }